osx_plugin.c 8.61 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 20
#include "../output_api.h"
#include "../utils.h"
21

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

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

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

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

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

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

52 53 54
	return ret;
}

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

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

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

Avuton Olrich's avatar
Avuton Olrich committed
73 74 75 76 77 78
	   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
79

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

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

Avuton Olrich's avatar
Avuton Olrich committed
91 92 93 94
static void freeOsxData(OsxData * od)
{
	if (od->buffer)
		free(od->buffer);
95 96
	g_mutex_free(od->mutex);
	g_cond_free(od->condition);
97 98 99
	free(od);
}

100
static void osx_finishDriver(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
101
{
102
	OsxData *od = data;
103 104 105
	freeOsxData(od);
}

106
static void osx_dropBufferedAudio(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
107
{
108
	OsxData *od = data;
109

110
	g_mutex_lock(od->mutex);
111
	od->len = 0;
112
	g_mutex_unlock(od->mutex);
113 114
}

115
static void osx_closeDevice(void *data)
Avuton Olrich's avatar
Avuton Olrich committed
116
{
117
	OsxData *od = data;
118

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

Avuton Olrich's avatar
Avuton Olrich committed
125
	if (od->started) {
126 127 128
		AudioOutputUnitStop(od->au);
		od->started = 0;
	}
129 130

	AudioUnitUninitialize(od->au);
131
	CloseComponent(od->au);
132 133
}

Max Kellermann's avatar
Max Kellermann committed
134 135
static OSStatus
osx_render(void *vdata,
136 137 138 139
	   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
140
	   AudioBufferList * bufferList)
Warren Dukes's avatar
Warren Dukes committed
141
{
Avuton Olrich's avatar
Avuton Olrich committed
142 143
	OsxData *od = (OsxData *) vdata;
	AudioBuffer *buffer = &bufferList->mBuffers[0];
144 145
	size_t bufferSize = buffer->mDataByteSize;
	size_t bytesToCopy;
Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
146
	size_t bytes;
Warren Dukes's avatar
Warren Dukes committed
147 148
	int curpos = 0;

Warren Dukes's avatar
Warren Dukes committed
149
	/*DEBUG("osx_render: enter : %i\n", (int)bufferList->mNumberBuffers);
Avuton Olrich's avatar
Avuton Olrich committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	   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");
	   }
	   } */
Warren Dukes's avatar
Warren Dukes committed
171

Eric Wong's avatar
Eric Wong committed
172
	/* while(bufferSize) {
Avuton Olrich's avatar
Avuton Olrich committed
173
	   DEBUG("osx_render: lock\n"); */
174
	g_mutex_lock(od->mutex);
Avuton Olrich's avatar
Avuton Olrich committed
175 176 177 178 179 180
	/*
	   DEBUG("%i:%i\n", bufferSize, od->len);
	   while(od->go && od->len < bufferSize && 
	   od->len < od->bufferSize)
	   {
	   DEBUG("osx_render: wait\n");
181
	   g_cond_wait(od->condition, od->mutex);
Avuton Olrich's avatar
Avuton Olrich committed
182 183 184
	   }
	 */

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
185
	bytesToCopy = MIN(od->len, bufferSize);
Avuton Olrich's avatar
Avuton Olrich committed
186 187 188
	bufferSize = bytesToCopy;
	od->len -= bytesToCopy;

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
189 190
	bytes = od->bufferSize - od->pos;
	if (bytesToCopy > bytes) {
191
		memcpy((unsigned char*)buffer->mData + curpos, od->buffer + od->pos, bytes);
Avuton Olrich's avatar
Avuton Olrich committed
192 193 194 195
		od->pos = 0;
		curpos += bytes;
		bytesToCopy -= bytes;
	}
Warren Dukes's avatar
Warren Dukes committed
196

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

Avuton Olrich's avatar
Avuton Olrich committed
200 201 202
	if (od->pos >= od->bufferSize)
		od->pos = 0;
	/* DEBUG("osx_render: unlock\n"); */
203 204
	g_mutex_unlock(od->mutex);
	g_cond_signal(od->condition);
Eric Wong's avatar
Eric Wong committed
205
	/* } */
Warren Dukes's avatar
Warren Dukes committed
206

Warren Dukes's avatar
Warren Dukes committed
207
	buffer->mDataByteSize = bufferSize;
Warren Dukes's avatar
Warren Dukes committed
208

Avuton Olrich's avatar
Avuton Olrich committed
209
	if (!bufferSize) {
Warren Dukes's avatar
Warren Dukes committed
210 211
		my_usleep(1000);
	}
Warren Dukes's avatar
Warren Dukes committed
212

Eric Wong's avatar
Eric Wong committed
213
	/* DEBUG("osx_render: leave\n"); */
Warren Dukes's avatar
Warren Dukes committed
214 215 216
	return 0;
}

217
static bool
218
osx_openDevice(void *data, struct audio_format *audioFormat)
Avuton Olrich's avatar
Avuton Olrich committed
219
{
220
	OsxData *od = data;
221 222 223 224 225
	ComponentDescription desc;
	Component comp;
	AURenderCallbackStruct callback;
	AudioStreamBasicDescription streamDesc;

226 227 228
	if (audioFormat->bits > 16)
		audioFormat->bits = 16;

229 230 231 232 233 234 235
	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
236
	if (comp == 0) {
237
		g_warning("Error finding OS X component\n");
238
		return false;
Warren Dukes's avatar
Warren Dukes committed
239 240
	}

Avuton Olrich's avatar
Avuton Olrich committed
241
	if (OpenAComponent(comp, &od->au) != noErr) {
242
		g_warning("Unable to open OS X component\n");
243
		return false;
Warren Dukes's avatar
Warren Dukes committed
244 245
	}

Avuton Olrich's avatar
Avuton Olrich committed
246
	if (AudioUnitInitialize(od->au) != 0) {
247
		CloseComponent(od->au);
248
		g_warning("Unable to initialize OS X audio unit\n");
249
		return false;
250 251 252 253 254
	}

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

Avuton Olrich's avatar
Avuton Olrich committed
255 256 257
	if (AudioUnitSetProperty(od->au, kAudioUnitProperty_SetRenderCallback,
				 kAudioUnitScope_Input, 0,
				 &callback, sizeof(callback)) != 0) {
258 259
		AudioUnitUninitialize(od->au);
		CloseComponent(od->au);
260
		g_warning("unable to set callback for OS X audio unit\n");
261
		return false;
262
	}
Warren Dukes's avatar
Warren Dukes committed
263

264
	streamDesc.mSampleRate = audioFormat->sample_rate;
265
	streamDesc.mFormatID = kAudioFormatLinearPCM;
266
	streamDesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
Max Kellermann's avatar
Max Kellermann committed
267
#if G_BYTE_ORDER == G_BIG_ENDIAN
268 269 270
	streamDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
#endif

271
	streamDesc.mBytesPerPacket = audio_format_frame_size(audioFormat);
272 273 274 275 276
	streamDesc.mFramesPerPacket = 1;
	streamDesc.mBytesPerFrame = streamDesc.mBytesPerPacket;
	streamDesc.mChannelsPerFrame = audioFormat->channels;
	streamDesc.mBitsPerChannel = audioFormat->bits;

Avuton Olrich's avatar
Avuton Olrich committed
277 278 279
	if (AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
				 kAudioUnitScope_Input, 0,
				 &streamDesc, sizeof(streamDesc)) != 0) {
280 281
		AudioUnitUninitialize(od->au);
		CloseComponent(od->au);
282
		g_warning("Unable to set format on OS X device\n");
283
		return false;
284 285
	}

286
	/* create a buffer of 1s */
287
	od->bufferSize = (audioFormat->sample_rate) *
288
		audio_format_frame_size(audioFormat);
289
	od->buffer = g_realloc(od->buffer, od->bufferSize);
290

Warren Dukes's avatar
Warren Dukes committed
291 292
	od->pos = 0;
	od->len = 0;
293

294
	return true;
295 296
}

297
static bool
298
osx_play(void *data, const char *playChunk, size_t size)
Avuton Olrich's avatar
Avuton Olrich committed
299
{
300
	OsxData *od = data;
301
	size_t bytesToCopy;
Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
302
	size_t bytes;
303
	size_t curpos;
Warren Dukes's avatar
Warren Dukes committed
304

Eric Wong's avatar
Eric Wong committed
305
	/* DEBUG("osx_play: enter\n"); */
Warren Dukes's avatar
Warren Dukes committed
306

Avuton Olrich's avatar
Avuton Olrich committed
307
	if (!od->started) {
Eric Wong's avatar
Eric Wong committed
308
		int err;
309
		od->started = 1;
Eric Wong's avatar
Eric Wong committed
310
		err = AudioOutputUnitStart(od->au);
Avuton Olrich's avatar
Avuton Olrich committed
311
		if (err) {
312
			g_warning("unable to start audio output: %i\n", err);
313
			return false;
314 315 316
		}
	}

317
	g_mutex_lock(od->mutex);
Warren Dukes's avatar
Warren Dukes committed
318

Avuton Olrich's avatar
Avuton Olrich committed
319
	while (size) {
Eric Wong's avatar
Eric Wong committed
320
		/* DEBUG("osx_play: lock\n"); */
Avuton Olrich's avatar
Avuton Olrich committed
321 322 323
		curpos = od->pos + od->len;
		if (curpos >= od->bufferSize)
			curpos -= od->bufferSize;
Warren Dukes's avatar
Warren Dukes committed
324

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
325
		bytesToCopy = MIN(od->bufferSize, size);
Warren Dukes's avatar
Warren Dukes committed
326

Avuton Olrich's avatar
Avuton Olrich committed
327
		while (od->len > od->bufferSize - bytesToCopy) {
Eric Wong's avatar
Eric Wong committed
328
			/* DEBUG("osx_play: wait\n"); */
329
			g_cond_wait(od->condition, od->mutex);
Warren Dukes's avatar
Warren Dukes committed
330 331
		}

332 333 334
		size -= bytesToCopy;
		od->len += bytesToCopy;

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
335 336
		bytes = od->bufferSize - curpos;
		if (bytesToCopy > bytes) {
Avuton Olrich's avatar
Avuton Olrich committed
337
			memcpy(od->buffer + curpos, playChunk, bytes);
338 339 340
			curpos = 0;
			playChunk += bytes;
			bytesToCopy -= bytes;
Warren Dukes's avatar
Warren Dukes committed
341 342
		}

Avuton Olrich's avatar
Avuton Olrich committed
343
		memcpy(od->buffer + curpos, playChunk, bytesToCopy);
344 345
		playChunk += bytesToCopy;

Warren Dukes's avatar
Warren Dukes committed
346
	}
Eric Wong's avatar
Eric Wong committed
347
	/* DEBUG("osx_play: unlock\n"); */
348
	g_mutex_unlock(od->mutex);
349

Eric Wong's avatar
Eric Wong committed
350
	/* DEBUG("osx_play: leave\n"); */
351
	return true;
352 353
}

354
const struct audio_output_plugin osxPlugin = {
355 356 357 358 359 360 361 362
	.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,
363
};