2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 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"
|
2020-07-01 22:48:49 +02:00
|
|
|
#include "apple/AudioObject.hxx"
|
2020-05-28 15:16:16 +02:00
|
|
|
#include "apple/AudioUnit.hxx"
|
2020-05-28 14:50:07 +02:00
|
|
|
#include "apple/StringRef.hxx"
|
2020-05-28 14:54:30 +02:00
|
|
|
#include "apple/Throw.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:43:11 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Domain.hxx"
|
2018-02-26 13:11:45 +01:00
|
|
|
#include "util/Manual.hxx"
|
|
|
|
#include "util/ConstBuffer.hxx"
|
2019-06-16 12:11:44 +02:00
|
|
|
#include "pcm/Export.hxx"
|
2013-01-29 16:59:21 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
|
|
|
#include "thread/Cond.hxx"
|
2019-03-08 10:21:10 +01:00
|
|
|
#include "util/ByteOrder.hxx"
|
2020-07-01 22:39:23 +02:00
|
|
|
#include "util/CharUtil.hxx"
|
2019-09-26 14:44:48 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2018-07-13 21:48:43 +02:00
|
|
|
#include "util/StringBuffer.hxx"
|
|
|
|
#include "util/StringFormat.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>
|
2018-07-14 07:27:24 +02:00
|
|
|
#include <AudioToolbox/AudioToolbox.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>
|
|
|
|
|
OSXOutputPlugin: set the buffer time to be 100ms
[mk: the following text was copied from
https://github.com/MusicPlayerDaemon/MPD/pull/167]
For certain format (hi-res files) and normal buffer size hardware, The
hardware may at once consume most of the buffers. However, in Delay()
function, MPD is supposed to wait for 25 ms after the next try. it
will create a hiccup. The negative impact is much major than
increasing the latency.
I understand larger buffers come at a price. That's why in my earlier
commit last year I significantly reduced it. However, the buffer size
in CoreAudio is set according to the hardware, which is super small
latency. For instance, the system audio of 2015 generation of macbook
pro has maximum buffer size of 4096 samples, which is just 0.09s for
44.1k framerate, or 0.04s for 96k frames --- . compare to the 0.5 sec
latency alsa plugin has, even if we quadruple it, it's still super
tiny.
2017-12-04 01:27:42 +01:00
|
|
|
static constexpr unsigned MPD_OSX_BUFFER_TIME_MS = 100;
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
static StringBuffer<64>
|
2019-07-03 22:12:47 +02:00
|
|
|
StreamDescriptionToString(const AudioStreamBasicDescription desc)
|
|
|
|
{
|
2018-07-13 21:48:43 +02:00
|
|
|
// Only convert the lpcm formats (nothing else supported / used by MPD)
|
|
|
|
assert(desc.mFormatID == kAudioFormatLinearPCM);
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
return StringFormat<64>("%u channel %s %sinterleaved %u-bit %s %s (%uHz)",
|
2019-07-03 22:12:47 +02:00
|
|
|
desc.mChannelsPerFrame,
|
|
|
|
(desc.mFormatFlags & kAudioFormatFlagIsNonMixable) ? "" : "mixable",
|
|
|
|
(desc.mFormatFlags & kAudioFormatFlagIsNonInterleaved) ? "non-" : "",
|
|
|
|
desc.mBitsPerChannel,
|
|
|
|
(desc.mFormatFlags & kAudioFormatFlagIsFloat) ? "Float" : "SInt",
|
|
|
|
(desc.mFormatFlags & kAudioFormatFlagIsBigEndian) ? "BE" : "LE",
|
|
|
|
(UInt32)desc.mSampleRate);
|
2018-07-13 21:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2020-05-28 13:30:52 +02:00
|
|
|
const char *const channel_map;
|
|
|
|
const bool hog_device;
|
2020-07-02 15:07:39 +02:00
|
|
|
|
2018-03-07 11:41:15 +01:00
|
|
|
bool pause;
|
2020-07-02 15:07:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the audio unit "started", i.e. was AudioOutputUnitStart() called?
|
|
|
|
*/
|
|
|
|
bool started;
|
|
|
|
|
2018-02-26 13:11:45 +01:00
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
/**
|
|
|
|
* Enable DSD over PCM according to the DoP standard?
|
|
|
|
*
|
|
|
|
* @see http://dsd-guide.com/dop-open-standard
|
|
|
|
*/
|
2020-05-28 13:30:52 +02:00
|
|
|
const bool dop_setting;
|
2018-07-14 01:36:29 +02:00
|
|
|
bool dop_enabled;
|
2018-07-13 21:48:43 +02:00
|
|
|
Manual<PcmExport> pcm_export;
|
2018-02-26 13:11:45 +01:00
|
|
|
#endif
|
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;
|
2018-03-07 11:41:15 +01:00
|
|
|
bool Pause() override;
|
2018-07-16 07:33:39 +02:00
|
|
|
void Cancel() noexcept 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
|
|
|
|
2009-02-26 21:03:06 +01:00
|
|
|
static bool
|
2019-07-03 22:12:47 +02:00
|
|
|
osx_output_test_default_device()
|
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)
|
2020-05-28 13:30:52 +02:00
|
|
|
:AudioOutput(FLAG_ENABLE_DISABLE|FLAG_PAUSE),
|
|
|
|
channel_map(block.GetBlockValue("channel_map")),
|
|
|
|
hog_device(block.GetBlockValue("hog_device", false))
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
, dop_setting(block.GetBlockValue("dop", false))
|
|
|
|
#endif
|
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
|
|
|
|
2019-09-26 14:44:48 +02:00
|
|
|
if (device == nullptr || StringIsEqual(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
|
|
|
}
|
2019-09-26 14:44:48 +02:00
|
|
|
else if (StringIsEqual(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2020-05-28 13:31:30 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress default_system_output_device{
|
|
|
|
kAudioHardwarePropertyDefaultSystemOutputDevice,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr AudioObjectPropertyAddress default_output_device{
|
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &aopa =
|
|
|
|
oo->component_subtype == kAudioUnitSubType_SystemOutput
|
2018-03-06 23:08:46 +01:00
|
|
|
// get system output dev_id if configured
|
2020-05-28 13:31:30 +02:00
|
|
|
? default_system_output_device
|
2019-07-03 22:12:47 +02:00
|
|
|
/* fallback to default device initially (can still be
|
|
|
|
changed by osx_output_set_device) */
|
2020-05-28 13:31:30 +02:00
|
|
|
: default_output_device;
|
2018-03-06 23:08:46 +01:00
|
|
|
|
2020-05-28 13:39:58 +02:00
|
|
|
AudioDeviceID dev_id = kAudioDeviceUnknown;
|
|
|
|
UInt32 dev_id_size = sizeof(dev_id);
|
2016-09-18 21:52:08 +02:00
|
|
|
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()
|
|
|
|
{
|
2020-05-27 19:48:46 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa = {
|
2020-05-27 19:47:47 +02:00
|
|
|
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
2018-07-11 00:51:32 +02:00
|
|
|
};
|
2017-08-21 18:10:12 +02:00
|
|
|
|
2020-05-28 15:16:16 +02:00
|
|
|
const auto vol = AudioObjectGetPropertyDataT<Float32>(dev_id,
|
|
|
|
aopa);
|
2017-08-21 18:10:12 +02:00
|
|
|
|
2020-10-14 15:53:15 +02:00
|
|
|
return static_cast<int>(vol * 100.0f);
|
2017-08-21 18:10:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-03 22:12:47 +02:00
|
|
|
OSXOutput::SetVolume(unsigned new_volume)
|
|
|
|
{
|
2018-07-11 00:51:32 +02:00
|
|
|
Float32 vol = new_volume / 100.0;
|
2020-05-27 19:48:46 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa = {
|
2020-05-27 19:47:47 +02:00
|
|
|
kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
2018-07-11 00:51:32 +02:00
|
|
|
};
|
|
|
|
UInt32 size = sizeof(vol);
|
|
|
|
OSStatus status = AudioObjectSetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
size,
|
|
|
|
&vol);
|
2017-08-21 18:10:12 +02:00
|
|
|
|
2020-05-28 14:54:30 +02:00
|
|
|
if (status != noErr)
|
|
|
|
Apple::ThrowOSStatus(status);
|
2017-08-21 18:10:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 11:43:11 +01:00
|
|
|
static void
|
2019-07-03 22:12:47 +02:00
|
|
|
osx_output_parse_channel_map(const char *device_name,
|
|
|
|
const char *channel_map_str,
|
|
|
|
SInt32 channel_map[],
|
|
|
|
UInt32 num_channels)
|
2016-07-09 13:03:41 +02:00
|
|
|
{
|
|
|
|
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 &&
|
2020-07-01 22:39:23 +02:00
|
|
|
(IsDigitASCII(*channel_map_str) || *channel_map_str == '-')
|
2016-07-09 13:03:41 +02:00
|
|
|
) {
|
2020-05-28 13:39:58 +02:00
|
|
|
char *endptr;
|
2016-07-09 13:03:41 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
static UInt32
|
|
|
|
AudioUnitGetChannelsPerFrame(AudioUnit inUnit)
|
|
|
|
{
|
|
|
|
const auto desc = AudioUnitGetPropertyT<AudioStreamBasicDescription>(inUnit,
|
|
|
|
kAudioUnitProperty_StreamFormat,
|
|
|
|
kAudioUnitScope_Output,
|
|
|
|
0);
|
|
|
|
return desc.mChannelsPerFrame;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
OSStatus status;
|
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
const UInt32 num_channels = AudioUnitGetChannelsPerFrame(oo->au);
|
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
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
UInt32 size = num_channels * sizeof(SInt32);
|
2016-07-09 13:03:41 +02:00
|
|
|
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);
|
2020-05-28 14:54:30 +02:00
|
|
|
if (status != noErr)
|
|
|
|
Apple::ThrowOSStatus(status, "unable to set channel map");
|
2016-07-09 13:03:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
|
|
|
|
static float
|
2019-07-03 22:12:47 +02:00
|
|
|
osx_output_score_sample_rate(Float64 destination_rate, unsigned source_rate)
|
|
|
|
{
|
2018-07-13 21:48:43 +02:00
|
|
|
float score = 0;
|
|
|
|
double int_portion;
|
|
|
|
double frac_portion = modf(source_rate / destination_rate, &int_portion);
|
|
|
|
// prefer sample rates that are multiples of the source sample rate
|
2020-07-01 17:20:19 +02:00
|
|
|
if (frac_portion < 0.01 || frac_portion >= 0.99)
|
|
|
|
score += 1000;
|
2018-07-13 21:48:43 +02:00
|
|
|
// prefer exact matches over other multiples
|
|
|
|
score += (int_portion == 1.0) ? 500 : 0;
|
2018-08-07 10:55:06 +02:00
|
|
|
if (source_rate == destination_rate)
|
|
|
|
score += 1000;
|
2019-07-03 22:12:47 +02:00
|
|
|
else if (source_rate > destination_rate)
|
2018-07-13 21:48:43 +02:00
|
|
|
score += (int_portion > 1 && int_portion < 100) ? (100 - int_portion) / 100 * 100 : 0;
|
|
|
|
else
|
|
|
|
score += (int_portion > 1 && int_portion < 100) ? (100 + int_portion) / 100 * 100 : 0;
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float
|
2019-07-03 22:12:47 +02:00
|
|
|
osx_output_score_format(const AudioStreamBasicDescription &format_desc,
|
|
|
|
const AudioStreamBasicDescription &target_format)
|
|
|
|
{
|
2018-07-13 21:48:43 +02:00
|
|
|
float score = 0;
|
|
|
|
// Score only linear PCM formats (everything else MPD cannot use)
|
|
|
|
if (format_desc.mFormatID == kAudioFormatLinearPCM) {
|
2019-07-03 22:12:47 +02:00
|
|
|
score += osx_output_score_sample_rate(format_desc.mSampleRate,
|
|
|
|
target_format.mSampleRate);
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
// Just choose the stream / format with the highest number of output channels
|
|
|
|
score += format_desc.mChannelsPerFrame * 5;
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-08-07 10:55:06 +02:00
|
|
|
if (target_format.mFormatFlags == kLinearPCMFormatFlagIsFloat) {
|
2018-07-13 21:48:43 +02:00
|
|
|
// for float, prefer the highest bitdepth we have
|
|
|
|
if (format_desc.mBitsPerChannel >= 16)
|
|
|
|
score += (format_desc.mBitsPerChannel / 8);
|
|
|
|
} else {
|
2018-08-07 10:55:06 +02:00
|
|
|
if (format_desc.mBitsPerChannel == target_format.mBitsPerChannel)
|
2018-07-13 21:48:43 +02:00
|
|
|
score += 5;
|
2018-08-07 10:55:06 +02:00
|
|
|
else if (format_desc.mBitsPerChannel > target_format.mBitsPerChannel)
|
2018-07-13 21:48:43 +02:00
|
|
|
score += 1;
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2018-02-26 13:11:45 +01:00
|
|
|
static Float64
|
2019-07-03 22:12:47 +02:00
|
|
|
osx_output_set_device_format(AudioDeviceID dev_id,
|
|
|
|
const AudioStreamBasicDescription &target_format)
|
2016-09-18 21:52:08 +02:00
|
|
|
{
|
2020-05-28 13:31:30 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa_device_streams = {
|
2018-07-13 21:48:43 +02:00
|
|
|
kAudioDevicePropertyStreams,
|
2016-09-18 21:52:08 +02:00
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
2020-05-28 13:31:30 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa_stream_direction = {
|
|
|
|
kAudioStreamPropertyDirection,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr AudioObjectPropertyAddress aopa_stream_phys_formats = {
|
|
|
|
kAudioStreamPropertyAvailablePhysicalFormats,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr AudioObjectPropertyAddress aopa_stream_phys_format = {
|
|
|
|
kAudioStreamPropertyPhysicalFormat,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
2020-05-28 15:16:16 +02:00
|
|
|
OSStatus err;
|
2019-06-08 11:51:15 +02:00
|
|
|
|
2020-05-28 15:16:16 +02:00
|
|
|
const auto streams =
|
|
|
|
AudioObjectGetPropertyDataArray<AudioStreamID>(dev_id,
|
|
|
|
aopa_device_streams);
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
bool format_found = false;
|
|
|
|
int output_stream;
|
|
|
|
AudioStreamBasicDescription output_format;
|
|
|
|
|
2020-05-28 15:16:16 +02:00
|
|
|
for (const auto stream : streams) {
|
|
|
|
const auto direction =
|
|
|
|
AudioObjectGetPropertyDataT<UInt32>(stream,
|
|
|
|
aopa_stream_direction);
|
2019-07-03 22:12:47 +02:00
|
|
|
if (direction != 0)
|
2018-07-13 21:48:43 +02:00
|
|
|
continue;
|
|
|
|
|
2020-05-28 15:16:16 +02:00
|
|
|
const auto format_list =
|
|
|
|
AudioObjectGetPropertyDataArray<AudioStreamRangedDescription>(stream,
|
|
|
|
aopa_stream_phys_formats);
|
2018-07-13 21:48:43 +02:00
|
|
|
|
|
|
|
float output_score = 0;
|
2020-05-28 15:57:37 +02:00
|
|
|
|
|
|
|
for (const auto &format : format_list) {
|
|
|
|
AudioStreamBasicDescription format_desc = format.mFormat;
|
2018-07-13 21:48:43 +02:00
|
|
|
std::string format_string;
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
// for devices with kAudioStreamAnyRate
|
|
|
|
// we use the requested samplerate here
|
|
|
|
if (format_desc.mSampleRate == kAudioStreamAnyRate)
|
2018-08-07 10:55:06 +02:00
|
|
|
format_desc.mSampleRate = target_format.mSampleRate;
|
|
|
|
float score = osx_output_score_format(format_desc, target_format);
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
// print all (linear pcm) formats and their rating
|
2020-10-14 15:53:15 +02:00
|
|
|
if (score > 0.0f)
|
2019-07-03 22:12:47 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Format: %s rated %f",
|
2020-10-14 15:53:15 +02:00
|
|
|
StreamDescriptionToString(format_desc).c_str(),
|
|
|
|
(double)score);
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
if (score > output_score) {
|
|
|
|
output_score = score;
|
|
|
|
output_format = format_desc;
|
|
|
|
output_stream = stream; // set the idx of the stream in the device
|
|
|
|
format_found = true;
|
2016-09-18 21:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
if (format_found) {
|
|
|
|
err = AudioObjectSetPropertyData(output_stream,
|
2020-05-28 13:31:30 +02:00
|
|
|
&aopa_stream_phys_format,
|
2019-07-03 22:12:47 +02:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
sizeof(output_format),
|
|
|
|
&output_format);
|
|
|
|
if (err != noErr)
|
2019-07-03 22:21:43 +02:00
|
|
|
throw FormatRuntimeError("Failed to change the stream format: %d",
|
2019-07-03 22:12:47 +02:00
|
|
|
err);
|
|
|
|
}
|
2018-07-13 21:48:43 +02:00
|
|
|
|
|
|
|
return output_format.mSampleRate;
|
2016-09-18 21:52:08 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
static UInt32
|
|
|
|
osx_output_set_buffer_size(AudioUnit au, AudioStreamBasicDescription desc)
|
2016-09-19 04:40:04 +02:00
|
|
|
{
|
2020-07-01 22:49:53 +02:00
|
|
|
const auto value_range = AudioUnitGetPropertyT<AudioValueRange>(au,
|
|
|
|
kAudioDevicePropertyBufferFrameSizeRange,
|
|
|
|
kAudioUnitScope_Global,
|
|
|
|
0);
|
2016-09-19 04:40:04 +02:00
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
try {
|
|
|
|
AudioUnitSetBufferFrameSize(au, value_range.mMaximum);
|
|
|
|
} catch (...) {
|
|
|
|
LogError(std::current_exception(),
|
|
|
|
"Failed to set maximum buffer size");
|
|
|
|
}
|
2016-09-19 04:40:04 +02:00
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
auto buffer_frame_size = AudioUnitGetBufferFrameSize(au);
|
2016-09-19 04:40:04 +02:00
|
|
|
buffer_frame_size *= desc.mBytesPerFrame;
|
|
|
|
|
|
|
|
// We set the frame size to a power of two integer that
|
|
|
|
// is larger than buffer_frame_size.
|
2020-07-01 22:49:53 +02:00
|
|
|
UInt32 frame_size = 1;
|
|
|
|
while (frame_size < buffer_frame_size + 1)
|
|
|
|
frame_size <<= 1;
|
2016-09-19 04:40:04 +02:00
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
return frame_size;
|
2016-09-19 04:40:04 +02:00
|
|
|
}
|
2016-09-18 21:52:08 +02:00
|
|
|
|
|
|
|
static void
|
2020-08-14 14:36:30 +02:00
|
|
|
osx_output_hog_device(AudioDeviceID dev_id, bool hog) noexcept
|
2016-09-18 21:52:08 +02:00
|
|
|
{
|
2020-05-27 19:48:46 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa = {
|
2016-09-18 21:52:08 +02:00
|
|
|
kAudioDevicePropertyHogMode,
|
|
|
|
kAudioObjectPropertyScopeOutput,
|
|
|
|
kAudioObjectPropertyElementMaster
|
|
|
|
};
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2020-08-14 14:36:30 +02:00
|
|
|
pid_t hog_pid;
|
|
|
|
|
|
|
|
try {
|
|
|
|
hog_pid = AudioObjectGetPropertyDataT<pid_t>(dev_id, aopa);
|
|
|
|
} catch (...) {
|
|
|
|
Log(LogLevel::DEBUG, std::current_exception(),
|
|
|
|
"Failed to query HogMode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
if (hog) {
|
|
|
|
if (hog_pid != -1) {
|
2019-07-03 22:12:47 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
2016-09-18 21:52:08 +02:00
|
|
|
"Device is already hogged.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hog_pid != getpid()) {
|
2019-07-03 22:12:47 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
2016-09-18 21:52:08 +02:00
|
|
|
"Device is not owned by this process.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2016-09-18 21:52:08 +02:00
|
|
|
hog_pid = hog ? getpid() : -1;
|
2020-05-28 15:16:16 +02:00
|
|
|
UInt32 size = sizeof(hog_pid);
|
|
|
|
OSStatus err;
|
2016-09-18 21:52:08 +02:00
|
|
|
err = AudioObjectSetPropertyData(dev_id,
|
|
|
|
&aopa,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
size,
|
|
|
|
&hog_pid);
|
|
|
|
if (err != noErr) {
|
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"Cannot hog the device: %d",
|
|
|
|
err);
|
|
|
|
} else {
|
2019-07-03 22:12:47 +02:00
|
|
|
LogDebug(osx_output_domain,
|
|
|
|
hog_pid == -1
|
|
|
|
? "Device is unhogged"
|
|
|
|
: "Device is hogged");
|
2016-09-18 21:52:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
gcc_pure
|
|
|
|
static bool
|
|
|
|
IsAudioDeviceName(AudioDeviceID id, const char *expected_name) noexcept
|
2010-12-21 04:21:46 +01:00
|
|
|
{
|
2020-05-28 15:46:48 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa_name{
|
|
|
|
kAudioObjectPropertyName,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
2016-11-09 11:49:21 +01:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
char actual_name[256];
|
2020-07-02 13:37:44 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
auto cfname = AudioObjectGetStringProperty(id, aopa_name);
|
|
|
|
if (!cfname.GetCString(actual_name, sizeof(actual_name)))
|
|
|
|
return false;
|
|
|
|
} catch (...) {
|
2020-05-28 15:46:48 +02:00
|
|
|
return false;
|
2020-07-02 13:37:44 +02:00
|
|
|
}
|
2010-12-21 04:21:46 +01:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
return StringIsEqual(actual_name, expected_name);
|
|
|
|
}
|
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
static AudioDeviceID
|
2020-05-28 15:46:48 +02:00
|
|
|
FindAudioDeviceByName(const char *name)
|
|
|
|
{
|
2020-05-28 15:16:16 +02:00
|
|
|
/* what are the available audio device IDs? */
|
2020-05-28 13:31:30 +02:00
|
|
|
static constexpr AudioObjectPropertyAddress aopa_hw_devices{
|
|
|
|
kAudioHardwarePropertyDevices,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
const auto ids =
|
2020-05-28 15:16:16 +02:00
|
|
|
AudioObjectGetPropertyDataArray<AudioDeviceID>(kAudioObjectSystemObject,
|
|
|
|
aopa_hw_devices);
|
2010-12-21 04:21:46 +01:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
for (const auto id : ids) {
|
|
|
|
if (IsAudioDeviceName(id, name))
|
|
|
|
return id;
|
|
|
|
}
|
2020-05-28 14:06:44 +02:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
throw FormatRuntimeError("Found no audio device with name '%s' ",
|
|
|
|
name);
|
2020-05-28 15:46:48 +02:00
|
|
|
}
|
2016-07-03 12:59:19 +02:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
static void
|
|
|
|
osx_output_set_device(OSXOutput *oo)
|
|
|
|
{
|
|
|
|
if (oo->component_subtype != kAudioUnitSubType_HALOutput)
|
|
|
|
return;
|
2019-07-03 22:12:47 +02:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
const auto id = FindAudioDeviceByName(oo->device_name);
|
2010-12-21 04:21:46 +01:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"found matching device: ID=%u, name=%s",
|
2020-05-28 15:46:48 +02:00
|
|
|
(unsigned)id, oo->device_name);
|
2020-05-28 15:46:48 +02:00
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
AudioUnitSetCurrentDevice(oo->au, id);
|
2013-09-27 22:31:24 +02:00
|
|
|
|
2020-05-28 15:46:48 +02:00
|
|
|
oo->dev_id = id;
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(osx_output_domain,
|
|
|
|
"set OS X audio output device ID=%u, name=%s",
|
2020-05-28 15:46:48 +02:00
|
|
|
(unsigned)id, oo->device_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
|
|
|
|
2019-07-03 22:12:47 +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,
|
2020-03-12 20:56:11 +01:00
|
|
|
[[maybe_unused]] AudioUnitRenderActionFlags *io_action_flags,
|
|
|
|
[[maybe_unused]] const AudioTimeStamp *in_timestamp,
|
|
|
|
[[maybe_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);
|
2019-07-03 22:12:47 +02:00
|
|
|
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-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);
|
2020-05-28 14:54:30 +02:00
|
|
|
if (status != noErr)
|
|
|
|
Apple::ThrowOSStatus(status, "Unable to open OS X component");
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2018-02-26 13:11:45 +01:00
|
|
|
pcm_export.Construct();
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
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);
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2018-02-26 13:11:45 +01:00
|
|
|
pcm_export.Destruct();
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
2016-11-09 11:43:11 +01:00
|
|
|
throw;
|
2005-03-16 05:46:41 +01:00
|
|
|
}
|
2019-07-03 22:12:47 +02: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);
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2018-02-26 13:11:45 +01:00
|
|
|
pcm_export.Destruct();
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
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
|
|
|
{
|
2020-07-02 15:13:05 +02:00
|
|
|
if (started)
|
|
|
|
AudioOutputUnitStop(au);
|
2017-08-09 16:58:44 +02:00
|
|
|
AudioUnitUninitialize(au);
|
|
|
|
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
|
|
|
{
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2018-02-26 13:11:45 +01:00
|
|
|
PcmExport::Params params;
|
|
|
|
params.alsa_channel_order = true;
|
2018-03-07 22:40:19 +01:00
|
|
|
bool dop = dop_setting;
|
|
|
|
#endif
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
memset(&asbd, 0, sizeof(asbd));
|
|
|
|
asbd.mFormatID = kAudioFormatLinearPCM;
|
2018-07-14 00:09:11 +02:00
|
|
|
if (audio_format.format == SampleFormat::FLOAT) {
|
|
|
|
asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
|
|
|
|
} else {
|
|
|
|
asbd.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2009-11-10 17:11:34 +01:00
|
|
|
}
|
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
|
|
|
|
2018-07-14 00:09:11 +02:00
|
|
|
if (audio_format.format == SampleFormat::S24_P32) {
|
|
|
|
asbd.mBitsPerChannel = 24;
|
|
|
|
} else {
|
|
|
|
asbd.mBitsPerChannel = audio_format.GetSampleSize() * 8;
|
|
|
|
}
|
2018-07-13 21:48:43 +02:00
|
|
|
asbd.mBytesPerPacket = audio_format.GetFrameSize();
|
2018-07-28 09:06:56 +02:00
|
|
|
asbd.mSampleRate = audio_format.sample_rate;
|
2018-07-14 00:09:11 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2018-07-14 00:09:11 +02:00
|
|
|
if (dop && audio_format.format == SampleFormat::DSD) {
|
|
|
|
asbd.mBitsPerChannel = 24;
|
2019-06-17 22:20:28 +02:00
|
|
|
params.dsd_mode = PcmExport::DsdMode::DOP;
|
2018-07-14 01:36:29 +02:00
|
|
|
asbd.mSampleRate = params.CalcOutputSampleRate(audio_format.sample_rate);
|
2018-02-26 13:11:45 +01:00
|
|
|
asbd.mBytesPerPacket = 4 * audio_format.channels;
|
2018-08-07 10:55:06 +02:00
|
|
|
|
2018-07-14 01:36:29 +02:00
|
|
|
}
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
2018-07-14 00:09:11 +02:00
|
|
|
|
2017-08-09 16:58:44 +02:00
|
|
|
asbd.mFramesPerPacket = 1;
|
|
|
|
asbd.mBytesPerFrame = asbd.mBytesPerPacket;
|
|
|
|
asbd.mChannelsPerFrame = audio_format.channels;
|
2011-01-07 17:15:37 +01:00
|
|
|
|
2018-08-07 10:55:06 +02:00
|
|
|
Float64 sample_rate = osx_output_set_device_format(dev_id, asbd);
|
2018-02-26 13:11:45 +01:00
|
|
|
|
|
|
|
#ifdef ENABLE_DSD
|
2019-07-03 22:12:47 +02:00
|
|
|
if (audio_format.format == SampleFormat::DSD &&
|
|
|
|
sample_rate != asbd.mSampleRate) {
|
|
|
|
// fall back to PCM in case sample_rate cannot be synchronized
|
2019-06-17 22:20:28 +02:00
|
|
|
params.dsd_mode = PcmExport::DsdMode::NONE;
|
2018-02-26 13:11:45 +01:00
|
|
|
audio_format.format = SampleFormat::S32;
|
|
|
|
asbd.mBitsPerChannel = 32;
|
|
|
|
asbd.mBytesPerPacket = audio_format.GetFrameSize();
|
|
|
|
asbd.mSampleRate = params.CalcOutputSampleRate(audio_format.sample_rate);
|
|
|
|
asbd.mBytesPerFrame = asbd.mBytesPerPacket;
|
|
|
|
}
|
2019-06-17 22:20:28 +02:00
|
|
|
dop_enabled = params.dsd_mode == PcmExport::DsdMode::DOP;
|
2018-02-26 13:11:45 +01:00
|
|
|
#endif
|
2016-09-18 21:52:08 +02:00
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
AudioUnitSetInputStreamFormat(au, asbd);
|
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
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
AudioUnitSetInputRenderCallback(au, callback);
|
2016-09-19 04:40:04 +02:00
|
|
|
|
2020-07-02 14:45:14 +02:00
|
|
|
OSStatus status = AudioUnitInitialize(au);
|
2020-05-28 14:54:30 +02:00
|
|
|
if (status != noErr)
|
|
|
|
Apple::ThrowOSStatus(status, "Unable to initialize OS X audio unit");
|
2011-12-24 15:55:35 +01:00
|
|
|
|
2020-07-01 22:49:53 +02:00
|
|
|
UInt32 buffer_frame_size = osx_output_set_buffer_size(au, asbd);
|
2016-08-05 23:42:25 +02:00
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
size_t ring_buffer_size = std::max<size_t>(buffer_frame_size,
|
2018-07-14 00:53:51 +02:00
|
|
|
MPD_OSX_BUFFER_TIME_MS * audio_format.GetFrameSize() * audio_format.sample_rate / 1000);
|
2018-07-14 01:36:29 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_DSD
|
2019-07-03 22:12:47 +02:00
|
|
|
if (dop_enabled) {
|
2018-07-14 01:36:29 +02:00
|
|
|
pcm_export->Open(audio_format.format, audio_format.channels, params);
|
|
|
|
ring_buffer_size = std::max<size_t>(buffer_frame_size,
|
2019-06-26 15:45:44 +02:00
|
|
|
MPD_OSX_BUFFER_TIME_MS * pcm_export->GetOutputFrameSize() * asbd.mSampleRate / 1000);
|
2018-07-14 01:36:29 +02:00
|
|
|
}
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
2017-12-16 20:50:53 +01:00
|
|
|
ring_buffer = new boost::lockfree::spsc_queue<uint8_t>(ring_buffer_size);
|
2005-03-13 20:23:09 +01:00
|
|
|
|
2018-03-07 11:41:15 +01:00
|
|
|
pause = false;
|
2020-07-02 15:21:19 +02:00
|
|
|
started = false;
|
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
|
|
|
{
|
2018-02-26 13:11:45 +01:00
|
|
|
assert(size > 0);
|
2020-07-02 15:07:39 +02:00
|
|
|
|
|
|
|
pause = false;
|
|
|
|
|
2020-07-02 15:25:45 +02:00
|
|
|
ConstBuffer<uint8_t> input((const uint8_t *)chunk, size);
|
|
|
|
|
2018-07-13 21:48:43 +02:00
|
|
|
#ifdef ENABLE_DSD
|
2019-07-03 22:12:47 +02:00
|
|
|
if (dop_enabled) {
|
2020-07-02 15:25:45 +02:00
|
|
|
input = ConstBuffer<uint8_t>::FromVoid(pcm_export->Export(input.ToVoid()));
|
|
|
|
if (input.empty())
|
2018-07-14 01:36:29 +02:00
|
|
|
return size;
|
|
|
|
}
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
2020-07-02 15:25:45 +02:00
|
|
|
|
|
|
|
size_t bytes_written = ring_buffer->push(input.data, input.size);
|
2018-02-26 13:11:45 +01:00
|
|
|
|
2020-07-02 15:22:16 +02:00
|
|
|
if (!started) {
|
|
|
|
OSStatus status = AudioOutputUnitStart(au);
|
|
|
|
if (status != noErr)
|
|
|
|
throw std::runtime_error("Unable to restart audio output after pause");
|
|
|
|
|
|
|
|
started = true;
|
2018-07-14 01:36:29 +02:00
|
|
|
}
|
2020-07-02 15:22:16 +02:00
|
|
|
|
2020-07-02 15:25:45 +02:00
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
if (dop_enabled)
|
2020-07-02 15:33:13 +02:00
|
|
|
bytes_written = pcm_export->CalcInputSize(bytes_written);
|
2018-07-13 21:48:43 +02:00
|
|
|
#endif
|
2020-07-02 15:25:45 +02:00
|
|
|
|
|
|
|
return bytes_written;
|
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
|
|
|
{
|
2018-07-14 01:14:59 +02:00
|
|
|
return ring_buffer->write_available() && !pause
|
2016-12-28 21:44:18 +01:00
|
|
|
? std::chrono::steady_clock::duration::zero()
|
OSXOutputPlugin: set the buffer time to be 100ms
[mk: the following text was copied from
https://github.com/MusicPlayerDaemon/MPD/pull/167]
For certain format (hi-res files) and normal buffer size hardware, The
hardware may at once consume most of the buffers. However, in Delay()
function, MPD is supposed to wait for 25 ms after the next try. it
will create a hiccup. The negative impact is much major than
increasing the latency.
I understand larger buffers come at a price. That's why in my earlier
commit last year I significantly reduced it. However, the buffer size
in CoreAudio is set according to the hardware, which is super small
latency. For instance, the system audio of 2015 generation of macbook
pro has maximum buffer size of 4096 samples, which is just 0.09s for
44.1k framerate, or 0.04s for 96k frames --- . compare to the 0.5 sec
latency alsa plugin has, even if we quadruple it, it's still super
tiny.
2017-12-04 01:27:42 +01:00
|
|
|
: std::chrono::milliseconds(MPD_OSX_BUFFER_TIME_MS / 4);
|
2016-09-19 17:51:03 +02:00
|
|
|
}
|
2019-07-03 22:12:47 +02:00
|
|
|
|
|
|
|
bool OSXOutput::Pause()
|
|
|
|
{
|
2020-07-02 15:07:39 +02:00
|
|
|
pause = true;
|
|
|
|
|
|
|
|
if (started) {
|
2018-03-07 11:41:15 +01:00
|
|
|
AudioOutputUnitStop(au);
|
2020-07-02 15:07:39 +02:00
|
|
|
started = false;
|
2018-03-07 11:41:15 +01:00
|
|
|
}
|
2020-07-02 15:07:39 +02:00
|
|
|
|
2018-03-07 11:41:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-16 07:33:39 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
OSXOutput::Cancel() noexcept
|
|
|
|
{
|
2020-07-02 15:13:05 +02:00
|
|
|
if (started) {
|
|
|
|
AudioOutputUnitStop(au);
|
|
|
|
started = false;
|
|
|
|
}
|
|
|
|
|
2018-07-16 07:33:39 +02:00
|
|
|
ring_buffer->reset();
|
|
|
|
#ifdef ENABLE_DSD
|
|
|
|
pcm_export->Reset();
|
|
|
|
#endif
|
2020-07-02 15:19:01 +02:00
|
|
|
|
|
|
|
/* the AudioUnit will be restarted by the next Play() call */
|
2018-07-16 07:33:39 +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
|
|
|
};
|