2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 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"
|
2017-08-21 18:10:12 +02:00
|
|
|
#include "mixer/MixerList.hxx"
|
2016-11-09 11:49:21 +01:00
|
|
|
#include "util/ScopeExit.hxx"
|
2016-11-09 11:43:11 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#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>
|
2016-09-19 04:40:55 +02:00
|
|
|
#include <boost/lockfree/spsc_queue.hpp>
|
2016-08-05 23:42:25 +02:00
|
|
|
|
2016-11-09 11:46:32 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
struct OSXOutput final : AudioOutput {
|
2010-12-21 04:21:46 +01:00
|
|
|
/* configuration settings */
|
|
|
|
OSType component_subtype;
|
|
|
|
/* only applicable with kAudioUnitSubType_HALOutput */
|
|
|
|
const char *device_name;
|
2016-07-09 13:03:41 +02:00
|
|
|
const char *channel_map;
|
2016-09-19 04:12:45 +02:00
|
|
|
bool hog_device;
|
|
|
|
bool sync_sample_rate;
|
2010-12-21 04:21:46 +01:00
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
AudioDeviceID dev_id;
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioComponentInstance au;
|
2016-07-07 23:53:05 +02:00
|
|
|
AudioStreamBasicDescription asbd;
|
2016-08-05 23:42:25 +02:00
|
|
|
|
|
|
|
boost::lockfree::spsc_queue<uint8_t> *ring_buffer;
|
2014-01-28 23:39:48 +01:00
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
OSXOutput(const ConfigBlock &block);
|
2017-08-09 16:58:44 +02:00
|
|
|
|
|
|
|
static AudioOutput *Create(EventLoop &, const ConfigBlock &block);
|
2017-08-21 18:10:12 +02:00
|
|
|
int GetVolume();
|
|
|
|
void SetVolume(unsigned new_volume);
|
2017-08-09 16:58:44 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Enable() override;
|
|
|
|
void Disable() noexcept override;
|
|
|
|
|
|
|
|
void Open(AudioFormat &audio_format) override;
|
|
|
|
void Close() noexcept override;
|
|
|
|
|
|
|
|
std::chrono::steady_clock::duration Delay() const noexcept override;
|
|
|
|
size_t Play(const void *chunk, size_t size) override;
|
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
|
|
|
|
2016-07-03 14:06:53 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
OSXOutput::OSXOutput(const ConfigBlock &block)
|
2017-08-09 16:58:44 +02:00
|
|
|
:AudioOutput(FLAG_ENABLE_DISABLE)
|
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")) {
|
2016-11-09 11:43:11 +01:00
|
|
|
component_subtype = kAudioUnitSubType_DefaultOutput;
|
|
|
|
device_name = nullptr;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
else if (0 == strcmp(device, "system")) {
|
2016-11-09 11:43:11 +01:00
|
|
|
component_subtype = kAudioUnitSubType_SystemOutput;
|
|
|
|
device_name = nullptr;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-11-09 11:43:11 +01:00
|
|
|
component_subtype = kAudioUnitSubType_HALOutput;
|
2014-01-07 23:33:46 +01:00
|
|
|
/* XXX am I supposed to strdup() this? */
|
2016-11-09 11:43:11 +01:00
|
|
|
device_name = device;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
2016-07-09 13:03:41 +02:00
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
channel_map = block.GetBlockValue("channel_map");
|
|
|
|
hog_device = block.GetBlockValue("hog_device", false);
|
|
|
|
sync_sample_rate = block.GetBlockValue("sync_sample_rate", false);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioOutput *
|
|
|
|
OSXOutput::Create(EventLoop &, const ConfigBlock &block)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2016-11-09 11:43:11 +01:00
|
|
|
OSXOutput *oo = new OSXOutput(block);
|
2009-02-26 21:03:06 +01:00
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
AudioObjectPropertyAddress aopa = {
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioDeviceID dev_id = kAudioDeviceUnknown;
|
|
|
|
UInt32 dev_id_size = sizeof(dev_id);
|
|
|
|
AudioObjectGetPropertyData(kAudioObjectSystemObject,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&dev_id_size,
|
|
|
|
&dev_id);
|
|
|
|
oo->dev_id = dev_id;
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
return oo;
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 11:36:37 +02:00
|
|
|
|
2017-08-21 18:10:12 +02:00
|
|
|
int
|
|
|
|
OSXOutput::GetVolume()
|
|
|
|
{
|
|
|
|
AudioUnitParameterValue dvolume;
|
|
|
|
char errormsg[1024];
|
|
|
|
|
|
|
|
OSStatus status = AudioUnitGetParameter(au, kHALOutputParam_Volume,
|
|
|
|
kAudioUnitScope_Global, 0, &dvolume);
|
|
|
|
if (status != noErr) {
|
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
|
|
|
throw FormatRuntimeError("unable to get volume: %s", errormsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* see the explanation in SetVolume, below */
|
|
|
|
return static_cast<int>(dvolume * dvolume * 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OSXOutput::SetVolume(unsigned new_volume) {
|
|
|
|
char errormsg[1024];
|
|
|
|
|
|
|
|
/* The scaling below makes shifts in volume greater at the lower end
|
|
|
|
* of the scale. This mimics the "feel" of physical volume levers. This is
|
|
|
|
* generally what users of audio software expect.
|
|
|
|
*/
|
|
|
|
|
|
|
|
AudioUnitParameterValue scaled_volume =
|
|
|
|
sqrt(static_cast<AudioUnitParameterValue>(new_volume) / 100.0);
|
|
|
|
|
|
|
|
OSStatus status = AudioUnitSetParameter(au, kHALOutputParam_Volume,
|
|
|
|
kAudioUnitScope_Global, 0, scaled_volume, 0);
|
|
|
|
if (status != noErr) {
|
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
|
|
|
throw FormatRuntimeError( "unable to set new volume %u: %s",
|
|
|
|
new_volume, errormsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
static void
|
2016-07-09 13:03:41 +02:00
|
|
|
osx_output_parse_channel_map(
|
|
|
|
const char *device_name,
|
|
|
|
const char *channel_map_str,
|
|
|
|
SInt32 channel_map[],
|
2016-11-09 11:43:11 +01:00
|
|
|
UInt32 num_channels)
|
2016-07-09 13:03:41 +02:00
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
unsigned int inserted_channels = 0;
|
|
|
|
bool want_number = true;
|
|
|
|
|
|
|
|
while (*channel_map_str) {
|
2016-11-09 11:43:11 +01:00
|
|
|
if (inserted_channels >= num_channels)
|
|
|
|
throw FormatRuntimeError("%s: channel map contains more than %u entries or trailing garbage",
|
|
|
|
device_name, num_channels);
|
2016-07-09 13:03:41 +02:00
|
|
|
|
|
|
|
if (!want_number && *channel_map_str == ',') {
|
|
|
|
++channel_map_str;
|
|
|
|
want_number = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want_number &&
|
|
|
|
(isdigit(*channel_map_str) || *channel_map_str == '-')
|
|
|
|
) {
|
|
|
|
channel_map[inserted_channels] = strtol(channel_map_str, &endptr, 10);
|
2016-11-09 11:43:11 +01:00
|
|
|
if (channel_map[inserted_channels] < -1)
|
|
|
|
throw FormatRuntimeError("%s: channel map value %d not allowed (must be -1 or greater)",
|
2016-11-10 12:54:45 +01:00
|
|
|
device_name, channel_map[inserted_channels]);
|
2016-11-09 11:43:11 +01:00
|
|
|
|
2016-07-09 13:03:41 +02:00
|
|
|
channel_map_str = endptr;
|
|
|
|
want_number = false;
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"%s: channel_map[%u] = %d",
|
|
|
|
device_name, inserted_channels, channel_map[inserted_channels]);
|
|
|
|
++inserted_channels;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("%s: invalid character '%c' in channel map",
|
|
|
|
device_name, *channel_map_str);
|
2016-07-09 13:03:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
if (inserted_channels < num_channels)
|
|
|
|
throw FormatRuntimeError("%s: channel map contains less than %u entries",
|
|
|
|
device_name, num_channels);
|
2016-07-09 13:03:41 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
static void
|
|
|
|
osx_output_set_channel_map(OSXOutput *oo)
|
2016-07-09 13:03:41 +02:00
|
|
|
{
|
|
|
|
AudioStreamBasicDescription desc;
|
|
|
|
OSStatus status;
|
|
|
|
UInt32 size, num_channels;
|
|
|
|
char errormsg[1024];
|
|
|
|
|
|
|
|
size = sizeof(desc);
|
|
|
|
memset(&desc, 0, size);
|
|
|
|
status = AudioUnitGetProperty(oo->au,
|
|
|
|
kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Output,
|
|
|
|
0,
|
|
|
|
&desc,
|
|
|
|
&size);
|
|
|
|
if (status != noErr) {
|
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("%s: unable to get number of output device channels: %s",
|
|
|
|
oo->device_name, errormsg);
|
2016-07-09 13:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
num_channels = desc.mChannelsPerFrame;
|
2016-11-09 11:46:32 +01:00
|
|
|
std::unique_ptr<SInt32[]> channel_map(new SInt32[num_channels]);
|
2016-11-09 11:43:11 +01:00
|
|
|
osx_output_parse_channel_map(oo->device_name,
|
|
|
|
oo->channel_map,
|
|
|
|
channel_map.get(),
|
2016-11-10 12:54:45 +01:00
|
|
|
num_channels);
|
2016-07-09 13:03:41 +02:00
|
|
|
|
|
|
|
size = num_channels * sizeof(SInt32);
|
|
|
|
status = AudioUnitSetProperty(oo->au,
|
|
|
|
kAudioOutputUnitProperty_ChannelMap,
|
|
|
|
kAudioUnitScope_Input,
|
|
|
|
0,
|
2016-11-09 11:46:32 +01:00
|
|
|
channel_map.get(),
|
2016-07-09 13:03:41 +02:00
|
|
|
size);
|
|
|
|
if (status != noErr) {
|
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("%s: unable to set channel map: %s", oo->device_name, errormsg);
|
2016-07-09 13:03:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
static void
|
|
|
|
osx_output_sync_device_sample_rate(AudioDeviceID dev_id, AudioStreamBasicDescription desc)
|
|
|
|
{
|
|
|
|
FormatDebug(osx_output_domain, "Syncing sample rate.");
|
|
|
|
AudioObjectPropertyAddress aopa = {
|
|
|
|
kAudioDevicePropertyAvailableNominalSampleRates,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
UInt32 property_size;
|
|
|
|
OSStatus err = AudioObjectGetPropertyDataSize(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&property_size);
|
|
|
|
|
|
|
|
int count = property_size/sizeof(AudioValueRange);
|
|
|
|
AudioValueRange ranges[count];
|
|
|
|
property_size = sizeof(ranges);
|
|
|
|
err = AudioObjectGetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&property_size,
|
|
|
|
&ranges);
|
|
|
|
// Get the maximum sample rate as fallback.
|
|
|
|
Float64 sample_rate = .0;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (ranges[i].mMaximum > sample_rate)
|
|
|
|
sample_rate = ranges[i].mMaximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to see if the device support our format sample rate.
|
|
|
|
// For some high quality media samples, the frame rate may exceed
|
|
|
|
// device capability. In this case, we let CoreAudio downsample
|
|
|
|
// by decimation with an integer factor ranging from 1 to 4.
|
|
|
|
for (int f = 4; f > 0; f--) {
|
|
|
|
Float64 rate = desc.mSampleRate / f;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (ranges[i].mMinimum <= rate
|
|
|
|
&& rate <= ranges[i].mMaximum) {
|
|
|
|
sample_rate = rate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aopa.mSelector = kAudioDevicePropertyNominalSampleRate,
|
|
|
|
|
|
|
|
err = AudioObjectSetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
sizeof(&desc.mSampleRate),
|
|
|
|
&sample_rate);
|
|
|
|
if (err != noErr) {
|
|
|
|
FormatWarning(osx_output_domain,
|
|
|
|
"Failed to synchronize the sample rate: %d",
|
|
|
|
err);
|
|
|
|
} else {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Sample rate synced to %f Hz.",
|
|
|
|
sample_rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 04:40:04 +02:00
|
|
|
static OSStatus
|
|
|
|
osx_output_set_buffer_size(AudioUnit au, AudioStreamBasicDescription desc, UInt32 *frame_size)
|
|
|
|
{
|
|
|
|
AudioValueRange value_range = {0, 0};
|
|
|
|
UInt32 property_size = sizeof(AudioValueRange);
|
|
|
|
OSStatus err = AudioUnitGetProperty(au,
|
|
|
|
kAudioDevicePropertyBufferFrameSizeRange,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0,
|
|
|
|
&value_range,
|
|
|
|
&property_size);
|
|
|
|
if (err != noErr)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
UInt32 buffer_frame_size = value_range.mMaximum;
|
|
|
|
err = AudioUnitSetProperty(au,
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0,
|
|
|
|
&buffer_frame_size,
|
|
|
|
sizeof(buffer_frame_size));
|
|
|
|
if (err != noErr)
|
|
|
|
FormatWarning(osx_output_domain,
|
|
|
|
"Failed to set maximum buffer size: %d",
|
|
|
|
err);
|
|
|
|
|
|
|
|
property_size = sizeof(buffer_frame_size);
|
|
|
|
err = AudioUnitGetProperty(au,
|
|
|
|
kAudioDevicePropertyBufferFrameSize,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0,
|
|
|
|
&buffer_frame_size,
|
|
|
|
&property_size);
|
|
|
|
if (err != noErr) {
|
|
|
|
FormatWarning(osx_output_domain,
|
|
|
|
"Cannot get the buffer frame size: %d",
|
|
|
|
err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_frame_size *= desc.mBytesPerFrame;
|
|
|
|
|
|
|
|
// We set the frame size to a power of two integer that
|
|
|
|
// is larger than buffer_frame_size.
|
|
|
|
while (*frame_size < buffer_frame_size + 1) {
|
|
|
|
*frame_size <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
2016-09-18 21:52:08 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
osx_output_hog_device(AudioDeviceID dev_id, bool hog)
|
|
|
|
{
|
|
|
|
pid_t hog_pid;
|
|
|
|
AudioObjectPropertyAddress aopa = {
|
|
|
|
kAudioDevicePropertyHogMode,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
UInt32 size = sizeof(hog_pid);
|
|
|
|
OSStatus err = AudioObjectGetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&size,
|
|
|
|
&hog_pid);
|
|
|
|
if (err != noErr) {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Cannot get hog information: %d",
|
|
|
|
err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hog) {
|
|
|
|
if (hog_pid != -1) {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Device is already hogged.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hog_pid != getpid()) {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Device is not owned by this process.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hog_pid = hog ? getpid() : -1;
|
|
|
|
size = sizeof(hog_pid);
|
|
|
|
err = AudioObjectSetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
size,
|
|
|
|
&hog_pid);
|
|
|
|
if (err != noErr) {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Cannot hog the device: %d",
|
|
|
|
err);
|
|
|
|
} else {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
hog_pid == -1 ? "Device is unhogged"
|
|
|
|
: "Device is hogged");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
static void
|
|
|
|
osx_output_set_device(OSXOutput *oo)
|
2010-12-21 04:21:46 +01:00
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
UInt32 size, numdevices;
|
2016-07-02 23:35:44 +02:00
|
|
|
AudioObjectPropertyAddress propaddr;
|
2016-07-03 12:59:19 +02:00
|
|
|
CFStringRef cfname = nullptr;
|
2016-07-03 14:06:53 +02:00
|
|
|
char errormsg[1024];
|
2010-12-21 04:21:46 +01:00
|
|
|
char name[256];
|
|
|
|
unsigned int i;
|
|
|
|
|
2016-11-09 11:49:21 +01:00
|
|
|
AtScopeExit(&cfname) {
|
|
|
|
if (cfname)
|
|
|
|
CFRelease(cfname);
|
|
|
|
};
|
|
|
|
|
2010-12-21 04:21:46 +01:00
|
|
|
if (oo->component_subtype != kAudioUnitSubType_HALOutput)
|
2016-11-09 11:43:11 +01:00
|
|
|
return;
|
2010-12-21 04:21:46 +01:00
|
|
|
|
|
|
|
/* 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) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to determine number of OS X audio devices: %s",
|
|
|
|
errormsg);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* what are the available audio device IDs? */
|
|
|
|
numdevices = size / sizeof(AudioDeviceID);
|
2016-11-09 11:46:32 +01:00
|
|
|
std::unique_ptr<AudioDeviceID[]> deviceids(new AudioDeviceID[numdevices]);
|
|
|
|
status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propaddr, 0, nullptr, &size, deviceids.get());
|
2010-12-21 04:21:46 +01:00
|
|
|
if (status != noErr) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to determine OS X audio device IDs: %s",
|
|
|
|
errormsg);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to determine OS X device name "
|
|
|
|
"(device %u): %s",
|
|
|
|
(unsigned int) deviceids[i],
|
|
|
|
errormsg);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
2016-07-03 13:18:44 +02:00
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
if (!CFStringGetCString(cfname, name, sizeof(name), kCFStringEncodingUTF8))
|
|
|
|
throw std::runtime_error("Unable to convert device name from CFStringRef to char*");
|
2016-07-03 12:59:19 +02:00
|
|
|
|
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);
|
2016-11-09 11:43:11 +01:00
|
|
|
return;
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = AudioUnitSetProperty(oo->au,
|
|
|
|
kAudioOutputUnitProperty_CurrentDevice,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0,
|
|
|
|
&(deviceids[i]),
|
|
|
|
sizeof(AudioDeviceID));
|
|
|
|
if (status != noErr) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to set OS X audio output device: %s",
|
|
|
|
errormsg);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
2013-09-27 22:31:24 +02:00
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
oo->dev_id = deviceids[i];
|
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
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
if (oo->channel_map)
|
|
|
|
osx_output_set_channel_map(oo);
|
2010-12-21 04:21:46 +01:00
|
|
|
}
|
|
|
|
|
2016-07-07 23:53:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
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,
|
2016-07-07 23:53:05 +02:00
|
|
|
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;
|
2016-07-07 23:53:05 +02:00
|
|
|
|
2016-09-19 04:40:04 +02:00
|
|
|
int count = in_number_frames * od->asbd.mBytesPerFrame;
|
|
|
|
buffer_list->mBuffers[0].mDataByteSize =
|
|
|
|
od->ring_buffer->pop((uint8_t *)buffer_list->mBuffers[0].mData,
|
|
|
|
count);
|
|
|
|
return noErr;
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OSXOutput::Enable()
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2016-07-03 14:06:53 +02:00
|
|
|
char errormsg[1024];
|
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;
|
2017-08-09 16:58:44 +02:00
|
|
|
desc.componentSubType = 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);
|
2016-11-09 11:43:11 +01:00
|
|
|
if (comp == 0)
|
|
|
|
throw std::runtime_error("Error finding OS X component");
|
2005-03-14 05:30:32 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
OSStatus status = AudioComponentInstanceNew(comp, &au);
|
2009-02-26 22:01:42 +01:00
|
|
|
if (status != noErr) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to open OS X component: %s",
|
|
|
|
errormsg);
|
2005-03-14 05:30:32 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
try {
|
2017-08-09 16:58:44 +02:00
|
|
|
osx_output_set_device(this);
|
2016-11-09 11:43:11 +01:00
|
|
|
} catch (...) {
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioComponentInstanceDispose(au);
|
2016-11-09 11:43:11 +01:00
|
|
|
throw;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
if (hog_device)
|
|
|
|
osx_output_hog_device(dev_id, true);
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OSXOutput::Disable() noexcept
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioComponentInstanceDispose(au);
|
2016-09-18 21:52:08 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
if (hog_device)
|
|
|
|
osx_output_hog_device(dev_id, false);
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OSXOutput::Close() noexcept
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioOutputUnitStop(au);
|
|
|
|
AudioUnitUninitialize(au);
|
2012-04-05 00:45:39 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
delete ring_buffer;
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
void
|
|
|
|
OSXOutput::Open(AudioFormat &audio_format)
|
2011-12-24 15:55:35 +01:00
|
|
|
{
|
2016-07-03 14:06:53 +02:00
|
|
|
char errormsg[1024];
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
memset(&asbd, 0, sizeof(asbd));
|
|
|
|
asbd.mSampleRate = audio_format.sample_rate;
|
|
|
|
asbd.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
asbd.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:
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mBitsPerChannel = 8;
|
2009-11-10 17:11:34 +01:00
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S16:
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mBitsPerChannel = 16;
|
2009-11-10 17:11:34 +01:00
|
|
|
break;
|
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
case SampleFormat::S32:
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mBitsPerChannel = 32;
|
2011-12-24 18:18:42 +01:00
|
|
|
break;
|
|
|
|
|
2009-11-10 17:11:34 +01:00
|
|
|
default:
|
2013-08-03 21:00:50 +02:00
|
|
|
audio_format.format = SampleFormat::S32;
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.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())
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
2011-01-07 17:15:37 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mBytesPerPacket = audio_format.GetFrameSize();
|
|
|
|
asbd.mFramesPerPacket = 1;
|
|
|
|
asbd.mBytesPerFrame = asbd.mBytesPerPacket;
|
|
|
|
asbd.mChannelsPerFrame = audio_format.channels;
|
2011-01-07 17:15:37 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
if (sync_sample_rate)
|
|
|
|
osx_output_sync_device_sample_rate(dev_id, asbd);
|
2016-09-18 21:52:08 +02:00
|
|
|
|
2016-07-02 23:35:44 +02:00
|
|
|
OSStatus status =
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioUnitSetProperty(au, kAudioUnitProperty_StreamFormat,
|
2011-12-24 15:55:35 +01:00
|
|
|
kAudioUnitScope_Input, 0,
|
2017-08-09 16:58:44 +02:00
|
|
|
&asbd,
|
|
|
|
sizeof(asbd));
|
2016-11-09 11:43:11 +01:00
|
|
|
if (status != noErr)
|
|
|
|
throw std::runtime_error("Unable to set format on OS X device");
|
2005-03-16 05:46:41 +01:00
|
|
|
|
2016-09-19 04:40:04 +02:00
|
|
|
AURenderCallbackStruct callback;
|
|
|
|
callback.inputProc = osx_render;
|
2017-08-09 16:58:44 +02:00
|
|
|
callback.inputProcRefCon = this;
|
2016-09-19 04:40:04 +02:00
|
|
|
|
|
|
|
status =
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioUnitSetProperty(au,
|
2016-09-19 04:40:04 +02:00
|
|
|
kAudioUnitProperty_SetRenderCallback,
|
|
|
|
kAudioUnitScope_Input, 0,
|
|
|
|
&callback, sizeof(callback));
|
|
|
|
if (status != noErr) {
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioComponentInstanceDispose(au);
|
2016-11-09 11:43:11 +01:00
|
|
|
throw std::runtime_error("unable to set callback for OS X audio unit");
|
2016-09-19 04:40:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
status = AudioUnitInitialize(au);
|
2011-12-24 15:55:35 +01:00
|
|
|
if (status != noErr) {
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to initialize OS X audio unit: %s",
|
|
|
|
errormsg);
|
2011-12-24 15:55:35 +01:00
|
|
|
}
|
|
|
|
|
2016-09-19 06:09:57 +02:00
|
|
|
UInt32 buffer_frame_size = 1;
|
2017-08-09 16:58:44 +02:00
|
|
|
status = osx_output_set_buffer_size(au, asbd, &buffer_frame_size);
|
2016-09-19 04:40:04 +02:00
|
|
|
if (status != noErr) {
|
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("Unable to set frame size: %s",
|
|
|
|
errormsg);
|
2016-09-19 04:40:04 +02:00
|
|
|
}
|
2016-08-05 23:42:25 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(buffer_frame_size);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
status = AudioOutputUnitStart(au);
|
2009-02-26 22:01:42 +01:00
|
|
|
if (status != 0) {
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioUnitUninitialize(au);
|
2016-07-03 14:06:53 +02:00
|
|
|
osx_os_status_to_cstring(status, errormsg, sizeof(errormsg));
|
2016-11-09 11:43:11 +01:00
|
|
|
throw FormatRuntimeError("unable to start audio output: %s",
|
|
|
|
errormsg);
|
2009-02-26 21:40:22 +01:00
|
|
|
}
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
size_t
|
|
|
|
OSXOutput::Play(const void *chunk, size_t size)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2017-08-09 16:58:44 +02:00
|
|
|
return ring_buffer->push((uint8_t *)chunk, size);
|
2005-03-13 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
std::chrono::steady_clock::duration
|
|
|
|
OSXOutput::Delay() const noexcept
|
2016-09-19 17:51:03 +02:00
|
|
|
{
|
2017-08-09 16:58:44 +02:00
|
|
|
return ring_buffer->write_available()
|
2016-12-28 21:44:18 +01:00
|
|
|
? std::chrono::steady_clock::duration::zero()
|
|
|
|
: std::chrono::milliseconds(25);
|
2016-09-19 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 18:10:12 +02:00
|
|
|
int
|
|
|
|
osx_output_get_volume(OSXOutput &output)
|
|
|
|
{
|
|
|
|
return output.GetVolume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
osx_output_set_volume(OSXOutput &output, unsigned new_volume)
|
|
|
|
{
|
|
|
|
return output.SetVolume(new_volume);
|
|
|
|
}
|
|
|
|
|
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,
|
2017-08-09 16:58:44 +02:00
|
|
|
&OSXOutput::Create,
|
2017-12-03 08:29:23 +01:00
|
|
|
&osx_mixer_plugin,
|
2005-03-13 20:23:09 +01:00
|
|
|
};
|