Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(recording): integrate master-clock drift handling in audio mixer
Richie McIlroy085771473882
diff --git a/crates/recording/src/sources/audio_mixer.rs b/crates/recording/src/sources/audio_mixer.rsindex f3666e10d..159587ba7 100644--- a/crates/recording/src/sources/audio_mixer.rs+++ b/crates/recording/src/sources/audio_mixer.rs@@ -1,7 +1,6 @@use cap_media_info::AudioInfo;-use cap_timestamp::{Timestamp, Timestamps};+use cap_timestamp::{MasterClock, SourceClockOutcome, SourceClockState, Timestamp, Timestamps};use futures::channel::{mpsc, oneshot};-#[cfg(not(any(target_os = "macos", windows)))]use std::time::Instant;use std::{collections::VecDeque,@@ -13,7 +12,7 @@ use std::{};use tracing::{debug, info, warn};-use crate::output_pipeline::AudioFrame;+use crate::output_pipeline::{AudioFrame, HealthSender, PipelineHealthEvent, emit_health};const DEFAULT_BUFFER_TIMEOUT: Duration = Duration::from_millis(100);const MIN_BUFFER_TIMEOUT_WIRED: Duration = Duration::from_millis(20);@@ -21,11 +20,13 @@ const MIN_BUFFER_TIMEOUT_WIRELESS: Duration = Duration::from_millis(90);const MAX_BUFFER_TIMEOUT: Duration = Duration::from_millis(250);const BUFFER_TIMEOUT_HEADROOM: f64 = 2.5;-// Wait TICK_MS for frames to arrive-// Assume all sources' frames for that tick have arrived after TICK_MS-// Insert silence where necessary for sources with no frames-//-// Current problem is generating an output timestamp that lines up with the input's timestamp+pub const MAX_BUFFERING_TICKS: u32 = 45;+pub const AUDIO_OUTPUT_FRAMES: u32 = cap_timestamp::AUDIO_OUTPUT_FRAMES as u32;+pub const DEFAULT_SAMPLE_RATE: u32 = cap_timestamp::DEFAULT_SAMPLE_RATE;+pub const MAX_BUFFERING_MS: u64 =+ (MAX_BUFFERING_TICKS as u64 * AUDIO_OUTPUT_FRAMES as u64 * 1000) / DEFAULT_SAMPLE_RATE as u64;+pub const FORCED_RESET_MS: u64 = MAX_BUFFERING_MS * 2;+const STALL_RECOVER_LOG_INTERVAL: Duration = Duration::from_secs(5);struct MixerSource {rx: mpsc::Receiver<AudioFrame>,@@ -34,11 +35,14 @@ struct MixerSource {buffer: VecDeque<AudioFrame>,buffer_last: Option<(Timestamp, Duration)>,last_input_timestamp: Option<Timestamp>,+ clock_state: SourceClockState,}pub struct AudioMixerBuilder {sources: Vec<MixerSource>,timestamps: Option<Timestamps>,+ master_clock: Option<Arc<MasterClock>>,+ health_tx: Option<HealthSender>,}impl Default for AudioMixerBuilder {@@ -52,6 +56,8 @@ impl AudioMixerBuilder {Self {sources: Vec::new(),timestamps: None,+ master_clock: None,+ health_tx: None,}}@@ -60,12 +66,23 @@ impl AudioMixerBuilder {self}+ pub fn with_master_clock(mut self, master_clock: Arc<MasterClock>) -> Self {+ self.master_clock = Some(master_clock);+ self+ }++ pub fn with_health_tx(mut self, health_tx: HealthSender) -> Self {+ self.health_tx = Some(health_tx);+ self+ }+pub fn has_sources(&self) -> bool {!self.sources.is_empty()}pub fn add_source(&mut self, info: AudioInfo, rx: mpsc::Receiver<AudioFrame>) {let buffer_timeout = buffer_timeout_for(&info);+ let clock_label = mixer_source_label(self.sources.len());self.sources.push(MixerSource {info,@@ -74,82 +91,13 @@ impl AudioMixerBuilder {buffer: VecDeque::new(),buffer_last: None,last_input_timestamp: None,+ clock_state: SourceClockState::new(clock_label),});}pub fn build(self, output: mpsc::Sender<AudioFrame>) -> Result<AudioMixer, ffmpeg::Error> {- let mut filter_graph = ffmpeg::filter::Graph::new();-- let mut abuffers = Vec::new();- let mut resamplers = Vec::new();-- let target_info = AudioMixer::INFO;- let target_rate = target_info.rate();- let target_sample_fmt = target_info.sample_format.name();- let target_channel_layout_bits = target_info.channel_layout().bits();-- for (i, source) in self.sources.iter().enumerate() {- let info = &source.info;- let args = format!(- "time_base={}:sample_rate={}:sample_fmt={}:channel_layout=0x{:x}",- info.time_base,- info.rate(),- info.sample_format.name(),- info.channel_layout().bits()- );-- debug!("audio mixer input {i}: {args}");-- let mut abuffer = filter_graph.add(- &ffmpeg::filter::find("abuffer").expect("Failed to find abuffer filter"),- &format!("src{i}"),- &args,- )?;-- let mut resample = filter_graph.add(- &ffmpeg::filter::find("aresample").expect("Failed to find aresample filter"),- &format!("resample{i}"),- &format!(- "out_sample_rate={target_rate}:out_sample_fmt={target_sample_fmt}:out_chlayout=0x{target_channel_layout_bits:x}"- ),- )?;-- abuffer.link(0, &mut resample, 0);-- abuffers.push(abuffer);- resamplers.push(resample);- }-- let mut amix = filter_graph.add(- &ffmpeg::filter::find("amix").expect("Failed to find amix filter"),- "amix",- &format!("inputs={}:duration=longest", abuffers.len()),- )?;-- let aformat_args = format!(- "sample_fmts={target_sample_fmt}:sample_rates={target_rate}:channel_layouts=0x{target_channel_layout_bits:x}"- );-- let mut aformat = filter_graph.add(- &ffmpeg::filter::find("aformat").expect("Failed to find aformat filter"),- "aformat",- &aformat_args,- )?;-- let mut abuffersink = filter_graph.add(- &ffmpeg::filter::find("abuffersink").expect("Failed to find abuffersink filter"),- "sink",- "",- )?;-- for (i, resample) in resamplers.iter_mut().enumerate() {- resample.link(0, &mut amix, i as u32);- }-- amix.link(0, &mut aformat, 0);- aformat.link(0, &mut abuffersink, 0);-- filter_graph.validate()?;+ let infos: Vec<AudioInfo> = self.sources.iter().map(|s| s.info).collect();+ let graph = construct_filter_graph(&infos)?;let max_buffer_timeout = self.sources@@ -158,22 +106,29 @@ impl AudioMixerBuilder {.max().unwrap_or(DEFAULT_BUFFER_TIMEOUT);+ let timestamps = self.timestamps.unwrap_or_else(Timestamps::now);+ let master_clock = self+ .master_clock+ .unwrap_or_else(|| MasterClock::new(timestamps, AudioMixer::INFO.rate() as u32));+Ok(AudioMixer {sources: self.sources,samples_out: 0,last_tick: None,- abuffers,- abuffersink,+ abuffers: graph.abuffers,+ abuffersink: graph.abuffersink,output,- _resamplers: resamplers,- _filter_graph: filter_graph,- _amix: amix,- _aformat: aformat,+ _resamplers: graph.resamplers,+ _filter_graph: graph.filter_graph,+ _amix: graph.amix,+ _aformat: graph.aformat,start_timestamp: None,- timestamps: self.timestamps.unwrap_or_else(Timestamps::now),+ timestamps,max_buffer_timeout,wall_clock_start: None,- baseline_offset_secs: None,+ master_clock,+ buffering: MixerBufferingTracker::new(),+ health_tx: self.health_tx,})}@@ -235,7 +190,150 @@ pub struct AudioMixer {start_timestamp: Option<Timestamp>,max_buffer_timeout: Duration,wall_clock_start: Option<Timestamp>,- baseline_offset_secs: Option<f64>,+ master_clock: Arc<MasterClock>,+ buffering: MixerBufferingTracker,+ health_tx: Option<HealthSender>,+}++#[derive(Debug, Clone)]+pub enum MixerBufferingAction {+ ForceReset {+ source: &'static str,+ starvation_ms: u64,+ },+}++#[derive(Debug)]+pub struct MixerBufferingTracker {+ stall_started_at: Option<Instant>,+ last_stall_emit_at: Option<Instant>,+ emitted_for_current_stall: bool,+ forced_reset_fired_for_current_stall: bool,+ total_stall_events: u64,+ total_ignored_tick_events: u64,+ laggard_source: Option<&'static str>,+}++impl MixerBufferingTracker {+ pub fn new() -> Self {+ Self {+ stall_started_at: None,+ last_stall_emit_at: None,+ emitted_for_current_stall: false,+ forced_reset_fired_for_current_stall: false,+ total_stall_events: 0,+ total_ignored_tick_events: 0,+ laggard_source: None,+ }+ }++ pub fn observe(+ &mut self,+ now: Instant,+ laggard: Option<&'static str>,+ health_tx: Option<&HealthSender>,+ ) -> Option<MixerBufferingAction> {+ match laggard {+ Some(source) => {+ if self.stall_started_at.is_none() {+ self.stall_started_at = Some(now);+ self.emitted_for_current_stall = false;+ self.forced_reset_fired_for_current_stall = false;+ }+ self.laggard_source = Some(source);++ let waited = now+ .checked_duration_since(self.stall_started_at.unwrap_or(now))+ .unwrap_or_default();++ if !self.emitted_for_current_stall+ && waited >= Duration::from_millis(MAX_BUFFERING_MS)+ {+ self.emitted_for_current_stall = true;+ self.total_stall_events += 1;+ self.last_stall_emit_at = Some(now);+ warn!(+ source = source,+ waited_ms = waited.as_millis() as u64,+ total_stalls = self.total_stall_events,+ "AudioMixer: laggard source exceeded max buffering window, stalling mixer"+ );+ if let Some(tx) = health_tx {+ emit_health(+ tx,+ PipelineHealthEvent::Stalled {+ source: format!("mixer:{source}"),+ waited_ms: waited.as_millis() as u64,+ },+ );+ }+ }++ if !self.forced_reset_fired_for_current_stall+ && waited >= Duration::from_millis(FORCED_RESET_MS)+ {+ self.forced_reset_fired_for_current_stall = true;+ return Some(MixerBufferingAction::ForceReset {+ source,+ starvation_ms: waited.as_millis() as u64,+ });+ }+ None+ }+ None => {+ if let (Some(started), Some(source)) =+ (self.stall_started_at.take(), self.laggard_source.take())+ {+ let waited = now.checked_duration_since(started).unwrap_or_default();+ if self.emitted_for_current_stall {+ info!(+ source = source,+ waited_ms = waited.as_millis() as u64,+ "AudioMixer: laggard source caught up"+ );+ } else {+ let should_log = self+ .last_stall_emit_at+ .map(|t| now.duration_since(t) > STALL_RECOVER_LOG_INTERVAL)+ .unwrap_or(true);+ if should_log {+ debug!(+ source = source,+ waited_ms = waited.as_millis() as u64,+ "AudioMixer: brief source buffering below threshold"+ );+ }+ }+ self.emitted_for_current_stall = false;+ self.forced_reset_fired_for_current_stall = false;+ }+ None+ }+ }+ }++ pub fn record_forced_ignore(&mut self, source: &'static str) {+ self.total_ignored_tick_events += 1;+ warn!(+ source = source,+ total_ignored = self.total_ignored_tick_events,+ "AudioMixer: laggard source exceeded hard ceiling, forcibly inserting silence"+ );+ }++ pub fn total_stall_events(&self) -> u64 {+ self.total_stall_events+ }++ pub fn total_ignored_tick_events(&self) -> u64 {+ self.total_ignored_tick_events+ }+}++impl Default for MixerBufferingTracker {+ fn default() -> Self {+ Self::new()+ }}impl AudioMixer {@@ -245,95 +343,190 @@ impl AudioMixer {2,);- fn calculate_drift_corrected_timestamp(- &mut self,- start_timestamp: Timestamp,- wall_clock_elapsed: Duration,- ) -> Timestamp {- let mut latest_input_timestamp_secs: Option<f64> = None;+ fn detect_laggard_source(&self, now: Timestamp) -> Option<&'static str> {+ let start_timestamp = self.start_timestamp?;+ let elapsed = now+ .duration_since(self.timestamps)+ .checked_sub(start_timestamp.duration_since(self.timestamps))?;+ if elapsed < self.max_buffer_timeout {+ return None;+ }+for source in &self.sources {- if let Some(ts) = source.last_input_timestamp {- let ts_secs = ts.signed_duration_since_secs(self.timestamps);- match latest_input_timestamp_secs {- Some(current) if ts_secs > current => {- latest_input_timestamp_secs = Some(ts_secs);- }- None => {- latest_input_timestamp_secs = Some(ts_secs);- }- _ => {}+ if source.buffer_last.is_none() && source.last_input_timestamp.is_none() {+ return Some(source.clock_state.name());+ }++ if let Some((buffer_last_ts, buffer_last_dur)) = source.buffer_last {+ let last_elapsed = buffer_last_ts.duration_since(self.timestamps);+ let start_elapsed = start_timestamp.duration_since(self.timestamps);+ let from_start = last_elapsed.saturating_sub(start_elapsed);+ let projected = from_start.saturating_add(buffer_last_dur);+ if let Some(gap) = elapsed.checked_sub(projected)+ && gap > source.buffer_timeout.saturating_mul(2)+ {+ return Some(source.clock_state.name());}}}+ None+ }- let start_secs = start_timestamp.signed_duration_since_secs(self.timestamps);- let wall_clock_secs = wall_clock_elapsed.as_secs_f64();-- let Some(latest_secs) = latest_input_timestamp_secs else {- return start_timestamp;- };-- let input_elapsed_secs = latest_secs - start_secs;+ fn handle_source_format_change(+ &mut self,+ index: usize,+ old_info: AudioInfo,+ new_info: AudioInfo,+ ) {+ let label = self+ .sources+ .get(index)+ .map(|s| s.clock_state.name())+ .unwrap_or("<missing>");++ warn!(+ source = label,+ index = index,+ old_rate = old_info.rate(),+ old_channels = old_info.channels,+ old_fmt = %old_info.sample_format.name(),+ new_rate = new_info.rate(),+ new_channels = new_info.channels,+ new_fmt = %new_info.sample_format.name(),+ "AudioMixer: source audio format changed mid-recording; rebuilding filter graph"+ );- if input_elapsed_secs < 0.0 {- return start_timestamp;+ let infos: Vec<AudioInfo> = self.sources.iter().map(|s| s.info).collect();+ match construct_filter_graph(&infos) {+ Ok(graph) => {+ self.abuffers = graph.abuffers;+ self._resamplers = graph.resamplers;+ self._amix = graph.amix;+ self._aformat = graph.aformat;+ self.abuffersink = graph.abuffersink;+ self._filter_graph = graph.filter_graph;+ self.max_buffer_timeout = self+ .sources+ .iter()+ .map(|s| s.buffer_timeout)+ .max()+ .unwrap_or(DEFAULT_BUFFER_TIMEOUT);++ if let Some(tx) = &self.health_tx {+ emit_health(+ tx,+ PipelineHealthEvent::SourceAudioReset {+ source: label.to_string(),+ starvation_ms: 0,+ },+ );+ }+ }+ Err(e) => {+ tracing::error!(+ source = label,+ error = %e,+ "AudioMixer: failed to rebuild filter graph after source format change"+ );+ }}+ }- if input_elapsed_secs < 2.0 || wall_clock_secs < 2.0 {- return start_timestamp + Duration::from_secs_f64(input_elapsed_secs.max(0.0));+ fn force_reset_source(&mut self, source_label: &'static str, starvation_ms: u64) {+ let mut reset_any = false;+ for source in &mut self.sources {+ if source.clock_state.name() == source_label {+ source.clock_state.reset();+ source.buffer.clear();+ source.buffer_last = None;+ source.last_input_timestamp = None;+ reset_any = true;+ break;+ }}- if self.baseline_offset_secs.is_none() {- let offset = input_elapsed_secs - wall_clock_secs;- debug!(- wall_clock_secs,- input_elapsed_secs,- baseline_offset_secs = offset,- "AudioMixer: Capturing baseline offset after warmup"- );- self.baseline_offset_secs = Some(offset);+ if reset_any {+ self.buffering.record_forced_ignore(source_label);+ if let Some(tx) = &self.health_tx {+ emit_health(+ tx,+ PipelineHealthEvent::SourceAudioReset {+ source: source_label.to_string(),+ starvation_ms,+ },+ );+ }}+ }- let baseline = self.baseline_offset_secs.unwrap_or(0.0);- let adjusted_input_elapsed = input_elapsed_secs - baseline;-- let drift_ratio = if adjusted_input_elapsed > 0.0 {- wall_clock_secs / adjusted_input_elapsed+ fn sample_based_output_timestamp(&self, start_timestamp: Timestamp) -> Timestamp {+ let rate = self.master_clock.sample_rate().max(1) as u64;+ let nanos = (self.samples_out as u128 * 1_000_000_000u128) / rate as u128;+ let clamped = if nanos > u64::MAX as u128 {+ u64::MAX} else {- 1.0+ nanos as u64};-- if !(0.90..=1.10).contains(&drift_ratio) {- warn!(- drift_ratio,- wall_clock_secs,- adjusted_input_elapsed,- baseline,- "AudioMixer: Significant clock drift detected"- );- let clamped_ratio = drift_ratio.clamp(0.95, 1.05);- let corrected_secs = adjusted_input_elapsed * clamped_ratio;- return start_timestamp + Duration::from_secs_f64(corrected_secs.max(0.0));- }-- let corrected_secs = adjusted_input_elapsed * drift_ratio;- start_timestamp + Duration::from_secs_f64(corrected_secs.max(0.0))+ start_timestamp + Duration::from_nanos(clamped)}fn buffer_sources(&mut self, now: Timestamp) {- for source in &mut self.sources {+ let mut format_change: Option<(usize, AudioInfo, AudioInfo)> = None;+ let clock_anchor = self.master_clock.timestamps().instant();+ for (index, source) in self.sources.iter_mut().enumerate() {let rate = source.info.rate();let buffer_timeout = source.buffer_timeout;- // Do not inject silence based on wall-clock pacing. We only bridge actual gaps- // when a new frame arrives (below), to keep emission data-driven.-while let Ok(Some(AudioFrame {inner: frame,- timestamp,+ timestamp: raw_timestamp,})) = source.rx.try_next(){- source.last_input_timestamp = Some(timestamp);+ source.last_input_timestamp = Some(raw_timestamp);++ if format_change.is_none()+ && let Some(new_info) = detect_format_mismatch(&source.info, &frame)+ {+ let old_info = source.info;+ format_change = Some((index, old_info, new_info));+ source.info = new_info;+ source.buffer_timeout = buffer_timeout_for(&new_info);+ source.buffer.clear();+ source.buffer_last = None;+ source.last_input_timestamp = Some(raw_timestamp);+ source+ .buffer+ .push_back(AudioFrame::new(frame, raw_timestamp));+ break;+ }++ let frame_samples = frame.samples() as u64;+ let frame_duration_ns = if rate > 0 {+ (frame_samples as u128 * 1_000_000_000u128 / rate as u128).min(u64::MAX as u128)+ as u64+ } else {+ 0+ };++ let remap =+ source+ .clock_state+ .remap(&self.master_clock, raw_timestamp, frame_duration_ns);++ if matches!(remap.outcome, SourceClockOutcome::HardReset) {+ let flushed = source.buffer.len();+ warn!(+ source = source.clock_state.name(),+ hard_resets = source.clock_state.hard_reset_count(),+ flushed_frames = flushed,+ "AudioMixer: source clock hard-reset (>2s jump), flushing queued frames"+ );+ source.buffer_last = None;+ source.buffer.clear();+ source.last_input_timestamp = None;+ }++ let timestamp = Timestamp::Instant(clock_anchor + remap.duration());if let Some((buffer_last_timestamp, buffer_last_duration)) = source.buffer_last {let timestamp_elapsed = timestamp.duration_since(self.timestamps);@@ -381,6 +574,10 @@ impl AudioMixer {}}+ if let Some((index, old_info, new_info)) = format_change {+ self.handle_source_format_change(index, old_info, new_info);+ }+if self.start_timestamp.is_none() {self.start_timestamp = self.sources@@ -449,13 +646,19 @@ impl AudioMixer {return Ok(());};- let Some(wall_clock_start) = self.wall_clock_start else {- return Ok(());- };+ let wall_now = Instant::now();+ let laggard = self.detect_laggard_source(now);+ let action = self+ .buffering+ .observe(wall_now, laggard, self.health_tx.as_ref());- let wall_clock_elapsed = now- .duration_since(self.timestamps)- .saturating_sub(wall_clock_start.duration_since(self.timestamps));+ if let Some(MixerBufferingAction::ForceReset {+ source,+ starvation_ms,+ }) = action+ {+ self.force_reset_source(source, starvation_ms);+ }for (i, source) in self.sources.iter_mut().enumerate() {for buffer in source.buffer.drain(..) {@@ -469,8 +672,7 @@ impl AudioMixer {filtered.set_rate(output_rate_i32 as u32);- let output_timestamp =- self.calculate_drift_corrected_timestamp(start_timestamp, wall_clock_elapsed);+ let output_timestamp = self.sample_based_output_timestamp(start_timestamp);let frame_samples = filtered.samples();let mut frame = AudioFrame::new(filtered, output_timestamp);@@ -500,6 +702,136 @@ impl AudioMixer {}}+fn detect_format_mismatch(current: &AudioInfo, frame: &ffmpeg::frame::Audio) -> Option<AudioInfo> {+ let frame_rate: u32 = frame.rate();+ let frame_channels: usize = frame.channels() as usize;+ let frame_fmt = frame.format();++ if frame_rate == 0 || frame_channels == 0 {+ return None;+ }++ let rate_changed = frame_rate != current.sample_rate;+ let channels_changed = frame_channels != current.channels;+ let fmt_changed = frame_fmt != current.sample_format;++ if !rate_changed && !channels_changed && !fmt_changed {+ return None;+ }++ let mut updated = *current;+ updated.sample_rate = frame_rate;+ updated.channels = frame_channels;+ updated.sample_format = frame_fmt;+ Some(updated)+}++struct FilterGraphParts {+ filter_graph: ffmpeg::filter::Graph,+ abuffers: Vec<ffmpeg::filter::Context>,+ resamplers: Vec<ffmpeg::filter::Context>,+ amix: ffmpeg::filter::Context,+ aformat: ffmpeg::filter::Context,+ abuffersink: ffmpeg::filter::Context,+}++fn construct_filter_graph(infos: &[AudioInfo]) -> Result<FilterGraphParts, ffmpeg::Error> {+ let mut filter_graph = ffmpeg::filter::Graph::new();++ let mut abuffers = Vec::new();+ let mut resamplers = Vec::new();++ let target_info = AudioMixer::INFO;+ let target_rate = target_info.rate();+ let target_sample_fmt = target_info.sample_format.name();+ let target_channel_layout_bits = target_info.channel_layout().bits();++ for (i, info) in infos.iter().enumerate() {+ let args = format!(+ "time_base={}:sample_rate={}:sample_fmt={}:channel_layout=0x{:x}",+ info.time_base,+ info.rate(),+ info.sample_format.name(),+ info.channel_layout().bits()+ );++ debug!("audio mixer input {i}: {args}");++ let mut abuffer = filter_graph.add(+ &ffmpeg::filter::find("abuffer").expect("Failed to find abuffer filter"),+ &format!("src{i}"),+ &args,+ )?;++ let mut resample = filter_graph.add(+ &ffmpeg::filter::find("aresample").expect("Failed to find aresample filter"),+ &format!("resample{i}"),+ &format!(+ "out_sample_rate={target_rate}:out_sample_fmt={target_sample_fmt}:out_chlayout=0x{target_channel_layout_bits:x}"+ ),+ )?;++ abuffer.link(0, &mut resample, 0);++ abuffers.push(abuffer);+ resamplers.push(resample);+ }++ let mut amix = filter_graph.add(+ &ffmpeg::filter::find("amix").expect("Failed to find amix filter"),+ "amix",+ &format!("inputs={}:duration=longest", abuffers.len()),+ )?;++ let aformat_args = format!(+ "sample_fmts={target_sample_fmt}:sample_rates={target_rate}:channel_layouts=0x{target_channel_layout_bits:x}"+ );++ let mut aformat = filter_graph.add(+ &ffmpeg::filter::find("aformat").expect("Failed to find aformat filter"),+ "aformat",+ &aformat_args,+ )?;++ let mut abuffersink = filter_graph.add(+ &ffmpeg::filter::find("abuffersink").expect("Failed to find abuffersink filter"),+ "sink",+ "",+ )?;++ for (i, resample) in resamplers.iter_mut().enumerate() {+ resample.link(0, &mut amix, i as u32);+ }++ amix.link(0, &mut aformat, 0);+ aformat.link(0, &mut abuffersink, 0);++ filter_graph.validate()?;++ Ok(FilterGraphParts {+ filter_graph,+ abuffers,+ resamplers,+ amix,+ aformat,+ abuffersink,+ })+}++fn mixer_source_label(index: usize) -> &'static str {+ const LABELS: [&str; 8] = [+ "mixer-src-0",+ "mixer-src-1",+ "mixer-src-2",+ "mixer-src-3",+ "mixer-src-4",+ "mixer-src-5",+ "mixer-src-6",+ "mixer-src-7",+ ];+ LABELS.get(index).copied().unwrap_or("mixer-src-overflow")+}+fn buffer_timeout_for(info: &AudioInfo) -> Duration {if info.sample_rate == 0 || info.buffer_size == 0 {return DEFAULT_BUFFER_TIMEOUT;@@ -608,6 +940,166 @@ mod test {assert_eq!(samples[4], samples[5]);}+ mod buffering_tracker {+ use super::*;+ use crate::output_pipeline::{HealthReceiver, HealthSender};++ fn new_health_pair() -> (HealthSender, HealthReceiver) {+ tokio::sync::mpsc::channel(32)+ }++ #[test]+ fn no_emit_when_no_laggard() {+ let mut tracker = MixerBufferingTracker::new();+ let (tx, mut rx) = new_health_pair();+ let t0 = Instant::now();+ tracker.observe(t0, None, Some(&tx));+ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 100),+ None,+ Some(&tx),+ );+ assert_eq!(tracker.total_stall_events(), 0);+ assert!(rx.try_recv().is_err());+ }++ #[test]+ fn no_emit_for_brief_stall_under_budget() {+ let mut tracker = MixerBufferingTracker::new();+ let (tx, mut rx) = new_health_pair();+ let t0 = Instant::now();+ tracker.observe(t0, Some("mic"), Some(&tx));+ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS / 2),+ Some("mic"),+ Some(&tx),+ );+ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS / 2 + 10),+ None,+ Some(&tx),+ );+ assert_eq!(tracker.total_stall_events(), 0);+ assert!(rx.try_recv().is_err());+ }++ #[test]+ fn emits_once_per_sustained_stall_and_recovers() {+ let mut tracker = MixerBufferingTracker::new();+ let (tx, mut rx) = new_health_pair();+ let t0 = Instant::now();+ tracker.observe(t0, Some("mic"), Some(&tx));+ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 10),+ Some("mic"),+ Some(&tx),+ );+ let first = rx.try_recv().expect("first stall must emit");+ match first {+ PipelineHealthEvent::Stalled { source, waited_ms } => {+ assert_eq!(source, "mixer:mic");+ assert!(waited_ms >= MAX_BUFFERING_MS);+ }+ other => panic!("unexpected event {other:?}"),+ }++ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 200),+ Some("mic"),+ Some(&tx),+ );+ assert!(+ rx.try_recv().is_err(),+ "only one emit per sustained-stall window"+ );+ assert_eq!(tracker.total_stall_events(), 1);++ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 300),+ None,+ Some(&tx),+ );+ assert!(rx.try_recv().is_err());++ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 400),+ Some("mic"),+ Some(&tx),+ );+ tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS * 2 + 500),+ Some("mic"),+ Some(&tx),+ );+ let second = rx+ .try_recv()+ .expect("second sustained stall must emit again");+ match second {+ PipelineHealthEvent::Stalled { source, .. } => {+ assert_eq!(source, "mixer:mic")+ }+ other => panic!("unexpected event {other:?}"),+ }+ assert_eq!(tracker.total_stall_events(), 2);+ }++ const _MAX_BUFFERING_WINDOW_MATCHES_OBS_DEFAULT: () = {+ assert!(MAX_BUFFERING_TICKS == 45);+ assert!(+ MAX_BUFFERING_MS+ == (MAX_BUFFERING_TICKS as u64 * AUDIO_OUTPUT_FRAMES as u64 * 1000)+ / DEFAULT_SAMPLE_RATE as u64+ );+ assert!(+ MAX_BUFFERING_MS == 960,+ "MAX_BUFFERING_MS must equal OBS default of 960ms (45 * 1024 / 48)"+ );+ assert!(FORCED_RESET_MS == MAX_BUFFERING_MS * 2);+ };++ #[test]+ fn force_reset_action_emitted_after_forced_reset_threshold() {+ let mut tracker = MixerBufferingTracker::new();+ let (tx, mut rx) = new_health_pair();+ let t0 = Instant::now();+ tracker.observe(t0, Some("mic"), Some(&tx));++ let action_stall = tracker.observe(+ t0 + Duration::from_millis(MAX_BUFFERING_MS + 10),+ Some("mic"),+ Some(&tx),+ );+ assert!(action_stall.is_none());+ rx.try_recv().expect("stall event emitted");++ let action = tracker.observe(+ t0 + Duration::from_millis(FORCED_RESET_MS + 10),+ Some("mic"),+ Some(&tx),+ );+ match action {+ Some(MixerBufferingAction::ForceReset {+ source,+ starvation_ms,+ }) => {+ assert_eq!(source, "mic");+ assert!(starvation_ms >= FORCED_RESET_MS);+ }+ other => panic!("expected ForceReset, got {other:?}"),+ }++ let action_followup = tracker.observe(+ t0 + Duration::from_millis(FORCED_RESET_MS + 100),+ Some("mic"),+ Some(&tx),+ );+ assert!(+ action_followup.is_none(),+ "force reset must only fire once per sustained stall"+ );+ }+ }+mod source_buffer {use super::*;@@ -749,5 +1241,50 @@ mod test {assert_eq!(item.timestamp.duration_since(start), ONE_SECOND);assert_eq!(item.inner.samples(), SOURCE_INFO.rate() as usize / 2);}++ #[tokio::test]+ async fn hard_reset_flushes_queued_frames_obs_parity() {+ let (output_tx, _) = mpsc::channel(4);+ let mut builder = AudioMixerBuilder::new();++ let (mut tx, rx) = mpsc::channel(16);+ builder.add_source(SOURCE_INFO, rx);++ let mut mixer = builder.build(output_tx).unwrap();+ let start = mixer.timestamps;++ for i in 0..4 {+ tx.send(AudioFrame::new(+ SOURCE_INFO.wrap_frame(&vec![0; SAMPLES_SECOND / 4]),+ Timestamp::Instant(start.instant() + ONE_SECOND / 4 * i),+ ))+ .await+ .unwrap();+ }+ mixer.buffer_sources(Timestamp::Instant(start.instant() + ONE_SECOND));+ let initial_len = mixer.sources[0].buffer.len();+ assert!(+ initial_len >= 4,+ "expected pre-reset queue to hold ≥4 frames, got {initial_len}"+ );++ let far_future =+ Timestamp::Instant(start.instant() + Duration::from_secs(5) + ONE_SECOND);+ tx.send(AudioFrame::new(+ SOURCE_INFO.wrap_frame(&vec![0; SAMPLES_SECOND / 4]),+ far_future,+ ))+ .await+ .unwrap();+ mixer.buffer_sources(far_future);++ let post_reset_len = mixer.sources[0].buffer.len();+ assert_eq!(+ post_reset_len, 1,+ "after hard-reset mixer buffer must be flushed \+ (OBS reset_audio_data parity); post-reset contained {post_reset_len} frames"+ );+ assert!(mixer.sources[0].clock_state.hard_reset_count() >= 1);+ }}}