Commit 0265d3af9b0f61d49a0f4f85f5fa24672bbee539

Ryan C. Gordon 2016-09-04T01:23:55

coreaudio: Move from AudioUnits to AudioQueues. AudioQueues are available in Mac OS X 10.5 and later (and iOS 2.0 and later). Their API is much more clear (and if you don't mind the threading tapdance to get its own CFRunLoop) much easier to use in general for our purposes. As an added benefit: they seemlessly deal with format conversion in ways AudioUnits don't: for example, my MacBook Pro's built-in microphone won't capture at 8000Hz and the AudioUnit version wouldn't resample to hide this fact; the AudioQueue version, however, can handle this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
diff --git a/src/audio/coreaudio/SDL_coreaudio.c b/src/audio/coreaudio/SDL_coreaudio.c
index d9daded..49bfb0e 100644
--- a/src/audio/coreaudio/SDL_coreaudio.c
+++ b/src/audio/coreaudio/SDL_coreaudio.c
@@ -29,6 +29,7 @@
 #include "../SDL_sysaudio.h"
 #include "SDL_coreaudio.h"
 #include "SDL_assert.h"
+#include "../../thread/SDL_systhread.h"
 
 #define DEBUG_COREAUDIO 0
 
@@ -288,42 +289,20 @@ static void update_audio_session()
 }
 
 
-/* The CoreAudio callback */
-static OSStatus
-outputCallback(void *inRefCon,
-               AudioUnitRenderActionFlags * ioActionFlags,
-               const AudioTimeStamp * inTimeStamp,
-               UInt32 inBusNumber, UInt32 inNumberFrames,
-               AudioBufferList * ioData)
+/* The AudioQueue callback */
+static void
+outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
 {
-    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
-    AudioBuffer *abuf;
-    UInt32 remaining, len;
-    void *ptr;
-    UInt32 i;
-
-    /* Only do anything if audio is enabled and not paused */
+    SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
     if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
-        for (i = 0; i < ioData->mNumberBuffers; i++) {
-            abuf = &ioData->mBuffers[i];
-            SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
-        }
-        return 0;
-    }
-
-    /* No SDL conversion should be needed here, ever, since we accept
-       any input format in OpenAudio, and leave the conversion to CoreAudio.
-     */
-    /*
-       SDL_assert(!this->convert.needed);
-       SDL_assert(this->spec.channels == ioData->mNumberChannels);
-     */
+        /* Supply silence if audio is enabled and not paused */
+        SDL_memset(inBuffer->mAudioData, this->spec.silence, inBuffer->mAudioDataBytesCapacity);
+    } else {
+        UInt32 remaining = inBuffer->mAudioDataBytesCapacity;
+        Uint8 *ptr = (Uint8 *) inBuffer->mAudioData;
 
-    for (i = 0; i < ioData->mNumberBuffers; i++) {
-        abuf = &ioData->mBuffers[i];
-        remaining = abuf->mDataByteSize;
-        ptr = abuf->mData;
         while (remaining > 0) {
+            UInt32 len;
             if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
                 /* Generate the data */
                 SDL_LockMutex(this->mixer_lock);
@@ -334,51 +313,39 @@ outputCallback(void *inRefCon,
             }
 
             len = this->hidden->bufferSize - this->hidden->bufferOffset;
-            if (len > remaining)
+            if (len > remaining) {
                 len = remaining;
+            }
             SDL_memcpy(ptr, (char *)this->hidden->buffer +
                        this->hidden->bufferOffset, len);
-            ptr = (char *)ptr + len;
+            ptr = ptr + len;
             remaining -= len;
             this->hidden->bufferOffset += len;
         }
     }
 
-    return noErr;
-}
-
-static OSStatus
-inputCallback(void *inRefCon,
-              AudioUnitRenderActionFlags *ioActionFlags,
-              const AudioTimeStamp *inTimeStamp,
-              UInt32 inBusNumber, UInt32 inNumberFrames,
-              AudioBufferList *ioData)
-{
-    AudioBufferList bufferList;
-    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
-    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
-        return noErr;  /* just drop this if we're not accepting input. */
+    if (!SDL_AtomicGet(&this->hidden->shutdown)) {
+        AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
     }
 
-    bufferList.mNumberBuffers = 1;
-    bufferList.mBuffers[0].mData = NULL;
-
-    const OSStatus err = AudioUnitRender(this->hidden->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
-
-    if (err == noErr) {
-        const AudioBuffer *abuf = &bufferList.mBuffers[0];
-        UInt32 remaining = abuf->mDataByteSize;
-        const Uint8 *ptr = (const Uint8 *) abuf->mData;
+    inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
+}
 
-        /* No SDL conversion should be needed here, ever, since we accept
-           any input format in OpenAudio, and leave the conversion to CoreAudio.
-         */
+static void
+inputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer,
+              const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions,
+              const AudioStreamPacketDescription *inPacketDescs )
+{
+    SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
+    if (SDL_AtomicGet(&this->enabled) && !SDL_AtomicGet(&this->paused)) {  /* ignore unless we're active. */
+        const Uint8 *ptr = (const Uint8 *) inBuffer->mAudioData;
+        UInt32 remaining = inBuffer->mAudioDataByteSize;
         while (remaining > 0) {
             UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset;
-            if (len > remaining)
+            if (len > remaining) {
                 len = remaining;
+            }
 
-            /* !!! FIXME: why are we copying here? just pass the buffer to the callback? */
             SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len);
             ptr += len;
             remaining -= len;
@@ -386,15 +353,16 @@ inputCallback(void *inRefCon,
 
             if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
                 SDL_LockMutex(this->mixer_lock);
-                (*this->spec.callback)(this->spec.userdata,
-                            this->hidden->buffer, this->hidden->bufferSize);
+                (*this->spec.callback)(this->spec.userdata, this->hidden->buffer, this->hidden->bufferSize);
                 SDL_UnlockMutex(this->mixer_lock);
                 this->hidden->bufferOffset = 0;
             }
         }
     }
 
-    return noErr;
+    if (!SDL_AtomicGet(&this->hidden->shutdown)) {
+        AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
+    }
 }
 
 
@@ -439,30 +407,35 @@ device_unplugged(AudioObjectID devid, UInt32 num_addr, const AudioObjectProperty
 static void
 COREAUDIO_CloseDevice(_THIS)
 {
-    const int iscapture = this->iscapture;
+    const SDL_bool iscapture = this->iscapture;
+    int i;
 
-    if (this->hidden->audioUnitOpened) {
-        const AudioUnitElement output_bus = 0;
-        const AudioUnitElement input_bus = 1;
-        const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
-        AURenderCallbackStruct callback;
+/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
+/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
+#if MACOSX_COREAUDIO
+    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
+    AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
+#endif
 
-        #if MACOSX_COREAUDIO
-        /* Unregister our disconnect callback. */
-        AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
-        #endif
+    if (this->hidden->thread) {
+        SDL_AtomicSet(&this->hidden->shutdown, 1);
+        SDL_WaitThread(this->hidden->thread, NULL);
+    }
 
-        /* stop processing the audio unit */
-        AudioOutputUnitStop(this->hidden->audioUnit);
+    if (this->hidden->audioQueue) {
+        for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
+            if (this->hidden->audioBuffer[i]) {
+                AudioQueueFreeBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i]);
+            }
+        }
+        AudioQueueDispose(this->hidden->audioQueue, 1);
+    }
 
-        /* Remove the input callback */
-        SDL_zero(callback);
-        AudioUnitSetProperty(this->hidden->audioUnit,
-                             iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
-                             kAudioUnitScope_Global, bus, &callback, sizeof(callback));
-        AudioComponentInstanceDispose(this->hidden->audioUnit);
+    if (this->hidden->ready_semaphore) {
+        SDL_DestroySemaphore(this->hidden->ready_semaphore);
     }
 
+    SDL_free(this->hidden->thread_error);
     SDL_free(this->hidden->buffer);
     SDL_free(this->hidden);
 
@@ -530,103 +503,44 @@ prepare_device(_THIS, void *handle, int iscapture)
 #endif
 
 static int
-prepare_audiounit(_THIS, void *handle, int iscapture,
-                  const AudioStreamBasicDescription * strdesc)
+prepare_audioqueue(_THIS)
 {
-    OSStatus result = noErr;
-    AURenderCallbackStruct callback;
-    AudioComponentDescription desc;
-    AudioComponent comp = NULL;
-    const AudioUnitElement output_bus = 0;
-    const AudioUnitElement input_bus = 1;
-
-#if MACOSX_COREAUDIO
-    if (!prepare_device(this, handle, iscapture)) {
-        return 0;
-    }
-#endif
-
-    SDL_zero(desc);
-    desc.componentType = kAudioUnitType_Output;
-    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
-
-#if MACOSX_COREAUDIO
-    desc.componentSubType = iscapture ? kAudioUnitSubType_HALOutput : kAudioUnitSubType_DefaultOutput;
-#else
-    desc.componentSubType = kAudioUnitSubType_RemoteIO;
-#endif
-    comp = AudioComponentFindNext(NULL, &desc);
-
-    if (comp == NULL) {
-        SDL_SetError("Couldn't find requested CoreAudio component");
-        return 0;
-    }
-
-    /* Open & initialize the audio unit */
-    result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
-    CHECK_RESULT("AudioComponentInstanceNew");
-
-    this->hidden->audioUnitOpened = SDL_TRUE;
+    const AudioStreamBasicDescription *strdesc = &this->hidden->strdesc;
+    const int iscapture = this->iscapture;
+    OSStatus result;
+    int i;
 
-    if (iscapture) {  /* have to do EnableIO only for capture devices. */
-        UInt32 enable = 1;
-        result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                      kAudioOutputUnitProperty_EnableIO,
-                                      kAudioUnitScope_Input, input_bus,
-                                      &enable, sizeof (enable));
-        CHECK_RESULT
-            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO input bus)");
+    SDL_assert(CFRunLoopGetCurrent() != NULL);
 
-        enable = 0;
-        result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                      kAudioOutputUnitProperty_EnableIO,
-                                      kAudioUnitScope_Output, output_bus,
-                                      &enable, sizeof (enable));
-        CHECK_RESULT
-            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO output bus)");
+    if (iscapture) {
+        result = AudioQueueNewInput(strdesc, inputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
+        CHECK_RESULT("AudioQueueNewInput");
+    } else {
+        result = AudioQueueNewOutput(strdesc, outputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
+        CHECK_RESULT("AudioQueueNewOutput");
     }
 
 #if MACOSX_COREAUDIO
-    /* this is always on the output_bus, even for capture devices. */
-    result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                  kAudioOutputUnitProperty_CurrentDevice,
-                                  kAudioUnitScope_Global, output_bus,
-                                  &this->hidden->deviceID,
-                                  sizeof(AudioDeviceID));
-    CHECK_RESULT
-        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
+{
+    const AudioObjectPropertyAddress prop = {
+        kAudioDevicePropertyDeviceUID,
+        iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
+        kAudioObjectPropertyElementMaster
+    };
+    CFStringRef devuid;
+    UInt32 devuidsize = sizeof (devuid);
+    result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid);
+    CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)");
+    result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize);
+    CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)");
+
+    /* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
+    /* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
+    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
+    AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
+}
 #endif
 
-    /* Set the data format of the audio unit. */
-    result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                  kAudioUnitProperty_StreamFormat,
-                                  iscapture ? kAudioUnitScope_Output : kAudioUnitScope_Input,
-                                  iscapture ? input_bus : output_bus,
-                                  strdesc, sizeof (*strdesc));
-    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
-
-    if (iscapture) {  /* only need to do this for capture devices. */
-        const UInt32 yes = 1;
-        result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                      kAudioUnitProperty_ShouldAllocateBuffer,
-                                      kAudioUnitScope_Output,
-                                      input_bus, &yes, sizeof (yes));
-        CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_ShouldAllocateBuffer)");
-    }
-
-    /* Set the audio callback */
-    SDL_zero(callback);
-    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
-    callback.inputProcRefCon = this;
-
-    result = AudioUnitSetProperty(this->hidden->audioUnit,
-                                  iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
-                                  kAudioUnitScope_Global,
-                                  iscapture ? input_bus : output_bus,
-                                  &callback, sizeof (callback));
-    CHECK_RESULT
-        ("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
-
     /* Calculate the final parameters for this audio specification */
     SDL_CalculateAudioSpec(&this->spec);
 
@@ -640,29 +554,54 @@ prepare_audiounit(_THIS, void *handle, int iscapture,
         return 0;
     }
 
-    result = AudioUnitInitialize(this->hidden->audioUnit);
-    CHECK_RESULT("AudioUnitInitialize");
-
-    /* Finally, start processing of the audio unit */
-    result = AudioOutputUnitStart(this->hidden->audioUnit);
-    CHECK_RESULT("AudioOutputUnitStart");
+    for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
+        result = AudioQueueAllocateBuffer(this->hidden->audioQueue, this->spec.size, &this->hidden->audioBuffer[i]);
+        CHECK_RESULT("AudioQueueAllocateBuffer");
+        SDL_memset(this->hidden->audioBuffer[i]->mAudioData, this->spec.silence, this->hidden->audioBuffer[i]->mAudioDataBytesCapacity);
+        this->hidden->audioBuffer[i]->mAudioDataByteSize = this->hidden->audioBuffer[i]->mAudioDataBytesCapacity;
+        result = AudioQueueEnqueueBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i], 0, NULL);
+        CHECK_RESULT("AudioQueueEnqueueBuffer");
+    }
 
-/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
-/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
-#if MACOSX_COREAUDIO
-    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
-    AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
-#endif
+    result = AudioQueueStart(this->hidden->audioQueue, NULL);
+    CHECK_RESULT("AudioQueueStart");
 
     /* We're running! */
     return 1;
 }
 
+static int
+audioqueue_thread(void *arg)
+{
+    SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
+    const int rc = prepare_audioqueue(this);
+    if (!rc) {
+        this->hidden->thread_error = SDL_strdup(SDL_GetError());
+        SDL_SemPost(this->hidden->ready_semaphore);
+        return 0;
+    }
+
+    /* init was successful, alert parent thread and start running... */
+    SDL_SemPost(this->hidden->ready_semaphore);
+    while (!SDL_AtomicGet(&this->hidden->shutdown)) {
+        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. */
+        AudioQueueStop(this->hidden->audioQueue, 0);
+        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);
+    }
+
+    return 0;
+}
 
 static int
 COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
 {
-    AudioStreamBasicDescription strdesc;
+    AudioStreamBasicDescription *strdesc;
     SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
     int valid_datatype = 0;
 
@@ -674,6 +613,8 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
     }
     SDL_zerop(this->hidden);
 
+    strdesc = &this->hidden->strdesc;
+
     if (iscapture) {
         open_capture_devices++;
     } else {
@@ -682,12 +623,12 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
     update_audio_session();
 
     /* Setup a AudioStreamBasicDescription with the requested format */
-    SDL_zero(strdesc);
-    strdesc.mFormatID = kAudioFormatLinearPCM;
-    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
-    strdesc.mChannelsPerFrame = this->spec.channels;
-    strdesc.mSampleRate = this->spec.freq;
-    strdesc.mFramesPerPacket = 1;
+    SDL_zerop(strdesc);
+    strdesc->mFormatID = kAudioFormatLinearPCM;
+    strdesc->mFormatFlags = kLinearPCMFormatFlagIsPacked;
+    strdesc->mChannelsPerFrame = this->spec.channels;
+    strdesc->mSampleRate = this->spec.freq;
+    strdesc->mFramesPerPacket = 1;
 
     while ((!valid_datatype) && (test_format)) {
         this->spec.format = test_format;
@@ -704,14 +645,14 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
         case AUDIO_F32LSB:
         case AUDIO_F32MSB:
             valid_datatype = 1;
-            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
+            strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
             if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
-                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
+                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
 
             if (SDL_AUDIO_ISFLOAT(this->spec.format))
-                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
+                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat;
             else if (SDL_AUDIO_ISSIGNED(this->spec.format))
-                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
+                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
             break;
         }
     }
@@ -720,16 +661,37 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
         return SDL_SetError("Unsupported audio format");
     }
 
-    strdesc.mBytesPerFrame =
-        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
-    strdesc.mBytesPerPacket =
-        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
+    strdesc->mBytesPerFrame = strdesc->mBitsPerChannel * strdesc->mChannelsPerFrame / 8;
+    strdesc->mBytesPerPacket = strdesc->mBytesPerFrame * strdesc->mFramesPerPacket;
+
+#if MACOSX_COREAUDIO
+    if (!prepare_device(this, handle, iscapture)) {
+        return -1;
+    }
+#endif
+
+    /* This has to init in a new thread so it can get its own CFRunLoop. :/ */
+    SDL_AtomicSet(&this->hidden->shutdown, 0);
+    this->hidden->ready_semaphore = SDL_CreateSemaphore(0);
+    if (!this->hidden->ready_semaphore) {
+        return -1;  /* oh well. */
+    }
+
+    this->hidden->thread = SDL_CreateThreadInternal(audioqueue_thread, "AudioQueue thread", 512 * 1024, this);
+    if (!this->hidden->thread) {
+        return -1;
+    }
+
+    SDL_SemWait(this->hidden->ready_semaphore);
+    SDL_DestroySemaphore(this->hidden->ready_semaphore);
+    this->hidden->ready_semaphore = NULL;
 
-    if (!prepare_audiounit(this, handle, iscapture, &strdesc)) {
-        return -1;      /* prepare_audiounit() will call SDL_SetError()... */
+    if ((this->hidden->thread != NULL) && (this->hidden->thread_error != NULL)) {
+        SDL_SetError("%s", this->hidden->thread_error);
+        return -1;
     }
 
-    return 0;   /* good to go. */
+    return (this->hidden->thread != NULL) ? 0 : -1;
 }
 
 static void
@@ -760,6 +722,7 @@ COREAUDIO_Init(SDL_AudioDriverImpl * impl)
     /* Set category to ambient sound so that other music continues playing.
        You can change this at runtime in your own code if you need different
        behavior.  If this is common, we can add an SDL hint for this.
+       !!! FIXME: do this when a device is opened, and deinitialize when all devices close.
     */
     /* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
     AudioSessionInitialize(NULL, NULL, NULL, nil);
diff --git a/src/audio/coreaudio/SDL_coreaudio.h b/src/audio/coreaudio/SDL_coreaudio.h
index 606dfc2..47029c6 100644
--- a/src/audio/coreaudio/SDL_coreaudio.h
+++ b/src/audio/coreaudio/SDL_coreaudio.h
@@ -32,10 +32,9 @@
 #if MACOSX_COREAUDIO
 #include <CoreAudio/CoreAudio.h>
 #include <CoreServices/CoreServices.h>
-#else
-#include <AudioToolbox/AudioToolbox.h>
 #endif
 
+#include <AudioToolbox/AudioToolbox.h>
 #include <AudioUnit/AudioUnit.h>
 
 /* Hidden "this" pointer for the audio functions */
@@ -43,11 +42,16 @@
 
 struct SDL_PrivateAudioData
 {
-    AudioUnit audioUnit;
-    SDL_bool audioUnitOpened;
+    SDL_Thread *thread;
+    AudioQueueRef audioQueue;
+    AudioQueueBufferRef audioBuffer[2];
     void *buffer;
     UInt32 bufferOffset;
     UInt32 bufferSize;
+    AudioStreamBasicDescription strdesc;
+    SDL_sem *ready_semaphore;
+    char *thread_error;
+    SDL_atomic_t shutdown;
 #if MACOSX_COREAUDIO
     AudioDeviceID deviceID;
 #endif