coreaudio: Move from AudioUnits to AudioQueues.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 04 Sep 2016 01:23:55 -0400
changeset 1030938583cb96c1a
parent 10308 9c5833cbeb8f
child 10310 ac1c949c14b4
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.
src/audio/coreaudio/SDL_coreaudio.c
src/audio/coreaudio/SDL_coreaudio.h
     1.1 --- a/src/audio/coreaudio/SDL_coreaudio.c	Sat Sep 03 00:13:41 2016 -0400
     1.2 +++ b/src/audio/coreaudio/SDL_coreaudio.c	Sun Sep 04 01:23:55 2016 -0400
     1.3 @@ -29,6 +29,7 @@
     1.4  #include "../SDL_sysaudio.h"
     1.5  #include "SDL_coreaudio.h"
     1.6  #include "SDL_assert.h"
     1.7 +#include "../../thread/SDL_systhread.h"
     1.8  
     1.9  #define DEBUG_COREAUDIO 0
    1.10  
    1.11 @@ -288,42 +289,20 @@
    1.12  }
    1.13  
    1.14  
    1.15 -/* The CoreAudio callback */
    1.16 -static OSStatus
    1.17 -outputCallback(void *inRefCon,
    1.18 -               AudioUnitRenderActionFlags * ioActionFlags,
    1.19 -               const AudioTimeStamp * inTimeStamp,
    1.20 -               UInt32 inBusNumber, UInt32 inNumberFrames,
    1.21 -               AudioBufferList * ioData)
    1.22 +/* The AudioQueue callback */
    1.23 +static void
    1.24 +outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
    1.25  {
    1.26 -    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
    1.27 -    AudioBuffer *abuf;
    1.28 -    UInt32 remaining, len;
    1.29 -    void *ptr;
    1.30 -    UInt32 i;
    1.31 -
    1.32 -    /* Only do anything if audio is enabled and not paused */
    1.33 +    SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
    1.34      if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
    1.35 -        for (i = 0; i < ioData->mNumberBuffers; i++) {
    1.36 -            abuf = &ioData->mBuffers[i];
    1.37 -            SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
    1.38 -        }
    1.39 -        return 0;
    1.40 -    }
    1.41 +        /* Supply silence if audio is enabled and not paused */
    1.42 +        SDL_memset(inBuffer->mAudioData, this->spec.silence, inBuffer->mAudioDataBytesCapacity);
    1.43 +    } else {
    1.44 +        UInt32 remaining = inBuffer->mAudioDataBytesCapacity;
    1.45 +        Uint8 *ptr = (Uint8 *) inBuffer->mAudioData;
    1.46  
    1.47 -    /* No SDL conversion should be needed here, ever, since we accept
    1.48 -       any input format in OpenAudio, and leave the conversion to CoreAudio.
    1.49 -     */
    1.50 -    /*
    1.51 -       SDL_assert(!this->convert.needed);
    1.52 -       SDL_assert(this->spec.channels == ioData->mNumberChannels);
    1.53 -     */
    1.54 -
    1.55 -    for (i = 0; i < ioData->mNumberBuffers; i++) {
    1.56 -        abuf = &ioData->mBuffers[i];
    1.57 -        remaining = abuf->mDataByteSize;
    1.58 -        ptr = abuf->mData;
    1.59          while (remaining > 0) {
    1.60 +            UInt32 len;
    1.61              if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
    1.62                  /* Generate the data */
    1.63                  SDL_LockMutex(this->mixer_lock);
    1.64 @@ -334,51 +313,39 @@
    1.65              }
    1.66  
    1.67              len = this->hidden->bufferSize - this->hidden->bufferOffset;
    1.68 -            if (len > remaining)
    1.69 +            if (len > remaining) {
    1.70                  len = remaining;
    1.71 +            }
    1.72              SDL_memcpy(ptr, (char *)this->hidden->buffer +
    1.73                         this->hidden->bufferOffset, len);
    1.74 -            ptr = (char *)ptr + len;
    1.75 +            ptr = ptr + len;
    1.76              remaining -= len;
    1.77              this->hidden->bufferOffset += len;
    1.78          }
    1.79      }
    1.80  
    1.81 -    return noErr;
    1.82 -}
    1.83 -
    1.84 -static OSStatus
    1.85 -inputCallback(void *inRefCon,
    1.86 -              AudioUnitRenderActionFlags *ioActionFlags,
    1.87 -              const AudioTimeStamp *inTimeStamp,
    1.88 -              UInt32 inBusNumber, UInt32 inNumberFrames,
    1.89 -              AudioBufferList *ioData)
    1.90 -{
    1.91 -    AudioBufferList bufferList;
    1.92 -    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
    1.93 -    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
    1.94 -        return noErr;  /* just drop this if we're not accepting input. */
    1.95 +    if (!SDL_AtomicGet(&this->hidden->shutdown)) {
    1.96 +        AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
    1.97      }
    1.98  
    1.99 -    bufferList.mNumberBuffers = 1;
   1.100 -    bufferList.mBuffers[0].mData = NULL;
   1.101 -
   1.102 -    const OSStatus err = AudioUnitRender(this->hidden->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
   1.103 +    inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
   1.104 +}
   1.105  
   1.106 -    if (err == noErr) {
   1.107 -        const AudioBuffer *abuf = &bufferList.mBuffers[0];
   1.108 -        UInt32 remaining = abuf->mDataByteSize;
   1.109 -        const Uint8 *ptr = (const Uint8 *) abuf->mData;
   1.110 -
   1.111 -        /* No SDL conversion should be needed here, ever, since we accept
   1.112 -           any input format in OpenAudio, and leave the conversion to CoreAudio.
   1.113 -         */
   1.114 +static void
   1.115 +inputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer,
   1.116 +              const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions,
   1.117 +              const AudioStreamPacketDescription *inPacketDescs )
   1.118 +{
   1.119 +    SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
   1.120 +    if (SDL_AtomicGet(&this->enabled) && !SDL_AtomicGet(&this->paused)) {  /* ignore unless we're active. */
   1.121 +        const Uint8 *ptr = (const Uint8 *) inBuffer->mAudioData;
   1.122 +        UInt32 remaining = inBuffer->mAudioDataByteSize;
   1.123          while (remaining > 0) {
   1.124              UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset;
   1.125 -            if (len > remaining)
   1.126 +            if (len > remaining) {
   1.127                  len = remaining;
   1.128 +            }
   1.129  
   1.130 -            /* !!! FIXME: why are we copying here? just pass the buffer to the callback? */
   1.131              SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len);
   1.132              ptr += len;
   1.133              remaining -= len;
   1.134 @@ -386,15 +353,16 @@
   1.135  
   1.136              if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
   1.137                  SDL_LockMutex(this->mixer_lock);
   1.138 -                (*this->spec.callback)(this->spec.userdata,
   1.139 -                            this->hidden->buffer, this->hidden->bufferSize);
   1.140 +                (*this->spec.callback)(this->spec.userdata, this->hidden->buffer, this->hidden->bufferSize);
   1.141                  SDL_UnlockMutex(this->mixer_lock);
   1.142                  this->hidden->bufferOffset = 0;
   1.143              }
   1.144          }
   1.145      }
   1.146  
   1.147 -    return noErr;
   1.148 +    if (!SDL_AtomicGet(&this->hidden->shutdown)) {
   1.149 +        AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
   1.150 +    }
   1.151  }
   1.152  
   1.153  
   1.154 @@ -439,30 +407,35 @@
   1.155  static void
   1.156  COREAUDIO_CloseDevice(_THIS)
   1.157  {
   1.158 -    const int iscapture = this->iscapture;
   1.159 -
   1.160 -    if (this->hidden->audioUnitOpened) {
   1.161 -        const AudioUnitElement output_bus = 0;
   1.162 -        const AudioUnitElement input_bus = 1;
   1.163 -        const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
   1.164 -        AURenderCallbackStruct callback;
   1.165 +    const SDL_bool iscapture = this->iscapture;
   1.166 +    int i;
   1.167  
   1.168 -        #if MACOSX_COREAUDIO
   1.169 -        /* Unregister our disconnect callback. */
   1.170 -        AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
   1.171 -        #endif
   1.172 +/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
   1.173 +/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
   1.174 +#if MACOSX_COREAUDIO
   1.175 +    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
   1.176 +    AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
   1.177 +#endif
   1.178  
   1.179 -        /* stop processing the audio unit */
   1.180 -        AudioOutputUnitStop(this->hidden->audioUnit);
   1.181 -
   1.182 -        /* Remove the input callback */
   1.183 -        SDL_zero(callback);
   1.184 -        AudioUnitSetProperty(this->hidden->audioUnit,
   1.185 -                             iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
   1.186 -                             kAudioUnitScope_Global, bus, &callback, sizeof(callback));
   1.187 -        AudioComponentInstanceDispose(this->hidden->audioUnit);
   1.188 +    if (this->hidden->thread) {
   1.189 +        SDL_AtomicSet(&this->hidden->shutdown, 1);
   1.190 +        SDL_WaitThread(this->hidden->thread, NULL);
   1.191      }
   1.192  
   1.193 +    if (this->hidden->audioQueue) {
   1.194 +        for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
   1.195 +            if (this->hidden->audioBuffer[i]) {
   1.196 +                AudioQueueFreeBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i]);
   1.197 +            }
   1.198 +        }
   1.199 +        AudioQueueDispose(this->hidden->audioQueue, 1);
   1.200 +    }
   1.201 +
   1.202 +    if (this->hidden->ready_semaphore) {
   1.203 +        SDL_DestroySemaphore(this->hidden->ready_semaphore);
   1.204 +    }
   1.205 +
   1.206 +    SDL_free(this->hidden->thread_error);
   1.207      SDL_free(this->hidden->buffer);
   1.208      SDL_free(this->hidden);
   1.209  
   1.210 @@ -530,102 +503,43 @@
   1.211  #endif
   1.212  
   1.213  static int
   1.214 -prepare_audiounit(_THIS, void *handle, int iscapture,
   1.215 -                  const AudioStreamBasicDescription * strdesc)
   1.216 +prepare_audioqueue(_THIS)
   1.217  {
   1.218 -    OSStatus result = noErr;
   1.219 -    AURenderCallbackStruct callback;
   1.220 -    AudioComponentDescription desc;
   1.221 -    AudioComponent comp = NULL;
   1.222 -    const AudioUnitElement output_bus = 0;
   1.223 -    const AudioUnitElement input_bus = 1;
   1.224 -
   1.225 -#if MACOSX_COREAUDIO
   1.226 -    if (!prepare_device(this, handle, iscapture)) {
   1.227 -        return 0;
   1.228 -    }
   1.229 -#endif
   1.230 -
   1.231 -    SDL_zero(desc);
   1.232 -    desc.componentType = kAudioUnitType_Output;
   1.233 -    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
   1.234 -
   1.235 -#if MACOSX_COREAUDIO
   1.236 -    desc.componentSubType = iscapture ? kAudioUnitSubType_HALOutput : kAudioUnitSubType_DefaultOutput;
   1.237 -#else
   1.238 -    desc.componentSubType = kAudioUnitSubType_RemoteIO;
   1.239 -#endif
   1.240 -    comp = AudioComponentFindNext(NULL, &desc);
   1.241 +    const AudioStreamBasicDescription *strdesc = &this->hidden->strdesc;
   1.242 +    const int iscapture = this->iscapture;
   1.243 +    OSStatus result;
   1.244 +    int i;
   1.245  
   1.246 -    if (comp == NULL) {
   1.247 -        SDL_SetError("Couldn't find requested CoreAudio component");
   1.248 -        return 0;
   1.249 -    }
   1.250 -
   1.251 -    /* Open & initialize the audio unit */
   1.252 -    result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
   1.253 -    CHECK_RESULT("AudioComponentInstanceNew");
   1.254 -
   1.255 -    this->hidden->audioUnitOpened = SDL_TRUE;
   1.256 +    SDL_assert(CFRunLoopGetCurrent() != NULL);
   1.257  
   1.258 -    if (iscapture) {  /* have to do EnableIO only for capture devices. */
   1.259 -        UInt32 enable = 1;
   1.260 -        result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.261 -                                      kAudioOutputUnitProperty_EnableIO,
   1.262 -                                      kAudioUnitScope_Input, input_bus,
   1.263 -                                      &enable, sizeof (enable));
   1.264 -        CHECK_RESULT
   1.265 -            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO input bus)");
   1.266 -
   1.267 -        enable = 0;
   1.268 -        result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.269 -                                      kAudioOutputUnitProperty_EnableIO,
   1.270 -                                      kAudioUnitScope_Output, output_bus,
   1.271 -                                      &enable, sizeof (enable));
   1.272 -        CHECK_RESULT
   1.273 -            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO output bus)");
   1.274 +    if (iscapture) {
   1.275 +        result = AudioQueueNewInput(strdesc, inputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
   1.276 +        CHECK_RESULT("AudioQueueNewInput");
   1.277 +    } else {
   1.278 +        result = AudioQueueNewOutput(strdesc, outputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
   1.279 +        CHECK_RESULT("AudioQueueNewOutput");
   1.280      }
   1.281  
   1.282  #if MACOSX_COREAUDIO
   1.283 -    /* this is always on the output_bus, even for capture devices. */
   1.284 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.285 -                                  kAudioOutputUnitProperty_CurrentDevice,
   1.286 -                                  kAudioUnitScope_Global, output_bus,
   1.287 -                                  &this->hidden->deviceID,
   1.288 -                                  sizeof(AudioDeviceID));
   1.289 -    CHECK_RESULT
   1.290 -        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
   1.291 -#endif
   1.292 -
   1.293 -    /* Set the data format of the audio unit. */
   1.294 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.295 -                                  kAudioUnitProperty_StreamFormat,
   1.296 -                                  iscapture ? kAudioUnitScope_Output : kAudioUnitScope_Input,
   1.297 -                                  iscapture ? input_bus : output_bus,
   1.298 -                                  strdesc, sizeof (*strdesc));
   1.299 -    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
   1.300 +{
   1.301 +    const AudioObjectPropertyAddress prop = {
   1.302 +        kAudioDevicePropertyDeviceUID,
   1.303 +        iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
   1.304 +        kAudioObjectPropertyElementMaster
   1.305 +    };
   1.306 +    CFStringRef devuid;
   1.307 +    UInt32 devuidsize = sizeof (devuid);
   1.308 +    result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid);
   1.309 +    CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)");
   1.310 +    result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize);
   1.311 +    CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)");
   1.312  
   1.313 -    if (iscapture) {  /* only need to do this for capture devices. */
   1.314 -        const UInt32 yes = 1;
   1.315 -        result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.316 -                                      kAudioUnitProperty_ShouldAllocateBuffer,
   1.317 -                                      kAudioUnitScope_Output,
   1.318 -                                      input_bus, &yes, sizeof (yes));
   1.319 -        CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_ShouldAllocateBuffer)");
   1.320 -    }
   1.321 -
   1.322 -    /* Set the audio callback */
   1.323 -    SDL_zero(callback);
   1.324 -    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
   1.325 -    callback.inputProcRefCon = this;
   1.326 -
   1.327 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.328 -                                  iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
   1.329 -                                  kAudioUnitScope_Global,
   1.330 -                                  iscapture ? input_bus : output_bus,
   1.331 -                                  &callback, sizeof (callback));
   1.332 -    CHECK_RESULT
   1.333 -        ("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
   1.334 +    /* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
   1.335 +    /* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
   1.336 +    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
   1.337 +    AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
   1.338 +}
   1.339 +#endif
   1.340  
   1.341      /* Calculate the final parameters for this audio specification */
   1.342      SDL_CalculateAudioSpec(&this->spec);
   1.343 @@ -640,29 +554,54 @@
   1.344          return 0;
   1.345      }
   1.346  
   1.347 -    result = AudioUnitInitialize(this->hidden->audioUnit);
   1.348 -    CHECK_RESULT("AudioUnitInitialize");
   1.349 -
   1.350 -    /* Finally, start processing of the audio unit */
   1.351 -    result = AudioOutputUnitStart(this->hidden->audioUnit);
   1.352 -    CHECK_RESULT("AudioOutputUnitStart");
   1.353 +    for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
   1.354 +        result = AudioQueueAllocateBuffer(this->hidden->audioQueue, this->spec.size, &this->hidden->audioBuffer[i]);
   1.355 +        CHECK_RESULT("AudioQueueAllocateBuffer");
   1.356 +        SDL_memset(this->hidden->audioBuffer[i]->mAudioData, this->spec.silence, this->hidden->audioBuffer[i]->mAudioDataBytesCapacity);
   1.357 +        this->hidden->audioBuffer[i]->mAudioDataByteSize = this->hidden->audioBuffer[i]->mAudioDataBytesCapacity;
   1.358 +        result = AudioQueueEnqueueBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i], 0, NULL);
   1.359 +        CHECK_RESULT("AudioQueueEnqueueBuffer");
   1.360 +    }
   1.361  
   1.362 -/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
   1.363 -/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
   1.364 -#if MACOSX_COREAUDIO
   1.365 -    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
   1.366 -    AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
   1.367 -#endif
   1.368 +    result = AudioQueueStart(this->hidden->audioQueue, NULL);
   1.369 +    CHECK_RESULT("AudioQueueStart");
   1.370  
   1.371      /* We're running! */
   1.372      return 1;
   1.373  }
   1.374  
   1.375 +static int
   1.376 +audioqueue_thread(void *arg)
   1.377 +{
   1.378 +    SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
   1.379 +    const int rc = prepare_audioqueue(this);
   1.380 +    if (!rc) {
   1.381 +        this->hidden->thread_error = SDL_strdup(SDL_GetError());
   1.382 +        SDL_SemPost(this->hidden->ready_semaphore);
   1.383 +        return 0;
   1.384 +    }
   1.385 +
   1.386 +    /* init was successful, alert parent thread and start running... */
   1.387 +    SDL_SemPost(this->hidden->ready_semaphore);
   1.388 +    while (!SDL_AtomicGet(&this->hidden->shutdown)) {
   1.389 +        CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.10, 1);
   1.390 +    }
   1.391 +
   1.392 +    if (this->iscapture) {  /* just stop immediately for capture devices. */
   1.393 +        AudioQueueStop(this->hidden->audioQueue, 1);
   1.394 +    } else {  /* Drain off any pending playback. */
   1.395 +        AudioQueueStop(this->hidden->audioQueue, 0);
   1.396 +        const CFTimeInterval secs = (((this->spec.size / (SDL_AUDIO_BITSIZE(this->spec.format) / 8)) / this->spec.channels) / ((CFTimeInterval) this->spec.freq)) * 2.0;
   1.397 +        CFRunLoopRunInMode(kCFRunLoopDefaultMode, secs, 0);
   1.398 +    }
   1.399 +
   1.400 +    return 0;
   1.401 +}
   1.402  
   1.403  static int
   1.404  COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
   1.405  {
   1.406 -    AudioStreamBasicDescription strdesc;
   1.407 +    AudioStreamBasicDescription *strdesc;
   1.408      SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   1.409      int valid_datatype = 0;
   1.410  
   1.411 @@ -674,6 +613,8 @@
   1.412      }
   1.413      SDL_zerop(this->hidden);
   1.414  
   1.415 +    strdesc = &this->hidden->strdesc;
   1.416 +
   1.417      if (iscapture) {
   1.418          open_capture_devices++;
   1.419      } else {
   1.420 @@ -682,12 +623,12 @@
   1.421      update_audio_session();
   1.422  
   1.423      /* Setup a AudioStreamBasicDescription with the requested format */
   1.424 -    SDL_zero(strdesc);
   1.425 -    strdesc.mFormatID = kAudioFormatLinearPCM;
   1.426 -    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   1.427 -    strdesc.mChannelsPerFrame = this->spec.channels;
   1.428 -    strdesc.mSampleRate = this->spec.freq;
   1.429 -    strdesc.mFramesPerPacket = 1;
   1.430 +    SDL_zerop(strdesc);
   1.431 +    strdesc->mFormatID = kAudioFormatLinearPCM;
   1.432 +    strdesc->mFormatFlags = kLinearPCMFormatFlagIsPacked;
   1.433 +    strdesc->mChannelsPerFrame = this->spec.channels;
   1.434 +    strdesc->mSampleRate = this->spec.freq;
   1.435 +    strdesc->mFramesPerPacket = 1;
   1.436  
   1.437      while ((!valid_datatype) && (test_format)) {
   1.438          this->spec.format = test_format;
   1.439 @@ -704,14 +645,14 @@
   1.440          case AUDIO_F32LSB:
   1.441          case AUDIO_F32MSB:
   1.442              valid_datatype = 1;
   1.443 -            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   1.444 +            strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   1.445              if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
   1.446 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   1.447 +                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   1.448  
   1.449              if (SDL_AUDIO_ISFLOAT(this->spec.format))
   1.450 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   1.451 +                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   1.452              else if (SDL_AUDIO_ISSIGNED(this->spec.format))
   1.453 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   1.454 +                strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   1.455              break;
   1.456          }
   1.457      }
   1.458 @@ -720,16 +661,37 @@
   1.459          return SDL_SetError("Unsupported audio format");
   1.460      }
   1.461  
   1.462 -    strdesc.mBytesPerFrame =
   1.463 -        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
   1.464 -    strdesc.mBytesPerPacket =
   1.465 -        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
   1.466 +    strdesc->mBytesPerFrame = strdesc->mBitsPerChannel * strdesc->mChannelsPerFrame / 8;
   1.467 +    strdesc->mBytesPerPacket = strdesc->mBytesPerFrame * strdesc->mFramesPerPacket;
   1.468  
   1.469 -    if (!prepare_audiounit(this, handle, iscapture, &strdesc)) {
   1.470 -        return -1;      /* prepare_audiounit() will call SDL_SetError()... */
   1.471 +#if MACOSX_COREAUDIO
   1.472 +    if (!prepare_device(this, handle, iscapture)) {
   1.473 +        return -1;
   1.474 +    }
   1.475 +#endif
   1.476 +
   1.477 +    /* This has to init in a new thread so it can get its own CFRunLoop. :/ */
   1.478 +    SDL_AtomicSet(&this->hidden->shutdown, 0);
   1.479 +    this->hidden->ready_semaphore = SDL_CreateSemaphore(0);
   1.480 +    if (!this->hidden->ready_semaphore) {
   1.481 +        return -1;  /* oh well. */
   1.482      }
   1.483  
   1.484 -    return 0;   /* good to go. */
   1.485 +    this->hidden->thread = SDL_CreateThreadInternal(audioqueue_thread, "AudioQueue thread", 512 * 1024, this);
   1.486 +    if (!this->hidden->thread) {
   1.487 +        return -1;
   1.488 +    }
   1.489 +
   1.490 +    SDL_SemWait(this->hidden->ready_semaphore);
   1.491 +    SDL_DestroySemaphore(this->hidden->ready_semaphore);
   1.492 +    this->hidden->ready_semaphore = NULL;
   1.493 +
   1.494 +    if ((this->hidden->thread != NULL) && (this->hidden->thread_error != NULL)) {
   1.495 +        SDL_SetError("%s", this->hidden->thread_error);
   1.496 +        return -1;
   1.497 +    }
   1.498 +
   1.499 +    return (this->hidden->thread != NULL) ? 0 : -1;
   1.500  }
   1.501  
   1.502  static void
   1.503 @@ -760,6 +722,7 @@
   1.504      /* Set category to ambient sound so that other music continues playing.
   1.505         You can change this at runtime in your own code if you need different
   1.506         behavior.  If this is common, we can add an SDL hint for this.
   1.507 +       !!! FIXME: do this when a device is opened, and deinitialize when all devices close.
   1.508      */
   1.509      /* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
   1.510      AudioSessionInitialize(NULL, NULL, NULL, nil);
     2.1 --- a/src/audio/coreaudio/SDL_coreaudio.h	Sat Sep 03 00:13:41 2016 -0400
     2.2 +++ b/src/audio/coreaudio/SDL_coreaudio.h	Sun Sep 04 01:23:55 2016 -0400
     2.3 @@ -32,10 +32,9 @@
     2.4  #if MACOSX_COREAUDIO
     2.5  #include <CoreAudio/CoreAudio.h>
     2.6  #include <CoreServices/CoreServices.h>
     2.7 -#else
     2.8 -#include <AudioToolbox/AudioToolbox.h>
     2.9  #endif
    2.10  
    2.11 +#include <AudioToolbox/AudioToolbox.h>
    2.12  #include <AudioUnit/AudioUnit.h>
    2.13  
    2.14  /* Hidden "this" pointer for the audio functions */
    2.15 @@ -43,11 +42,16 @@
    2.16  
    2.17  struct SDL_PrivateAudioData
    2.18  {
    2.19 -    AudioUnit audioUnit;
    2.20 -    SDL_bool audioUnitOpened;
    2.21 +    SDL_Thread *thread;
    2.22 +    AudioQueueRef audioQueue;
    2.23 +    AudioQueueBufferRef audioBuffer[2];
    2.24      void *buffer;
    2.25      UInt32 bufferOffset;
    2.26      UInt32 bufferSize;
    2.27 +    AudioStreamBasicDescription strdesc;
    2.28 +    SDL_sem *ready_semaphore;
    2.29 +    char *thread_error;
    2.30 +    SDL_atomic_t shutdown;
    2.31  #if MACOSX_COREAUDIO
    2.32      AudioDeviceID deviceID;
    2.33  #endif