Browse Source

mac-capture: Simplify coreaudio_get_device_id

The current method would enum though all devices looking for a matching
uid instead of directly asking for kAudioHardwarePropertyDeviceForUID.
Asking directly instead would also enable finding hidden devices that
can only be found directly via uid and not via enumerating (were we ever
to need to find such a device).
gxalpha 2 years ago
parent
commit
eaf6444236
1 changed files with 13 additions and 24 deletions
  1. 13 24
      plugins/mac-capture/audio-device-enum.c

+ 13 - 24
plugins/mac-capture/audio-device-enum.c

@@ -137,30 +137,19 @@ void coreaudio_enum_devices(struct device_list *list, bool input)
 	enum_devices(coreaudio_enum_add_device, &data);
 }
 
-struct device_id_data {
-	CFStringRef uid;
-	AudioDeviceID *id;
-	bool found;
-};
-
-static bool get_device_id(void *param, CFStringRef cf_name, CFStringRef cf_uid,
-			  AudioDeviceID id)
-{
-	struct device_id_data *data = param;
-
-	if (CFStringCompare(cf_uid, data->uid, 0) == 0) {
-		*data->id = id;
-		data->found = true;
-		return false;
-	}
-
-	UNUSED_PARAMETER(cf_name);
-	return true;
-}
-
 bool coreaudio_get_device_id(CFStringRef uid, AudioDeviceID *id)
 {
-	struct device_id_data data = {uid, id, false};
-	enum_devices(get_device_id, &data);
-	return data.found;
+	AudioObjectPropertyAddress propertyAddress = {
+		kAudioHardwarePropertyDeviceForUID,
+		kAudioObjectPropertyScopeGlobal,
+		kAudioObjectPropertyElementMaster};
+
+	AudioValueTranslation translation = {&uid, sizeof(CFStringRef), id,
+					     sizeof(AudioDeviceID)};
+	UInt32 size = sizeof(translation);
+
+	OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
+						     &propertyAddress, 0, NULL,
+						     &size, &translation);
+	return result == noErr;
 }