osx_plugin.c 7.35 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 const 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
static void freeOsxData(OsxData * od)
{
93
	g_free(od->buffer);
94 95
	g_mutex_free(od->mutex);
	g_cond_free(od->condition);
96
	g_free(od);
97 98
}

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

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

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

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

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

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

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

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

148
	g_mutex_lock(od->mutex);
Avuton Olrich's avatar
Avuton Olrich committed
149

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

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

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

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

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

Warren Dukes's avatar
Warren Dukes committed
171
	buffer->mDataByteSize = bufferSize;
Warren Dukes's avatar
Warren Dukes committed
172

Avuton Olrich's avatar
Avuton Olrich committed
173
	if (!bufferSize) {
Warren Dukes's avatar
Warren Dukes committed
174 175
		my_usleep(1000);
	}
Warren Dukes's avatar
Warren Dukes committed
176 177 178 179

	return 0;
}

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

189 190 191
	if (audioFormat->bits > 16)
		audioFormat->bits = 16;

192 193 194 195 196 197 198
	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
199
	if (comp == 0) {
200
		g_warning("Error finding OS X component\n");
201
		return false;
Warren Dukes's avatar
Warren Dukes committed
202 203
	}

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

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

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

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

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

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

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

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

Warren Dukes's avatar
Warren Dukes committed
254 255
	od->pos = 0;
	od->len = 0;
256

257
	return true;
258 259
}

260
static bool
261
osx_play(void *data, const char *playChunk, size_t size)
Avuton Olrich's avatar
Avuton Olrich committed
262
{
263
	OsxData *od = data;
264
	size_t bytesToCopy;
Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
265
	size_t bytes;
266
	size_t curpos;
Warren Dukes's avatar
Warren Dukes committed
267

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

278
	g_mutex_lock(od->mutex);
Warren Dukes's avatar
Warren Dukes committed
279

Avuton Olrich's avatar
Avuton Olrich committed
280 281 282 283
	while (size) {
		curpos = od->pos + od->len;
		if (curpos >= od->bufferSize)
			curpos -= od->bufferSize;
Warren Dukes's avatar
Warren Dukes committed
284

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

Avuton Olrich's avatar
Avuton Olrich committed
287
		while (od->len > od->bufferSize - bytesToCopy) {
288
			g_cond_wait(od->condition, od->mutex);
Warren Dukes's avatar
Warren Dukes committed
289 290
		}

291 292 293
		size -= bytesToCopy;
		od->len += bytesToCopy;

Emanuele Giaquinta's avatar
Emanuele Giaquinta committed
294 295
		bytes = od->bufferSize - curpos;
		if (bytesToCopy > bytes) {
Avuton Olrich's avatar
Avuton Olrich committed
296
			memcpy(od->buffer + curpos, playChunk, bytes);
297 298 299
			curpos = 0;
			playChunk += bytes;
			bytesToCopy -= bytes;
Warren Dukes's avatar
Warren Dukes committed
300 301
		}

Avuton Olrich's avatar
Avuton Olrich committed
302
		memcpy(od->buffer + curpos, playChunk, bytesToCopy);
303 304
		playChunk += bytesToCopy;

Warren Dukes's avatar
Warren Dukes committed
305
	}
306

307
	g_mutex_unlock(od->mutex);
308

309
	return true;
310 311
}

312
const struct audio_output_plugin osxPlugin = {
313 314 315 316 317 318 319 320
	.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,
321
};