2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2005-03-13 20:23:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-03-13 20:23:09 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
|
|
|
#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>
|
2009-09-20 23:30:37 +02:00
|
|
|
#include <CoreServices/CoreServices.h>
|
2008-10-26 21:58:37 +01:00
|
|
|
|
2008-12-08 23:23:30 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "osx"
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output {
|
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;
|
2009-02-26 21:03:06 +01:00
|
|
|
size_t buffer_size;
|
2008-04-12 06:15:52 +02:00
|
|
|
size_t pos;
|
|
|
|
size_t len;
|
2009-02-26 21:03:06 +01:00
|
|
|
};
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2009-02-26 22:04:59 +01:00
|
|
|
/**
|
|
|
|
* The quark used for GError.domain.
|
|
|
|
*/
|
|
|
|
static inline GQuark
|
|
|
|
osx_output_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("osx_output");
|
|
|
|
}
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static bool
|
|
|
|
osx_output_test_default_device(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:33:13 +01:00
|
|
|
/* on a Mac, this is always the default plugin, if nothing
|
|
|
|
else is configured */
|
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-26 21:03:06 +01:00
|
|
|
osx_output_init(G_GNUC_UNUSED const struct audio_format *audio_format,
|
2009-02-26 22:04:59 +01:00
|
|
|
G_GNUC_UNUSED const struct config_param *param,
|
|
|
|
G_GNUC_UNUSED GError **error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *oo = g_new(struct osx_output, 1);
|
|
|
|
|
|
|
|
oo->mutex = g_mutex_new();
|
|
|
|
oo->condition = g_cond_new();
|
|
|
|
|
|
|
|
oo->pos = 0;
|
|
|
|
oo->len = 0;
|
|
|
|
oo->buffer = NULL;
|
|
|
|
oo->buffer_size = 0;
|
|
|
|
|
|
|
|
return oo;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static void osx_output_finish(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *od = data;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static void osx_output_cancel(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *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
|
|
|
}
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static void osx_output_close(void *data)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *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
|
|
|
|
2009-02-26 21:40:22 +01:00
|
|
|
AudioOutputUnitStop(od->au);
|
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-02-26 21:03:06 +01:00
|
|
|
G_GNUC_UNUSED AudioUnitRenderActionFlags *io_action_flags,
|
|
|
|
G_GNUC_UNUSED const AudioTimeStamp *in_timestamp,
|
|
|
|
G_GNUC_UNUSED UInt32 in_bus_number,
|
|
|
|
G_GNUC_UNUSED UInt32 in_number_frames,
|
|
|
|
AudioBufferList *buffer_list)
|
2005-03-14 05:30:32 +01:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *od = (struct osx_output *) vdata;
|
|
|
|
AudioBuffer *buffer = &buffer_list->mBuffers[0];
|
|
|
|
size_t buffer_size = buffer->mDataByteSize;
|
|
|
|
size_t bytes_to_copy;
|
|
|
|
size_t trailer_length;
|
|
|
|
size_t dest_pos = 0;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(od->mutex);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
bytes_to_copy = MIN(od->len, buffer_size);
|
|
|
|
buffer_size = bytes_to_copy;
|
|
|
|
od->len -= bytes_to_copy;
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
trailer_length = od->buffer_size - od->pos;
|
|
|
|
if (bytes_to_copy > trailer_length) {
|
|
|
|
memcpy((unsigned char*)buffer->mData + dest_pos,
|
|
|
|
od->buffer + od->pos, trailer_length);
|
2006-07-20 18:02:40 +02:00
|
|
|
od->pos = 0;
|
2009-02-26 21:03:06 +01:00
|
|
|
dest_pos += trailer_length;
|
|
|
|
bytes_to_copy -= trailer_length;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
memcpy((unsigned char*)buffer->mData + dest_pos,
|
|
|
|
od->buffer + od->pos, bytes_to_copy);
|
|
|
|
od->pos += bytes_to_copy;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
if (od->pos >= od->buffer_size)
|
2006-07-20 18:02:40 +02:00
|
|
|
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
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
buffer->mDataByteSize = buffer_size;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
if (!buffer_size) {
|
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
|
2009-02-26 22:04:59 +01:00
|
|
|
osx_output_open(void *data, struct audio_format *audio_format, GError **error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *od = data;
|
2005-03-16 05:46:41 +01:00
|
|
|
ComponentDescription desc;
|
|
|
|
Component comp;
|
|
|
|
AURenderCallbackStruct callback;
|
2009-02-26 21:03:06 +01:00
|
|
|
AudioStreamBasicDescription stream_description;
|
2009-02-26 22:01:42 +01:00
|
|
|
OSStatus status;
|
|
|
|
ComponentResult result;
|
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) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"Error finding OS X component");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
status = OpenAComponent(comp, &od->au);
|
|
|
|
if (status != noErr) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"Unable to open OS X component: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
status = AudioUnitInitialize(od->au);
|
|
|
|
if (status != noErr) {
|
2005-03-16 05:46:41 +01:00
|
|
|
CloseComponent(od->au);
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"Unable to initialize OS X audio unit: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
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;
|
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
result = AudioUnitSetProperty(od->au,
|
|
|
|
kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&callback, sizeof(callback));
|
|
|
|
if (result != noErr) {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"unable to set callback for OS X audio unit");
|
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
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
stream_description.mSampleRate = audio_format->sample_rate;
|
|
|
|
stream_description.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
stream_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2009-01-05 12:40:57 +01:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2009-02-26 21:03:06 +01:00
|
|
|
stream_description.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
2006-09-09 12:01:25 +02:00
|
|
|
#endif
|
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
stream_description.mBytesPerPacket =
|
|
|
|
audio_format_frame_size(audio_format);
|
|
|
|
stream_description.mFramesPerPacket = 1;
|
|
|
|
stream_description.mBytesPerFrame = stream_description.mBytesPerPacket;
|
|
|
|
stream_description.mChannelsPerFrame = audio_format->channels;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
|
|
|
switch (audio_format->format) {
|
|
|
|
case SAMPLE_FORMAT_S8:
|
|
|
|
stream_description.mBitsPerChannel = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SAMPLE_FORMAT_S16:
|
|
|
|
stream_description.mBitsPerChannel = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
audio_format->format = SAMPLE_FORMAT_S16;
|
|
|
|
stream_description.mBitsPerChannel = 16;
|
|
|
|
break;
|
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
result = AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&stream_description,
|
|
|
|
sizeof(stream_description));
|
|
|
|
if (result != noErr) {
|
2005-03-16 05:46:41 +01:00
|
|
|
AudioUnitUninitialize(od->au);
|
|
|
|
CloseComponent(od->au);
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"Unable to set format on OS X device");
|
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 */
|
2009-02-26 21:03:06 +01:00
|
|
|
od->buffer_size = (audio_format->sample_rate) *
|
|
|
|
audio_format_frame_size(audio_format);
|
|
|
|
od->buffer = g_realloc(od->buffer, od->buffer_size);
|
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
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
status = AudioOutputUnitStart(od->au);
|
|
|
|
if (status != 0) {
|
2009-02-26 22:04:59 +01:00
|
|
|
g_set_error(error, osx_output_quark(), 0,
|
|
|
|
"unable to start audio output: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2009-02-26 21:40:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-26 22:04:59 +01:00
|
|
|
osx_output_play(void *data, const void *chunk, size_t size,
|
|
|
|
G_GNUC_UNUSED GError **error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:03:06 +01:00
|
|
|
struct osx_output *od = data;
|
2009-02-23 09:29:56 +01:00
|
|
|
size_t start, nbytes;
|
2005-03-14 05:30:32 +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-26 21:03:06 +01:00
|
|
|
while (od->len >= od->buffer_size)
|
2009-02-23 09:29:56 +01:00
|
|
|
/* 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;
|
2009-02-26 21:03:06 +01:00
|
|
|
if (start >= od->buffer_size)
|
|
|
|
start -= od->buffer_size;
|
2005-03-24 12:45:39 +01:00
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
nbytes = start < od->pos
|
|
|
|
? od->pos - start
|
2009-02-26 21:03:06 +01:00
|
|
|
: od->buffer_size - 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",
|
2009-02-26 21:03:06 +01:00
|
|
|
.test_default_device = osx_output_test_default_device,
|
|
|
|
.init = osx_output_init,
|
|
|
|
.finish = osx_output_finish,
|
|
|
|
.open = osx_output_open,
|
|
|
|
.close = osx_output_close,
|
|
|
|
.play = osx_output_play,
|
|
|
|
.cancel = osx_output_cancel,
|
2005-03-13 20:23:09 +01:00
|
|
|
};
|