Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(recording): heal stretched display tracks and cap WGC capture cadence
Richie McIlroy258c9f4a7bdd
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rsindex 249b3d19e..a685e5829 100644--- a/apps/desktop/src-tauri/src/lib.rs+++ b/apps/desktop/src-tauri/src/lib.rs@@ -6500,19 +6500,20 @@ pub(crate) async fn wait_for_recording_ready(app: &AppHandle, path: &Path) -> Re}if meta.studio_meta().is_some() {- // Repair camera tracks that were stamped at the wrong rate by older- // recorders (2x-length slow-motion camera). Non-fatal: the editor- // still opens with the unhealed file if this fails.+ // Repair video tracks that were stamped at the wrong rate by older+ // recorders (slow-motion display on high-refresh Windows monitors,+ // 2x-length camera). Non-fatal: the editor still opens with the+ // unhealed files if this fails.let path = path.to_path_buf();match tokio::task::spawn_blocking(move || {- cap_recording::camera_heal::heal_stretched_camera(&path)+ cap_recording::track_heal::heal_stretched_tracks(&path)}).await{- Ok(Ok(true)) => info!("Healed stretched camera track(s)"),+ Ok(Ok(true)) => info!("Healed stretched video track(s)"),Ok(Ok(false)) => {}- Ok(Err(e)) => warn!("Camera heal check failed: {e:#}"),- Err(e) => warn!("Camera heal task panicked: {e}"),+ Ok(Err(e)) => warn!("Track heal check failed: {e:#}"),+ Err(e) => warn!("Track heal task panicked: {e}"),}}diff --git a/crates/enc-ffmpeg/src/remux.rs b/crates/enc-ffmpeg/src/remux.rsindex 3d8e63195..d9554ba89 100644--- a/crates/enc-ffmpeg/src/remux.rs+++ b/crates/enc-ffmpeg/src/remux.rs@@ -573,6 +573,89 @@ fn get_video_fps_inner(path: &Path) -> Option<u32> {Some((rate.numerator() as f64 / rate.denominator() as f64).round() as u32)}+/// How closely a video track's packet timestamps follow a fixed ladder of+/// `1/nominal_fps` steps. Recordings stamped by the pre-fix pipeline (snap+/// ladder + full-frame monotonic bump) advance exactly one nominal tick per+/// frame, so every delta conforms; genuinely capture-timed tracks jitter and+/// gap. Note a healthy vsync-locked track can also conform — conformance+/// alone does not prove a stretch, it only rules one out.+pub struct LadderProbe {+ pub deltas: usize,+ pub conforming: usize,+}++impl LadderProbe {+ /// True when there is enough evidence and essentially every step matches+ /// the nominal cadence. The slack absorbs pause/resume resyncs, which+ /// leave a handful of oversized steps in an otherwise perfect ladder.+ pub fn is_ladder(&self) -> bool {+ const MIN_DELTAS: usize = 30;+ const MIN_CONFORMANCE: f64 = 0.98;+ self.deltas >= MIN_DELTAS && self.conforming as f64 / self.deltas as f64 >= MIN_CONFORMANCE+ }+}++pub fn probe_video_pts_ladder(path: &Path, nominal_fps: u32) -> Option<LadderProbe> {+ suppress_ffmpeg_logs();+ let result = probe_video_pts_ladder_inner(path, nominal_fps);+ restore_ffmpeg_logs();+ result+}++fn probe_video_pts_ladder_inner(path: &Path, nominal_fps: u32) -> Option<LadderProbe> {+ // 60s at 60fps; enough to judge a track without reading huge files whole.+ const MAX_DELTAS: usize = 3600;++ if nominal_fps == 0 {+ return None;+ }++ let mut ictx = avformat::input(path).ok()?;+ let (stream_index, expected_ticks) = {+ let stream = ictx.streams().best(ffmpeg::media::Type::Video)?;+ let tb = stream.time_base();+ if tb.numerator() <= 0 || tb.denominator() <= 0 {+ return None;+ }+ let ticks =+ (tb.denominator() as f64 / (tb.numerator() as f64 * nominal_fps as f64)).round() as i64;+ if ticks < 1 {+ return None;+ }+ (stream.index(), ticks)+ };++ let mut probe = LadderProbe {+ deltas: 0,+ conforming: 0,+ };+ let mut prev_ts: Option<i64> = None;++ for (stream, packet) in ictx.packets() {+ if stream.index() != stream_index {+ continue;+ }+ let Some(ts) = packet.dts().or(packet.pts()) else {+ continue;+ };+ if let Some(prev) = prev_ts {+ let delta = ts - prev;+ if delta > 0 {+ probe.deltas += 1;+ if (delta - expected_ticks).abs() <= 1 {+ probe.conforming += 1;+ }+ }+ }+ prev_ts = Some(ts);+ if probe.deltas >= MAX_DELTAS {+ break;+ }+ }++ Some(probe)+}+pub fn probe_m4s_can_decode_with_init(init_path: &Path,segment_path: &Path,@@ -873,6 +956,131 @@ fn merge_video_audio_inner(#[cfg(test)]mod tests {use super::build_seek_probe_positions;+ use super::probe_video_pts_ladder;+ use std::time::Duration;++ /// Encodes a small mp4 whose frames are stamped with the given+ /// timestamps, mirroring how recordings reach disk.+ fn encode_test_mp4(dir: &std::path::Path, timestamps: &[Duration]) -> std::path::PathBuf {+ use crate::h264::H264Encoder;+ use crate::mp4::MP4File;+ use cap_media_info::VideoInfo;++ ffmpeg::init().ok();++ let video_info = VideoInfo {+ pixel_format: cap_media_info::Pixel::NV12,+ width: 320,+ height: 240,+ time_base: ffmpeg::Rational(1, 1_000_000),+ frame_rate: ffmpeg::Rational(30, 1),+ };++ let path = dir.join("ladder_probe_test.mp4");+ let mut file = MP4File::init(+ "ladder-probe-test",+ path.clone(),+ false,+ |o| H264Encoder::builder(video_info).build(o),+ |_| None,+ )+ .unwrap();++ for ts in timestamps {+ let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::NV12, 320, 240);+ for plane_idx in 0..frame.planes() {+ for byte in frame.data_mut(plane_idx).iter_mut() {+ *byte = 128;+ }+ }+ file.queue_video_frame(frame, *ts).unwrap();+ }+ file.finish().unwrap();++ dir.join("ladder_probe_test.mp4")+ }++ #[test]+ fn ladder_probe_detects_synthetic_cfr_stamping() {+ let dir = tempfile::tempdir().unwrap();+ // The defective pipeline's output: exactly one nominal tick per+ // frame, regardless of how fast frames really arrived.+ let timestamps: Vec<Duration> = (0..120)+ .map(|i| Duration::from_nanos(i * 1_000_000_000 / 30))+ .collect();+ let path = encode_test_mp4(dir.path(), ×tamps);++ let probe = probe_video_pts_ladder(&path, 30).unwrap();+ assert!(+ probe.is_ladder(),+ "conforming {}/{}",+ probe.conforming,+ probe.deltas+ );+ }++ #[test]+ fn ladder_probe_tolerates_a_pause_gap() {+ let dir = tempfile::tempdir().unwrap();+ // A pause/resume leaves one oversized step in an otherwise perfect+ // ladder; the track is still synthetic.+ let timestamps: Vec<Duration> = (0..120)+ .map(|i| {+ let gap = if i >= 60 { 5_000_000_000u64 } else { 0 };+ Duration::from_nanos(i * 1_000_000_000 / 30 + gap)+ })+ .collect();+ let path = encode_test_mp4(dir.path(), ×tamps);++ let probe = probe_video_pts_ladder(&path, 30).unwrap();+ assert!(+ probe.is_ladder(),+ "conforming {}/{}",+ probe.conforming,+ probe.deltas+ );+ }++ #[test]+ fn ladder_probe_rejects_capture_timed_tracks() {+ let dir = tempfile::tempdir().unwrap();+ // Real capture timestamps jitter by milliseconds around the nominal+ // cadence; deterministic pseudo-jitter stands in for QPC noise.+ let timestamps: Vec<Duration> = (0..120)+ .map(|i| {+ let jitter_us = ((i * 7919) % 7000) as u64; // 0..7ms+ Duration::from_nanos(i * 1_000_000_000 / 30 + jitter_us * 1_000)+ })+ .collect();+ let path = encode_test_mp4(dir.path(), ×tamps);++ let probe = probe_video_pts_ladder(&path, 30).unwrap();+ assert!(+ !probe.is_ladder(),+ "conforming {}/{}",+ probe.conforming,+ probe.deltas+ );+ }++ #[test]+ fn ladder_probe_rejects_faster_uniform_cadence() {+ let dir = tempfile::tempdir().unwrap();+ // A healed (or genuinely high-rate, correctly-timed) track is+ // uniform at a rate other than nominal — not the nominal ladder.+ let timestamps: Vec<Duration> = (0..120)+ .map(|i| Duration::from_nanos(i * 1_000_000_000 / 67))+ .collect();+ let path = encode_test_mp4(dir.path(), ×tamps);++ let probe = probe_video_pts_ladder(&path, 30).unwrap();+ assert!(+ !probe.is_ladder(),+ "conforming {}/{}",+ probe.conforming,+ probe.deltas+ );+ }#[test]fn seek_probe_positions_cover_start_middle_and_end() {diff --git a/crates/recording/src/camera_heal.rs b/crates/recording/src/camera_heal.rsdeleted file mode 100644index 49321a63d..000000000--- a/crates/recording/src/camera_heal.rs+++ /dev/null@@ -1,315 +0,0 @@-//! Load-time repair for recordings whose camera track was stamped at the-//! wrong rate.-//!-//! A camera that delivered frames faster than the pipeline's nominal rate-//! (e.g. an AVFoundation device free-running at 60fps while Cap recorded it-//! as 30fps) produced a camera.mp4 whose duration is a clean multiple of the-//! real recording span — the video plays in slow motion and stretches the-//! editor timeline. The recorder-side defects are fixed, but affected-//! recordings on disk stay broken; this module detects the signature on-//! editor open and losslessly rewrites the camera track's timestamps.--use std::path::{Path, PathBuf};--use cap_enc_ffmpeg::remux::{get_media_duration, get_video_fps, rescale_video_timestamps};-use cap_project::{RecordingMeta, RecordingMetaInner, StudioRecordingMeta};-use tracing::{info, warn};--/// Minimum reference span for a heal to be considered; very short recordings-/// have too much probe noise to trust a ratio.-const MIN_REFERENCE_SECS: f64 = 2.0;-/// Camera must exceed the reference span by at least this factor…-const MIN_STRETCH_RATIO: f64 = 1.5;-/// …and by at least this many seconds, so long recordings with slightly-/// off-by-probe durations never trigger.-const MIN_STRETCH_SECS: f64 = 1.0;-/// When only the display track is available as a reference (no audio), the-/// ratio must sit in the rate-doubling family (60/30, 50/25, 60/24) to rule-/// out a display track that legitimately stopped early.-const DISPLAY_ONLY_RATIO_RANGE: (f64, f64) = (1.6, 2.6);-/// References must agree with each other within this fraction of the larger-/// one before the camera is judged against them.-const REFERENCE_AGREEMENT_RATIO: f64 = 0.15;-/// The healed file must land within this fraction of the expected span or the-/// original is kept untouched.-const HEAL_VERIFY_TOLERANCE_RATIO: f64 = 0.05;--const BACKUP_FILE_NAME: &str = "camera.original.mp4";--/// Detects and repairs stretched camera tracks in a studio recording.-/// Returns `true` if any segment was healed. Never modifies media unless the-/// stretch signature is unambiguous, and keeps the original file alongside-/// the healed one.-pub fn heal_stretched_camera(project_path: &Path) -> anyhow::Result<bool> {- let mut meta = RecordingMeta::load_for_project(project_path)- .map_err(|e| anyhow::anyhow!("failed to load recording meta: {e}"))?;-- let RecordingMetaInner::Studio(studio_meta) = &mut meta.inner else {- return Ok(false);- };-- let StudioRecordingMeta::MultipleSegments { inner } = studio_meta.as_mut() else {- return Ok(false);- };-- let project_path = project_path.to_path_buf();- let mut healed_any = false;-- for (segment_index, segment) in inner.segments.iter_mut().enumerate() {- let Some(camera) = segment.camera.as_mut() else {- continue;- };-- let camera_path = camera.path.to_path(&project_path);- let display_path = segment.display.path.to_path(&project_path);-- let backup_path = backup_path_for(&camera_path);- if backup_path.exists() {- // Healed before — never rescale the same track twice.- continue;- }-- let Some(camera_duration) = get_media_duration(&camera_path) else {- continue;- };-- // Each reference predicts the camera's span from its own duration- // plus the difference in track start times (a track that started- // later ran for less wall time).- let camera_start = camera.start_time.unwrap_or(0.0);- let mut references: Vec<(&'static str, f64)> = Vec::new();-- if let Some(display_duration) = get_media_duration(&display_path) {- let display_start = segment.display.start_time.unwrap_or(0.0);- references.push((- "display",- display_duration.as_secs_f64() + (display_start - camera_start),- ));- }-- for (name, audio) in [("mic", segment.mic.as_ref()), ("system-audio", segment.system_audio.as_ref())] {- let Some(audio) = audio else { continue };- let audio_path = audio.path.to_path(&project_path);- if let Some(audio_duration) = get_media_duration(&audio_path) {- let audio_start = audio.start_time.unwrap_or(0.0);- references.push((- name,- audio_duration.as_secs_f64() + (audio_start - camera_start),- ));- }- }-- let Some(decision) = evaluate_stretch(camera_duration.as_secs_f64(), &references) else {- continue;- };-- info!(- segment_index,- camera_secs = camera_duration.as_secs_f64(),- expected_secs = decision.expected_span_secs,- ratio = decision.ratio,- references = ?references,- "Detected stretched camera track, rewriting timestamps"- );-- let healed_path = camera_path.with_extension("healed.mp4");- let _ = std::fs::remove_file(&healed_path);-- if let Err(e) = rescale_video_timestamps(&camera_path, &healed_path, decision.scale) {- warn!(segment_index, "Camera heal remux failed, keeping original: {e}");- let _ = std::fs::remove_file(&healed_path);- continue;- }-- let healed_ok = get_media_duration(&healed_path).is_some_and(|healed| {- let error = (healed.as_secs_f64() - decision.expected_span_secs).abs();- error <= decision.expected_span_secs * HEAL_VERIFY_TOLERANCE_RATIO- });-- if !healed_ok {- warn!(- segment_index,- "Healed camera track failed duration verification, keeping original"- );- let _ = std::fs::remove_file(&healed_path);- continue;- }-- if let Err(e) = std::fs::rename(&camera_path, &backup_path) {- warn!(segment_index, "Failed to back up original camera track: {e}");- let _ = std::fs::remove_file(&healed_path);- continue;- }-- if let Err(e) = std::fs::rename(&healed_path, &camera_path) {- warn!(segment_index, "Failed to install healed camera track: {e}");- // Put the original back so the recording stays playable.- let _ = std::fs::rename(&backup_path, &camera_path);- let _ = std::fs::remove_file(&healed_path);- continue;- }-- if let Some(fps) = get_video_fps(&camera_path).filter(|fps| *fps > 0) {- camera.fps = fps;- }-- info!(- segment_index,- healed_secs = get_media_duration(&camera_path).map(|d| d.as_secs_f64()),- fps = camera.fps,- "Camera track healed (original kept as {BACKUP_FILE_NAME})"- );- healed_any = true;- }-- if healed_any {- meta.save_for_project()- .map_err(|e| anyhow::anyhow!("failed to save healed recording meta: {e:?}"))?;- }-- Ok(healed_any)-}--fn backup_path_for(camera_path: &Path) -> PathBuf {- camera_path.with_file_name(BACKUP_FILE_NAME)-}--#[derive(Debug, PartialEq)]-struct StretchDecision {- expected_span_secs: f64,- ratio: f64,- scale: f64,-}--/// Decides whether `camera_secs` is a stretched track given the expected-/// spans predicted by the other tracks. Returns `None` unless the signature-/// is unambiguous.-fn evaluate_stretch(camera_secs: f64, references: &[(&str, f64)]) -> Option<StretchDecision> {- let spans: Vec<f64> = references.iter().map(|(_, span)| *span).collect();- if spans.is_empty() {- return None;- }-- let max_span = spans.iter().cloned().fold(f64::MIN, f64::max);- let min_span = spans.iter().cloned().fold(f64::MAX, f64::min);-- if max_span < MIN_REFERENCE_SECS {- return None;- }-- // All references must tell the same story; a mic that ran much longer- // than the display means the display died early, not that the camera is- // stretched.- if max_span - min_span > (max_span * REFERENCE_AGREEMENT_RATIO).max(MIN_STRETCH_SECS) {- return None;- }-- let expected_span_secs = max_span;- let ratio = camera_secs / expected_span_secs;-- if ratio < MIN_STRETCH_RATIO || camera_secs - expected_span_secs < MIN_STRETCH_SECS {- return None;- }-- let has_audio_reference = references.iter().any(|(name, _)| *name != "display");- if !has_audio_reference- && !(DISPLAY_ONLY_RATIO_RANGE.0..=DISPLAY_ONLY_RATIO_RANGE.1).contains(&ratio)- {- return None;- }-- Some(StretchDecision {- expected_span_secs,- ratio,- scale: expected_span_secs / camera_secs,- })-}--#[cfg(test)]-mod tests {- use super::*;-- const USER_REPORT_CAMERA_SECS: f64 = 13.666;-- fn user_report_references() -> Vec<(&'static str, f64)> {- // From the original report: display 6.75s starting at 0.302s, mic- // 6.91s starting at 0.102s, camera starting at 0.149s.- vec![- ("display", 6.75 + (0.302 - 0.149)),- ("mic", 6.91 + (0.102 - 0.149)),- ]- }-- #[test]- fn detects_the_reported_double_length_camera() {- let decision = evaluate_stretch(USER_REPORT_CAMERA_SECS, &user_report_references())- .expect("the reported recording must be detected");- assert!((decision.ratio - 2.0).abs() < 0.05, "ratio {}", decision.ratio);- assert!((decision.scale - 0.5).abs() < 0.02, "scale {}", decision.scale);- }-- #[test]- fn healthy_recording_is_left_alone() {- assert_eq!(- evaluate_stretch(6.9, &[("display", 6.75), ("mic", 6.91)]),- None- );- }-- #[test]- fn camera_slightly_longer_than_references_is_left_alone() {- // Cameras legitimately run a little past the other tracks.- assert_eq!(- evaluate_stretch(7.4, &[("display", 6.75), ("mic", 6.91)]),- None- );- }-- #[test]- fn display_dying_early_does_not_trigger_heal() {- // Display stopped at half-time but the mic kept going: references- // disagree, so the long camera is trusted.- assert_eq!(- evaluate_stretch(60.0, &[("display", 30.0), ("mic", 59.5)]),- None- );- }-- #[test]- fn display_only_requires_doubling_ratio() {- // Without audio, a 2x camera is healed…- assert!(evaluate_stretch(13.6, &[("display", 6.8)]).is_some());- // …but a 3x mismatch (not a rate-doubling signature) is not.- assert_eq!(evaluate_stretch(20.4, &[("display", 6.8)]), None);- // …nor is a display that died at two-thirds.- assert_eq!(evaluate_stretch(90.0, &[("display", 60.0)]), None);- }-- #[test]- fn short_recordings_are_not_healed() {- assert_eq!(- evaluate_stretch(3.0, &[("display", 1.5), ("mic", 1.5)]),- None- );- }-- #[test]- fn below_ratio_threshold_is_left_alone() {- assert_eq!(- evaluate_stretch(3.1, &[("display", 2.1), ("mic", 2.1)]),- None- );- }-- /// Manual harness for healing a real .cap bundle:- /// `CAP_HEAL_PROJECT_PATH=/path/to/recording.cap cargo test -p cap-recording \- /// --lib camera_heal::tests::heal_project_from_env -- --ignored --nocapture`- #[test]- #[ignore]- fn heal_project_from_env() {- let path =- std::env::var("CAP_HEAL_PROJECT_PATH").expect("CAP_HEAL_PROJECT_PATH must be set");- let healed = heal_stretched_camera(Path::new(&path)).unwrap();- println!("healed: {healed}");- }-}diff --git a/crates/recording/src/lib.rs b/crates/recording/src/lib.rsindex b622d5121..d851c1005 100644--- a/crates/recording/src/lib.rs+++ b/crates/recording/src/lib.rs@@ -1,5 +1,4 @@pub mod benchmark;-pub mod camera_heal;mod capture_pipeline;pub mod cursor;pub mod defaults;@@ -16,6 +15,7 @@ pub mod screenshot;pub mod sources;pub mod studio_recording;pub mod sync_calibration;+pub mod track_heal;pub use resolution_limits::{H264_MAX_DIMENSION, calculate_gpu_compatible_size};diff --git a/crates/recording/src/sources/screen_capture/cadence.rs b/crates/recording/src/sources/screen_capture/cadence.rsnew file mode 100644index 000000000..46adf4c95--- /dev/null+++ b/crates/recording/src/sources/screen_capture/cadence.rs@@ -0,0 +1,254 @@+//! Caps a capture source's delivered frame rate at the pipeline's nominal+//! rate.+//!+//! Windows Graphics Capture delivers a frame for every screen update — up to+//! the monitor's refresh rate — and `MinUpdateInterval` is only honoured on+//! newer Windows builds. On a 165Hz monitor a nominally-60fps recording+//! otherwise encodes ~2.75x the frames it needs: the timestamps are real (so+//! timing stays correct), but file size, encode load, and editor decode load+//! all scale with the excess.+//!+//! The gate has two modes chosen by an estimate of the source's own cadence:+//!+//! - **Pass-through** while the source runs at or below the nominal rate.+//! No frame is ever dropped in this mode, so ordinary monitors — including+//! 59.94Hz ones and sources with timestamp jitter — are untouched by+//! construction. This is deliberate: a per-frame spacing threshold either+//! drops frames on slightly-slow sources (accumulated drift) or lets+//! near-nominal multiples through (a 120Hz source quantizes to 120fps),+//! so rate limiting only ever engages when the source is measurably fast.+//! - **Decimation** once the source is clearly faster than nominal: frames+//! are admitted against a deadline ladder that advances one nominal+//! interval per admitted frame, which converges on exactly the nominal+//! rate for any source rate above it.++/// Smoothing factor for the source-cadence estimate.+const EMA_ALPHA: f64 = 0.1;+/// Enter decimation when the estimated source interval falls below this+/// fraction of nominal (i.e. the source is >~14% fast). Gap to the exit+/// ratio provides hysteresis so refresh-rate jitter cannot flap the mode.+const ENTER_DECIMATION_RATIO: f64 = 0.88;+/// Leave decimation when the source slows back to within ~5% of nominal.+const EXIT_DECIMATION_RATIO: f64 = 0.95;++pub struct FrameCadenceGate {+ /// Nominal frame interval in whatever tick unit `admit` is fed+ /// (the Windows capture path uses QPC hundred-nanosecond units).+ nominal_interval: i64,+ /// EMA of inter-frame deltas; `None` until the second frame.+ ema_delta: Option<f64>,+ last_ts: Option<i64>,+ decimating: bool,+ next_due: i64,+}++impl FrameCadenceGate {+ pub fn new(nominal_interval: i64) -> Self {+ Self {+ nominal_interval: nominal_interval.max(1),+ ema_delta: None,+ last_ts: None,+ decimating: false,+ next_due: 0,+ }+ }++ /// Decides whether a frame captured at `ts` should be kept. Call with+ /// every delivered frame, in delivery order.+ pub fn admit(&mut self, ts: i64) -> bool {+ let interval = self.nominal_interval;++ if let Some(last) = self.last_ts {+ let delta = ts - last;+ if delta <= 0 {+ // Non-monotonic source clock: never drop on bad data.+ // Re-anchor the ladder and let the pipeline's own anomaly+ // handling deal with the timestamps.+ self.last_ts = Some(ts);+ self.next_due = ts + interval;+ return true;+ }+ // Idle gaps are absence of updates, not cadence; keep them out+ // of the estimate so one static stretch doesn't mask a fast+ // source (and vice versa).+ if delta < interval * 2 {+ let d = delta as f64;+ self.ema_delta = Some(match self.ema_delta {+ Some(ema) => ema * (1.0 - EMA_ALPHA) + d * EMA_ALPHA,+ None => d,+ });+ }+ }+ self.last_ts = Some(ts);++ let nominal = interval as f64;+ if let Some(ema) = self.ema_delta {+ if self.decimating {+ if ema > nominal * EXIT_DECIMATION_RATIO {+ self.decimating = false;+ }+ } else if ema < nominal * ENTER_DECIMATION_RATIO {+ self.decimating = true;+ // Start the ladder at this frame so it is admitted.+ self.next_due = ts;+ }+ }++ if !self.decimating {+ return true;+ }++ if ts >= self.next_due {+ // Advance from the deadline, not the admitted timestamp: dense+ // fast sources always overshoot the deadline a little, and+ // anchoring to the frame would compound that overshoot into an+ // admitted rate below nominal. Only a genuine idle gap (a full+ // interval past due) re-anchors the ladder.+ self.next_due = if ts >= self.next_due + interval {+ ts + interval+ } else {+ self.next_due + interval+ };+ true+ } else {+ false+ }+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ /// Ticks here are microseconds for readability; the gate is+ /// unit-agnostic.+ const NOMINAL_60FPS: i64 = 16_667;++ fn run_source(gate: &mut FrameCadenceGate, timestamps: &[i64]) -> Vec<i64> {+ timestamps+ .iter()+ .copied()+ .filter(|ts| gate.admit(*ts))+ .collect()+ }++ /// Deterministic pseudo-jitter in `-max..=max`.+ fn jitter(i: i64, max: i64) -> i64 {+ ((i * 7919 + 104_729) % (2 * max + 1)) - max+ }++ #[test]+ fn nominal_rate_source_is_never_dropped() {+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ let timestamps: Vec<i64> = (0..2000)+ .map(|i| i * NOMINAL_60FPS + if i > 0 { jitter(i, 2_000) } else { 0 })+ .collect();+ let admitted = run_source(&mut gate, ×tamps);+ assert_eq!(admitted.len(), timestamps.len());+ }++ #[test]+ fn slightly_slow_source_is_never_dropped() {+ // A 59.94Hz monitor feeding a 60fps nominal pipeline: the classic+ // failure of deadline ladders is accumulating drift against this+ // source and dropping a frame every few seconds.+ let interval_5994 = 16_683;+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ let timestamps: Vec<i64> = (0..10_000).map(|i| i * interval_5994).collect();+ let admitted = run_source(&mut gate, ×tamps);+ assert_eq!(admitted.len(), timestamps.len());+ }++ #[test]+ fn slower_content_is_never_dropped() {+ // 30fps content updates on any monitor.+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ let timestamps: Vec<i64> = (0..500).map(|i| i * 33_333).collect();+ let admitted = run_source(&mut gate, ×tamps);+ assert_eq!(admitted.len(), timestamps.len());+ }++ fn assert_decimates_to_nominal(source_interval: i64, label: &str) {+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ let n = 20_000i64;+ let timestamps: Vec<i64> = (0..n)+ .map(|i| {+ i * source_interval+ + if i > 0 {+ jitter(i, source_interval / 20)+ } else {+ 0+ }+ })+ .collect();+ let admitted = run_source(&mut gate, ×tamps);++ let span = (timestamps.last().unwrap() - timestamps[0]) as f64;+ let admitted_rate = admitted.len() as f64 / span;+ let nominal_rate = 1.0 / NOMINAL_60FPS as f64;+ let ratio = admitted_rate / nominal_rate;+ assert!(+ (0.9..=1.02).contains(&ratio),+ "{label}: admitted {} of {} → {:.3}x nominal",+ admitted.len(),+ timestamps.len(),+ ratio+ );+ }++ #[test]+ fn fast_sources_decimate_to_the_nominal_rate() {+ assert_decimates_to_nominal(6_061, "165Hz"); // the reported monitor+ assert_decimates_to_nominal(6_944, "144Hz");+ assert_decimates_to_nominal(8_333, "120Hz");+ assert_decimates_to_nominal(13_333, "75Hz"); // per-frame thresholds miss this one+ assert_decimates_to_nominal(4_167, "240Hz");+ }++ #[test]+ fn first_frame_after_idle_is_admitted_immediately() {+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ // Animate fast enough to enter decimation…+ let mut timestamps: Vec<i64> = (0..200).map(|i| i * 6_061).collect();+ // …then go idle for five seconds and update once.+ let idle_end = timestamps.last().unwrap() + 5_000_000;+ timestamps.push(idle_end);+ let admitted = run_source(&mut gate, ×tamps);+ assert_eq!(+ admitted.last().copied(),+ Some(idle_end),+ "the update ending an idle stretch must not wait out stale deadlines"+ );+ }++ #[test]+ fn mode_follows_content_rate_changes() {+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);++ // Static-ish 60Hz content: everything passes.+ let phase1: Vec<i64> = (0..200).map(|i| i * NOMINAL_60FPS).collect();+ let admitted1 = run_source(&mut gate, &phase1);+ assert_eq!(admitted1.len(), phase1.len());++ // Animation at 165Hz: decimation engages within a few frames.+ let start = phase1.last().unwrap() + 6_061;+ let phase2: Vec<i64> = (0..2000).map(|i| start + i * 6_061).collect();+ let admitted2 = run_source(&mut gate, &phase2);+ let ratio = admitted2.len() as f64 / phase2.len() as f64;+ assert!(+ ratio < 0.45,+ "expected heavy decimation at 165Hz, admitted {ratio:.2}"+ );+ }++ #[test]+ fn non_monotonic_timestamps_are_admitted() {+ let mut gate = FrameCadenceGate::new(NOMINAL_60FPS);+ assert!(gate.admit(100_000));+ assert!(gate.admit(50_000), "backwards clock must never drop frames");+ assert!(+ gate.admit(50_000),+ "equal timestamps must never drop frames"+ );+ }+}diff --git a/crates/recording/src/sources/screen_capture/mod.rs b/crates/recording/src/sources/screen_capture/mod.rsindex edb12b56f..a78474184 100644--- a/crates/recording/src/sources/screen_capture/mod.rs+++ b/crates/recording/src/sources/screen_capture/mod.rs@@ -8,6 +8,8 @@ use specta::Type;use std::time::SystemTime;use tracing::*;+pub mod cadence;+#[cfg(target_os = "windows")]mod windows;#[cfg(target_os = "windows")]diff --git a/crates/recording/src/sources/screen_capture/windows.rs b/crates/recording/src/sources/screen_capture/windows.rsindex e8286d414..a1f8f5171 100644--- a/crates/recording/src/sources/screen_capture/windows.rs+++ b/crates/recording/src/sources/screen_capture/windows.rs@@ -1,6 +1,6 @@use crate::{AudioFrame, SetupCtx, output_pipeline,- screen_capture::{ScreenCaptureConfig, ScreenCaptureFormat},+ screen_capture::{ScreenCaptureConfig, ScreenCaptureFormat, cadence::FrameCadenceGate},};use ::windows::Win32::Graphics::Direct3D11::{D3D11_BIND_SHADER_RESOURCE, D3D11_BOX, D3D11_SUBRESOURCE_DATA, D3D11_TEXTURE2D_DESC,@@ -465,6 +465,12 @@ struct CreateCapturerParams<'a> {video_tx: &'a mpsc::Sender<VideoFrame>,video_frame_counter: &'a Arc<AtomicU32>,video_drop_counter: &'a Arc<AtomicU32>,+ /// Decimated by the cadence gate: expected on high-refresh monitors,+ /// tracked separately from drops so it never trips drop-rate health.+ video_decimated_counter: &'a Arc<AtomicU32>,+ /// Nominal frame interval in QPC hundred-nanosecond units; `None`+ /// disables rate capping.+ cadence_interval_hns: Option<i64>,expected_width: u32,expected_height: u32,frame_scaler: Arc<Mutex<WindowsFrameScaler>>,@@ -489,6 +495,8 @@ fn create_d3d_capturer({let video_frame_counter = params.video_frame_counter.clone();let video_drop_counter = params.video_drop_counter.clone();+ let video_decimated_counter = params.video_decimated_counter.clone();+ let mut cadence_gate = params.cadence_interval_hns.map(FrameCadenceGate::new);let mut tx = params.video_tx.clone();let expected_width = params.expected_width;let expected_height = params.expected_height;@@ -497,9 +505,21 @@ fn create_d3d_capturer(let scaled_frame_count = params.scaled_frame_count.clone();let stall_health_tx = params.stall_health_tx.clone();move |frame| {- let timestamp = frame.inner().SystemRelativeTime()?;+ let capture_time = frame.inner().SystemRelativeTime()?;++ // WGC delivers a frame per screen update — up to the monitor+ // refresh rate on systems without MinUpdateInterval support —+ // so cap delivery at the nominal rate before any conversion+ // or channel work.+ if let Some(gate) = cadence_gate.as_mut()+ && !gate.admit(capture_time.Duration)+ {+ video_decimated_counter.fetch_add(1, atomic::Ordering::Relaxed);+ return Ok(());+ }+let timestamp = Timestamp::PerformanceCounter(PerformanceCounterTimestamp::new(- timestamp.Duration,+ capture_time.Duration,));let frame_width = frame.width();@@ -627,6 +647,18 @@ impl output_pipeline::VideoSource for VideoSource {let scaling_logged: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));let scaled_frame_count: Arc<AtomicU32> = Arc::new(AtomicU32::new(0));+ // Belt-and-braces alongside WGC's MinUpdateInterval: on systems where+ // that API is unsupported (or silently ineffective) the gate keeps+ // high-refresh monitors from recording far more frames than the+ // nominal rate. For sources at or below nominal it admits everything.+ let cadence_interval_hns = if std::env::var("CAP_DISABLE_CAPTURE_CADENCE").is_ok() {+ info!("Capture cadence gate disabled via CAP_DISABLE_CAPTURE_CADENCE");+ None+ } else {+ let fps = video_info.fps();+ (fps > 0).then(|| 10_000_000i64 / fps as i64)+ };+let stats_health_tx = ctx.health_tx().clone();ctx.tasks().spawn_thread("d3d-capture-thread", {let restart_counter = restart_counter.clone();@@ -639,6 +671,7 @@ impl output_pipeline::VideoSource for VideoSource {let video_frame_counter: Arc<AtomicU32> = Arc::new(AtomicU32::new(0));let video_drop_counter: Arc<AtomicU32> = Arc::new(AtomicU32::new(0));+ let video_decimated_counter: Arc<AtomicU32> = Arc::new(AtomicU32::new(0));let cancel_token = CancellationToken::new();let mut error_tx = error_tx;let mut d3d_device = d3d_device;@@ -652,6 +685,8 @@ impl output_pipeline::VideoSource for VideoSource {video_tx: &video_tx,video_frame_counter: &video_frame_counter,video_drop_counter: &video_drop_counter,+ video_decimated_counter: &video_decimated_counter,+ cadence_interval_hns,expected_width,expected_height,frame_scaler: frame_scaler.clone(),@@ -682,6 +717,7 @@ impl output_pipeline::VideoSource for VideoSource {{let video_frame_counter = video_frame_counter.clone();let video_drop_counter = video_drop_counter.clone();+ let video_decimated_counter = video_decimated_counter.clone();let restart_counter = restart_counter.clone();let scaled_frame_count = scaled_frame_count.clone();let stats_health_tx = stats_health_tx.clone();@@ -690,8 +726,13 @@ impl output_pipeline::VideoSource for VideoSource {tokio::time::sleep(Duration::from_secs(5)).await;let captured = video_frame_counter.load(atomic::Ordering::Relaxed);let dropped = video_drop_counter.load(atomic::Ordering::Relaxed);+ let decimated =+ video_decimated_counter.load(atomic::Ordering::Relaxed);let restarts = restart_counter.load(atomic::Ordering::Relaxed);let scaled = scaled_frame_count.load(atomic::Ordering::Relaxed);+ // Decimated frames are the cadence gate doing+ // its job on a high-refresh monitor, not a+ // health problem; keep them out of drop rate.let total = captured + dropped;if dropped > 0 || restarts > 0 || scaled > 0 {let drop_pct = if total > 0 {@@ -703,6 +744,7 @@ impl output_pipeline::VideoSource for VideoSource {captured = captured,dropped = dropped,drop_pct = format!("{:.1}%", drop_pct),+ decimated = decimated,restarts = restarts,scaled_frames = scaled,"Screen capture stats"@@ -717,7 +759,11 @@ impl output_pipeline::VideoSource for VideoSource {);}} else {- debug!(captured = captured, "Screen capture frames");+ debug!(+ captured = captured,+ decimated = decimated,+ "Screen capture frames"+ );}}}diff --git a/crates/recording/src/track_heal.rs b/crates/recording/src/track_heal.rsnew file mode 100644index 000000000..c156714bc--- /dev/null+++ b/crates/recording/src/track_heal.rs@@ -0,0 +1,1135 @@+//! Load-time repair for recordings whose video tracks were stamped at the+//! wrong rate.+//!+//! A source that delivered frames faster than the pipeline's nominal rate+//! produced a track whose duration is a multiple of the real recording span —+//! the video plays in slow motion, desyncs from audio, and stretches the+//! editor timeline. Two variants shipped:+//!+//! - cameras free-running past their nominal rate (e.g. an AVFoundation+//! device at 60fps recorded as 30fps) → stretched camera.mp4;+//! - Windows screen capture on high-refresh monitors (WGC delivering at up+//! to the monitor rate with no MinUpdateInterval support) → stretched+//! display.mp4, with a content-dependent non-integer ratio+//! (delivered fps / nominal fps, e.g. ~2.24x for a 165Hz monitor).+//!+//! The recorder-side defects are fixed, but affected recordings on disk stay+//! broken; this module detects the signatures on editor open and losslessly+//! rewrites the tracks' timestamps.++use std::collections::HashMap;+use std::path::{Path, PathBuf};++use cap_enc_ffmpeg::remux::{+ get_media_duration, get_video_fps, probe_video_pts_ladder, rescale_video_timestamps,+};+use cap_project::{ProjectConfiguration, RecordingMeta, RecordingMetaInner, StudioRecordingMeta};+use tracing::{info, warn};++/// Runs every track repair for a studio recording, display first so the+/// camera heal can trust the display track as a reference. Returns `true`+/// if anything was healed.+pub fn heal_stretched_tracks(project_path: &Path) -> anyhow::Result<bool> {+ // Concurrent editor opens of the same project would race the+ // backup/install renames and could bury the original under an+ // already-healed file; only one heal per project may be in flight.+ // The app is single-instance and only the editor-open path heals, so+ // an in-process guard suffices.+ let Some(_guard) = HealInFlightGuard::try_claim(project_path) else {+ return Ok(false);+ };++ let display_healed = heal_stretched_display(project_path)?;+ let camera_healed = heal_stretched_camera(project_path)?;+ Ok(display_healed || camera_healed)+}++struct HealInFlightGuard(PathBuf);++impl HealInFlightGuard {+ fn in_flight() -> &'static std::sync::Mutex<std::collections::HashSet<PathBuf>> {+ static IN_FLIGHT: std::sync::OnceLock<+ std::sync::Mutex<std::collections::HashSet<PathBuf>>,+ > = std::sync::OnceLock::new();+ IN_FLIGHT.get_or_init(Default::default)+ }++ fn try_claim(project_path: &Path) -> Option<Self> {+ let path = project_path.to_path_buf();+ let mut set = Self::in_flight().lock().ok()?;+ set.insert(path.clone()).then(|| Self(path))+ }+}++impl Drop for HealInFlightGuard {+ fn drop(&mut self) {+ if let Ok(mut set) = Self::in_flight().lock() {+ set.remove(&self.0);+ }+ }+}++/// Minimum reference span for a heal to be considered; very short recordings+/// have too much probe noise to trust a ratio.+const MIN_REFERENCE_SECS: f64 = 2.0;+/// Camera must exceed the reference span by at least this factor…+const MIN_STRETCH_RATIO: f64 = 1.5;+/// …and by at least this many seconds, so long recordings with slightly+/// off-by-probe durations never trigger.+const MIN_STRETCH_SECS: f64 = 1.0;+/// When only the display track is available as a reference (no audio), the+/// ratio must sit in the rate-doubling family (60/30, 50/25, 60/24) to rule+/// out a display track that legitimately stopped early.+const DISPLAY_ONLY_RATIO_RANGE: (f64, f64) = (1.6, 2.6);+/// References must agree with each other within this fraction of the larger+/// one before the camera is judged against them.+const REFERENCE_AGREEMENT_RATIO: f64 = 0.15;+/// The healed file must land within this fraction of the expected span or the+/// original is kept untouched.+const HEAL_VERIFY_TOLERANCE_RATIO: f64 = 0.05;++const BACKUP_FILE_NAME: &str = "camera.original.mp4";++/// Detects and repairs stretched camera tracks in a studio recording.+/// Returns `true` if any segment was healed. Never modifies media unless the+/// stretch signature is unambiguous, and keeps the original file alongside+/// the healed one.+pub fn heal_stretched_camera(project_path: &Path) -> anyhow::Result<bool> {+ let mut meta = RecordingMeta::load_for_project(project_path)+ .map_err(|e| anyhow::anyhow!("failed to load recording meta: {e}"))?;++ let RecordingMetaInner::Studio(studio_meta) = &mut meta.inner else {+ return Ok(false);+ };++ let StudioRecordingMeta::MultipleSegments { inner } = studio_meta.as_mut() else {+ return Ok(false);+ };++ let project_path = project_path.to_path_buf();+ let mut healed_any = false;++ for (segment_index, segment) in inner.segments.iter_mut().enumerate() {+ let Some(camera) = segment.camera.as_mut() else {+ continue;+ };++ let camera_path = camera.path.to_path(&project_path);+ let display_path = segment.display.path.to_path(&project_path);++ let backup_path = backup_path_for(&camera_path);+ if backup_path.exists() {+ // Healed before — never rescale the same track twice.+ continue;+ }++ let Some(camera_duration) = get_media_duration(&camera_path) else {+ continue;+ };++ // Each reference predicts the camera's span from its own duration+ // plus the difference in track start times (a track that started+ // later ran for less wall time).+ let camera_start = camera.start_time.unwrap_or(0.0);+ let mut references: Vec<(&'static str, f64)> = Vec::new();++ if let Some(display_duration) = get_media_duration(&display_path) {+ let display_start = segment.display.start_time.unwrap_or(0.0);+ references.push((+ "display",+ display_duration.as_secs_f64() + (display_start - camera_start),+ ));+ }++ for (name, audio) in [+ ("mic", segment.mic.as_ref()),+ ("system-audio", segment.system_audio.as_ref()),+ ] {+ let Some(audio) = audio else { continue };+ let audio_path = audio.path.to_path(&project_path);+ if let Some(audio_duration) = get_media_duration(&audio_path) {+ let audio_start = audio.start_time.unwrap_or(0.0);+ references.push((+ name,+ audio_duration.as_secs_f64() + (audio_start - camera_start),+ ));+ }+ }++ let Some(decision) = evaluate_stretch(camera_duration.as_secs_f64(), &references) else {+ continue;+ };++ info!(+ segment_index,+ camera_secs = camera_duration.as_secs_f64(),+ expected_secs = decision.expected_span_secs,+ ratio = decision.ratio,+ references = ?references,+ "Detected stretched camera track, rewriting timestamps"+ );++ let healed_path = camera_path.with_extension("healed.mp4");+ let _ = std::fs::remove_file(&healed_path);++ if let Err(e) = rescale_video_timestamps(&camera_path, &healed_path, decision.scale) {+ warn!(+ segment_index,+ "Camera heal remux failed, keeping original: {e}"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ let healed_ok = get_media_duration(&healed_path).is_some_and(|healed| {+ let error = (healed.as_secs_f64() - decision.expected_span_secs).abs();+ error <= decision.expected_span_secs * HEAL_VERIFY_TOLERANCE_RATIO+ });++ if !healed_ok {+ warn!(+ segment_index,+ "Healed camera track failed duration verification, keeping original"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ if let Err(e) = std::fs::rename(&camera_path, &backup_path) {+ warn!(+ segment_index,+ "Failed to back up original camera track: {e}"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ if let Err(e) = std::fs::rename(&healed_path, &camera_path) {+ warn!(segment_index, "Failed to install healed camera track: {e}");+ // Put the original back so the recording stays playable.+ let _ = std::fs::rename(&backup_path, &camera_path);+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ if let Some(fps) = get_video_fps(&camera_path).filter(|fps| *fps > 0) {+ camera.fps = fps;+ }++ info!(+ segment_index,+ healed_secs = get_media_duration(&camera_path).map(|d| d.as_secs_f64()),+ fps = camera.fps,+ "Camera track healed (original kept as {BACKUP_FILE_NAME})"+ );+ healed_any = true;+ }++ if healed_any {+ meta.save_for_project()+ .map_err(|e| anyhow::anyhow!("failed to save healed recording meta: {e:?}"))?;+ }++ Ok(healed_any)+}++fn backup_path_for(camera_path: &Path) -> PathBuf {+ camera_path.with_file_name(BACKUP_FILE_NAME)+}++const DISPLAY_BACKUP_FILE_NAME: &str = "display.original.mp4";+/// The display ratio is content-dependent (delivered fps / nominal), so no+/// ratio-family gate applies; instead the floor is low enough to catch a+/// 75Hz monitor (1.25x) while staying above audio-tail noise.+const DISPLAY_MIN_STRETCH_RATIO: f64 = 1.15;+/// Absolute stretch floor, so long recordings with slightly off-by-probe+/// durations never trigger.+const DISPLAY_MIN_STRETCH_SECS: f64 = 2.0;+/// No real refresh/nominal mismatch exceeds this; anything above means a+/// different defect we should not touch.+const DISPLAY_MAX_STRETCH_RATIO: f64 = 6.0;+/// Video cannot run longer than the wall clock; requiring it to exceed the+/// log-derived wall span (which over-counts by a few seconds of pre/post+/// roll) by this factor is the physical proof of a stretch. It also rejects+/// the nastiest false positive: a healthy recording whose audio track died+/// early, which an audio-only ratio would mis-read as a stretched video.+const WALL_GATE_RATIO: f64 = 1.1;+/// An audio reference predicting a content span below this fraction of the+/// wall span is treated as a track that died early, not as a sync target.+const AUDIO_REF_PLAUSIBLE_WALL_FRACTION: f64 = 0.4;++/// Detects and repairs stretched display tracks in a studio recording.+/// Returns `true` if any segment was healed. The display is the timeline's+/// backbone, so this is deliberately more conservative than the camera heal:+/// it requires the synthetic CFR-ladder signature that the defective+/// pipeline always produced, plus either wall-clock proof from the recording+/// log or two independently-agreeing audio references.+pub fn heal_stretched_display(project_path: &Path) -> anyhow::Result<bool> {+ let meta = RecordingMeta::load_for_project(project_path)+ .map_err(|e| anyhow::anyhow!("failed to load recording meta: {e}"))?;++ let RecordingMetaInner::Studio(studio_meta) = &meta.inner else {+ return Ok(false);+ };++ let StudioRecordingMeta::MultipleSegments { inner } = studio_meta.as_ref() else {+ return Ok(false);+ };++ let wall_span_secs = recording_log_wall_span_secs(project_path);+ let project_path = project_path.to_path_buf();+ let mut healed_scales: HashMap<u32, f64> = HashMap::new();++ for (segment_index, segment) in inner.segments.iter().enumerate() {+ let display_path = segment.display.path.to_path(&project_path);++ let backup_path = display_path.with_file_name(DISPLAY_BACKUP_FILE_NAME);+ if backup_path.exists() {+ // Healed before — never rescale the same track twice.+ continue;+ }++ let Some(display_duration) = get_media_duration(&display_path) else {+ continue;+ };+ let display_secs = display_duration.as_secs_f64();+ let display_start = segment.display.start_time.unwrap_or(0.0);++ let mut audio_refs: Vec<(&'static str, f64)> = Vec::new();+ let mut audio_track_count = 0usize;+ for (name, audio) in [+ ("mic", segment.mic.as_ref()),+ ("system-audio", segment.system_audio.as_ref()),+ ] {+ let Some(audio) = audio else { continue };+ audio_track_count += 1;+ let audio_path = audio.path.to_path(&project_path);+ if let Some(audio_duration) = get_media_duration(&audio_path) {+ let audio_start = audio.start_time.unwrap_or(0.0);+ audio_refs.push((+ name,+ audio_duration.as_secs_f64() + (audio_start - display_start),+ ));+ }+ }++ // The ladder probe reads packets, so run the cheap gates through+ // `evaluate_display_stretch` first with a claimed ladder, then+ // confirm against the file before touching anything.+ let inputs = DisplayStretchInputs {+ display_secs,+ display_start,+ audio_refs: audio_refs.clone(),+ audio_track_count,+ wall_span_secs,+ ladder_is_synthetic: true,+ };+ let Some(decision) = evaluate_display_stretch(&inputs) else {+ continue;+ };++ let ladder_confirmed = probe_video_pts_ladder(&display_path, segment.display.fps)+ .is_some_and(|probe| probe.is_ladder());+ if !ladder_confirmed {+ info!(+ segment_index,+ display_secs,+ "Display track is long relative to references but not ladder-stamped; leaving it alone"+ );+ continue;+ }++ info!(+ segment_index,+ display_secs,+ expected_secs = decision.expected_span_secs,+ ratio = decision.ratio,+ wall_span_secs,+ references = ?audio_refs,+ "Detected stretched display track, rewriting timestamps"+ );++ let healed_path = display_path.with_extension("healed.mp4");+ let _ = std::fs::remove_file(&healed_path);++ if let Err(e) = rescale_video_timestamps(&display_path, &healed_path, decision.scale) {+ warn!(+ segment_index,+ "Display heal remux failed, keeping original: {e}"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ let healed_ok = get_media_duration(&healed_path).is_some_and(|healed| {+ let error = (healed.as_secs_f64() - decision.expected_span_secs).abs();+ error <= decision.expected_span_secs * HEAL_VERIFY_TOLERANCE_RATIO+ });++ if !healed_ok {+ warn!(+ segment_index,+ "Healed display track failed duration verification, keeping original"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ if let Err(e) = std::fs::rename(&display_path, &backup_path) {+ warn!(+ segment_index,+ "Failed to back up original display track: {e}"+ );+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ if let Err(e) = std::fs::rename(&healed_path, &display_path) {+ warn!(segment_index, "Failed to install healed display track: {e}");+ // Put the original back so the recording stays playable.+ let _ = std::fs::rename(&backup_path, &display_path);+ let _ = std::fs::remove_file(&healed_path);+ continue;+ }++ info!(+ segment_index,+ healed_secs = get_media_duration(&display_path).map(|d| d.as_secs_f64()),+ "Display track healed (original kept as {DISPLAY_BACKUP_FILE_NAME})"+ );+ healed_scales.insert(segment_index as u32, decision.scale);+ }++ if !healed_scales.is_empty() {+ rescale_project_config(&project_path, &healed_scales);+ }++ Ok(!healed_scales.is_empty())+}++/// Wall-clock span of the recording session, from the first and last+/// timestamped lines of `recording-logs.log`. Over-counts the capture span+/// by a few seconds (setup before the pipeline plays, finalization after+/// stop), which is the safe direction for a "video exceeds wall time" gate.+///+/// Log timestamps are system wall-clock, not monotonic: if the clock+/// stepped backward mid-recording (NTP correction), the net span+/// under-counts and could authorize — and mis-target — a heal of a healthy+/// no-audio recording. A backward step is visible as a backwards jump+/// between adjacent log lines, so any such jump discredits the whole log.+/// Small inversions are normal (threads capture timestamps before the+/// appender serializes them) and are tolerated.+fn recording_log_wall_span_secs(project_path: &Path) -> Option<f64> {+ // Logs are tens of KB; cap the read defensively rather than streaming.+ const MAX_LOG_READ_BYTES: u64 = 8 * 1024 * 1024;+ /// Well above async-logging interleave, well below any real clock step.+ const MAX_BACKWARD_INTERLEAVE_MS: i64 = 2_000;++ let log_path = project_path.join("recording-logs.log");+ let len = std::fs::metadata(&log_path).ok()?.len();+ if len == 0 || len > MAX_LOG_READ_BYTES {+ return None;+ }+ let contents = std::fs::read_to_string(&log_path).ok()?;++ let mut first: Option<chrono::DateTime<chrono::FixedOffset>> = None;+ let mut prev: Option<chrono::DateTime<chrono::FixedOffset>> = None;+ for line in contents.lines() {+ let Some(ts_str) = line.split_whitespace().next() else {+ continue;+ };+ let Ok(ts) = chrono::DateTime::parse_from_rfc3339(ts_str) else {+ continue;+ };+ if let Some(prev) = prev+ && prev.signed_duration_since(ts).num_milliseconds() > MAX_BACKWARD_INTERLEAVE_MS+ {+ return None;+ }+ if first.is_none() {+ first = Some(ts);+ }+ prev = Some(ts);+ }++ let span = prev?.signed_duration_since(first?);+ let secs = span.num_milliseconds() as f64 / 1000.0;+ (secs > 0.0).then_some(secs)+}++#[derive(Debug, Clone)]+struct DisplayStretchInputs {+ display_secs: f64,+ display_start: f64,+ /// Expected display spans predicted by each audio track.+ audio_refs: Vec<(&'static str, f64)>,+ /// How many audio tracks the recording has, probed successfully or not.+ audio_track_count: usize,+ wall_span_secs: Option<f64>,+ /// Whether the track's packet timestamps form the synthetic+ /// one-nominal-tick-per-frame ladder the defective pipeline produced.+ ladder_is_synthetic: bool,+}++/// Decides whether a display track is stretched. Returns `None` unless the+/// signature is unambiguous.+fn evaluate_display_stretch(inputs: &DisplayStretchInputs) -> Option<StretchDecision> {+ if !inputs.ladder_is_synthetic {+ return None;+ }++ // Physical gate: a video track cannot outrun the wall clock. When the+ // log span is available it must prove the stretch, whatever the audio+ // says.+ if let Some(wall) = inputs.wall_span_secs+ && inputs.display_secs < wall * WALL_GATE_RATIO+ {+ return None;+ }++ // Audio references that died early (pre-keepalive system audio) predict+ // a span far below the wall clock; they are not sync targets.+ let plausible_refs: Vec<(&'static str, f64)> = inputs+ .audio_refs+ .iter()+ .filter(|(_, span)| match inputs.wall_span_secs {+ Some(wall) => *span >= wall * AUDIO_REF_PLAUSIBLE_WALL_FRACTION,+ None => true,+ })+ .copied()+ .collect();++ let expected_span_secs = if !plausible_refs.is_empty() {+ let spans: Vec<f64> = plausible_refs.iter().map(|(_, span)| *span).collect();+ let max_span = spans.iter().cloned().fold(f64::MIN, f64::max);+ let min_span = spans.iter().cloned().fold(f64::MAX, f64::min);++ // All remaining references must tell the same story.+ if max_span - min_span > (max_span * REFERENCE_AGREEMENT_RATIO).max(MIN_STRETCH_SECS) {+ return None;+ }++ // Without wall-clock proof, one audio track alone cannot rule out+ // "the audio died early"; require two independent tracks agreeing.+ if inputs.wall_span_secs.is_none() && plausible_refs.len() < 2 {+ return None;+ }++ max_span+ } else if inputs.audio_track_count > 0 {+ // The recording had audio but no track produced a plausible+ // reference — too ambiguous to touch.+ return None;+ } else {+ // No audio at all: the wall span is the only reference. Without a+ // log there is nothing to measure against.+ inputs.wall_span_secs? - inputs.display_start+ };++ if expected_span_secs < MIN_REFERENCE_SECS {+ return None;+ }++ let ratio = inputs.display_secs / expected_span_secs;+ if ratio < DISPLAY_MIN_STRETCH_RATIO+ || ratio > DISPLAY_MAX_STRETCH_RATIO+ || inputs.display_secs - expected_span_secs < DISPLAY_MIN_STRETCH_SECS+ {+ return None;+ }++ Some(StretchDecision {+ expected_span_secs,+ ratio,+ scale: expected_span_secs / inputs.display_secs,+ })+}++/// Rewrites an existing project config so edits made against a stretched+/// recording keep pointing at the same content after the heal. For the pure+/// ladder stretch the source-time mapping is exactly linear, so scaling+/// segment bounds is exact, not approximate. Overlay tracks live on the+/// output timeline, which only scales by the same factor when every timeline+/// segment does; keyboard and caption segments are regenerable from their+/// source-of-truth files, so they are left untouched.+fn rescale_project_config(project_path: &Path, scales: &HashMap<u32, f64>) {+ let Ok(mut config) = ProjectConfiguration::load(project_path) else {+ // No config yet: the editor will build a default timeline from the+ // healed media.+ return;+ };+ let Some(timeline) = config.timeline.as_mut() else {+ return;+ };++ let mut changed = false;+ let mut all_clips_healed = true;++ for segment in &mut timeline.segments {+ if let Some(scale) = scales.get(&segment.recording_clip) {+ // `timescale` (the user-facing Speed control) is deliberately+ // untouched: scaling start/end alone keeps content selection and+ // overlay alignment exact and makes a "2x" label mean 2x of real+ // time again. A speed edit made against the stretched media was+ // compensating for the defect; preserving its on-screen rate+ // would carry the defect forward.+ segment.start *= scale;+ segment.end *= scale;+ changed = true;+ } else {+ all_clips_healed = false;+ }+ }++ let uniform_scale = if scales.len() == 1 && all_clips_healed {+ scales.values().next().copied()+ } else {+ None+ };++ if let Some(scale) = uniform_scale {+ for s in &mut timeline.zoom_segments {+ s.start *= scale;+ s.end *= scale;+ }+ for s in &mut timeline.scene_segments {+ s.start *= scale;+ s.end *= scale;+ }+ for s in &mut timeline.mask_segments {+ s.start *= scale;+ s.end *= scale;+ }+ for s in &mut timeline.text_segments {+ s.start *= scale;+ s.end *= scale;+ }+ for s in &mut timeline.audio_segments {+ s.start *= scale;+ s.end *= scale;+ }+ changed = true;+ }++ if changed {+ if let Err(e) = config.write(project_path) {+ warn!("Failed to write rescaled project config after display heal: {e}");+ }+ }+}++#[derive(Debug, PartialEq)]+struct StretchDecision {+ expected_span_secs: f64,+ ratio: f64,+ scale: f64,+}++/// Decides whether `camera_secs` is a stretched track given the expected+/// spans predicted by the other tracks. Returns `None` unless the signature+/// is unambiguous.+fn evaluate_stretch(camera_secs: f64, references: &[(&str, f64)]) -> Option<StretchDecision> {+ let spans: Vec<f64> = references.iter().map(|(_, span)| *span).collect();+ if spans.is_empty() {+ return None;+ }++ let max_span = spans.iter().cloned().fold(f64::MIN, f64::max);+ let min_span = spans.iter().cloned().fold(f64::MAX, f64::min);++ if max_span < MIN_REFERENCE_SECS {+ return None;+ }++ // All references must tell the same story; a mic that ran much longer+ // than the display means the display died early, not that the camera is+ // stretched.+ if max_span - min_span > (max_span * REFERENCE_AGREEMENT_RATIO).max(MIN_STRETCH_SECS) {+ return None;+ }++ let expected_span_secs = max_span;+ let ratio = camera_secs / expected_span_secs;++ if ratio < MIN_STRETCH_RATIO || camera_secs - expected_span_secs < MIN_STRETCH_SECS {+ return None;+ }++ let has_audio_reference = references.iter().any(|(name, _)| *name != "display");+ if !has_audio_reference+ && !(DISPLAY_ONLY_RATIO_RANGE.0..=DISPLAY_ONLY_RATIO_RANGE.1).contains(&ratio)+ {+ return None;+ }++ Some(StretchDecision {+ expected_span_secs,+ ratio,+ scale: expected_span_secs / camera_secs,+ })+}++#[cfg(test)]+mod tests {+ use super::*;++ const USER_REPORT_CAMERA_SECS: f64 = 13.666;++ fn user_report_references() -> Vec<(&'static str, f64)> {+ // From the original report: display 6.75s starting at 0.302s, mic+ // 6.91s starting at 0.102s, camera starting at 0.149s.+ vec![+ ("display", 6.75 + (0.302 - 0.149)),+ ("mic", 6.91 + (0.102 - 0.149)),+ ]+ }++ #[test]+ fn detects_the_reported_double_length_camera() {+ let decision = evaluate_stretch(USER_REPORT_CAMERA_SECS, &user_report_references())+ .expect("the reported recording must be detected");+ assert!(+ (decision.ratio - 2.0).abs() < 0.05,+ "ratio {}",+ decision.ratio+ );+ assert!(+ (decision.scale - 0.5).abs() < 0.02,+ "scale {}",+ decision.scale+ );+ }++ #[test]+ fn healthy_recording_is_left_alone() {+ assert_eq!(+ evaluate_stretch(6.9, &[("display", 6.75), ("mic", 6.91)]),+ None+ );+ }++ #[test]+ fn camera_slightly_longer_than_references_is_left_alone() {+ // Cameras legitimately run a little past the other tracks.+ assert_eq!(+ evaluate_stretch(7.4, &[("display", 6.75), ("mic", 6.91)]),+ None+ );+ }++ #[test]+ fn display_dying_early_does_not_trigger_heal() {+ // Display stopped at half-time but the mic kept going: references+ // disagree, so the long camera is trusted.+ assert_eq!(+ evaluate_stretch(60.0, &[("display", 30.0), ("mic", 59.5)]),+ None+ );+ }++ #[test]+ fn display_only_requires_doubling_ratio() {+ // Without audio, a 2x camera is healed…+ assert!(evaluate_stretch(13.6, &[("display", 6.8)]).is_some());+ // …but a 3x mismatch (not a rate-doubling signature) is not.+ assert_eq!(evaluate_stretch(20.4, &[("display", 6.8)]), None);+ // …nor is a display that died at two-thirds.+ assert_eq!(evaluate_stretch(90.0, &[("display", 60.0)]), None);+ }++ #[test]+ fn short_recordings_are_not_healed() {+ assert_eq!(+ evaluate_stretch(3.0, &[("display", 1.5), ("mic", 1.5)]),+ None+ );+ }++ #[test]+ fn below_ratio_threshold_is_left_alone() {+ assert_eq!(+ evaluate_stretch(3.1, &[("display", 2.1), ("mic", 2.1)]),+ None+ );+ }++ /// Manual harness for healing a real .cap bundle:+ /// `CAP_HEAL_PROJECT_PATH=/path/to/recording.cap cargo test -p cap-recording \+ /// --lib track_heal::tests::heal_project_from_env -- --ignored --nocapture`+ #[test]+ #[ignore]+ fn heal_project_from_env() {+ let path =+ std::env::var("CAP_HEAL_PROJECT_PATH").expect("CAP_HEAL_PROJECT_PATH must be set");+ let healed = heal_stretched_tracks(Path::new(&path)).unwrap();+ println!("healed: {healed}");+ }++ // -- Display heal --+ //+ // Numbers below are from the 2026-07-06 user report: a 90.3s window+ // recording on a 165Hz monitor produced a 202.22s display.mp4 (12,133+ // frames stamped at exactly 1/60s) against 90.82s of real-time system+ // audio and a 92.8s log wall span.++ fn report_inputs() -> DisplayStretchInputs {+ DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![("system-audio", 90.8225 - 0.4728)],+ audio_track_count: 1,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ }+ }++ #[test]+ fn detects_the_reported_high_refresh_display_stretch() {+ let decision = evaluate_display_stretch(&report_inputs())+ .expect("the reported recording must be detected");+ assert!(+ (decision.ratio - 2.24).abs() < 0.02,+ "ratio {}",+ decision.ratio+ );+ // The scale targets the audio span, which is what playback syncs to.+ assert!(+ (decision.scale - 0.4468).abs() < 0.002,+ "scale {}",+ decision.scale+ );+ }++ #[test]+ fn non_ladder_track_is_never_healed() {+ // Long relative to audio but with real, jittered timestamps: the+ // audio died early, the video is fine.+ let inputs = DisplayStretchInputs {+ ladder_is_synthetic: false,+ ..report_inputs()+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn wall_clock_gate_blocks_healthy_video_with_dead_audio() {+ // Pre-fix 60Hz recording (ladder-stamped but correctly paced) whose+ // system audio died at 30s: the audio ratio alone screams "stretch"+ // but the video does not exceed the wall clock.+ let inputs = DisplayStretchInputs {+ display_secs: 90.3,+ display_start: 0.4,+ audio_refs: vec![("system-audio", 30.0)],+ audio_track_count: 1,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn implausible_audio_reference_is_not_a_sync_target() {+ // Genuinely stretched video, but the only audio track died at a+ // quarter of the wall span; squeezing the video to it would over-+ // correct, so nothing is healed.+ let inputs = DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![("system-audio", 22.0)],+ audio_track_count: 1,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn without_wall_clock_a_single_audio_reference_is_not_enough() {+ let inputs = DisplayStretchInputs {+ wall_span_secs: None,+ ..report_inputs()+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn without_wall_clock_two_agreeing_audio_references_heal() {+ let inputs = DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![("mic", 90.1), ("system-audio", 90.35)],+ audio_track_count: 2,+ wall_span_secs: None,+ ladder_is_synthetic: true,+ };+ let decision = evaluate_display_stretch(&inputs).expect("two refs must be enough");+ assert!((decision.expected_span_secs - 90.35).abs() < 0.01);+ }++ #[test]+ fn without_wall_clock_disagreeing_references_do_not_heal() {+ let inputs = DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![("mic", 45.0), ("system-audio", 90.35)],+ audio_track_count: 2,+ wall_span_secs: None,+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn no_audio_heals_against_the_wall_span() {+ let inputs = DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![],+ audio_track_count: 0,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ };+ let decision = evaluate_display_stretch(&inputs).expect("wall span must be enough");+ assert!((decision.expected_span_secs - (92.8 - 0.4728)).abs() < 0.01);+ }++ #[test]+ fn no_audio_and_no_log_does_not_heal() {+ let inputs = DisplayStretchInputs {+ display_secs: 202.22,+ display_start: 0.4728,+ audio_refs: vec![],+ audio_track_count: 0,+ wall_span_secs: None,+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn healthy_sixty_hertz_prefix_recording_is_left_alone() {+ // Pre-fix on a 60Hz monitor: ladder-stamped but correctly paced,+ // so the ratio sits at ~1.+ let inputs = DisplayStretchInputs {+ display_secs: 90.3,+ display_start: 0.4728,+ audio_refs: vec![("system-audio", 90.35)],+ audio_track_count: 1,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn paused_stretched_recording_heals_to_the_audio_span() {+ // 90s wall with a long pause: 45s of content stretched 2.24x. The+ // wall gate still passes (100.8 > 99) and audio remains the target.+ let inputs = DisplayStretchInputs {+ display_secs: 100.8,+ display_start: 0.4,+ audio_refs: vec![("mic", 45.0), ("system-audio", 45.2)],+ audio_track_count: 2,+ wall_span_secs: Some(90.0),+ ladder_is_synthetic: true,+ };+ let decision = evaluate_display_stretch(&inputs).expect("paused stretch must heal");+ assert!((decision.expected_span_secs - 45.2).abs() < 0.01);+ }++ #[test]+ fn absurd_ratio_is_not_touched() {+ let inputs = DisplayStretchInputs {+ display_secs: 800.0,+ display_start: 0.0,+ audio_refs: vec![("mic", 90.0), ("system-audio", 90.0)],+ audio_track_count: 2,+ wall_span_secs: Some(92.8),+ ladder_is_synthetic: true,+ };+ assert_eq!(evaluate_display_stretch(&inputs), None);+ }++ #[test]+ fn wall_span_parses_first_and_last_log_timestamps() {+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(+ dir.path().join("recording-logs.log"),+ "2026-07-06T18:19:14.754110Z INFO recording: start\n\+ not a timestamped line\n\+ 2026-07-06T18:19:58.715980Z INFO snapshot\n\+ 2026-07-06T18:20:47.563260Z INFO cap_desktop_lib::recording: done\n",+ )+ .unwrap();+ let span = recording_log_wall_span_secs(dir.path()).unwrap();+ assert!((span - 92.809).abs() < 0.01, "span {span}");+ }++ #[test]+ fn wall_span_is_none_for_missing_or_unparseable_logs() {+ let dir = tempfile::tempdir().unwrap();+ assert_eq!(recording_log_wall_span_secs(dir.path()), None);+ std::fs::write(+ dir.path().join("recording-logs.log"),+ "no timestamps here\n",+ )+ .unwrap();+ assert_eq!(recording_log_wall_span_secs(dir.path()), None);+ }++ #[test]+ fn wall_span_rejects_logs_with_backward_clock_steps() {+ // A backward system-clock step mid-recording under-counts the span,+ // which could both authorize and mis-target a no-audio heal; such a+ // log must be discredited entirely.+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(+ dir.path().join("recording-logs.log"),+ "2026-07-06T18:19:14.754110Z INFO start\n\+ 2026-07-06T18:19:58.715980Z INFO snapshot\n\+ 2026-07-06T18:19:20.000000Z INFO after ntp step\n\+ 2026-07-06T18:20:47.563260Z INFO done\n",+ )+ .unwrap();+ assert_eq!(recording_log_wall_span_secs(dir.path()), None);+ }++ #[test]+ fn wall_span_tolerates_async_log_interleaving() {+ // Millisecond-scale inversions are normal: threads capture their+ // timestamps before the appender serializes the lines.+ let dir = tempfile::tempdir().unwrap();+ std::fs::write(+ dir.path().join("recording-logs.log"),+ "2026-07-06T18:19:14.754110Z INFO start\n\+ 2026-07-06T18:19:58.715980Z INFO a\n\+ 2026-07-06T18:19:58.703215Z INFO b (interleaved)\n\+ 2026-07-06T18:20:47.563260Z INFO done\n",+ )+ .unwrap();+ let span = recording_log_wall_span_secs(dir.path()).unwrap();+ assert!((span - 92.809).abs() < 0.01, "span {span}");+ }++ #[test]+ fn concurrent_heals_of_the_same_project_are_exclusive() {+ let dir = tempfile::tempdir().unwrap();+ let first = HealInFlightGuard::try_claim(dir.path());+ assert!(first.is_some());+ assert!(+ HealInFlightGuard::try_claim(dir.path()).is_none(),+ "second claim must be refused while the first is held"+ );+ drop(first);+ assert!(+ HealInFlightGuard::try_claim(dir.path()).is_some(),+ "claim must be reusable after release"+ );+ }++ #[test]+ fn config_rescale_scales_timeline_and_overlays_exactly() {+ use cap_project::{+ ProjectConfiguration, TimelineConfiguration, TimelineSegment, ZoomMode, ZoomSegment,+ };++ let dir = tempfile::tempdir().unwrap();+ let mut config = ProjectConfiguration::default();+ config.timeline = Some(TimelineConfiguration {+ segments: vec![TimelineSegment {+ recording_clip: 0,+ timescale: 1.0,+ start: 0.0,+ end: 202.220711,+ name: None,+ }],+ zoom_segments: vec![ZoomSegment {+ start: 100.0,+ end: 150.0,+ amount: 2.0,+ mode: ZoomMode::Auto,+ glide_direction: Default::default(),+ glide_speed: 0.5,+ instant_animation: false,+ edge_snap_ratio: 0.25,+ }],+ scene_segments: vec![],+ mask_segments: vec![],+ text_segments: vec![],+ caption_segments: vec![],+ keyboard_segments: vec![],+ audio_segments: vec![],+ });+ config.write(dir.path()).unwrap();++ let scale = 0.4468;+ let mut scales = HashMap::new();+ scales.insert(0u32, scale);+ rescale_project_config(dir.path(), &scales);++ let reloaded = ProjectConfiguration::load(dir.path()).unwrap();+ let timeline = reloaded.timeline.unwrap();+ assert!((timeline.segments[0].end - 202.220711 * scale).abs() < 1e-6);+ assert!((timeline.zoom_segments[0].start - 100.0 * scale).abs() < 1e-6);+ assert!((timeline.zoom_segments[0].end - 150.0 * scale).abs() < 1e-6);+ }++ #[test]+ fn config_rescale_leaves_overlays_alone_with_unhealed_clips() {+ use cap_project::{+ ProjectConfiguration, TimelineConfiguration, TimelineSegment, ZoomMode, ZoomSegment,+ };++ let dir = tempfile::tempdir().unwrap();+ let mut config = ProjectConfiguration::default();+ config.timeline = Some(TimelineConfiguration {+ segments: vec![+ TimelineSegment {+ recording_clip: 0,+ timescale: 1.0,+ start: 0.0,+ end: 100.0,+ name: None,+ },+ TimelineSegment {+ recording_clip: 1,+ timescale: 1.0,+ start: 0.0,+ end: 50.0,+ name: None,+ },+ ],+ zoom_segments: vec![ZoomSegment {+ start: 10.0,+ end: 20.0,+ amount: 2.0,+ mode: ZoomMode::Auto,+ glide_direction: Default::default(),+ glide_speed: 0.5,+ instant_animation: false,+ edge_snap_ratio: 0.25,+ }],+ scene_segments: vec![],+ mask_segments: vec![],+ text_segments: vec![],+ caption_segments: vec![],+ keyboard_segments: vec![],+ audio_segments: vec![],+ });+ config.write(dir.path()).unwrap();++ let mut scales = HashMap::new();+ scales.insert(0u32, 0.5);+ rescale_project_config(dir.path(), &scales);++ let reloaded = ProjectConfiguration::load(dir.path()).unwrap();+ let timeline = reloaded.timeline.unwrap();+ // Healed clip's timeline segment scales…+ assert!((timeline.segments[0].end - 50.0).abs() < 1e-6);+ // …the unhealed clip's does not…+ assert!((timeline.segments[1].end - 50.0).abs() < 1e-6);+ // …and output-time overlays are left untouched because the output+ // timeline no longer scales uniformly.+ assert!((timeline.zoom_segments[0].start - 10.0).abs() < 1e-6);+ }+}diff --git a/crates/recording/tests/sync_matrix.rs b/crates/recording/tests/sync_matrix.rsindex 964a19759..8ef2ac4c7 100644--- a/crates/recording/tests/sync_matrix.rs+++ b/crates/recording/tests/sync_matrix.rs@@ -1280,6 +1280,12 @@ async fn synthetic_device_matrix_preserves_sync() {(30, 240, VideoScenario::Steady, true),(30, 1000, VideoScenario::Steady, false),(60, 24, VideoScenario::Steady, true), // slow source into a fast belief+ // Windows screen capture on a 165Hz monitor without+ // MinUpdateInterval support: WGC free-runs at the content update+ // rate. 134 is the delivered rate from the reported recording+ // (2.24x slow-motion display track); 165 is the refresh ceiling.+ (60, 134, VideoScenario::Jitter, true),+ (60, 165, VideoScenario::Steady, true),];for (nominal, delivered, scenario, fragmented) in mismatch_cases {@@ -1290,8 +1296,10 @@ async fn synthetic_device_matrix_preserves_sync() {scenario.name(),if fragmented { "fragmented" } else { "mp4" });- let outcome =- run_video_case(VideoCase::mismatch(nominal, delivered, scenario, fragmented)).await;+ let outcome = run_video_case(VideoCase::mismatch(+ nominal, delivered, scenario, fragmented,+ ))+ .await;record(&mut results, name, outcome);}