2007-02-20 01:02:23 +00:00
|
|
|
#import "OutputsArrayController.h"
|
|
|
|
|
|
|
|
@implementation OutputsArrayController
|
|
|
|
|
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
|
|
|
[self removeObjects:[self arrangedObjects]];
|
|
|
|
|
|
|
|
[self setSelectsInsertedObjects:NO];
|
2007-02-20 02:08:37 +00:00
|
|
|
|
|
|
|
NSDictionary *defaultDevice = [[[NSUserDefaultsController sharedUserDefaultsController] defaults] objectForKey:@"outputDevice"];
|
2020-02-17 17:20:48 +00:00
|
|
|
NSString *defaultDeviceName = defaultDevice[@"name"];
|
|
|
|
NSNumber *defaultDeviceIDNum = defaultDevice[@"deviceID"];
|
|
|
|
AudioDeviceID defaultDeviceID = [defaultDeviceIDNum unsignedIntValue];
|
|
|
|
|
2020-02-01 13:44:07 +00:00
|
|
|
[self enumerateAudioOutputsUsingBlock:
|
|
|
|
^(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop) {
|
2020-02-01 12:59:30 +00:00
|
|
|
NSDictionary *deviceInfo = @{
|
2020-02-01 13:44:07 +00:00
|
|
|
@"name": deviceName,
|
2020-02-17 17:20:48 +00:00
|
|
|
@"deviceID": @(deviceID),
|
2020-02-01 12:59:30 +00:00
|
|
|
};
|
2007-02-20 01:02:23 +00:00
|
|
|
[self addObject:deviceInfo];
|
2020-02-01 13:44:07 +00:00
|
|
|
|
2007-02-20 02:08:37 +00:00
|
|
|
if (defaultDevice) {
|
2020-02-17 17:20:48 +00:00
|
|
|
if ((deviceID == defaultDeviceID) ||
|
|
|
|
([deviceName isEqualToString:defaultDeviceName])) {
|
2007-02-20 02:08:37 +00:00
|
|
|
[self setSelectedObjects:[NSArray arrayWithObject:deviceInfo]];
|
2020-02-01 12:59:30 +00:00
|
|
|
// Update `outputDevice`, in case the ID has changed.
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:deviceInfo forKey:@"outputDevice"];
|
2007-02-20 02:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-03 08:00:58 +00:00
|
|
|
else {
|
2020-02-01 13:44:07 +00:00
|
|
|
if (deviceID == systemDefaultID) {
|
2013-10-03 08:00:58 +00:00
|
|
|
[self setSelectedObjects:[NSArray arrayWithObject:deviceInfo]];
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 13:44:07 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
if (!defaultDevice) {
|
|
|
|
[self setSelectionIndex:0];
|
2007-02-20 01:02:23 +00:00
|
|
|
}
|
2020-02-01 13:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)enumerateAudioOutputsUsingBlock:(void (NS_NOESCAPE ^ _Nonnull)(NSString *deviceName, AudioDeviceID deviceID, AudioDeviceID systemDefaultID, BOOL *stop))block
|
|
|
|
{
|
|
|
|
UInt32 propsize;
|
|
|
|
AudioObjectPropertyAddress theAddress = {
|
|
|
|
.mSelector = kAudioHardwarePropertyDevices,
|
|
|
|
.mScope = kAudioObjectPropertyScopeGlobal,
|
|
|
|
.mElement = kAudioObjectPropertyElementMaster
|
|
|
|
};
|
|
|
|
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize));
|
|
|
|
UInt32 nDevices = propsize / (UInt32)sizeof(AudioDeviceID);
|
|
|
|
AudioDeviceID *devids = malloc(propsize);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize, devids));
|
2007-02-20 01:02:23 +00:00
|
|
|
|
2020-02-01 13:44:07 +00:00
|
|
|
theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
|
|
|
AudioDeviceID systemDefault;
|
|
|
|
propsize = sizeof(systemDefault);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &propsize, &systemDefault));
|
|
|
|
|
|
|
|
theAddress.mScope = kAudioDevicePropertyScopeOutput;
|
|
|
|
|
|
|
|
for (UInt32 i = 0; i < nDevices; ++i) {
|
|
|
|
CFStringRef name = NULL;
|
|
|
|
propsize = sizeof(name);
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyDeviceNameCFString;
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, &name));
|
2007-02-20 02:08:37 +00:00
|
|
|
|
2020-02-01 13:44:07 +00:00
|
|
|
propsize = 0;
|
|
|
|
theAddress.mSelector = kAudioDevicePropertyStreamConfiguration;
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyDataSize(devids[i], &theAddress, 0, NULL, &propsize));
|
|
|
|
|
|
|
|
if (propsize < sizeof(UInt32)) continue;
|
|
|
|
|
|
|
|
AudioBufferList * bufferList = (AudioBufferList *) malloc(propsize);
|
|
|
|
__Verify_noErr(AudioObjectGetPropertyData(devids[i], &theAddress, 0, NULL, &propsize, bufferList));
|
|
|
|
UInt32 bufferCount = bufferList->mNumberBuffers;
|
|
|
|
free(bufferList);
|
|
|
|
|
|
|
|
if (!bufferCount) continue;
|
|
|
|
|
|
|
|
BOOL stop = NO;
|
|
|
|
block([NSString stringWithString:(__bridge NSString *)name],
|
|
|
|
devids[i],
|
|
|
|
systemDefault,
|
|
|
|
&stop);
|
|
|
|
|
|
|
|
CFRelease(name);
|
|
|
|
|
|
|
|
if (stop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(devids);
|
2007-02-20 01:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|