Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Merge branch 'main' into perf/scrub-preview-frames

Richie McIlroy4d1ff9dfa0c8
diff --cc crates/editor/src/editor_instance.rs
index 937f70125,0c4abd9db..a5126e9bd
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@@ -27,33 -26,20 +27,46 @@@ use tracing::warn
pub(super) const PREVIEW_RENDER_MAX_ATTEMPTS: u32 = 3;
pub(super) const PREVIEW_RENDER_RETRY_DELAY_MS: u64 = 120;
const PREVIEW_CURSOR_CACHE_CAPACITY: usize = 2;
+const PREVIEW_SCRUB_FRAME_INTERVAL: Duration = Duration::from_millis(33);
+
+type PreviewRequest = Option<PreviewFrameInstruction>;
+
+// A scrub sends a new request faster than a seek can decode. Abandoning the
+// in-flight frame for every newer request starves the preview until the mouse
+// stops, so a newer request only wins while a frame was presented recently.
+fn preview_frame_overdue(last_presented: Option<Instant>) -> bool {
+ last_presented.is_none_or(|at| at.elapsed() >= PREVIEW_SCRUB_FRAME_INTERVAL)
+}
+
+fn preview_superseded(
+ preview_rx: &watch::Receiver<PreviewRequest>,
+ last_presented: Option<Instant>,
+) -> bool {
+ preview_rx.has_changed().unwrap_or(false) && !preview_frame_overdue(last_presented)
+}
+
+async fn preview_superseded_while_fresh(
+ mut probe: watch::Receiver<PreviewRequest>,
+ last_presented: Option<Instant>,
+) {
+ if probe.changed().await.is_err() || preview_frame_overdue(last_presented) {
+ std::future::pending::<()>().await;
+ }
+}
+ pub(super) fn cursor_timeline_settings(cursor: &cap_project::CursorConfiguration) -> [u32; 7] {
+ let click_spring = cursor.click_spring_config();
+ [
+ u32::from(cursor.raw),
+ cursor.tension.to_bits(),
+ cursor.mass.to_bits(),
+ cursor.friction.to_bits(),
+ click_spring.tension.to_bits(),
+ click_spring.mass.to_bits(),
+ click_spring.friction.to_bits(),
+ ]
+ }
+
#[derive(Default)]
pub(super) struct PreviewCursorCache {
entries: Vec<PreviewCursorCacheEntry>,
@@@ -1010,9 -1011,9 +1038,10 @@@ impl EditorInstance
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut prefetch_cancel_token: Option<CancellationToken> = None;
+ let mut last_presented: Option<Instant> = None;
let mut cursor_cache = PreviewCursorCache::default();
let mut project_rx = self.project_config.1.clone();
+ let mut project = Arc::new(project_rx.borrow_and_update().clone());
let mut zoom_timelines = HashMap::<u32, ZoomTransformTimeline>::new();
let mut outgoing_zoom_timelines = HashMap::<u32, ZoomTransformTimeline>::new();
@@@ -1143,18 -1150,26 +1178,29 @@@
.unwrap_or_default();
let outgoing_frames = tokio::select! {
biased;
- _ = preview_rx.changed() => {
+ _ = preview_superseded_while_fresh(
+ preview_rx.clone(),
+ last_presented,
+ ) => {
continue;
}
- frames = outgoing_media.decoders.get_frames_initial(
- outgoing.source_time as f32,
- project.requires_camera(),
- true,
- outgoing_offsets,
- ) => frames,
+ frames = async {
+ let decoders = if outgoing.segment.recording_clip
+ == segment.recording_clip
+ {
+ outgoing_media.decoders.transition_side().await
+ } else {
+ &outgoing_media.decoders
+ };
+ decoders
+ .get_frames_initial(
+ outgoing.source_time as f32,
+ project.requires_camera(),
+ true,
+ outgoing_offsets,
+ )
+ .await
+ } => frames,
};
if let Some(outgoing_frames) = outgoing_frames {
let outgoing_zoom = outgoing_zoom_timelines