2005-03-13 20:23:09 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2005-03-13 20:23:09 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2008-09-07 22:41:17 +02:00
|
|
|
#include "../output_api.h"
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2008-12-08 23:23:30 +01:00
|
|
|
#include <glib.h>
|
2008-10-26 21:58:37 +01:00
|
|
|
#include <AudioUnit/AudioUnit.h>
|
|
|
|
|
2008-12-08 23:23:30 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "osx"
|
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
typedef struct _OsxData {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnit au;
|
2008-12-28 22:09:42 +01:00
|
|
|
GMutex *mutex;
|
|
|
|
GCond *condition;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *buffer;
|
2008-04-12 06:15:52 +02:00
|
|
|
size_t bufferSize;
|
|
|
|
size_t pos;
|
|
|
|
size_t len;
|
2005-03-14 05:30:32 +01:00
|
|
|
int started;
|
2005-03-13 20:23:09 +01:00
|
|
|
} OsxData;
|
|
|
|
|
2008-11-04 11:26:04 +01:00
|
|
|
static OsxData *newOsxData(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-12-08 23:23:30 +01:00
|
|
|
OsxData *ret = g_new(OsxData, 1);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
ret->mutex = g_mutex_new();
|
|
|
|
ret->condition = g_cond_new();
|
2005-03-14 05:30:32 +01:00
|
|
|
|
|
|
|
ret->pos = 0;
|
|
|
|
ret->len = 0;
|
|
|
|
ret->started = 0;
|
2005-03-17 04:08:34 +01:00
|
|
|
ret->buffer = NULL;
|
|
|
|
ret->bufferSize = 0;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-04 11:26:04 +01:00
|
|
|
static bool osx_testDefault(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2005-03-16 05:46:41 +01:00
|
|
|
/*AudioUnit au;
|
2006-07-20 18:02:40 +02:00
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_Output;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
comp = FindNextComponent(NULL, &desc);
|
|
|
|
if(!comp) {
|
|
|
|
ERROR("Unable to open default OS X defice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if(OpenAComponent(comp, &au) != noErr) {
|
|
|
|
ERROR("Unable to open default OS X defice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseComponent(au); */
|
2005-03-13 22:33:55 +01:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2005-03-13 22:33:55 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 11:19:37 +01:00
|
|
|
static void *
|
2009-02-25 18:34:02 +01:00
|
|
|
osx_initDriver(G_GNUC_UNUSED const struct audio_format *audio_format,
|
2009-01-25 16:03:49 +01:00
|
|
|
G_GNUC_UNUSED const struct config_param *param)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
return newOsxData();
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeOsxData(OsxData * od)
|
|
|
|
{
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(od->buffer);
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_free(od->mutex);
|
|
|
|
g_cond_free(od->condition);
|
2009-01-25 18:47:21 +01:00
|
|
|
g_free(od);
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 11:19:37 +01:00
|
|
|
static void osx_finishDriver(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
OsxData *od = data;
|
2005-03-13 20:23:09 +01:00
|
|
|
freeOsxData(od);
|
|
|
|
}
|
|
|
|
|
2008-11-04 11:19:37 +01:00
|
|
|
static void osx_dropBufferedAudio(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
OsxData *od = data;
|
2005-03-17 00:24:07 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(od->mutex);
|
2005-03-17 00:24:07 +01:00
|
|
|
od->len = 0;
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(od->mutex);
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 11:19:37 +01:00
|
|
|
static void osx_closeDevice(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
OsxData *od = data;
|
2005-03-17 00:05:25 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(od->mutex);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (od->len) {
|
2008-12-28 22:09:42 +01:00
|
|
|
g_cond_wait(od->condition, od->mutex);
|
2005-03-17 00:05:25 +01:00
|
|
|
}
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(od->mutex);
|
2005-03-17 00:05:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (od->started) {
|
2005-03-17 00:24:07 +01:00
|
|
|
AudioOutputUnitStop(od->au);
|
|
|
|
od->started = 0;
|
|
|
|
}
|
2005-03-17 00:05:25 +01:00
|
|
|
|
|
|
|
AudioUnitUninitialize(od->au);
|
2008-12-17 15:47:13 +01:00
|
|
|
CloseComponent(od->au);
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 11:26:04 +01:00
|
|
|
static OSStatus
|
|
|
|
osx_render(void *vdata,
|
2009-01-01 18:08:29 +01:00
|
|
|
G_GNUC_UNUSED AudioUnitRenderActionFlags * ioActionFlags,
|
|
|
|
G_GNUC_UNUSED const AudioTimeStamp * inTimeStamp,
|
|
|
|
G_GNUC_UNUSED UInt32 inBusNumber,
|
|
|
|
G_GNUC_UNUSED UInt32 inNumberFrames,
|
2008-11-04 11:26:04 +01:00
|
|
|
AudioBufferList * bufferList)
|
2005-03-14 05:30:32 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
OsxData *od = (OsxData *) vdata;
|
|
|
|
AudioBuffer *buffer = &bufferList->mBuffers[0];
|
2008-04-12 06:15:52 +02:00
|
|
|
size_t bufferSize = buffer->mDataByteSize;
|
|
|
|
size_t bytesToCopy;
|
2008-12-17 15:49:14 +01:00
|
|
|
size_t bytes;
|
2005-03-14 05:30:32 +01:00
|
|
|
int curpos = 0;
|
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(od->mutex);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-12-17 15:48:12 +01:00
|
|
|
bytesToCopy = MIN(od->len, bufferSize);
|
2006-07-20 18:02:40 +02:00
|
|
|
bufferSize = bytesToCopy;
|
|
|
|
od->len -= bytesToCopy;
|
|
|
|
|
2008-12-17 15:49:14 +01:00
|
|
|
bytes = od->bufferSize - od->pos;
|
|
|
|
if (bytesToCopy > bytes) {
|
2008-12-08 23:23:28 +01:00
|
|
|
memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytes);
|
2006-07-20 18:02:40 +02:00
|
|
|
od->pos = 0;
|
|
|
|
curpos += bytes;
|
|
|
|
bytesToCopy -= bytes;
|
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2008-12-08 23:23:28 +01:00
|
|
|
memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytesToCopy);
|
2006-07-20 18:02:40 +02:00
|
|
|
od->pos += bytesToCopy;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (od->pos >= od->bufferSize)
|
|
|
|
od->pos = 0;
|
2009-02-10 20:57:21 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(od->mutex);
|
|
|
|
g_cond_signal(od->condition);
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-03-24 12:45:39 +01:00
|
|
|
buffer->mDataByteSize = bufferSize;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!bufferSize) {
|
2009-02-19 13:33:03 +01:00
|
|
|
g_usleep(1000);
|
2005-03-24 12:45:39 +01:00
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2008-11-04 11:19:37 +01:00
|
|
|
osx_openDevice(void *data, struct audio_format *audioFormat)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
OsxData *od = data;
|
2005-03-16 05:46:41 +01:00
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
AURenderCallbackStruct callback;
|
|
|
|
AudioStreamBasicDescription streamDesc;
|
|
|
|
|
2008-12-08 23:23:37 +01:00
|
|
|
if (audioFormat->bits > 16)
|
|
|
|
audioFormat->bits = 16;
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
comp = FindNextComponent(NULL, &desc);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (comp == 0) {
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("Error finding OS X component\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (OpenAComponent(comp, &od->au) != noErr) {
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("Unable to open OS X component\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (AudioUnitInitialize(od->au) != 0) {
|
2005-03-16 05:46:41 +01:00
|
|
|
CloseComponent(od->au);
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("Unable to initialize OS X audio unit\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
callback.inputProc = osx_render;
|
|
|
|
callback.inputProcRefCon = od;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (AudioUnitSetProperty(od->au, kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&callback, sizeof(callback)) != 0) {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("unable to set callback for OS X audio unit\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
streamDesc.mSampleRate = audioFormat->sample_rate;
|
2005-03-16 05:46:41 +01:00
|
|
|
streamDesc.mFormatID = kAudioFormatLinearPCM;
|
2006-09-09 12:01:25 +02:00
|
|
|
streamDesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2009-01-05 12:40:57 +01:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2006-09-09 12:01:25 +02:00
|
|
|
streamDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
|
|
|
#endif
|
|
|
|
|
2008-10-10 14:41:37 +02:00
|
|
|
streamDesc.mBytesPerPacket = audio_format_frame_size(audioFormat);
|
2005-03-16 05:46:41 +01:00
|
|
|
streamDesc.mFramesPerPacket = 1;
|
|
|
|
streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket;
|
|
|
|
streamDesc.mChannelsPerFrame = audioFormat->channels;
|
|
|
|
streamDesc.mBitsPerChannel = audioFormat->bits;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&streamDesc, sizeof(streamDesc)) != 0) {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("Unable to set format on OS X device\n");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
|
|
|
|
2005-03-17 04:28:05 +01:00
|
|
|
/* create a buffer of 1s */
|
2008-10-10 14:40:54 +02:00
|
|
|
od->bufferSize = (audioFormat->sample_rate) *
|
2008-10-10 14:41:37 +02:00
|
|
|
audio_format_frame_size(audioFormat);
|
2008-12-08 23:23:30 +01:00
|
|
|
od->buffer = g_realloc(od->buffer, od->bufferSize);
|
2005-03-17 04:08:34 +01:00
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
od->pos = 0;
|
|
|
|
od->len = 0;
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
static size_t
|
2009-02-23 09:34:26 +01:00
|
|
|
osx_play(void *data, const void *chunk, size_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-04 11:19:37 +01:00
|
|
|
OsxData *od = data;
|
2009-02-23 09:29:56 +01:00
|
|
|
size_t start, nbytes;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!od->started) {
|
2005-11-19 11:29:20 +01:00
|
|
|
int err;
|
2005-03-17 00:24:07 +01:00
|
|
|
od->started = 1;
|
2005-11-19 11:29:20 +01:00
|
|
|
err = AudioOutputUnitStart(od->au);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (err) {
|
2008-12-08 23:23:30 +01:00
|
|
|
g_warning("unable to start audio output: %i\n", err);
|
2009-02-23 09:29:56 +01:00
|
|
|
return 0;
|
2005-03-17 00:24:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(od->mutex);
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
while (od->len >= od->bufferSize)
|
|
|
|
/* wait for some free space in the buffer */
|
|
|
|
g_cond_wait(od->condition, od->mutex);
|
2005-03-24 12:45:39 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
start = od->pos + od->len;
|
|
|
|
if (start >= od->bufferSize)
|
|
|
|
start -= od->bufferSize;
|
2005-03-24 12:45:39 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
nbytes = start < od->pos
|
|
|
|
? od->pos - start
|
|
|
|
: od->bufferSize - start;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
assert(nbytes > 0);
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
if (nbytes > size)
|
|
|
|
nbytes = size;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2009-02-23 09:34:26 +01:00
|
|
|
memcpy(od->buffer + start, chunk, nbytes);
|
2009-02-23 09:29:56 +01:00
|
|
|
od->len += nbytes;
|
2009-02-10 20:57:21 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(od->mutex);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
return nbytes;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2008-09-08 11:43:38 +02:00
|
|
|
const struct audio_output_plugin osxPlugin = {
|
2008-09-29 15:55:17 +02:00
|
|
|
.name = "osx",
|
|
|
|
.test_default_device = osx_testDefault,
|
|
|
|
.init = osx_initDriver,
|
|
|
|
.finish = osx_finishDriver,
|
|
|
|
.open = osx_openDevice,
|
|
|
|
.play = osx_play,
|
|
|
|
.cancel = osx_dropBufferedAudio,
|
|
|
|
.close = osx_closeDevice,
|
2005-03-13 20:23:09 +01:00
|
|
|
};
|