Merge pull request #707 from generalelectrix/core-audio-disconnect-device
Implement disconnection detection for CoreAudio on MacOS.
This commit is contained in:
commit
def424809b
4 changed files with 329 additions and 167 deletions
|
|
@ -42,6 +42,7 @@ jack = { version = "0.9", optional = true }
|
||||||
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
|
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
|
||||||
core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
|
core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
|
||||||
mach = "0.3" # For access to mach_timebase type.
|
mach = "0.3" # For access to mach_timebase type.
|
||||||
|
parking_lot = "0.12"
|
||||||
|
|
||||||
[target.'cfg(target_os = "macos")'.dependencies]
|
[target.'cfg(target_os = "macos")'.dependencies]
|
||||||
coreaudio-rs = { version = "0.10", default-features = false, features = ["audio_unit", "core_audio"] }
|
coreaudio-rs = { version = "0.10", default-features = false, features = ["audio_unit", "core_audio"] }
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,17 @@ use self::coreaudio::audio_unit::render_callback::{self, data};
|
||||||
use self::coreaudio::audio_unit::{AudioUnit, Element, Scope};
|
use self::coreaudio::audio_unit::{AudioUnit, Element, Scope};
|
||||||
use self::coreaudio::sys::{
|
use self::coreaudio::sys::{
|
||||||
kAudioDevicePropertyAvailableNominalSampleRates, kAudioDevicePropertyBufferFrameSize,
|
kAudioDevicePropertyAvailableNominalSampleRates, kAudioDevicePropertyBufferFrameSize,
|
||||||
kAudioDevicePropertyBufferFrameSizeRange, kAudioDevicePropertyDeviceNameCFString,
|
kAudioDevicePropertyBufferFrameSizeRange, kAudioDevicePropertyDeviceIsAlive,
|
||||||
kAudioDevicePropertyNominalSampleRate, kAudioDevicePropertyScopeOutput,
|
kAudioDevicePropertyDeviceNameCFString, kAudioDevicePropertyNominalSampleRate,
|
||||||
kAudioDevicePropertyStreamConfiguration, kAudioDevicePropertyStreamFormat,
|
kAudioDevicePropertyScopeOutput, kAudioDevicePropertyStreamConfiguration,
|
||||||
kAudioObjectPropertyElementMaster, kAudioObjectPropertyScopeGlobal,
|
kAudioDevicePropertyStreamFormat, kAudioObjectPropertyElementMaster,
|
||||||
kAudioObjectPropertyScopeInput, kAudioObjectPropertyScopeOutput,
|
kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeInput,
|
||||||
kAudioOutputUnitProperty_CurrentDevice, kAudioOutputUnitProperty_EnableIO,
|
kAudioObjectPropertyScopeOutput, kAudioOutputUnitProperty_CurrentDevice,
|
||||||
kAudioUnitProperty_StreamFormat, kCFStringEncodingUTF8, AudioBuffer, AudioBufferList,
|
kAudioOutputUnitProperty_EnableIO, kAudioUnitProperty_StreamFormat, kCFStringEncodingUTF8,
|
||||||
AudioDeviceID, AudioObjectAddPropertyListener, AudioObjectGetPropertyData,
|
AudioBuffer, AudioBufferList, AudioDeviceID, AudioObjectGetPropertyData,
|
||||||
AudioObjectGetPropertyDataSize, AudioObjectID, AudioObjectPropertyAddress,
|
AudioObjectGetPropertyDataSize, AudioObjectID, AudioObjectPropertyAddress,
|
||||||
AudioObjectPropertyScope, AudioObjectRemovePropertyListener, AudioObjectSetPropertyData,
|
AudioObjectPropertyScope, AudioObjectSetPropertyData, AudioStreamBasicDescription,
|
||||||
AudioStreamBasicDescription, AudioValueRange, OSStatus,
|
AudioValueRange, OSStatus,
|
||||||
};
|
};
|
||||||
use crate::traits::{DeviceTrait, HostTrait, StreamTrait};
|
use crate::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
|
@ -28,22 +28,26 @@ use crate::{
|
||||||
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
|
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
|
||||||
SupportedStreamConfigsError,
|
SupportedStreamConfigsError,
|
||||||
};
|
};
|
||||||
use std::cell::RefCell;
|
use parking_lot::Mutex;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use std::ptr::null;
|
use std::ptr::null;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::thread;
|
use std::sync::mpsc::{channel, RecvTimeoutError};
|
||||||
use std::time::Duration;
|
use std::sync::Arc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
pub use self::enumerate::{
|
pub use self::enumerate::{
|
||||||
default_input_device, default_output_device, Devices, SupportedInputConfigs,
|
default_input_device, default_output_device, Devices, SupportedInputConfigs,
|
||||||
SupportedOutputConfigs,
|
SupportedOutputConfigs,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use property_listener::AudioObjectPropertyListener;
|
||||||
|
|
||||||
pub mod enumerate;
|
pub mod enumerate;
|
||||||
|
mod property_listener;
|
||||||
|
|
||||||
/// Coreaudio host, the default host on macOS.
|
/// Coreaudio host, the default host on macOS.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -413,6 +417,8 @@ impl fmt::Debug for Device {
|
||||||
struct StreamInner {
|
struct StreamInner {
|
||||||
playing: bool,
|
playing: bool,
|
||||||
audio_unit: AudioUnit,
|
audio_unit: AudioUnit,
|
||||||
|
/// Manage the lifetime of the closure that handles device diconnection.
|
||||||
|
_disconnect_listener: Option<AudioObjectPropertyListener>,
|
||||||
// Track the device with which the audio unit was spawned.
|
// Track the device with which the audio unit was spawned.
|
||||||
//
|
//
|
||||||
// We must do this so that we can avoid changing the device sample rate if there is already
|
// We must do this so that we can avoid changing the device sample rate if there is already
|
||||||
|
|
@ -421,6 +427,33 @@ struct StreamInner {
|
||||||
device_id: AudioDeviceID,
|
device_id: AudioDeviceID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register the on-disconnect callback.
|
||||||
|
/// This will both stop the stream and call the error callback with DeviceNotAvailable.
|
||||||
|
/// This function should only be called once per stream.
|
||||||
|
fn add_disconnect_listener<E>(
|
||||||
|
stream: &Stream,
|
||||||
|
error_callback: Arc<Mutex<E>>,
|
||||||
|
) -> Result<(), BuildStreamError>
|
||||||
|
where
|
||||||
|
E: FnMut(StreamError) + Send + 'static,
|
||||||
|
{
|
||||||
|
let stream_copy = stream.clone();
|
||||||
|
let mut stream_inner = stream.inner.lock();
|
||||||
|
stream_inner._disconnect_listener = Some(AudioObjectPropertyListener::new(
|
||||||
|
stream_inner.device_id,
|
||||||
|
AudioObjectPropertyAddress {
|
||||||
|
mSelector: kAudioDevicePropertyDeviceIsAlive,
|
||||||
|
mScope: kAudioObjectPropertyScopeGlobal,
|
||||||
|
mElement: kAudioObjectPropertyElementMaster,
|
||||||
|
},
|
||||||
|
move || {
|
||||||
|
let _ = stream_copy.pause();
|
||||||
|
(error_callback.lock())(StreamError::DeviceNotAvailable);
|
||||||
|
},
|
||||||
|
)?);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn audio_unit_from_device(device: &Device, input: bool) -> Result<AudioUnit, coreaudio::Error> {
|
fn audio_unit_from_device(device: &Device, input: bool) -> Result<AudioUnit, coreaudio::Error> {
|
||||||
let output_type = if device.is_default && !input {
|
let output_type = if device.is_default && !input {
|
||||||
coreaudio::audio_unit::IOType::DefaultOutput
|
coreaudio::audio_unit::IOType::DefaultOutput
|
||||||
|
|
@ -468,7 +501,7 @@ impl Device {
|
||||||
config: &StreamConfig,
|
config: &StreamConfig,
|
||||||
sample_format: SampleFormat,
|
sample_format: SampleFormat,
|
||||||
mut data_callback: D,
|
mut data_callback: D,
|
||||||
mut error_callback: E,
|
error_callback: E,
|
||||||
) -> Result<Stream, BuildStreamError>
|
) -> Result<Stream, BuildStreamError>
|
||||||
where
|
where
|
||||||
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
|
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
|
||||||
|
|
@ -478,144 +511,8 @@ impl Device {
|
||||||
let scope = Scope::Output;
|
let scope = Scope::Output;
|
||||||
let element = Element::Input;
|
let element = Element::Input;
|
||||||
|
|
||||||
// Check whether or not we need to change the device sample rate to suit the one specified for the stream.
|
// Potentially change the device sample rate to match the config.
|
||||||
unsafe {
|
set_sample_rate(self.audio_device_id, config.sample_rate)?;
|
||||||
// Get the current sample rate.
|
|
||||||
let mut property_address = AudioObjectPropertyAddress {
|
|
||||||
mSelector: kAudioDevicePropertyNominalSampleRate,
|
|
||||||
mScope: kAudioObjectPropertyScopeGlobal,
|
|
||||||
mElement: kAudioObjectPropertyElementMaster,
|
|
||||||
};
|
|
||||||
let sample_rate: f64 = 0.0;
|
|
||||||
let data_size = mem::size_of::<f64>() as u32;
|
|
||||||
let status = AudioObjectGetPropertyData(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
0,
|
|
||||||
null(),
|
|
||||||
&data_size as *const _ as *mut _,
|
|
||||||
&sample_rate as *const _ as *mut _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
|
|
||||||
// If the requested sample rate is different to the device sample rate, update the device.
|
|
||||||
if sample_rate as u32 != config.sample_rate.0 {
|
|
||||||
// Get available sample rate ranges.
|
|
||||||
property_address.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;
|
|
||||||
let data_size = 0u32;
|
|
||||||
let status = AudioObjectGetPropertyDataSize(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
0,
|
|
||||||
null(),
|
|
||||||
&data_size as *const _ as *mut _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
let n_ranges = data_size as usize / mem::size_of::<AudioValueRange>();
|
|
||||||
let mut ranges: Vec<u8> = vec![];
|
|
||||||
ranges.reserve_exact(data_size as usize);
|
|
||||||
let status = AudioObjectGetPropertyData(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
0,
|
|
||||||
null(),
|
|
||||||
&data_size as *const _ as *mut _,
|
|
||||||
ranges.as_mut_ptr() as *mut _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
|
||||||
let ranges: &'static [AudioValueRange] = slice::from_raw_parts(ranges, n_ranges);
|
|
||||||
|
|
||||||
// Now that we have the available ranges, pick the one matching the desired rate.
|
|
||||||
let sample_rate = config.sample_rate.0;
|
|
||||||
let maybe_index = ranges.iter().position(|r| {
|
|
||||||
r.mMinimum as u32 == sample_rate && r.mMaximum as u32 == sample_rate
|
|
||||||
});
|
|
||||||
let range_index = match maybe_index {
|
|
||||||
None => return Err(BuildStreamError::StreamConfigNotSupported),
|
|
||||||
Some(i) => i,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Update the property selector to specify the nominal sample rate.
|
|
||||||
property_address.mSelector = kAudioDevicePropertyNominalSampleRate;
|
|
||||||
|
|
||||||
// Setting the sample rate of a device is an asynchronous process in coreaudio.
|
|
||||||
//
|
|
||||||
// Thus, we are required to set a `listener` so that we may be notified when the
|
|
||||||
// change occurs.
|
|
||||||
unsafe extern "C" fn rate_listener(
|
|
||||||
device_id: AudioObjectID,
|
|
||||||
_n_addresses: u32,
|
|
||||||
_properties: *const AudioObjectPropertyAddress,
|
|
||||||
rate_ptr: *mut ::std::os::raw::c_void,
|
|
||||||
) -> OSStatus {
|
|
||||||
let rate_ptr: *const f64 = rate_ptr as *const _;
|
|
||||||
let data_size = mem::size_of::<f64>();
|
|
||||||
let property_address = AudioObjectPropertyAddress {
|
|
||||||
mSelector: kAudioDevicePropertyNominalSampleRate,
|
|
||||||
mScope: kAudioObjectPropertyScopeGlobal,
|
|
||||||
mElement: kAudioObjectPropertyElementMaster,
|
|
||||||
};
|
|
||||||
AudioObjectGetPropertyData(
|
|
||||||
device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
0,
|
|
||||||
null(),
|
|
||||||
&data_size as *const _ as *mut _,
|
|
||||||
rate_ptr as *const _ as *mut _,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add our sample rate change listener callback.
|
|
||||||
let reported_rate: f64 = 0.0;
|
|
||||||
let status = AudioObjectAddPropertyListener(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
Some(rate_listener),
|
|
||||||
&reported_rate as *const _ as *mut _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
|
|
||||||
// Finally, set the sample rate.
|
|
||||||
let sample_rate = sample_rate as f64;
|
|
||||||
let status = AudioObjectSetPropertyData(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
0,
|
|
||||||
null(),
|
|
||||||
data_size,
|
|
||||||
&ranges[range_index] as *const _ as *const _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
|
|
||||||
// Wait for the reported_rate to change.
|
|
||||||
//
|
|
||||||
// This should not take longer than a few ms, but we timeout after 1 sec just in case.
|
|
||||||
//
|
|
||||||
// WARNING: a reference to reported_rate is unsafely captured above,
|
|
||||||
// and the loop below assumes it can change - but compiler does not know that!
|
|
||||||
//
|
|
||||||
let timer = ::std::time::Instant::now();
|
|
||||||
while sample_rate != reported_rate {
|
|
||||||
if timer.elapsed() > Duration::from_secs(1) {
|
|
||||||
let description =
|
|
||||||
"timeout waiting for sample rate update for device".into();
|
|
||||||
let err = BackendSpecificError { description };
|
|
||||||
return Err(err.into());
|
|
||||||
}
|
|
||||||
thread::sleep(Duration::from_millis(5));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the `rate_listener` callback.
|
|
||||||
let status = AudioObjectRemovePropertyListener(
|
|
||||||
self.audio_device_id,
|
|
||||||
&property_address as *const _,
|
|
||||||
Some(rate_listener),
|
|
||||||
&reported_rate as *const _ as *mut _,
|
|
||||||
);
|
|
||||||
coreaudio::Error::from_os_status(status)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut audio_unit = audio_unit_from_device(self, true)?;
|
let mut audio_unit = audio_unit_from_device(self, true)?;
|
||||||
|
|
||||||
|
|
@ -646,6 +543,9 @@ impl Device {
|
||||||
BufferSize::Default => (),
|
BufferSize::Default => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let error_callback = Arc::new(Mutex::new(error_callback));
|
||||||
|
let error_callback_disconnect = error_callback.clone();
|
||||||
|
|
||||||
// Register the callback that is being called by coreaudio whenever it needs data to be
|
// Register the callback that is being called by coreaudio whenever it needs data to be
|
||||||
// fed to the audio buffer.
|
// fed to the audio buffer.
|
||||||
let bytes_per_channel = sample_format.sample_size();
|
let bytes_per_channel = sample_format.sample_size();
|
||||||
|
|
@ -670,7 +570,7 @@ impl Device {
|
||||||
// TODO: Need a better way to get delay, for now we assume a double-buffer offset.
|
// TODO: Need a better way to get delay, for now we assume a double-buffer offset.
|
||||||
let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
|
let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error_callback(err.into());
|
(error_callback.lock())(err.into());
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
Ok(cb) => cb,
|
Ok(cb) => cb,
|
||||||
|
|
@ -687,13 +587,22 @@ impl Device {
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
audio_unit.start()?;
|
let stream = Stream::new(StreamInner {
|
||||||
|
|
||||||
Ok(Stream::new(StreamInner {
|
|
||||||
playing: true,
|
playing: true,
|
||||||
|
_disconnect_listener: None,
|
||||||
audio_unit,
|
audio_unit,
|
||||||
device_id: self.audio_device_id,
|
device_id: self.audio_device_id,
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
// If we didn't request the default device, stop the stream if the
|
||||||
|
// device disconnects.
|
||||||
|
if !self.is_default {
|
||||||
|
add_disconnect_listener(&stream, error_callback_disconnect)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.inner.lock().audio_unit.start()?;
|
||||||
|
|
||||||
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_output_stream_raw<D, E>(
|
fn build_output_stream_raw<D, E>(
|
||||||
|
|
@ -701,7 +610,7 @@ impl Device {
|
||||||
config: &StreamConfig,
|
config: &StreamConfig,
|
||||||
sample_format: SampleFormat,
|
sample_format: SampleFormat,
|
||||||
mut data_callback: D,
|
mut data_callback: D,
|
||||||
mut error_callback: E,
|
error_callback: E,
|
||||||
) -> Result<Stream, BuildStreamError>
|
) -> Result<Stream, BuildStreamError>
|
||||||
where
|
where
|
||||||
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
|
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
|
||||||
|
|
@ -740,6 +649,9 @@ impl Device {
|
||||||
BufferSize::Default => (),
|
BufferSize::Default => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let error_callback = Arc::new(Mutex::new(error_callback));
|
||||||
|
let error_callback_disconnect = error_callback.clone();
|
||||||
|
|
||||||
// Register the callback that is being called by coreaudio whenever it needs data to be
|
// Register the callback that is being called by coreaudio whenever it needs data to be
|
||||||
// fed to the audio buffer.
|
// fed to the audio buffer.
|
||||||
let bytes_per_channel = sample_format.sample_size();
|
let bytes_per_channel = sample_format.sample_size();
|
||||||
|
|
@ -761,7 +673,7 @@ impl Device {
|
||||||
|
|
||||||
let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
|
let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error_callback(err.into());
|
(error_callback.lock())(err.into());
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
Ok(cb) => cb,
|
Ok(cb) => cb,
|
||||||
|
|
@ -779,31 +691,194 @@ impl Device {
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
audio_unit.start()?;
|
let stream = Stream::new(StreamInner {
|
||||||
|
|
||||||
Ok(Stream::new(StreamInner {
|
|
||||||
playing: true,
|
playing: true,
|
||||||
|
_disconnect_listener: None,
|
||||||
audio_unit,
|
audio_unit,
|
||||||
device_id: self.audio_device_id,
|
device_id: self.audio_device_id,
|
||||||
}))
|
});
|
||||||
|
|
||||||
|
// If we didn't request the default device, stop the stream if the
|
||||||
|
// device disconnects.
|
||||||
|
if !self.is_default {
|
||||||
|
add_disconnect_listener(&stream, error_callback_disconnect)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.inner.lock().audio_unit.start()?;
|
||||||
|
|
||||||
|
Ok(stream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attempt to set the device sample rate to the provided rate.
|
||||||
|
/// Return an error if the requested sample rate is not supported by the device.
|
||||||
|
fn set_sample_rate(
|
||||||
|
audio_device_id: AudioObjectID,
|
||||||
|
target_sample_rate: SampleRate,
|
||||||
|
) -> Result<(), BuildStreamError> {
|
||||||
|
// Get the current sample rate.
|
||||||
|
let mut property_address = AudioObjectPropertyAddress {
|
||||||
|
mSelector: kAudioDevicePropertyNominalSampleRate,
|
||||||
|
mScope: kAudioObjectPropertyScopeGlobal,
|
||||||
|
mElement: kAudioObjectPropertyElementMaster,
|
||||||
|
};
|
||||||
|
let sample_rate: f64 = 0.0;
|
||||||
|
let data_size = mem::size_of::<f64>() as u32;
|
||||||
|
let status = unsafe {
|
||||||
|
AudioObjectGetPropertyData(
|
||||||
|
audio_device_id,
|
||||||
|
&property_address as *const _,
|
||||||
|
0,
|
||||||
|
null(),
|
||||||
|
&data_size as *const _ as *mut _,
|
||||||
|
&sample_rate as *const _ as *mut _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
coreaudio::Error::from_os_status(status)?;
|
||||||
|
|
||||||
|
// If the requested sample rate is different to the device sample rate, update the device.
|
||||||
|
if sample_rate as u32 != target_sample_rate.0 {
|
||||||
|
// Get available sample rate ranges.
|
||||||
|
property_address.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;
|
||||||
|
let data_size = 0u32;
|
||||||
|
let status = unsafe {
|
||||||
|
AudioObjectGetPropertyDataSize(
|
||||||
|
audio_device_id,
|
||||||
|
&property_address as *const _,
|
||||||
|
0,
|
||||||
|
null(),
|
||||||
|
&data_size as *const _ as *mut _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
coreaudio::Error::from_os_status(status)?;
|
||||||
|
let n_ranges = data_size as usize / mem::size_of::<AudioValueRange>();
|
||||||
|
let mut ranges: Vec<u8> = vec![];
|
||||||
|
ranges.reserve_exact(data_size as usize);
|
||||||
|
let status = unsafe {
|
||||||
|
AudioObjectGetPropertyData(
|
||||||
|
audio_device_id,
|
||||||
|
&property_address as *const _,
|
||||||
|
0,
|
||||||
|
null(),
|
||||||
|
&data_size as *const _ as *mut _,
|
||||||
|
ranges.as_mut_ptr() as *mut _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
coreaudio::Error::from_os_status(status)?;
|
||||||
|
let ranges: *mut AudioValueRange = ranges.as_mut_ptr() as *mut _;
|
||||||
|
let ranges: &'static [AudioValueRange] = unsafe { slice::from_raw_parts(ranges, n_ranges) };
|
||||||
|
|
||||||
|
// Now that we have the available ranges, pick the one matching the desired rate.
|
||||||
|
let sample_rate = target_sample_rate.0;
|
||||||
|
let maybe_index = ranges
|
||||||
|
.iter()
|
||||||
|
.position(|r| r.mMinimum as u32 == sample_rate && r.mMaximum as u32 == sample_rate);
|
||||||
|
let range_index = match maybe_index {
|
||||||
|
None => return Err(BuildStreamError::StreamConfigNotSupported),
|
||||||
|
Some(i) => i,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (send, recv) = channel::<Result<f64, coreaudio::Error>>();
|
||||||
|
let sample_rate_address = AudioObjectPropertyAddress {
|
||||||
|
mSelector: kAudioDevicePropertyNominalSampleRate,
|
||||||
|
mScope: kAudioObjectPropertyScopeGlobal,
|
||||||
|
mElement: kAudioObjectPropertyElementMaster,
|
||||||
|
};
|
||||||
|
// Send sample rate updates back on a channel.
|
||||||
|
let sample_rate_handler = move || {
|
||||||
|
let mut rate: f64 = 0.0;
|
||||||
|
let data_size = mem::size_of::<f64>();
|
||||||
|
|
||||||
|
let result = unsafe {
|
||||||
|
AudioObjectGetPropertyData(
|
||||||
|
audio_device_id,
|
||||||
|
&sample_rate_address as *const _,
|
||||||
|
0,
|
||||||
|
null(),
|
||||||
|
&data_size as *const _ as *mut _,
|
||||||
|
&mut rate as *const _ as *mut _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
send.send(coreaudio::Error::from_os_status(result).map(|_| rate))
|
||||||
|
.ok();
|
||||||
|
};
|
||||||
|
|
||||||
|
let listener = AudioObjectPropertyListener::new(
|
||||||
|
audio_device_id,
|
||||||
|
sample_rate_address,
|
||||||
|
sample_rate_handler,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Finally, set the sample rate.
|
||||||
|
property_address.mSelector = kAudioDevicePropertyNominalSampleRate;
|
||||||
|
let status = unsafe {
|
||||||
|
AudioObjectSetPropertyData(
|
||||||
|
audio_device_id,
|
||||||
|
&property_address as *const _,
|
||||||
|
0,
|
||||||
|
null(),
|
||||||
|
data_size,
|
||||||
|
&ranges[range_index] as *const _ as *const _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
coreaudio::Error::from_os_status(status)?;
|
||||||
|
|
||||||
|
// Wait for the reported_rate to change.
|
||||||
|
//
|
||||||
|
// This should not take longer than a few ms, but we timeout after 1 sec just in case.
|
||||||
|
// We loop over potentially several events from the channel to ensure
|
||||||
|
// that we catch the expected change in sample rate.
|
||||||
|
let mut timeout = Duration::from_secs(1);
|
||||||
|
let start = Instant::now();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match recv.recv_timeout(timeout) {
|
||||||
|
Err(err) => {
|
||||||
|
let description = match err {
|
||||||
|
RecvTimeoutError::Disconnected => {
|
||||||
|
"sample rate listener channel disconnected unexpectedly"
|
||||||
|
}
|
||||||
|
RecvTimeoutError::Timeout => {
|
||||||
|
"timeout waiting for sample rate update for device"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.to_string();
|
||||||
|
return Err(BackendSpecificError { description }.into());
|
||||||
|
}
|
||||||
|
Ok(Ok(reported_sample_rate)) => {
|
||||||
|
if reported_sample_rate == target_sample_rate.0 as f64 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Err(_)) => {
|
||||||
|
// TODO: should we consider collecting this error?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
timeout = timeout
|
||||||
|
.checked_sub(start.elapsed())
|
||||||
|
.unwrap_or(Duration::ZERO);
|
||||||
|
}
|
||||||
|
listener.remove()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Stream {
|
pub struct Stream {
|
||||||
inner: RefCell<StreamInner>,
|
inner: Arc<Mutex<StreamInner>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stream {
|
impl Stream {
|
||||||
fn new(inner: StreamInner) -> Self {
|
fn new(inner: StreamInner) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: RefCell::new(inner),
|
inner: Arc::new(Mutex::new(inner)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StreamTrait for Stream {
|
impl StreamTrait for Stream {
|
||||||
fn play(&self) -> Result<(), PlayStreamError> {
|
fn play(&self) -> Result<(), PlayStreamError> {
|
||||||
let mut stream = self.inner.borrow_mut();
|
let mut stream = self.inner.lock();
|
||||||
|
|
||||||
if !stream.playing {
|
if !stream.playing {
|
||||||
if let Err(e) = stream.audio_unit.start() {
|
if let Err(e) = stream.audio_unit.start() {
|
||||||
|
|
@ -817,7 +892,7 @@ impl StreamTrait for Stream {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pause(&self) -> Result<(), PauseStreamError> {
|
fn pause(&self) -> Result<(), PauseStreamError> {
|
||||||
let mut stream = self.inner.borrow_mut();
|
let mut stream = self.inner.lock();
|
||||||
|
|
||||||
if stream.playing {
|
if stream.playing {
|
||||||
if let Err(e) = stream.audio_unit.stop() {
|
if let Err(e) = stream.audio_unit.stop() {
|
||||||
|
|
|
||||||
85
src/host/coreaudio/macos/property_listener.rs
Normal file
85
src/host/coreaudio/macos/property_listener.rs
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
//! Helper code for registering audio object property listeners.
|
||||||
|
use super::coreaudio::sys::{
|
||||||
|
AudioObjectAddPropertyListener, AudioObjectID, AudioObjectPropertyAddress,
|
||||||
|
AudioObjectRemovePropertyListener, OSStatus,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::BuildStreamError;
|
||||||
|
|
||||||
|
/// A double-indirection to be able to pass a closure (a fat pointer)
|
||||||
|
/// via a single c_void.
|
||||||
|
struct PropertyListenerCallbackWrapper(Box<dyn FnMut() -> ()>);
|
||||||
|
|
||||||
|
/// Maintain an audio object property listener.
|
||||||
|
/// The listener will be removed when this type is dropped.
|
||||||
|
pub struct AudioObjectPropertyListener {
|
||||||
|
callback: Box<PropertyListenerCallbackWrapper>,
|
||||||
|
property_address: AudioObjectPropertyAddress,
|
||||||
|
audio_object_id: AudioObjectID,
|
||||||
|
removed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AudioObjectPropertyListener {
|
||||||
|
/// Attach the provieded callback as a audio object property listener.
|
||||||
|
pub fn new<F: FnMut() -> () + 'static>(
|
||||||
|
audio_object_id: AudioObjectID,
|
||||||
|
property_address: AudioObjectPropertyAddress,
|
||||||
|
callback: F,
|
||||||
|
) -> Result<Self, BuildStreamError> {
|
||||||
|
let callback = Box::new(PropertyListenerCallbackWrapper(Box::new(callback)));
|
||||||
|
unsafe {
|
||||||
|
coreaudio::Error::from_os_status(AudioObjectAddPropertyListener(
|
||||||
|
audio_object_id,
|
||||||
|
&property_address as *const _,
|
||||||
|
Some(property_listener_handler_shim),
|
||||||
|
&*callback as *const _ as *mut _,
|
||||||
|
))?;
|
||||||
|
};
|
||||||
|
Ok(Self {
|
||||||
|
callback,
|
||||||
|
audio_object_id,
|
||||||
|
property_address,
|
||||||
|
removed: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Explicitly remove the property listener.
|
||||||
|
/// Use this method if you need to explicitly handle failure to remove
|
||||||
|
/// the property listener.
|
||||||
|
pub fn remove(mut self) -> Result<(), BuildStreamError> {
|
||||||
|
self.remove_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_inner(&mut self) -> Result<(), BuildStreamError> {
|
||||||
|
unsafe {
|
||||||
|
coreaudio::Error::from_os_status(AudioObjectRemovePropertyListener(
|
||||||
|
self.audio_object_id,
|
||||||
|
&self.property_address as *const _,
|
||||||
|
Some(property_listener_handler_shim),
|
||||||
|
&*self.callback as *const _ as *mut _,
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
self.removed = true;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for AudioObjectPropertyListener {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
if !self.removed {
|
||||||
|
let _ = self.remove_inner();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Callback used to call user-provided closure as a property listener.
|
||||||
|
unsafe extern "C" fn property_listener_handler_shim(
|
||||||
|
_: AudioObjectID,
|
||||||
|
_: u32,
|
||||||
|
_: *const AudioObjectPropertyAddress,
|
||||||
|
callback: *mut ::std::os::raw::c_void,
|
||||||
|
) -> OSStatus {
|
||||||
|
let wrapper = callback as *mut PropertyListenerCallbackWrapper;
|
||||||
|
(*wrapper).0();
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
extern crate coreaudio;
|
extern crate coreaudio;
|
||||||
|
extern crate parking_lot;
|
||||||
|
|
||||||
use self::coreaudio::sys::{
|
use self::coreaudio::sys::{
|
||||||
kAudioFormatFlagIsFloat, kAudioFormatFlagIsPacked, kAudioFormatLinearPCM,
|
kAudioFormatFlagIsFloat, kAudioFormatFlagIsPacked, kAudioFormatLinearPCM,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue