1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Copyright 2003-2016 The Music Player Daemon Project
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include "OSXOutputPlugin.hxx"
#include "../OutputAPI.hxx"
#include "util/DynamicFifoBuffer.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "thread/Mutex.hxx"
#include "thread/Cond.hxx"
#include "system/ByteOrder.hxx"
#include "Log.hxx"
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include <CoreServices/CoreServices.h>
struct OSXOutput {
AudioOutput base;
/* configuration settings */
OSType component_subtype;
/* only applicable with kAudioUnitSubType_HALOutput */
const char *device_name;
AudioComponentInstance au;
AudioStreamBasicDescription asbd;
Mutex mutex;
Cond condition;
DynamicFifoBuffer<uint8_t> *buffer;
OSXOutput()
:base(osx_output_plugin) {}
};
static constexpr Domain osx_output_domain("osx_output");
static void
osx_os_status_to_cstring(OSStatus status, char *str, size_t size) {
CFErrorRef cferr = CFErrorCreate(nullptr, kCFErrorDomainOSStatus, status, nullptr);
CFStringRef cfstr = CFErrorCopyDescription(cferr);
if (!CFStringGetCString(cfstr, str, size, kCFStringEncodingUTF8)) {
/* conversion failed, return empty string */
*str = '\0';
}
if (cferr)
CFRelease(cferr);
if (cfstr)
CFRelease(cfstr);
}
static bool
osx_output_test_default_device(void)
{
/* on a Mac, this is always the default plugin, if nothing
else is configured */
return true;
}
static void
osx_output_configure(OSXOutput *oo, const ConfigBlock &block)
{
const char *device = block.GetBlockValue("device");
if (device == nullptr || 0 == strcmp(device, "default")) {
oo->component_subtype = kAudioUnitSubType_DefaultOutput;
oo->device_name = nullptr;
}
else if (0 == strcmp(device, "system")) {
oo->component_subtype = kAudioUnitSubType_SystemOutput;
oo->device_name = nullptr;
}
else {
oo->component_subtype = kAudioUnitSubType_HALOutput;
/* XXX am I supposed to strdup() this? */
oo->device_name = device;
}
}
static AudioOutput *
osx_output_init(const ConfigBlock &block, Error &error)
{
OSXOutput *oo = new OSXOutput();
if (!oo->base.Configure(block, error)) {
delete oo;
return nullptr;
}
osx_output_configure(oo, block);
return &oo->base;
}
static void
osx_output_finish(AudioOutput *ao)
{
OSXOutput *oo = (OSXOutput *)ao;
delete oo;
}
static bool
osx_output_set_device(OSXOutput *oo, Error &error)
{
bool ret = true;
OSStatus status;
UInt32 size, numdevices;
AudioDeviceID *deviceids = nullptr;
AudioObjectPropertyAddress propaddr;
CFStringRef cfname = nullptr;
char errormsg[1024];
char name[256];
unsigned int i;
if (oo->component_subtype != kAudioUnitSubType_HALOutput)
goto done;
/* how many audio devices are there? */
propaddr = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size);
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to determine number of OS X audio devices: %s",
errormsg);
ret = false;
goto done;
}
/* what are the available audio device IDs? */
numdevices = size / sizeof(AudioDeviceID);
deviceids = new AudioDeviceID[numdevices];
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size, deviceids);
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to determine OS X audio device IDs: %s",
errormsg);
ret = false;
goto done;
}
/* which audio device matches oo->device_name? */
propaddr = { kAudioObjectPropertyName, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
size = sizeof(CFStringRef);
for (i = 0; i < numdevices; i++) {
status = AudioObjectGetPropertyData(deviceids[i], &propaddr, 0, nullptr, &size, &cfname);
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to determine OS X device name "
"(device %u): %s",
(unsigned int) deviceids[i],
errormsg);
ret = false;
goto done;
}
if (!CFStringGetCString(cfname, name, sizeof(name), kCFStringEncodingUTF8)) {
error.Set(osx_output_domain, "Unable to convert device name from CFStringRef to char*");
ret = false;
goto done;
}
if (strcmp(oo->device_name, name) == 0) {
FormatDebug(osx_output_domain,
"found matching device: ID=%u, name=%s",
(unsigned)deviceids[i], name);
break;
}
}
if (i == numdevices) {
FormatWarning(osx_output_domain,
"Found no audio device with name '%s' "
"(will use default audio device)",
oo->device_name);
goto done;
}
status = AudioUnitSetProperty(oo->au,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
&(deviceids[i]),
sizeof(AudioDeviceID));
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to set OS X audio output device: %s",
errormsg);
ret = false;
goto done;
}
FormatDebug(osx_output_domain,
"set OS X audio output device ID=%u, name=%s",
(unsigned)deviceids[i], name);
done:
delete[] deviceids;
if (cfname)
CFRelease(cfname);
return ret;
}
/*
This function (the 'render callback' osx_render) is called by the
OS X audio subsystem (CoreAudio) to request audio data that will be
played by the audio hardware. This function has hard time constraints
so it cannot do IO (debug statements) or memory allocations.
The caller (i.e. CoreAudio) requests a specific number of
audio frames (in_number_frames) to be rendered into a
collection of output buffers (buffer_list). Depending on the
number of output buffers the render callback has to interleave
or de-interleave audio data to match the layout of the output
buffers. The intput buffer is always interleaved. In practice,
it seems this callback always gets a single output buffer
meaning that no de-interleaving actually takes place. For the
sake of correctness this callback allows for de-interleaving
anyway, and calculates the expected output layout by examining
the output buffers.
The input buffer is a DynamicFifoBuffer. When a
DynamicFifoBuffer contains less data than we want to read from
it there is no point in doing a second Read(). So in the case
of insufficient audio data in the input buffer, this render
callback will emit silence into the output buffers (memset
zero).
*/
static OSStatus
osx_render(void *vdata,
gcc_unused AudioUnitRenderActionFlags *io_action_flags,
gcc_unused const AudioTimeStamp *in_timestamp,
gcc_unused UInt32 in_bus_number,
UInt32 in_number_frames,
AudioBufferList *buffer_list)
{
AudioBuffer *output_buffer = nullptr;
size_t output_buffer_frame_size, dest;
OSXOutput *od = (OSXOutput *) vdata;
DynamicFifoBuffer<uint8_t> *input_buffer = od->buffer;
assert(input_buffer != nullptr);
/*
By convention when interfacing with audio hardware in CoreAudio,
in_bus_number equals 0 for output and 1 for input. Because this is an
audio output plugin our in_bus_number should always be 0.
*/
assert(in_bus_number == 0);
unsigned int input_channel_count = od->asbd.mChannelsPerFrame;
unsigned int output_channel_count = 0;
for (unsigned int i = 0 ; i < buffer_list->mNumberBuffers; ++i) {
output_buffer = &buffer_list->mBuffers[i];
assert(output_buffer->mData != nullptr);
output_channel_count += output_buffer->mNumberChannels;
}
assert(output_channel_count == input_channel_count);
size_t input_buffer_frame_size = od->asbd.mBytesPerFrame;
size_t sample_size = input_buffer_frame_size / input_channel_count;
// Acquire mutex when accessing input_buffer
od->mutex.lock();
auto src = input_buffer->Read();
UInt32 available_frames = src.size / input_buffer_frame_size;
// Never write more frames than we were asked
if (available_frames > in_number_frames)
available_frames = in_number_frames;
/*
To de-interleave the data in the input buffer so that it fits in
the output buffers we divide the input buffer frames into 'sub frames'
that fit into the output buffers.
*/
size_t sub_frame_offset = 0;
for (unsigned int i = 0 ; i < buffer_list->mNumberBuffers; ++i) {
output_buffer = &buffer_list->mBuffers[i];
output_buffer_frame_size = output_buffer->mNumberChannels * sample_size;
for (UInt32 current_frame = 0; current_frame < available_frames; ++current_frame) {
dest = (size_t) output_buffer->mData + current_frame * output_buffer_frame_size;
memcpy(
(void *) dest,
src.data + current_frame * input_buffer_frame_size + sub_frame_offset,
output_buffer_frame_size
);
}
sub_frame_offset += output_buffer_frame_size;
}
input_buffer->Consume(available_frames * input_buffer_frame_size);
od->condition.signal();
od->mutex.unlock();
if (available_frames < in_number_frames) {
for (unsigned int i = 0 ; i < buffer_list->mNumberBuffers; ++i) {
output_buffer = &buffer_list->mBuffers[i];
output_buffer_frame_size = output_buffer->mNumberChannels * sample_size;
dest = (size_t) output_buffer->mData + available_frames * output_buffer_frame_size;
memset(
(void *) dest,
0,
(in_number_frames - available_frames) * output_buffer_frame_size
);
}
}
return noErr;
}
static bool
osx_output_enable(AudioOutput *ao, Error &error)
{
char errormsg[1024];
OSXOutput *oo = (OSXOutput *)ao;
AudioComponentDescription desc;
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = oo->component_subtype;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
AudioComponent comp = AudioComponentFindNext(nullptr, &desc);
if (comp == 0) {
error.Set(osx_output_domain,
"Error finding OS X component");
return false;
}
OSStatus status = AudioComponentInstanceNew(comp, &oo->au);
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to open OS X component: %s",
errormsg);
return false;
}
if (!osx_output_set_device(oo, error)) {
AudioComponentInstanceDispose(oo->au);
return false;
}
AURenderCallbackStruct callback;
callback.inputProc = osx_render;
callback.inputProcRefCon = oo;
status =
AudioUnitSetProperty(oo->au,
kAudioUnitProperty_SetRenderCallback,
kAudioUnitScope_Input, 0,
&callback, sizeof(callback));
if (status != noErr) {
AudioComponentInstanceDispose(oo->au);
error.Set(osx_output_domain, status,
"unable to set callback for OS X audio unit");
return false;
}
return true;
}
static void
osx_output_disable(AudioOutput *ao)
{
OSXOutput *oo = (OSXOutput *)ao;
AudioComponentInstanceDispose(oo->au);
}
static void
osx_output_cancel(AudioOutput *ao)
{
OSXOutput *od = (OSXOutput *)ao;
const ScopeLock protect(od->mutex);
od->buffer->Clear();
}
static void
osx_output_close(AudioOutput *ao)
{
OSXOutput *od = (OSXOutput *)ao;
AudioOutputUnitStop(od->au);
AudioUnitUninitialize(od->au);
delete od->buffer;
}
static bool
osx_output_open(AudioOutput *ao, AudioFormat &audio_format,
Error &error)
{
char errormsg[1024];
OSXOutput *od = (OSXOutput *)ao;
memset(&od->asbd, 0, sizeof(od->asbd));
od->asbd.mSampleRate = audio_format.sample_rate;
od->asbd.mFormatID = kAudioFormatLinearPCM;
od->asbd.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
switch (audio_format.format) {
case SampleFormat::S8:
od->asbd.mBitsPerChannel = 8;
break;
case SampleFormat::S16:
od->asbd.mBitsPerChannel = 16;
break;
case SampleFormat::S32:
od->asbd.mBitsPerChannel = 32;
break;
default:
audio_format.format = SampleFormat::S32;
od->asbd.mBitsPerChannel = 32;
break;
}
if (IsBigEndian())
od->asbd.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
od->asbd.mBytesPerPacket = audio_format.GetFrameSize();
od->asbd.mFramesPerPacket = 1;
od->asbd.mBytesPerFrame = od->asbd.mBytesPerPacket;
od->asbd.mChannelsPerFrame = audio_format.channels;
OSStatus status =
AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
kAudioUnitScope_Input, 0,
&od->asbd,
sizeof(od->asbd));
if (status != noErr) {
error.Set(osx_output_domain, status,
"Unable to set format on OS X device");
return false;
}
status = AudioUnitInitialize(od->au);
if (status != noErr) {
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"Unable to initialize OS X audio unit: %s",
errormsg);
return false;
}
/* create a buffer of 1s */
od->buffer = new DynamicFifoBuffer<uint8_t>(audio_format.sample_rate *
audio_format.GetFrameSize());
status = AudioOutputUnitStart(od->au);
if (status != 0) {
AudioUnitUninitialize(od->au);
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
error.Format(osx_output_domain, status,
"unable to start audio output: %s",
errormsg);
return false;
}
return true;
}
static size_t
osx_output_play(AudioOutput *ao, const void *chunk, size_t size,
gcc_unused Error &error)
{
OSXOutput *od = (OSXOutput *)ao;
const ScopeLock protect(od->mutex);
DynamicFifoBuffer<uint8_t>::Range dest;
while (true) {
dest = od->buffer->Write();
if (!dest.IsEmpty())
break;
/* wait for some free space in the buffer */
od->condition.wait(od->mutex);
}
if (size > dest.size)
size = dest.size;
memcpy(dest.data, chunk, size);
od->buffer->Append(size);
return size;
}
const struct AudioOutputPlugin osx_output_plugin = {
"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,
};