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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../audioOutput.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_OSX
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
#include <AudioUnit/AudioUnit.h>
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
#include "../log.h"
|
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
typedef struct _OsxData {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnit au;
|
2006-07-20 18:02:40 +02:00
|
|
|
pthread_mutex_t mutex;
|
2005-03-24 12:45:39 +01:00
|
|
|
pthread_cond_t condition;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *buffer;
|
2005-03-17 04:08:34 +01:00
|
|
|
int bufferSize;
|
2005-03-14 05:30:32 +01:00
|
|
|
int pos;
|
|
|
|
int len;
|
|
|
|
int started;
|
2005-03-13 20:23:09 +01:00
|
|
|
} OsxData;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OsxData *newOsxData()
|
|
|
|
{
|
2006-08-26 08:25:57 +02:00
|
|
|
OsxData *ret = xmalloc(sizeof(OsxData));
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_mutex_init(&ret->mutex, NULL);
|
2005-03-24 12:45:39 +01:00
|
|
|
pthread_cond_init(&ret->condition, NULL);
|
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;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int osx_testDefault()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int osx_initDriver(AudioOutput * audioOutput, ConfigParam * param)
|
|
|
|
{
|
|
|
|
OsxData *od = newOsxData();
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
audioOutput->data = od;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void freeOsxData(OsxData * od)
|
|
|
|
{
|
|
|
|
if (od->buffer)
|
|
|
|
free(od->buffer);
|
2005-10-02 16:52:13 +02:00
|
|
|
pthread_mutex_destroy(&od->mutex);
|
|
|
|
pthread_cond_destroy(&od->condition);
|
2005-03-13 20:23:09 +01:00
|
|
|
free(od);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void osx_finishDriver(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
OsxData *od = (OsxData *) audioOutput->data;
|
2005-03-13 20:23:09 +01:00
|
|
|
freeOsxData(od);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void osx_dropBufferedAudio(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
OsxData *od = (OsxData *) audioOutput->data;
|
2005-03-17 00:24:07 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
od->len = 0;
|
|
|
|
pthread_mutex_unlock(&od->mutex);
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void osx_closeDevice(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
OsxData *od = (OsxData *) audioOutput->data;
|
2005-03-17 00:05:25 +01:00
|
|
|
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (od->len) {
|
2005-03-24 12:45:39 +01:00
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
2005-03-17 00:05:25 +01:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&od->mutex);
|
|
|
|
|
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
|
|
|
|
|
|
|
CloseComponent(od->au);
|
|
|
|
AudioUnitUninitialize(od->au);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
audioOutput->open = 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static OSStatus osx_render(void *vdata,
|
|
|
|
AudioUnitRenderActionFlags * ioActionFlags,
|
|
|
|
const AudioTimeStamp * inTimeStamp,
|
|
|
|
UInt32 inBusNumber, UInt32 inNumberFrames,
|
|
|
|
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];
|
2005-03-16 05:46:41 +01:00
|
|
|
int bufferSize = buffer->mDataByteSize;
|
|
|
|
int bytesToCopy;
|
2005-03-14 05:30:32 +01:00
|
|
|
int curpos = 0;
|
|
|
|
|
2005-03-24 12:45:39 +01:00
|
|
|
/*DEBUG("osx_render: enter : %i\n", (int)bufferList->mNumberBuffers);
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("osx_render: ioActionFlags: %p\n", ioActionFlags);
|
|
|
|
if(ioActionFlags) {
|
|
|
|
if(*ioActionFlags & kAudioUnitRenderAction_PreRender) {
|
|
|
|
DEBUG("prerender\n");
|
|
|
|
}
|
|
|
|
if(*ioActionFlags & kAudioUnitRenderAction_PostRender) {
|
|
|
|
DEBUG("post render\n");
|
|
|
|
}
|
|
|
|
if(*ioActionFlags & kAudioUnitRenderAction_OutputIsSilence) {
|
|
|
|
DEBUG("post render\n");
|
|
|
|
}
|
|
|
|
if(*ioActionFlags & kAudioOfflineUnitRenderAction_Preflight) {
|
|
|
|
DEBUG("prefilight\n");
|
|
|
|
}
|
|
|
|
if(*ioActionFlags & kAudioOfflineUnitRenderAction_Render) {
|
|
|
|
DEBUG("render\n");
|
|
|
|
}
|
|
|
|
if(*ioActionFlags & kAudioOfflineUnitRenderAction_Complete) {
|
|
|
|
DEBUG("complete\n");
|
|
|
|
}
|
|
|
|
} */
|
2005-03-24 12:45:39 +01:00
|
|
|
|
2005-11-19 11:52:47 +01:00
|
|
|
/* while(bufferSize) {
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("osx_render: lock\n"); */
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
/*
|
|
|
|
DEBUG("%i:%i\n", bufferSize, od->len);
|
|
|
|
while(od->go && od->len < bufferSize &&
|
|
|
|
od->len < od->bufferSize)
|
|
|
|
{
|
|
|
|
DEBUG("osx_render: wait\n");
|
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
bytesToCopy = od->len < bufferSize ? od->len : bufferSize;
|
|
|
|
bufferSize = bytesToCopy;
|
|
|
|
od->len -= bytesToCopy;
|
|
|
|
|
|
|
|
if (od->pos + bytesToCopy > od->bufferSize) {
|
|
|
|
int bytes = od->bufferSize - od->pos;
|
|
|
|
memcpy(buffer->mData + curpos, od->buffer + od->pos, bytes);
|
|
|
|
od->pos = 0;
|
|
|
|
curpos += bytes;
|
|
|
|
bytesToCopy -= bytes;
|
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
memcpy(buffer->mData + curpos, od->buffer + od->pos, bytesToCopy);
|
|
|
|
od->pos += bytesToCopy;
|
|
|
|
curpos += 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;
|
|
|
|
/* DEBUG("osx_render: unlock\n"); */
|
|
|
|
pthread_mutex_unlock(&od->mutex);
|
|
|
|
pthread_cond_signal(&od->condition);
|
2005-11-19 11:52:47 +01:00
|
|
|
/* } */
|
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) {
|
2005-03-24 12:45:39 +01:00
|
|
|
my_usleep(1000);
|
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_render: leave\n"); */
|
2005-03-14 05:30:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int osx_openDevice(AudioOutput * audioOutput)
|
|
|
|
{
|
|
|
|
OsxData *od = (OsxData *) audioOutput->data;
|
2005-03-16 05:46:41 +01:00
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
AURenderCallbackStruct callback;
|
2006-07-20 18:02:40 +02:00
|
|
|
AudioFormat *audioFormat = &audioOutput->outAudioFormat;
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioStreamBasicDescription streamDesc;
|
|
|
|
|
|
|
|
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) {
|
2005-03-16 05:46:41 +01:00
|
|
|
ERROR("Error finding OS X component\n");
|
2005-03-14 05:30:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (OpenAComponent(comp, &od->au) != noErr) {
|
2005-03-16 05:46:41 +01:00
|
|
|
ERROR("Unable to open OS X component\n");
|
2005-03-14 05:30:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (AudioUnitInitialize(od->au) != 0) {
|
2005-03-16 05:46:41 +01:00
|
|
|
CloseComponent(od->au);
|
2006-08-11 23:50:56 +02:00
|
|
|
ERROR("Unable to initialize OS X audio unit\n");
|
2005-03-16 05:46:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2006-08-11 23:50:56 +02:00
|
|
|
ERROR("unable to set callback for OS X audio unit\n");
|
2005-03-16 05:46:41 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
streamDesc.mSampleRate = audioFormat->sampleRate;
|
|
|
|
streamDesc.mFormatID = kAudioFormatLinearPCM;
|
2006-09-09 12:01:25 +02:00
|
|
|
streamDesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
streamDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
|
|
|
#endif
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
streamDesc.mBytesPerPacket =
|
|
|
|
audioFormat->channels * audioFormat->bits / 8;
|
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);
|
|
|
|
ERROR("Unable to set format on OS X device\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-17 04:28:05 +01:00
|
|
|
/* create a buffer of 1s */
|
|
|
|
od->bufferSize = (audioFormat->sampleRate) *
|
2006-07-20 18:02:40 +02:00
|
|
|
(audioFormat->bits >> 3) * (audioFormat->channels);
|
2006-08-26 08:25:57 +02:00
|
|
|
od->buffer = xrealloc(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
|
|
|
|
|
|
|
audioOutput->open = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int osx_play(AudioOutput * audioOutput, char *playChunk, int size)
|
|
|
|
{
|
|
|
|
OsxData *od = (OsxData *) audioOutput->data;
|
2005-03-16 05:46:41 +01:00
|
|
|
int bytesToCopy;
|
|
|
|
int curpos;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_play: enter\n"); */
|
2005-03-24 12:45:39 +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) {
|
2005-03-17 00:24:07 +01:00
|
|
|
ERROR("unable to start audio output: %i\n", err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (size) {
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_play: lock\n"); */
|
2006-07-20 18:02:40 +02:00
|
|
|
curpos = od->pos + od->len;
|
|
|
|
if (curpos >= od->bufferSize)
|
|
|
|
curpos -= od->bufferSize;
|
2005-03-24 12:45:39 +01:00
|
|
|
|
|
|
|
bytesToCopy = od->bufferSize < size ? od->bufferSize : size;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (od->len > od->bufferSize - bytesToCopy) {
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_play: wait\n"); */
|
2005-03-24 12:45:39 +01:00
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2005-03-17 04:08:34 +01:00
|
|
|
bytesToCopy = od->bufferSize - od->len;
|
2005-03-16 05:46:41 +01:00
|
|
|
bytesToCopy = bytesToCopy < size ? bytesToCopy : size;
|
|
|
|
size -= bytesToCopy;
|
|
|
|
od->len += bytesToCopy;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (curpos + bytesToCopy > od->bufferSize) {
|
|
|
|
int bytes = od->bufferSize - curpos;
|
|
|
|
memcpy(od->buffer + curpos, playChunk, bytes);
|
2005-03-16 05:46:41 +01:00
|
|
|
curpos = 0;
|
|
|
|
playChunk += bytes;
|
|
|
|
bytesToCopy -= bytes;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
memcpy(od->buffer + curpos, playChunk, bytesToCopy);
|
2005-03-16 05:46:41 +01:00
|
|
|
curpos += bytesToCopy;
|
|
|
|
playChunk += bytesToCopy;
|
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_play: unlock\n"); */
|
2005-03-16 05:46:41 +01:00
|
|
|
pthread_mutex_unlock(&od->mutex);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2005-11-19 11:52:47 +01:00
|
|
|
/* DEBUG("osx_play: leave\n"); */
|
2005-03-13 20:23:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
AudioOutputPlugin osxPlugin = {
|
2005-03-13 20:23:09 +01:00
|
|
|
"osx",
|
2005-03-13 22:33:55 +01:00
|
|
|
osx_testDefault,
|
2005-03-13 20:23:09 +01:00
|
|
|
osx_initDriver,
|
|
|
|
osx_finishDriver,
|
|
|
|
osx_openDevice,
|
|
|
|
osx_play,
|
|
|
|
osx_dropBufferedAudio,
|
|
|
|
osx_closeDevice,
|
2006-07-20 20:53:56 +02:00
|
|
|
NULL, /* sendMetadataFunc */
|
2005-03-13 20:23:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2006-07-14 19:39:14 +02:00
|
|
|
DISABLED_AUDIO_OUTPUT_PLUGIN(osxPlugin)
|
2005-03-13 20:23:09 +01:00
|
|
|
#endif
|