2005-03-13 20:23:09 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
|
|
|
|
* 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-13 20:23:09 +01:00
|
|
|
#include <stdlib.h>
|
2005-03-14 05:30:32 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
#include "../log.h"
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 8192
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
typedef struct _OsxData {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnit au;
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t condition;
|
2005-03-16 05:46:41 +01:00
|
|
|
char buffer[BUFFER_SIZE];
|
2005-03-14 05:30:32 +01:00
|
|
|
int pos;
|
|
|
|
int len;
|
|
|
|
int go;
|
|
|
|
int started;
|
2005-03-13 20:23:09 +01:00
|
|
|
} OsxData;
|
|
|
|
|
|
|
|
static OsxData * newOsxData() {
|
|
|
|
OsxData * ret = malloc(sizeof(OsxData));
|
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_mutex_init(&ret->mutex, NULL);
|
|
|
|
pthread_cond_init(&ret->condition, NULL);
|
|
|
|
|
|
|
|
ret->pos = 0;
|
|
|
|
ret->len = 0;
|
|
|
|
ret->go = 0;
|
|
|
|
ret->started = 0;
|
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-03-13 22:33:55 +01:00
|
|
|
static int osx_testDefault() {
|
2005-03-16 05:46:41 +01:00
|
|
|
/*AudioUnit au;
|
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_Output;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
|
|
|
comp = FindNextComponent(NULL, &desc);
|
|
|
|
if(!comp) {
|
|
|
|
ERROR("Unable to open default OS X defice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(OpenAComponent(comp, &au) != noErr) {
|
|
|
|
ERROR("Unable to open default OS X defice\n");
|
2005-03-13 22:33:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
|
|
|
|
CloseComponent(au);*/
|
2005-03-13 22:33:55 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
static int osx_initDriver(AudioOutput * audioOutput, ConfigParam * param) {
|
|
|
|
OsxData * od = newOsxData();
|
|
|
|
|
|
|
|
audioOutput->data = od;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeOsxData(OsxData * od) {
|
|
|
|
free(od);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void osx_finishDriver(AudioOutput * audioOutput) {
|
|
|
|
OsxData * od = (OsxData *)audioOutput->data;
|
|
|
|
freeOsxData(od);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void osx_dropBufferedAudio(AudioOutput * audioOutput) {
|
|
|
|
/* not implemented yet */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void osx_closeDevice(AudioOutput * audioOutput) {
|
2005-03-17 00:05:25 +01:00
|
|
|
OsxData * od = (OsxData *) audioOutput->data;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
while(od->len) {
|
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
|
|
|
}
|
|
|
|
od->go = 0;
|
|
|
|
pthread_mutex_unlock(&od->mutex);
|
|
|
|
|
|
|
|
AudioOutputUnitStop(od->au);
|
|
|
|
|
|
|
|
CloseComponent(od->au);
|
|
|
|
AudioUnitUninitialize(od->au);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
audioOutput->open = 0;
|
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01: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
|
|
|
{
|
|
|
|
OsxData * od = (OsxData *)vdata;
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioBuffer * buffer = &bufferList->mBuffers[0];
|
|
|
|
int bufferSize = buffer->mDataByteSize;
|
|
|
|
int bytesToCopy;
|
2005-03-14 05:30:32 +01:00
|
|
|
int curpos = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
|
|
|
|
while((od->go || od->len) && bufferSize) {
|
|
|
|
while(od->go && od->len < bufferSize &&
|
|
|
|
od->len < BUFFER_SIZE)
|
|
|
|
{
|
2005-03-16 05:46:41 +01:00
|
|
|
pthread_cond_signal(&od->condition);
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
bytesToCopy = od->len < bufferSize ? od->len : bufferSize;
|
|
|
|
bufferSize -= bytesToCopy;
|
|
|
|
od->len -= bytesToCopy;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
if(od->pos+bytesToCopy > BUFFER_SIZE) {
|
|
|
|
int bytes = BUFFER_SIZE-od->pos;
|
|
|
|
memcpy(buffer->mData+curpos, od->buffer+od->pos, bytes);
|
2005-03-14 05:30:32 +01:00
|
|
|
od->pos = 0;
|
2005-03-16 05:46:41 +01:00
|
|
|
curpos += bytes;
|
|
|
|
bytesToCopy -= bytes;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
assert(bytesToCopy);
|
|
|
|
memcpy(buffer->mData+curpos, od->buffer+od->pos, bytesToCopy);
|
|
|
|
od->pos += bytesToCopy;
|
|
|
|
curpos += bytesToCopy;
|
|
|
|
|
|
|
|
if(od->pos >= BUFFER_SIZE) od->pos = 0;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(bufferSize) {
|
2005-03-16 05:46:41 +01:00
|
|
|
memset(buffer->mData+curpos, 0, bufferSize);
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_cond_signal(&od->condition);
|
2005-03-16 05:46:41 +01:00
|
|
|
pthread_mutex_unlock(&od->mutex);
|
2005-03-14 05:30:32 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-13 20:23:09 +01:00
|
|
|
static int osx_openDevice(AudioOutput * audioOutput) {
|
2005-03-14 05:30:32 +01:00
|
|
|
OsxData * od = (OsxData *)audioOutput->data;
|
2005-03-16 05:46:41 +01:00
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
AURenderCallbackStruct callback;
|
2005-03-14 05:30:32 +01: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;
|
|
|
|
|
|
|
|
DEBUG("finding component\n");
|
|
|
|
|
|
|
|
comp = FindNextComponent(NULL, &desc);
|
|
|
|
if(comp == 0) {
|
|
|
|
ERROR("Error finding OS X component\n");
|
2005-03-14 05:30:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
DEBUG("opening component\n");
|
|
|
|
if(OpenAComponent(comp, &od->au) != noErr) {
|
|
|
|
ERROR("Unable to open OS X component\n");
|
2005-03-14 05:30:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
DEBUG("initializing au\n");
|
|
|
|
if(AudioUnitInitialize(od->au) != 0) {
|
|
|
|
CloseComponent(od->au);
|
|
|
|
ERROR("Unable to initialuze OS X audio unit\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback.inputProc = osx_render;
|
|
|
|
callback.inputProcRefCon = od;
|
|
|
|
|
|
|
|
DEBUG("set callback\n");
|
|
|
|
if(AudioUnitSetProperty(od->au, kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&callback, sizeof(callback)) != 0)
|
|
|
|
{
|
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
|
|
|
ERROR("unable to set callbak for OS X audio unit\n");
|
|
|
|
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;
|
|
|
|
streamDesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
|
|
|
|
kLinearPCMFormatFlagIsBigEndian;
|
|
|
|
streamDesc.mBytesPerPacket = audioFormat->channels*audioFormat->bits/8;
|
|
|
|
streamDesc.mFramesPerPacket = 1;
|
|
|
|
streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket;
|
|
|
|
streamDesc.mChannelsPerFrame = audioFormat->channels;
|
|
|
|
streamDesc.mBitsPerChannel = audioFormat->bits;
|
|
|
|
|
|
|
|
DEBUG("set format\n");
|
|
|
|
if(AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&streamDesc, sizeof(streamDesc)) != 0)
|
|
|
|
{
|
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
|
|
|
ERROR("Unable to set format on OS X device\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("start\n");
|
|
|
|
int err = AudioOutputUnitStart(od->au);
|
2005-03-14 05:30:32 +01:00
|
|
|
if(err) {
|
2005-03-16 05:46:41 +01:00
|
|
|
ERROR("unable to start audio output: %i\n", err);
|
2005-03-14 05:30:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
od->go = 1;
|
|
|
|
od->pos = 0;
|
|
|
|
od->len = 0;
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
audioOutput->open = 1;
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
DEBUG("opened OS X device\n");
|
2005-03-13 20:23:09 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int osx_play(AudioOutput * audioOutput, char * playChunk, int size) {
|
2005-03-14 05:30:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
pthread_mutex_lock(&od->mutex);
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
curpos = od->pos+od->len;
|
|
|
|
if(curpos >= BUFFER_SIZE) curpos -= BUFFER_SIZE;
|
|
|
|
|
2005-03-14 05:30:32 +01:00
|
|
|
while(size) {
|
2005-03-16 05:46:41 +01:00
|
|
|
while(od->len >= BUFFER_SIZE) {
|
|
|
|
if(curpos!=od->pos) {
|
|
|
|
DEBUG("%i != %i\n", curpos, od->pos);
|
|
|
|
abort();
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
pthread_cond_signal(&od->condition);
|
2005-03-14 05:30:32 +01:00
|
|
|
pthread_cond_wait(&od->condition, &od->mutex);
|
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
bytesToCopy = BUFFER_SIZE - od->len;
|
|
|
|
bytesToCopy = bytesToCopy < size ? bytesToCopy : size;
|
|
|
|
size -= bytesToCopy;
|
|
|
|
od->len += bytesToCopy;
|
|
|
|
|
|
|
|
if(curpos+bytesToCopy > BUFFER_SIZE) {
|
|
|
|
int bytes = BUFFER_SIZE-curpos;
|
|
|
|
memcpy(od->buffer+curpos, playChunk, bytes);
|
|
|
|
curpos = 0;
|
|
|
|
playChunk += bytes;
|
|
|
|
bytesToCopy -= bytes;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2005-03-16 05:46:41 +01:00
|
|
|
assert(bytesToCopy);
|
|
|
|
memcpy(od->buffer+curpos, playChunk, bytesToCopy);
|
|
|
|
curpos += bytesToCopy;
|
|
|
|
playChunk += bytesToCopy;
|
|
|
|
|
|
|
|
if(curpos >= BUFFER_SIZE) curpos = 0;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_cond_signal(&od->condition);
|
2005-03-16 05:46:41 +01:00
|
|
|
pthread_mutex_unlock(&od->mutex);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioOutputPlugin osxPlugin =
|
|
|
|
{
|
|
|
|
"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,
|
|
|
|
NULL /* sendMetadataFunc */
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
AudioOutputPlugin osxPlugin =
|
|
|
|
{
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|