Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(preview): keep the cursor moving while a screen frame is held

Marc Diaz3017c825e735
diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs
index ca1645605..206b1cac5 100644
--- a/crates/compositor/src/live.rs
+++ b/crates/compositor/src/live.rs
@@ -632,14 +632,26 @@ impl Player {
/// exactement à 60fps (30/59.94/etc. sont courants), au lieu de suivre le pts réel du
/// décodeur — exactement la cause du "zoom désynchronisé de la timeline" observé.
unsafe fn sync_time(&self, comp: &Compositor) {
- let t = self.sdec.cur_time_sec() as f32;
- comp.set_cursor_time(Some(t));
- comp.set_timeline_time(Some(t));
- comp.set_programme_time(self.programme_time());
+ self.sync_time_at(comp, self.sdec.cur_time_sec());
+ }
+
+ unsafe fn sync_time_at(&self, comp: &Compositor, t: f64) {
+ comp.set_cursor_time(Some(t as f32));
+ comp.set_timeline_time(Some(t as f32));
+ comp.set_programme_time(self.programme.as_ref().map(|c| c.at(t) as f32));
}
/// Recompose la frame courante (déjà décodée) — rafraîchit après un changement de param.
pub unsafe fn recompose(&self, comp: &Compositor, cfg: &Cfg) -> Result<bool> {
+ self.recompose_at(comp, cfg, self.sdec.cur_time_sec())
+ }
+
+ /// Recomposes the current frame with the cursor, zooms and programme time at `clock_sec`
+ /// instead of the frame's pts. ScreenCaptureKit only writes a frame when the screen
+ /// changes, and the editable cursor is not part of it, so composing only on new screen
+ /// frames froze the cursor through every still stretch (up to 1 s in real recordings).
+ /// Export never had this: it sets the cursor time per output frame (`timeline_walk.rs`).
+ pub unsafe fn recompose_at(&self, comp: &Compositor, cfg: &Cfg, clock_sec: f64) -> Result<bool> {
if !self.has_current_frame {
return Ok(false);
}
@@ -648,7 +660,7 @@ impl Player {
if sf.is_null() || wf.is_null() {
return Ok(false);
}
- self.sync_time(comp);
+ self.sync_time_at(comp, clock_sec);
let f = self.idx.saturating_sub(1);
comp.compose_frame(sf, wf, f as f32, cfg)?;
Ok(true)
@@ -1284,6 +1296,19 @@ fn should_settle(now: Instant, settle_until: Option<Instant>, last_settle: Insta
&& now.duration_since(last_settle) >= SETTLE_STEP
}
+const HOLD_RECOMPOSE_STEP: Duration = Duration::from_micros(16_667);
+
+fn hold_clock_sec(screen_time_sec: f64, acc: f64, clip_end_sec: Option<f64>) -> f64 {
+ let t = screen_time_sec + acc;
+ clip_end_sec.map_or(t, |end| t.min(end)).max(screen_time_sec)
+}
+
+/// `acc > 0`: at the end of the programme `acc` is reset every tick, and the same frame
+/// should not be recomposed at 60 Hz.
+fn should_recompose_hold(now: Instant, last_compose: Instant, acc: f64) -> bool {
+ acc > 0.0 && now.duration_since(last_compose) >= HOLD_RECOMPOSE_STEP
+}
+
/// Le clip que la lecture libre enchaîne après `active`. Aucun après le dernier : le programme
/// s'arrête là, comme la tête de lecture de l'app. Reboucler sur le premier emmenait la vue
/// où l'app ne la suivait pas, et ses seeks suivants, qui ne nomment pas de clip, cherchaient
@@ -1539,6 +1564,7 @@ unsafe fn render_thread(
let mut last = Instant::now();
let mut acc = 0.0f64;
+ let mut last_play_compose = Instant::now();
let mut first = true;
let mut last_preview_size: (u32, u32) = (0, 0);
let mut last_ip: Option<InspectorParams> = None;
@@ -1921,6 +1947,19 @@ unsafe fn render_thread(
break;
}
}
+ if !stepped && should_recompose_hold(now, last_play_compose, acc) {
+ let clip_end_sec = full_scene
+ .as_ref()
+ .and_then(|scene| scene.clips.get(active_clip_index))
+ .map(|clip| clip.source_end_sec);
+ let clock_sec = hold_clock_sec(player.screen_time_sec(), acc, clip_end_sec);
+ if let Ok(true) = player.recompose_at(&comp, &cfg, clock_sec) {
+ stepped = true;
+ }
+ }
+ if stepped {
+ last_play_compose = now;
+ }
} else if first || ip_changed || scene_changed || clip_changed || resized || fx_landed {
// pause : recompose la frame courante (param / scène / clip / résolution changés).
(settle_until, last_settle) = open_settle_window(now);
@@ -2299,6 +2338,29 @@ mod tests {
assert!(!should_settle(t0 + SETTLE_WINDOW, until, last), "puis la fenêtre se referme");
}
+ #[test]
+ fn a_held_frame_is_composed_at_the_playback_clock() {
+ assert_eq!(hold_clock_sec(10.0, 0.4, Some(20.0)), 10.4);
+ assert_eq!(hold_clock_sec(10.0, 0.4, None), 10.4, "no scene: no clip window");
+ }
+
+ #[test]
+ fn the_hold_clock_stops_at_the_end_of_the_clip() {
+ assert_eq!(hold_clock_sec(10.0, 0.4, Some(10.25)), 10.25);
+ assert_eq!(hold_clock_sec(10.0, 0.4, Some(9.0)), 10.0, "never before the held frame's pts");
+ }
+
+ #[test]
+ fn a_held_frame_is_recomposed_at_60_hz_and_only_when_the_clock_moved() {
+ let t0 = Instant::now();
+ assert!(!should_recompose_hold(t0 + HOLD_RECOMPOSE_STEP / 2, t0, 0.1), "too early");
+ assert!(should_recompose_hold(t0 + HOLD_RECOMPOSE_STEP, t0, 0.1), "due");
+ assert!(
+ !should_recompose_hold(t0 + HOLD_RECOMPOSE_STEP, t0, 0.0),
+ "end of programme: the clock no longer moves"
+ );
+ }
+
use super::*;
fn multiclip_scene() -> Scene {
diff --git a/crates/compositor/tests/held_frame_clock.rs b/crates/compositor/tests/held_frame_clock.rs
new file mode 100644
index 000000000..96380e147
--- /dev/null
+++ b/crates/compositor/tests/held_frame_clock.rs
@@ -0,0 +1,69 @@
+//! Not run in CI: needs a GPU and a video source, otherwise it skips. Any MP4 of 4 s or more:
+//!
+//! ```sh
+//! OPENSCREEN_LIVE_SOURCE=/path/source.mp4 cargo test -p openscreen-compositor --test held_frame_clock -- --nocapture
+//! ```
+
+#![cfg(any(windows, target_os = "macos"))]
+
+use openscreen_compositor::compositor::Compositor;
+use openscreen_compositor::config;
+use openscreen_compositor::d3d::Gpu;
+use openscreen_compositor::frame_geometry::live_params_from_scene;
+use openscreen_compositor::live::Player;
+use openscreen_compositor::scene::Scene;
+
+fn scene_json(source: &str) -> String {
+ let s = source.replace('\\', "/");
+ format!(
+ r##"{{
+ "clips": [{{"screenPath":"{s}","webcamPath":"","sourceStartSec":0,"sourceEndSec":100000,"webcamOffsetSec":0,"hasAudio":false}}],
+ "layout": {{"preset":"no-webcam","webcamSize":1.0,"webcamShape":"rounded","webcamMirror":false,"webcamPosition":null,"webcamReactiveZoom":false}},
+ "effects": {{"padding":0.1,"blur":false,"shadow":0.0,"roundnessFrac":0,"motionBlur":0.0}},
+ "background": {{"kind":"color","color":"#303030"}},
+ "zoomRegions": [],
+ "speedRegions": [],
+ "cursor": {{"show":false,"size":1,"smoothing":0,"motionBlur":0,"clickBounce":0,"clipToBounds":false,"theme":"default"}},
+ "cropByClip": [null],
+ "output": {{"width":480,"height":270,"fps":30}}
+ }}"##
+ )
+}
+
+#[test]
+fn a_held_frame_follows_the_playback_clock() {
+ let Ok(source) = std::env::var("OPENSCREEN_LIVE_SOURCE") else {
+ println!("SKIP: set OPENSCREEN_LIVE_SOURCE (see the file header).");
+ return;
+ };
+ let gpu = Gpu::create(false).expect("device");
+ let mut cfg = config::all().pop().expect("at least one config");
+ cfg.zoom = false;
+ cfg.layout_anim = false;
+ let comp = Compositor::new_sized(&gpu, 480, 270).expect("compositor");
+ let scene = Scene::from_json(&scene_json(&source)).expect("valid scene");
+ comp.set_live_params(live_params_from_scene(&scene));
+ comp.set_scene(Some(scene.clone()));
+ comp.clear_cursor();
+
+ unsafe {
+ let mut player = Player::open(&source, "", &gpu).expect("open the source");
+ player.set_programme_clock(Some(&scene), 0);
+ assert!(player.present_frame(&comp, &cfg, 1.0).expect("seek"), "paused seek");
+ let held = player.screen_time_sec();
+ let at_pts = player.programme_time().expect("programme clock");
+
+ comp.set_programme_time(None);
+ assert!(player.recompose_at(&comp, &cfg, held + 0.25).expect("recompose_at"));
+ let advanced = comp.programme_time().expect("recompose_at did not set the programme time");
+ assert_eq!(player.screen_time_sec(), held, "the screen frame must stay the held one");
+ assert!(
+ (advanced as f64 - at_pts as f64 - 0.25).abs() < 1e-3,
+ "programme time {advanced} instead of {at_pts} + 0.25 s"
+ );
+
+ comp.set_programme_time(None);
+ assert!(player.recompose(&comp, &cfg).expect("recompose"));
+ assert_eq!(comp.programme_time(), Some(at_pts), "recompose stays at the pts");
+ }
+}