Skip to content

Commit e7a765e

Browse files
committed
oto: use a longer retry budget for AVAudioSessionErrorCodeCannotStartPlaying
This error indicates the app is not active (e.g. backgrounded or in the middle of an interruption), so it can take noticeably longer to clear than the other transient AudioQueueStart errors. Bump the retry budget for this code to ~10s in both newContext and resume, while keeping the shorter ~2.8s budget for Unspecified and CannotInterruptOthers. newContext now also uses sleepTime() instead of a fixed 10ms sleep, resolving the prior TODO. Updates #93
1 parent b68f3d2 commit e7a765e

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

driver_darwin.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ func newContext(sampleRate int, channelCount int, format mux.Format, bufferSizeI
141141
var retryCount int
142142
try:
143143
if osstatus := _AudioQueueStart(c.audioQueue, nil); osstatus != noErr {
144-
if (osstatus == avAudioSessionErrorCodeCannotStartPlaying ||
145-
osstatus == avAudioSessionErrorCodeUnspecified) &&
146-
retryCount < 100 {
147-
// TODO: use sleepTime() after investigating when this error happens.
148-
time.Sleep(10 * time.Millisecond)
144+
maxRetry := 0
145+
switch osstatus {
146+
case avAudioSessionErrorCodeCannotStartPlaying:
147+
maxRetry = 100
148+
case avAudioSessionErrorCodeUnspecified:
149+
maxRetry = 30
150+
}
151+
if retryCount < maxRetry {
152+
time.Sleep(sleepTime(retryCount))
149153
retryCount++
150154
goto try
151155
}
@@ -255,10 +259,15 @@ func (c *context) resume() error {
255259
var retryCount int
256260
try:
257261
if osstatus := _AudioQueueStart(c.audioQueue, nil); osstatus != noErr {
258-
if (osstatus == avAudioSessionErrorCodeCannotStartPlaying ||
259-
osstatus == avAudioSessionErrorCodeCannotInterruptOthers ||
260-
osstatus == avAudioSessionErrorCodeUnspecified) &&
261-
retryCount < 30 {
262+
maxRetry := 0
263+
switch osstatus {
264+
case avAudioSessionErrorCodeCannotStartPlaying:
265+
maxRetry = 100
266+
case avAudioSessionErrorCodeCannotInterruptOthers,
267+
avAudioSessionErrorCodeUnspecified:
268+
maxRetry = 30
269+
}
270+
if retryCount < maxRetry {
262271
// It is uncertain that this error is temporary or not. Then let's use exponential-time sleeping.
263272
time.Sleep(sleepTime(retryCount))
264273
retryCount++

0 commit comments

Comments
 (0)