diff options
Diffstat (limited to 'src/output/osx_plugin.c')
-rw-r--r-- | src/output/osx_plugin.c | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/src/output/osx_plugin.c b/src/output/osx_plugin.c index b368ed8a1..3a7c009ed 100644 --- a/src/output/osx_plugin.c +++ b/src/output/osx_plugin.c @@ -18,10 +18,14 @@ #include "../output_api.h" #include "../utils.h" -#include "../log.h" +#include <glib.h> +#include <pthread.h> #include <AudioUnit/AudioUnit.h> +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "osx" + typedef struct _OsxData { AudioUnit au; pthread_mutex_t mutex; @@ -35,7 +39,7 @@ typedef struct _OsxData { static OsxData *newOsxData(void) { - OsxData *ret = xmalloc(sizeof(OsxData)); + OsxData *ret = g_new(OsxData, 1); pthread_mutex_init(&ret->mutex, NULL); pthread_cond_init(&ret->condition, NULL); @@ -124,8 +128,8 @@ static void osx_closeDevice(void *data) od->started = 0; } - CloseComponent(od->au); AudioUnitUninitialize(od->au); + CloseComponent(od->au); } static OSStatus @@ -139,6 +143,7 @@ osx_render(void *vdata, AudioBuffer *buffer = &bufferList->mBuffers[0]; size_t bufferSize = buffer->mDataByteSize; size_t bytesToCopy; + size_t bytes; int curpos = 0; /*DEBUG("osx_render: enter : %i\n", (int)bufferList->mNumberBuffers); @@ -177,21 +182,20 @@ osx_render(void *vdata, } */ - bytesToCopy = od->len < bufferSize ? od->len : bufferSize; + bytesToCopy = MIN(od->len, bufferSize); bufferSize = bytesToCopy; od->len -= bytesToCopy; - if (od->pos + bytesToCopy > od->bufferSize) { - size_t bytes = od->bufferSize - od->pos; - memcpy(buffer->mData + curpos, od->buffer + od->pos, bytes); + bytes = od->bufferSize - od->pos; + if (bytesToCopy > bytes) { + memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytes); od->pos = 0; curpos += bytes; bytesToCopy -= bytes; } - memcpy(buffer->mData + curpos, od->buffer + od->pos, bytesToCopy); + memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytesToCopy); od->pos += bytesToCopy; - curpos += bytesToCopy; if (od->pos >= od->bufferSize) od->pos = 0; @@ -219,6 +223,9 @@ osx_openDevice(void *data, struct audio_format *audioFormat) AURenderCallbackStruct callback; AudioStreamBasicDescription streamDesc; + if (audioFormat->bits > 16) + audioFormat->bits = 16; + desc.componentType = kAudioUnitType_Output; desc.componentSubType = kAudioUnitSubType_DefaultOutput; desc.componentManufacturer = kAudioUnitManufacturer_Apple; @@ -227,18 +234,18 @@ osx_openDevice(void *data, struct audio_format *audioFormat) comp = FindNextComponent(NULL, &desc); if (comp == 0) { - ERROR("Error finding OS X component\n"); + g_warning("Error finding OS X component\n"); return false; } if (OpenAComponent(comp, &od->au) != noErr) { - ERROR("Unable to open OS X component\n"); + g_warning("Unable to open OS X component\n"); return false; } if (AudioUnitInitialize(od->au) != 0) { CloseComponent(od->au); - ERROR("Unable to initialize OS X audio unit\n"); + g_warning("Unable to initialize OS X audio unit\n"); return false; } @@ -250,7 +257,7 @@ osx_openDevice(void *data, struct audio_format *audioFormat) &callback, sizeof(callback)) != 0) { AudioUnitUninitialize(od->au); CloseComponent(od->au); - ERROR("unable to set callback for OS X audio unit\n"); + g_warning("unable to set callback for OS X audio unit\n"); return false; } @@ -272,14 +279,14 @@ osx_openDevice(void *data, struct audio_format *audioFormat) &streamDesc, sizeof(streamDesc)) != 0) { AudioUnitUninitialize(od->au); CloseComponent(od->au); - ERROR("Unable to set format on OS X device\n"); + g_warning("Unable to set format on OS X device\n"); return false; } /* create a buffer of 1s */ od->bufferSize = (audioFormat->sample_rate) * audio_format_frame_size(audioFormat); - od->buffer = xrealloc(od->buffer, od->bufferSize); + od->buffer = g_realloc(od->buffer, od->bufferSize); od->pos = 0; od->len = 0; @@ -292,6 +299,7 @@ osx_play(void *data, const char *playChunk, size_t size) { OsxData *od = data; size_t bytesToCopy; + size_t bytes; size_t curpos; /* DEBUG("osx_play: enter\n"); */ @@ -301,7 +309,7 @@ osx_play(void *data, const char *playChunk, size_t size) od->started = 1; err = AudioOutputUnitStart(od->au); if (err) { - ERROR("unable to start audio output: %i\n", err); + g_warning("unable to start audio output: %i\n", err); return false; } } @@ -314,20 +322,18 @@ osx_play(void *data, const char *playChunk, size_t size) if (curpos >= od->bufferSize) curpos -= od->bufferSize; - bytesToCopy = od->bufferSize < size ? od->bufferSize : size; + bytesToCopy = MIN(od->bufferSize, size); while (od->len > od->bufferSize - bytesToCopy) { /* DEBUG("osx_play: wait\n"); */ pthread_cond_wait(&od->condition, &od->mutex); } - bytesToCopy = od->bufferSize - od->len; - bytesToCopy = bytesToCopy < size ? bytesToCopy : size; size -= bytesToCopy; od->len += bytesToCopy; - if (curpos + bytesToCopy > od->bufferSize) { - size_t bytes = od->bufferSize - curpos; + bytes = od->bufferSize - curpos; + if (bytesToCopy > bytes) { memcpy(od->buffer + curpos, playChunk, bytes); curpos = 0; playChunk += bytes; @@ -335,7 +341,6 @@ osx_play(void *data, const char *playChunk, size_t size) } memcpy(od->buffer + curpos, playChunk, bytesToCopy); - curpos += bytesToCopy; playChunk += bytesToCopy; } |