use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
use crate::registry::ServiceRegistry;
pub trait MicrophoneStandby: Send + Sync {
fn set_held(&self, held: bool);
}
static PLATFORM: ServiceRegistry<dyn MicrophoneStandby> = ServiceRegistry::new();
static HOLDERS: AtomicUsize = AtomicUsize::new(0);
pub fn set_platform_microphone_standby(standby: Option<Arc<dyn MicrophoneStandby>>) {
match standby {
Some(standby) => PLATFORM.set(standby),
None => PLATFORM.clear(),
}
}
pub fn microphone_standby_available() -> bool {
PLATFORM.get().is_some()
}
pub struct MicrophoneStandbyLease {
_private: (),
}
impl Drop for MicrophoneStandbyLease {
fn drop(&mut self) {
if HOLDERS.fetch_sub(1, Ordering::AcqRel) == 1
&& let Some(standby) = PLATFORM.get()
{
standby.set_held(false);
}
}
}
pub fn hold_microphone_standby() -> MicrophoneStandbyLease {
if HOLDERS.fetch_add(1, Ordering::AcqRel) == 0
&& let Some(standby) = PLATFORM.get()
{
standby.set_held(true);
}
MicrophoneStandbyLease { _private: () }
}
#[cfg(test)]
#[path = "tests/microphone_standby_tests.rs"]
mod tests;