osx_plugin.c 7.09 KB
Newer Older
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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
 */

19
#include "../output_api.h"
20

21
#include <glib.h>
22 23
#include <AudioUnit/AudioUnit.h>

24 25 26
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "osx"

27
typedef struct _OsxData {
28
	AudioUnit au;
29 30
	GMutex *mutex;
	GCond *condition;
Avuton Olrich's avatar
Avuton Olrich committed
31
	char *buffer;
32 33 34
	size_t bufferSize;
	size_t pos;
	size_t len;
Warren Dukes's avatar
Warren Dukes committed
35
	int started;
36 37
} OsxData;

Max Kellermann's avatar
Max Kellermann committed
38
static OsxData *newOsxData(void)
Avuton Olrich's avatar
Avuton Olrich committed
39
{
40
	OsxData *ret = g_new(OsxData, 1);
41

42 43
	ret->mutex = g_mutex_new();
	ret->condition = g_cond_new();
Warren Dukes's avatar
Warren Dukes committed
44 45 46 47

	ret->pos = 0;
	ret->len = 0;
	ret->started = 0;
48 49
	ret->buffer = NULL;
	ret->bufferSize = 0;
Warren Dukes's avatar
Warren Dukes committed
50

51 52 53
	return ret;
}

Max Kellermann's avatar
Max Kellermann committed
54
static bool osx_testDefault(void)
Avuton Olrich's avatar
Avuton Olrich committed
55
{
56
	/*AudioUnit au;
Avuton Olrich's avatar
Avuton Olrich committed
57 58
	   ComponentDescription desc;
	   Component comp;
59

Avuton Olrich's avatar
Avuton Olrich committed
60 61 62 63 64
	   desc.componentType = kAudioUnitType_Output;
	   desc.componentSubType = kAudioUnitSubType_Output;
	   desc.componentManufacturer = kAudioUnitManufacturer_Apple;
	   desc.componentFlags = 0;
	   desc.componentFlagsMask = 0;
65

Avuton Olrich's avatar
Avuton Olrich committed
66 67 68 69 70
	   comp = FindNextComponent(NULL, &desc);
	   if(!comp) {
	   ERROR("Unable to open default OS X defice\n");
	   return -1;
	   }
71

Avuton Olrich's avatar
Avuton Olrich committed
72 73 74 75 76 77
	   if(OpenAComponent(comp, &au) != noErr) {
	   ERROR("Unable to open default OS X defice\n");
	   return -1;
	   }

	   CloseComponent(au); */
Warren Dukes's avatar
Warren Dukes committed
78

79
	return true;
Warren Dukes's avatar
Warren Dukes committed
80 81
}

82
static void *
83
osx_initDriver(G_GNUC_UNUSED const struct audio_format *audio_format,
84
	       G_GNUC_UNUSED const struct config_param *param)
Avuton Olrich's avatar
Avuton Olrich committed
85
{
86
	return newOsxData();
87 88
}

Avuton Olrich's avatar
Avuton Olrich committed
89 90
static void freeOsxData(OsxData * od)
{
91
	g_free(od->buffer);
92 93
	g_mutex_free(od->mutex);
	g_cond_free(od->condition);
94
	g_free(od);
95 96
}

97
static void osx_finishDriver(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
98
{
99
	OsxData *od = data;
100 101 102
	freeOsxData(od);
}

103
static void osx_dropBufferedAudio(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
104
{
105
	OsxData *od = data;
106

107
	g_mutex_lock(od->mutex);
108
	od->len = 0;
109
	g_mutex_unlock(od->mutex);
110 111
}

112
static void osx_closeDevice(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
113
{
114
	OsxData *od = data;
115

116
	g_mutex_lock(od->mutex);
Avuton Olrich's avatar
Avuton Olrich committed
117
	while (od->len) {
118
		g_cond_wait(od->condition, od->mutex);
119
	}
120
	g_mutex_unlock(od->mutex);
121

Avuton Olrich's avatar
Avuton Olrich committed
122
	if (od->started) {
123 124 125
		AudioOutputUnitStop(od->au);
		od->started = 0;
	}
126 127

	AudioUnitUninitialize(od->au);
128
	CloseComponent(od->au);
129 130
}

Max Kellermann's avatar
Max Kellermann committed
131 132
static OSStatus
osx_render(void *vdata,
133 134 135 136
	   G_GNUC_UNUSED AudioUnitRenderActionFlags * ioActionFlags,
	   G_GNUC_UNUSED const AudioTimeStamp * inTimeStamp,
	   G_GNUC_UNUSED UInt32 inBusNumber,
	   G_GNUC_UNUSED UInt32 inNumberFrames,
Max Kellermann's avatar
Max Kellermann committed
137
	   AudioBufferList * bufferList)
Warren Dukes's avatar
Warren Dukes committed
138
{
Avuton Olrich's avatar
Avuton Olrich committed
139 140
	OsxData *od = (OsxData *) vdata;
	AudioBuffer *buffer = &bufferList->mBuffers[0];
141 142
	size_t bufferSize = buffer->mDataByteSize;
	size_t bytesToCopy;
Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
143
	size_t bytes;
Warren Dukes's avatar
Warren Dukes committed
144 145
	int curpos = 0;

146
	g_mutex_lock(od->mutex);
Avuton Olrich's avatar
Avuton Olrich committed
147

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
148
	bytesToCopy = MIN(od->len, bufferSize);
Avuton Olrich's avatar
Avuton Olrich committed
149 150 151
	bufferSize = bytesToCopy;
	od->len -= bytesToCopy;

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
152 153
	bytes = od->bufferSize - od->pos;
	if (bytesToCopy > bytes) {
154
		memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytes);
Avuton Olrich's avatar
Avuton Olrich committed
155 156 157 158
		od->pos = 0;
		curpos += bytes;
		bytesToCopy -= bytes;
	}
Warren Dukes's avatar
Warren Dukes committed
159

160
	memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytesToCopy);
Avuton Olrich's avatar
Avuton Olrich committed
161
	od->pos += bytesToCopy;
162

Avuton Olrich's avatar
Avuton Olrich committed
163 164
	if (od->pos >= od->bufferSize)
		od->pos = 0;
165

166 167
	g_mutex_unlock(od->mutex);
	g_cond_signal(od->condition);
Warren Dukes's avatar
Warren Dukes committed
168

Warren Dukes's avatar
Warren Dukes committed
169
	buffer->mDataByteSize = bufferSize;
Warren Dukes's avatar
Warren Dukes committed
170

Avuton Olrich's avatar
Avuton Olrich committed
171
	if (!bufferSize) {
172
		g_usleep(1000);
Warren Dukes's avatar
Warren Dukes committed
173
	}
Warren Dukes's avatar
Warren Dukes committed
174 175 176 177

	return 0;
}

178
static bool
179
osx_openDevice(void *data, struct audio_format *audioFormat)
Avuton Olrich's avatar
Avuton Olrich committed
180
{
181
	OsxData *od = data;
182 183 184 185 186
	ComponentDescription desc;
	Component comp;
	AURenderCallbackStruct callback;
	AudioStreamBasicDescription streamDesc;

187 188 189
	if (audioFormat->bits > 16)
		audioFormat->bits = 16;

190 191 192 193 194 195 196
	desc.componentType = kAudioUnitType_Output;
	desc.componentSubType = kAudioUnitSubType_DefaultOutput;
	desc.componentManufacturer = kAudioUnitManufacturer_Apple;
	desc.componentFlags = 0;
	desc.componentFlagsMask = 0;

	comp = FindNextComponent(NULL, &desc);
Avuton Olrich's avatar
Avuton Olrich committed
197
	if (comp == 0) {
198
		g_warning("Error finding OS X component\n");
199
		return false;
Warren Dukes's avatar
Warren Dukes committed
200 201
	}

Avuton Olrich's avatar
Avuton Olrich committed
202
	if (OpenAComponent(comp, &od->au) != noErr) {
203
		g_warning("Unable to open OS X component\n");
204
		return false;
Warren Dukes's avatar
Warren Dukes committed
205 206
	}

Avuton Olrich's avatar
Avuton Olrich committed
207
	if (AudioUnitInitialize(od->au) != 0) {
208
		CloseComponent(od->au);
209
		g_warning("Unable to initialize OS X audio unit\n");
210
		return false;
211 212 213 214 215
	}

	callback.inputProc = osx_render;
	callback.inputProcRefCon = od;

Avuton Olrich's avatar
Avuton Olrich committed
216 217 218
	if (AudioUnitSetProperty(od->au, kAudioUnitProperty_SetRenderCallback,
				 kAudioUnitScope_Input, 0,
				 &callback, sizeof(callback)) != 0) {
219 220
		AudioUnitUninitialize(od->au);
		CloseComponent(od->au);
221
		g_warning("unable to set callback for OS X audio unit\n");
222
		return false;
223
	}
Warren Dukes's avatar
Warren Dukes committed
224

225
	streamDesc.mSampleRate = audioFormat->sample_rate;
226
	streamDesc.mFormatID = kAudioFormatLinearPCM;
227
	streamDesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
Max Kellermann's avatar
Max Kellermann committed
228
#if G_BYTE_ORDER == G_BIG_ENDIAN
229 230 231
	streamDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
#endif

232
	streamDesc.mBytesPerPacket = audio_format_frame_size(audioFormat);
233 234 235 236 237
	streamDesc.mFramesPerPacket = 1;
	streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket;
	streamDesc.mChannelsPerFrame = audioFormat->channels;
	streamDesc.mBitsPerChannel = audioFormat->bits;

Avuton Olrich's avatar
Avuton Olrich committed
238 239 240
	if (AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
				 kAudioUnitScope_Input, 0,
				 &streamDesc, sizeof(streamDesc)) != 0) {
241 242
		AudioUnitUninitialize(od->au);
		CloseComponent(od->au);
243
		g_warning("Unable to set format on OS X device\n");
244
		return false;
245 246
	}

247
	/* create a buffer of 1s */
248
	od->bufferSize = (audioFormat->sample_rate) *
249
		audio_format_frame_size(audioFormat);
250
	od->buffer = g_realloc(od->buffer, od->bufferSize);
251

Warren Dukes's avatar
Warren Dukes committed
252 253
	od->pos = 0;
	od->len = 0;
254

255
	return true;
256 257
}

258
static size_t
259
osx_play(void *data, const void *chunk, size_t size)
Avuton Olrich's avatar
Avuton Olrich committed
260
{
261
	OsxData *od = data;
262
	size_t start, nbytes;
Warren Dukes's avatar
Warren Dukes committed
263

Avuton Olrich's avatar
Avuton Olrich committed
264
	if (!od->started) {
Eric Wong's avatar
Eric Wong committed
265
		int err;
266
		od->started = 1;
Eric Wong's avatar
Eric Wong committed
267
		err = AudioOutputUnitStart(od->au);
Avuton Olrich's avatar
Avuton Olrich committed
268
		if (err) {
269
			g_warning("unable to start audio output: %i\n", err);
270
			return 0;
271 272 273
		}
	}

274
	g_mutex_lock(od->mutex);
Warren Dukes's avatar
Warren Dukes committed
275

276 277 278
	while (od->len >= od->bufferSize)
		/* wait for some free space in the buffer */
		g_cond_wait(od->condition, od->mutex);
Warren Dukes's avatar
Warren Dukes committed
279

280 281 282
	start = od->pos + od->len;
	if (start >= od->bufferSize)
		start -= od->bufferSize;
Warren Dukes's avatar
Warren Dukes committed
283

284 285 286
	nbytes = start < od->pos
		? od->pos - start
		: od->bufferSize - start;
Warren Dukes's avatar
Warren Dukes committed
287

288
	assert(nbytes > 0);
289

290 291
	if (nbytes > size)
		nbytes = size;
292

293
	memcpy(od->buffer + start, chunk, nbytes);
294
	od->len += nbytes;
295

296
	g_mutex_unlock(od->mutex);
297

298
	return nbytes;
299 300
}

301
const struct audio_output_plugin osxPlugin = {
302 303 304 305 306 307 308 309
	.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,
310
};