Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): flag sustained exact-zero microphone audio during calls

ManthanNimodiya246e35846b5b
diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs
index 51c4bfa52..81cc72e22 100644
--- a/apps/desktop-gpui/src/editor_clips.rs
+++ b/apps/desktop-gpui/src/editor_clips.rs
@@ -1932,6 +1932,7 @@ impl PreparedMp4Import {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
Ok(Self {
target,
diff --git a/apps/desktop-gpui/src/editor_preparing/audio.rs b/apps/desktop-gpui/src/editor_preparing/audio.rs
index bb5206e20..1b58380b9 100644
--- a/apps/desktop-gpui/src/editor_preparing/audio.rs
+++ b/apps/desktop-gpui/src/editor_preparing/audio.rs
@@ -338,6 +338,7 @@ mod tests {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
for extension in ["m4a", "ogg"] {
let relative = PathBuf::from(format!("segment-0/audio-input.{extension}"));
diff --git a/apps/desktop-gpui/src/import.rs b/apps/desktop-gpui/src/import.rs
index 30f70292c..58b8a4438 100644
--- a/apps/desktop-gpui/src/import.rs
+++ b/apps/desktop-gpui/src/import.rs
@@ -134,6 +134,7 @@ fn imported_video_meta(
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}),
cursor: None,
keyboard: None,
diff --git a/apps/desktop/src-tauri/src/editor_preparing/audio.rs b/apps/desktop/src-tauri/src/editor_preparing/audio.rs
index 0a58e3d73..a7e106ada 100644
--- a/apps/desktop/src-tauri/src/editor_preparing/audio.rs
+++ b/apps/desktop/src-tauri/src/editor_preparing/audio.rs
@@ -199,6 +199,7 @@ mod tests {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
for extension in ["m4a", "ogg"] {
let relative = PathBuf::from(format!("segment-0/audio-input.{extension}"));
diff --git a/apps/desktop/src-tauri/src/import.rs b/apps/desktop/src-tauri/src/import.rs
index cebe7d9c0..3b517fcd5 100644
--- a/apps/desktop/src-tauri/src/import.rs
+++ b/apps/desktop/src-tauri/src/import.rs
@@ -1517,6 +1517,7 @@ pub async fn start_video_import(app: AppHandle, source_path: PathBuf) -> Result<
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
@@ -1702,6 +1703,7 @@ async fn append_mp4_to_editor_project(
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index 757c65f87..55c0bd2f8 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -3422,6 +3422,10 @@ async fn start_recording_prepared(
cap_recording::PipelineHealthEvent::CaptureTargetLost { target } => {
Some(format!("Capture target lost: {target}"))
}
+ cap_recording::PipelineHealthEvent::MicrophoneDigitalSilence => Some(
+ "Microphone is recording silence. Another app, like a call, may be using it."
+ .to_string(),
+ ),
cap_recording::PipelineHealthEvent::SourceRestarted => None,
};
diff --git a/apps/desktop/src-tauri/src/recording_telemetry.rs b/apps/desktop/src-tauri/src/recording_telemetry.rs
index b13bcccf8..5d3238f87 100644
--- a/apps/desktop/src-tauri/src/recording_telemetry.rs
+++ b/apps/desktop/src-tauri/src/recording_telemetry.rs
@@ -139,7 +139,8 @@ impl RecordingHealthAccumulator {
| PipelineHealthEvent::EncoderRebuilt { .. }
| PipelineHealthEvent::SourceAudioReset { .. }
| PipelineHealthEvent::RecoveryFragmentCorrupt { .. }
- | PipelineHealthEvent::CaptureTargetLost { .. } => None,
+ | PipelineHealthEvent::CaptureTargetLost { .. }
+ | PipelineHealthEvent::MicrophoneDigitalSilence => None,
}
}
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index 767b14c17..65e5b101a 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -710,7 +710,12 @@ overlap_dropped_frames: number;
startup_overlap_drops: number }
export type AudioInputLevelChange = number
export type AudioLibraryTrack = { id: string; name: string; category: string }
-export type AudioMeta = { path: string; start_time?: number | null; device_id?: string | null; gap_summary?: AudioGapSummary | null }
+export type AudioMeta = { path: string; start_time?: number | null; device_id?: string | null; gap_summary?: AudioGapSummary | null;
+/**
+ * Milliseconds the unmuted microphone delivered sustained bit-exact silence,
+ * e.g. while a VoIP call held the input on macOS.
+ */
+digital_silence_ms?: number | null }
/**
* A timeline-positioned audio clip (background music or imported audio).
*
diff --git a/crates/editor/src/completed_audio.rs b/crates/editor/src/completed_audio.rs
index 357581e94..c2e8c7886 100644
--- a/crates/editor/src/completed_audio.rs
+++ b/crates/editor/src/completed_audio.rs
@@ -187,6 +187,7 @@ pub(crate) mod tests {
start_time: Some(0.125),
device_id: Some("mic".into()),
gap_summary: None,
+ digital_silence_ms: None,
}),
system_audio: Some(AudioMeta {
path: format!("content/segments/segment-{index}/system_audio.ogg")
@@ -194,6 +195,7 @@ pub(crate) mod tests {
start_time: Some(-0.25),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}),
cursor: None,
keyboard: None,
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs
index 86b5514ed..27c1981d9 100644
--- a/crates/export/src/lib.rs
+++ b/crates/export/src/lib.rs
@@ -799,6 +799,7 @@ mod clip_offset_tests {
start_time: Some(start),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
};
StudioRecordingMeta::MultipleSegments {
inner: MultipleSegments {
diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs
index 08b5a4094..a276cbd2f 100644
--- a/crates/project/src/meta.rs
+++ b/crates/project/src/meta.rs
@@ -55,6 +55,10 @@ pub struct AudioMeta {
pub device_id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub gap_summary: Option<AudioGapSummary>,
+ /// Milliseconds the unmuted microphone delivered sustained bit-exact silence,
+ /// e.g. while a VoIP call held the input on macOS.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub digital_silence_ms: Option<u32>,
}
/// Overlap-trim accounting captured by the recorder's audio gap tracker, persisted so the
@@ -1142,6 +1146,7 @@ mod test {
start_time,
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
}
}
diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs
index 5efa225f5..a3e77414b 100644
--- a/crates/recording/src/feeds/microphone.rs
+++ b/crates/recording/src/feeds/microphone.rs
@@ -3079,7 +3079,7 @@ impl Message<Unlock> for MicrophoneFeed {
/// Test-only microphone feeds driven by a synthetic device instead of
/// hardware, so recording tests exercise the real feed, lock and pipeline
/// paths with samples whose capture times are known exactly.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
pub mod synthetic {
use super::*;
diff --git a/crates/recording/src/feeds/mod.rs b/crates/recording/src/feeds/mod.rs
index 09263bf3b..6f084fa23 100644
--- a/crates/recording/src/feeds/mod.rs
+++ b/crates/recording/src/feeds/mod.rs
@@ -6,7 +6,7 @@ pub mod microphone;
/// full or the feed has stopped. Test-only sources hand over the same way so
/// a feed that cannot keep up is visible as a rejection, not as a late
/// source.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Admission {
Accepted,
@@ -14,7 +14,7 @@ pub enum Admission {
Closed,
}
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
impl Admission {
fn of<M, E>(result: Result<(), kameo::error::SendError<M, E>>) -> Self {
match result {
@@ -27,5 +27,5 @@ impl Admission {
/// Test-only slow consumer: the feed actor handles nothing else for the
/// given time, as a feed blocked on a downstream consumer would.
-#[cfg(feature = "test-utils")]
+#[cfg(any(test, feature = "test-utils"))]
pub struct Stall(pub std::time::Duration);
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index 9045431f6..c7fb51597 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -426,6 +426,7 @@ pub enum PipelineHealthEvent {
source: String,
starvation_ms: u64,
},
+ MicrophoneDigitalSilence,
RecoveryFragmentCorrupt {
path: String,
reason: String,
@@ -496,6 +497,9 @@ pub fn emit_health(tx: &HealthSender, event: PipelineHealthEvent) {
PipelineHealthEvent::CaptureTargetLost { .. } => {
health_event(13, "capture_target_lost", &[])
}
+ PipelineHealthEvent::MicrophoneDigitalSilence => {
+ health_event(14, "microphone_digital_silence", &[])
+ }
}
let _ = tx.try_send(event);
}
@@ -2273,6 +2277,7 @@ pub struct SetupCtx {
master_clock: Arc<MasterClock>,
stop_token: CancellationToken,
stop_signal: PipelineStopSignal,
+ digital_silence_ms: Arc<AtomicU64>,
}
impl SetupCtx {
@@ -2288,9 +2293,14 @@ impl SetupCtx {
master_clock,
stop_token,
stop_signal,
+ digital_silence_ms: Arc::new(AtomicU64::new(0)),
}
}
+ pub fn digital_silence_ms(&self) -> Arc<AtomicU64> {
+ self.digital_silence_ms.clone()
+ }
+
pub fn tasks(&mut self) -> &mut TaskPool {
&mut self.tasks
}
@@ -3005,6 +3015,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
);
let audio_gap_summary = Arc::new(OnceLock::new());
+ let digital_silence_ms = setup_ctx.digital_silence_ms();
finish_build(
setup_ctx,
@@ -3047,6 +3058,7 @@ impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
video_timestamp_span,
health_rx: Some(build_ctx.health_rx),
audio_gap_summary,
+ digital_silence_ms,
})
}
}
@@ -3104,6 +3116,7 @@ impl OutputPipelineBuilder<NoVideo> {
let shared_pause = SharedWallClockPause::new(build_ctx.pause_flag.clone());
let audio_gap_summary = Arc::new(OnceLock::new());
+ let digital_silence_ms = setup_ctx.digital_silence_ms();
finish_build(
setup_ctx,
@@ -3139,6 +3152,7 @@ impl OutputPipelineBuilder<NoVideo> {
video_timestamp_span: Arc::new(VideoTimestampSpan::default()),
health_rx: Some(build_ctx.health_rx),
audio_gap_summary,
+ digital_silence_ms,
})
}
}
@@ -4931,6 +4945,7 @@ pub struct OutputPipeline {
video_timestamp_span: Arc<VideoTimestampSpan>,
health_rx: Option<HealthReceiver>,
audio_gap_summary: Arc<OnceLock<AudioGapSummary>>,
+ digital_silence_ms: Arc<AtomicU64>,
}
pub struct FinishedOutputPipeline {
@@ -4942,6 +4957,7 @@ pub struct FinishedOutputPipeline {
/// media span for VFR content.
pub video_timestamp_span: Option<(Duration, Duration)>,
pub audio_gap_summary: Option<AudioGapSummary>,
+ pub digital_silence_ms: Option<u32>,
}
pub(crate) struct OutputPipelineStopOutcome {
@@ -5075,6 +5091,10 @@ impl OutputPipeline {
video_frame_count: self.video_frame_count.load(Ordering::Acquire),
video_timestamp_span: self.video_timestamp_span.get(),
audio_gap_summary: self.audio_gap_summary.get().copied(),
+ digital_silence_ms: match self.digital_silence_ms.load(Ordering::Relaxed) {
+ 0 => None,
+ ms => Some(u32::try_from(ms).unwrap_or(u32::MAX)),
+ },
},
media_error,
})
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs
index f85d1070d..e19c90ba4 100644
--- a/crates/recording/src/recovery.rs
+++ b/crates/recording/src/recovery.rs
@@ -2637,6 +2637,7 @@ impl RecoveryManager {
.and_then(|s| s.mic.as_ref())
.and_then(|m| m.device_id.clone()),
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
@@ -2663,6 +2664,7 @@ impl RecoveryManager {
.and_then(|s| s.system_audio.as_ref())
.and_then(|a| a.device_id.clone()),
gap_summary: None,
+ digital_silence_ms: None,
})
} else {
None
@@ -5234,12 +5236,14 @@ mod clean_studio_snapshot_tests {
start_time: Some(-0.125),
device_id: Some("benchmark microphone".into()),
gap_summary: None,
+ digital_silence_ms: None,
});
inner.segments[0].system_audio = Some(AudioMeta {
path: "content/segments/segment-0/system_audio.m4a".into(),
start_time: Some(0.25),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
@@ -5302,6 +5306,7 @@ mod clean_studio_snapshot_tests {
start_time: Some(-0.125),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
let recording = RecoveryManager::analyze_incomplete(project, &meta).unwrap();
diff --git a/crates/recording/src/recovery/preparing_projection.rs b/crates/recording/src/recovery/preparing_projection.rs
index 38a5bed98..37ce3e5a1 100644
--- a/crates/recording/src/recovery/preparing_projection.rs
+++ b/crates/recording/src/recovery/preparing_projection.rs
@@ -990,12 +990,14 @@ mod tests {
overlap_dropped_frames: 2,
startup_overlap_drops: 1,
}),
+ digital_silence_ms: None,
});
metadata.segments[0].system_audio = Some(AudioMeta {
path: RelativePathBuf::from(format!("{base}/system_audio.ogg")),
start_time: Some(0.375),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
metadata.segments[0].cursor =
Some(RelativePathBuf::from(format!("{base}/cursor.json")));
diff --git a/crates/recording/src/sources/microphone.rs b/crates/recording/src/sources/microphone.rs
index a81ea9da8..1f335f127 100644
--- a/crates/recording/src/sources/microphone.rs
+++ b/crates/recording/src/sources/microphone.rs
@@ -75,6 +75,114 @@ fn reset_reconnect_after_frame(
*next_reconnect_after = MIC_RECONNECT_AFTER;
}
+// macOS can hand an active VoIP call the input while CoreAudio keeps delivering
+// on-time buffers of exact 0.0 (#2303). Those are real captured frames, so the
+// stall/keepalive path never fires. A live microphone's noise floor is never
+// bit-exact zero, even in a quiet room (~4% non-zero samples at -91 dBFS in the
+// issue's measurements), so a sustained run of exact zeros is the signal.
+const DIGITAL_SILENCE_WINDOW: Duration = Duration::from_secs(1);
+const DIGITAL_SILENCE_MIN_WINDOWS: u32 = 5;
+const DIGITAL_SILENCE_MAX_NONZERO_PER_MILLE: u64 = 1;
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum DigitalSilenceUpdate {
+ Unchanged,
+ RunStarted,
+ RunExtended,
+ RunEnded,
+}
+
+struct DigitalSilenceDetector {
+ sample_rate: u64,
+ window_len: u64,
+ window_total: u64,
+ window_nonzero: u64,
+ silent_windows: u32,
+ run_samples: u64,
+ flagged_samples: u64,
+}
+
+impl DigitalSilenceDetector {
+ fn new(sample_rate: u32) -> Self {
+ let sample_rate = u64::from(sample_rate).max(1);
+ Self {
+ sample_rate,
+ window_len: (sample_rate * DIGITAL_SILENCE_WINDOW.as_millis() as u64 / 1000).max(1),
+ window_total: 0,
+ window_nonzero: 0,
+ silent_windows: 0,
+ run_samples: 0,
+ flagged_samples: 0,
+ }
+ }
+
+ fn reset(&mut self) -> DigitalSilenceUpdate {
+ let was_flagged = self.silent_windows >= DIGITAL_SILENCE_MIN_WINDOWS;
+ self.window_total = 0;
+ self.window_nonzero = 0;
+ self.silent_windows = 0;
+ self.run_samples = 0;
+ if was_flagged {
+ DigitalSilenceUpdate::RunEnded
+ } else {
+ DigitalSilenceUpdate::Unchanged
+ }
+ }
+
+ fn observe(&mut self, total: u64, nonzero: u64) -> DigitalSilenceUpdate {
+ self.window_total = self.window_total.saturating_add(total);
+ self.window_nonzero = self.window_nonzero.saturating_add(nonzero);
+ if self.window_total < self.window_len {
+ return DigitalSilenceUpdate::Unchanged;
+ }
+
+ let window_total = self.window_total;
+ let silent = self.window_nonzero.saturating_mul(1000)
+ <= window_total.saturating_mul(DIGITAL_SILENCE_MAX_NONZERO_PER_MILLE);
+ self.window_total = 0;
+ self.window_nonzero = 0;
+
+ if !silent {
+ return self.reset();
+ }
+
+ self.silent_windows = self.silent_windows.saturating_add(1);
+ self.run_samples = self.run_samples.saturating_add(window_total);
+ match self.silent_windows.cmp(&DIGITAL_SILENCE_MIN_WINDOWS) {
+ std::cmp::Ordering::Less => DigitalSilenceUpdate::Unchanged,
+ std::cmp::Ordering::Equal => {
+ self.flagged_samples = self.flagged_samples.saturating_add(self.run_samples);
+ DigitalSilenceUpdate::RunStarted
+ }
+ std::cmp::Ordering::Greater => {
+ self.flagged_samples = self.flagged_samples.saturating_add(window_total);
+ DigitalSilenceUpdate::RunExtended
+ }
+ }
+ }
+
+ fn run_ms(&self) -> u64 {
+ self.run_samples.saturating_mul(1000) / self.sample_rate
+ }
+
+ fn flagged_ms(&self) -> u64 {
+ self.flagged_samples.saturating_mul(1000) / self.sample_rate
+ }
+}
+
+fn count_nonzero_samples(frame: &ffmpeg::frame::Audio) -> Option<(u64, u64)> {
+ if frame.format() != ffmpeg::format::Sample::F32(ffmpeg::format::sample::Type::Packed) {
+ return None;
+ }
+ let samples = frame.samples() * usize::from(frame.channels());
+ let bytes = frame.data(0).get(..samples * size_of::<f32>())?;
+ let nonzero = bytes
+ .chunks_exact(size_of::<f32>())
+ .filter(|sample| f32::from_ne_bytes([sample[0], sample[1], sample[2], sample[3]]) != 0.0)
+ .count();
+ Some((samples as u64, nonzero as u64))
+}
+
pub struct Microphone {
#[cfg(windows)]
capture_scope: crate::output_pipeline::PipelineBuildScope,
@@ -263,6 +371,7 @@ impl AudioSource for Microphone {
Self: Sized,
{
let health_tx = ctx.health_tx().clone();
+ let digital_silence_ms = ctx.digital_silence_ms();
#[cfg(windows)]
let capture_scope = crate::output_pipeline::PipelineBuildScope::new();
#[cfg(windows)]
@@ -375,6 +484,7 @@ impl AudioSource for Microphone {
let health_tx = health_tx.clone();
let cancel = cancel.clone();
let recording_muted = recording_muted.clone();
+ let digital_silence_ms = digital_silence_ms.clone();
#[cfg(windows)]
let capture_scope = capture_scope.clone();
#[cfg(windows)]
@@ -382,6 +492,7 @@ impl AudioSource for Microphone {
#[cfg(target_os = "linux")]
let required_health = required_health.clone();
async move {
+ let mut digital_silence = DigitalSilenceDetector::new(audio_info.sample_rate);
let mut resampler: Option<MicResampler> = None;
let mut silence_mode = false;
let mut silence_start: Option<Instant> = None;
@@ -511,10 +622,46 @@ impl AudioSource for Microphone {
// frame: cadence, sample counts, and timestamps must stay
// identical to the unmuted stream so the muxer timeline
// and the stall/keepalive machinery see no difference.
- if recording_muted.load(Ordering::Relaxed) {
+ let muted = recording_muted.load(Ordering::Relaxed);
+ if muted {
silence_frame_payload(&mut audio_frame.inner);
}
+ let silence_update = if muted {
+ digital_silence.reset()
+ } else {
+ count_nonzero_samples(&audio_frame.inner)
+ .map(|(total, nonzero)| {
+ digital_silence.observe(total, nonzero)
+ })
+ .unwrap_or(DigitalSilenceUpdate::Unchanged)
+ };
+ match silence_update {
+ DigitalSilenceUpdate::RunStarted => {
+ warn!(
+ run_ms = digital_silence.run_ms(),
+ "Microphone is delivering exact digital silence while unmuted; another app may be holding the input"
+ );
+ digital_silence_ms
+ .store(digital_silence.flagged_ms(), Ordering::Relaxed);
+ emit_health(
+ &health_tx,
+ PipelineHealthEvent::MicrophoneDigitalSilence,
+ );
+ }
+ DigitalSilenceUpdate::RunExtended => {
+ digital_silence_ms
+ .store(digital_silence.flagged_ms(), Ordering::Relaxed);
+ }
+ DigitalSilenceUpdate::RunEnded => {
+ info!(
+ total_digital_silence_ms = digital_silence.flagged_ms(),
+ "Microphone digital silence ended"
+ );
+ }
+ DigitalSilenceUpdate::Unchanged => {}
+ }
+
let sample_count = audio_frame.samples();
last_frame_duration = Duration::from_secs_f64(
sample_count as f64 / audio_info.rate() as f64,
@@ -1569,3 +1716,200 @@ mod owned_microphone_tests {
assert!(cancel.is_cancelled());
}
}
+
+#[cfg(test)]
+mod digital_silence_tests {
+ use super::*;
+ use crate::{
+ feeds::microphone::synthetic::SyntheticMicrophone,
+ output_pipeline::{AudioMuxer, Muxer, OutputPipeline, TaskPool},
+ };
+ use cap_timestamp::Timestamp;
+
+ const RATE: u32 = 48_000;
+ const CHUNK: usize = 480;
+
+ fn target_info() -> AudioInfo {
+ AudioInfo::new_raw(
+ cap_media_info::Sample::F32(cap_media_info::Type::Packed),
+ RATE,
+ 1,
+ )
+ }
+
+ fn frame_with(samples: &[f32]) -> ffmpeg::frame::Audio {
+ let mut frame = create_silence_frame(&target_info(), samples.len());
+ let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_ne_bytes()).collect();
+ frame.data_mut(0)[..bytes.len()].copy_from_slice(&bytes);
+ frame
+ }
+
+ fn quiet_room(len: usize) -> Vec<f32> {
+ (0..len)
+ .map(|i| if i % 25 == 0 { 3.0e-5 } else { 0.0 })
+ .collect()
+ }
+
+ fn feed_seconds(
+ detector: &mut DigitalSilenceDetector,
+ samples: &[f32],
+ seconds: f64,
+ ) -> Vec<DigitalSilenceUpdate> {
+ let frame = frame_with(samples);
+ let (total, nonzero) = count_nonzero_samples(&frame).unwrap();
+ let chunks = (seconds * f64::from(RATE) / samples.len() as f64).round() as usize;
+ (0..chunks)
+ .map(|_| detector.observe(total, nonzero))
+ .filter(|update| *update != DigitalSilenceUpdate::Unchanged)
+ .collect()
+ }
+
+ #[test]
+ fn exact_zero_frames_flag_after_a_sustained_window() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ assert!(feed_seconds(&mut detector, &zeros, 4.99).is_empty());
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 0.01),
+ vec![DigitalSilenceUpdate::RunStarted]
+ );
+ assert_eq!(detector.flagged_ms(), 5_000);
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 2.0),
+ vec![DigitalSilenceUpdate::RunExtended; 2]
+ );
+ assert_eq!(detector.flagged_ms(), 7_000);
+ }
+
+ #[test]
+ fn quiet_room_noise_floor_is_not_digital_silence() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ assert!(feed_seconds(&mut detector, &quiet_room(CHUNK), 30.0).is_empty());
+ assert_eq!(detector.flagged_ms(), 0);
+ }
+
+ #[test]
+ fn muting_breaks_a_digital_silence_run() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ assert!(feed_seconds(&mut detector, &zeros, 4.0).is_empty());
+ assert_eq!(detector.reset(), DigitalSilenceUpdate::Unchanged);
+ assert!(feed_seconds(&mut detector, &zeros, 4.0).is_empty());
+ assert_eq!(detector.flagged_ms(), 0);
+ }
+
+ #[test]
+ fn real_audio_ends_a_run_and_later_runs_accumulate() {
+ let mut detector = DigitalSilenceDetector::new(RATE);
+ let zeros = vec![0.0; CHUNK];
+ feed_seconds(&mut detector, &zeros, 6.0);
+ assert_eq!(
+ feed_seconds(&mut detector, &quiet_room(CHUNK), 1.0),
+ vec![DigitalSilenceUpdate::RunEnded]
+ );
+ assert_eq!(detector.flagged_ms(), 6_000);
+ assert_eq!(
+ feed_seconds(&mut detector, &zeros, 5.0),
+ vec![DigitalSilenceUpdate::RunStarted]
+ );
+ assert_eq!(detector.flagged_ms(), 11_000);
+ }
+
+ #[test]
+ fn only_bit_exact_zero_counts_as_silent() {
+ let tiny = frame_with(&[1.0e-30; CHUNK]);
+ assert_eq!(
+ count_nonzero_samples(&tiny),
+ Some((CHUNK as u64, CHUNK as u64))
+ );
+ let negative_zero = frame_with(&[-0.0; CHUNK]);
+ assert_eq!(
+ count_nonzero_samples(&negative_zero),
+ Some((CHUNK as u64, 0))
+ );
+ }
+
+ struct CountingMuxer(Arc<AtomicU64>);
+
+ impl Muxer for CountingMuxer {
+ type Config = Arc<AtomicU64>;
+
+ async fn setup(
+ counter: Arc<AtomicU64>,
+ _: std::path::PathBuf,
+ _: Option<cap_media_info::VideoInfo>,
+ _: Option<AudioInfo>,
+ _: Arc<AtomicBool>,
+ _: &mut TaskPool,
+ ) -> anyhow::Result<Self> {
+ Ok(Self(counter))
+ }
+
+ fn finish(&mut self, _: Duration) -> anyhow::Result<anyhow::Result<()>> {
+ Ok(Ok(()))
+ }
+ }
+
+ impl AudioMuxer for CountingMuxer {
+ fn send_audio_frame(&mut self, frame: AudioFrame, _: Duration) -> anyhow::Result<()> {
+ self.0
+ .fetch_add(frame.inner.samples() as u64, Ordering::Release);
+ Ok(())
+ }
+ }
+
+ async fn record_through_feed(samples: &[f32], seconds: u32) -> (Option<u32>, bool) {
+ let mic = SyntheticMicrophone::spawn("voip-held-input", RATE, 1);
+ let lock = Arc::new(mic.lock().await.unwrap());
+ let muxed = Arc::new(AtomicU64::new(0));
+ let temp = tempfile::tempdir().unwrap();
+ let mut pipeline = OutputPipeline::builder(temp.path().join("audio-input.ogg"))
+ .with_audio_source::<Microphone>(lock.clone())
+ .build::<CountingMuxer>(muxed.clone())
+ .await
+ .unwrap();
+ let mut health = pipeline.take_health_rx().unwrap();
+
+ let start = Instant::now();
+ let chunks_per_second = RATE as usize / samples.len();
+ let total_chunks = chunks_per_second * seconds as usize;
+ for i in 0..total_chunks {
+ let at = start + Duration::from_secs_f64(i as f64 / chunks_per_second as f64);
+ mic.push(samples, Timestamp::Instant(at)).await;
+ tokio::task::yield_now().await;
+ }
+
+ let expected = u64::from(RATE) * u64::from(seconds) * 9 / 10;
+ tokio::time::timeout(Duration::from_secs(10), async {
+ while muxed.load(Ordering::Acquire) < expected {
+ tokio::time::sleep(Duration::from_millis(5)).await;
+ }
+ })
+ .await
+ .expect("the forwarded samples never reached the muxer");
+
+ let finished = pipeline.stop().await.unwrap();
+ let mut reported = false;
+ while let Ok(event) = health.try_recv() {
+ reported |= matches!(event, PipelineHealthEvent::MicrophoneDigitalSilence);
+ }
+ (finished.digital_silence_ms, reported)
+ }
+
+ #[tokio::test]
+ async fn all_zero_frames_through_the_feed_surface_on_stop() {
+ let (digital_silence_ms, reported) = record_through_feed(&[0.0; 4_800], 8).await;
+ assert!(reported, "the live health event must fire");
+ assert!(
+ digital_silence_ms.is_some_and(|ms| ms >= 5_000),
+ "expected the stop result to carry the silent span, got {digital_silence_ms:?}"
+ );
+ }
+
+ #[tokio::test]
+ async fn quiet_room_through_the_feed_reports_nothing() {
+ let (digital_silence_ms, reported) = record_through_feed(&quiet_room(4_800), 8).await;
+ assert!(!reported);
+ assert_eq!(digital_silence_ms, None);
+ }
+}
diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs
index 8d524aef1..eea27059d 100644
--- a/crates/recording/src/studio_recording.rs
+++ b/crates/recording/src/studio_recording.rs
@@ -2838,12 +2838,14 @@ async fn stop_recording(
start_time: mic_start_time,
device_id: s.mic_device_id.clone(),
gap_summary: to_project_gap_summary(mic.audio_gap_summary),
+ digital_silence_ms: mic.digital_silence_ms,
}),
system_audio: s.pipeline.system_audio.map(|audio| AudioMeta {
path: make_relative(&audio.path),
start_time: start_times.system_audio,
device_id: None,
gap_summary: to_project_gap_summary(audio.audio_gap_summary),
+ digital_silence_ms: None,
}),
cursor: s
.pipeline
@@ -5405,6 +5407,7 @@ mod tests {
video_frame_count,
video_timestamp_span: None,
audio_gap_summary: None,
+ digital_silence_ms: None,
}
}
diff --git a/crates/recording/tests/recovery.rs b/crates/recording/tests/recovery.rs
index 590815539..d27e5e52f 100644
--- a/crates/recording/tests/recovery.rs
+++ b/crates/recording/tests/recovery.rs
@@ -1745,12 +1745,14 @@ fn set_recovery_optional_tracks(project: &Path) {
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
segment.system_audio = Some(cap_project::AudioMeta {
path: RelativePathBuf::from("content/segments/segment-0/system_audio.ogg"),
start_time: Some(0.0),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
});
meta.save_for_project().unwrap();
}
diff --git a/crates/verify/src/fixture.rs b/crates/verify/src/fixture.rs
index de6ac9197..8c5da2342 100644
--- a/crates/verify/src/fixture.rs
+++ b/crates/verify/src/fixture.rs
@@ -412,6 +412,7 @@ pub fn write_fixture(spec: &FixtureSpec, dir: &Path) -> anyhow::Result<()> {
start_time: Some(a.start_time),
device_id: None,
gap_summary: None,
+ digital_silence_ms: None,
})
}
None => None,
diff --git a/crates/verify/src/pipeline.rs b/crates/verify/src/pipeline.rs
index cf127cc65..f4970b650 100644
--- a/crates/verify/src/pipeline.rs
+++ b/crates/verify/src/pipeline.rs
@@ -495,6 +495,7 @@ async fn record_segment(
overlap_dropped_frames: s.overlap_dropped_frames,
startup_overlap_drops: s.startup_overlap_drops,
}),
+ digital_silence_ms: finished.digital_silence_ms,
};
if name == "mic" {
mic_meta = Some(meta);