2019-06-20 20:37:36 +00:00
|
|
|
use {BackendSpecificError, DevicesError, SupportedFormat};
|
2018-01-26 12:49:47 +00:00
|
|
|
use std::mem;
|
|
|
|
use std::ptr::null;
|
|
|
|
use std::vec::IntoIter as VecIntoIter;
|
|
|
|
use super::coreaudio::sys::{
|
|
|
|
AudioDeviceID,
|
|
|
|
AudioObjectPropertyAddress,
|
|
|
|
AudioObjectGetPropertyData,
|
|
|
|
AudioObjectGetPropertyDataSize,
|
|
|
|
kAudioHardwareNoError,
|
2018-02-12 13:10:24 +00:00
|
|
|
kAudioHardwarePropertyDefaultInputDevice,
|
2018-01-26 12:49:47 +00:00
|
|
|
kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
kAudioHardwarePropertyDevices,
|
|
|
|
kAudioObjectPropertyElementMaster,
|
|
|
|
kAudioObjectPropertyScopeGlobal,
|
|
|
|
kAudioObjectSystemObject,
|
|
|
|
OSStatus,
|
|
|
|
};
|
2018-02-12 13:10:24 +00:00
|
|
|
use super::Device;
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
unsafe fn audio_devices() -> Result<Vec<AudioDeviceID>, OSStatus> {
|
2018-01-26 12:49:47 +00:00
|
|
|
let property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioHardwarePropertyDevices,
|
|
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
macro_rules! try_status_or_return {
|
|
|
|
($status:expr) => {
|
|
|
|
if $status != kAudioHardwareNoError as i32 {
|
|
|
|
return Err($status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2018-01-26 12:49:47 +00:00
|
|
|
let data_size = 0u32;
|
|
|
|
let status = AudioObjectGetPropertyDataSize(
|
|
|
|
kAudioObjectSystemObject,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
);
|
|
|
|
try_status_or_return!(status);
|
|
|
|
|
|
|
|
let device_count = data_size / mem::size_of::<AudioDeviceID>() as u32;
|
|
|
|
let mut audio_devices = vec![];
|
|
|
|
audio_devices.reserve_exact(device_count as usize);
|
|
|
|
|
|
|
|
let status = AudioObjectGetPropertyData(
|
|
|
|
kAudioObjectSystemObject,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
audio_devices.as_mut_ptr() as *mut _,
|
|
|
|
);
|
|
|
|
try_status_or_return!(status);
|
|
|
|
|
|
|
|
audio_devices.set_len(device_count as usize);
|
|
|
|
|
|
|
|
Ok(audio_devices)
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub struct Devices(VecIntoIter<AudioDeviceID>);
|
2015-09-24 03:02:28 +00:00
|
|
|
|
2019-06-20 20:37:36 +00:00
|
|
|
impl Devices {
|
|
|
|
pub fn new() -> Result<Self, DevicesError> {
|
2018-01-26 12:49:47 +00:00
|
|
|
let devices = unsafe {
|
2019-06-20 20:37:36 +00:00
|
|
|
match audio_devices() {
|
|
|
|
Ok(devices) => devices,
|
|
|
|
Err(os_status) => {
|
|
|
|
let description = format!("{}", os_status);
|
|
|
|
let err = BackendSpecificError { description };
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 12:49:47 +00:00
|
|
|
};
|
2019-06-20 20:37:36 +00:00
|
|
|
Ok(Devices(devices.into_iter()))
|
2018-02-12 13:10:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 20:37:36 +00:00
|
|
|
unsafe impl Send for Devices {
|
|
|
|
}
|
|
|
|
unsafe impl Sync for Devices {
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
impl Iterator for Devices {
|
|
|
|
type Item = Device;
|
|
|
|
fn next(&mut self) -> Option<Device> {
|
|
|
|
self.0.next().map(|id| Device { audio_device_id: id })
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub fn default_input_device() -> Option<Device> {
|
|
|
|
let property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioHardwarePropertyDefaultInputDevice,
|
|
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
|
|
|
|
let audio_device_id: AudioDeviceID = 0;
|
|
|
|
let data_size = mem::size_of::<AudioDeviceID>();;
|
|
|
|
let status = unsafe {
|
|
|
|
AudioObjectGetPropertyData(
|
|
|
|
kAudioObjectSystemObject,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
&audio_device_id as *const _ as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if status != kAudioHardwareNoError as i32 {
|
|
|
|
return None;
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
2018-02-12 13:10:24 +00:00
|
|
|
|
|
|
|
let device = Device {
|
|
|
|
audio_device_id: audio_device_id,
|
|
|
|
};
|
|
|
|
Some(device)
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub fn default_output_device() -> Option<Device> {
|
2018-01-26 12:49:47 +00:00
|
|
|
let property_address = AudioObjectPropertyAddress {
|
|
|
|
mSelector: kAudioHardwarePropertyDefaultOutputDevice,
|
|
|
|
mScope: kAudioObjectPropertyScopeGlobal,
|
|
|
|
mElement: kAudioObjectPropertyElementMaster,
|
|
|
|
};
|
|
|
|
|
|
|
|
let audio_device_id: AudioDeviceID = 0;
|
|
|
|
let data_size = mem::size_of::<AudioDeviceID>();;
|
|
|
|
let status = unsafe {
|
|
|
|
AudioObjectGetPropertyData(
|
|
|
|
kAudioObjectSystemObject,
|
|
|
|
&property_address as *const _,
|
|
|
|
0,
|
|
|
|
null(),
|
|
|
|
&data_size as *const _ as *mut _,
|
|
|
|
&audio_device_id as *const _ as *mut _,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if status != kAudioHardwareNoError as i32 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
let device = Device {
|
2018-01-26 12:49:47 +00:00
|
|
|
audio_device_id: audio_device_id,
|
|
|
|
};
|
2018-02-12 13:10:24 +00:00
|
|
|
Some(device)
|
2015-09-24 03:02:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 13:10:24 +00:00
|
|
|
pub type SupportedInputFormats = VecIntoIter<SupportedFormat>;
|
|
|
|
pub type SupportedOutputFormats = VecIntoIter<SupportedFormat>;
|