Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(recording): start gated Instant video where its audio starts when the gate arms before the pipeline epoch
Richie McIlroy40be845a1c03
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rsindex 5611519d6..e28e3fc97 100644--- a/crates/recording/src/output_pipeline/core.rs+++ b/crates/recording/src/output_pipeline/core.rs@@ -3512,10 +3512,11 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: V// followed the arm within a frame interval.let mut held_frame: Option<TVideo::Frame> = None;let mut wall_clock_origin = timestamps.instant();- // Set when the start gate puts the video origin at its arm point (see- // `RecordingStartGate::video_origin_lead`). Capture times flow through- // clock remapping and drift tracking unchanged; only the muxed- // timeline is offset.+ // Set when the start gate starts the video where the audio starts:+ // the arm point, or the pipeline epoch if the gate was armed before it+ // (see `RecordingStartGate::video_origin_lead`). Capture times flow+ // through clock remapping and drift tracking unchanged; only the+ // muxed timeline is offset.let mut origin_lead: Option<(Instant, Duration)> = None;let res = stop_token@@ -3576,9 +3577,10 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: Vgate.offset_secs(timestamp).unwrap_or_default() * 1000.0,"Start gate admitted first video frame");+ let origin = audio_epoch(timestamps, Some(gate));origin_lead = gate- .armed_instant()- .zip(gate.video_origin_lead(timestamp));+ .video_origin_lead(timestamp, origin)+ .map(|lead| (origin.instant(), lead));}wall_clock_origin = gate.armed_instant()@@ -3729,9 +3731,10 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: Vif !gate.admits_video(timestamp) {continue;}+ let origin = audio_epoch(timestamps, Some(gate));origin_lead = gate- .armed_instant()- .zip(gate.video_origin_lead(timestamp));+ .video_origin_lead(timestamp, origin)+ .map(|lead| (origin.instant(), lead));wall_clock_origin = gate.armed_instant().unwrap_or(timestamps.instant());@@ -8857,11 +8860,17 @@ mod tests {assert!(timestamps.len() >= 3, "{timestamps:?}");}- #[tokio::test]- async fn primed_instant_video_starts_at_the_arm_point() {+ /// A gated Instant pipeline, its gate armed after the pipeline epoch+ /// or (`armed_first`) 40 ms before it. Its origin is where the audio+ /// starts, the later of the two; frames are captured 20, 53 and 86 ms+ /// after it.+ async fn record_gated_instant(armed_first: bool) -> (Instant, Vec<Duration>, Timestamp) {let temp_dir = tempfile::tempdir().unwrap();let gate = RecordingStartGate::new();gate.set_video_origin_at_arm();+ let early_arm = armed_first.then(Timestamps::now);+ tokio::time::sleep(Duration::from_millis(40)).await;+ let epoch = Timestamps::now();let (sender, receiver) = flume::bounded(8);let sent = Arc::new(std::sync::Mutex::new(Vec::new()));let pipeline = OutputPipeline::builder(temp_dir.path().join("instant.mp4"))@@ -8869,18 +8878,19 @@ mod tests {VideoInfo::from_raw(cap_media_info::RawVideoFormat::Bgra, 16, 16, 30),receiver,))- .with_timestamps(Timestamps::now())+ .with_timestamps(epoch).with_start_gate(Some(gate.clone())).build::<ObservedMuxer>(sent.clone()).await.unwrap();- let armed = Timestamps::now();+ let armed = early_arm.unwrap_or_else(Timestamps::now);assert!(gate.arm_at(armed));- // Fresh frames from 20 ms after the arm point, one frame interval+ let origin = if armed_first { epoch } else { armed };+ // Fresh frames from 20 ms after the origin, one frame interval// apart, each delivered when it is captured.for offset_ms in [20u64, 53, 86] {- let captured = armed.instant() + Duration::from_millis(offset_ms);+ let captured = origin.instant() + Duration::from_millis(offset_ms);tokio::time::sleep_until(captured.into()).await;sender.send_async(StaticFrame {@@ -8891,14 +8901,17 @@ mod tests {}tokio::time::sleep(Duration::from_millis(33)).await;let outcome = pipeline.stop().await.unwrap();+ let timestamps = sent.lock().unwrap().clone();+ (origin.instant(), timestamps, outcome.first_timestamp)+ }- match outcome.first_timestamp {- Timestamp::Instant(instant) => assert_eq!(instant, armed.instant()),+ fn assert_starts_at_origin(origin: Instant, timestamps: &[Duration], first: Timestamp) {+ match first {+ Timestamp::Instant(instant) => assert_eq!(instant, origin),other => panic!("unexpected first timestamp {other:?}"),}- let timestamps = sent.lock().unwrap().clone();assert_eq!(timestamps.first(), Some(&Duration::ZERO));- // Later frames keep their capture times relative to the arm point.+ // Later frames keep their capture times relative to the origin.for (index, expected_ms) in [(1, 53), (2, 86)] {let muxed = timestamps.get(index).copied().expect("frame muxed");assert!(@@ -8908,6 +8921,18 @@ mod tests {}}+ #[tokio::test]+ async fn primed_instant_video_starts_at_the_arm_point() {+ let (origin, timestamps, first) = record_gated_instant(false).await;+ assert_starts_at_origin(origin, ×tamps, first);+ }++ #[tokio::test]+ async fn primed_instant_video_armed_before_the_epoch_starts_at_the_epoch() {+ let (origin, timestamps, first) = record_gated_instant(true).await;+ assert_starts_at_origin(origin, ×tamps, first);+ }+#[tokio::test]async fn primed_static_screen_opens_on_the_frame_held_at_the_arm_point() {let temp_dir = tempfile::tempdir().unwrap();diff --git a/crates/recording/src/output_pipeline/start_gate.rs b/crates/recording/src/output_pipeline/start_gate.rsindex 2825720dc..428407d3a 100644--- a/crates/recording/src/output_pipeline/start_gate.rs+++ b/crates/recording/src/output_pipeline/start_gate.rs@@ -54,16 +54,23 @@ impl RecordingStartGate {self.0.video_origin_at_arm.store(true, Ordering::Release);}- /// How far the first admitted video frame was captured after the arm- /// point, when [`Self::set_video_origin_at_arm`] is on. The video track- /// then starts at the arm point: the first frame is shown from there and- /// every later frame is muxed this much later than its offset from the- /// first, so each keeps its capture time relative to the arm point.- pub(crate) fn video_origin_lead(&self, admitted: Timestamp) -> Option<Duration> {+ /// How far the first admitted video frame was captured after `origin`,+ /// when [`Self::set_video_origin_at_arm`] is on and the gate is open.+ /// `origin` is where the recording's audio starts: the arm point, or the+ /// pipeline epoch if the gate was armed before it. The video track then+ /// starts there too: the first frame is shown from the origin and every+ /// later frame is muxed this much later than its offset from the first,+ /// so each keeps its capture time relative to the origin.+ pub(crate) fn video_origin_lead(+ &self,+ admitted: Timestamp,+ origin: Timestamps,+ ) -> Option<Duration> {if !self.0.video_origin_at_arm.load(Ordering::Acquire) {return None;}- let offset = self.offset_secs(admitted)?;+ self.armed_at_or_failsafe()?;+ let offset = admitted.signed_duration_since_secs(origin);Some(Duration::from_secs_f64(offset.max(0.0)))}@@ -179,18 +186,38 @@ mod tests {fn video_origin_lead_only_when_asked() {let armed = Timestamps::now();let admitted = at(armed, 21);+ let near = |lead: Option<Duration>, ms: u64| {+ lead.is_some_and(|lead| {+ lead.abs_diff(Duration::from_millis(ms)) < Duration::from_micros(1)+ })+ };let gate = RecordingStartGate::new();assert!(gate.arm_at(armed));- assert_eq!(gate.video_origin_lead(admitted), None);+ assert_eq!(gate.video_origin_lead(admitted, armed), None);let gate = RecordingStartGate::new();gate.set_video_origin_at_arm();- assert_eq!(gate.video_origin_lead(admitted), None, "closed gate");+ assert_eq!(gate.video_origin_lead(admitted, armed), None, "closed gate");+ assert!(gate.arm_at(armed));+ assert!(near(gate.video_origin_lead(admitted, armed), 21));+ assert_eq!(+ gate.video_origin_lead(at(armed, 0), armed),+ Some(Duration::ZERO)+ );+ }++ #[test]+ fn video_origin_lead_counts_from_a_pipeline_epoch_after_the_arm_point() {+ let armed = Timestamps::now();+ std::thread::sleep(Duration::from_millis(5));+ let epoch = Timestamps::now();+ let gate = RecordingStartGate::new();+ gate.set_video_origin_at_arm();assert!(gate.arm_at(armed));- let lead = gate.video_origin_lead(admitted).unwrap();- assert!(lead.abs_diff(Duration::from_millis(21)) < Duration::from_micros(1));- assert_eq!(gate.video_origin_lead(at(armed, 0)), Some(Duration::ZERO));+ let admitted = Timestamp::Instant(epoch.instant() + Duration::from_millis(12));+ let lead = gate.video_origin_lead(admitted, epoch).unwrap();+ assert!(lead.abs_diff(Duration::from_millis(12)) < Duration::from_micros(1));}#[test]