| 1 |
use super::*; |
| 2 |
|
| 3 |
pub fn get_device_uid( |
| 4 |
id: AudioDeviceID, |
| 5 |
devtype: DeviceType, |
| 6 |
) -> std::result::Result<StringRef, OSStatus> { |
| 7 |
assert_ne!(id, kAudioObjectUnknown); |
| 8 |
debug_assert_running_serially(); |
| 9 |
|
| 10 |
let address = get_property_address(Property::DeviceUID, devtype); |
| 11 |
let mut size = mem::size_of::<CFStringRef>(); |
| 12 |
let mut uid: CFStringRef = ptr::null(); |
| 13 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut uid); |
| 14 |
if err == NO_ERR { |
| 15 |
Ok(StringRef::new(uid as _)) |
| 16 |
} else { |
| 17 |
Err(err) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
pub fn get_devices() -> Vec<AudioObjectID> { |
| 22 |
debug_assert_running_serially(); |
| 23 |
let mut size: usize = 0; |
| 24 |
let address = get_property_address( |
| 25 |
Property::HardwareDevices, |
| 26 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 27 |
); |
| 28 |
let mut ret = |
| 29 |
audio_object_get_property_data_size(kAudioObjectSystemObject, &address, &mut size); |
| 30 |
if ret != NO_ERR { |
| 31 |
return Vec::new(); |
| 32 |
} |
| 33 |
// Total number of input and output devices. |
| 34 |
let mut devices: Vec<AudioObjectID> = allocate_array_by_size(size); |
| 35 |
ret = audio_object_get_property_data( |
| 36 |
kAudioObjectSystemObject, |
| 37 |
&address, |
| 38 |
&mut size, |
| 39 |
devices.as_mut_ptr(), |
| 40 |
); |
| 41 |
if ret != NO_ERR { |
| 42 |
return Vec::new(); |
| 43 |
} |
| 44 |
devices |
| 45 |
} |
| 46 |
|
| 47 |
pub fn get_device_model_uid( |
| 48 |
id: AudioDeviceID, |
| 49 |
devtype: DeviceType, |
| 50 |
) -> std::result::Result<StringRef, OSStatus> { |
| 51 |
assert_ne!(id, kAudioObjectUnknown); |
| 52 |
debug_assert_running_serially(); |
| 53 |
|
| 54 |
let address = get_property_address(Property::ModelUID, devtype); |
| 55 |
let mut size = mem::size_of::<CFStringRef>(); |
| 56 |
let mut uid: CFStringRef = ptr::null(); |
| 57 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut uid); |
| 58 |
if err == NO_ERR { |
| 59 |
Ok(StringRef::new(uid as _)) |
| 60 |
} else { |
| 61 |
Err(err) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
pub fn get_device_transport_type( |
| 66 |
id: AudioDeviceID, |
| 67 |
devtype: DeviceType, |
| 68 |
) -> std::result::Result<u32, OSStatus> { |
| 69 |
assert_ne!(id, kAudioObjectUnknown); |
| 70 |
debug_assert_running_serially(); |
| 71 |
|
| 72 |
let address = get_property_address(Property::TransportType, devtype); |
| 73 |
let mut size = mem::size_of::<u32>(); |
| 74 |
let mut transport: u32 = 0; |
| 75 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut transport); |
| 76 |
if err == NO_ERR { |
| 77 |
Ok(transport) |
| 78 |
} else { |
| 79 |
Err(err) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
pub fn get_device_source( |
| 84 |
id: AudioDeviceID, |
| 85 |
devtype: DeviceType, |
| 86 |
) -> std::result::Result<u32, OSStatus> { |
| 87 |
assert_ne!(id, kAudioObjectUnknown); |
| 88 |
debug_assert_running_serially(); |
| 89 |
|
| 90 |
let address = get_property_address(Property::DeviceSource, devtype); |
| 91 |
let mut size = mem::size_of::<u32>(); |
| 92 |
let mut source: u32 = 0; |
| 93 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut source); |
| 94 |
if err == NO_ERR { |
| 95 |
Ok(source) |
| 96 |
} else { |
| 97 |
Err(err) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub fn get_device_source_name( |
| 102 |
id: AudioDeviceID, |
| 103 |
devtype: DeviceType, |
| 104 |
) -> std::result::Result<StringRef, OSStatus> { |
| 105 |
assert_ne!(id, kAudioObjectUnknown); |
| 106 |
debug_assert_running_serially(); |
| 107 |
|
| 108 |
let mut source: u32 = get_device_source(id, devtype)?; |
| 109 |
let address = get_property_address(Property::DeviceSourceName, devtype); |
| 110 |
let mut size = mem::size_of::<AudioValueTranslation>(); |
| 111 |
let mut name: CFStringRef = ptr::null(); |
| 112 |
let mut trl = AudioValueTranslation { |
| 113 |
mInputData: &mut source as *mut u32 as *mut c_void, |
| 114 |
mInputDataSize: mem::size_of::<u32>() as u32, |
| 115 |
mOutputData: &mut name as *mut CFStringRef as *mut c_void, |
| 116 |
mOutputDataSize: mem::size_of::<CFStringRef>() as u32, |
| 117 |
}; |
| 118 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut trl); |
| 119 |
if err == NO_ERR { |
| 120 |
Ok(StringRef::new(name as _)) |
| 121 |
} else { |
| 122 |
Err(err) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
pub fn get_device_name( |
| 127 |
id: AudioDeviceID, |
| 128 |
devtype: DeviceType, |
| 129 |
) -> std::result::Result<StringRef, OSStatus> { |
| 130 |
assert_ne!(id, kAudioObjectUnknown); |
| 131 |
debug_assert_running_serially(); |
| 132 |
|
| 133 |
let address = get_property_address(Property::DeviceName, devtype); |
| 134 |
let mut size = mem::size_of::<CFStringRef>(); |
| 135 |
let mut name: CFStringRef = ptr::null(); |
| 136 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut name); |
| 137 |
if err == NO_ERR { |
| 138 |
Ok(StringRef::new(name as _)) |
| 139 |
} else { |
| 140 |
Err(err) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pub fn get_device_manufacturer( |
| 145 |
id: AudioDeviceID, |
| 146 |
devtype: DeviceType, |
| 147 |
) -> std::result::Result<StringRef, OSStatus> { |
| 148 |
assert_ne!(id, kAudioObjectUnknown); |
| 149 |
debug_assert_running_serially(); |
| 150 |
|
| 151 |
let address = get_property_address(Property::DeviceManufacturer, devtype); |
| 152 |
let mut size = mem::size_of::<CFStringRef>(); |
| 153 |
let mut manufacturer: CFStringRef = ptr::null(); |
| 154 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut manufacturer); |
| 155 |
if err == NO_ERR { |
| 156 |
Ok(StringRef::new(manufacturer as _)) |
| 157 |
} else { |
| 158 |
Err(err) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
pub fn get_device_buffer_frame_size_range( |
| 163 |
id: AudioDeviceID, |
| 164 |
devtype: DeviceType, |
| 165 |
) -> std::result::Result<AudioValueRange, OSStatus> { |
| 166 |
assert_ne!(id, kAudioObjectUnknown); |
| 167 |
debug_assert_running_serially(); |
| 168 |
|
| 169 |
let address = get_property_address(Property::DeviceBufferFrameSizeRange, devtype); |
| 170 |
let mut size = mem::size_of::<AudioValueRange>(); |
| 171 |
let mut range = AudioValueRange::default(); |
| 172 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut range); |
| 173 |
if err == NO_ERR { |
| 174 |
Ok(range) |
| 175 |
} else { |
| 176 |
Err(err) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
pub fn get_device_latency( |
| 181 |
id: AudioDeviceID, |
| 182 |
devtype: DeviceType, |
| 183 |
) -> std::result::Result<u32, OSStatus> { |
| 184 |
assert_ne!(id, kAudioObjectUnknown); |
| 185 |
debug_assert_running_serially(); |
| 186 |
|
| 187 |
let address = get_property_address(Property::DeviceLatency, devtype); |
| 188 |
let mut size = mem::size_of::<u32>(); |
| 189 |
let mut latency: u32 = 0; |
| 190 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut latency); |
| 191 |
if err == NO_ERR { |
| 192 |
Ok(latency) |
| 193 |
} else { |
| 194 |
Err(err) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#[derive(Debug)] |
| 199 |
pub struct DeviceStream { |
| 200 |
pub device: AudioDeviceID, |
| 201 |
pub stream: AudioStreamID, |
| 202 |
} |
| 203 |
pub fn get_device_streams( |
| 204 |
id: AudioDeviceID, |
| 205 |
devtype: DeviceType, |
| 206 |
) -> std::result::Result<Vec<DeviceStream>, OSStatus> { |
| 207 |
assert_ne!(id, kAudioObjectUnknown); |
| 208 |
debug_assert_running_serially(); |
| 209 |
|
| 210 |
let address = get_property_address(Property::DeviceStreams, devtype); |
| 211 |
|
| 212 |
let mut size: usize = 0; |
| 213 |
let err = audio_object_get_property_data_size(id, &address, &mut size); |
| 214 |
if err != NO_ERR { |
| 215 |
return Err(err); |
| 216 |
} |
| 217 |
|
| 218 |
let mut streams = vec![AudioObjectID::default(); size / mem::size_of::<AudioObjectID>()]; |
| 219 |
let err = audio_object_get_property_data(id, &address, &mut size, streams.as_mut_ptr()); |
| 220 |
if err != NO_ERR { |
| 221 |
return Err(err); |
| 222 |
} |
| 223 |
|
| 224 |
let mut device_streams = streams |
| 225 |
.into_iter() |
| 226 |
.map(|stream| DeviceStream { device: id, stream }) |
| 227 |
.collect::<Vec<_>>(); |
| 228 |
if devtype.contains(DeviceType::INPUT) { |
| 229 |
// With VPIO, output devices will/may get a Tap that appears as an input stream on the |
| 230 |
// output device id. It is unclear what kind of Tap this is as it cannot be enumerated |
| 231 |
// as a Tap through the public APIs. There is no property on the stream itself that |
| 232 |
// can consistently identify it as originating from another device's output either. |
| 233 |
// TerminalType gets close but is often kAudioStreamTerminalTypeUnknown, and there are |
| 234 |
// cases reported where real input streams have that TerminalType, too. |
| 235 |
// See Firefox bug 1890186. |
| 236 |
// We rely on AudioObjectID order instead. AudioDeviceID and AudioStreamID (and more) |
| 237 |
// are all AudioObjectIDs underneath, and they're all distinct. The Tap streams |
| 238 |
// mentioned above are created when VPIO is created, and their AudioObjectIDs are higher |
| 239 |
// than the VPIO device's AudioObjectID, but lower than the next *real* device's |
| 240 |
// AudioObjectID. |
| 241 |
// Simplified, a device's native streams have AudioObjectIDs higher than their device's |
| 242 |
// AudioObjectID but lower than the next device's AudioObjectID. |
| 243 |
// We use this to filter streams, and hope that it holds across macOS versions. |
| 244 |
// Note that for aggregate devices this does not hold, as their stream IDs seem to be |
| 245 |
// repurposed by VPIO. We sum up the result of the above algorithm for each of their sub |
| 246 |
// devices instead, as that seems to hold. |
| 247 |
let mut devices = get_devices(); |
| 248 |
let sub_devices = AggregateDevice::get_sub_devices(id); |
| 249 |
if let Ok(sub_device_ids) = sub_devices { |
| 250 |
cubeb_log!( |
| 251 |
"Getting input device streams for aggregate device {}. Summing over sub devices {:?}.", |
| 252 |
id, |
| 253 |
sub_device_ids |
| 254 |
); |
| 255 |
return Ok(sub_device_ids |
| 256 |
.into_iter() |
| 257 |
.filter_map(|sub_id| match sub_id { |
| 258 |
#[allow(non_upper_case_globals)] |
| 259 |
kAudioObjectUnknown => None, |
| 260 |
i => get_device_streams(i, devtype).ok(), |
| 261 |
}) |
| 262 |
.flatten() |
| 263 |
.collect()); |
| 264 |
} |
| 265 |
debug_assert!(devices.contains(&id)); |
| 266 |
devices.sort(); |
| 267 |
let next_id = devices.into_iter().skip_while(|&i| i != id).nth(1); |
| 268 |
cubeb_log!( |
| 269 |
"Filtering input streams {:?} for device {}. Next device is {:?}.", |
| 270 |
device_streams |
| 271 |
.iter() |
| 272 |
.map(|ds| ds.stream) |
| 273 |
.collect::<Vec<_>>(), |
| 274 |
id, |
| 275 |
next_id |
| 276 |
); |
| 277 |
if let Some(next_id) = next_id { |
| 278 |
device_streams.retain(|ds| ds.stream > id && ds.stream < next_id); |
| 279 |
} else { |
| 280 |
device_streams.retain(|ds| ds.stream > id); |
| 281 |
} |
| 282 |
cubeb_log!( |
| 283 |
"Input stream filtering for device {} retained {:?}.", |
| 284 |
id, |
| 285 |
device_streams |
| 286 |
.iter() |
| 287 |
.map(|ds| ds.stream) |
| 288 |
.collect::<Vec<_>>() |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
Ok(device_streams) |
| 293 |
} |
| 294 |
|
| 295 |
pub fn get_device_sample_rate( |
| 296 |
id: AudioDeviceID, |
| 297 |
devtype: DeviceType, |
| 298 |
) -> std::result::Result<f64, OSStatus> { |
| 299 |
assert_ne!(id, kAudioObjectUnknown); |
| 300 |
debug_assert_running_serially(); |
| 301 |
|
| 302 |
let address = get_property_address(Property::DeviceSampleRate, devtype); |
| 303 |
let mut size = mem::size_of::<f64>(); |
| 304 |
let mut rate: f64 = 0.0; |
| 305 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut rate); |
| 306 |
if err == NO_ERR { |
| 307 |
Ok(rate) |
| 308 |
} else { |
| 309 |
Err(err) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
pub fn get_ranges_of_device_sample_rate( |
| 314 |
id: AudioDeviceID, |
| 315 |
devtype: DeviceType, |
| 316 |
) -> std::result::Result<Vec<AudioValueRange>, OSStatus> { |
| 317 |
assert_ne!(id, kAudioObjectUnknown); |
| 318 |
debug_assert_running_serially(); |
| 319 |
|
| 320 |
let address = get_property_address(Property::DeviceSampleRates, devtype); |
| 321 |
|
| 322 |
let mut size: usize = 0; |
| 323 |
let err = audio_object_get_property_data_size(id, &address, &mut size); |
| 324 |
if err != NO_ERR { |
| 325 |
return Err(err); |
| 326 |
} |
| 327 |
|
| 328 |
let mut ranges: Vec<AudioValueRange> = allocate_array_by_size(size); |
| 329 |
let err = audio_object_get_property_data(id, &address, &mut size, ranges.as_mut_ptr()); |
| 330 |
if err == NO_ERR { |
| 331 |
Ok(ranges) |
| 332 |
} else { |
| 333 |
Err(err) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
pub fn get_stream_latency(id: AudioStreamID) -> std::result::Result<u32, OSStatus> { |
| 338 |
assert_ne!(id, kAudioObjectUnknown); |
| 339 |
debug_assert_running_serially(); |
| 340 |
|
| 341 |
let address = get_property_address( |
| 342 |
Property::StreamLatency, |
| 343 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 344 |
); |
| 345 |
let mut size = mem::size_of::<u32>(); |
| 346 |
let mut latency: u32 = 0; |
| 347 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut latency); |
| 348 |
if err == NO_ERR { |
| 349 |
Ok(latency) |
| 350 |
} else { |
| 351 |
Err(err) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
pub fn get_stream_virtual_format( |
| 356 |
id: AudioStreamID, |
| 357 |
) -> std::result::Result<AudioStreamBasicDescription, OSStatus> { |
| 358 |
assert_ne!(id, kAudioObjectUnknown); |
| 359 |
debug_assert_running_serially(); |
| 360 |
|
| 361 |
let address = get_property_address( |
| 362 |
Property::StreamVirtualFormat, |
| 363 |
DeviceType::INPUT | DeviceType::OUTPUT, |
| 364 |
); |
| 365 |
let mut size = mem::size_of::<AudioStreamBasicDescription>(); |
| 366 |
let mut format = AudioStreamBasicDescription::default(); |
| 367 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut format); |
| 368 |
if err == NO_ERR { |
| 369 |
Ok(format) |
| 370 |
} else { |
| 371 |
Err(err) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
pub fn get_clock_domain( |
| 376 |
id: AudioStreamID, |
| 377 |
devtype: DeviceType, |
| 378 |
) -> std::result::Result<u32, OSStatus> { |
| 379 |
assert_ne!(id, kAudioObjectUnknown); |
| 380 |
debug_assert_running_serially(); |
| 381 |
|
| 382 |
let address = get_property_address(Property::ClockDomain, devtype); |
| 383 |
let mut size = mem::size_of::<u32>(); |
| 384 |
let mut clock_domain: u32 = 0; |
| 385 |
let err = audio_object_get_property_data(id, &address, &mut size, &mut clock_domain); |
| 386 |
if err == NO_ERR { |
| 387 |
Ok(clock_domain) |
| 388 |
} else { |
| 389 |
Err(err) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
pub enum Property { |
| 394 |
DeviceBufferFrameSizeRange, |
| 395 |
DeviceIsAlive, |
| 396 |
DeviceLatency, |
| 397 |
DeviceManufacturer, |
| 398 |
DeviceName, |
| 399 |
DeviceSampleRate, |
| 400 |
DeviceSampleRates, |
| 401 |
DeviceSource, |
| 402 |
DeviceSourceName, |
| 403 |
DeviceStreams, |
| 404 |
DeviceUID, |
| 405 |
HardwareDefaultInputDevice, |
| 406 |
HardwareDefaultOutputDevice, |
| 407 |
HardwareDevices, |
| 408 |
ModelUID, |
| 409 |
StreamLatency, |
| 410 |
StreamTerminalType, |
| 411 |
StreamVirtualFormat, |
| 412 |
TransportType, |
| 413 |
ClockDomain, |
| 414 |
} |
| 415 |
|
| 416 |
impl From<Property> for AudioObjectPropertySelector { |
| 417 |
fn from(p: Property) -> Self { |
| 418 |
match p { |
| 419 |
Property::DeviceBufferFrameSizeRange => kAudioDevicePropertyBufferFrameSizeRange, |
| 420 |
Property::DeviceIsAlive => kAudioDevicePropertyDeviceIsAlive, |
| 421 |
Property::DeviceLatency => kAudioDevicePropertyLatency, |
| 422 |
Property::DeviceManufacturer => kAudioObjectPropertyManufacturer, |
| 423 |
Property::DeviceName => kAudioObjectPropertyName, |
| 424 |
Property::DeviceSampleRate => kAudioDevicePropertyNominalSampleRate, |
| 425 |
Property::DeviceSampleRates => kAudioDevicePropertyAvailableNominalSampleRates, |
| 426 |
Property::DeviceSource => kAudioDevicePropertyDataSource, |
| 427 |
Property::DeviceSourceName => kAudioDevicePropertyDataSourceNameForIDCFString, |
| 428 |
Property::DeviceStreams => kAudioDevicePropertyStreams, |
| 429 |
Property::DeviceUID => kAudioDevicePropertyDeviceUID, |
| 430 |
Property::HardwareDefaultInputDevice => kAudioHardwarePropertyDefaultInputDevice, |
| 431 |
Property::HardwareDefaultOutputDevice => kAudioHardwarePropertyDefaultOutputDevice, |
| 432 |
Property::HardwareDevices => kAudioHardwarePropertyDevices, |
| 433 |
Property::ModelUID => kAudioDevicePropertyModelUID, |
| 434 |
Property::StreamLatency => kAudioStreamPropertyLatency, |
| 435 |
Property::StreamTerminalType => kAudioStreamPropertyTerminalType, |
| 436 |
Property::StreamVirtualFormat => kAudioStreamPropertyVirtualFormat, |
| 437 |
Property::TransportType => kAudioDevicePropertyTransportType, |
| 438 |
Property::ClockDomain => kAudioDevicePropertyClockDomain, |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
pub fn get_property_address(property: Property, devtype: DeviceType) -> AudioObjectPropertyAddress { |
| 444 |
const GLOBAL: ffi::cubeb_device_type = |
| 445 |
ffi::CUBEB_DEVICE_TYPE_INPUT | ffi::CUBEB_DEVICE_TYPE_OUTPUT; |
| 446 |
let scope = match devtype.bits() { |
| 447 |
ffi::CUBEB_DEVICE_TYPE_INPUT => kAudioDevicePropertyScopeInput, |
| 448 |
ffi::CUBEB_DEVICE_TYPE_OUTPUT => kAudioDevicePropertyScopeOutput, |
| 449 |
GLOBAL => kAudioObjectPropertyScopeGlobal, |
| 450 |
_ => panic!("Invalid type"), |
| 451 |
}; |
| 452 |
AudioObjectPropertyAddress { |
| 453 |
mSelector: property.into(), |
| 454 |
mScope: scope, |
| 455 |
mElement: kAudioObjectPropertyElementMaster, |
| 456 |
} |
| 457 |
} |