Bug 1530715 - P33: Register and unregister the device-changed callbacks when stream setup and close. r=padenot

1. The code readability is slightly better if we can register and
unregister the callbacks in the same places. The device-chaned callbacks
are registered when stream setup and unregistered when stream close.

2. In the later mutex replacement, the core stream variables that may be
touched by different threads when reinitializing or destroying the
stream, including {in, out}_unit, will be wrapped in a Rust mutex.
Moving these callbacks (un)registration into the same places makes it
easier to find where the critical sections are.

Differential Revision: https://phabricator.services.mozilla.com/D34066

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Chun-Min Chang 2019-07-10 08:06:37 +00:00
Родитель 56e7631637
Коммит 93932acf07
2 изменённых файлов: 25 добавлений и 37 удалений

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

@ -3,4 +3,4 @@ git repository using the update.sh script.
The cubeb-coreaudio-rs git repository is: https://github.com/ChunMinChang/cubeb-coreaudio-rs
The git commit ID used was 87e92cb0760cb7be490dd1588748dc8f1e5ed961 (2019-06-25 11:32:22 -0700)
The git commit ID used was e4ca148920d86a15f2a8dc0f09e60fec480860cd (2019-06-25 11:32:22 -0700)

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

@ -2409,14 +2409,6 @@ impl ContextOps for AudioUnitContext {
return Err(r);
}
if let Err(r) = boxed_stream.install_system_changed_callback() {
cubeb_log!(
"({:p}) Could not install the device change callback.",
boxed_stream.as_ref()
);
return Err(r);
}
let cubeb_stream = unsafe { Stream::from_ptr(Box::into_raw(boxed_stream) as *mut _) };
cubeb_log!(
"({:p}) Cubeb stream init successful.",
@ -2666,13 +2658,6 @@ impl<'ctx> AudioUnitStream<'ctx> {
self.stop_internal();
}
if self.uninstall_device_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall all device change listeners.",
self as *const AudioUnitStream
);
}
{
let mutex_ptr = &mut self.mutex as *mut OwnedCriticalSection;
let _lock = AutoLock::new(unsafe { &mut (*mutex_ptr) });
@ -2798,12 +2783,6 @@ impl<'ctx> AudioUnitStream<'ctx> {
}
if stm_guard.reinit().is_err() {
if stm_guard.uninstall_system_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall system changed callback",
stm_ptr
);
}
stm_guard.notify_state_changed(State::Error);
cubeb_log!(
"({:p}) Could not reopen the stream after switching.",
@ -3449,11 +3428,20 @@ impl<'ctx> AudioUnitStream<'ctx> {
}
}
if self.install_device_changed_callback().is_err() {
if let Err(r) = self.install_system_changed_callback() {
cubeb_log!(
"({:p}) Could not install the device change callback.",
self as *const AudioUnitStream
);
return Err(r);
}
if let Err(r) = self.install_device_changed_callback() {
cubeb_log!(
"({:p}) Could not install all device change callback.",
self as *const AudioUnitStream
);
return Err(r);
}
Ok(())
@ -3462,6 +3450,20 @@ impl<'ctx> AudioUnitStream<'ctx> {
fn close(&mut self) {
self.mutex.assert_current_thread_owns();
if self.uninstall_system_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall the system changed callback",
self as *const AudioUnitStream
);
}
if self.uninstall_device_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall all device change listeners",
self as *const AudioUnitStream
);
}
if !self.input_unit.is_null() {
audio_unit_uninitialize(self.input_unit);
dispose_audio_unit(self.input_unit);
@ -3483,20 +3485,6 @@ impl<'ctx> AudioUnitStream<'ctx> {
}
fn destroy_internal(&mut self) {
if self.uninstall_system_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall the device changed callback",
self as *const AudioUnitStream
);
}
if self.uninstall_device_changed_callback().is_err() {
cubeb_log!(
"({:p}) Could not uninstall all device change listeners",
self as *const AudioUnitStream
);
}
let mutex_ptr = &mut self.mutex as *mut OwnedCriticalSection;
let _lock = AutoLock::new(unsafe { &mut (*mutex_ptr) });
self.close();