Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(timestamp): move skipped video frames back onto source time
Richie McIlroyf7479b101b97
diff --git a/crates/timestamp/src/master_clock.rs b/crates/timestamp/src/master_clock.rsindex a203d4d08..747375d10 100644--- a/crates/timestamp/src/master_clock.rs+++ b/crates/timestamp/src/master_clock.rs@@ -170,6 +170,10 @@ pub struct SourceClockState {timing_adjust: i64,next_expected_ns: Option<i64>,last_cadence_output_ns: Option<u64>,+ detect_skipped_frames: bool,+ pending_lag_ns: Option<i64>,+ skip_confirmed: bool,+ skip_count: u64,snap_count: u64,hard_reset_count: u64,resync_count: u64,@@ -185,6 +189,10 @@ impl SourceClockState {timing_adjust: 0,next_expected_ns: None,last_cadence_output_ns: None,+ detect_skipped_frames: false,+ pending_lag_ns: None,+ skip_confirmed: false,+ skip_count: 0,snap_count: 0,hard_reset_count: 0,resync_count: 0,@@ -193,6 +201,16 @@ impl SourceClockState {}}+ /// Frames that arrive a whole cadence slot late because the source skipped+ /// frames are moved onto their real timestamp once the lag persists into the+ /// next frame, instead of being snapped back a slot for the rest of the+ /// recording. A single late frame, or a backlog that drains, is still+ /// smoothed onto the cadence.+ pub fn detecting_skipped_frames(mut self) -> Self {+ self.detect_skipped_frames = true;+ self+ }+pub fn name(&self) -> &'static str {self.name}@@ -209,6 +227,14 @@ impl SourceClockState {self.next_expected_ns}+ pub fn skip_count(&self) -> u64 {+ self.skip_count+ }++ pub fn lag_pending(&self) -> bool {+ self.pending_lag_ns.is_some()+ }+pub fn snap_count(&self) -> u64 {self.snap_count}@@ -278,6 +304,8 @@ impl SourceClockState {let mut ts_ns = raw_ns;let duration_ns = frame_duration_ns.min(i64::MAX as u64) as i64;+ let pending_lag_ns = self.pending_lag_ns.take();+ let skip_confirmed = std::mem::take(&mut self.skip_confirmed);if !self.timing_set {self.timing_adjust = now_ns.saturating_sub(raw_ns);@@ -291,6 +319,17 @@ impl SourceClockState {self.hard_reset_count = self.hard_reset_count.saturating_add(1);self.resync_count = self.resync_count.saturating_add(1);outcome = SourceClockOutcome::HardReset;+ } else if smooth+ && diff < TS_SMOOTHING_THRESHOLD_NS+ && self.skipped_frames(+ raw_ns - expected,+ duration_ns,+ pending_lag_ns,+ skip_confirmed,+ )+ {+ self.skip_confirmed = true;+ self.skip_count = self.skip_count.saturating_add(1);} else if smooth && diff < TS_SMOOTHING_THRESHOLD_NS {// Snap jitter onto the expected cadence, but cap how far the// ladder may lead the source clock. Each snap re-anchors@@ -338,11 +377,33 @@ impl SourceClockState {}}+ fn skipped_frames(+ &mut self,+ lag_ns: i64,+ duration_ns: i64,+ pending_lag_ns: Option<i64>,+ skip_confirmed: bool,+ ) -> bool {+ let half_frame_ns = duration_ns / 2;+ if !self.detect_skipped_frames || half_frame_ns == 0 || lag_ns < half_frame_ns {+ return false;+ }+ if skip_confirmed+ || pending_lag_ns.is_some_and(|previous| lag_ns >= previous - half_frame_ns)+ {+ return true;+ }+ self.pending_lag_ns = Some(lag_ns);+ false+ }+pub fn reset(&mut self) {self.timing_set = false;self.timing_adjust = 0;self.next_expected_ns = None;self.last_cadence_output_ns = None;+ self.pending_lag_ns = None;+ self.skip_confirmed = false;}}@@ -898,4 +959,180 @@ mod tests {);assert_eq!(second.master_ns - first.master_ns, 47_000_000);}++ const FRAME_60_NS: u64 = 16_666_666;++ fn frame_raw_ns(frame: u64) -> i64 {+ (frame * 1_000_000_000 / 60) as i64+ }++ fn replay(state: &mut SourceClockState, raws: &[i64], frame_ns: u64) -> Vec<u64> {+ raws.iter()+ .map(|&raw| {+ state+ .remap_ns(raw, raw + 20_000_000, frame_ns, true)+ .master_ns+ })+ .collect()+ }++ fn skip_tracking() -> SourceClockState {+ SourceClockState::new("video").detecting_skipped_frames()+ }++ fn frames_with_skips(total: u64, skips: &[(u64, u64)]) -> Vec<i64> {+ (0..total)+ .filter(|frame| {+ !skips+ .iter()+ .any(|&(start, count)| *frame >= start && *frame < start + count)+ })+ .map(frame_raw_ns)+ .collect()+ }++ #[test]+ fn skipped_frames_at_start_return_to_source_time() {+ for dropped in [1, 2, 3, 4] {+ let raws = frames_with_skips(240, &[(1, dropped)]);+ let legacy = replay(&mut SourceClockState::new("legacy"), &raws, FRAME_60_NS);+ let output = replay(&mut skip_tracking(), &raws, FRAME_60_NS);++ let lag =+ (raws[239 - dropped as usize] as u64).abs_diff(legacy[239 - dropped as usize]);+ assert!(+ lag.abs_diff(dropped * FRAME_60_NS) < 1_000_000,+ "legacy smoothing keeps {dropped} skipped frames as lag, got {lag}"+ );+ for (index, (&raw, &mapped)) in raws.iter().zip(&output).enumerate().skip(2) {+ assert!(+ (raw as u64).abs_diff(mapped) < 1_000,+ "dropped={dropped} frame {index}: {mapped} vs source {raw}"+ );+ }+ }+ }++ #[test]+ fn skipped_frames_beyond_smoothing_threshold_match_legacy() {+ for dropped in [5, 8] {+ let raws = frames_with_skips(240, &[(1, dropped)]);+ let legacy = replay(&mut SourceClockState::new("legacy"), &raws, FRAME_60_NS);+ assert_eq!(replay(&mut skip_tracking(), &raws, FRAME_60_NS), legacy);+ }+ }++ #[test]+ fn first_frame_after_skip_is_pending_until_lag_persists() {+ let raws = frames_with_skips(10, &[(3, 2)]);+ let mut state = skip_tracking();+ let mut pending = Vec::new();+ let mut output = Vec::new();+ for raw in raws.iter().copied() {+ output.push(state.remap_ns(raw, raw, FRAME_60_NS, true).master_ns);+ pending.push(state.lag_pending());+ }+ assert_eq!(+ pending,+ [false, false, false, true, false, false, false, false]+ );+ assert_eq!(output[3], output[2] + FRAME_60_NS);+ assert_eq!(output[4], raws[4] as u64);+ assert_eq!(state.skip_count(), 1);+ }++ #[test]+ fn repeated_mid_recording_skips_never_accumulate_lag() {+ let raws = frames_with_skips(600, &[(100, 1), (200, 3), (300, 2), (301 + 50, 4)]);+ let output = replay(&mut skip_tracking(), &raws, FRAME_60_NS);+ let last = raws.len() - 1;+ assert!((raws[last] as u64).abs_diff(output[last]) < 1_000);+ for pair in output.windows(2) {+ assert!(pair[1] > pair[0]);+ }+ }++ #[test]+ fn skip_tracking_is_identical_to_legacy_without_skips() {+ for nominal in [24.0, 30.0, 60.0] {+ let frame_ns = (1_000_000_000.0 / nominal) as u64;+ for seed in 0..20 {+ let mut rng = TestRng(0x5EED + seed);+ let raws: Vec<i64> = (0..600)+ .map(|frame| {+ let jitter = (rng.next_f64() * 2.0 - 1.0) * 0.24 * frame_ns as f64;+ (frame as f64 * 1_000_000_000.0 / nominal + jitter).max(0.0) as i64+ })+ .collect();+ assert_eq!(+ replay(&mut skip_tracking(), &raws, frame_ns),+ replay(&mut SourceClockState::new("legacy"), &raws, frame_ns),+ "nominal={nominal} seed={seed}"+ );+ }+ }+ }++ #[test]+ fn single_late_frame_is_still_smoothed() {+ let mut raws: Vec<i64> = (0..120).map(frame_raw_ns).collect();+ raws[40] += 12_000_000;+ raws[80] += 60_000_000;+ let output = replay(&mut skip_tracking(), &raws, FRAME_60_NS);+ assert_eq!(+ output,+ replay(&mut SourceClockState::new("legacy"), &raws, FRAME_60_NS)+ );+ assert_eq!(output[40], output[39] + FRAME_60_NS);+ }++ #[test]+ fn delivery_backlog_draining_is_still_smoothed() {+ for stall_ms in [20, 40, 60] {+ let stall_ns = stall_ms * 1_000_000;+ let resume = frame_raw_ns(30) + stall_ns;+ let raws: Vec<i64> = (0..120)+ .map(|frame| {+ let raw = frame_raw_ns(frame);+ if frame >= 30 {+ raw.max(resume + (frame as i64 - 30) * 100_000)+ } else {+ raw+ }+ })+ .collect();+ let output = replay(&mut skip_tracking(), &raws, FRAME_60_NS);+ assert_eq!(+ output,+ replay(&mut SourceClockState::new("legacy"), &raws, FRAME_60_NS),+ "stall={stall_ms}ms"+ );+ }+ }++ #[test]+ fn slow_source_tracks_source_time_after_first_skip() {+ let raws: Vec<i64> = (0..60).map(|frame| frame * 66_666_666).collect();+ let output = replay(&mut skip_tracking(), &raws, 33_333_333);+ for (index, (&raw, &mapped)) in raws.iter().zip(&output).enumerate().skip(2) {+ assert_eq!(mapped, raw as u64, "frame {index}");+ }+ }++ #[test]+ fn skip_tracking_keeps_fast_source_bounds() {+ for nominal in [24.0, 30.0, 60.0] {+ let frame_ns = (1_000_000_000.0 / nominal) as u64;+ for actual in [60.0, 90.0, 120.0, 240.0, 1000.0] {+ let raws: Vec<i64> = (0..((1.5 * actual) as i64))+ .map(|frame| (frame as f64 * 1_000_000_000.0 / actual) as i64)+ .collect();+ assert_eq!(+ replay(&mut skip_tracking(), &raws, frame_ns),+ replay(&mut SourceClockState::new("legacy"), &raws, frame_ns),+ "nominal={nominal} actual={actual}"+ );+ }+ }+ }}