Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions src/output/plugins/OSXOutputPlugin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ struct OSXOutput final : AudioOutput {
void SetVolume(unsigned new_volume);

private:
/**
* Determine the device to operate on. Only
* kAudioUnitSubType_HALOutput has a fixed device; the other
* subtypes follow the current default device, which can change
* at any time.
*/
AudioDeviceID GetDeviceID() const;

void Enable() override;
void Disable() noexcept override;

Expand Down Expand Up @@ -143,11 +151,12 @@ OSXOutput::OSXOutput(const ConfigBlock &block)
}
}

AudioOutput *
OSXOutput::Create(EventLoop &, const ConfigBlock &block)
/**
* Query the current default output device.
*/
static AudioDeviceID
GetDefaultOutputDevice(OSType component_subtype)
{
OSXOutput *oo = new OSXOutput(block);

static constexpr AudioObjectPropertyAddress default_system_output_device{
kAudioHardwarePropertyDefaultSystemOutputDevice,
kAudioObjectPropertyScopeOutput,
Expand All @@ -161,23 +170,38 @@ OSXOutput::Create(EventLoop &, const ConfigBlock &block)
};

const auto &aopa =
oo->component_subtype == kAudioUnitSubType_SystemOutput
component_subtype == kAudioUnitSubType_SystemOutput
// get system output dev_id if configured
? default_system_output_device
/* fallback to default device initially (can still be
changed by osx_output_set_device) */
: default_output_device;

return AudioObjectGetPropertyDataT<AudioDeviceID>(kAudioObjectSystemObject,
aopa);
}

AudioOutput *
OSXOutput::Create(EventLoop &, const ConfigBlock &block)
{
OSXOutput *oo = new OSXOutput(block);

try {
oo->dev_id = AudioObjectGetPropertyDataT<AudioDeviceID>(kAudioObjectSystemObject,
aopa);
/* fallback to default device initially (can still be
changed by osx_output_set_device) */
oo->dev_id = GetDefaultOutputDevice(oo->component_subtype);
} catch (...) {
oo->dev_id = kAudioDeviceUnknown;
}

return oo;
}

AudioDeviceID
OSXOutput::GetDeviceID() const
{
return component_subtype == kAudioUnitSubType_HALOutput
? dev_id
: GetDefaultOutputDevice(component_subtype);
}

int
OSXOutput::GetVolume()
Expand All @@ -188,7 +212,7 @@ OSXOutput::GetVolume()
kAudioObjectPropertyElementMain,
};

const auto vol = AudioObjectGetPropertyDataT<Float32>(dev_id,
const auto vol = AudioObjectGetPropertyDataT<Float32>(GetDeviceID(),
aopa);

return std::lround(vol * 100.0f);
Expand All @@ -204,7 +228,7 @@ OSXOutput::SetVolume(unsigned new_volume)
};

const Float32 vol = new_volume / 100.0f;
AudioObjectSetPropertyDataT(dev_id, aopa, vol);
AudioObjectSetPropertyDataT(GetDeviceID(), aopa, vol);
}

static void
Expand Down Expand Up @@ -692,7 +716,7 @@ OSXOutput::Open(AudioFormat &audio_format)
asbd.mChannelsPerFrame = audio_format.channels;

[[maybe_unused]] const Float64 sample_rate =
osx_output_set_device_format(dev_id, asbd);
osx_output_set_device_format(GetDeviceID(), asbd);

#ifdef ENABLE_DSD
if (audio_format.format == SampleFormat::DSD &&
Expand Down
Loading