summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2020-05-28 15:46:48 +0200
committerMax Kellermann <max@musicpd.org>2020-07-01 22:48:12 +0200
commitde632882d13fae5ad79acafc4531b707b47ae0f0 (patch)
tree9f4cdad13e57fc4cbe828b4f05078640f26295cd /src
parent745e492d159f34b8c643ebd0d70ab57dc6d5f493 (diff)
output/osx: move code to FindAudioDeviceByName()
Diffstat (limited to 'src')
-rw-r--r--src/output/plugins/OSXOutputPlugin.cxx91
1 files changed, 48 insertions, 43 deletions
diff --git a/src/output/plugins/OSXOutputPlugin.cxx b/src/output/plugins/OSXOutputPlugin.cxx
index 8034575f1..9ccc45cd2 100644
--- a/src/output/plugins/OSXOutputPlugin.cxx
+++ b/src/output/plugins/OSXOutputPlugin.cxx
@@ -542,16 +542,36 @@ osx_output_hog_device(AudioDeviceID dev_id, bool hog)
}
}
-
-static void
-osx_output_set_device(OSXOutput *oo)
+gcc_pure
+static bool
+IsAudioDeviceName(AudioDeviceID id, const char *expected_name) noexcept
{
- OSStatus status;
- UInt32 size;
+ static constexpr AudioObjectPropertyAddress aopa_name{
+ kAudioObjectPropertyName,
+ kAudioObjectPropertyScopeGlobal,
+ kAudioObjectPropertyElementMaster,
+ };
- if (oo->component_subtype != kAudioUnitSubType_HALOutput)
- return;
+ CFStringRef cfname;
+ UInt32 size = sizeof(cfname);
+
+ if (AudioObjectGetPropertyData(id, &aopa_name,
+ 0, nullptr,
+ &size, &cfname) != noErr)
+ return false;
+
+ const Apple::StringRef cfname_(cfname);
+
+ char actual_name[256];
+ if (!cfname_.GetCString(actual_name, sizeof(actual_name)))
+ return false;
+ return StringIsEqual(actual_name, expected_name);
+}
+
+static AudioDeviceID
+FindAudioDeviceByName(const char *name)
+{
/* what are the available audio device IDs? */
static constexpr AudioObjectPropertyAddress aopa_hw_devices{
kAudioHardwarePropertyDevices,
@@ -559,60 +579,45 @@ osx_output_set_device(OSXOutput *oo)
kAudioObjectPropertyElementMaster,
};
- const auto deviceids =
+ const auto ids =
AudioObjectGetPropertyDataArray<AudioDeviceID>(kAudioObjectSystemObject,
aopa_hw_devices);
- /* which audio device matches oo->device_name? */
- static constexpr AudioObjectPropertyAddress aopa_name{
- kAudioObjectPropertyName,
- kAudioObjectPropertyScopeGlobal,
- kAudioObjectPropertyElementMaster,
- };
-
- const unsigned numdevices = deviceids.size();
- unsigned i;
- size = sizeof(CFStringRef);
- for (i = 0; i < numdevices; i++) {
- CFStringRef cfname = nullptr;
- status = AudioObjectGetPropertyData(deviceids[i], &aopa_name,
- 0, nullptr,
- &size, &cfname);
- if (status != noErr)
- continue;
+ for (const auto id : ids) {
+ if (IsAudioDeviceName(id, name))
+ return id;
+ }
- const Apple::StringRef cfname_(cfname);
+ throw FormatRuntimeError("Found no audio device with name '%s' ",
+ name);
+}
- char name[256];
- if (!cfname_.GetCString(name, sizeof(name)))
- continue;
+static void
+osx_output_set_device(OSXOutput *oo)
+{
+ if (oo->component_subtype != kAudioUnitSubType_HALOutput)
+ return;
- if (StringIsEqual(oo->device_name, name)) {
- FormatDebug(osx_output_domain,
- "found matching device: ID=%u, name=%s",
- (unsigned)deviceids[i], name);
- break;
- }
- }
+ const auto id = FindAudioDeviceByName(oo->device_name);
- if (i == numdevices)
- throw FormatRuntimeError("Found no audio device with name '%s' ",
- oo->device_name);
+ FormatDebug(osx_output_domain,
+ "found matching device: ID=%u, name=%s",
+ (unsigned)id, oo->device_name);
+ OSStatus status;
status = AudioUnitSetProperty(oo->au,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
0,
- &(deviceids[i]),
- sizeof(AudioDeviceID));
+ &id, sizeof(id));
if (status != noErr)
Apple::ThrowOSStatus(status,
"Unable to set OS X audio output device");
- oo->dev_id = deviceids[i];
+ oo->dev_id = id;
FormatDebug(osx_output_domain,
"set OS X audio output device ID=%u, name=%s",
- (unsigned)deviceids[i], oo->device_name);
+ (unsigned)id, oo->device_name);
if (oo->channel_map)
osx_output_set_channel_map(oo);