A paused picture holds still, and a late one catches up
Browse files at this commit084f02d2b475891deba3374402c3cba4e494ad3bParent: 0e324ec- fix
- a paused picture holds still, and a late one catches up
2 changed files +97 -2
- crates/app/src/main.rs +87 -2
- crates/engine/src/playback.rs +10 -0
crates/app/src/main.rsmodified+87 -2
| @@ -1545,6 +1545,9 @@ struct Player { | ||
| 1545 | 1545 | titled: String, |
| 1546 | 1546 | displayed: u32, |
| 1547 | 1547 | dropped: u32, |
| 1548 | + /// When the picture was last restarted at the clock for falling behind it | |
| 1549 | + /// ([`should_resync`]). The cool-down's only state. | |
| 1550 | + resynced: Option<Instant>, | |
| 1548 | 1551 | /// Wall clock of the first displayed frame -- the real-speed measurement. |
| 1549 | 1552 | started: Option<Instant>, |
| 1550 | 1553 | focus: FocusHandle, |
| @@ -1553,7 +1556,9 @@ struct Player { | ||
| 1553 | 1556 | impl Player { |
| 1554 | 1557 | /// Catches the display up to the clock: everything already due is taken off |
| 1555 | 1558 | /// the channel and only the last of them is shown, which *is* the |
| 1556 | - /// drop-when-behind policy. A frame that is not due yet waits in `held`. | |
| 1559 | + /// drop-when-behind policy. A frame that is not due yet waits in `held`, and | |
| 1560 | + /// while the clock is paused *nothing* is due -- a repaint re-presents the | |
| 1561 | + /// frame already on screen, whatever asked for the repaint. | |
| 1557 | 1562 | fn pump(&mut self, window: &mut Window) { |
| 1558 | 1563 | // Where the transport was before this drain, so the crossing into |
| 1559 | 1564 | // `Ended` can be recognised as the one transition it is. |
| @@ -1565,7 +1570,20 @@ impl Player { | ||
| 1565 | 1570 | }; |
| 1566 | 1571 | let target = session.now() * self.fps; |
| 1567 | 1572 | let mut newest: Option<Frame> = None; |
| 1568 | - loop { | |
| 1573 | + // A frame the screen is owed: a seek's landing, and the one readiness | |
| 1574 | + // signal there is ([`Player::reset_after_reseek`]). | |
| 1575 | + let owed = self.seek_since.is_some(); | |
| 1576 | + // Paused, the clock is frozen and *nothing new is due*. Whatever the | |
| 1577 | + // decoder is still handing over is the backlog it was behind by when | |
| 1578 | + // the pause landed -- frames at a position the transport has already | |
| 1579 | + // left -- and taking one per repaint is what walked the picture on | |
| 1580 | + // after the sound had stopped, at exactly the rate the pointer was | |
| 1581 | + // moved over the timeline. Gated here, at the one place a frame ever | |
| 1582 | + // reaches the screen, rather than in the handlers that repaint: a | |
| 1583 | + // hover, a notice, a resize and a vsync are all the same event to this. | |
| 1584 | + // An owed frame is still taken, playing or not -- a scrub is paused by | |
| 1585 | + // definition, and its landing is the whole point of it. | |
| 1586 | + while session.is_playing() || owed { | |
| 1569 | 1587 | let frame = match self.held.take() { |
| 1570 | 1588 | Some(frame) => frame, |
| 1571 | 1589 | // Nothing waiting means either a clip boundary being rebuilt or |
| @@ -1585,6 +1603,15 @@ impl Player { | ||
| 1585 | 1603 | } |
| 1586 | 1604 | } |
| 1587 | 1605 | |
| 1606 | + // How far behind the master clock the picture just handed over is, in | |
| 1607 | + // seconds. Measured off a frame that really arrived and nothing else: a | |
| 1608 | + // clip boundary being reopened delivers nothing at all for hundreds of | |
| 1609 | + // milliseconds, and restarting *that* would only cancel the open it is | |
| 1610 | + // waiting on. | |
| 1611 | + let late = newest | |
| 1612 | + .as_ref() | |
| 1613 | + .map_or(0., |f| (target - f64::from(f.index)) / self.fps); | |
| 1614 | + | |
| 1588 | 1615 | if let Some(frame) = newest { |
| 1589 | 1616 | self.displayed += 1; |
| 1590 | 1617 | self.seek_since = None; |
| @@ -1608,6 +1635,30 @@ impl Player { | ||
| 1608 | 1635 | } |
| 1609 | 1636 | } |
| 1610 | 1637 | |
| 1638 | + // Audio is the master clock and a decoder that cannot keep up with it | |
| 1639 | + // never gets back on its own: it hands over every frame in order, | |
| 1640 | + // whether or not its moment has passed, so what it is behind by only | |
| 1641 | + // grows -- a minute in, the picture is seconds behind what is being | |
| 1642 | + // heard, and that is the whole of "the video can't catch the audio". | |
| 1643 | + // Past `LATE_RESYNC` the backlog is abandoned and the picture restarted | |
| 1644 | + // at the clock, which touches neither the sound nor the clock | |
| 1645 | + // (`PlaybackSession::resync_picture`), so nothing the ear is following | |
| 1646 | + // moves. Never on a frame a seek owed: that one is late by however long | |
| 1647 | + // its own reopen took, and answering it with another reopen is a loop. | |
| 1648 | + // | |
| 1649 | + // ponytail: on a machine that cannot decode the file in real time at | |
| 1650 | + // all this settles into one restart per `RESYNC_GAP` -- in sync, and | |
| 1651 | + // stuttering, which is the honest picture of what that machine can do. | |
| 1652 | + // The upgrade path is dropping late frames *inside* the worker (skip | |
| 1653 | + // the convert and the send for anything already past due), which needs | |
| 1654 | + // the deadline shared with it. | |
| 1655 | + if !owed && session.is_playing() && should_resync(late, self.resynced) { | |
| 1656 | + eprintln!("picture {late:.3}s behind the clock: restarting it there"); | |
| 1657 | + session.resync_picture(); | |
| 1658 | + self.held = None; | |
| 1659 | + self.resynced = Some(Instant::now()); | |
| 1660 | + } | |
| 1661 | + | |
| 1611 | 1662 | if self.transport() == Transport::Ended { |
| 1612 | 1663 | // A seek whose worker never produced a frame (vanished file) would |
| 1613 | 1664 | // otherwise repaint at vsync forever. Held clear for as long as the |
| @@ -6339,6 +6390,21 @@ fn scrub_due(target: u32, last_target: u32, since: Duration) -> bool { | ||
| 6339 | 6390 | target != last_target && since >= SCRUB_GAP |
| 6340 | 6391 | } |
| 6341 | 6392 | |
| 6393 | +/// How far the picture may fall behind the sound before it is restarted at the | |
| 6394 | +/// clock instead of left to crawl after it. Past what an eye reads as lip sync | |
| 6395 | +/// (a tenth of a second or so) and above what a single reopen costs to fix, so a | |
| 6396 | +/// picture that is merely a reopen behind is not answered with another one. | |
| 6397 | +const LATE_RESYNC: f64 = 0.4; | |
| 6398 | +/// The least time between two such restarts: the decoder that cannot keep up is | |
| 6399 | +/// the one this fires for, and it will still be behind straight afterwards. | |
| 6400 | +const RESYNC_GAP: Duration = Duration::from_secs(2); | |
| 6401 | + | |
| 6402 | +/// Whether a picture `late` seconds behind the master clock is restarted at it, | |
| 6403 | +/// given when the last restart was ([`Player::pump`]). | |
| 6404 | +fn should_resync(late: f64, last: Option<Instant>) -> bool { | |
| 6405 | + late > LATE_RESYNC && last.is_none_or(|t| t.elapsed() >= RESYNC_GAP) | |
| 6406 | +} | |
| 6407 | + | |
| 6342 | 6408 | /// The gate a live drag sample goes through. With the worker still owing a |
| 6343 | 6409 | /// frame (`busy`), writing now would only cancel the open the picture is already |
| 6344 | 6410 | /// waiting for -- the sample is held in `stash` instead, and the frame that |
| @@ -10046,6 +10112,7 @@ mod tests { | ||
| 10046 | 10112 | frame_rate_ladder, histogram, |
| 10047 | 10113 | inserted_band, is_bare_modifier, is_project, keymap, keys_filter, keys_rows, lanes_h, |
| 10048 | 10114 | marked, menu_at, menu_items, menu_rows_h, next_audio_kbps, next_container, normalise, nothing_to_play, panel_h, project_path, |
| 10115 | + LATE_RESYNC, RESYNC_GAP, should_resync, | |
| 10049 | 10116 | retarget, row_enable, row_items, scrub_due, secs_label, show_label, silence_rate, size_label, snap_cue, snap_marks, snapped, |
| 10050 | 10117 | SUB_PLAN_CHARS, source_tint, span_partner, speed_at, sub_pick_after_removal, subtitle_plan, summary_head, summary_tail, timecode, transport, |
| 10051 | 10118 | EXPORT_DONE, NOTICES_MAX, STATUS_ERROR, TIMELINE_FIXED_H, TIMELINE_SHARE, lanes_shown, |
| @@ -12002,6 +12069,23 @@ mod tests { | ||
| 12002 | 12069 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 12003 | 12070 | } |
| 12004 | 12071 | |
| 12072 | + #[test] | |
| 12073 | + fn the_picture_is_restarted_only_when_it_is_really_behind_and_not_twice() { | |
| 12074 | + // A quantum's worth of lateness is what every playing frame has; only a | |
| 12075 | + // gap an eye reads as out of sync answers with a reopen. | |
| 12076 | + assert!(!should_resync(0., None)); | |
| 12077 | + assert!(!should_resync(LATE_RESYNC, None)); | |
| 12078 | + assert!(should_resync(LATE_RESYNC + 0.1, None)); | |
| 12079 | + // ...and a decoder that is still behind right after a restart -- the | |
| 12080 | + // usual case, since it was too slow to begin with -- waits out the gap | |
| 12081 | + // instead of reopening at every repaint. | |
| 12082 | + assert!(!should_resync(9., Some(Instant::now()))); | |
| 12083 | + assert!(should_resync( | |
| 12084 | + 9., | |
| 12085 | + Some(Instant::now() - RESYNC_GAP - Duration::from_millis(1)) | |
| 12086 | + )); | |
| 12087 | + } | |
| 12088 | + | |
| 12005 | 12089 | #[test] |
| 12006 | 12090 | fn export_lands_beside_the_source_and_never_on_it() { |
| 12007 | 12091 | // The whole point: the export of an .mp4 is never that .mp4. |
| @@ -16060,6 +16144,7 @@ fn main() { | ||
| 16060 | 16144 | // poster frame to the screen is asked for when it lands |
| 16061 | 16145 | // (`open_media` -> `reset_after_reseek`). |
| 16062 | 16146 | seek_since: None, |
| 16147 | + resynced: None, | |
| 16063 | 16148 | session: None, |
| 16064 | 16149 | // Full and unmuted, which is what the session it was just |
| 16065 | 16150 | // handed is already set to: nothing to push at startup. |
crates/engine/src/playback.rsmodified+10 -0
| @@ -2406,6 +2406,16 @@ impl PlaybackSession { | ||
| 2406 | 2406 | self.resume(was_playing); |
| 2407 | 2407 | } |
| 2408 | 2408 | |
| 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 | + | |
| 2409 | 2419 | /// Where a seek to `secs` lands: the clamped timeline position and the |
| 2410 | 2420 | /// timeline frame it means. The one place the clamp lives, so every |
| 2411 | 2421 | /// invalidation below lands on the same frame a seek would. |