From 8e895c88c0f33fd7ac0bc1ef55ba314c697855a7 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sun, 26 Dec 2021 21:33:28 -0800 Subject: [PATCH] Core Audio output: Properly apply or remove default device change listener when switching preferences between the 'System Default Device' setting and manually setting the device to the current default --- Audio/Output/OutputCoreAudio.m | 45 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Audio/Output/OutputCoreAudio.m b/Audio/Output/OutputCoreAudio.m index a8b85bb6a..de227831f 100644 --- a/Audio/Output/OutputCoreAudio.m +++ b/Audio/Output/OutputCoreAudio.m @@ -127,9 +127,6 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const } if (audioQueue) { - if (outputDeviceID == deviceID) - return noErr; - AudioObjectPropertyAddress defaultDeviceAddress = theAddress; if (listenerapplied && !defaultDevice) { @@ -137,31 +134,33 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const listenerapplied = NO; } - printf("DEVICE: %i\n", deviceID); - outputDeviceID = deviceID; + if (outputDeviceID != deviceID) { + printf("DEVICE: %i\n", deviceID); + outputDeviceID = deviceID; - CFStringRef theDeviceUID; - theAddress.mSelector = kAudioDevicePropertyDeviceUID; - theAddress.mScope = kAudioDevicePropertyScopeOutput; - thePropSize = sizeof(theDeviceUID); - err = AudioObjectGetPropertyData(outputDeviceID, &theAddress, 0, NULL, &thePropSize, &theDeviceUID); + CFStringRef theDeviceUID; + theAddress.mSelector = kAudioDevicePropertyDeviceUID; + theAddress.mScope = kAudioDevicePropertyScopeOutput; + thePropSize = sizeof(theDeviceUID); + err = AudioObjectGetPropertyData(outputDeviceID, &theAddress, 0, NULL, &thePropSize, &theDeviceUID); - if (err) { - DLog(@"Error getting device UID as string"); - return err; - } + if (err) { + DLog(@"Error getting device UID as string"); + return err; + } - err = AudioQueueStop(audioQueue, true); - if (err) { - DLog(@"Error stopping stream to set device"); + err = AudioQueueStop(audioQueue, true); + if (err) { + DLog(@"Error stopping stream to set device"); + CFRelease(theDeviceUID); + return err; + } + primed = NO; + err = AudioQueueSetProperty(audioQueue, kAudioQueueProperty_CurrentDevice, &theDeviceUID, sizeof(theDeviceUID)); CFRelease(theDeviceUID); - return err; + if (running) + [self start]; } - primed = NO; - err = AudioQueueSetProperty(audioQueue, kAudioQueueProperty_CurrentDevice, &theDeviceUID, sizeof(theDeviceUID)); - CFRelease(theDeviceUID); - if (running) - [self start]; if (!listenerapplied && defaultDevice) { AudioObjectAddPropertyListener(kAudioObjectSystemObject, &defaultDeviceAddress, default_device_changed, (__bridge void * _Nullable)(self));