forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-a5a3b9113629c64f8'

Browse files at this commit

84d0c15a823d2bd1b148965689720efb12d791a8Ahmet Kaan GümüşParent: dec2ced084f02d
crates/app/src/main.rsmodified+87 -2
@@ -1552,6 +1552,9 @@ struct Player {
15521552 titled: String,
15531553 displayed: u32,
15541554 dropped: u32,
1555+ /// When the picture was last restarted at the clock for falling behind it
1556+ /// ([`should_resync`]). The cool-down's only state.
1557+ resynced: Option<Instant>,
15551558 /// Wall clock of the first displayed frame -- the real-speed measurement.
15561559 started: Option<Instant>,
15571560 focus: FocusHandle,
@@ -1560,7 +1563,9 @@ struct Player {
15601563 impl Player {
15611564 /// Catches the display up to the clock: everything already due is taken off
15621565 /// the channel and only the last of them is shown, which *is* the
1563- /// drop-when-behind policy. A frame that is not due yet waits in `held`.
1566+ /// drop-when-behind policy. A frame that is not due yet waits in `held`, and
1567+ /// while the clock is paused *nothing* is due -- a repaint re-presents the
1568+ /// frame already on screen, whatever asked for the repaint.
15641569 fn pump(&mut self, window: &mut Window) {
15651570 // Where the transport was before this drain, so the crossing into
15661571 // `Ended` can be recognised as the one transition it is.
@@ -1572,7 +1577,20 @@ impl Player {
15721577 };
15731578 let target = session.now() * self.fps;
15741579 let mut newest: Option<Frame> = None;
1575- loop {
1580+ // A frame the screen is owed: a seek's landing, and the one readiness
1581+ // signal there is ([`Player::reset_after_reseek`]).
1582+ let owed = self.seek_since.is_some();
1583+ // Paused, the clock is frozen and *nothing new is due*. Whatever the
1584+ // decoder is still handing over is the backlog it was behind by when
1585+ // the pause landed -- frames at a position the transport has already
1586+ // left -- and taking one per repaint is what walked the picture on
1587+ // after the sound had stopped, at exactly the rate the pointer was
1588+ // moved over the timeline. Gated here, at the one place a frame ever
1589+ // reaches the screen, rather than in the handlers that repaint: a
1590+ // hover, a notice, a resize and a vsync are all the same event to this.
1591+ // An owed frame is still taken, playing or not -- a scrub is paused by
1592+ // definition, and its landing is the whole point of it.
1593+ while session.is_playing() || owed {
15761594 let frame = match self.held.take() {
15771595 Some(frame) => frame,
15781596 // Nothing waiting means either a clip boundary being rebuilt or
@@ -1592,6 +1610,15 @@ impl Player {
15921610 }
15931611 }
15941612
1613+ // How far behind the master clock the picture just handed over is, in
1614+ // seconds. Measured off a frame that really arrived and nothing else: a
1615+ // clip boundary being reopened delivers nothing at all for hundreds of
1616+ // milliseconds, and restarting *that* would only cancel the open it is
1617+ // waiting on.
1618+ let late = newest
1619+ .as_ref()
1620+ .map_or(0., |f| (target - f64::from(f.index)) / self.fps);
1621+
15951622 if let Some(frame) = newest {
15961623 self.displayed += 1;
15971624 self.seek_since = None;
@@ -1615,6 +1642,30 @@ impl Player {
16151642 }
16161643 }
16171644
1645+ // Audio is the master clock and a decoder that cannot keep up with it
1646+ // never gets back on its own: it hands over every frame in order,
1647+ // whether or not its moment has passed, so what it is behind by only
1648+ // grows -- a minute in, the picture is seconds behind what is being
1649+ // heard, and that is the whole of "the video can't catch the audio".
1650+ // Past `LATE_RESYNC` the backlog is abandoned and the picture restarted
1651+ // at the clock, which touches neither the sound nor the clock
1652+ // (`PlaybackSession::resync_picture`), so nothing the ear is following
1653+ // moves. Never on a frame a seek owed: that one is late by however long
1654+ // its own reopen took, and answering it with another reopen is a loop.
1655+ //
1656+ // ponytail: on a machine that cannot decode the file in real time at
1657+ // all this settles into one restart per `RESYNC_GAP` -- in sync, and
1658+ // stuttering, which is the honest picture of what that machine can do.
1659+ // The upgrade path is dropping late frames *inside* the worker (skip
1660+ // the convert and the send for anything already past due), which needs
1661+ // the deadline shared with it.
1662+ if !owed && session.is_playing() && should_resync(late, self.resynced) {
1663+ eprintln!("picture {late:.3}s behind the clock: restarting it there");
1664+ session.resync_picture();
1665+ self.held = None;
1666+ self.resynced = Some(Instant::now());
1667+ }
1668+
16181669 if self.transport() == Transport::Ended {
16191670 // A seek whose worker never produced a frame (vanished file) would
16201671 // otherwise repaint at vsync forever. Held clear for as long as the
@@ -6365,6 +6416,21 @@ fn scrub_due(target: u32, last_target: u32, since: Duration) -> bool {
63656416 target != last_target && since >= SCRUB_GAP
63666417 }
63676418
6419+/// How far the picture may fall behind the sound before it is restarted at the
6420+/// clock instead of left to crawl after it. Past what an eye reads as lip sync
6421+/// (a tenth of a second or so) and above what a single reopen costs to fix, so a
6422+/// picture that is merely a reopen behind is not answered with another one.
6423+const LATE_RESYNC: f64 = 0.4;
6424+/// The least time between two such restarts: the decoder that cannot keep up is
6425+/// the one this fires for, and it will still be behind straight afterwards.
6426+const RESYNC_GAP: Duration = Duration::from_secs(2);
6427+
6428+/// Whether a picture `late` seconds behind the master clock is restarted at it,
6429+/// given when the last restart was ([`Player::pump`]).
6430+fn should_resync(late: f64, last: Option<Instant>) -> bool {
6431+ late > LATE_RESYNC && last.is_none_or(|t| t.elapsed() >= RESYNC_GAP)
6432+}
6433+
63686434 /// The gate a live drag sample goes through. With the worker still owing a
63696435 /// frame (`busy`), writing now would only cancel the open the picture is already
63706436 /// waiting for -- the sample is held in `stash` instead, and the frame that
@@ -10084,6 +10150,7 @@ mod tests {
1008410150 frame_rate_ladder, histogram,
1008510151 inserted_band, is_bare_modifier, is_project, keymap, keys_filter, keys_rows, lanes_h,
1008610152 marked, menu_at, menu_items, menu_rows_h, next_audio_kbps, next_container, normalise, nothing_to_play, panel_h, project_path,
10153+ LATE_RESYNC, RESYNC_GAP, should_resync,
1008710154 retarget, row_enable, row_items, scrub_due, secs_label, show_label, silence_rate, size_label, snap_cue, snap_marks, snapped,
1008810155 SUB_PLAN_CHARS, source_tint, span_partner, speed_at, sub_pick_after_removal, subtitle_plan, summary_head, summary_tail, timecode, transport,
1008910156 EXPORT_DONE, NOTICES_MAX, STATUS_ERROR, TIMELINE_FIXED_H, TIMELINE_SHARE, lanes_shown,
@@ -12040,6 +12107,23 @@ mod tests {
1204012107 assert!(scrub_due(1, 0, Duration::from_secs(9)));
1204112108 }
1204212109
12110+ #[test]
12111+ fn the_picture_is_restarted_only_when_it_is_really_behind_and_not_twice() {
12112+ // A quantum's worth of lateness is what every playing frame has; only a
12113+ // gap an eye reads as out of sync answers with a reopen.
12114+ assert!(!should_resync(0., None));
12115+ assert!(!should_resync(LATE_RESYNC, None));
12116+ assert!(should_resync(LATE_RESYNC + 0.1, None));
12117+ // ...and a decoder that is still behind right after a restart -- the
12118+ // usual case, since it was too slow to begin with -- waits out the gap
12119+ // instead of reopening at every repaint.
12120+ assert!(!should_resync(9., Some(Instant::now())));
12121+ assert!(should_resync(
12122+ 9.,
12123+ Some(Instant::now() - RESYNC_GAP - Duration::from_millis(1))
12124+ ));
12125+ }
12126+
1204312127 #[test]
1204412128 fn export_lands_beside_the_source_and_never_on_it() {
1204512129 // The whole point: the export of an .mp4 is never that .mp4.
@@ -16112,6 +16196,7 @@ fn main() {
1611216196 // poster frame to the screen is asked for when it lands
1611316197 // (`open_media` -> `reset_after_reseek`).
1611416198 seek_since: None,
16199+ resynced: None,
1611516200 session: None,
1611616201 // Full and unmuted, which is what the session it was just
1611716202 // handed is already set to: nothing to push at startup.
crates/engine/src/playback.rsmodified+10 -0
@@ -2406,6 +2406,16 @@ impl PlaybackSession {
24062406 self.resume(was_playing);
24072407 }
24082408
2409+ /// Abandons whatever the picture worker still owes and restarts it at the
2410+ /// clock. The sound and the clock are untouched -- this is the *video*
2411+ /// catching up to its master, not a seek -- so nothing the ear is following
2412+ /// moves, and a caller that is behind by more than it can watch calls this
2413+ /// instead of waiting for a decoder that only falls further back
2414+ /// (`Player::pump`).
2415+ pub fn resync_picture(&mut self) {
2416+ self.invalidate(Dirty::Picture);
2417+ }
2418+
24092419 /// Where a seek to `secs` lands: the clamped timeline position and the
24102420 /// timeline frame it means. The one place the clamp lives, so every
24112421 /// invalidation below lands on the same frame a seek would.