瀏覽代碼

coreaudio: changed device close procedure to prevent long hangs in some cases.

The audioqueue thread needs to keep running, and processing the CFRunLoop
until the AudioQueue is disposed of, otherwise CoreAudio will hang waiting for
final data to feed the device.

At least, I think this is how it all works. It definitely fixes the bug here!

Since AudioQueueDispose() calls AudioQueueStop() internally, there's no need
for our thread to handle this, either, which is good because the AudioQueue
would be disposed by this point. So now the AudioQueue is disposed first, and
then our thread is joined, and everything works out okay.

Just in case, we mark the device "paused" before setting everything in motion,
so any further callbacks from CoreAudio will write silence and not fire the
app's audio callback again.

Fixes Bugzilla #3868.
Ryan C. Gordon 8 年之前
父節點
當前提交
fa15674134
共有 1 個文件被更改,包括 8 次插入8 次删除
  1. 8 8
      src/audio/coreaudio/SDL_coreaudio.m

+ 8 - 8
src/audio/coreaudio/SDL_coreaudio.m

@@ -541,15 +541,18 @@ COREAUDIO_CloseDevice(_THIS)
     update_audio_session(this, SDL_FALSE);
 #endif
 
-    if (this->hidden->thread) {
-        SDL_AtomicSet(&this->hidden->shutdown, 1);
-        SDL_WaitThread(this->hidden->thread, NULL);
-    }
+    /* if callback fires again, feed silence; don't call into the app. */
+    SDL_AtomicSet(&this->paused, 1);
 
     if (this->hidden->audioQueue) {
         AudioQueueDispose(this->hidden->audioQueue, 1);
     }
 
+    if (this->hidden->thread) {
+        SDL_AtomicSet(&this->hidden->shutdown, 1);
+        SDL_WaitThread(this->hidden->thread, NULL);
+    }
+
     if (this->hidden->ready_semaphore) {
         SDL_DestroySemaphore(this->hidden->ready_semaphore);
     }
@@ -731,12 +734,9 @@ audioqueue_thread(void *arg)
         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.10, 1);
     }
 
-    if (this->iscapture) {  /* just stop immediately for capture devices. */
-        AudioQueueStop(this->hidden->audioQueue, 1);
-    } else {  /* Drain off any pending playback. */
+    if (!this->iscapture) {  /* Drain off any pending playback. */
         const CFTimeInterval secs = (((this->spec.size / (SDL_AUDIO_BITSIZE(this->spec.format) / 8)) / this->spec.channels) / ((CFTimeInterval) this->spec.freq)) * 2.0;
         CFRunLoopRunInMode(kCFRunLoopDefaultMode, secs, 0);
-        AudioQueueStop(this->hidden->audioQueue, 0);
     }
 
     return 0;