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

CQTexperiment
Christopher Snowhill 2021-12-26 21:33:28 -08:00
parent 8435416cd7
commit 8e895c88c0
1 changed files with 22 additions and 23 deletions

View File

@ -127,9 +127,6 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
} }
if (audioQueue) { if (audioQueue) {
if (outputDeviceID == deviceID)
return noErr;
AudioObjectPropertyAddress defaultDeviceAddress = theAddress; AudioObjectPropertyAddress defaultDeviceAddress = theAddress;
if (listenerapplied && !defaultDevice) { if (listenerapplied && !defaultDevice) {
@ -137,31 +134,33 @@ default_device_changed(AudioObjectID inObjectID, UInt32 inNumberAddresses, const
listenerapplied = NO; listenerapplied = NO;
} }
printf("DEVICE: %i\n", deviceID); if (outputDeviceID != deviceID) {
outputDeviceID = deviceID; printf("DEVICE: %i\n", deviceID);
outputDeviceID = deviceID;
CFStringRef theDeviceUID; CFStringRef theDeviceUID;
theAddress.mSelector = kAudioDevicePropertyDeviceUID; theAddress.mSelector = kAudioDevicePropertyDeviceUID;
theAddress.mScope = kAudioDevicePropertyScopeOutput; theAddress.mScope = kAudioDevicePropertyScopeOutput;
thePropSize = sizeof(theDeviceUID); thePropSize = sizeof(theDeviceUID);
err = AudioObjectGetPropertyData(outputDeviceID, &theAddress, 0, NULL, &thePropSize, &theDeviceUID); err = AudioObjectGetPropertyData(outputDeviceID, &theAddress, 0, NULL, &thePropSize, &theDeviceUID);
if (err) { if (err) {
DLog(@"Error getting device UID as string"); DLog(@"Error getting device UID as string");
return err; return err;
} }
err = AudioQueueStop(audioQueue, true); err = AudioQueueStop(audioQueue, true);
if (err) { if (err) {
DLog(@"Error stopping stream to set device"); DLog(@"Error stopping stream to set device");
CFRelease(theDeviceUID);
return err;
}
primed = NO;
err = AudioQueueSetProperty(audioQueue, kAudioQueueProperty_CurrentDevice, &theDeviceUID, sizeof(theDeviceUID));
CFRelease(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) { if (!listenerapplied && defaultDevice) {
AudioObjectAddPropertyListener(kAudioObjectSystemObject, &defaultDeviceAddress, default_device_changed, (__bridge void * _Nullable)(self)); AudioObjectAddPropertyListener(kAudioObjectSystemObject, &defaultDeviceAddress, default_device_changed, (__bridge void * _Nullable)(self));