Exclude virtual devices and aggregate devices for Audio input devices

This commit is contained in:
Beingpax 2025-12-04 21:50:23 +05:45
parent feea0d7f2a
commit 8cc83f84dc

View File

@ -134,7 +134,7 @@ class AudioDeviceManager: ObservableObject {
let devices = deviceIDs.compactMap { deviceID -> (id: AudioDeviceID, uid: String, name: String)? in let devices = deviceIDs.compactMap { deviceID -> (id: AudioDeviceID, uid: String, name: String)? in
guard let name = getDeviceName(deviceID: deviceID), guard let name = getDeviceName(deviceID: deviceID),
let uid = getDeviceUID(deviceID: deviceID), let uid = getDeviceUID(deviceID: deviceID),
isInputDevice(deviceID: deviceID) else { isValidInputDevice(deviceID: deviceID) else {
return nil return nil
} }
return (id: deviceID, uid: uid, name: name) return (id: deviceID, uid: uid, name: name)
@ -162,13 +162,13 @@ class AudioDeviceManager: ObservableObject {
return name as String? return name as String?
} }
private func isInputDevice(deviceID: AudioDeviceID) -> Bool { private func isValidInputDevice(deviceID: AudioDeviceID) -> Bool {
var address = AudioObjectPropertyAddress( var address = AudioObjectPropertyAddress(
mSelector: kAudioDevicePropertyStreamConfiguration, mSelector: kAudioDevicePropertyStreamConfiguration,
mScope: kAudioDevicePropertyScopeInput, mScope: kAudioDevicePropertyScopeInput,
mElement: kAudioObjectPropertyElementMain mElement: kAudioObjectPropertyElementMain
) )
var propertySize: UInt32 = 0 var propertySize: UInt32 = 0
var result = AudioObjectGetPropertyDataSize( var result = AudioObjectGetPropertyDataSize(
deviceID, deviceID,
@ -177,15 +177,15 @@ class AudioDeviceManager: ObservableObject {
nil, nil,
&propertySize &propertySize
) )
if result != noErr { if result != noErr {
logger.error("Error checking input capability for device \(deviceID): \(result)") logger.error("Error checking input capability for device \(deviceID): \(result)")
return false return false
} }
let bufferList = UnsafeMutablePointer<AudioBufferList>.allocate(capacity: Int(propertySize)) let bufferList = UnsafeMutablePointer<AudioBufferList>.allocate(capacity: Int(propertySize))
defer { bufferList.deallocate() } defer { bufferList.deallocate() }
result = AudioObjectGetPropertyData( result = AudioObjectGetPropertyData(
deviceID, deviceID,
&address, &address,
@ -194,16 +194,42 @@ class AudioDeviceManager: ObservableObject {
&propertySize, &propertySize,
bufferList bufferList
) )
if result != noErr { if result != noErr {
logger.error("Error getting stream configuration for device \(deviceID): \(result)") logger.error("Error getting stream configuration for device \(deviceID): \(result)")
return false return false
} }
let bufferCount = Int(bufferList.pointee.mNumberBuffers) let bufferCount = Int(bufferList.pointee.mNumberBuffers)
return bufferCount > 0 guard bufferCount > 0 else {
return false
}
address.mSelector = kAudioDevicePropertyTransportType
address.mScope = kAudioObjectPropertyScopeGlobal
var transportType: UInt32 = 0
propertySize = UInt32(MemoryLayout<UInt32>.size)
let status = AudioObjectGetPropertyData(
deviceID,
&address,
0,
nil,
&propertySize,
&transportType
)
if status != noErr {
logger.warning("Could not get transport type for device \(deviceID), including it anyway")
return true
}
let isVirtual = transportType == kAudioDeviceTransportTypeVirtual
let isAggregate = transportType == kAudioDeviceTransportTypeAggregate
return !isVirtual && !isAggregate
} }
func selectDevice(id: AudioDeviceID) { func selectDevice(id: AudioDeviceID) {
logger.info("Selecting device with ID: \(id)") logger.info("Selecting device with ID: \(id)")
if let name = getDeviceName(deviceID: id) { if let name = getDeviceName(deviceID: id) {