Clean up output device code.

CQTexperiment
Jan Weiß 2020-02-17 18:20:48 +01:00
parent f01915ed80
commit da775ce8a6
3 changed files with 36 additions and 19 deletions

View File

@ -26,7 +26,8 @@
- (id)initWithController:(OutputNode *)c; - (id)initWithController:(OutputNode *)c;
- (BOOL)setup; - (BOOL)setup;
- (BOOL)setOutputDevice:(AudioDeviceID)outputDevice; - (OSStatus)setOutputDeviceByID:(AudioDeviceID)deviceID;
- (BOOL)setOutputDeviceWithDeviceDict:(NSDictionary *)deviceDict;
- (void)start; - (void)start;
- (void)pause; - (void)pause;
- (void)resume; - (void)resume;

View File

@ -71,7 +71,8 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc
} }
- (OSStatus)setOutputDeviceByID:(AudioDeviceID)deviceID { - (OSStatus)setOutputDeviceByID:(AudioDeviceID)deviceID
{
OSStatus err; OSStatus err;
if (deviceID == -1) { if (deviceID == -1) {
@ -85,10 +86,13 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &size, &deviceID); err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &size, &deviceID);
if (err != noErr) { if (err != noErr) {
ALog(@"THERE'S NO DEFAULT OUTPUT DEVICE"); DLog(@"THERE'S NO DEFAULT OUTPUT DEVICE");
return err; return err;
} }
else {
outputDeviceID = deviceID;
}
} }
printf("DEVICE: %i\n", deviceID); printf("DEVICE: %i\n", deviceID);
@ -101,6 +105,12 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc
&deviceID, &deviceID,
sizeof(AudioDeviceID)); sizeof(AudioDeviceID));
if (err != noErr) {
DLog(@"No output device with ID %d could be found.", deviceID);
return err;
}
return err; return err;
} }
@ -114,23 +124,26 @@ static OSStatus Sound_Renderer(void *inRefCon, AudioUnitRenderActionFlags *ioAc
if (err != noErr) { if (err != noErr) {
// Try matching by name. // Try matching by name.
NSString *userDeviceName = deviceDict[@"name"]; NSString *userDeviceName = deviceDict[@"name"];
[self enumerateAudioOutputsUsingBlock: [self enumerateAudioOutputsUsingBlock:
^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) { ^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) {
if ([deviceName isEqualToString:userDeviceName]) { if ([deviceName isEqualToString:userDeviceName]) {
err = [self setOutputDeviceByID:deviceID]; err = [self setOutputDeviceByID:deviceID];
#if 0 #if 0
// Disable. Would cause loop by triggering `-observeValueForKeyPath:ofObject:change:context:` above. // Disable. Would cause loop by triggering `-observeValueForKeyPath:ofObject:change:context:` above.
// Update `outputDevice`, in case the ID has changed. // Update `outputDevice`, in case the ID has changed.
NSDictionary *deviceInfo = @{ NSDictionary *deviceInfo = @{
@"name": deviceName, @"name": deviceName,
@"deviceID": [NSNumber numberWithUnsignedInt:deviceID], @"deviceID": @(deviceID),
}; };
[[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"]; [[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"];
#endif #endif
return; DLog(@"Found output device: \"%@\" (%d).", deviceName, deviceID);
}
*stop = YES;
}
}]; }];
} }

View File

@ -9,18 +9,21 @@
[self setSelectsInsertedObjects:NO]; [self setSelectsInsertedObjects:NO];
NSDictionary *defaultDevice = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"]; NSDictionary *defaultDevice = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"];
NSString *defaultDeviceName = defaultDevice[@"name"];
NSNumber *defaultDeviceIDNum = defaultDevice[@"deviceID"];
AudioDeviceID defaultDeviceID = [defaultDeviceIDNum unsignedIntValue];
[self enumerateAudioOutputsUsingBlock: [self enumerateAudioOutputsUsingBlock:
^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) { ^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) {
NSDictionary *deviceInfo = @{ NSDictionary *deviceInfo = @{
@"name": deviceName, @"name": deviceName,
@"deviceID": [NSNumber numberWithUnsignedInt:deviceID], @"deviceID": @(deviceID),
}; };
[self addObject:deviceInfo]; [self addObject:deviceInfo];
if (defaultDevice) { if (defaultDevice) {
if (([[defaultDevice objectForKey:@"deviceID"] isEqualToNumber:[deviceInfo objectForKey:@"deviceID"]]) || if ((deviceID == defaultDeviceID) ||
([[defaultDevice objectForKey:@"name"] isEqualToString:[deviceInfo objectForKey:@"name"]])) { ([deviceName isEqualToString:defaultDeviceName])) {
[self setSelectedObjects:[NSArray arrayWithObject:deviceInfo]]; [self setSelectedObjects:[NSArray arrayWithObject:deviceInfo]];
// Update `outputDevice`, in case the ID has changed. // Update `outputDevice`, in case the ID has changed.
[[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"]; [[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"];