OSXOutputPlugin.cxx 10.3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "OSXOutputPlugin.hxx"
22
#include "../OutputAPI.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "util/DynamicFifoBuffer.hxx"
24 25
#include "util/Error.hxx"
#include "util/Domain.hxx"
26 27
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
28
#include "system/ByteOrder.hxx"
29
#include "Log.hxx"
30

Rich Healey's avatar
Rich Healey committed
31
#include <CoreAudio/AudioHardware.h>
32
#include <AudioUnit/AudioUnit.h>
33
#include <CoreServices/CoreServices.h>
34

35
struct OSXOutput {
36
	AudioOutput base;
37

38 39 40 41 42
	/* configuration settings */
	OSType component_subtype;
	/* only applicable with kAudioUnitSubType_HALOutput */
	const char *device_name;

43
	AudioUnit au;
44 45
	Mutex mutex;
	Cond condition;
Max Kellermann's avatar
Max Kellermann committed
46

47
	DynamicFifoBuffer<uint8_t> *buffer;
48 49 50

	OSXOutput()
		:base(osx_output_plugin) {}
Max Kellermann's avatar
Max Kellermann committed
51
};
52

53
static constexpr Domain osx_output_domain("osx_output");
54

Max Kellermann's avatar
Max Kellermann committed
55 56
static bool
osx_output_test_default_device(void)
Avuton Olrich's avatar
Avuton Olrich committed
57
{
58 59
	/* on a Mac, this is always the default plugin, if nothing
	   else is configured */
60
	return true;
Warren Dukes's avatar
Warren Dukes committed
61 62
}

63
static void
64
osx_output_configure(OSXOutput *oo, const config_param &param)
65
{
66
	const char *device = param.GetBlockValue("device");
67 68 69 70 71 72 73 74 75 76 77

	if (device == NULL || 0 == strcmp(device, "default")) {
		oo->component_subtype = kAudioUnitSubType_DefaultOutput;
		oo->device_name = NULL;
	}
	else if (0 == strcmp(device, "system")) {
		oo->component_subtype = kAudioUnitSubType_SystemOutput;
		oo->device_name = NULL;
	}
	else {
		oo->component_subtype = kAudioUnitSubType_HALOutput;
78
		/* XXX am I supposed to strdup() this? */
79 80 81 82
		oo->device_name = device;
	}
}

83
static AudioOutput *
84
osx_output_init(const config_param &param, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
85
{
86
	OSXOutput *oo = new OSXOutput();
87
	if (!oo->base.Configure(param, error)) {
88
		delete oo;
89 90
		return NULL;
	}
Max Kellermann's avatar
Max Kellermann committed
91

92
	osx_output_configure(oo, param);
Max Kellermann's avatar
Max Kellermann committed
93

94
	return &oo->base;
95 96
}

97
static void
98
osx_output_finish(AudioOutput *ao)
Avuton Olrich's avatar
Avuton Olrich committed
99
{
100
	OSXOutput *oo = (OSXOutput *)ao;
Max Kellermann's avatar
Max Kellermann committed
101

102
	delete oo;
103 104
}

105
static bool
106
osx_output_set_device(OSXOutput *oo, Error &error)
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
{
	bool ret = true;
	OSStatus status;
	UInt32 size, numdevices;
	AudioDeviceID *deviceids = NULL;
	char name[256];
	unsigned int i;

	if (oo->component_subtype != kAudioUnitSubType_HALOutput)
		goto done;

	/* how many audio devices are there? */
	status = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
					      &size,
					      NULL);
	if (status != noErr) {
123 124 125
		error.Format(osx_output_domain, status,
			     "Unable to determine number of OS X audio devices: %s",
			     GetMacOSStatusCommentString(status));
126 127 128 129 130 131
		ret = false;
		goto done;
	}

	/* what are the available audio device IDs? */
	numdevices = size / sizeof(AudioDeviceID);
132
	deviceids = new AudioDeviceID[numdevices];
133 134 135 136
	status = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
					  &size,
					  deviceids);
	if (status != noErr) {
137 138 139
		error.Format(osx_output_domain, status,
			     "Unable to determine OS X audio device IDs: %s",
			     GetMacOSStatusCommentString(status));
140 141 142 143 144 145 146 147 148 149 150
		ret = false;
		goto done;
	}

	/* which audio device matches oo->device_name? */
	for (i = 0; i < numdevices; i++) {
		size = sizeof(name);
		status = AudioDeviceGetProperty(deviceids[i], 0, false,
						kAudioDevicePropertyDeviceName,
						&size, name);
		if (status != noErr) {
151 152 153 154 155
			error.Format(osx_output_domain, status,
				     "Unable to determine OS X device name "
				     "(device %u): %s",
				     (unsigned int) deviceids[i],
				     GetMacOSStatusCommentString(status));
156 157 158 159
			ret = false;
			goto done;
		}
		if (strcmp(oo->device_name, name) == 0) {
160 161 162
			FormatDebug(osx_output_domain,
				    "found matching device: ID=%u, name=%s",
				    (unsigned)deviceids[i], name);
163 164 165 166
			break;
		}
	}
	if (i == numdevices) {
167 168 169 170
		FormatWarning(osx_output_domain,
			      "Found no audio device with name '%s' "
			      "(will use default audio device)",
			      oo->device_name);
171 172 173 174 175 176 177 178 179 180
		goto done;
	}

	status = AudioUnitSetProperty(oo->au,
				      kAudioOutputUnitProperty_CurrentDevice,
				      kAudioUnitScope_Global,
				      0,
				      &(deviceids[i]),
				      sizeof(AudioDeviceID));
	if (status != noErr) {
181 182 183
		error.Format(osx_output_domain, status,
			     "Unable to set OS X audio output device: %s",
			     GetMacOSStatusCommentString(status));
184 185 186
		ret = false;
		goto done;
	}
187 188 189 190

	FormatDebug(osx_output_domain,
		    "set OS X audio output device ID=%u, name=%s",
		    (unsigned)deviceids[i], name);
191 192

done:
193
	delete[] deviceids;
194 195 196
	return ret;
}

197 198
static OSStatus
osx_render(void *vdata,
199 200 201 202
	   gcc_unused AudioUnitRenderActionFlags *io_action_flags,
	   gcc_unused const AudioTimeStamp *in_timestamp,
	   gcc_unused UInt32 in_bus_number,
	   gcc_unused UInt32 in_number_frames,
203 204
	   AudioBufferList *buffer_list)
{
205
	OSXOutput *od = (OSXOutput *) vdata;
206 207 208
	AudioBuffer *buffer = &buffer_list->mBuffers[0];
	size_t buffer_size = buffer->mDataByteSize;

Max Kellermann's avatar
Max Kellermann committed
209
	assert(od->buffer != NULL);
210

211
	od->mutex.lock();
212

213 214 215 216
	auto src = od->buffer->Read();
	if (!src.IsEmpty()) {
		if (src.size > buffer_size)
			src.size = buffer_size;
217

218 219 220
		memcpy(buffer->mData, src.data, src.size);
		od->buffer->Consume(src.size);
	}
221

222 223
	od->condition.signal();
	od->mutex.unlock();
224

225
	buffer->mDataByteSize = src.size;
226 227 228 229 230 231

	unsigned i;
	for (i = 1; i < buffer_list->mNumberBuffers; ++i) {
		buffer = &buffer_list->mBuffers[i];
		buffer->mDataByteSize = 0;
	}
232 233 234 235

	return 0;
}

236
static bool
237
osx_output_enable(AudioOutput *ao, Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
238
{
239
	OSXOutput *oo = (OSXOutput *)ao;
240

241
	ComponentDescription desc;
242
	desc.componentType = kAudioUnitType_Output;
243
	desc.componentSubType = oo->component_subtype;
244 245 246 247
	desc.componentManufacturer = kAudioUnitManufacturer_Apple;
	desc.componentFlags = 0;
	desc.componentFlagsMask = 0;

248
	Component comp = FindNextComponent(NULL, &desc);
Avuton Olrich's avatar
Avuton Olrich committed
249
	if (comp == 0) {
250 251
		error.Set(osx_output_domain,
			  "Error finding OS X component");
252
		return false;
Warren Dukes's avatar
Warren Dukes committed
253 254
	}

255
	OSStatus status = OpenAComponent(comp, &oo->au);
256
	if (status != noErr) {
257 258 259
		error.Format(osx_output_domain, status,
			     "Unable to open OS X component: %s",
			     GetMacOSStatusCommentString(status));
260
		return false;
Warren Dukes's avatar
Warren Dukes committed
261 262
	}

263
	if (!osx_output_set_device(oo, error)) {
264
		CloseComponent(oo->au);
265
		return false;
266 267
	}

268
	AURenderCallbackStruct callback;
269
	callback.inputProc = osx_render;
270
	callback.inputProcRefCon = oo;
271

272 273 274 275 276
	ComponentResult result =
		AudioUnitSetProperty(oo->au,
				     kAudioUnitProperty_SetRenderCallback,
				     kAudioUnitScope_Input, 0,
				     &callback, sizeof(callback));
277
	if (result != noErr) {
278
		CloseComponent(oo->au);
279 280
		error.Set(osx_output_domain, result,
			  "unable to set callback for OS X audio unit");
281
		return false;
282
	}
Warren Dukes's avatar
Warren Dukes committed
283

284 285 286 287
	return true;
}

static void
288
osx_output_disable(AudioOutput *ao)
289
{
290
	OSXOutput *oo = (OSXOutput *)ao;
291 292 293 294 295

	CloseComponent(oo->au);
}

static void
296
osx_output_cancel(AudioOutput *ao)
297
{
298
	OSXOutput *od = (OSXOutput *)ao;
299

300
	const ScopeLock protect(od->mutex);
301
	od->buffer->Clear();
302 303 304
}

static void
305
osx_output_close(AudioOutput *ao)
306
{
307
	OSXOutput *od = (OSXOutput *)ao;
308 309 310

	AudioOutputUnitStop(od->au);
	AudioUnitUninitialize(od->au);
Max Kellermann's avatar
Max Kellermann committed
311

312
	delete od->buffer;
313 314 315
}

static bool
316
osx_output_open(AudioOutput *ao, AudioFormat &audio_format,
317
		Error &error)
318
{
319
	OSXOutput *od = (OSXOutput *)ao;
320 321

	AudioStreamBasicDescription stream_description;
322
	stream_description.mSampleRate = audio_format.sample_rate;
Max Kellermann's avatar
Max Kellermann committed
323 324
	stream_description.mFormatID = kAudioFormatLinearPCM;
	stream_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
325

326 327
	switch (audio_format.format) {
	case SampleFormat::S8:
328 329 330
		stream_description.mBitsPerChannel = 8;
		break;

331
	case SampleFormat::S16:
332 333 334
		stream_description.mBitsPerChannel = 16;
		break;

335
	case SampleFormat::S32:
336 337 338
		stream_description.mBitsPerChannel = 32;
		break;

339
	default:
340
		audio_format.format = SampleFormat::S32;
341
		stream_description.mBitsPerChannel = 32;
342 343
		break;
	}
344

345 346
	if (IsBigEndian())
		stream_description.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
347

348
	stream_description.mBytesPerPacket = audio_format.GetFrameSize();
349 350
	stream_description.mFramesPerPacket = 1;
	stream_description.mBytesPerFrame = stream_description.mBytesPerPacket;
351
	stream_description.mChannelsPerFrame = audio_format.channels;
352

353 354 355 356 357
	ComponentResult result =
		AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
				     kAudioUnitScope_Input, 0,
				     &stream_description,
				     sizeof(stream_description));
358
	if (result != noErr) {
359 360
		error.Set(osx_output_domain, result,
			  "Unable to set format on OS X device");
361
		return false;
362 363
	}

364 365
	OSStatus status = AudioUnitInitialize(od->au);
	if (status != noErr) {
366 367 368
		error.Format(osx_output_domain, status,
			     "Unable to initialize OS X audio unit: %s",
			     GetMacOSStatusCommentString(status));
369 370 371
		return false;
	}

372
	/* create a buffer of 1s */
373 374
	od->buffer = new DynamicFifoBuffer<uint8_t>(audio_format.sample_rate *
						    audio_format.GetFrameSize());
375

376 377
	status = AudioOutputUnitStart(od->au);
	if (status != 0) {
378
		AudioUnitUninitialize(od->au);
379 380 381
		error.Format(osx_output_domain, status,
			     "unable to start audio output: %s",
			     GetMacOSStatusCommentString(status));
382 383 384
		return false;
	}

385
	return true;
386 387
}

388
static size_t
389
osx_output_play(AudioOutput *ao, const void *chunk, size_t size,
390
		gcc_unused Error &error)
Avuton Olrich's avatar
Avuton Olrich committed
391
{
392
	OSXOutput *od = (OSXOutput *)ao;
Warren Dukes's avatar
Warren Dukes committed
393

394
	const ScopeLock protect(od->mutex);
Warren Dukes's avatar
Warren Dukes committed
395

396
	DynamicFifoBuffer<uint8_t>::Range dest;
Max Kellermann's avatar
Max Kellermann committed
397
	while (true) {
398 399
		dest = od->buffer->Write();
		if (!dest.IsEmpty())
Max Kellermann's avatar
Max Kellermann committed
400
			break;
Warren Dukes's avatar
Warren Dukes committed
401

Max Kellermann's avatar
Max Kellermann committed
402
		/* wait for some free space in the buffer */
403
		od->condition.wait(od->mutex);
Max Kellermann's avatar
Max Kellermann committed
404
	}
405

406 407
	if (size > dest.size)
		size = dest.size;
408

409 410
	memcpy(dest.data, chunk, size);
	od->buffer->Append(size);
411

Max Kellermann's avatar
Max Kellermann committed
412
	return size;
413 414
}

415
const struct AudioOutputPlugin osx_output_plugin = {
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	"osx",
	osx_output_test_default_device,
	osx_output_init,
	osx_output_finish,
	osx_output_enable,
	osx_output_disable,
	osx_output_open,
	osx_output_close,
	nullptr,
	nullptr,
	osx_output_play,
	nullptr,
	osx_output_cancel,
	nullptr,
	nullptr,
431
};