From 6a20551a8dc67bc187af689012334e53eeff5fbd Mon Sep 17 00:00:00 2001 From: "alexey.lovchikov" Date: Sun, 22 Nov 2026 15:55:49 +0500 Subject: [PATCH 1/5] fix(cursor): restart the cursor spring at every clip window The cursor spring ran over the whole recording, including footage a trim or a clip boundary removed. At the cut the drawn cursor was wherever the hidden path had dragged the spring, so it teleported late. The spring now restarts at the source start of every clip window on the file, the same way it already restarts when the cursor reappears, in preview and export. Co-Authored-By: Claude Opus 5.5 --- crates/compositor/src/cursor.rs | 51 +++++++++++++++++++++++++- crates/compositor/src/live.rs | 23 ++++++++++-- crates/compositor/src/scene.rs | 11 ++++++ crates/compositor/src/timeline_walk.rs | 7 +++- 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/crates/compositor/src/cursor.rs b/crates/compositor/src/cursor.rs index cec1353112..b33cfcd62d 100644 --- a/crates/compositor/src/cursor.rs +++ b/crates/compositor/src/cursor.rs @@ -513,6 +513,15 @@ impl CursorTrack { /// du slider (0 = passthrough, retourne un clone). Les clics restent sur leurs instants /// bruts (le bounce est temporel, pas positionnel — ne doit pas suivre le lissage). pub fn smoothed(&self, factor: f32) -> CursorTrack { + self.smoothed_with_breaks(factor, &[]) + } + + /// `smoothed(factor)`, with the spring also restarted at each of `breaks` (source seconds), + /// as it already restarts when the cursor reappears. The callers pass the start of every clip + /// window on this file (`Scene::cursor_breaks_for`): without them the spring ran across the + /// footage a trim or a clip boundary removed, and the drawn cursor teleported at the cut to + /// wherever that hidden path had dragged it. + pub fn smoothed_with_breaks(&self, factor: f32, breaks: &[f32]) -> CursorTrack { if self.samples.len() < 2 || factor <= 0.0 { return CursorTrack::new_with_visibility( self.samples.clone(), @@ -542,14 +551,22 @@ impl CursorTrack { } let (stiffness, damping, mass) = cursor_spring_config(factor); // Une phase masquée coupe le ressort. Au retour, le curseur repart du premier point - // réellement visible au lieu de traverser la trajectoire cachée avec du retard. + // réellement visible au lieu de traverser la trajectoire cachée avec du retard. A break + // (a clip window's start) cuts it the same way. + let mut sorted_breaks: Vec = breaks.iter().copied().filter(|b| b.is_finite()).collect(); + sorted_breaks.sort_by(|a, b| a.partial_cmp(b).unwrap()); + let segment_of = |t: f32| sorted_breaks.partition_point(|&b| b <= t); let mut xs = Vec::with_capacity(n); let mut ys = Vec::with_capacity(n); let mut run_start = 0; while run_start < n { let visible = self.visible_at(times[run_start]); + let segment = segment_of(times[run_start]); let mut run_end = run_start + 1; - while run_end < n && self.visible_at(times[run_end]) == visible { + while run_end < n + && self.visible_at(times[run_end]) == visible + && segment_of(times[run_end]) == segment + { run_end += 1; } xs.extend(spring_smooth( @@ -1215,3 +1232,33 @@ mod tests { assert_eq!(track.opacity_at(5.9, true), 0.0); } } + +#[cfg(test)] +mod break_tests { + use super::*; + + #[test] + fn smoothed_without_breaks_is_unchanged() { + let samples: Vec<_> = (0..=60).map(|i| (i as f32 / 30.0, 0.1 + 0.01 * i as f32, 0.5)).collect(); + let raw = CursorTrack::new(samples, vec![], vec![(0.0, "arrow".to_string())]); + for t in [0.0, 0.7, 1.3, 2.0] { + assert_eq!(raw.smoothed_with_breaks(0.4, &[]).at(t), raw.smoothed(0.4).at(t)); + } + } + + #[test] + fn a_break_restarts_the_spring_so_a_cut_does_not_teleport_late() { + // Still at x = 0.1 until 2 s, then at x = 0.9: what a cut to later footage looks like. + let samples: Vec<_> = (0..=120) + .map(|i| { + let t = i as f32 / 30.0; + (t, if t < 2.0 { 0.1 } else { 0.9 }, 0.5) + }) + .collect(); + let raw = CursorTrack::new(samples, vec![], vec![(0.0, "arrow".to_string())]); + let lagging = raw.smoothed(0.5).at(2.05).unwrap().0; + let restarted = raw.smoothed_with_breaks(0.5, &[2.0]).at(2.05).unwrap().0; + assert!(lagging < 0.8, "without a break the spring still trails: {lagging}"); + assert!((restarted - 0.9).abs() < 1e-3, "after a break it starts on the pointer: {restarted}"); + } +} diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs index 52a95f905f..e6410a815a 100644 --- a/crates/compositor/src/live.rs +++ b/crates/compositor/src/live.rs @@ -903,6 +903,7 @@ fn reload_cursor_if_changed( screen_path: &str, turned: bool, smoothing: f32, + breaks: &[f32], ) -> bool { let key = cursor_key(screen_path, turned); if key == *loaded_cursor_path { @@ -929,12 +930,17 @@ fn reload_cursor_if_changed( // curseur (`Compositor::resized`), et le relisser parcourt tout // l'enregistrement à chaque bascule d'un scrub. match raw_cursor { - Some(track) => comp.set_cursor(track.smoothed(smoothing)), + Some(track) => comp.set_cursor(track.smoothed_with_breaks(smoothing, breaks)), None => comp.clear_cursor(), } true } +/// Where the spring of `screen_path`'s cursor track restarts (`Scene::cursor_breaks_for`). +fn cursor_breaks(scene: Option<&Scene>, screen_path: &str) -> Vec { + scene.map(|s| s.cursor_breaks_for(screen_path)).unwrap_or_default() +} + fn scene_for_clip(scene: &Scene, clip_index: usize) -> Scene { match scene.clips.get(clip_index) { Some(clip) => scene.for_clip_window( @@ -1699,6 +1705,8 @@ unsafe fn render_thread( let mut last_preview_size: (u32, u32) = (0, 0); let mut last_ip: Option = None; let mut last_smoothing: f32 = -1.0; // force la 1re application (0.0 est une valeur valide) + // The breaks the current track was smoothed with (`cursor_breaks`). + let mut last_breaks: Vec = Vec::new(); // Fenêtre de stabilisation après un changement EN PAUSE. Un seul recompose ne suffit pas // quand l'effet demandé est asynchrone : la segmentation webcam (détourage / flou / fond // personnalisé) démarre son worker au 1er compose, ne SOUMET la frame qu'au 2e et ne @@ -1812,6 +1820,7 @@ unsafe fn render_thread( // construction identique. Mesuré : 66 bascules sur un scrub de deux clips, // donc 66 relectures du même fichier. // The key also carries the clip's turn: the same file turned is another track. + let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); if reload_cursor_if_changed( &comp, &mut raw_cursor, @@ -1819,8 +1828,10 @@ unsafe fn render_thread( &active_screen_path, turned, ip.cursor_smoothing, + &breaks, ) { last_smoothing = ip.cursor_smoothing; + last_breaks = breaks; } clip_changed = true; } @@ -1858,10 +1869,13 @@ unsafe fn render_thread( }); // Lissage ressort-amortisseur : re-génère la piste (240 Hz) uniquement quand la valeur // change (pas à chaque frame — le resample+ressort parcourt tout l'enregistrement). + // The clip breaks reshape it the same way, when the scene changes them. if let Some(raw) = &raw_cursor { - if ip.cursor_smoothing != last_smoothing { - comp.set_cursor(raw.smoothed(ip.cursor_smoothing)); + let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); + if ip.cursor_smoothing != last_smoothing || breaks != last_breaks { + comp.set_cursor(raw.smoothed_with_breaks(ip.cursor_smoothing, &breaks)); last_smoothing = ip.cursor_smoothing; + last_breaks = breaks; } } // un changement de param doit se voir même en pause (édition live des sliders) : @@ -1894,6 +1908,7 @@ unsafe fn render_thread( // here, mirrored the new way. let turned = scene.as_ref().is_some_and(Scene::screen_turned); comp.set_scene(scene); + let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); if reload_cursor_if_changed( &comp, &mut raw_cursor, @@ -1901,8 +1916,10 @@ unsafe fn render_thread( &active_screen_path, turned, ip.cursor_smoothing, + &breaks, ) { last_smoothing = ip.cursor_smoothing; + last_breaks = breaks; } } // Le temps programme dépend du clip actif ET de la scène entière (durées des clips diff --git a/crates/compositor/src/scene.rs b/crates/compositor/src/scene.rs index b4a5fb11d9..d27b20150c 100644 --- a/crates/compositor/src/scene.rs +++ b/crates/compositor/src/scene.rs @@ -973,6 +973,17 @@ pub struct Scene { } impl Scene { + /// Where the cursor spring of `screen_path`'s track restarts: the source start of every clip + /// window on that file (`CursorTrack::smoothed_with_breaks`). A trim splits a clip into kept + /// segments, each a clip here, so the instant after a cut is a window start too. + pub fn cursor_breaks_for(&self, screen_path: &str) -> Vec { + self.clips + .iter() + .filter(|clip| clip.screen_path == screen_path) + .map(|clip| clip.source_start_sec as f32) + .collect() + } + pub fn layout_at(&self, t: f32) -> Option> { self.layout_regions.iter().find(|r| r.clip_index == self.active_clip_index && t >= r.start_sec && t < r.end_sec).map(|r| r.sample(t)) diff --git a/crates/compositor/src/timeline_walk.rs b/crates/compositor/src/timeline_walk.rs index ac05f17a14..090f10c381 100644 --- a/crates/compositor/src/timeline_walk.rs +++ b/crates/compositor/src/timeline_walk.rs @@ -512,7 +512,12 @@ unsafe fn walk_timeline( let path = format!("{}.cursor.json", clip.screen); if let Ok(raw) = CursorTrack::load(&path, 0.0, 24.0 * 3600.0) { let raw = if turned { raw.turned() } else { raw }; - cursor_tracks.insert(key.clone(), raw.smoothed(cursor_smoothing)); + // The spring restarts at every clip window on this file, as in preview. + let breaks = scene + .as_ref() + .map(|s| s.cursor_breaks_for(&clip.screen)) + .unwrap_or_default(); + cursor_tracks.insert(key.clone(), raw.smoothed_with_breaks(cursor_smoothing, &breaks)); } // absente/illisible → pas d'entrée : ce clip s'exporte sans curseur (visible, // pas masqué en un curseur fantôme d'un autre clip). From 975a248d3e927a3ba8fa830c384d8a2c4eb97590 Mon Sep 17 00:00:00 2001 From: "alexey.lovchikov" Date: Sun, 22 Nov 2026 16:20:30 +0500 Subject: [PATCH 2/5] fix(cursor): restart per clip window, exactly on its start Review follow-ups: - The restart is the playing clip's own source start only. Every clip start on the file used to restart one shared track, so a second clip over the same footage reset the cursor inside the first. - No restart when the clip before it is the same file ending where this one starts: the source runs on, and a restart would itself be a jump. - The break is a sample of its own, so a clip that starts between two 240 Hz grid samples no longer shows a first frame interpolated from the run before it. - Clips are addressed by index, not by comparing paths. Export reads the raw telemetry once per file and keys the smoothed track by the break. Co-Authored-By: Claude Opus 5.5 --- crates/compositor/src/cursor.rs | 49 ++++++++++++------ crates/compositor/src/live.rs | 14 +++--- crates/compositor/src/scene.rs | 70 ++++++++++++++++++++++---- crates/compositor/src/timeline_walk.rs | 31 ++++++++---- 4 files changed, 125 insertions(+), 39 deletions(-) diff --git a/crates/compositor/src/cursor.rs b/crates/compositor/src/cursor.rs index b33cfcd62d..6e93bf65be 100644 --- a/crates/compositor/src/cursor.rs +++ b/crates/compositor/src/cursor.rs @@ -517,8 +517,8 @@ impl CursorTrack { } /// `smoothed(factor)`, with the spring also restarted at each of `breaks` (source seconds), - /// as it already restarts when the cursor reappears. The callers pass the start of every clip - /// window on this file (`Scene::cursor_breaks_for`): without them the spring ran across the + /// as it already restarts when the cursor reappears. The callers pass the start of the clip + /// window being played (`Scene::cursor_break_for_clip`): without it the spring ran across the /// footage a trim or a clip boundary removed, and the drawn cursor teleported at the cut to /// wherever that hidden path had dragged it. pub fn smoothed_with_breaks(&self, factor: f32, breaks: &[f32]) -> CursorTrack { @@ -534,18 +534,24 @@ impl CursorTrack { let start = self.samples[0].0; let end = self.samples[self.samples.len() - 1].0; let step_count = (((end - start) / STEP_S).round() as usize).max(1); - let n = step_count + 1; - let mut times = Vec::with_capacity(n); + let mut sorted_breaks: Vec = breaks.iter().copied().filter(|b| b.is_finite()).collect(); + sorted_breaks.sort_by(|a, b| a.partial_cmp(b).unwrap()); + let mut times: Vec = (0..=step_count) + .map(|i| if i == step_count { end } else { start + i as f32 * STEP_S }) + .collect(); + // Each break is a sample of its own, the first of its run: a clip rarely starts on the + // 240 Hz grid, and restarting at the next grid sample instead left the clip's first frame + // interpolating from the run before, still trailing. The one step this shortens is + // integrated as a whole one; the run starts at rest on its target, so nothing visible + // changes. + times.extend(sorted_breaks.iter().copied().filter(|&b| b > start && b < end)); + times.sort_by(|a, b| a.partial_cmp(b).unwrap()); + times.dedup_by(|later, earlier| (*later - *earlier).abs() < 1e-6); + let n = times.len(); let mut raw_x = Vec::with_capacity(n); let mut raw_y = Vec::with_capacity(n); - for i in 0..n { - let t = if i == n - 1 { - end - } else { - start + i as f32 * STEP_S - }; + for &t in × { let (cx, cy) = self.at(t).unwrap_or((0.0, 0.0)); - times.push(t); raw_x.push(cx); raw_y.push(cy); } @@ -553,9 +559,7 @@ impl CursorTrack { // Une phase masquée coupe le ressort. Au retour, le curseur repart du premier point // réellement visible au lieu de traverser la trajectoire cachée avec du retard. A break // (a clip window's start) cuts it the same way. - let mut sorted_breaks: Vec = breaks.iter().copied().filter(|b| b.is_finite()).collect(); - sorted_breaks.sort_by(|a, b| a.partial_cmp(b).unwrap()); - let segment_of = |t: f32| sorted_breaks.partition_point(|&b| b <= t); + let segment_of = |t: f32| sorted_breaks.partition_point(|&b| b <= t + 1e-6); let mut xs = Vec::with_capacity(n); let mut ys = Vec::with_capacity(n); let mut run_start = 0; @@ -1261,4 +1265,21 @@ mod break_tests { assert!(lagging < 0.8, "without a break the spring still trails: {lagging}"); assert!((restarted - 0.9).abs() < 1e-3, "after a break it starts on the pointer: {restarted}"); } + + #[test] + fn a_break_between_grid_samples_restarts_exactly_on_it() { + // The clip starts at 2.001 s, off the 240 Hz grid: its first frame must already be on the + // pointer, not interpolated from the run before the break. + let samples: Vec<_> = (0..=120) + .map(|i| { + let t = i as f32 / 30.0; + (t, if t < 2.0 { 0.1 } else { 0.9 }, 0.5) + }) + .collect(); + let raw = CursorTrack::new(samples, vec![], vec![(0.0, "arrow".to_string())]); + let at_break = 2.001; + let target = raw.at(at_break).unwrap().0; + let restarted = raw.smoothed_with_breaks(0.5, &[at_break]).at(at_break).unwrap().0; + assert!((restarted - target).abs() < 1e-4, "{restarted} vs the pointer at {target}"); + } } diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs index e6410a815a..21df526721 100644 --- a/crates/compositor/src/live.rs +++ b/crates/compositor/src/live.rs @@ -936,9 +936,11 @@ fn reload_cursor_if_changed( true } -/// Where the spring of `screen_path`'s cursor track restarts (`Scene::cursor_breaks_for`). -fn cursor_breaks(scene: Option<&Scene>, screen_path: &str) -> Vec { - scene.map(|s| s.cursor_breaks_for(screen_path)).unwrap_or_default() +/// Where the cursor spring restarts while clip `clip_index` plays +/// (`Scene::cursor_break_for_clip`). It changes with the active clip, so the track is smoothed +/// again on a clip change even when the file stays the same: one spring pass over the recording. +fn cursor_breaks(scene: Option<&Scene>, clip_index: usize) -> Vec { + scene.and_then(|s| s.cursor_break_for_clip(clip_index)).into_iter().collect() } fn scene_for_clip(scene: &Scene, clip_index: usize) -> Scene { @@ -1820,7 +1822,7 @@ unsafe fn render_thread( // construction identique. Mesuré : 66 bascules sur un scrub de deux clips, // donc 66 relectures du même fichier. // The key also carries the clip's turn: the same file turned is another track. - let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); + let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); if reload_cursor_if_changed( &comp, &mut raw_cursor, @@ -1871,7 +1873,7 @@ unsafe fn render_thread( // change (pas à chaque frame — le resample+ressort parcourt tout l'enregistrement). // The clip breaks reshape it the same way, when the scene changes them. if let Some(raw) = &raw_cursor { - let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); + let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); if ip.cursor_smoothing != last_smoothing || breaks != last_breaks { comp.set_cursor(raw.smoothed_with_breaks(ip.cursor_smoothing, &breaks)); last_smoothing = ip.cursor_smoothing; @@ -1908,7 +1910,7 @@ unsafe fn render_thread( // here, mirrored the new way. let turned = scene.as_ref().is_some_and(Scene::screen_turned); comp.set_scene(scene); - let breaks = cursor_breaks(full_scene.as_ref(), &active_screen_path); + let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); if reload_cursor_if_changed( &comp, &mut raw_cursor, diff --git a/crates/compositor/src/scene.rs b/crates/compositor/src/scene.rs index d27b20150c..6364ae9427 100644 --- a/crates/compositor/src/scene.rs +++ b/crates/compositor/src/scene.rs @@ -973,15 +973,23 @@ pub struct Scene { } impl Scene { - /// Where the cursor spring of `screen_path`'s track restarts: the source start of every clip - /// window on that file (`CursorTrack::smoothed_with_breaks`). A trim splits a clip into kept - /// segments, each a clip here, so the instant after a cut is a window start too. - pub fn cursor_breaks_for(&self, screen_path: &str) -> Vec { - self.clips - .iter() - .filter(|clip| clip.screen_path == screen_path) - .map(|clip| clip.source_start_sec as f32) - .collect() + /// Where the cursor spring restarts while clip `clip_index` plays + /// (`CursorTrack::smoothed_with_breaks`): its own source start, and nothing else. Another + /// clip's start on the same file is no cut in this one, so it must not reset this clip's + /// cursor mid-window. `None` when the clip before it is the same file ending exactly where + /// this one starts: the source runs on across that boundary (a split with nothing removed), + /// and a restart there would itself be a jump. The clips are addressed by index, as + /// everywhere else in the scene, so no path comparison picks the clip. + pub fn cursor_break_for_clip(&self, clip_index: usize) -> Option { + let clip = self.clips.get(clip_index)?; + let continues = clip_index + .checked_sub(1) + .and_then(|previous| self.clips.get(previous)) + .is_some_and(|previous| { + previous.screen_path.eq_ignore_ascii_case(&clip.screen_path) + && (previous.source_end_sec - clip.source_start_sec).abs() < 1e-3 + }); + (!continues).then_some(clip.source_start_sec as f32) } pub fn layout_at(&self, t: f32) -> Option> { @@ -1439,3 +1447,47 @@ mod annotation_tests { ); } } + +#[cfg(test)] +mod cursor_break_tests { + use super::*; + + fn scene_with_clips(clips: &[(&str, f64, f64)]) -> Scene { + let clips: Vec = clips + .iter() + .map(|(path, start, end)| { + format!( + r#"{{"screenPath":"{path}","webcamPath":"","sourceStartSec":{start},"sourceEndSec":{end},"webcamOffsetSec":0}}"# + ) + }) + .collect(); + let json = format!( + r##"{{"clips":[{}],"layout":{{"preset":"no-webcam","webcamSize":1,"webcamShape":"rectangle","webcamMirror":false,"webcamPosition":null,"webcamReactiveZoom":false}},"effects":{{"padding":0,"blur":false,"shadow":0,"roundnessFrac":0,"motionBlur":0}},"background":{{"kind":"color","color":"#000000"}},"zoomRegions":[],"cursor":{{"show":false,"size":1,"smoothing":0,"motionBlur":0,"clickBounce":0,"clipToBounds":false,"theme":"default"}},"cropByClip":[],"output":{{"width":1920,"height":1080,"fps":30}}}}"##, + clips.join(",") + ); + Scene::from_json(&json).expect("scene") + } + + #[test] + fn each_clip_restarts_at_its_own_start_only() { + // A = [0, 6] and B = [2, 4] on one file: B's start is no cut inside A. + let scene = scene_with_clips(&[("a.mp4", 0.0, 6.0), ("a.mp4", 2.0, 4.0)]); + assert_eq!(scene.cursor_break_for_clip(0), Some(0.0)); + assert_eq!(scene.cursor_break_for_clip(1), Some(2.0)); + assert_eq!(scene.cursor_break_for_clip(2), None, "no such clip"); + } + + #[test] + fn a_clip_that_continues_the_one_before_it_does_not_restart() { + // A trim removed 4..6: the segment after the cut restarts, a plain split at 2 does not. + let scene = scene_with_clips(&[ + ("C:/Capture.mp4", 0.0, 2.0), + ("c:/capture.mp4", 2.0, 4.0), + ("C:/Capture.mp4", 6.0, 9.0), + ("other.mp4", 9.0, 12.0), + ]); + assert_eq!(scene.cursor_break_for_clip(1), None, "same file, source runs on"); + assert_eq!(scene.cursor_break_for_clip(2), Some(6.0), "footage was removed before it"); + assert_eq!(scene.cursor_break_for_clip(3), Some(9.0), "another file"); + } +} diff --git a/crates/compositor/src/timeline_walk.rs b/crates/compositor/src/timeline_walk.rs index 090f10c381..3ef4f3bad2 100644 --- a/crates/compositor/src/timeline_walk.rs +++ b/crates/compositor/src/timeline_walk.rs @@ -350,6 +350,8 @@ unsafe fn walk_timeline( let cursor_enabled = scene.is_some(); let cursor_smoothing = scene.as_ref().map(|s| s.cursor.smoothing).unwrap_or(0.0); let mut cursor_tracks: HashMap = HashMap::new(); + // `None`: the file has no readable telemetry, so it is not read again for the next clip. + let mut raw_cursor_tracks: HashMap> = HashMap::new(); let mut cursor_active_path: Option = None; let mut frames: u64 = 0; @@ -506,17 +508,26 @@ unsafe fn walk_timeline( } if cursor_enabled { - // Keyed by file AND turn: the same file turned is another track. - let key = cursor_key(&clip.screen, turned); + // Keyed by file AND turn: the same file turned is another track. The raw file is read + // once; the smoothed track is also keyed by where this clip's spring restarts + // (`Scene::cursor_break_for_clip`, as in preview), so one clip's start never resets + // the cursor inside another clip on the same file. + let raw_key = cursor_key(&clip.screen, turned); + let clip_break = scene.as_ref().and_then(|s| s.cursor_break_for_clip(clip_index)); + let key = match clip_break { + Some(at) => format!("{raw_key}#{}", at.to_bits()), + None => raw_key.clone(), + }; if !cursor_tracks.contains_key(&key) { - let path = format!("{}.cursor.json", clip.screen); - if let Ok(raw) = CursorTrack::load(&path, 0.0, 24.0 * 3600.0) { - let raw = if turned { raw.turned() } else { raw }; - // The spring restarts at every clip window on this file, as in preview. - let breaks = scene - .as_ref() - .map(|s| s.cursor_breaks_for(&clip.screen)) - .unwrap_or_default(); + if !raw_cursor_tracks.contains_key(&raw_key) { + let path = format!("{}.cursor.json", clip.screen); + let raw = CursorTrack::load(&path, 0.0, 24.0 * 3600.0) + .ok() + .map(|raw| if turned { raw.turned() } else { raw }); + raw_cursor_tracks.insert(raw_key.clone(), raw); + } + if let Some(Some(raw)) = raw_cursor_tracks.get(&raw_key) { + let breaks: Vec = clip_break.into_iter().collect(); cursor_tracks.insert(key.clone(), raw.smoothed_with_breaks(cursor_smoothing, &breaks)); } // absente/illisible → pas d'entrée : ce clip s'exporte sans curseur (visible, From 78d04b3e00b4f463d361e732fcdb6c5c9172c3ac Mon Sep 17 00:00:00 2001 From: "alexey.lovchikov" Date: Sun, 22 Nov 2026 17:02:46 +0500 Subject: [PATCH 3/5] fix(cursor): keep a chain's restart across plain splits Second review round: - The restart is the source start of the clip's chain: the run of clips playing one unbroken stretch of one file. A plain split after a cut used to fall back on a track with no restart at all, so the clip after the split showed the spring that crossed the removed footage. - Export keeps one smoothed track at a time. Each spans the whole recording, and one per cut stayed in memory until the export ended. - Preview reads the breaks after the scene update, so the frame composed in that turn already uses them, and an auto-advanced clip gets its smoothed track before its first frame instead of one turn later. Co-Authored-By: Claude Fable 5.2 --- crates/compositor/src/cursor.rs | 20 +++++++++ crates/compositor/src/live.rs | 37 ++++++++++------ crates/compositor/src/scene.rs | 60 +++++++++++++++++++------- crates/compositor/src/timeline_walk.rs | 5 +++ 4 files changed, 93 insertions(+), 29 deletions(-) diff --git a/crates/compositor/src/cursor.rs b/crates/compositor/src/cursor.rs index 6e93bf65be..899a1d2de7 100644 --- a/crates/compositor/src/cursor.rs +++ b/crates/compositor/src/cursor.rs @@ -1282,4 +1282,24 @@ mod break_tests { let restarted = raw.smoothed_with_breaks(0.5, &[at_break]).at(at_break).unwrap().0; assert!((restarted - target).abs() < 1e-4, "{restarted} vs the pointer at {target}"); } + + #[test] + fn the_clips_of_a_chain_draw_one_path_across_their_split() { + // A pointer that keeps moving after a cut at 2 s. Both clips of the chain [2, 2.05] and + // [2.05, 4] are smoothed with the restart at 2, so the path has no step at the split, + // where a track without that restart is still far behind. + let samples: Vec<_> = (0..=120) + .map(|i| { + let t = i as f32 / 30.0; + (t, if t < 2.0 { 0.1 } else { 0.5 + 0.2 * (t - 2.0) }, 0.5) + }) + .collect(); + let raw = CursorTrack::new(samples, vec![], vec![(0.0, "arrow".to_string())]); + let chain = raw.smoothed_with_breaks(0.5, &[2.0]); + let before = chain.at(2.05 - 1.0 / 60.0).unwrap().0; + let after = chain.at(2.05).unwrap().0; + assert!((after - before).abs() < 0.01, "a step of {} at the split", after - before); + let without_restart = raw.smoothed(0.5).at(2.05).unwrap().0; + assert!((after - without_restart).abs() > 0.05, "the restart is what keeps it on the pointer"); + } } diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs index 21df526721..a160a97808 100644 --- a/crates/compositor/src/live.rs +++ b/crates/compositor/src/live.rs @@ -1526,7 +1526,9 @@ unsafe fn advance_to_next_scene_clip( active_clip_index: &mut usize, raw_cursor: &mut Option, loaded_cursor_path: &mut String, + smoothing: f32, last_smoothing: &mut f32, + last_breaks: &mut Vec, ) -> Option { let Some(next_index) = next_clip_index(scene, *active_clip_index) else { return None; @@ -1600,11 +1602,15 @@ unsafe fn advance_to_next_scene_clip( // fichier d'avant le croit encore chargé, ne relit rien, et dessine sur ce clip la // télémétrie de celui qu'on vient de quitter. *loaded_cursor_path = cursor_key(active_screen_path, turned); + // Smoothed for the clip it lands on before its first frame is composed: installing + // the raw track and correcting it on the next loop turn showed one frame elsewhere. + let breaks = cursor_breaks(Some(scene), next_index); match raw_cursor { - Some(track) => comp.set_cursor(track.smoothed(0.0)), + Some(track) => comp.set_cursor(track.smoothed_with_breaks(smoothing, &breaks)), None => comp.clear_cursor(), } - *last_smoothing = -1.0; + *last_smoothing = smoothing; + *last_breaks = breaks; Some(next_clip.source_start_sec) } Err(e) => { @@ -1869,17 +1875,6 @@ unsafe fn render_thread( cursor_auto_hide: ip.cursor_auto_hide, has_webcam: has_real_webcam, }); - // Lissage ressort-amortisseur : re-génère la piste (240 Hz) uniquement quand la valeur - // change (pas à chaque frame — le resample+ressort parcourt tout l'enregistrement). - // The clip breaks reshape it the same way, when the scene changes them. - if let Some(raw) = &raw_cursor { - let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); - if ip.cursor_smoothing != last_smoothing || breaks != last_breaks { - comp.set_cursor(raw.smoothed_with_breaks(ip.cursor_smoothing, &breaks)); - last_smoothing = ip.cursor_smoothing; - last_breaks = breaks; - } - } // un changement de param doit se voir même en pause (édition live des sliders) : // on recompose la frame courante dans la branche pause ci-dessous. let ip_changed = last_ip != Some(ip); @@ -1924,6 +1919,18 @@ unsafe fn render_thread( last_breaks = breaks; } } + // Lissage ressort-amortisseur : re-génère la piste (240 Hz) uniquement quand la valeur + // change (pas à chaque frame — le resample+ressort parcourt tout l'enregistrement). + // The clip breaks reshape it the same way, when the scene changes them: read here, after + // the scene update above, so the frame composed below already uses the new ones. + if let Some(raw) = &raw_cursor { + let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); + if ip.cursor_smoothing != last_smoothing || breaks != last_breaks { + comp.set_cursor(raw.smoothed_with_breaks(ip.cursor_smoothing, &breaks)); + last_smoothing = ip.cursor_smoothing; + last_breaks = breaks; + } + } // Le temps programme dépend du clip actif ET de la scène entière (durées des clips // précédents) : à recalculer dès que l'un des deux change. if clip_changed || scene_changed { @@ -2060,7 +2067,9 @@ unsafe fn render_thread( &mut active_clip_index, &mut raw_cursor, &mut loaded_cursor_path, + ip.cursor_smoothing, &mut last_smoothing, + &mut last_breaks, ) { acc = anchor_acc(start, player.screen_time_sec()); } @@ -2091,7 +2100,9 @@ unsafe fn render_thread( &mut active_clip_index, &mut raw_cursor, &mut loaded_cursor_path, + ip.cursor_smoothing, &mut last_smoothing, + &mut last_breaks, ) .map(|start| anchor_acc(start, player.screen_time_sec())); } diff --git a/crates/compositor/src/scene.rs b/crates/compositor/src/scene.rs index 6364ae9427..ef6fdaffb9 100644 --- a/crates/compositor/src/scene.rs +++ b/crates/compositor/src/scene.rs @@ -973,23 +973,40 @@ pub struct Scene { } impl Scene { + /// Whether clip `index` goes on exactly where the clip before it stopped, on the same file: + /// a split with nothing removed between the two. + fn continues_previous_clip(&self, index: usize) -> bool { + let (Some(previous), Some(clip)) = + (index.checked_sub(1).and_then(|i| self.clips.get(i)), self.clips.get(index)) + else { + return false; + }; + previous.screen_path.eq_ignore_ascii_case(&clip.screen_path) + && (previous.source_end_sec - clip.source_start_sec).abs() < 1e-3 + } + + /// The first clip of the chain clip `clip_index` belongs to: the run of clips that play one + /// unbroken stretch of one file. A cut (footage removed, or another file) starts a new chain; + /// a plain split does not. + pub fn cursor_chain_start(&self, clip_index: usize) -> Option { + self.clips.get(clip_index)?; + let mut start = clip_index; + while self.continues_previous_clip(start) { + start -= 1; + } + Some(start) + } + /// Where the cursor spring restarts while clip `clip_index` plays - /// (`CursorTrack::smoothed_with_breaks`): its own source start, and nothing else. Another - /// clip's start on the same file is no cut in this one, so it must not reset this clip's - /// cursor mid-window. `None` when the clip before it is the same file ending exactly where - /// this one starts: the source runs on across that boundary (a split with nothing removed), - /// and a restart there would itself be a jump. The clips are addressed by index, as - /// everywhere else in the scene, so no path comparison picks the clip. + /// (`CursorTrack::smoothed_with_breaks`): the source start of its chain + /// (`cursor_chain_start`), and nothing else. Every clip of a chain gets the same restart, so + /// a plain split neither resets the cursor nor forgets the cut the chain started at; another + /// clip's start on the same file is no cut in this one and never resets it mid-window. The + /// clips are addressed by index, as everywhere else in the scene, so no path comparison + /// picks the clip. pub fn cursor_break_for_clip(&self, clip_index: usize) -> Option { - let clip = self.clips.get(clip_index)?; - let continues = clip_index - .checked_sub(1) - .and_then(|previous| self.clips.get(previous)) - .is_some_and(|previous| { - previous.screen_path.eq_ignore_ascii_case(&clip.screen_path) - && (previous.source_end_sec - clip.source_start_sec).abs() < 1e-3 - }); - (!continues).then_some(clip.source_start_sec as f32) + let start = self.cursor_chain_start(clip_index)?; + Some(self.clips[start].source_start_sec as f32) } pub fn layout_at(&self, t: f32) -> Option> { @@ -1486,8 +1503,19 @@ mod cursor_break_tests { ("C:/Capture.mp4", 6.0, 9.0), ("other.mp4", 9.0, 12.0), ]); - assert_eq!(scene.cursor_break_for_clip(1), None, "same file, source runs on"); + assert_eq!(scene.cursor_break_for_clip(1), Some(0.0), "same file, source runs on"); assert_eq!(scene.cursor_break_for_clip(2), Some(6.0), "footage was removed before it"); assert_eq!(scene.cursor_break_for_clip(3), Some(9.0), "another file"); } + + #[test] + fn a_split_after_a_cut_keeps_the_restart_of_the_cut() { + // [0, 1], then a cut to 2, then a plain split at 2.05: the third clip must keep the + // restart at 2, or it falls back on a spring that crossed the removed footage. + let scene = scene_with_clips(&[("a.mp4", 0.0, 1.0), ("a.mp4", 2.0, 2.05), ("a.mp4", 2.05, 4.0)]); + assert_eq!(scene.cursor_chain_start(1), Some(1)); + assert_eq!(scene.cursor_chain_start(2), Some(1)); + assert_eq!(scene.cursor_break_for_clip(1), Some(2.0)); + assert_eq!(scene.cursor_break_for_clip(2), Some(2.0), "the chain's restart, not none"); + } } diff --git a/crates/compositor/src/timeline_walk.rs b/crates/compositor/src/timeline_walk.rs index 3ef4f3bad2..e1efc9aebd 100644 --- a/crates/compositor/src/timeline_walk.rs +++ b/crates/compositor/src/timeline_walk.rs @@ -528,6 +528,11 @@ unsafe fn walk_timeline( } if let Some(Some(raw)) = raw_cursor_tracks.get(&raw_key) { let breaks: Vec = clip_break.into_iter().collect(); + // One smoothed track at a time: each spans the whole recording (two 240 Hz + // lists, about 20 MB per hour), and a timeline with many cuts would keep one + // per cut until the export ends. Only the playing clip's is read; the raw + // telemetry stays cached, so coming back to a chain costs one spring pass. + cursor_tracks.clear(); cursor_tracks.insert(key.clone(), raw.smoothed_with_breaks(cursor_smoothing, &breaks)); } // absente/illisible → pas d'entrée : ce clip s'exporte sans curseur (visible, From 7b20625551633c5f0c6a3c14af333f88a80f4639 Mon Sep 17 00:00:00 2001 From: "alexey.lovchikov" Date: Sun, 22 Nov 2026 18:55:14 +0500 Subject: [PATCH 4/5] docs(testing): log the Windows partial pass for the cursor restart Co-Authored-By: Claude Fable 5.2 --- technical-documentation/testing/manual-e2e-checklist.md | 1 + 1 file changed, 1 insertion(+) diff --git a/technical-documentation/testing/manual-e2e-checklist.md b/technical-documentation/testing/manual-e2e-checklist.md index 6b46ffc4ea..6b9016116d 100644 --- a/technical-documentation/testing/manual-e2e-checklist.md +++ b/technical-documentation/testing/manual-e2e-checklist.md @@ -1128,3 +1128,4 @@ These checks are the human slice: an agent cannot click Apple's picker (see abov | 2026-11-17 | Exact public `v2.3.0-rc.6` @ `a4641a0b7`, AppImage SHA256 `7c79f01496c1d448807f2e24477b4dfb6ce69886edb7ef0ffef591de3acac363` | Vivo Ubuntu24.04/GNOME46 Wayland | **Fail — residual cross-file ruler seek stops playback** | Final scope follows the maintainer's RC5→RC6 diff request; observed unaffected RC5 passes are reused, never-observed unrelated checks remain uncovered. Two distinct synthetic H264/AAC files: same-asset seek keeps playing; native ruler seeks in both cross-file directions stop. Passive trace has pointerup64.2ms after pointerdown on a replacement video still paused/ready0, then metadata63.5ms later with no media error or resume. This exposes a gap in the original seek fix; the earlier Mac counterproof used clips from the same file. Public package/native hashes are unchanged. Follow-up source PR1613 and frozen-branch PR1614 address queued intent and premature play; this row remains the public RC6 verdict. Other essential exact-package capture/export/audio checks remain separately recorded. Unrelated/hardware cases are excluded. Evidence: takeover/linux/seek-one-control-provenance.json, seek-passive-event-trace-normalized.json, seek-two-assets.openscreen and C227. | | 2026-11-17 | Tag `a4641a0b7` plus only PR1613 head `22b5daeee`; private final renderer V2 SHA256 `0be50bc95ce5b5b11ace96d61440c14fe73ea534876cc01a330a8f19fe17fb32`, runtime patch ID `ecc90aba7c7c43d267946492a23cba2169485d4e` | Windows11 x64 and Vivo Linux Wayland; Mac counterproof skipped | **Pass for four targeted controls on Windows and Vivo; Partial overall** | Real OS input with distinct real files/asset IDs passes playing ruler seeks in both directions, rapid cross-file drag and initially paused seek. Windows reproduces the preserved V1 drag failure (jump to programme end in269ms) successfully with V2 in263ms, then time advances; paused seek remains fixed after1s. Vivo's forward/reverse/drag clocks advance and paused seek stays fixed across two reads. All124 renderer hashes match, native sets remain complete/unchanged and public RC6 is untouched; Windows verifies149-file runtimes and24 natives before/after. Two original unit cases and the later three-seek cancellation case fail before their fixes; final104 targeted tests, both TypeScript configurations, lint and build pass. Main PR1613 is merged with green CI and independent review PASS; frozen-branch PR1614 contains only the same four source/test files. Its tracked tree equals the compiled QA source tree. **Skipped:** Mac fixed-renderer execution, because the initial external-renderer QA route stayed on an empty target with no renderer request; no seek ran, no signature/quarantine/security change or alternate route was attempted. A signed future candidate remains needed for Mac. No new capture/benchmark/hardware matrix or whole-file Pass. Evidence: takeover/windows/RC6-CROSS-ASSET-SCRUB.md, takeover/linux/seek-v2-native-counterproof.json, takeover/mac/MAC-CROSS-ASSET-SCRUB-FIX.md and renderer manifests. | | 2026-11-17 | Official signed ARM64 `v2.3.0-rc.7` @ `460ea875510d9b54f0a298047817797f4d038d79`, CI44080825057; DMG SHA256 `a39e46e405d8aa890cc477e03765920abe450a2f7f14ddddd44523e4c046f09a` | macOS27.0.1, Apple M1 | **Partial — targeted cross-file seek 4/4 Pass** | Version7/hash verified; codesign deep/strict valid, Gatekeeper Notarized Developer ID accepted. Intact read-only mounted bundle launched via normal LaunchServices open -n with isolated QA profile, no VITE/addon/ORT/DYLD override; file renderer and bundled compositor/FFmpeg paths confirmed. Real OS inputs on two distinct files/assetIds (10s A + first10s of58.4s B): playing click A→B and B→A, real scrub drag6→14 across10s boundary, initially paused cross-file seek remains paused. Correct file src, readyState4, no video error; both timeline and video clocks advance in all three playing cases, paused values remain unchanged800ms. Native A/B still images inspected with rendered mire/source clocks. No recording/export/camera/install/security or general-checklist work. QA quit normally, original installed app preserved. RC7-MAC-SEEK.md and rc7-cross-asset-scrub/validation.json. Public release published 2026-11-17T12:42:05Z with16assets; tested DMG and updater ZIP match public SHA256/size, all three update feeds name2.3.0-rc.7 and Mac SHA512 matches the CI sidecar. Previously observed unaffected RC5/RC6 controls are reused; no whole-checklist claim or stable promotion. | +| 2026-11-22 | PR #1651 (`fix/cursor-spring-clip-breaks` @ `78d04b3e0`), run as part of an Electron dev build of `feat/cursor-motion-lane` (which contains these commits) with the unrelated PR #1521 merged locally; compositor addon rebuilt from that tree | Windows 11 Pro 26300, 1920×1080, NVIDIA RTX 4060 Ti (D3D11) | **Partial — cursor across a trim: passed** | Human-driven, no computer-use. One real 20 s take with a 0.94 s trim over a moving pointer, cursor smoothing at 100 %. Before the fix (a build without it, screen-captured and read frame by frame): the first frame after the cut is on the pointer at (0.469, 0.428) of the frame, the next one is back at (0.448, 0.396), then it catches up over about half a second; a replay of the spring on the take's own `.cursor.json` gives (0.452, 0.392) for that second frame without a restart and (0.473, 0.421) with one. With the fix: no jump after the cut in the preview and in an exported MP4, by eye, not measured. Skipped: a cut followed by a plain split (no editor action splits a clip without removing footage; unit tests only), macOS, Linux, and every other section of this checklist. | From 3720ab2f6f77674118ee79436303919ce629eb86 Mon Sep 17 00:00:00 2001 From: "alexey.lovchikov" Date: Wed, 25 Nov 2026 22:15:26 +0500 Subject: [PATCH 5/5] perf(preview): keep the smoothed cursor tracks across cuts The preview smoothed the whole cursor track again whenever the break the spring restarts at changed, on the render thread: at every crossing of a cut, played or scrubbed. That is one pass over the recording, 62 ms for an hour of footage on the frame the cut shows. The tracks it built are now kept, the four most recently used, keyed by the telemetry, the smoothing and the breaks. Crossing a cut again installs a copy: 2.2 ms for an hour, 0.003 ms for 32 s. Co-Authored-By: Claude Fable 5.2 --- crates/compositor/src/live.rs | 175 ++++++++++++++++++++++++++++++++-- 1 file changed, 169 insertions(+), 6 deletions(-) diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs index a160a97808..df06c8552f 100644 --- a/crates/compositor/src/live.rs +++ b/crates/compositor/src/live.rs @@ -893,13 +893,78 @@ fn oriented_cursor(track: CursorTrack, turned: bool) -> CursorTrack { if turned { track.turned() } else { track } } +/// How many smoothed tracks `SmoothedCursors` keeps. Each is the whole recording at 240 Hz, about +/// 20 MB for an hour of footage, so the cap bounds memory. Scrubbing back and forth over a cut +/// needs two. +const SMOOTHED_CURSORS_CAP: usize = 4; + +/// What a smoothed track was built from: the telemetry (`cursor_key`), the smoothing, and where +/// the spring restarts (`cursor_breaks`). +#[derive(Clone, PartialEq)] +struct SmoothedCursorKey { + track: String, + smoothing: f32, + breaks: Vec, +} + +/// The smoothed cursor tracks the preview already built, the most recently used last. +/// +/// Smoothing is one spring pass over the whole recording, on the render thread, and the break +/// the spring restarts at changes with the chain of clips that plays. Without this, every +/// crossing of a cut, played or scrubbed, paid that pass again: tens of milliseconds on an hour +/// of footage, at the very frame the cut shows. +#[derive(Default)] +struct SmoothedCursors { + entries: Vec<(SmoothedCursorKey, CursorTrack)>, +} + +impl SmoothedCursors { + /// The track for `key`, from `build` the first time and from memory afterwards. + fn get_or_build( + &mut self, + key: SmoothedCursorKey, + build: impl FnOnce() -> CursorTrack, + ) -> CursorTrack { + if let Some(found) = self.entries.iter().position(|(k, _)| *k == key) { + let entry = self.entries.remove(found); + let track = entry.1.clone(); + self.entries.push(entry); + return track; + } + let track = build(); + if self.entries.len() >= SMOOTHED_CURSORS_CAP { + self.entries.remove(0); + } + self.entries.push((key, track.clone())); + track + } + + /// `raw`, the telemetry `track_key` names, smoothed with these breaks. + fn smoothed( + &mut self, + raw: &CursorTrack, + track_key: &str, + smoothing: f32, + breaks: &[f32], + ) -> CursorTrack { + let key = SmoothedCursorKey { + track: track_key.to_string(), + smoothing, + breaks: breaks.to_vec(), + }; + self.get_or_build(key, || raw.smoothed_with_breaks(smoothing, breaks)) + } +} + /// Rereads the active clip's cursor telemetry when its key changed (another file, or the same /// file turned the other way) and hands it to the compositor already smoothed, so a paused /// preview never shows one unsmoothed frame. Returns whether it reread. +#[allow(clippy::too_many_arguments)] fn reload_cursor_if_changed( comp: &Compositor, raw_cursor: &mut Option, loaded_cursor_path: &mut String, + smoothed_cursors: &mut SmoothedCursors, screen_path: &str, turned: bool, smoothing: f32, @@ -930,15 +995,20 @@ fn reload_cursor_if_changed( // curseur (`Compositor::resized`), et le relisser parcourt tout // l'enregistrement à chaque bascule d'un scrub. match raw_cursor { - Some(track) => comp.set_cursor(track.smoothed_with_breaks(smoothing, breaks)), + Some(track) => comp.set_cursor(smoothed_cursors.smoothed( + track, + loaded_cursor_path, + smoothing, + breaks, + )), None => comp.clear_cursor(), } true } /// Where the cursor spring restarts while clip `clip_index` plays -/// (`Scene::cursor_break_for_clip`). It changes with the active clip, so the track is smoothed -/// again on a clip change even when the file stays the same: one spring pass over the recording. +/// (`Scene::cursor_break_for_clip`). It changes with the chain of clips that plays, so the same +/// file needs another smoothed track there: built once, then kept (`SmoothedCursors`). fn cursor_breaks(scene: Option<&Scene>, clip_index: usize) -> Vec { scene.and_then(|s| s.cursor_break_for_clip(clip_index)).into_iter().collect() } @@ -1526,6 +1596,7 @@ unsafe fn advance_to_next_scene_clip( active_clip_index: &mut usize, raw_cursor: &mut Option, loaded_cursor_path: &mut String, + smoothed_cursors: &mut SmoothedCursors, smoothing: f32, last_smoothing: &mut f32, last_breaks: &mut Vec, @@ -1606,7 +1677,12 @@ unsafe fn advance_to_next_scene_clip( // the raw track and correcting it on the next loop turn showed one frame elsewhere. let breaks = cursor_breaks(Some(scene), next_index); match raw_cursor { - Some(track) => comp.set_cursor(track.smoothed_with_breaks(smoothing, &breaks)), + Some(track) => comp.set_cursor(smoothed_cursors.smoothed( + track, + loaded_cursor_path, + smoothing, + &breaks, + )), None => comp.clear_cursor(), } *last_smoothing = smoothing; @@ -1715,6 +1791,7 @@ unsafe fn render_thread( let mut last_smoothing: f32 = -1.0; // force la 1re application (0.0 est une valeur valide) // The breaks the current track was smoothed with (`cursor_breaks`). let mut last_breaks: Vec = Vec::new(); + let mut smoothed_cursors = SmoothedCursors::default(); // Fenêtre de stabilisation après un changement EN PAUSE. Un seul recompose ne suffit pas // quand l'effet demandé est asynchrone : la segmentation webcam (détourage / flou / fond // personnalisé) démarre son worker au 1er compose, ne SOUMET la frame qu'au 2e et ne @@ -1833,6 +1910,7 @@ unsafe fn render_thread( &comp, &mut raw_cursor, &mut loaded_cursor_path, + &mut smoothed_cursors, &active_screen_path, turned, ip.cursor_smoothing, @@ -1910,6 +1988,7 @@ unsafe fn render_thread( &comp, &mut raw_cursor, &mut loaded_cursor_path, + &mut smoothed_cursors, &active_screen_path, turned, ip.cursor_smoothing, @@ -1926,7 +2005,12 @@ unsafe fn render_thread( if let Some(raw) = &raw_cursor { let breaks = cursor_breaks(full_scene.as_ref(), active_clip_index); if ip.cursor_smoothing != last_smoothing || breaks != last_breaks { - comp.set_cursor(raw.smoothed_with_breaks(ip.cursor_smoothing, &breaks)); + comp.set_cursor(smoothed_cursors.smoothed( + raw, + &loaded_cursor_path, + ip.cursor_smoothing, + &breaks, + )); last_smoothing = ip.cursor_smoothing; last_breaks = breaks; } @@ -2067,6 +2151,7 @@ unsafe fn render_thread( &mut active_clip_index, &mut raw_cursor, &mut loaded_cursor_path, + &mut smoothed_cursors, ip.cursor_smoothing, &mut last_smoothing, &mut last_breaks, @@ -2100,6 +2185,7 @@ unsafe fn render_thread( &mut active_clip_index, &mut raw_cursor, &mut loaded_cursor_path, + &mut smoothed_cursors, ip.cursor_smoothing, &mut last_smoothing, &mut last_breaks, @@ -2484,7 +2570,11 @@ pub fn run_standalone(_screen: &str, _webcam: &str, _cursor_json: &str) -> anyho #[cfg(test)] mod tests { - use super::{cursor_key, open_settle_window, should_settle, SETTLE_STEP, SETTLE_WINDOW}; + use super::{ + cursor_key, open_settle_window, should_settle, SmoothedCursorKey, SmoothedCursors, + SETTLE_STEP, SETTLE_WINDOW, SMOOTHED_CURSORS_CAP, + }; + use crate::cursor::CursorTrack; use std::time::Instant; #[test] @@ -2493,6 +2583,79 @@ mod tests { assert_ne!(cursor_key("a.mp4", false), cursor_key("a.mp4", true)); } + /// A pointer crossing the frame in four seconds, sampled at 30 Hz. + fn moving_cursor() -> CursorTrack { + let samples = (0..=120).map(|i| (i as f32 / 30.0, i as f32 / 120.0, 0.5)).collect(); + CursorTrack::new(samples, Vec::new(), Vec::new()) + } + + fn smoothed_key(track: &str, breaks: &[f32]) -> SmoothedCursorKey { + SmoothedCursorKey { track: track.to_string(), smoothing: 0.5, breaks: breaks.to_vec() } + } + + /// How many of `keys`, asked in order, had to be built. + fn builds(cache: &mut SmoothedCursors, keys: &[SmoothedCursorKey]) -> usize { + let mut built = 0; + for key in keys { + cache.get_or_build(key.clone(), || { + built += 1; + moving_cursor() + }); + } + built + } + + /// The cost this removes: scrubbing back and forth over a cut asked for the same two tracks + /// again and again, and each request was a spring pass over the whole recording. + #[test] + fn a_smoothed_cursor_is_built_once_per_track_smoothing_and_breaks() { + let mut cache = SmoothedCursors::default(); + let before_cut = smoothed_key("a", &[0.0]); + let after_cut = smoothed_key("a", &[2.0]); + let crossings = + [before_cut.clone(), after_cut.clone(), before_cut.clone(), after_cut.clone()]; + assert_eq!(builds(&mut cache, &crossings), 2); + + let turned = smoothed_key("a#turned", &[2.0]); + let stronger = SmoothedCursorKey { smoothing: 0.8, ..after_cut.clone() }; + assert_eq!(builds(&mut cache, &[turned, stronger]), 2, "another track or smoothing"); + } + + #[test] + fn the_smoothed_cursors_kept_are_the_most_recently_used() { + let mut cache = SmoothedCursors::default(); + let keys: Vec = + (0..=SMOOTHED_CURSORS_CAP).map(|i| smoothed_key("a", &[i as f32])).collect(); + assert_eq!(builds(&mut cache, &keys[..SMOOTHED_CURSORS_CAP]), SMOOTHED_CURSORS_CAP); + // Using the oldest again makes the second oldest the one a new track pushes out. + assert_eq!(builds(&mut cache, &keys[..1]), 0); + assert_eq!(builds(&mut cache, &keys[SMOOTHED_CURSORS_CAP..]), 1); + assert_eq!(builds(&mut cache, &keys[..1]), 0, "the one used again is still there"); + assert_eq!(builds(&mut cache, &keys[1..2]), 1, "the second oldest is gone"); + } + + /// What comes out of the cache is the track the spring gives for those breaks, the first + /// time and every time after it. + #[test] + fn a_kept_smoothed_cursor_is_the_one_its_breaks_give() { + let raw = moving_cursor(); + let mut cache = SmoothedCursors::default(); + for _ in 0..2 { + for breaks in [vec![], vec![2.0f32]] { + let expected = raw.smoothed_with_breaks(0.5, &breaks); + let got = cache.smoothed(&raw, "a", 0.5, &breaks); + for t in [0.5f32, 2.0, 2.1, 3.5] { + assert_eq!(got.at(t), expected.at(t), "breaks {breaks:?} at {t}"); + } + } + } + // The two differ where it matters: right after the break the restarted spring is on the + // pointer, the unbroken one still trails it. + let unbroken = cache.smoothed(&raw, "a", 0.5, &[]).at(2.0).unwrap(); + let restarted = cache.smoothed(&raw, "a", 0.5, &[2.0]).at(2.0).unwrap(); + assert!(restarted.0 - unbroken.0 > 0.01, "{restarted:?} against {unbroken:?}"); + } + /// Le bug d'origine : en pause, un seul recompose par changement, donc le masque de /// segmentation (asynchrone, 3 composes de latence) n'arrivait jamais avant un scrub. #[test]