From 7248868e338fc18ca797f5431d91b8dee8dd0d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 7 Jul 2025 15:12:17 +0800 Subject: [PATCH] Sound (macOS): improve device id detection --- src/detection/sound/sound_apple.c | 34 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/detection/sound/sound_apple.c b/src/detection/sound/sound_apple.c index 88881adc5..cd21912c0 100644 --- a/src/detection/sound/sound_apple.c +++ b/src/detection/sound/sound_apple.c @@ -32,15 +32,6 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) { AudioDeviceID deviceId = deviceIds[index]; - FF_CFTYPE_AUTO_RELEASE CFStringRef name = NULL; - dataSize = sizeof(name); - if(AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){ - kAudioObjectPropertyName, - kAudioObjectPropertyScopeOutput, - kAudioObjectPropertyElementMain - }, 0, NULL, &dataSize, &name) != kAudioHardwareNoError) - continue; - // Ignore input devices if(AudioObjectGetPropertyDataSize(deviceId, &(AudioObjectPropertyAddress){ kAudioDevicePropertyStreams, @@ -71,10 +62,31 @@ const char* ffDetectSound(FFlist* devices /* List of FFSoundDevice */) device->main = deviceId == mainDeviceId; device->active = false; device->volume = FF_SOUND_VOLUME_UNKNOWN; - ffStrbufInitF(&device->identifier, "%u", (unsigned) deviceId); + ffStrbufInit(&device->identifier); ffStrbufInit(&device->name); ffStrbufInitStatic(&device->platformApi, "Core Audio"); - ffCfStrGetString(name, &device->name); + + FF_CFTYPE_AUTO_RELEASE CFStringRef uid = NULL; + dataSize = sizeof(uid); + if(AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress) { + kAudioDevicePropertyDeviceUID, + kAudioObjectPropertyScopeOutput, + kAudioObjectPropertyElementMain + }, 0, NULL, &dataSize, &uid) == kAudioHardwareNoError) + ffCfStrGetString(uid, &device->identifier); + else + ffStrbufAppendF(&device->identifier, "ID-%u", (unsigned) deviceId); + + FF_CFTYPE_AUTO_RELEASE CFStringRef name = NULL; + dataSize = sizeof(name); + if(AudioObjectGetPropertyData(deviceId, &(AudioObjectPropertyAddress){ + kAudioObjectPropertyName, + kAudioObjectPropertyScopeOutput, + kAudioObjectPropertyElementMain + }, 0, NULL, &dataSize, &name) == kAudioHardwareNoError) + ffCfStrGetString(name, &device->name); + else + ffStrbufSet(&device->name, &device->identifier); uint32_t muted; dataSize = sizeof(muted);