forge
Explore
Log in

Don't you dare go Hollow

🔀 merge(D1): the transport tells the truth at the end — one state machine, and the clock stops where the timeline does

Browse files at this commit

d9fd7cfea7ffecc50f2d451a1d3d8ed82d40dbd3Ahmet Kaan GümüşParent: 364c82133f72b4
crates/app/src/main.rsmodified+226 -60
@@ -618,6 +618,48 @@ const HIST_H: f32 = 96.;
618 /// counts, lightened enough to read on the dark box.618 /// counts, lightened enough to read on the dark box.
619 const HIST_INK: [u32; 3] = [0xE0_5A_5A, 0x5A_D0_7A, 0x5A_9A_E0];619 const HIST_INK: [u32; 3] = [0xE0_5A_5A, 0x5A_D0_7A, 0x5A_9A_E0];
620 620
621+/// Where the transport is. The one answer the button's glyph, its label, its
622+/// enablement, the play key and the repaint loop all read -- there is no play
623+/// flag anywhere else, because a second one is a second answer.
624+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
625+enum Transport {
626+ /// No timeline open. Nothing to play and the transport is dimmed.
627+ Stopped,
628+ Playing,
629+ Paused,
630+ /// Played out: the last frame is on screen, the decoder is finished, and the
631+ /// clock is still running past it -- which is exactly why "is the clock
632+ /// going" is not the same question as "is this playing".
633+ Ended,
634+}
635+
636+impl Transport {
637+ /// The timeline is in motion: two bars on the button, and a repaint owed
638+ /// every vsync.
639+ fn is_playing(self) -> bool {
640+ matches!(self, Transport::Playing)
641+ }
642+
643+ /// The play key and the transport button start over from the top rather
644+ /// than toggling -- the end of a timeline is where every NLE does this, and
645+ /// the button does it because the key already did.
646+ fn restarts(self) -> bool {
647+ matches!(self, Transport::Ended)
648+ }
649+}
650+
651+/// What a session's own two answers mean: the clock, unless the timeline has
652+/// been played out -- `played_out` is the engine's end of stream with no frame
653+/// still waiting on the pump, and it wins, because past the end a running clock
654+/// is measuring wall time and not a picture.
655+fn transport(playing: bool, played_out: bool) -> Transport {
656+ match (played_out, playing) {
657+ (true, _) => Transport::Ended,
658+ (_, true) => Transport::Playing,
659+ (_, false) => Transport::Paused,
660+ }
661+}
662+
621 struct Player {663 struct Player {
622 /// The timeline, once there is one. A run with no file opens without it and664 /// The timeline, once there is one. A run with no file opens without it and
623 /// waits: the first media import or project load is what fills it, and665 /// waits: the first media import or project load is what fills it, and
@@ -629,11 +671,10 @@ struct Player {
629 name: SharedString,671 name: SharedString,
630 image: Option<Arc<RenderImage>>,672 image: Option<Arc<RenderImage>>,
631 /// A frame that arrived before its time; shown on the tick it comes due.673 /// A frame that arrived before its time; shown on the tick it comes due.
674+ /// The pump's buffer, not transport state -- but a frame waiting here is
675+ /// what keeps a finished decoder from reading as [`Transport::Ended`] one
676+ /// tick early. See [`Player::transport`].
632 held: Option<Frame>,677 held: Option<Frame>,
633- /// The decoder's channel closed. Frames may still be waiting in `held`.
634- eos: bool,
635- /// Last frame shown; nothing left to animate.
636- done: bool,
637 /// A seek is waiting for its frame. Keeps the repaint loop alive while678 /// A seek is waiting for its frame. Keeps the repaint loop alive while
638 /// paused, which is the only way the new still ever reaches the screen.679 /// paused, which is the only way the new still ever reaches the screen.
639 pending_seek: bool,680 pending_seek: bool,
@@ -889,6 +930,9 @@ impl Player {
889 /// the channel and only the last of them is shown, which *is* the930 /// the channel and only the last of them is shown, which *is* the
890 /// drop-when-behind policy. A frame that is not due yet waits in `held`.931 /// drop-when-behind policy. A frame that is not due yet waits in `held`.
891 fn pump(&mut self, window: &mut Window) {932 fn pump(&mut self, window: &mut Window) {
933+ // Where the transport was before this drain, so the crossing into
934+ // `Ended` can be recognised as the one transition it is.
935+ let was = self.transport();
892 // No timeline, nothing to catch up to: the window is showing its empty936 // No timeline, nothing to catch up to: the window is showing its empty
893 // state and there is no decoder to drain.937 // state and there is no decoder to drain.
894 let Some(session) = &mut self.session else {938 let Some(session) = &mut self.session else {
@@ -904,10 +948,7 @@ impl Player {
904 // them apart -- `frame.index` is already a timeline index.948 // them apart -- `frame.index` is already a timeline index.
905 None => match session.try_frame() {949 None => match session.try_frame() {
906 Some(frame) => frame,950 Some(frame) => frame,
907- None => {951+ None => break,
908- self.eos = session.is_eos();
909- break;
910- }
911 },952 },
912 };953 };
913 if f64::from(frame.index) <= target {954 if f64::from(frame.index) <= target {
@@ -942,29 +983,56 @@ impl Player {
942 }983 }
943 }984 }
944 985
945- if self.eos && self.held.is_none() && !self.done {986+ if self.transport() == Transport::Ended {
946- self.done = true;
947 // A seek whose worker never produced a frame (vanished file) would987 // A seek whose worker never produced a frame (vanished file) would
948- // otherwise repaint at vsync forever.988+ // otherwise repaint at vsync forever. Held true for as long as the
989+ // state does, not just on the crossing: nothing else is coming.
949 self.pending_seek = false;990 self.pending_seek = false;
991+ if was != Transport::Ended {
992+ // Ended is a *stopped* transport, so the clock stops with it,
993+ // on the out point the timecode and the playhead have been
994+ // showing all along. Nothing else ever stopped it: past the
995+ // last frame wall time takes over and `now()` walks off the end
996+ // of the timeline for as long as the window is left open -- and
997+ // the playhead is what a cut, a paste, an insert and the
998+ // analyser all act at, so every one of them was aiming into
999+ // empty space (measured: a 5 s timeline recognised its end at
1000+ // clock 17.5 s under a slow renderer). End of stream is left
1001+ // set, so this is still `Ended` and the next press restarts.
1002+ if let Some(session) = &mut self.session {
1003+ session.halt_at_end();
1004+ }
950 let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());1005 let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64());
951 eprintln!(1006 eprintln!(
952 "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",1007 "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s",
953 self.displayed,1008 self.displayed,
954 self.dropped,1009 self.dropped,
955- session.now()1010+ self.session.as_ref().map_or(0., PlaybackSession::now)
956 );1011 );
957 }1012 }
958 }1013 }
1014+ }
1015+
1016+ /// Where the transport is, asked of the session rather than remembered:
1017+ /// end of stream is the engine's own flag (any seek clears it, which is why
1018+ /// an edit past the end revives the picture) and so is the clock. A held
1019+ /// frame is one still owed to the screen, so the end is not the end yet.
1020+ fn transport(&self) -> Transport {
1021+ let Some(session) = &self.session else {
1022+ return Transport::Stopped;
1023+ };
1024+ transport(
1025+ session.is_playing(),
1026+ session.is_eos() && self.held.is_none(),
1027+ )
1028+ }
959 1029
960- /// Every end-of-stream flag is app state and the engine knows nothing about1030+ /// A frame owed to the screen after a reseek, and the buffered one dropped:
961- /// it, so clearing them after a reseek is what stops the picture from1031+ /// what stops the picture from staying frozen on the old last frame. The
962- /// staying frozen on the old last frame. Edits reseek inside the engine and1032+ /// end-of-stream flag itself is the engine's and its own seek clears it --
963- /// still owe this.1033+ /// edits reseek inside the engine and still owe this.
964 fn reset_after_reseek(&mut self) {1034 fn reset_after_reseek(&mut self) {
965 self.held = None;1035 self.held = None;
966- self.eos = false;
967- self.done = false;
968 self.pending_seek = true;1036 self.pending_seek = true;
969 // An edit moves the indices a drag in flight is holding -- a stroke1037 // An edit moves the indices a drag in flight is holding -- a stroke
970 // during one is exactly that -- and an edge committed against a moved1038 // during one is exactly that -- and an edge committed against a moved
@@ -1933,16 +2001,17 @@ impl Player {
1933 /// door a ruler click uses -- so a step while playing keeps playing, exactly2001 /// door a ruler click uses -- so a step while playing keeps playing, exactly
1934 /// as a click does. It starts from the frame the transport is showing, which2002 /// as a click does. It starts from the frame the transport is showing, which
1935 /// past the end is the last one, and that is what lets a step back off EOS2003 /// past the end is the last one, and that is what lets a step back off EOS
1936- /// revive the picture ([`Player::reset_after_reseek`] clears `done`). Both2004+ /// revive the picture (the engine's seek leaves [`Transport::Ended`]). Both
1937 /// ends clamp, so the two go-to actions are this same step asked for more2005 /// ends clamp, so the two go-to actions are this same step asked for more
1938 /// frames than the timeline has. Selection is untouched: a seek is not an2006 /// frames than the timeline has. Selection is untouched: a seek is not an
1939 /// edit, and nothing it does moves a clip index.2007 /// edit, and nothing it does moves a clip index.
1940 fn step(&mut self, frames: i64, cx: &mut Context<Self>) {2008 fn step(&mut self, frames: i64, cx: &mut Context<Self>) {
2009+ let ended = self.transport() == Transport::Ended;
1941 let Some(session) = &self.session else {2010 let Some(session) = &self.session else {
1942 return;2011 return;
1943 };2012 };
1944 let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);2013 let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0);
1945- let now = match self.done {2014+ let now = match ended {
1946 true => last,2015 true => last,
1947 false => i64::from(frame_at(session.now(), self.fps)),2016 false => i64::from(frame_at(session.now(), self.fps)),
1948 };2017 };
@@ -2554,12 +2623,11 @@ impl Player {
2554 }2623 }
2555 }2624 }
2556 2625
2557- /// Where the playhead is, as the panel draws it. The clock keeps running2626+ /// Where the playhead is, as the panel draws it: pinned to the out point
2558- /// after the last frame (wall time takes over at audio EOF) while the2627+ /// once playback is done, and clamped to the drawn duration otherwise -- a
2559- /// picture is frozen, so what the UI shows is the clamped one, pinned to2628+ /// tail being dragged draws past the timeline it is about to become.
2560- /// the out-point once playback is done.
2561 fn playhead(&self, duration: f64) -> f64 {2629 fn playhead(&self, duration: f64) -> f64 {
2562- if self.done {2630+ if self.transport() == Transport::Ended {
2563 duration2631 duration
2564 } else {2632 } else {
2565 self.session2633 self.session
@@ -2754,11 +2822,11 @@ impl Player {
2754 self.export_path = PathBuf::new();2822 self.export_path = PathBuf::new();
2755 self.project_path = PathBuf::new();2823 self.project_path = PathBuf::new();
2756 self.fps = 30.;2824 self.fps = 30.;
2757- // No decoder to wait for a frame from: the hint is what shows.2825+ // No decoder to wait for a frame from: the hint is what shows. The
2826+ // transport reads `Stopped` from the session being gone, so there is no
2827+ // end-of-stream state left to clear here.
2758 self.reset_after_reseek();2828 self.reset_after_reseek();
2759 self.pending_seek = false;2829 self.pending_seek = false;
2760- self.eos = false;
2761- self.done = false;
2762 }2830 }
2763 2831
2764 /// One item of a library row's menu, done. Every one of them closes the2832 /// One item of a library row's menu, done. Every one of them closes the
@@ -3174,11 +3242,11 @@ impl Player {
3174 return;3242 return;
3175 }3243 }
3176 // Nothing to play is a message, not a transport state. An empty3244 // Nothing to play is a message, not a transport state. An empty
3177- // timeline is `done` from its one black frame onward, so the restart3245+ // timeline is [`Transport::Ended`] from its one black frame onward, so
3178- // below would start a clock against a zero-length timeline -- and it is3246+ // the restart below would start a clock against a zero-length timeline
3179- // `done` again by the next repaint, so no later press could ever stop3247+ // -- and it is Ended again by the next repaint, so no later press could
3180- // it: the button would read "Pause" and never pause. A delete can empty3248+ // ever stop it: the button would read "Pause" and never pause. A delete
3181- // the timeline mid-play, and that press must still stop it.3249+ // can empty the timeline mid-play, and that press must still stop it.
3182 if nothing_to_play(self.session.as_ref()) {3250 if nothing_to_play(self.session.as_ref()) {
3183 match self.session.as_mut().filter(|s| s.is_playing()) {3251 match self.session.as_mut().filter(|s| s.is_playing()) {
3184 Some(session) => session.pause(),3252 Some(session) => session.pause(),
@@ -3187,17 +3255,27 @@ impl Player {
3187 cx.notify();3255 cx.notify();
3188 return;3256 return;
3189 }3257 }
3190- if self.done {3258+ match self.transport() {
3259+ // Nothing open: the button is dimmed and the key says nothing.
3260+ Transport::Stopped => {}
3261+ // Back to the top and away, for the key and the button alike --
3262+ // whichever asked, the transport was showing Play.
3263+ state if state.restarts() => {
3191 self.seek(0., cx);3264 self.seek(0., cx);
3192 if let Some(session) = &mut self.session {3265 if let Some(session) = &mut self.session {
3193 session.play();3266 session.play();
3194 }3267 }
3195- } else if let Some(session) = &mut self.session {3268+ }
3269+ _ => {
3270+ if let Some(session) = &mut self.session {
3196 session.toggle();3271 session.toggle();
3197- // Past EOF nothing else asks for a repaint.3272+ // A paused timeline animates nothing; this is the repaint
3273+ // that puts the new glyph up.
3198 cx.notify();3274 cx.notify();
3199 }3275 }
3200 }3276 }
3277+ }
3278+ }
3201 3279
3202 /// The export that owns the UI, if any. A cancelled one does not: it has3280 /// The export that owns the UI, if any. A cancelled one does not: it has
3203 /// its own copy of the edit list and owes only its own cleanup.3281 /// its own copy of the edit list and owes only its own cleanup.
@@ -3412,11 +3490,9 @@ impl Render for Player {
3412 window.set_window_title(&title);3490 window.set_window_title(&title);
3413 self.titled = title;3491 self.titled = title;
3414 }3492 }
3415- // No shadow flag: the clock is the only truth about play state.3493+ // No shadow flag: the session is the only truth about play state, and
3416- let playing = self3494+ // [`Player::transport`] is the one place it is read.
3417- .session3495+ let state = self.transport();
3418- .as_ref()
3419- .is_some_and(PlaybackSession::is_playing);
3420 // A paused timeline has nothing to animate; the toggle handlers notify,3496 // A paused timeline has nothing to animate; the toggle handlers notify,
3421 // which is what starts the loop again. A paused seek keeps the loop3497 // which is what starts the loop again. A paused seek keeps the loop
3422 // running by itself until `pump` has the frame it asked for. An export3498 // running by itself until `pump` has the frame it asked for. An export
@@ -3424,7 +3500,7 @@ impl Render for Player {
3424 // the screen on a repaint. A notice does not: it waits to be dismissed3500 // the screen on a repaint. A notice does not: it waits to be dismissed
3425 // rather than for a clock, so keeping the loop alive for it would spin3501 // rather than for a clock, so keeping the loop alive for it would spin
3426 // the GPU until someone answered it.3502 // the GPU until someone answered it.
3427- if (playing && !self.done) || self.pending_seek || self.export.is_some() {3503+ if state.is_playing() || self.pending_seek || self.export.is_some() {
3428 window.request_animation_frame();3504 window.request_animation_frame();
3429 }3505 }
3430 3506
@@ -3889,7 +3965,7 @@ impl Render for Player {
3889 // Above the panel and only when there is one to show, so it costs3965 // Above the panel and only when there is one to show, so it costs
3890 // the picture nothing the rest of the time.3966 // the picture nothing the rest of the time.
3891 .children(self.notice_bar(cx))3967 .children(self.notice_bar(cx))
3892- .child(self.panel(position, duration, playing, cx))3968+ .child(self.panel(position, duration, state, cx))
3893 // Over the panel it was opened on, and under the cards: it is only3969 // Over the panel it was opened on, and under the cards: it is only
3894 // ever up while neither of them is (`modal`).3970 // ever up while neither of them is (`modal`).
3895 .children(self.context_card(window.viewport_size(), cx))3971 .children(self.context_card(window.viewport_size(), cx))
@@ -4210,7 +4286,7 @@ impl Player {
4210 &self,4286 &self,
4211 position: f64,4287 position: f64,
4212 duration: f64,4288 duration: f64,
4213- playing: bool,4289+ state: Transport,
4214 cx: &mut Context<Self>,4290 cx: &mut Context<Self>,
4215 ) -> impl IntoElement {4291 ) -> impl IntoElement {
4216 // Where the playhead is *on the bed*, which at the fit is where along4292 // Where the playhead is *on the bed*, which at the fit is where along
@@ -4224,7 +4300,7 @@ impl Player {
4224 let exporting = self.exporting().is_some();4300 let exporting = self.exporting().is_some();
4225 // Everything but Import and Keys needs a timeline to act on: with none4301 // Everything but Import and Keys needs a timeline to act on: with none
4226 // open they are dimmed rather than silently doing nothing.4302 // open they are dimmed rather than silently doing nothing.
4227- let live = self.session.is_some() && !exporting;4303+ let live = state != Transport::Stopped && !exporting;
4228 // The project's own picture size, for the button that cycles it: read4304 // The project's own picture size, for the button that cycles it: read
4229 // per render like everything else here, so a cycle shows on the button4305 // per render like everything else here, so a cycle shows on the button
4230 // that made it.4306 // that made it.
@@ -4270,7 +4346,7 @@ impl Player {
4270 // of this line is what a narrow window truncates.4346 // of this line is what a narrow window truncates.
4271 (4347 (
4272 join_detail(4348 join_detail(
4273- &self.live_decode(position, playing),4349+ &self.live_decode(position, state.is_playing()),
4274 &format!(4350 &format!(
4275 "{} copy · {} paste · {} undo · click the bar to seek · drop a file to \4351 "{} copy · {} paste · {} undo · click the bar to seek · drop a file to \
4276 import",4352 import",
@@ -4309,9 +4385,9 @@ impl Player {
4309 .overflow_x_scroll()4385 .overflow_x_scroll()
4310 .child(control(4386 .child(control(
4311 "transport",4387 "transport",
4312- Some(transport_glyph(playing).into_any_element()),4388+ Some(transport_glyph(state).into_any_element()),
4313- if playing { "Pause" } else { "Play" },4389+ if state.is_playing() { "Pause" } else { "Play" },
4314- if nothing_to_play(self.session.as_ref()) && !playing {4390+ if nothing_to_play(self.session.as_ref()) && !state.is_playing() {
4315 format!("{} — put a clip on a lane first", key(ActionId::Play))4391 format!("{} — put a clip on a lane first", key(ActionId::Play))
4316 } else {4392 } else {
4317 key(ActionId::Play)4393 key(ActionId::Play)
@@ -4321,7 +4397,7 @@ impl Player {
4321 // against nothing (the key press answers with the4397 // against nothing (the key press answers with the
4322 // notice). Still live while it *is* playing: a delete4398 // notice). Still live while it *is* playing: a delete
4323 // can empty the timeline mid-play and that has to stop.4399 // can empty the timeline mid-play and that has to stop.
4324- live && (playing || !nothing_to_play(self.session.as_ref())),4400+ live && (state.is_playing() || !nothing_to_play(self.session.as_ref())),
4325 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),4401 cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)),
4326 ))4402 ))
4327 .child(separator())4403 .child(separator())
@@ -8814,9 +8890,12 @@ fn empty_hint() -> impl IntoElement {
8814 )8890 )
8815 }8891 }
8816 8892
8817-/// Two bars while playing, a triangle while paused. Drawn, so there is no icon8893+/// Two bars while playing, a triangle in every other state -- paused, nothing
8818-/// font and no glyph coverage to depend on.8894+/// open, and played out, where the button's next act is to start over rather
8819-fn transport_glyph(playing: bool) -> impl IntoElement {8895+/// than to stop something. Drawn, so there is no icon font and no glyph
8896+/// coverage to depend on.
8897+fn transport_glyph(state: Transport) -> impl IntoElement {
8898+ let playing = state.is_playing();
8820 div()8899 div()
8821 .flex()8900 .flex()
8822 .items_center()8901 .items_center()
@@ -8872,15 +8951,15 @@ mod tests {
8872 KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, LETTERBOX, LIBRARY_MAX_W,8951 KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LANES_MAX, LETTERBOX, LIBRARY_MAX_W,
8873 LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, MENU_ROWS_H, MENU_W, NO_FILE,8952 LIBRARY_MIN_W, Lane, MENU_ITEMS, MENU_PAD, MENU_ROW_H, MENU_ROWS_H, MENU_W, NO_FILE,
8874 PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,8953 PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SILENCE_ROWS, SOURCE_TINTS, SPEED_PRESETS,
8875- SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, VOLUME_W, Volume, WAVE_BPS,8954+ SPEED_STEP, SURFACE, SWATCH_W, Source, Speed, StreamInfo, Transport, VOLUME_W, Volume,
8876- WAVE_COL, Wave, applicable, band_label, bitrate_refusal, can_add, cancels_export,8955+ WAVE_BPS, WAVE_COL, Wave, applicable, band_label, bitrate_refusal, can_add, cancels_export,
8877 clipboard_after_remove, color_snap, containers, envelope, eq_spectrum, eq_x, eq_y,8956 clipboard_after_remove, color_snap, containers, envelope, eq_spectrum, eq_x, eq_y,
8878 estimated_mb, export_path, export_settings, format_key, format_line, format_refusal,8957 estimated_mb, export_path, export_settings, format_key, format_line, format_refusal,
8879 fps_label, frac_along, frac_down, frame_at, histogram, is_bare_modifier, is_project,8958 fps_label, frac_along, frac_down, frame_at, histogram, is_bare_modifier, is_project,
8880 keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h,8959 keymap, lanes_h, marked, menu_at, next_container, normalise, nothing_to_play, panel_h,
8881 project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped,8960 project_path, push_digit, retarget, scrub_due, show_label, silence_rate, snapped,
8882- source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, unseen_paths,8961+ source_tint, span_partner, speed_at, summary_head, summary_tail, timecode, transport,
8883- unseen_sources, whole_take, width_frac, window_title,8962+ unseen_paths, unseen_sources, whole_take, width_frac, window_title,
8884 };8963 };
8885 use super::{8964 use super::{
8886 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,8965 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,
@@ -8909,7 +8988,7 @@ mod tests {
8909 use gpui::{Bounds, Pixels, point, px, size};8988 use gpui::{Bounds, Pixels, point, px, size};
8910 use std::collections::HashMap;8989 use std::collections::HashMap;
8911 use std::path::{Path, PathBuf};8990 use std::path::{Path, PathBuf};
8912- use std::time::Duration;8991+ use std::time::{Duration, Instant};
8913 8992
8914 fn asset(name: &str) -> PathBuf {8993 fn asset(name: &str) -> PathBuf {
8915 Path::new(env!("CARGO_MANIFEST_DIR"))8994 Path::new(env!("CARGO_MANIFEST_DIR"))
@@ -10762,6 +10841,95 @@ mod tests {
10762 assert_eq!(volume.gain(), 0.75);10841 assert_eq!(volume.gain(), 0.75);
10763 }10842 }
10764 10843
10844+ /// The whole transport in one place: the clock keeps running past the last
10845+ /// frame (wall time takes over at audio EOF), so "the clock is going" is
10846+ /// not "this is playing" -- and a button that read the clock showed Pause
10847+ /// on a timeline that had stopped moving. Ended is its own state, it draws
10848+ /// Play, and the next press starts over from the top.
10849+ #[test]
10850+ fn a_played_out_timeline_is_not_playing_and_the_next_press_starts_it_over() {
10851+ assert_eq!(transport(true, false), Transport::Playing);
10852+ assert_eq!(transport(false, false), Transport::Paused);
10853+ // The transition the bug was about: the clock is still running and the
10854+ // decoder is finished. Played out wins, however the clock reads.
10855+ assert_eq!(transport(true, true), Transport::Ended);
10856+ assert_eq!(transport(false, true), Transport::Ended);
10857+
10858+ // What the button draws, in each state. Two bars only while it moves.
10859+ assert!(Transport::Playing.is_playing());
10860+ for state in [Transport::Paused, Transport::Ended, Transport::Stopped] {
10861+ assert!(!state.is_playing(), "{state:?} must draw the Play triangle");
10862+ }
10863+
10864+ // And what a press does: start over at the end, plain toggle before it,
10865+ // nothing with no timeline. Same answer for the key and the button --
10866+ // both come through `Player::toggle_or_restart`.
10867+ assert!(Transport::Ended.restarts());
10868+ for state in [Transport::Playing, Transport::Paused, Transport::Stopped] {
10869+ assert!(!state.restarts(), "{state:?} must toggle, not reseek");
10870+ }
10871+ }
10872+
10873+ /// The half of `Ended` the eye cannot see: the clock. Wall time takes over
10874+ /// at the last frame and nothing used to stop it, so the playhead walked off
10875+ /// the end of the timeline in real time -- and the playhead is what a cut, a
10876+ /// paste, an insert and the analyser all act at. `pump` pauses on the
10877+ /// crossing; this is the engine contract that rests on, driven exactly as
10878+ /// the pump drives it.
10879+ #[test]
10880+ fn the_clock_stops_where_the_timeline_does_and_the_end_still_restarts() {
10881+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
10882+ session.set_gain(0.0);
10883+ // Start a breath short of the end: the tail is what this is about, and
10884+ // playing the whole five seconds would say nothing more.
10885+ session.seek(4.8);
10886+ session.play();
10887+
10888+ // The pump's own loop -- tick, drain, ask where the transport is --
10889+ // with a deadline so a fixture that will not decode fails as a failure
10890+ // rather than as a hang.
10891+ let deadline = Instant::now() + Duration::from_secs(20);
10892+ let mut state = transport(session.is_playing(), session.is_eos());
10893+ while state != Transport::Ended {
10894+ assert!(Instant::now() < deadline, "never reached the end of a 5s file");
10895+ session.tick();
10896+ while session.try_frame().is_some() {}
10897+ state = transport(session.is_playing(), session.is_eos());
10898+ }
10899+
10900+ // What `pump` does on the crossing, and the whole point of it: the
10901+ // position holds still afterwards instead of counting on past the end,
10902+ // and it holds still *on the out point* -- where the timecode and the
10903+ // playhead have been showing it. The clock at the moment the end is
10904+ // recognised is not that: a slow renderer reaches EOF with the clock
10905+ // seconds past the timeline, which is why this repositions rather than
10906+ // only freezing.
10907+ session.halt_at_end();
10908+ let stopped_at = session.now();
10909+ assert_eq!(stopped_at, session.timeline_duration());
10910+ std::thread::sleep(Duration::from_millis(250));
10911+ assert_eq!(session.now(), stopped_at, "the clock kept running past EOF");
10912+ // And it is still the end: pausing must not spend the state the glyph
10913+ // and the restart both read.
10914+ assert!(session.is_eos());
10915+ assert_eq!(
10916+ transport(session.is_playing(), session.is_eos()),
10917+ Transport::Ended
10918+ );
10919+
10920+ // The restart path off that frozen end, which is what the button and
10921+ // the play key do from `Ended`: back to the top, and running.
10922+ session.seek(0.);
10923+ session.play();
10924+ assert!(!session.is_eos(), "a seek revives the session");
10925+ assert!(session.now() < 1.0);
10926+ assert_eq!(
10927+ transport(session.is_playing(), session.is_eos()),
10928+ Transport::Playing
10929+ );
10930+ session.pause();
10931+ }
10932+
10765 /// Both ends hold under a key held down: the ABI only accepts `0.0..=1.0`,10933 /// Both ends hold under a key held down: the ABI only accepts `0.0..=1.0`,
10766 /// and a wrapped step count would hand it something else.10934 /// and a wrapped step count would hand it something else.
10767 #[test]10935 #[test]
@@ -11725,8 +11893,6 @@ fn main() {
11725 name: name.clone(),11893 name: name.clone(),
11726 image: None,11894 image: None,
11727 held: None,11895 held: None,
11728- eos: false,
11729- done: false,
11730 ruler: Rc::default(),11896 ruler: Rc::default(),
11731 // The whole timeline across the bed, which is where a11897 // The whole timeline across the bed, which is where a
11732 // project opens: zooming is something the user asks for.11898 // project opens: zooming is something the user asks for.
crates/engine/src/playback.rsmodified+19 -0
@@ -1787,6 +1787,25 @@ impl PlaybackSession {
1787 }1787 }
1788 }1788 }
1789 1789
1790+ /// Stops the transport where the timeline stops: the clock is paused and put
1791+ /// on the out point.
1792+ ///
1793+ /// Playing past the last frame is wall time counting on past the end of the
1794+ /// media -- there is no picture left to pace it -- so a session left at EOF
1795+ /// reports a `now()` that walks off the end of the timeline in real time,
1796+ /// and a caller that reads the playhead (a cut, a paste, an insert) then
1797+ /// acts at a position no clip is at. How far off depends only on how long
1798+ /// the window was left open, so no clamp in a caller is worth writing.
1799+ ///
1800+ /// End of stream is deliberately left set: the picture is still the last
1801+ /// frame and this is still the end. That is what makes it safe to call on
1802+ /// the crossing into it -- the state the transport shows does not change,
1803+ /// only the running clock stops.
1804+ pub fn halt_at_end(&mut self) {
1805+ self.pause();
1806+ self.clock.seek_to(self.timeline_duration());
1807+ }
1808+
1790 /// Sets the monitoring gain, 0.0 (silent) to 1.0. Mute is a gain of 0.0:1809 /// Sets the monitoring gain, 0.0 (silent) to 1.0. Mute is a gain of 0.0:
1791 /// one knob, because muting and turning it down are the same thing to the1810 /// one knob, because muting and turning it down are the same thing to the
1792 /// device, and *which* the user meant is a question only the UI can answer1811 /// device, and *which* the user meant is a question only the UI can answer