Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): shift mic capture timestamps back by measured input pipeline latency

ManthanNimodiya53a8f2b881c6
diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs
index a123537a2..224b17540 100644
--- a/crates/recording/src/feeds/microphone.rs
+++ b/crates/recording/src/feeds/microphone.rs
@@ -697,6 +697,24 @@ impl MicrophoneFeed {
CallbackSampleRateEstimator::new(callback_sample_rate);
let mut pending_samples = VecDeque::new();
+ let latency_info = estimate_input_latency(
+ callback_sample_rate,
+ buffer_size_frames.unwrap_or(1024),
+ Some(&label),
+ );
+ let capture_latency = Duration::from_secs_f64(
+ latency_info
+ .device_latency_secs
+ .clamp(0.0, MAX_CAPTURE_LATENCY_COMPENSATION_SECS),
+ );
+ if !capture_latency.is_zero() {
+ info!(
+ "🎤 Compensating capture timestamps by {:.1}ms input pipeline latency (transport: {:?})",
+ capture_latency.as_secs_f64() * 1000.0,
+ latency_info.transport
+ );
+ }
+
let stream = match device.build_input_stream_raw(
&stream_config,
sample_format,
@@ -730,7 +748,8 @@ impl MicrophoneFeed {
sample_rate: effective_sample_rate.sample_rate,
channels: callback_channels,
info: info.clone(),
- timestamp: Timestamp::from_cpal(input_timestamp.capture),
+ timestamp: Timestamp::from_cpal(input_timestamp.capture)
+ - capture_latency,
};
if !effective_sample_rate.settled {
@@ -914,6 +933,14 @@ const WIRELESS_TARGET_LATENCY_MS: u32 = 80;
const WIRELESS_MIN_LATENCY_MS: u32 = 50;
const WIRELESS_MAX_LATENCY_MS: u32 = 200;
+// The cpal capture timestamp (mHostTime / QPC) marks when samples left the
+// audio HAL, not when the sound reached the microphone. The gap between the
+// two is the input pipeline latency (device latency + safety offset + stream
+// latency), which otherwise lands in the recording as the mic track running
+// late relative to video. Buffer latency is deliberately excluded: the
+// callback timestamp already refers to the first frame of the buffer.
+const MAX_CAPTURE_LATENCY_COMPENSATION_SECS: f64 = 0.5;
+
fn stream_config_with_latency(
config: &SupportedStreamConfig,
device_name: Option<&str>,