use crate::error::AudioError;
use crate::stage::CaptureStageStats;
use crossbeam_queue::ArrayQueue;
use std::sync::Arc;
use std::time::{Duration, Instant};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Level {
pub level_dbov: f32,
pub speaking: bool,
}
impl Default for Level {
fn default() -> Self {
Level {
level_dbov: -127.0,
speaking: false,
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Levels {
pub local: Level,
pub participants: Vec<(u64, Level)>,
pub seq: u64,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct DeviceInfo {
pub id: String,
pub name: String,
pub is_default: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DeviceList {
pub inputs: Vec<DeviceInfo>,
pub outputs: Vec<DeviceInfo>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum DeviceChoice {
#[default]
Default,
Id(String),
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DeviceSelection {
pub input: Option<DeviceChoice>,
pub output: Option<DeviceChoice>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DevicesInUse {
pub input: Option<DeviceInfo>,
pub output: Option<DeviceInfo>,
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Event {
Devices(DeviceList),
DevicesInUse(DevicesInUse),
Error(AudioError),
Levels(Levels),
CaptureStage(CaptureStageStats),
#[cfg(any(
test,
all(feature = "os-voice", any(target_os = "ios", target_os = "android"))
))]
PhoneMode(crate::phone::PhoneModeChange),
}
#[derive(Clone)]
pub struct EventRx {
pub(crate) q: Arc<ArrayQueue<Event>>,
}
impl EventRx {
pub fn try_recv(&self) -> Option<Event> {
self.q.pop()
}
pub fn recv_timeout(&self, timeout: Duration) -> Option<Event> {
let deadline = Instant::now() + timeout;
loop {
if let Some(e) = self.q.pop() {
return Some(e);
}
if Instant::now() >= deadline {
return None;
}
std::thread::sleep(Duration::from_millis(1));
}
}
}
impl std::fmt::Debug for EventRx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EventRx")
.field("len", &self.q.len())
.finish()
}
}