From baa697c7fc94dbe50e52ea33f66063d729666398 Mon Sep 17 00:00:00 2001 From: general electrix Date: Sat, 8 Oct 2022 01:38:05 -0700 Subject: [PATCH 1/5] Implement disconnection detection for CoreAudio on MacOS. * Create AudioObjectPropertyListener type, providing an easy way to attach a Rust callback. * Refactor the existing sample rate update mechanism to use AudioObjectPropertyListener. * Add an optional property listener that registers a callback that is triggered on device disconnection. * Create a set_on_disconnect method to register the disconnection callback. --- src/host/coreaudio/macos/mod.rs | 357 ++++++++++-------- src/host/coreaudio/macos/property_listener.rs | 85 +++++ 2 files changed, 292 insertions(+), 150 deletions(-) create mode 100644 src/host/coreaudio/macos/property_listener.rs diff --git a/src/host/coreaudio/macos/mod.rs b/src/host/coreaudio/macos/mod.rs index e96d708..b81665e 100644 --- a/src/host/coreaudio/macos/mod.rs +++ b/src/host/coreaudio/macos/mod.rs @@ -8,17 +8,17 @@ use self::coreaudio::audio_unit::render_callback::{self, data}; use self::coreaudio::audio_unit::{AudioUnit, Element, Scope}; use self::coreaudio::sys::{ kAudioDevicePropertyAvailableNominalSampleRates, kAudioDevicePropertyBufferFrameSize, - kAudioDevicePropertyBufferFrameSizeRange, kAudioDevicePropertyDeviceNameCFString, - kAudioDevicePropertyNominalSampleRate, kAudioDevicePropertyScopeOutput, - kAudioDevicePropertyStreamConfiguration, kAudioDevicePropertyStreamFormat, - kAudioObjectPropertyElementMaster, kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyScopeInput, kAudioObjectPropertyScopeOutput, - kAudioOutputUnitProperty_CurrentDevice, kAudioOutputUnitProperty_EnableIO, - kAudioUnitProperty_StreamFormat, kCFStringEncodingUTF8, AudioBuffer, AudioBufferList, - AudioDeviceID, AudioObjectAddPropertyListener, AudioObjectGetPropertyData, + kAudioDevicePropertyBufferFrameSizeRange, kAudioDevicePropertyDeviceIsAlive, + kAudioDevicePropertyDeviceNameCFString, kAudioDevicePropertyNominalSampleRate, + kAudioDevicePropertyScopeOutput, kAudioDevicePropertyStreamConfiguration, + kAudioDevicePropertyStreamFormat, kAudioObjectPropertyElementMaster, + kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyScopeInput, + kAudioObjectPropertyScopeOutput, kAudioOutputUnitProperty_CurrentDevice, + kAudioOutputUnitProperty_EnableIO, kAudioUnitProperty_StreamFormat, kCFStringEncodingUTF8, + AudioBuffer, AudioBufferList, AudioDeviceID, AudioObjectGetPropertyData, AudioObjectGetPropertyDataSize, AudioObjectID, AudioObjectPropertyAddress, - AudioObjectPropertyScope, AudioObjectRemovePropertyListener, AudioObjectSetPropertyData, - AudioStreamBasicDescription, AudioValueRange, OSStatus, + AudioObjectPropertyScope, AudioObjectSetPropertyData, AudioStreamBasicDescription, + AudioValueRange, OSStatus, }; use crate::traits::{DeviceTrait, HostTrait, StreamTrait}; use crate::{ @@ -35,15 +35,18 @@ use std::mem; use std::os::raw::c_char; use std::ptr::null; use std::slice; -use std::thread; -use std::time::Duration; +use std::sync::mpsc::{channel, RecvTimeoutError}; +use std::time::{Duration, Instant}; pub use self::enumerate::{ default_input_device, default_output_device, Devices, SupportedInputConfigs, SupportedOutputConfigs, }; +use property_listener::AudioObjectPropertyListener; + pub mod enumerate; +mod property_listener; /// Coreaudio host, the default host on macOS. #[derive(Debug)] @@ -413,6 +416,8 @@ impl fmt::Debug for Device { struct StreamInner { playing: bool, audio_unit: AudioUnit, + /// Track whether the device has been disconnected. + disconnect_listener: Option, // 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 @@ -421,6 +426,30 @@ struct StreamInner { device_id: AudioDeviceID, } +impl StreamInner { + /// Add or replace an existing callback that will be called if the device is disconnected. + fn set_on_disconnect () + 'static>( + &mut self, + callback: F, + ) -> Result<(), BuildStreamError> { + // Drop any existing callback first to ensure deregistration happens before + // we register the new callback. + { + self.disconnect_listener = None; + } + self.disconnect_listener = Some(AudioObjectPropertyListener::new( + self.device_id, + AudioObjectPropertyAddress { + mSelector: kAudioDevicePropertyDeviceIsAlive, + mScope: kAudioObjectPropertyScopeGlobal, + mElement: kAudioObjectPropertyElementMaster, + }, + callback, + )?); + Ok(()) + } +} + fn audio_unit_from_device(device: &Device, input: bool) -> Result { let output_type = if device.is_default && !input { coreaudio::audio_unit::IOType::DefaultOutput @@ -478,144 +507,8 @@ impl Device { let scope = Scope::Output; let element = Element::Input; - // Check whether or not we need to change the device sample rate to suit the one specified for the stream. - unsafe { - // 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::() 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::(); - let mut ranges: Vec = 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::(); - 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)?; - } - } + // Potentially change the device sample rate to match the config. + set_sample_rate(self.audio_device_id, config.sample_rate)?; let mut audio_unit = audio_unit_from_device(self, true)?; @@ -691,6 +584,7 @@ impl Device { Ok(Stream::new(StreamInner { playing: true, + disconnect_listener: None, audio_unit, device_id: self.audio_device_id, })) @@ -783,12 +677,166 @@ impl Device { Ok(Stream::new(StreamInner { playing: true, + disconnect_listener: None, audio_unit, device_id: self.audio_device_id, })) } } +/// 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::() 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::(); + let mut ranges: Vec = 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::>(); + 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::(); + + 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(()) +} + pub struct Stream { inner: RefCell, } @@ -799,6 +847,15 @@ impl Stream { inner: RefCell::new(inner), } } + + /// Add or replace an existing callback that will be called if the device is disconnected. + pub fn set_on_disconnect () + 'static>( + &self, + f: F, + ) -> Result<(), BuildStreamError> { + let mut stream = self.inner.borrow_mut(); + stream.set_on_disconnect(f) + } } impl StreamTrait for Stream { diff --git a/src/host/coreaudio/macos/property_listener.rs b/src/host/coreaudio/macos/property_listener.rs new file mode 100644 index 0000000..f959e26 --- /dev/null +++ b/src/host/coreaudio/macos/property_listener.rs @@ -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 ()>); + +/// Maintain an audio object property listener. +/// The listener will be removed when this type is dropped. +pub struct AudioObjectPropertyListener { + callback: Box, + property_address: AudioObjectPropertyAddress, + audio_object_id: AudioObjectID, + removed: bool, +} + +impl AudioObjectPropertyListener { + /// Attach the provieded callback as a audio object property listener. + pub fn new () + 'static>( + audio_object_id: AudioObjectID, + property_address: AudioObjectPropertyAddress, + callback: F, + ) -> Result { + 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() + } + + fn _remove(&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 { + self._remove().ok(); + } + } +} + +/// 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 +} From 0af6a1c4660fe4d928206a1a903b12625c0720fe Mon Sep 17 00:00:00 2001 From: general electrix Date: Tue, 11 Oct 2022 02:55:03 -0700 Subject: [PATCH 2/5] Refactor disconnection to trigger error_callback. --- Cargo.toml | 1 + src/host/coreaudio/macos/mod.rs | 123 ++++++++++++++++++-------------- src/host/coreaudio/mod.rs | 1 + 3 files changed, 73 insertions(+), 52 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d5f54c7..d31a806 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ jack = { version = "0.9", optional = true } [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. mach = "0.3" # For access to mach_timebase type. +parking_lot = "0.12" [target.'cfg(target_os = "macos")'.dependencies] coreaudio-rs = { version = "0.10", default-features = false, features = ["audio_unit", "core_audio"] } diff --git a/src/host/coreaudio/macos/mod.rs b/src/host/coreaudio/macos/mod.rs index b81665e..94b655f 100644 --- a/src/host/coreaudio/macos/mod.rs +++ b/src/host/coreaudio/macos/mod.rs @@ -28,7 +28,7 @@ use crate::{ SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange, SupportedStreamConfigsError, }; -use std::cell::RefCell; +use parking_lot::Mutex; use std::ffi::CStr; use std::fmt; use std::mem; @@ -36,6 +36,7 @@ use std::os::raw::c_char; use std::ptr::null; use std::slice; use std::sync::mpsc::{channel, RecvTimeoutError}; +use std::sync::Arc; use std::time::{Duration, Instant}; pub use self::enumerate::{ @@ -416,8 +417,8 @@ impl fmt::Debug for Device { struct StreamInner { playing: bool, audio_unit: AudioUnit, - /// Track whether the device has been disconnected. - disconnect_listener: Option, + /// Manage the lifetime of the closure that handles device diconnection. + _disconnect_listener: Option, // 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 @@ -426,28 +427,32 @@ struct StreamInner { device_id: AudioDeviceID, } -impl StreamInner { - /// Add or replace an existing callback that will be called if the device is disconnected. - fn set_on_disconnect () + 'static>( - &mut self, - callback: F, - ) -> Result<(), BuildStreamError> { - // Drop any existing callback first to ensure deregistration happens before - // we register the new callback. - { - self.disconnect_listener = None; - } - self.disconnect_listener = Some(AudioObjectPropertyListener::new( - self.device_id, - AudioObjectPropertyAddress { - mSelector: kAudioDevicePropertyDeviceIsAlive, - mScope: kAudioObjectPropertyScopeGlobal, - mElement: kAudioObjectPropertyElementMaster, - }, - callback, - )?); - Ok(()) - } +/// Register the on-disconnect callback. +/// This will both stop the stream and call the error callback DeviceNotAvailable. +/// This function should only be called once per stream. +fn add_disconnect_listener( + stream: &Stream, + error_callback: Arc>, +) -> 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 || { + println!("Disconnecting."); + let _ = stream_copy.pause(); + (error_callback.lock())(StreamError::DeviceNotAvailable); + }, + )?); + Ok(()) } fn audio_unit_from_device(device: &Device, input: bool) -> Result { @@ -497,7 +502,7 @@ impl Device { config: &StreamConfig, sample_format: SampleFormat, mut data_callback: D, - mut error_callback: E, + error_callback: E, ) -> Result where D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, @@ -539,6 +544,9 @@ impl Device { 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 // fed to the audio buffer. let bytes_per_channel = sample_format.sample_size(); @@ -563,7 +571,7 @@ impl Device { // 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) { Err(err) => { - error_callback(err.into()); + (error_callback.lock())(err.into()); return Err(()); } Ok(cb) => cb, @@ -580,14 +588,22 @@ impl Device { Ok(()) })?; - audio_unit.start()?; - - Ok(Stream::new(StreamInner { + let stream = Stream::new(StreamInner { playing: true, - disconnect_listener: None, + _disconnect_listener: None, audio_unit, 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( @@ -595,7 +611,7 @@ impl Device { config: &StreamConfig, sample_format: SampleFormat, mut data_callback: D, - mut error_callback: E, + error_callback: E, ) -> Result where D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, @@ -634,6 +650,9 @@ impl Device { 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 // fed to the audio buffer. let bytes_per_channel = sample_format.sample_size(); @@ -655,7 +674,7 @@ impl Device { let callback = match host_time_to_stream_instant(args.time_stamp.mHostTime) { Err(err) => { - error_callback(err.into()); + (error_callback.lock())(err.into()); return Err(()); } Ok(cb) => cb, @@ -673,14 +692,22 @@ impl Device { Ok(()) })?; - audio_unit.start()?; - - Ok(Stream::new(StreamInner { + let stream = Stream::new(StreamInner { playing: true, - disconnect_listener: None, + _disconnect_listener: None, audio_unit, 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) } } @@ -837,30 +864,22 @@ fn set_sample_rate( Ok(()) } +#[derive(Clone)] pub struct Stream { - inner: RefCell, + inner: Arc>, } impl Stream { fn new(inner: StreamInner) -> Self { Self { - inner: RefCell::new(inner), + inner: Arc::new(Mutex::new(inner)), } } - - /// Add or replace an existing callback that will be called if the device is disconnected. - pub fn set_on_disconnect () + 'static>( - &self, - f: F, - ) -> Result<(), BuildStreamError> { - let mut stream = self.inner.borrow_mut(); - stream.set_on_disconnect(f) - } } impl StreamTrait for Stream { fn play(&self) -> Result<(), PlayStreamError> { - let mut stream = self.inner.borrow_mut(); + let mut stream = self.inner.lock(); if !stream.playing { if let Err(e) = stream.audio_unit.start() { @@ -874,7 +893,7 @@ impl StreamTrait for Stream { } fn pause(&self) -> Result<(), PauseStreamError> { - let mut stream = self.inner.borrow_mut(); + let mut stream = self.inner.lock(); if stream.playing { if let Err(e) = stream.audio_unit.stop() { diff --git a/src/host/coreaudio/mod.rs b/src/host/coreaudio/mod.rs index 73d8509..021986c 100644 --- a/src/host/coreaudio/mod.rs +++ b/src/host/coreaudio/mod.rs @@ -1,4 +1,5 @@ extern crate coreaudio; +extern crate parking_lot; use self::coreaudio::sys::{ kAudioFormatFlagIsFloat, kAudioFormatFlagIsPacked, kAudioFormatLinearPCM, From 97af8b77ae16540cab6de4956d71b9d6771c0ba6 Mon Sep 17 00:00:00 2001 From: general electrix Date: Tue, 11 Oct 2022 02:59:02 -0700 Subject: [PATCH 3/5] Respond to code review tidbits. --- src/host/coreaudio/macos/property_listener.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/host/coreaudio/macos/property_listener.rs b/src/host/coreaudio/macos/property_listener.rs index f959e26..0731d7c 100644 --- a/src/host/coreaudio/macos/property_listener.rs +++ b/src/host/coreaudio/macos/property_listener.rs @@ -47,10 +47,10 @@ impl AudioObjectPropertyListener { /// Use this method if you need to explicitly handle failure to remove /// the property listener. pub fn remove(mut self) -> Result<(), BuildStreamError> { - self._remove() + self.remove_inner() } - fn _remove(&mut self) -> Result<(), BuildStreamError> { + fn remove_inner(&mut self) -> Result<(), BuildStreamError> { unsafe { coreaudio::Error::from_os_status(AudioObjectRemovePropertyListener( self.audio_object_id, @@ -67,7 +67,7 @@ impl AudioObjectPropertyListener { impl Drop for AudioObjectPropertyListener { fn drop(&mut self) { if !self.removed { - self._remove().ok(); + let _ = self.remove_inner(); } } } From d1bbbe0400facd5aefe610fe04346074eff7a921 Mon Sep 17 00:00:00 2001 From: general electrix Date: Tue, 11 Oct 2022 03:06:34 -0700 Subject: [PATCH 4/5] Fix docstring grammar. --- src/host/coreaudio/macos/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/host/coreaudio/macos/mod.rs b/src/host/coreaudio/macos/mod.rs index 94b655f..bd05013 100644 --- a/src/host/coreaudio/macos/mod.rs +++ b/src/host/coreaudio/macos/mod.rs @@ -428,7 +428,7 @@ struct StreamInner { } /// Register the on-disconnect callback. -/// This will both stop the stream and call the error callback DeviceNotAvailable. +/// 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( stream: &Stream, From 3d99456c2bedb77aa542297d7d15146e746d4a43 Mon Sep 17 00:00:00 2001 From: general electrix Date: Tue, 11 Oct 2022 03:08:10 -0700 Subject: [PATCH 5/5] Remove debug print statement. --- src/host/coreaudio/macos/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/host/coreaudio/macos/mod.rs b/src/host/coreaudio/macos/mod.rs index bd05013..6d55cc4 100644 --- a/src/host/coreaudio/macos/mod.rs +++ b/src/host/coreaudio/macos/mod.rs @@ -447,7 +447,6 @@ where mElement: kAudioObjectPropertyElementMaster, }, move || { - println!("Disconnecting."); let _ = stream_copy.pause(); (error_callback.lock())(StreamError::DeviceNotAvailable); },