forge
Explore
Log in

Don't you dare go Hollow

One state, four names — the end of a clip is not paused

Browse files at this commit

bb16b0d61c310a935bcdb0e1acd0f8057dc3079eAhmet Kaan GümüşParent: 88fbed0
  • refactor
    • one state, four names — the end of a clip is not paused
Show full message
✨ refactor(transport): one state, four names — the end of a clip is not paused

The play button read the clock, and past the last frame the clock is still
running on wall time -- so a played-out timeline drew Pause and looked like it
was playing. State was a lattice of bools (eos, done, held) that only the pump
knew how to spell.

Transport{Stopped,Playing,Paused,Ended} is now the single answer, asked of the
session rather than remembered: the engine's own end-of-stream flag (any seek
clears it) and its clock. The glyph, the label, the button's enablement, the
repaint loop, the clamped timecode, the step-back-off-the-end and both doors of
play/restart (key and button) all read it. The eos and done bools are gone.
crates/app/src/main.rsmodified+149 -54
@@ -493,6 +493,48 @@ const HIST_H: f32 = 96.;
493 /// counts, lightened enough to read on the dark box.493 /// counts, lightened enough to read on the dark box.
494 const HIST_INK: [u32; 3] = [0xE0_5A_5A, 0x5A_D0_7A, 0x5A_9A_E0];494 const HIST_INK: [u32; 3] = [0xE0_5A_5A, 0x5A_D0_7A, 0x5A_9A_E0];
495 495
496+/// Where the transport is. The one answer the button's glyph, its label, its
497+/// enablement, the play key and the repaint loop all read -- there is no play
498+/// flag anywhere else, because a second one is a second answer.
499+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
500+enum Transport {
501+ /// No timeline open. Nothing to play and the transport is dimmed.
502+ Stopped,
503+ Playing,
504+ Paused,
505+ /// Played out: the last frame is on screen, the decoder is finished, and the
506+ /// clock is still running past it -- which is exactly why "is the clock
507+ /// going" is not the same question as "is this playing".
508+ Ended,
509+}
510+
511+impl Transport {
512+ /// The timeline is in motion: two bars on the button, and a repaint owed
513+ /// every vsync.
514+ fn is_playing(self) -> bool {
515+ matches!(self, Transport::Playing)
516+ }
517+
518+ /// The play key and the transport button start over from the top rather
519+ /// than toggling -- the end of a timeline is where every NLE does this, and
520+ /// the button does it because the key already did.
521+ fn restarts(self) -> bool {
522+ matches!(self, Transport::Ended)
523+ }
524+}
525+
526+/// What a session's own two answers mean: the clock, unless the timeline has
527+/// been played out -- `played_out` is the engine's end of stream with no frame
528+/// still waiting on the pump, and it wins, because past the end a running clock
529+/// is measuring wall time and not a picture.
530+fn transport(playing: bool, played_out: bool) -> Transport {
531+ match (played_out, playing) {
532+ (true, _) => Transport::Ended,
533+ (_, true) => Transport::Playing,
534+ (_, false) => Transport::Paused,
535+ }
536+}
537+
496 struct Player {538 struct Player {
497 /// The timeline, once there is one. A run with no file opens without it and539 /// The timeline, once there is one. A run with no file opens without it and
498 /// waits: the first media import or project load is what fills it, and540 /// waits: the first media import or project load is what fills it, and
@@ -504,11 +546,10 @@ struct Player {
504 name: SharedString,546 name: SharedString,
505 image: Option<Arc<RenderImage>>,547 image: Option<Arc<RenderImage>>,
506 /// A frame that arrived before its time; shown on the tick it comes due.548 /// A frame that arrived before its time; shown on the tick it comes due.
549+ /// The pump's buffer, not transport state -- but a frame waiting here is
550+ /// what keeps a finished decoder from reading as [`Transport::Ended`] one
551+ /// tick early. See [`Player::transport`].
507 held: Option<Frame>,552 held: Option<Frame>,
508- /// The decoder's channel closed. Frames may still be waiting in `held`.
509- eos: bool,
510- /// Last frame shown; nothing left to animate.
511- done: bool,
512 /// A seek is waiting for its frame. Keeps the repaint loop alive while553 /// A seek is waiting for its frame. Keeps the repaint loop alive while
513 /// paused, which is the only way the new still ever reaches the screen.554 /// paused, which is the only way the new still ever reaches the screen.
514 pending_seek: bool,555 pending_seek: bool,
@@ -705,6 +746,9 @@ impl Player {
705 /// the channel and only the last of them is shown, which *is* the746 /// the channel and only the last of them is shown, which *is* the
706 /// drop-when-behind policy. A frame that is not due yet waits in `held`.747 /// drop-when-behind policy. A frame that is not due yet waits in `held`.
707 fn pump(&mut self, window: &mut Window) {748 fn pump(&mut self, window: &mut Window) {
749+ // Where the transport was before this drain, so the crossing into
750+ // `Ended` can be recognised as the one transition it is.
751+ let was = self.transport();
708 // No timeline, nothing to catch up to: the window is showing its empty752 // No timeline, nothing to catch up to: the window is showing its empty
709 // state and there is no decoder to drain.753 // state and there is no decoder to drain.
710 let Some(session) = &mut self.session else {754 let Some(session) = &mut self.session else {
@@ -720,10 +764,7 @@ impl Player {
720 // them apart -- `frame.index` is already a timeline index.764 // them apart -- `frame.index` is already a timeline index.
721 None => match session.try_frame() {765 None => match session.try_frame() {
722 Some(frame) => frame,766 Some(frame) => frame,
723- None => {767+ None => break,
724- self.eos = session.is_eos();
725- break;
726- }
727 },768 },
728 };769 };
729 if f64::from(frame.index) <= target {770 if f64::from(frame.index) <= target {
@@ -758,29 +799,43 @@ impl Player {
758 }799 }
759 }800 }
760 801
761- if self.eos && self.held.is_none() && !self.done {802+ if self.transport() == Transport::Ended {
762- self.done = true;
763 // A seek whose worker never produced a frame (vanished file) would803 // A seek whose worker never produced a frame (vanished file) would
764- // otherwise repaint at vsync forever.804+ // otherwise repaint at vsync forever. Held true for as long as the
805+ // state does, not just on the crossing: nothing else is coming.
765 self.pending_seek = false;806 self.pending_seek = false;
766- let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());807+ if was != Transport::Ended {
767- eprintln!(808+ let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());
768- "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",809+ eprintln!(
769- self.displayed,810+ "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
770- self.dropped,811+ self.displayed,
771- session.now()812+ self.dropped,
772- );813+ self.session.as_ref().map_or(0., PlaybackSession::now)
814+ );
815+ }
773 }816 }
774 }817 }
775 818
776- /// Every end-of-stream flag is app state and the engine knows nothing about819+ /// Where the transport is, asked of the session rather than remembered:
777- /// it, so clearing them after a reseek is what stops the picture from820+ /// end of stream is the engine's own flag (any seek clears it, which is why
778- /// staying frozen on the old last frame. Edits reseek inside the engine and821+ /// an edit past the end revives the picture) and so is the clock. A held
779- /// still owe this.822+ /// frame is one still owed to the screen, so the end is not the end yet.
823+ fn transport(&self) -> Transport {
824+ let Some(session) = &self.session else {
825+ return Transport::Stopped;
826+ };
827+ transport(
828+ session.is_playing(),
829+ session.is_eos() && self.held.is_none(),
830+ )
831+ }
832+
833+ /// A frame owed to the screen after a reseek, and the buffered one dropped:
834+ /// what stops the picture from staying frozen on the old last frame. The
835+ /// end-of-stream flag itself is the engine's and its own seek clears it --
836+ /// edits reseek inside the engine and still owe this.
780 fn reset_after_reseek(&mut self) {837 fn reset_after_reseek(&mut self) {
781 self.held = None;838 self.held = None;
782- self.eos = false;
783- self.done = false;
784 self.pending_seek = true;839 self.pending_seek = true;
785 // An edit moves the indices a drag in flight is holding -- a stroke840 // An edit moves the indices a drag in flight is holding -- a stroke
786 // during one is exactly that -- and an edge committed against a moved841 // during one is exactly that -- and an edge committed against a moved
@@ -1604,16 +1659,17 @@ impl Player {
1604 /// door a ruler click uses -- so a step while playing keeps playing, exactly1659 /// door a ruler click uses -- so a step while playing keeps playing, exactly
1605 /// as a click does. It starts from the frame the transport is showing, which1660 /// as a click does. It starts from the frame the transport is showing, which
1606 /// past the end is the last one, and that is what lets a step back off EOS1661 /// past the end is the last one, and that is what lets a step back off EOS
1607- /// revive the picture ([`Player::reset_after_reseek`] clears `done`). Both1662+ /// revive the picture (the engine's seek leaves [`Transport::Ended`]). Both
1608 /// ends clamp, so the two go-to actions are this same step asked for more1663 /// ends clamp, so the two go-to actions are this same step asked for more
1609 /// frames than the timeline has. Selection is untouched: a seek is not an1664 /// frames than the timeline has. Selection is untouched: a seek is not an
1610 /// edit, and nothing it does moves a clip index.1665 /// edit, and nothing it does moves a clip index.
1611 fn step(&mut self, frames: i64, cx: &mut Context<Self>) {1666 fn step(&mut self, frames: i64, cx: &mut Context<Self>) {
1667+ let ended = self.transport() == Transport::Ended;
1612 let Some(session) = &self.session else {1668 let Some(session) = &self.session else {
1613 return;1669 return;
1614 };1670 };
1615 let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);1671 let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);
1616- let now = match self.done {1672+ let now = match ended {
1617 true => last,1673 true => last,
1618 false => i64::from(frame_at(session.now(), self.fps)),1674 false => i64::from(frame_at(session.now(), self.fps)),
1619 };1675 };
@@ -2487,15 +2543,25 @@ impl Player {
2487 if self.exporting().is_some() {2543 if self.exporting().is_some() {
2488 return;2544 return;
2489 }2545 }
2490- if self.done {2546+ match self.transport() {
2491- self.seek(0., cx);2547+ // Nothing open: the button is dimmed and the key says nothing.
2492- if let Some(session) = &mut self.session {2548+ Transport::Stopped => {}
2493- session.play();2549+ // Back to the top and away, for the key and the button alike --
2550+ // whichever asked, the transport was showing Play.
2551+ state if state.restarts() => {
2552+ self.seek(0., cx);
2553+ if let Some(session) = &mut self.session {
2554+ session.play();
2555+ }
2556+ }
2557+ _ => {
2558+ if let Some(session) = &mut self.session {
2559+ session.toggle();
2560+ // A paused timeline animates nothing; this is the repaint
2561+ // that puts the new glyph up.
2562+ cx.notify();
2563+ }
2494 }2564 }
2495- } else if let Some(session) = &mut self.session {
2496- session.toggle();
2497- // Past EOF nothing else asks for a repaint.
2498- cx.notify();
2499 }2565 }
2500 }2566 }
2501 2567
@@ -2679,11 +2745,9 @@ impl Render for Player {
2679 window.set_window_title(&title);2745 window.set_window_title(&title);
2680 self.titled = title;2746 self.titled = title;
2681 }2747 }
2682- // No shadow flag: the clock is the only truth about play state.2748+ // No shadow flag: the session is the only truth about play state, and
2683- let playing = self2749+ // [`Player::transport`] is the one place it is read.
2684- .session2750+ let state = self.transport();
2685- .as_ref()
2686- .is_some_and(PlaybackSession::is_playing);
2687 // A paused timeline has nothing to animate; the toggle handlers notify,2751 // A paused timeline has nothing to animate; the toggle handlers notify,
2688 // which is what starts the loop again. A paused seek keeps the loop2752 // which is what starts the loop again. A paused seek keeps the loop
2689 // running by itself until `pump` has the frame it asked for. An export2753 // running by itself until `pump` has the frame it asked for. An export
@@ -2691,7 +2755,7 @@ impl Render for Player {
2691 // the screen on a repaint. A notice does not: it waits to be dismissed2755 // the screen on a repaint. A notice does not: it waits to be dismissed
2692 // rather than for a clock, so keeping the loop alive for it would spin2756 // rather than for a clock, so keeping the loop alive for it would spin
2693 // the GPU until someone answered it.2757 // the GPU until someone answered it.
2694- if (playing && !self.done) || self.pending_seek || self.export.is_some() {2758+ if state.is_playing() || self.pending_seek || self.export.is_some() {
2695 window.request_animation_frame();2759 window.request_animation_frame();
2696 }2760 }
2697 2761
@@ -2702,7 +2766,7 @@ impl Render for Player {
2702 // The clock keeps running after the last frame (wall time takes over at2766 // The clock keeps running after the last frame (wall time takes over at
2703 // audio EOF) while the picture is frozen, so the timeline the UI shows is2767 // audio EOF) while the picture is frozen, so the timeline the UI shows is
2704 // the clamped one, pinned to the out-point once playback is done.2768 // the clamped one, pinned to the out-point once playback is done.
2705- let position = if self.done {2769+ let position = if state == Transport::Ended {
2706 duration2770 duration
2707 } else {2771 } else {
2708 self.session2772 self.session
@@ -3095,7 +3159,7 @@ impl Render for Player {
3095 // Above the panel and only when there is one to show, so it costs3159 // Above the panel and only when there is one to show, so it costs
3096 // the picture nothing the rest of the time.3160 // the picture nothing the rest of the time.
3097 .children(self.notice_bar(cx))3161 .children(self.notice_bar(cx))
3098- .child(self.panel(position, duration, playing, cx))3162+ .child(self.panel(position, duration, state, cx))
3099 // Over the panel it was opened on, and under the cards: it is only3163 // Over the panel it was opened on, and under the cards: it is only
3100 // ever up while neither of them is (`modal`).3164 // ever up while neither of them is (`modal`).
3101 .children(self.context_card(window.viewport_size(), cx))3165 .children(self.context_card(window.viewport_size(), cx))
@@ -3379,7 +3443,7 @@ impl Player {
3379 &self,3443 &self,
3380 position: f64,3444 position: f64,
3381 duration: f64,3445 duration: f64,
3382- playing: bool,3446+ state: Transport,
3383 cx: &mut Context<Self>,3447 cx: &mut Context<Self>,
3384 ) -> impl IntoElement {3448 ) -> impl IntoElement {
3385 let filled = if duration > 0. {3449 let filled = if duration > 0. {
@@ -3393,7 +3457,7 @@ impl Player {
3393 let exporting = self.exporting().is_some();3457 let exporting = self.exporting().is_some();
3394 // Everything but Import and Keys needs a timeline to act on: with none3458 // Everything but Import and Keys needs a timeline to act on: with none
3395 // open they are dimmed rather than silently doing nothing.3459 // open they are dimmed rather than silently doing nothing.
3396- let live = self.session.is_some() && !exporting;3460+ let live = state != Transport::Stopped && !exporting;
3397 let key = |action| self.keymap.display(action);3461 let key = |action| self.keymap.display(action);
3398 // The lanes the project has, or the pair a fresh one starts with so the3462 // The lanes the project has, or the pair a fresh one starts with so the
3399 // panel reads the same before a file is open as after.3463 // panel reads the same before a file is open as after.
@@ -3451,8 +3515,8 @@ impl Player {
3451 .gap(px(8.))3515 .gap(px(8.))
3452 .child(control(3516 .child(control(
3453 "transport",3517 "transport",
3454- Some(transport_glyph(playing).into_any_element()),3518+ Some(transport_glyph(state).into_any_element()),
3455- if playing { "Pause" } else { "Play" },3519+ if state.is_playing() { "Pause" } else { "Play" },
3456 key(ActionId::Play),3520 key(ActionId::Play),
3457 live,3521 live,
3458 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),3522 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),
@@ -6653,9 +6717,12 @@ fn empty_hint() -> impl IntoElement {
6653 )6717 )
6654 }6718 }
6655 6719
6656-/// Two bars while playing, a triangle while paused. Drawn, so there is no icon6720+/// Two bars while playing, a triangle in every other state -- paused, nothing
6657-/// font and no glyph coverage to depend on.6721+/// open, and played out, where the button's next act is to start over rather
6658-fn transport_glyph(playing: bool) -> impl IntoElement {6722+/// than to stop something. Drawn, so there is no icon font and no glyph
6723+/// coverage to depend on.
6724+fn transport_glyph(state: Transport) -> impl IntoElement {
6725+ let playing = state.is_playing();
6659 div()6726 div()
6660 .flex()6727 .flex()
6661 .items_center()6728 .items_center()
@@ -6710,13 +6777,14 @@ mod tests {
6710 INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,6777 INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX,
6711 LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H,6778 LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H,
6712 Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,6779 Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,
6713- SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave,6780+ SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Transport, Volume, WAVE_BPS,
6781+ WAVE_COL, Wave,
6714 applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y,6782 applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y,
6715 export_path, export_settings, format_key, format_line, format_refusal, frac_along,6783 export_path, export_settings, format_key, format_line, format_refusal, frac_along,
6716 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,6784 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,
6717 menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,6785 menu_at, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,
6718- silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, unseen_paths,6786+ silence_rate, source_tint, span_partner, speed_at, start_frac, timecode, transport,
6719- unseen_sources, whole_take, width_frac, window_title,6787+ unseen_paths, unseen_sources, whole_take, width_frac, window_title,
6720 };6788 };
6721 use super::{LaneKind, file_name, file_uri, library_rows, unscannable};6789 use super::{LaneKind, file_name, file_uri, library_rows, unscannable};
6722 6790
@@ -8123,6 +8191,35 @@ mod tests {
8123 assert_eq!(volume.gain(), 0.75);8191 assert_eq!(volume.gain(), 0.75);
8124 }8192 }
8125 8193
8194+ /// The whole transport in one place: the clock keeps running past the last
8195+ /// frame (wall time takes over at audio EOF), so "the clock is going" is
8196+ /// not "this is playing" -- and a button that read the clock showed Pause
8197+ /// on a timeline that had stopped moving. Ended is its own state, it draws
8198+ /// Play, and the next press starts over from the top.
8199+ #[test]
8200+ fn a_played_out_timeline_is_not_playing_and_the_next_press_starts_it_over() {
8201+ assert_eq!(transport(true, false), Transport::Playing);
8202+ assert_eq!(transport(false, false), Transport::Paused);
8203+ // The transition the bug was about: the clock is still running and the
8204+ // decoder is finished. Played out wins, however the clock reads.
8205+ assert_eq!(transport(true, true), Transport::Ended);
8206+ assert_eq!(transport(false, true), Transport::Ended);
8207+
8208+ // What the button draws, in each state. Two bars only while it moves.
8209+ assert!(Transport::Playing.is_playing());
8210+ for state in [Transport::Paused, Transport::Ended, Transport::Stopped] {
8211+ assert!(!state.is_playing(), "{state:?} must draw the Play triangle");
8212+ }
8213+
8214+ // And what a press does: start over at the end, plain toggle before it,
8215+ // nothing with no timeline. Same answer for the key and the button --
8216+ // both come through `Player::toggle_or_restart`.
8217+ assert!(Transport::Ended.restarts());
8218+ for state in [Transport::Playing, Transport::Paused, Transport::Stopped] {
8219+ assert!(!state.restarts(), "{state:?} must toggle, not reseek");
8220+ }
8221+ }
8222+
8126 /// Both ends hold under a key held down: the ABI only accepts `0.0..=1.0`,8223 /// Both ends hold under a key held down: the ABI only accepts `0.0..=1.0`,
8127 /// and a wrapped step count would hand it something else.8224 /// and a wrapped step count would hand it something else.
8128 #[test]8225 #[test]
@@ -8687,8 +8784,6 @@ fn main() {
8687 name: name.clone(),8784 name: name.clone(),
8688 image: None,8785 image: None,
8689 held: None,8786 held: None,
8690- eos: false,
8691- done: false,
8692 ruler: Rc::default(),8787 ruler: Rc::default(),
8693 selected: None,8788 selected: None,
8694 context_menu: None,8789 context_menu: None,