Refactor get_acceptable_latency_range

This commit is contained in:
Chun-Min Chang 2019-04-18 14:30:31 -07:00
Родитель 4f808a2931
Коммит 35408305dd
2 изменённых файлов: 20 добавлений и 26 удалений

Просмотреть файл

@ -1122,37 +1122,35 @@ fn audiounit_uninstall_system_changed_callback(stm: &mut AudioUnitStream) -> Res
Ok(())
}
fn audiounit_get_acceptable_latency_range(latency_range: &mut AudioValueRange) -> Result<()>
fn audiounit_get_acceptable_latency_range() -> Result<(AudioValueRange)>
{
let mut size: usize = 0;
let mut r = NO_ERR;
let mut output_device_id: AudioDeviceID = kAudioObjectUnknown;
let output_device_buffer_size_range = AudioObjectPropertyAddress {
mSelector: kAudioDevicePropertyBufferFrameSizeRange,
mScope: kAudioDevicePropertyScopeOutput,
mElement: kAudioObjectPropertyElementMaster,
};
output_device_id = audiounit_get_default_device_id(DeviceType::OUTPUT);
let output_device_id = audiounit_get_default_device_id(DeviceType::OUTPUT);
if output_device_id == kAudioObjectUnknown {
cubeb_log!("Could not get default output device id.");
return Err(Error::error());
}
/* Get the buffer size range this device supports */
size = mem::size_of_val(latency_range);
assert_eq!(size, mem::size_of::<AudioValueRange>());
r = audio_object_get_property_data(output_device_id,
&output_device_buffer_size_range,
&mut size,
latency_range);
let mut range = AudioValueRange::default();
let mut size = mem::size_of::<AudioValueRange>();
let r = audio_object_get_property_data(
output_device_id,
&output_device_buffer_size_range,
&mut size,
&mut range
);
if r != NO_ERR {
cubeb_log!("AudioObjectGetPropertyData/buffer size range rv={}", r);
return Err(Error::error());
}
Ok(())
Ok(range)
}
fn audiounit_get_default_device_id(devtype: DeviceType) -> AudioObjectID
@ -3472,13 +3470,13 @@ impl ContextOps for AudioUnitContext {
}
#[cfg(not(target_os = "ios"))]
fn min_latency(&mut self, _params: StreamParams) -> Result<u32> {
let mut latency_range = AudioValueRange::default();
if audiounit_get_acceptable_latency_range(&mut latency_range).is_err() {
let range = audiounit_get_acceptable_latency_range();
if range.is_err() {
cubeb_log!("Could not get acceptable latency range.");
return Err(Error::error()); // TODO: return the error we get instead?
}
Ok(cmp::max(latency_range.mMinimum as u32,
Ok(cmp::max(range.unwrap().mMinimum as u32,
SAFE_MIN_LATENCY_FRAMES))
}
#[cfg(target_os = "ios")]

Просмотреть файл

@ -616,22 +616,18 @@ fn test_remove_listener_unknown_device() {
// ------------------------------------
#[test]
fn test_get_acceptable_latency_range() {
let mut latency_range = AudioValueRange::default();
let default_output = test_get_default_device(Scope::Output);
let range = audiounit_get_acceptable_latency_range();
if default_output.is_none() {
println!("No output device.");
assert_eq!(
audiounit_get_acceptable_latency_range(&mut latency_range).unwrap_err(),
Error::error()
);
assert_eq!(range.unwrap_err(), Error::error());
return;
}
assert!(audiounit_get_acceptable_latency_range(&mut latency_range).is_ok());
assert!(latency_range.mMinimum > 0.0);
assert!(latency_range.mMaximum > 0.0);
assert!(latency_range.mMaximum > latency_range.mMinimum);
let range = range.unwrap();
assert!(range.mMinimum > 0.0);
assert!(range.mMaximum > 0.0);
assert!(range.mMaximum > range.mMinimum);
}
// get_default_device_id