Merge branch 'worktree-agent-a5a3b9113629c64f8'
Browse files at this commit2 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
| @@ -1552,6 +1552,9 @@ struct Player { | ||
| 1552 | 1552 | titled: String, |
| 1553 | 1553 | displayed: u32, |
| 1554 | 1554 | 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>, | |
| 1555 | 1558 | /// Wall clock of the first displayed frame -- the real-speed measurement. |
| 1556 | 1559 | started: Option<Instant>, |
| 1557 | 1560 | focus: FocusHandle, |
| @@ -1560,7 +1563,9 @@ struct Player { | ||
| 1560 | 1563 | impl Player { |
| 1561 | 1564 | /// Catches the display up to the clock: everything already due is taken off |
| 1562 | 1565 | /// 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. | |
| 1564 | 1569 | fn pump(&mut self, window: &mut Window) { |
| 1565 | 1570 | // Where the transport was before this drain, so the crossing into |
| 1566 | 1571 | // `Ended` can be recognised as the one transition it is. |
| @@ -1572,7 +1577,20 @@ impl Player { | ||
| 1572 | 1577 | }; |
| 1573 | 1578 | let target = session.now() * self.fps; |
| 1574 | 1579 | 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 { | |
| 1576 | 1594 | let frame = match self.held.take() { |
| 1577 | 1595 | Some(frame) => frame, |
| 1578 | 1596 | // Nothing waiting means either a clip boundary being rebuilt or |
| @@ -1592,6 +1610,15 @@ impl Player { | ||
| 1592 | 1610 | } |
| 1593 | 1611 | } |
| 1594 | 1612 | |
| 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 | + | |
| 1595 | 1622 | if let Some(frame) = newest { |
| 1596 | 1623 | self.displayed += 1; |
| 1597 | 1624 | self.seek_since = None; |
| @@ -1615,6 +1642,30 @@ impl Player { | ||
| 1615 | 1642 | } |
| 1616 | 1643 | } |
| 1617 | 1644 | |
| 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 | + | |
| 1618 | 1669 | if self.transport() == Transport::Ended { |
| 1619 | 1670 | // A seek whose worker never produced a frame (vanished file) would |
| 1620 | 1671 | // 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 { | ||
| 6365 | 6416 | target != last_target && since >= SCRUB_GAP |
| 6366 | 6417 | } |
| 6367 | 6418 | |
| 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 | + | |
| 6368 | 6434 | /// The gate a live drag sample goes through. With the worker still owing a |
| 6369 | 6435 | /// frame (`busy`), writing now would only cancel the open the picture is already |
| 6370 | 6436 | /// waiting for -- the sample is held in `stash` instead, and the frame that |
| @@ -10084,6 +10150,7 @@ mod tests { | ||
| 10084 | 10150 | frame_rate_ladder, histogram, |
| 10085 | 10151 | inserted_band, is_bare_modifier, is_project, keymap, keys_filter, keys_rows, lanes_h, |
| 10086 | 10152 | 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, | |
| 10087 | 10154 | retarget, row_enable, row_items, scrub_due, secs_label, show_label, silence_rate, size_label, snap_cue, snap_marks, snapped, |
| 10088 | 10155 | SUB_PLAN_CHARS, source_tint, span_partner, speed_at, sub_pick_after_removal, subtitle_plan, summary_head, summary_tail, timecode, transport, |
| 10089 | 10156 | EXPORT_DONE, NOTICES_MAX, STATUS_ERROR, TIMELINE_FIXED_H, TIMELINE_SHARE, lanes_shown, |
| @@ -12040,6 +12107,23 @@ mod tests { | ||
| 12040 | 12107 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 12041 | 12108 | } |
| 12042 | 12109 | |
| 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 | + | |
| 12043 | 12127 | #[test] |
| 12044 | 12128 | fn export_lands_beside_the_source_and_never_on_it() { |
| 12045 | 12129 | // The whole point: the export of an .mp4 is never that .mp4. |
| @@ -16112,6 +16196,7 @@ fn main() { | ||
| 16112 | 16196 | // poster frame to the screen is asked for when it lands |
| 16113 | 16197 | // (`open_media` -> `reset_after_reseek`). |
| 16114 | 16198 | seek_since: None, |
| 16199 | + resynced: None, | |
| 16115 | 16200 | session: None, |
| 16116 | 16201 | // Full and unmuted, which is what the session it was just |
| 16117 | 16202 | // 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. |