//! Independent, self-healing meeting audio sources. //! //! The microphone and the system audio each run on their own supervisor //! thread. A source that fails is dropped and reopened with bounded backoff //! for as long as the meeting records, so losing one never stops the other. //! The system audio source is each OS's own (`super::os`): a Core //! Audio process tap on macOS, a PipeWire monitor of the default output on //! Linux, and WASAPI loopback on Windows. use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::mpsc::{SyncSender, TrySendError}; use std::sync::{Mutex, OnceLock}; use std::thread::{self, JoinHandle}; use std::time::{Duration, Instant}; use color_eyre::eyre::{Result, eyre}; use super::warning::{SilenceAction, SilencePolicy, SilenceRecovery, TrackWarnings}; use super::{AudioPacket, MeetingSource, SAMPLE_RATE}; use crate::dictation::StreamingResampler; pub(super) const RETRY_INITIAL: Duration = Duration::from_millis(500); pub(super) const RETRY_MAX: Duration = Duration::from_secs(5); /// A source that ran this long before failing started; one that dies sooner /// keeps backing off instead of reopening at the initial delay. const STARTED_AFTER: Duration = Duration::from_secs(1); pub(super) const POLL_INTERVAL: Duration = Duration::from_millis(100); /// Core Audio delivers silence continuously; this long without a buffer /// means the device or tap stopped. pub(super) const STALL_TIMEOUT: Duration = Duration::from_secs(3); /// Delivers one source's 16 kHz audio to the track writer, stamped on the /// meeting's shared capture clock. #[derive(Clone)] pub(super) struct PacketSink { pub(super) source: MeetingSource, pub(super) sender: SyncSender, pub(super) drops: Arc, pub(super) clock: Instant, } impl PacketSink { pub(super) fn send(&self, sample_rate: u32, samples: Vec, pts_us: Option) { if samples.is_empty() { return; } record_level(self.source, &samples); let packet = AudioPacket { source: self.source, pts_us, arrival_us: self.clock.elapsed().as_micros() as u64, position: 0, sample_rate, samples, }; if matches!(self.sender.try_send(packet), Err(TrySendError::Full(_))) { self.drops.fetch_add(1, Ordering::Relaxed); } } } /// The newest buffer's loudness per source (f32 bits, 0 to 1) and when it /// arrived (milliseconds since [`level_epoch`]), for the recording pill's /// level histories. One meeting records at a time. static LEVELS: [AtomicU32; 2] = [AtomicU32::new(0), AtomicU32::new(0)]; static LEVEL_AT_MS: [AtomicU64; 2] = [AtomicU64::new(0), AtomicU64::new(0)]; /// A reading older than this is silence: the source stopped delivering. const LEVEL_STALE: Duration = Duration::from_millis(300); fn level_epoch() -> Instant { static EPOCH: OnceLock = OnceLock::new(); *EPOCH.get_or_init(Instant::now) } fn level_slot(source: MeetingSource) -> usize { match source { MeetingSource::Microphone => 0, MeetingSource::System => 1, } } fn record_level(source: MeetingSource, samples: &[f32]) { let slot = level_slot(source); let rms = (samples.iter().map(|s| s * s).sum::() / samples.len() as f32).sqrt(); LEVELS[slot].store(rms.to_bits(), Ordering::Relaxed); let now = level_epoch().elapsed().as_millis() as u64; LEVEL_AT_MS[slot].store(now.max(1), Ordering::Relaxed); } /// Latest microphone and system RMS, in that order, in the same units Dot scope takes. pub fn track_levels() -> [f32; 2] { let now = level_epoch().elapsed().as_millis() as u64; std::array::from_fn(|slot| { fresh_level( now, LEVEL_AT_MS[slot].load(Ordering::Relaxed), f32::from_bits(LEVELS[slot].load(Ordering::Relaxed)), ) }) } fn fresh_level(now: u64, at: u64, level: f32) -> f32 { if at != 0 && now.saturating_sub(at) <= LEVEL_STALE.as_millis() as u64 { level } else { 0.0 } } #[test] fn a_missing_or_stale_track_reads_as_silence() { assert_eq!(fresh_level(500, 0, 0.8), 0.0); assert_eq!(fresh_level(500, 199, 0.8), 0.0); assert_eq!(fresh_level(500, 200, 0.8), 0.8); } /// An open source that the supervisor polls until it fails. pub(super) trait LiveSource { /// Waits briefly for audio and forwards it; an error ends this instance. fn poll(&mut self, sink: &PacketSink) -> std::result::Result<(), String>; fn describe(&self) -> &'static str; } /// Owns one source's supervisor thread. Dropping it stops the source. pub(super) struct SourceSupervisor { stop: Arc, thread: Option>, } impl SourceSupervisor { /// Records the input the call app `application` (a canonical bundle /// identifier) records from where the OS reports it, else the /// configured microphone. pub(super) fn microphone( sink: PacketSink, application: Option, warnings: TrackWarnings, ) -> Self { let recovery = Arc::new(SilenceRecovery::default()); // The processes recording from the input, shared across reopens so a // release between a stream failure and its replacement is still seen. let recorders = Arc::new(InputRecorders::default()); Self::spawn(sink, move || { MicrophoneSource::open( crate::app_settings::microphone_selection(), application.clone(), recovery.clone(), recorders.clone(), warnings.clone(), ) .map(|source| Box::new(source) as Box) }) } /// Records the system audio of the call app `application` (a canonical /// bundle identifier), or of every output for a manual recording. pub(super) fn system(sink: PacketSink, application: Option) -> Self { let source_sink = sink.clone(); Self::spawn(sink, move || { super::os::open_system_audio(&source_sink, application.as_deref()) }) } fn spawn( sink: PacketSink, mut open: impl FnMut() -> Result> + Send + 'static, ) -> Self { let stop = Arc::new(AtomicBool::new(false)); let thread_stop = stop.clone(); let thread = thread::Builder::new() .name(format!("meeting-{:?}", sink.source).to_ascii_lowercase()) .spawn(move || supervise(&sink, &thread_stop, &mut open)) .expect("could not spawn meeting capture thread"); Self { stop, thread: Some(thread), } } } impl Drop for SourceSupervisor { fn drop(&mut self) { self.stop.store(true, Ordering::Relaxed); if let Some(thread) = self.thread.take() && thread.join().is_err() { tracing::error!("meeting capture thread panicked"); } } } fn supervise( sink: &PacketSink, stop: &AtomicBool, open: &mut dyn FnMut() -> Result>, ) { let source = sink.source; let mut delay = RETRY_INITIAL; let mut interrupted = false; let mut last_open_error = None::; while !stop.load(Ordering::Relaxed) { match open() { Ok(mut live) => { tracing::info!( ?source, backend = live.describe(), resumed = interrupted, "meeting capture source started" ); last_open_error = None; let started = Instant::now(); let failure = loop { if stop.load(Ordering::Relaxed) { break None; } if let Err(error) = live.poll(sink) { break Some(error); } }; drop(live); let Some(error) = failure else { return; }; interrupted = true; delay = retry_delay(delay, started.elapsed()); tracing::warn!( ?source, %error, "meeting capture source stopped; keeping the other source and retrying" ); } Err(error) => { let error = format!("{error:#}"); if last_open_error.as_deref() != Some(error.as_str()) { tracing::warn!(?source, %error, "could not open meeting capture source; retrying"); last_open_error = Some(error); } } } let resume_at = Instant::now() + delay; while Instant::now() < resume_at { if stop.load(Ordering::Relaxed) { return; } thread::sleep(POLL_INTERVAL); } delay = delay.saturating_mul(2).min(RETRY_MAX); } } /// The wait before reopening a source that ran for `ran` before it failed: /// the initial delay once it genuinely started, else the delay it already /// reached, so a source that dies right after every open keeps backing off /// instead of spawning and logging twice a second forever. pub(super) fn retry_delay(delay: Duration, ran: Duration) -> Duration { if ran >= STARTED_AFTER { RETRY_INITIAL } else { delay } } /// 16 kHz samples each output's audio is held back before mixing, so two /// outputs playing at once line up instead of following each other. Linux and /// Windows mix their outputs; macOS hears one tap and compiles this only for /// the mixer's test. #[cfg(any(not(target_os = "macos"), test))] pub(super) const MIX_HOLD: usize = SAMPLE_RATE as usize / 20; /// Mixes each output's pending audio up to the latest output's position less /// `hold`, leaving the rest for the next call. An output that delivered /// nothing (nothing plays on it) adds nothing. Linux and Windows mix their /// outputs; macOS hears one tap and compiles this only for the mixer's test. #[cfg(any(not(target_os = "macos"), test))] pub(super) fn mix(pending: &mut [&mut Vec], hold: usize) -> Vec { let length = pending .iter() .map(|samples| samples.len()) .max() .unwrap_or(0) .saturating_sub(hold); let mut mixed = vec![0.0f32; length]; for samples in pending.iter_mut() { let take = samples.len().min(length); for (out, sample) in mixed.iter_mut().zip(samples.drain(..take)) { *out += sample; } } for sample in &mut mixed { *sample = sample.clamp(-1.0, 1.0); } mixed } /// Converts any device rate to the 16 kHz meeting tracks so a reopened /// device with a different rate never changes a track's format. pub(super) struct To16k { pub(super) rate: u32, resampler: StreamingResampler, } impl To16k { pub(super) fn new(rate: u32) -> Result { Ok(Self { rate, resampler: StreamingResampler::new(rate).map_err(|error| eyre!(error))?, }) } pub(super) fn push(&mut self, samples: &[f32]) -> Vec { self.resampler.push(samples) } } /// Looks again, a bounded number of times, for a saved microphone that is /// listed but would not open, while the system default stands in for it. #[derive(Debug, Default)] struct UnopenableRetry { due: Option, tries: u32, } impl UnopenableRetry { const TRIES: u32 = 8; const FIRST: Duration = Duration::from_secs(1); const MAX: Duration = Duration::from_secs(30); /// Schedules the next look after resolving to `selection` at `now`. fn resolved(&mut self, selection: crate::audio::Selection, now: Instant) { if selection != crate::audio::Selection::Unopenable { *self = Self::default(); return; } if self.tries >= Self::TRIES { self.due = None; return; } let delay = Self::FIRST .saturating_mul(1 << self.tries.min(16)) .min(Self::MAX); self.due = Some(now + delay); self.tries += 1; } fn is_due(&self, now: Instant) -> bool { self.due.is_some_and(|due| now >= due) } } /// How often a meeting looks at which input its call app records from. const CALL_INPUT_INTERVAL: Duration = Duration::from_secs(1); /// The input a meeting follows for its call app, by UID: the one it follows /// while the app still reports it or reports none (as some do while muted), /// else the first the app reports. fn follow_call_input(current: Option, reported: Vec) -> Option { match current { Some(current) if reported.is_empty() || reported.contains(¤t) => Some(current), _ => reported.into_iter().next(), } } /// Whether `opened`, the input a meeting records from, is one the call app /// reports recording from. fn call_app_reports_input(opened: Option<&str>, reported: &[String]) -> bool { opened.is_some_and(|opened| reported.iter().any(|reported| reported == opened)) } /// The UIDs of the inputs the call app records from; none where the OS /// does not report them. fn call_app_inputs(application: &str) -> Vec { #[cfg(target_os = "macos")] { crate::microphone_activity::application_input_devices(application).unwrap_or_else(|error| { tracing::debug!(%error, "could not read the inputs the call app uses"); Vec::new() }) } #[cfg(not(target_os = "macos"))] { let _ = application; Vec::new() } } /// The processes recording from the input, shared by every stream the /// supervisor opens so an app that let go between a stream failure and its /// reopen is still seen leaving. #[derive(Default)] struct InputRecorders { seen: Mutex, /// When our own renegotiating open last changed the device's rate; a /// recorder that disappears just after it is the driver restarting /// streams, not another app letting go. renegotiated: Mutex>, } /// The last input whose recorders were seen, and those processes. #[derive(Default)] struct SeenInput { uid: Option, pids: Vec, } impl InputRecorders { /// Whether the shared baseline is for `uid`. fn same_input(&self, uid: Option<&str>) -> bool { self.seen .lock() .unwrap_or_else(|error| error.into_inner()) .uid .as_deref() == uid } /// Records `current` for `uid`, reporting whether a process known on that /// same input stopped recording. A different input starts over. The /// comparison made by our own renegotiating open, and by any reopen /// within [`RENEGOTIATION_SETTLE`] of it, ignores a disappearing pid: /// the driver is restarting its streams, not another app letting go. fn update(&self, uid: Option<&str>, current: Vec, now: Instant) -> bool { let mut renegotiated = self .renegotiated .lock() .unwrap_or_else(|error| error.into_inner()); let mut seen = self.seen.lock().unwrap_or_else(|error| error.into_inner()); let same_input = seen.uid.as_deref() == uid; if !same_input { // The old input's rate change means nothing to this one. *renegotiated = None; } let settled = renegotiated.is_none_or(|at| now.duration_since(at) >= RENEGOTIATION_SETTLE); let released = same_input && settled && released_processes(&seen.pids, ¤t); seen.uid = uid.map(str::to_owned); seen.pids = current; released } /// Starts over on `uid` without treating its difference as a release. fn reset(&self, uid: Option<&str>, current: Vec) { *self.seen.lock().unwrap_or_else(|error| error.into_inner()) = SeenInput { uid: uid.map(str::to_owned), pids: current, }; } /// Notes that this open renegotiated the device's sample rate, which /// restarts its other streams. fn note_renegotiation(&self, now: Instant) { *self .renegotiated .lock() .unwrap_or_else(|error| error.into_inner()) = Some(now); } } /// How long after our own rate change a recorder that disappears is taken to /// be the driver restarting streams rather than another app letting go. const RENEGOTIATION_SETTLE: Duration = Duration::from_secs(1); /// Whether any process that was recording (`known`) is gone from `current`. fn released_processes(known: &[i32], current: &[i32]) -> bool { known.iter().any(|pid| !current.contains(pid)) } /// The processes recording from the input `uid`, Hex's own excluded, from /// Core Audio's process metadata: no audio is read. None when the OS does /// not report them or any part of the read failed, so a failed read never /// looks like a release. fn recording_processes(uid: Option<&str>) -> Option> { #[cfg(target_os = "macos")] { let uid = uid?; let ours = i32::try_from(std::process::id()).ok(); match crate::microphone_activity::input_recorders(uid) { Ok(pids) => Some(pids.into_iter().filter(|pid| Some(*pid) != ours).collect()), Err(error) => { tracing::debug!(%error, "could not read the processes recording from the meeting microphone"); None } } } #[cfg(not(target_os = "macos"))] { let _ = uid; None } } struct MicrophoneSource { input: crate::audio::AudioInput, resample: To16k, last_audio: Instant, /// The per-chunk digital silence decision, sharing this recording's /// reopen budget and retry count with every reopened stream. silence: SilencePolicy, /// The controller-visible warning, shared across reopened streams. warnings: TrackWarnings, selected: Option, /// The [`crate::app_settings::microphone_selection`] revision of `selected`. selection_revision: u64, /// The [`crate::audio::input_route_generation`] the input was resolved at. route_generation: u64, retry: UnopenableRetry, /// The detected call app's canonical bundle identifier. application: Option, /// The UID of the input the call app records from. call_input: Option, /// The input UIDs the call app last reported recording from. call_inputs: Vec, /// The UID of this stream's input. opened_uid: Option, call_input_checked: Instant, /// The processes recording from the input, shared across reopens. recorders: Arc, recorders_checked: Instant, } impl MicrophoneSource { fn open( (selection_revision, selected): (u64, Option), application: Option, silence_recovery: Arc, recorders: Arc, warnings: TrackWarnings, ) -> Result { let route_generation = crate::audio::input_route_generation(); let call_inputs = application .as_deref() .map(call_app_inputs) .unwrap_or_default(); let call_input = follow_call_input(None, call_inputs.clone()); let renegotiate = silence_recovery.take_renegotiation(crate::transcription::engine::dictation_busy()); if !renegotiate && silence_recovery.renegotiation_pending() { // A rate change would cut the dictation. The flag stays set and // the still-silent stream reopens once dictation is idle. tracing::info!( "dictation is active; opening the meeting microphone without renegotiating its rate" ); } let crate::audio::Opened::Input(input) = crate::audio::open_meeting_input( selected.as_deref(), call_input.as_deref(), None, renegotiate, )? else { unreachable!("nothing to keep"); }; // The renegotiation is taken when this open attempts it, so a failing // open cannot flip the rate on every supervisor retry; only a // deferral for dictation leaves it pending. if renegotiate { // The open already did what a retry would: a release noted before // it is spent, though one this open's own comparison sees is not. silence_recovery.forget_release(); } let input = *input; let opened_uid = input.uid.clone(); let now = Instant::now(); // A release from the old input says nothing about the new one. if !recorders.same_input(opened_uid.as_deref()) { silence_recovery.forget_release(); } if application.is_some() { if renegotiate { recorders.note_renegotiation(now); } // Compare this open against the set the last stream saw, so an // app that let go while the source was down still counts as a // release. The shared state keeps it if this stream fails too. if let Some(current) = recording_processes(opened_uid.as_deref()) && recorders.update(opened_uid.as_deref(), current, now) { silence_recovery.note_release(); } } let resample = To16k::new(input.sample_rate)?; tracing::info!( device = input.device_name, rate = resample.rate, call_input = call_input.is_some(), renegotiated = renegotiate, "meeting microphone opened" ); let mut retry = UnopenableRetry::default(); retry.resolved(input.selection, Instant::now()); let warning = warnings.live().is_some(); Ok(Self { input, silence: SilencePolicy::new(resample.rate, silence_recovery, warning), warnings, resample, last_audio: Instant::now(), selected, selection_revision, route_generation, retry, application, call_input, call_inputs, opened_uid, call_input_checked: Instant::now(), recorders, recorders_checked: Instant::now(), }) } /// Whether the call app records from the input this stream opened, which /// is when another app's voice processing can leave it digitally silent. /// A manual recording has no call app and never holds the device. fn call_app_holds_device(&self) -> bool { call_app_reports_input(self.opened_uid.as_deref(), &self.call_inputs) } /// Notes an application other than Hex stopping recording from this /// stream's input since the last look, sampled at the call app's once a /// second cadence from Core Audio process metadata; no audio is read. The /// shared recovery keeps it, so a stream that fails before its next chunk /// cannot lose it. fn poll_released(&mut self, now: Instant) { if self.application.is_none() || now.duration_since(self.recorders_checked) < CALL_INPUT_INTERVAL { return; } self.recorders_checked = now; let Some(current) = recording_processes(self.opened_uid.as_deref()) else { return; }; if self .recorders .update(self.opened_uid.as_deref(), current, now) { self.silence.note_release(); } } /// Follows a change of the input the call app records from, of the /// system default or of the saved microphone in Settings, the saved /// microphone returning, or a due look at one that would not open, by /// opening the input it now resolves to before dropping the old one. A /// failed open keeps the current input. fn follow_route(&mut self) { let now = Instant::now(); self.poll_released(now); let mut call_input_changed = false; if self.application.is_some() && now.duration_since(self.call_input_checked) >= CALL_INPUT_INTERVAL { self.call_input_checked = now; self.call_inputs = self .application .as_deref() .map(call_app_inputs) .unwrap_or_default(); let call_input = follow_call_input(self.call_input.clone(), self.call_inputs.clone()); call_input_changed = call_input != self.call_input; self.call_input = call_input; } let generation = crate::audio::input_route_generation(); let (revision, selected) = crate::app_settings::microphone_selection(); if !call_input_changed && generation == self.route_generation && revision == self.selection_revision && !self.retry.is_due(now) { return; } self.route_generation = generation; if revision != self.selection_revision { self.selection_revision = revision; self.selected = selected; self.retry = UnopenableRetry::default(); } let opened = crate::audio::open_meeting_input( self.selected.as_deref(), self.call_input.as_deref(), Some(&self.input.device_name), false, ); let input = match opened { Ok(crate::audio::Opened::Input(input)) => *input, Ok(crate::audio::Opened::Unchanged { selection }) => { self.input.selection = selection; self.retry.resolved(selection, now); return; } Err(error) => { tracing::warn!(%error, "could not switch the meeting microphone; keeping it"); self.retry.resolved(self.input.selection, now); return; } }; if input.sample_rate != self.resample.rate { match To16k::new(input.sample_rate) { Ok(resample) => self.resample = resample, Err(error) => { tracing::warn!(%error, "could not switch the meeting microphone; keeping it"); self.retry.resolved(self.input.selection, now); return; } } } tracing::info!( from = %self.input.device_name, to = %input.device_name, "meeting microphone switched" ); self.retry.resolved(input.selection, now); // A new device starts a fresh zero count, but a warning raised by the // old one stays until real audio clears it. self.silence.reset(input.sample_rate); self.opened_uid = input.uid.clone(); if let Some(current) = recording_processes(self.opened_uid.as_deref()) { self.recorders.reset(self.opened_uid.as_deref(), current); } self.recorders_checked = now; // A release or renegotiation from the old input says nothing about // the new one. self.silence.forget_release(); self.silence.clear_renegotiation(); self.input = input; self.last_audio = Instant::now(); } } impl LiveSource for MicrophoneSource { fn poll(&mut self, sink: &PacketSink) -> std::result::Result<(), String> { self.follow_route(); match self.input.recv_timeout(POLL_INTERVAL) { crate::audio::AudioInputEvent::Chunk { samples, .. } => { self.last_audio = Instant::now(); let action = self.silence.observe( &samples, self.call_app_holds_device(), crate::transcription::engine::dictation_busy, ); sink.send(SAMPLE_RATE, self.resample.push(&samples), None); match action { SilenceAction::Reopen => { self.silence.request_renegotiation(); if self.silence.retrying() { tracing::warn!( device = %self.input.device_name, "meeting microphone is still digitally silent; retrying it after an app released it" ); } else { tracing::warn!( device = %self.input.device_name, "meeting microphone delivered digital silence; reopening it" ); } return Err("microphone delivered digital silence".into()); } SilenceAction::Gated => { tracing::info!( device = %self.input.device_name, device_uid = ?self.opened_uid, call_app_inputs = ?self.call_inputs, "meeting microphone is digitally silent but the call app does not report recording from it; not reopening or warning" ); } SilenceAction::Warn => { if self.warnings.warn() { self.silence.applied(SilenceAction::Warn); tracing::warn!( device = %self.input.device_name, "meeting microphone is still digitally silent; keeping the stream open" ); } } SilenceAction::Clear => { if self.warnings.clear() { self.silence.applied(SilenceAction::Clear); tracing::info!( device = %self.input.device_name, "meeting microphone audio returned" ); } } SilenceAction::Nothing => {} } Ok(()) } crate::audio::AudioInputEvent::Timeout if self.last_audio.elapsed() > STALL_TIMEOUT => { Err("microphone stopped delivering audio".into()) } crate::audio::AudioInputEvent::Timeout => Ok(()), crate::audio::AudioInputEvent::StreamFailed(error) => Err(error), } } fn describe(&self) -> &'static str { "cpal" } } #[cfg(test)] mod tests { use super::*; use crate::audio::Selection; #[test] fn a_meeting_follows_the_input_its_call_app_records_from() { let uids = |uids: &[&str]| uids.iter().map(|uid| (*uid).to_owned()).collect::>(); let current = |uid: &str| Some(uid.to_owned()); // The first input the app reports, until it reports a different one. assert_eq!(follow_call_input(None, uids(&["Jabra"])), current("Jabra")); assert_eq!( follow_call_input(current("Jabra"), uids(&["BuiltIn", "Jabra"])), current("Jabra") ); assert_eq!( follow_call_input(current("Jabra"), uids(&["BuiltIn"])), current("BuiltIn") ); // An app that reports none, as some do while muted, keeps its input. assert_eq!( follow_call_input(current("Jabra"), Vec::new()), current("Jabra") ); // With nothing reported yet, the configured microphone stays. assert_eq!(follow_call_input(None, Vec::new()), None); } #[test] fn a_microphone_that_would_not_open_is_tried_a_bounded_number_of_times() { let start = Instant::now(); let mut retry = UnopenableRetry::default(); let mut now = start; let mut delays = Vec::new(); retry.resolved(Selection::Unopenable, now); while let Some(due) = retry.due { assert!(!retry.is_due(due - Duration::from_millis(1))); assert!(retry.is_due(due)); delays.push(due - now); now = due; retry.resolved(Selection::Unopenable, now); } assert_eq!( delays, [1, 2, 4, 8, 16, 30, 30, 30].map(Duration::from_secs) ); // Opening it, or a microphone that is merely unlisted, stops looking. let mut retry = UnopenableRetry::default(); retry.resolved(Selection::Unopenable, start); retry.resolved(Selection::InUse, start); assert!(retry.due.is_none()); retry.resolved(Selection::Unlisted, start); assert!(retry.due.is_none()); } #[test] fn a_source_that_dies_right_after_each_open_keeps_backing_off() { let instantly = Duration::from_millis(20); // A source that really started reopens at the initial delay. assert_eq!(retry_delay(RETRY_MAX, STARTED_AFTER), RETRY_INITIAL); // One that failed instantly keeps the delay it already reached. assert_eq!(retry_delay(RETRY_MAX, instantly), RETRY_MAX); // So each instant failure waits longer, instead of spawning forever // twice a second. let mut delay = RETRY_INITIAL; let mut waits = Vec::new(); for _ in 0..4 { delay = retry_delay(delay, instantly); waits.push(delay); delay = delay.saturating_mul(2).min(RETRY_MAX); } assert_eq!(waits, [500, 1000, 2000, 4000].map(Duration::from_millis)); } #[test] fn a_process_that_stops_recording_between_reopens_is_a_release() { let now = Instant::now(); // The shared set from the old stream still names a process the new // stream no longer sees. let recorders = InputRecorders::default(); assert!(!recorders.update(Some("BuiltIn"), vec![41, 42], now)); assert!(recorders.update(Some("BuiltIn"), vec![42], now)); // A different input starts over rather than reporting every old pid. assert!(!recorders.update(Some("Jabra"), vec![7], now)); assert!(!recorders.update(Some("Jabra"), vec![7, 8], now)); // The same input still catches a release after the new baseline. assert!(recorders.update(Some("Jabra"), vec![8], now)); // An empty first baseline is not a release. let recorders = InputRecorders::default(); assert!(!recorders.update(Some("BuiltIn"), Vec::new(), now)); assert!(!recorders.update(Some("BuiltIn"), vec![1], now)); assert!(!released_processes(&[], &[1, 2])); assert!(!released_processes(&[1, 2], &[1, 2])); assert!(released_processes(&[1, 2], &[2])); } #[test] fn a_release_just_after_our_own_rate_change_is_not_another_apps() { let now = Instant::now(); let recorders = InputRecorders::default(); assert!(!recorders.update(Some("BuiltIn"), vec![41, 42], now)); recorders.note_renegotiation(now); // The driver restarting streams drops a pid; that is our change. assert!(!recorders.update(Some("BuiltIn"), vec![42], now)); // The baseline still moved, so a later release is seen. assert!(recorders.update(Some("BuiltIn"), Vec::new(), now + RENEGOTIATION_SETTLE)); } #[test] fn a_release_belongs_only_to_the_input_that_saw_it() { let now = Instant::now(); let recorders = InputRecorders::default(); assert!(!recorders.update(Some("BuiltIn"), vec![41], now)); assert!(recorders.same_input(Some("BuiltIn"))); assert!(!recorders.same_input(Some("Jabra"))); } #[test] fn only_an_input_the_call_app_reports_counts_as_held() { let reported = ["BuiltIn".to_owned()]; assert!(call_app_reports_input(Some("BuiltIn"), &reported)); assert!(!call_app_reports_input(Some("Jabra"), &reported)); // A manual recording, or an app macOS reports no inputs for. assert!(!call_app_reports_input(None, &reported)); assert!(!call_app_reports_input(Some("BuiltIn"), &[])); } #[test] fn audio_on_any_output_reaches_the_system_track() { // Only the headset plays (#1221): its audio is kept, less the hold. let mut speakers = Vec::new(); let mut headset = vec![0.25; 10]; let mixed = mix(&mut [&mut speakers, &mut headset], 4); assert_eq!(mixed, vec![0.25; 6]); assert_eq!(headset.len(), 4); // Both play: their audio adds up sample by sample, clamped. let mut speakers = vec![0.5; 8]; let mut headset = vec![0.75; 6]; let mixed = mix(&mut [&mut speakers, &mut headset], 2); assert_eq!(mixed, vec![1.0; 6]); assert_eq!((speakers.len(), headset.len()), (2, 0)); // Nothing plays: nothing is sent. let mut idle = Vec::new(); assert!(mix(&mut [&mut idle], 2).is_empty()); // The shared hold is 50 ms at 16 kHz, so a 1,000-sample buffer is // mixed only up to 200. assert_eq!(MIX_HOLD, 800); let mut left = vec![0.5; 1_000]; let mut right = Vec::new(); assert_eq!(mix(&mut [&mut left, &mut right], MIX_HOLD), vec![0.5; 200]); } }