Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
384 changes: 354 additions & 30 deletions crates/compositor/src/cursor.rs

Large diffs are not rendered by default.

37 changes: 34 additions & 3 deletions crates/compositor/src/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ fn reload_cursor_if_changed(
screen_path: &str,
turned: bool,
smoothing: f32,
shape: &CursorShape,
) -> bool {
let key = cursor_key(screen_path, turned);
if key == *loaded_cursor_path {
Expand All @@ -929,12 +930,30 @@ 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.shaped(smoothing, &shape.0, &shape.1)),
None => comp.clear_cursor(),
}
true
}

/// What reshapes a cursor track besides the smoothing slider (`CursorTrack::shaped`): where its
/// spring restarts and the editor's cursor-motion sections, both for one recording file.
type CursorShape = (Vec<f32>, Vec<crate::cursor::MotionRegion>);

/// The break is the playing clip's own start (`Scene::cursor_break_for_clip`), so it changes
/// with the active clip and the track is shaped again on a clip change even when the file stays
/// the same: one spring pass over the recording.
fn cursor_shape_inputs(scene: Option<&Scene>, clip_index: usize, screen_path: &str) -> CursorShape {
scene
.map(|s| {
(
s.cursor_break_for_clip(clip_index).into_iter().collect(),
s.cursor.motion_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(
Expand Down Expand Up @@ -1699,6 +1718,8 @@ unsafe fn render_thread(
let mut last_preview_size: (u32, u32) = (0, 0);
let mut last_ip: Option<InspectorParams> = None;
let mut last_smoothing: f32 = -1.0; // force la 1re application (0.0 est une valeur valide)
// The breaks and motion sections the current track was shaped with (`cursor_shape_inputs`).
let mut last_shape: CursorShape = Default::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
Expand Down Expand Up @@ -1812,15 +1833,18 @@ 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 shape = cursor_shape_inputs(full_scene.as_ref(), active_clip_index, &active_screen_path);
if reload_cursor_if_changed(
&comp,
&mut raw_cursor,
&mut loaded_cursor_path,
&active_screen_path,
turned,
ip.cursor_smoothing,
&shape,
) {
last_smoothing = ip.cursor_smoothing;
last_shape = shape;
}
clip_changed = true;
}
Expand Down Expand Up @@ -1858,10 +1882,14 @@ 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 cursor-motion sections and 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 shape = cursor_shape_inputs(full_scene.as_ref(), active_clip_index, &active_screen_path);
if ip.cursor_smoothing != last_smoothing || shape != last_shape {
comp.set_cursor(raw.shaped(ip.cursor_smoothing, &shape.0, &shape.1));
last_smoothing = ip.cursor_smoothing;
last_shape = shape;
}
}
// un changement de param doit se voir même en pause (édition live des sliders) :
Expand Down Expand Up @@ -1894,15 +1922,18 @@ 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 shape = cursor_shape_inputs(full_scene.as_ref(), active_clip_index, &active_screen_path);
if reload_cursor_if_changed(
&comp,
&mut raw_cursor,
&mut loaded_cursor_path,
&active_screen_path,
turned,
ip.cursor_smoothing,
&shape,
) {
last_smoothing = ip.cursor_smoothing;
last_shape = shape;
}
}
// Le temps programme dépend du clip actif ET de la scène entière (durées des clips
Expand Down
173 changes: 173 additions & 0 deletions crates/compositor/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,76 @@ pub struct SceneCursor {
/// `#[serde(default)]` : champ ajouté après coup, absent des JSON de test existants.
#[serde(default)]
pub cursor_sprites: std::collections::HashMap<String, SceneCursorSprite>,
/// Per-section path overrides (`SceneCursorMotionRegion` app-side). `#[serde(default)]`: the
/// app omits the key when there are none.
#[serde(default)]
pub motion: Vec<SceneCursorMotionRegion>,
}

/// One cursor-motion region, in the SOURCE time of the recording `screen_path` names.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SceneCursorMotionRegion {
pub screen_path: String,
pub start_sec: f32,
pub end_sec: f32,
pub mode: SceneCursorMotionMode,
#[serde(default)]
pub strength: f32,
#[serde(default)]
pub easing: SceneCursorMotionEasing,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SceneCursorMotionMode {
Recorded,
Smooth,
Straight,
Freeze,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum SceneCursorMotionEasing {
#[default]
EaseInOut,
Linear,
EaseIn,
EaseOut,
}

impl SceneCursor {
/// The motion regions that apply to the cursor track of `screen_path`, for
/// `CursorTrack::shaped`. Paths compare as the live clip resolution compares them
/// (`same_source_path`): without regard to ASCII case.
pub fn motion_for(&self, screen_path: &str) -> Vec<crate::cursor::MotionRegion> {
use crate::cursor::{MotionEasing, MotionMode, MotionRegion};
self.motion
.iter()
.filter(|region| {
region.screen_path.eq_ignore_ascii_case(screen_path)
&& region.end_sec > region.start_sec
})
.map(|region| MotionRegion {
start: region.start_sec,
end: region.end_sec,
mode: match region.mode {
SceneCursorMotionMode::Recorded => MotionMode::Recorded,
SceneCursorMotionMode::Smooth => MotionMode::Smooth,
SceneCursorMotionMode::Straight => MotionMode::Straight,
SceneCursorMotionMode::Freeze => MotionMode::Freeze,
},
strength: region.strength.clamp(0.0, 1.0),
easing: match region.easing {
SceneCursorMotionEasing::EaseInOut => MotionEasing::EaseInOut,
SceneCursorMotionEasing::Linear => MotionEasing::Linear,
SceneCursorMotionEasing::EaseIn => MotionEasing::EaseIn,
SceneCursorMotionEasing::EaseOut => MotionEasing::EaseOut,
},
})
.collect()
}
}

/// Un sprite de curseur : image + point de pivot.
Expand Down Expand Up @@ -973,6 +1043,25 @@ pub struct Scene {
}

impl Scene {
/// Where the cursor spring restarts while clip `clip_index` plays
/// (`CursorTrack::shaped`): 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<f32> {
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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

pub fn layout_at(&self, t: f32) -> Option<SampledLayout<'_>> {
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))
Expand Down Expand Up @@ -1428,3 +1517,87 @@ mod annotation_tests {
);
}
}

#[cfg(test)]
mod cursor_motion_tests {
use super::*;

fn cursor(extra: serde_json::Value) -> SceneCursor {
let mut json = serde_json::json!({
"show": true, "size": 1.0, "smoothing": 0.3, "motionBlur": 0.0,
"clickBounce": 1.0, "clipToBounds": false, "theme": "default"
});
json.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
serde_json::from_value(json).unwrap()
}

#[test]
fn a_scene_without_motion_parses_with_none() {
assert!(cursor(serde_json::json!({})).motion.is_empty());
}

#[test]
fn motion_regions_parse_and_are_matched_by_file() {
let c = cursor(serde_json::json!({ "motion": [
{ "screenPath": "a.mp4", "startSec": 1.0, "endSec": 2.0, "mode": "smooth",
"strength": 0.7, "easing": "linear" },
{ "screenPath": "a.mp4", "startSec": 3.0, "endSec": 4.0, "mode": "straight",
"strength": 0.5, "easing": "ease-out" },
{ "screenPath": "b.mp4", "startSec": 0.0, "endSec": 1.0, "mode": "freeze" },
{ "screenPath": "a.mp4", "startSec": 5.0, "endSec": 5.0, "mode": "recorded" }
]}));
let regions = c.motion_for("a.mp4");
assert_eq!(regions.len(), 2, "the other file and the empty span are left out");
assert_eq!(regions[0].mode, crate::cursor::MotionMode::Smooth);
assert!((regions[0].strength - 0.7).abs() < 1e-6);
assert_eq!(regions[1].easing, crate::cursor::MotionEasing::EaseOut);
assert_eq!(c.motion_for("A.MP4").len(), 2, "paths compare without regard to ASCII case");
let b = c.motion_for("b.mp4");
assert_eq!(b[0].mode, crate::cursor::MotionMode::Freeze);
assert_eq!(b[0].easing, crate::cursor::MotionEasing::EaseInOut, "absent easing defaults");
}
}

#[cfg(test)]
mod cursor_break_tests {
use super::*;

fn scene_with_clips(clips: &[(&str, f64, f64)]) -> Scene {
let clips: Vec<String> = 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");
}
}
34 changes: 28 additions & 6 deletions crates/compositor/src/timeline_walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, CursorTrack> = 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<String, Option<CursorTrack>> = HashMap::new();
let mut cursor_active_path: Option<String> = None;

let mut frames: u64 = 0;
Expand Down Expand Up @@ -506,13 +508,33 @@ 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 shaped 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. The editor's cursor-motion
// sections reshape it too (`CursorTrack::shaped`); they are the file's, the same for
// every clip on it.
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()),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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 };
cursor_tracks.insert(key.clone(), raw.smoothed(cursor_smoothing));
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<f32> = clip_break.into_iter().collect();
let motion = scene
.as_ref()
.map(|s| s.cursor.motion_for(&clip.screen))
.unwrap_or_default();
cursor_tracks.insert(key.clone(), raw.shaped(cursor_smoothing, &breaks, &motion));
}
// absente/illisible → pas d'entrée : ce clip s'exporte sans curseur (visible,
// pas masqué en un curseur fantôme d'un autre clip).
Expand Down
39 changes: 39 additions & 0 deletions electron/ai-edition/agent-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ describe("the mutating-tool table", () => {
"addAnnotation",
"addAudio",
"addCameraFullscreen",
"addCursorMotion",
"setCursorMotion",
"addEffect",
"addLayoutChange",
"addSpeed",
Expand Down Expand Up @@ -847,6 +849,43 @@ describe("executeAgentTool", () => {
expect(() => documentSchema.parse(result.document)).not.toThrow();
});

it("addCursorMotion / setCursorMotion / removeModifier round-trip a cursor motion section", () => {
const added = executeAgentTool(
fixtureDocument(),
"addCursorMotion",
JSON.stringify({ startSec: 2, endSec: 4, mode: "straight" }),
);
expect(added.ok).toBe(true);
const id = JSON.parse(added.resultJson).cursorMotionId as string;
expect(added.document?.cursorMotionRanges).toEqual([
expect.objectContaining({ id, startMs: 2000, endMs: 4000, mode: "straight" }),
]);
expect(() => documentSchema.parse(added.document)).not.toThrow();
const snapshot = documentSnapshotForModel(added.document as AxcutDocument) as {
cursorMotionRegions: unknown[];
};
expect(snapshot.cursorMotionRegions).toEqual([
{ id, startSec: 2, endSec: 4, mode: "straight", easing: "ease-in-out" },
]);

const set = executeAgentTool(
added.document as AxcutDocument,
"setCursorMotion",
JSON.stringify({ cursorMotionId: id, mode: "smooth", strength: 0.8 }),
);
expect(set.ok).toBe(true);
expect(set.document?.cursorMotionRanges?.[0]).toMatchObject({ mode: "smooth", strength: 0.8 });

const removed = executeAgentTool(
set.document as AxcutDocument,
"removeModifier",
JSON.stringify({ id }),
);
expect(removed.ok).toBe(true);
expect(JSON.parse(removed.resultJson).kind).toBe("cursorMotion");
expect(removed.document?.cursorMotionRanges).toEqual([]);
});

it("refuses a speed outside the range every reader plays", () => {
for (const speed of [100, 0.1]) {
const result = executeAgentTool(
Expand Down
Loading
Loading