2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2005-03-13 20:23:09 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2005-03-13 20:23:09 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-29 16:59:21 +01:00
|
|
|
#include "OSXOutputPlugin.hxx"
|
2014-01-23 23:49:50 +01:00
|
|
|
#include "../OutputAPI.hxx"
|
2013-12-19 12:46:10 +01:00
|
|
|
#include "util/DynamicFifoBuffer.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2013-01-29 16:59:21 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
#include "thread/Cond.hxx"
|
2013-10-16 21:09:19 +02:00
|
|
|
#include "system/ByteOrder.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
#include <CoreAudio/CoreAudio.h>
|
2008-10-26 21:58:37 +01:00
|
|
|
#include <AudioUnit/AudioUnit.h>
|
2009-09-20 23:30:37 +02:00
|
|
|
#include <CoreServices/CoreServices.h>
|
2008-10-26 21:58:37 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
struct OSXOutput {
|
2014-01-28 11:34:09 +01:00
|
|
|
AudioOutput base;
|
2011-09-16 23:31:48 +02:00
|
|
|
|
2010-12-21 04:21:46 +01:00
|
|
|
/* configuration settings */
|
|
|
|
OSType component_subtype;
|
|
|
|
/* only applicable with kAudioUnitSubType_HALOutput */
|
|
|
|
const char *device_name;
|
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponentInstance au;
|
2013-01-29 16:59:21 +01:00
|
|
|
Mutex mutex;
|
|
|
|
Cond condition;
|
2012-04-05 00:45:39 +02:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
DynamicFifoBuffer<uint8_t> *buffer;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
|
|
|
OSXOutput()
|
|
|
|
:base(osx_output_plugin) {}
|
2009-02-26 21:03:06 +01:00
|
|
|
};
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain osx_output_domain("osx_output");
|
2009-02-26 22:04:59 +01:00
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static bool
|
|
|
|
osx_output_test_default_device(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-02-26 21:33:13 +01:00
|
|
|
/* on a Mac, this is always the default plugin, if nothing
|
|
|
|
else is configured */
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2005-03-13 22:33:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-21 04:21:46 +01:00
|
|
|
static void
|
2015-01-21 22:13:44 +01:00
|
|
|
osx_output_configure(OSXOutput *oo, const ConfigBlock &block)
|
2010-12-21 04:21:46 +01:00
|
|
|
{
|
2015-01-21 22:13:44 +01:00
|
|
|
const char *device = block.GetBlockValue("device");
|
2010-12-21 04:21:46 +01:00
|
|
|
|
2014-12-31 11:47:27 +01:00
|
|
|
if (device == nullptr || 0 == strcmp(device, "default")) {
|
2010-12-21 04:21:46 +01:00
|
|
|
oo->component_subtype = kAudioUnitSubType_DefaultOutput;
|
2014-12-31 11:47:27 +01:00
|
|
|
oo->device_name = nullptr;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(device, "system")) {
|
|
|
|
oo->component_subtype = kAudioUnitSubType_SystemOutput;
|
2014-12-31 11:47:27 +01:00
|
|
|
oo->device_name = nullptr;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
oo->component_subtype = kAudioUnitSubType_HALOutput;
|
2014-01-07 23:33:46 +01:00
|
|
|
/* XXX am I supposed to strdup() this? */
|
2010-12-21 04:21:46 +01:00
|
|
|
oo->device_name = device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 11:34:09 +01:00
|
|
|
static AudioOutput *
|
2015-01-21 22:13:44 +01:00
|
|
|
osx_output_init(const ConfigBlock &block, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *oo = new OSXOutput();
|
2015-01-21 22:13:44 +01:00
|
|
|
if (!oo->base.Configure(block, error)) {
|
2013-01-29 16:59:21 +01:00
|
|
|
delete oo;
|
2014-12-31 11:47:27 +01:00
|
|
|
return nullptr;
|
2011-09-16 23:31:48 +02:00
|
|
|
}
|
2009-02-26 21:03:06 +01:00
|
|
|
|
2015-01-21 22:13:44 +01:00
|
|
|
osx_output_configure(oo, block);
|
2009-02-26 21:03:06 +01:00
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
return &oo->base;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 23:31:48 +02:00
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_finish(AudioOutput *ao)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *oo = (OSXOutput *)ao;
|
2009-02-26 21:03:06 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
delete oo;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2010-12-21 04:21:46 +01:00
|
|
|
static bool
|
2013-08-10 18:02:44 +02:00
|
|
|
osx_output_set_device(OSXOutput *oo, Error &error)
|
2010-12-21 04:21:46 +01:00
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
OSStatus status;
|
|
|
|
UInt32 size, numdevices;
|
2014-12-31 11:47:27 +01:00
|
|
|
AudioDeviceID *deviceids = nullptr;
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioObjectPropertyAddress propaddr;
|
2016-07-03 12:59:19 +02:00
|
|
|
CFStringRef cfname = nullptr;
|
2010-12-21 04:21:46 +01:00
|
|
|
char name[256];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (oo->component_subtype != kAudioUnitSubType_HALOutput)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* how many audio devices are there? */
|
2016-07-02 23:35:44 +02:00
|
|
|
propaddr = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
|
|
|
|
status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size);
|
2010-12-21 04:21:46 +01:00
|
|
|
if (status != noErr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to determine number of OS X audio devices: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2010-12-21 04:21:46 +01:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* what are the available audio device IDs? */
|
|
|
|
numdevices = size / sizeof(AudioDeviceID);
|
2013-01-29 16:59:21 +01:00
|
|
|
deviceids = new AudioDeviceID[numdevices];
|
2016-07-02 23:35:44 +02:00
|
|
|
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size, deviceids);
|
2010-12-21 04:21:46 +01:00
|
|
|
if (status != noErr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to determine OS X audio device IDs: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2010-12-21 04:21:46 +01:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* which audio device matches oo->device_name? */
|
2016-07-02 23:35:44 +02:00
|
|
|
propaddr = { kAudioObjectPropertyName, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
|
2016-07-03 13:18:44 +02:00
|
|
|
size = sizeof(CFStringRef);
|
2010-12-21 04:21:46 +01:00
|
|
|
for (i = 0; i < numdevices; i++) {
|
2016-07-03 12:59:19 +02:00
|
|
|
status = AudioObjectGetPropertyData(deviceids[i], &propaddr, 0, nullptr, &size, &cfname);
|
2010-12-21 04:21:46 +01:00
|
|
|
if (status != noErr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to determine OS X device name "
|
|
|
|
"(device %u): %s",
|
|
|
|
(unsigned int) deviceids[i],
|
|
|
|
GetMacOSStatusCommentString(status));
|
2010-12-21 04:21:46 +01:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
|
|
|
}
|
2016-07-03 13:18:44 +02:00
|
|
|
|
2016-07-03 12:59:19 +02:00
|
|
|
if (!CFStringGetCString(cfname, name, sizeof(name), kCFStringEncodingUTF8)) {
|
2016-07-03 13:36:35 +02:00
|
|
|
error.Set(osx_output_domain, "Unable to convert device name from CFStringRef to char*");
|
2016-07-03 12:59:19 +02:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2010-12-21 04:21:46 +01:00
|
|
|
if (strcmp(oo->device_name, name) == 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"found matching device: ID=%u, name=%s",
|
|
|
|
(unsigned)deviceids[i], name);
|
2010-12-21 04:21:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == numdevices) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(osx_output_domain,
|
|
|
|
"Found no audio device with name '%s' "
|
|
|
|
"(will use default audio device)",
|
|
|
|
oo->device_name);
|
2010-12-21 04:21:46 +01:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = AudioUnitSetProperty(oo->au,
|
|
|
|
kAudioOutputUnitProperty_CurrentDevice,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0,
|
|
|
|
&(deviceids[i]),
|
|
|
|
sizeof(AudioDeviceID));
|
|
|
|
if (status != noErr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to set OS X audio output device: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2010-12-21 04:21:46 +01:00
|
|
|
ret = false;
|
|
|
|
goto done;
|
|
|
|
}
|
2013-09-27 22:31:24 +02:00
|
|
|
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"set OS X audio output device ID=%u, name=%s",
|
|
|
|
(unsigned)deviceids[i], name);
|
2010-12-21 04:21:46 +01:00
|
|
|
|
|
|
|
done:
|
2013-01-29 16:59:21 +01:00
|
|
|
delete[] deviceids;
|
2016-07-03 12:59:19 +02:00
|
|
|
if (cfname) {
|
|
|
|
CFRelease(cfname);
|
|
|
|
}
|
2010-12-21 04:21:46 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-24 15:55:35 +01:00
|
|
|
static OSStatus
|
|
|
|
osx_render(void *vdata,
|
2013-08-04 23:48:01 +02:00
|
|
|
gcc_unused AudioUnitRenderActionFlags *io_action_flags,
|
|
|
|
gcc_unused const AudioTimeStamp *in_timestamp,
|
|
|
|
gcc_unused UInt32 in_bus_number,
|
|
|
|
gcc_unused UInt32 in_number_frames,
|
2011-12-24 15:55:35 +01:00
|
|
|
AudioBufferList *buffer_list)
|
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *od = (OSXOutput *) vdata;
|
2011-12-24 15:55:35 +01:00
|
|
|
AudioBuffer *buffer = &buffer_list->mBuffers[0];
|
|
|
|
size_t buffer_size = buffer->mDataByteSize;
|
|
|
|
|
2014-12-31 11:47:27 +01:00
|
|
|
assert(od->buffer != nullptr);
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
od->mutex.lock();
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
auto src = od->buffer->Read();
|
|
|
|
if (!src.IsEmpty()) {
|
|
|
|
if (src.size > buffer_size)
|
|
|
|
src.size = buffer_size;
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
memcpy(buffer->mData, src.data, src.size);
|
|
|
|
od->buffer->Consume(src.size);
|
|
|
|
}
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
od->condition.signal();
|
|
|
|
od->mutex.unlock();
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
buffer->mDataByteSize = src.size;
|
2012-10-02 17:18:41 +02:00
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
for (i = 1; i < buffer_list->mNumberBuffers; ++i) {
|
|
|
|
buffer = &buffer_list->mBuffers[i];
|
|
|
|
buffer->mDataByteSize = 0;
|
|
|
|
}
|
2011-12-24 15:55:35 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
static bool
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_enable(AudioOutput *ao, Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *oo = (OSXOutput *)ao;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponentDescription desc;
|
2005-03-16 05:46:41 +01:00
|
|
|
desc.componentType = kAudioUnitType_Output;
|
2011-12-24 15:55:35 +01:00
|
|
|
desc.componentSubType = oo->component_subtype;
|
2005-03-16 05:46:41 +01:00
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
desc.componentFlags = 0;
|
|
|
|
desc.componentFlagsMask = 0;
|
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponent comp = AudioComponentFindNext(nullptr, &desc);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (comp == 0) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Set(osx_output_domain,
|
|
|
|
"Error finding OS X component");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
OSStatus status = AudioComponentInstanceNew(comp, &oo->au);
|
2009-02-26 22:01:42 +01:00
|
|
|
if (status != noErr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to open OS X component: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
if (!osx_output_set_device(oo, error)) {
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponentInstanceDispose(oo->au);
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-24 15:55:35 +01:00
|
|
|
AURenderCallbackStruct callback;
|
2005-03-16 05:46:41 +01:00
|
|
|
callback.inputProc = osx_render;
|
2011-12-24 15:55:35 +01:00
|
|
|
callback.inputProcRefCon = oo;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
status =
|
2011-12-24 15:55:35 +01:00
|
|
|
AudioUnitSetProperty(oo->au,
|
|
|
|
kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&callback, sizeof(callback));
|
2016-07-02 23:35:44 +02:00
|
|
|
if (status != noErr) {
|
|
|
|
AudioComponentInstanceDispose(oo->au);
|
|
|
|
error.Set(osx_output_domain, status,
|
2013-08-10 18:02:44 +02:00
|
|
|
"unable to set callback for OS X audio unit");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2011-12-24 15:55:35 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_disable(AudioOutput *ao)
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *oo = (OSXOutput *)ao;
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponentInstanceDispose(oo->au);
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_cancel(AudioOutput *ao)
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *od = (OSXOutput *)ao;
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
const ScopeLock protect(od->mutex);
|
2013-12-19 10:49:31 +01:00
|
|
|
od->buffer->Clear();
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_close(AudioOutput *ao)
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *od = (OSXOutput *)ao;
|
2011-12-24 15:55:35 +01:00
|
|
|
|
|
|
|
AudioOutputUnitStop(od->au);
|
|
|
|
AudioUnitUninitialize(od->au);
|
2012-04-05 00:45:39 +02:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
delete od->buffer;
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_open(AudioOutput *ao, AudioFormat &audio_format,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *od = (OSXOutput *)ao;
|
2011-12-24 15:55:35 +01:00
|
|
|
|
|
|
|
AudioStreamBasicDescription stream_description;
|
2013-08-03 21:00:50 +02:00
|
|
|
stream_description.mSampleRate = audio_format.sample_rate;
|
2009-02-26 21:03:06 +01:00
|
|
|
stream_description.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
stream_description.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2009-11-10 17:11:34 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
switch (audio_format.format) {
|
|
|
|
case SampleFormat::S8:
|
2009-11-10 17:11:34 +01:00
|
|
|
stream_description.mBitsPerChannel = 8;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2009-11-10 17:11:34 +01:00
|
|
|
stream_description.mBitsPerChannel = 16;
|
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S32:
|
2011-12-24 18:18:42 +01:00
|
|
|
stream_description.mBitsPerChannel = 32;
|
|
|
|
break;
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
default:
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S32;
|
2011-12-24 18:18:42 +01:00
|
|
|
stream_description.mBitsPerChannel = 32;
|
2009-11-10 17:11:34 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2013-10-16 21:09:19 +02:00
|
|
|
if (IsBigEndian())
|
|
|
|
stream_description.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
2011-01-07 17:15:37 +01:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
stream_description.mBytesPerPacket = audio_format.GetFrameSize();
|
2011-01-07 17:15:37 +01:00
|
|
|
stream_description.mFramesPerPacket = 1;
|
|
|
|
stream_description.mBytesPerFrame = stream_description.mBytesPerPacket;
|
2013-08-03 21:00:50 +02:00
|
|
|
stream_description.mChannelsPerFrame = audio_format.channels;
|
2011-01-07 17:15:37 +01:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
OSStatus status =
|
2011-12-24 15:55:35 +01:00
|
|
|
AudioUnitSetProperty(od->au, kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&stream_description,
|
|
|
|
sizeof(stream_description));
|
2016-07-02 23:35:44 +02:00
|
|
|
if (status != noErr) {
|
|
|
|
error.Set(osx_output_domain, status,
|
2013-08-10 18:02:44 +02:00
|
|
|
"Unable to set format on OS X device");
|
2008-10-29 20:40:27 +01:00
|
|
|
return false;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
status = AudioUnitInitialize(od->au);
|
2011-12-24 15:55:35 +01:00
|
|
|
if (status != noErr) {
|
2013-12-10 19:16:37 +01:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"Unable to initialize OS X audio unit: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2011-12-24 15:55:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-03-17 04:28:05 +01:00
|
|
|
/* create a buffer of 1s */
|
2013-12-19 10:49:31 +01:00
|
|
|
od->buffer = new DynamicFifoBuffer<uint8_t>(audio_format.sample_rate *
|
|
|
|
audio_format.GetFrameSize());
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2009-02-26 22:01:42 +01:00
|
|
|
status = AudioOutputUnitStart(od->au);
|
|
|
|
if (status != 0) {
|
2011-12-24 15:55:35 +01:00
|
|
|
AudioUnitUninitialize(od->au);
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(osx_output_domain, status,
|
|
|
|
"unable to start audio output: %s",
|
|
|
|
GetMacOSStatusCommentString(status));
|
2009-02-26 21:40:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-29 20:40:27 +01:00
|
|
|
return true;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2009-02-23 09:29:56 +01:00
|
|
|
static size_t
|
2014-01-28 11:34:09 +01:00
|
|
|
osx_output_play(AudioOutput *ao, const void *chunk, size_t size,
|
2013-08-10 18:02:44 +02:00
|
|
|
gcc_unused Error &error)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-29 16:59:21 +01:00
|
|
|
OSXOutput *od = (OSXOutput *)ao;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2013-01-29 16:59:21 +01:00
|
|
|
const ScopeLock protect(od->mutex);
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
DynamicFifoBuffer<uint8_t>::Range dest;
|
2012-04-05 00:45:39 +02:00
|
|
|
while (true) {
|
2013-12-19 10:49:31 +01:00
|
|
|
dest = od->buffer->Write();
|
|
|
|
if (!dest.IsEmpty())
|
2012-04-05 00:45:39 +02:00
|
|
|
break;
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2012-04-05 00:45:39 +02:00
|
|
|
/* wait for some free space in the buffer */
|
2013-01-29 16:59:21 +01:00
|
|
|
od->condition.wait(od->mutex);
|
2012-04-05 00:45:39 +02:00
|
|
|
}
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
if (size > dest.size)
|
|
|
|
size = dest.size;
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2013-12-19 10:49:31 +01:00
|
|
|
memcpy(dest.data, chunk, size);
|
|
|
|
od->buffer->Append(size);
|
2009-02-10 20:57:21 +01:00
|
|
|
|
2012-04-05 00:45:39 +02:00
|
|
|
return size;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2014-01-28 11:22:27 +01:00
|
|
|
const struct AudioOutputPlugin osx_output_plugin = {
|
2013-01-29 16:59:21 +01:00
|
|
|
"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,
|
2005-03-13 20:23:09 +01:00
|
|
|
};
|