Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(recording): track audio gap summary in output pipeline

Richie McIlroy13a8834fd58a
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index ef416e393..670243cb3 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -16,7 +16,7 @@ use std::{
ops::Deref,
path::{Path, PathBuf},
sync::{
- Arc,
+ Arc, OnceLock,
atomic::{self, AtomicBool, AtomicU64, Ordering},
},
time::{Duration, Instant},
@@ -667,6 +667,20 @@ fn audio_tail_padding_duration(audio_elapsed: Duration, target_elapsed: Duration
.min(MAX_AUDIO_TAIL_PADDING)
}
+/// A whole-frame overlap drop counts towards the stale-startup signature only while at most this
+/// many frames have been committed — the buffered burst that triggers startup drift lands in the
+/// first frames, before the steady-state capture clock settles.
+const STARTUP_OVERLAP_DROP_FRAME_LIMIT: u64 = 3;
+
+/// Overlap-trim accounting surfaced from the audio mux task at finish so the editor can
+/// compensate for stale-startup drift from typed metadata rather than the recording log.
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
+pub struct AudioGapSummary {
+ pub total_overlap_trimmed_ms: u32,
+ pub overlap_dropped_frames: u32,
+ pub startup_overlap_drops: u32,
+}
+
struct AudioGapTracker {
first_frame_ts: Option<Timestamp>,
first_frame_wall_clock: Option<Instant>,
@@ -677,6 +691,7 @@ struct AudioGapTracker {
last_silence_log: Option<Instant>,
overlap_event_count: u64,
overlap_dropped_frames: u64,
+ startup_overlap_drops: u64,
total_overlap_trimmed: Duration,
}
@@ -696,15 +711,28 @@ impl AudioGapTracker {
last_silence_log: None,
overlap_event_count: 0,
overlap_dropped_frames: 0,
+ startup_overlap_drops: 0,
total_overlap_trimmed: Duration::ZERO,
}
}
- fn record_overlap(&mut self, overlap: Duration, dropped_whole_frame: bool) {
+ fn record_overlap(&mut self, overlap: Duration, dropped_whole_frame: bool, frame_count: u64) {
self.overlap_event_count += 1;
self.total_overlap_trimmed = self.total_overlap_trimmed.saturating_add(overlap);
if dropped_whole_frame {
self.overlap_dropped_frames += 1;
+ if frame_count <= STARTUP_OVERLAP_DROP_FRAME_LIMIT {
+ self.startup_overlap_drops += 1;
+ }
+ }
+ }
+
+ fn gap_summary(&self) -> AudioGapSummary {
+ AudioGapSummary {
+ total_overlap_trimmed_ms: u32::try_from(self.total_overlap_trimmed.as_millis())
+ .unwrap_or(u32::MAX),
+ overlap_dropped_frames: u32::try_from(self.overlap_dropped_frames).unwrap_or(u32::MAX),
+ startup_overlap_drops: u32::try_from(self.startup_overlap_drops).unwrap_or(u32::MAX),
}
}
@@ -1555,6 +1583,8 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
video_start_gate.clone(),
);
+ let audio_gap_summary = Arc::new(OnceLock::new());
+
finish_build(
setup_ctx,
audio,
@@ -1568,6 +1598,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
true,
video_start_gate,
build_ctx.stop_signal,
+ audio_gap_summary.clone(),
)
.await?;
@@ -1581,6 +1612,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
cancel_token: build_ctx.stop_token,
video_frame_count,
health_rx: Some(build_ctx.health_rx),
+ audio_gap_summary,
})
}
}
@@ -1634,6 +1666,7 @@ impl OutputPipelineBuilder<NoVideo> {
.await?;
let shared_pause = SharedWallClockPause::new(build_ctx.pause_flag.clone());
+ let audio_gap_summary = Arc::new(OnceLock::new());
finish_build(
setup_ctx,
@@ -1648,6 +1681,7 @@ impl OutputPipelineBuilder<NoVideo> {
false,
None,
build_ctx.stop_signal,
+ audio_gap_summary.clone(),
)
.await?;
@@ -1661,6 +1695,7 @@ impl OutputPipelineBuilder<NoVideo> {
cancel_token: build_ctx.stop_token,
video_frame_count: Arc::new(AtomicU64::new(0)),
health_rx: Some(build_ctx.health_rx),
+ audio_gap_summary,
})
}
}
@@ -1716,6 +1751,7 @@ async fn finish_build(
has_video: bool,
video_start_gate: Option<VideoStartGate>,
stop_signal: PipelineStopSignal,
+ gap_summary_slot: Arc<OnceLock<AudioGapSummary>>,
) -> anyhow::Result<()> {
if let Some(audio) = audio {
audio.configure(
@@ -1727,6 +1763,7 @@ async fn finish_build(
shared_pause,
has_video,
video_start_gate,
+ gap_summary_slot,
);
}
@@ -2118,6 +2155,7 @@ impl PreparedAudioSources {
shared_pause: SharedWallClockPause,
has_video: bool,
video_start_gate: Option<VideoStartGate>,
+ gap_summary_slot: Arc<OnceLock<AudioGapSummary>>,
) {
let audio_info = self.audio_info;
let has_wireless_source = self.has_wireless_source;
@@ -2332,6 +2370,10 @@ impl PreparedAudioSources {
);
}
+ if gap_tracker.overlap_event_count > 0 {
+ let _ = gap_summary_slot.set(gap_tracker.gap_summary());
+ }
+
for source in &mut self.erased_audio_sources {
let _ = (source.stop_fn)(source.inner.as_mut()).await;
}
@@ -2449,7 +2491,9 @@ async fn process_audio_frame<TMutex: AudioMuxer>(
let frame_samples = frame.inner.samples();
if trim_samples >= frame_samples {
- state.gap_tracker.record_overlap(overlap_duration, true);
+ state
+ .gap_tracker
+ .record_overlap(overlap_duration, true, *state.frame_count);
debug!(
frame_count = *state.frame_count,
overlap_ms = overlap_duration.as_millis() as u64,
@@ -2462,7 +2506,9 @@ async fn process_audio_frame<TMutex: AudioMuxer>(
if trim_samples > 0 {
if let Some(trimmed) = trim_audio_frame_front(&frame.inner, trim_samples) {
- state.gap_tracker.record_overlap(overlap_duration, false);
+ state
+ .gap_tracker
+ .record_overlap(overlap_duration, false, *state.frame_count);
debug!(
frame_count = *state.frame_count,
overlap_ms = overlap_duration.as_millis() as u64,
@@ -2669,6 +2715,7 @@ pub struct OutputPipeline {
cancel_token: CancellationToken,
video_frame_count: Arc<AtomicU64>,
health_rx: Option<HealthReceiver>,
+ audio_gap_summary: Arc<OnceLock<AudioGapSummary>>,
}
pub struct FinishedOutputPipeline {
@@ -2676,6 +2723,7 @@ pub struct FinishedOutputPipeline {
pub first_timestamp: Timestamp,
pub video_info: Option<VideoInfo>,
pub video_frame_count: u64,
+ pub audio_gap_summary: Option<AudioGapSummary>,
}
#[derive(Clone, Default)]
@@ -2767,6 +2815,7 @@ impl OutputPipeline {
first_timestamp,
video_info: self.video_info,
video_frame_count: self.video_frame_count.load(Ordering::Acquire),
+ audio_gap_summary: self.audio_gap_summary.get().copied(),
})
}
@@ -3176,6 +3225,29 @@ mod tests {
assert_eq!(gap, MAX_SILENCE_INSERTION);
}
+
+ #[test]
+ fn gap_summary_counts_only_early_whole_frame_drops_as_startup() {
+ let mut tracker = AudioGapTracker::new(false, Timestamps::now());
+
+ // Whole-frame drops within the startup window carry the stale-burst signature.
+ tracker.record_overlap(Duration::from_millis(35), true, 0);
+ tracker.record_overlap(Duration::from_millis(35), true, 2);
+ tracker.record_overlap(
+ Duration::from_millis(35),
+ true,
+ STARTUP_OVERLAP_DROP_FRAME_LIMIT,
+ );
+ // A whole-frame drop past the startup window is a mid-recording overlap, not startup drift.
+ tracker.record_overlap(Duration::from_millis(35), true, 50);
+ // A partial trim keeps the frame, so it is never a startup drop.
+ tracker.record_overlap(Duration::from_millis(10), false, 1);
+
+ let summary = tracker.gap_summary();
+ assert_eq!(summary.startup_overlap_drops, 3);
+ assert_eq!(summary.overlap_dropped_frames, 4);
+ assert_eq!(summary.total_overlap_trimmed_ms, 35 * 4 + 10);
+ }
}
mod audio_tail_padding {