From 2c2fa0ce30c6b59b7e37acf71a89291cd81e4ed6 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 17 Apr 2015 10:54:03 +0200 Subject: [PATCH] (CoreAudio) Use straight pthread again --- audio/drivers/coreaudio.c | 96 ++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 53 deletions(-) diff --git a/audio/drivers/coreaudio.c b/audio/drivers/coreaudio.c index 3debd7ce02..fd025f171e 100644 --- a/audio/drivers/coreaudio.c +++ b/audio/drivers/coreaudio.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include #ifdef OSX #include @@ -33,8 +33,8 @@ typedef struct coreaudio { - slock_t *lock; - scond_t *cond; + pthread_mutex_t lock; + pthread_cond_t cond; #ifdef OSX_PPC ComponentInstance dev; @@ -71,8 +71,8 @@ static void coreaudio_free(void *data) if (dev->buffer) fifo_free(dev->buffer); - slock_free(dev->lock); - scond_free(dev->cond); + pthread_mutex_destroy(&dev->lock); + pthread_cond_destroy(&dev->cond); free(dev); } @@ -82,8 +82,8 @@ static OSStatus audio_write_cb(void *userdata, const AudioTimeStamp *time_stamp, UInt32 bus_number, UInt32 number_frames, AudioBufferList *io_data) { + void *outbuf; unsigned write_avail; - void *outbuf = NULL; coreaudio_t *dev = (coreaudio_t*)userdata; (void)time_stamp; @@ -96,9 +96,9 @@ static OSStatus audio_write_cb(void *userdata, return noErr; write_avail = io_data->mBuffers[0].mDataByteSize; - outbuf = io_data->mBuffers[0].mData; + outbuf = io_data->mBuffers[0].mData; - slock_lock(dev->lock); + pthread_mutex_lock(&dev->lock); if (fifo_read_avail(dev->buffer) < write_avail) { @@ -107,15 +107,16 @@ static OSStatus audio_write_cb(void *userdata, /* Seems to be needed. */ memset(outbuf, 0, write_avail); - goto end; + pthread_mutex_unlock(&dev->lock); + + /* Technically possible to deadlock without. */ + pthread_cond_signal(&dev->cond); + return noErr; } fifo_read(dev->buffer, outbuf, write_avail); - -end: - slock_unlock(dev->lock); - scond_signal(dev->cond); - + pthread_mutex_unlock(&dev->lock); + pthread_cond_signal(&dev->cond); return noErr; } @@ -123,8 +124,7 @@ end: static void choose_output_device(coreaudio_t *dev, const char* device) { unsigned i; - UInt32 size = 0, deviceCount; - AudioDeviceID *devices = NULL; + AudioDeviceID *devices; AudioObjectPropertyAddress propaddr = { kAudioHardwarePropertyDevices, @@ -132,23 +132,22 @@ static void choose_output_device(coreaudio_t *dev, const char* device) kAudioObjectPropertyElementMaster }; + UInt32 size = 0, deviceCount; + if (AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propaddr, 0, 0, &size) != noErr) return; - deviceCount = size / sizeof(AudioDeviceID); - devices = (AudioDeviceID*)malloc(size); + deviceCount = size / sizeof(AudioDeviceID); + devices = (AudioDeviceID*)malloc(size); - if (!devices) - goto done; - - if (AudioObjectGetPropertyData(kAudioObjectSystemObject, + if (!devices || AudioObjectGetPropertyData(kAudioObjectSystemObject, &propaddr, 0, 0, &size, devices) != noErr) goto done; - propaddr.mScope = kAudioDevicePropertyScopeOutput; + propaddr.mScope = kAudioDevicePropertyScopeOutput; propaddr.mSelector = kAudioDevicePropertyDeviceName; - size = 1024; + size = 1024; for (i = 0; i < deviceCount; i ++) { @@ -196,12 +195,12 @@ static void *coreaudio_init(const char *device, AudioStreamBasicDescription stream_desc = {0}; static bool session_initialized = false; coreaudio_t *dev = NULL; - settings_t *settings = config_get_ptr(); #ifdef OSX_PPC ComponentDescription desc = {0}; #else AudioComponentDescription desc = {0}; #endif + settings_t *settings = config_get_ptr(); (void)session_initialized; (void)device; @@ -210,22 +209,8 @@ static void *coreaudio_init(const char *device, if (!dev) return NULL; - dev->lock = slock_new(); - - if (!dev->lock) - { - free(dev); - return NULL; - } - - dev->cond = scond_new(); - - if (!dev->cond) - { - free(dev->lock); - free(dev); - return NULL; - } + pthread_mutex_init(&dev->lock, NULL); + pthread_cond_init(&dev->cond, NULL); #ifdef IOS if (!session_initialized) @@ -284,8 +269,7 @@ static void *coreaudio_init(const char *device, goto error; /* Check returned audio format. */ - i_size = sizeof(real_desc); - + i_size = sizeof(real_desc); if (AudioUnitGetProperty(dev->dev, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &real_desc, &i_size) != noErr) goto error; @@ -351,40 +335,46 @@ static ssize_t coreaudio_write(void *data, const void *buf_, size_t size) size_t written = 0; #ifdef IOS + struct timespec timeout; struct timeval time; + gettimeofday(&time, 0); + + memset(&timeout, 0, sizeof(timeout)); + timeout.tv_sec = time.tv_sec + 3; + timeout.tv_nsec = time.tv_usec * 1000; #endif while (!g_interrupted && size > 0) { size_t write_avail; - slock_lock(dev->lock); + pthread_mutex_lock(&dev->lock); write_avail = fifo_write_avail(dev->buffer); if (write_avail > size) write_avail = size; fifo_write(dev->buffer, buf, write_avail); - buf += write_avail; + buf += write_avail; written += write_avail; - size -= write_avail; + size -= write_avail; if (dev->nonblock) { - slock_unlock(dev->lock); + pthread_mutex_unlock(&dev->lock); break; } #ifdef IOS - if (write_avail == 0 && !scond_wait_timeout( - dev->cond, dev->lock, time.tv_usec)) + if (write_avail == 0 && pthread_cond_timedwait( + &dev->cond, &dev->lock, &timeout) == ETIMEDOUT) g_interrupted = true; #else if (write_avail == 0) - scond_wait(dev->cond, dev->lock); + pthread_cond_wait(&dev->cond, &dev->lock); #endif - slock_unlock(dev->lock); + pthread_mutex_unlock(&dev->lock); } return written; @@ -434,9 +424,9 @@ static size_t coreaudio_write_avail(void *data) size_t avail; coreaudio_t *dev = (coreaudio_t*)data; - slock_lock(dev->lock); + pthread_mutex_lock(&dev->lock); avail = fifo_write_avail(dev->buffer); - slock_unlock(dev->lock); + pthread_mutex_unlock(&dev->lock); return avail; }