The progress line counts up, counts down and says how far
Browse files at this commitb640c4e46f0e5ffdcb8dda576aa15aa226b126c2Parent: dea8a27- feat
- the progress line counts up, counts down and says how far
1 changed file +164 -4
- crates/app/src/main.rs +164 -4
crates/app/src/main.rsmodified+164 -4
| @@ -713,6 +713,11 @@ struct Player { | |||
| 713 | /// handle is held until the worker settles, because its last act is to | 713 | /// handle is held until the worker settles, because its last act is to |
| 714 | /// delete the output file and a second export must not be what it deletes. | 714 | /// delete the output file and a second export must not be what it deletes. |
| 715 | cancelling: bool, | 715 | cancelling: bool, |
| 716 | + /// When the running export started, and how far it had come at each sample | ||
| 717 | + /// since, as `(elapsed, progress)` marks. The elapsed clock and the | ||
| 718 | + /// rolling-window estimate the progress line reads; see [`note_progress`]. | ||
| 719 | + export_started: Option<Instant>, | ||
| 720 | + export_marks: Vec<(f32, f32)>, | ||
| 716 | /// Where an export writes. Built once from the source path, which is not | 721 | /// Where an export writes. Built once from the source path, which is not |
| 717 | /// otherwise kept. | 722 | /// otherwise kept. |
| 718 | export_path: PathBuf, | 723 | export_path: PathBuf, |
| @@ -3343,6 +3348,10 @@ impl Player { | |||
| 3343 | } | 3348 | } |
| 3344 | session.pause(); | 3349 | session.pause(); |
| 3345 | self.export = Some(session.export_to_with(&self.export_path, &settings)); | 3350 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 3351 | + // The clock starts with the worker, not with the first repaint that | ||
| 3352 | + // happens to notice it. | ||
| 3353 | + self.export_started = Some(Instant::now()); | ||
| 3354 | + self.export_marks.clear(); | ||
| 3346 | // The card has been answered; the progress line takes the panel from | 3355 | // The card has been answered; the progress line takes the panel from |
| 3347 | // here, and it is the running export's escape that matters now. | 3356 | // here, and it is the running export's escape that matters now. |
| 3348 | self.export_open = false; | 3357 | self.export_open = false; |
| @@ -3361,10 +3370,23 @@ impl Player { | |||
| 3361 | /// Takes the export's verdict once it has one. The only place the app | 3370 | /// Takes the export's verdict once it has one. The only place the app |
| 3362 | /// touches the handle's completion side. | 3371 | /// touches the handle's completion side. |
| 3363 | fn poll_export(&mut self) { | 3372 | fn poll_export(&mut self) { |
| 3373 | + // Sampled here rather than while drawing: a repaint stays a repaint, | ||
| 3374 | + // and this runs once per repaint either way. | ||
| 3375 | + if let (Some(progress), Some(started)) = ( | ||
| 3376 | + self.exporting().map(ExportHandle::progress), | ||
| 3377 | + self.export_started, | ||
| 3378 | + ) { | ||
| 3379 | + note_progress( | ||
| 3380 | + &mut self.export_marks, | ||
| 3381 | + started.elapsed().as_secs_f32(), | ||
| 3382 | + progress, | ||
| 3383 | + ); | ||
| 3384 | + } | ||
| 3364 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { | 3385 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { |
| 3365 | return; | 3386 | return; |
| 3366 | }; | 3387 | }; |
| 3367 | self.export = None; | 3388 | self.export = None; |
| 3389 | + self.export_started = None; | ||
| 3368 | // A cancellation is reported as an error, and the one who asked for it | 3390 | // A cancellation is reported as an error, and the one who asked for it |
| 3369 | // has had the editor back since the keystroke. Nothing to say. | 3391 | // has had the editor back since the keystroke. Nothing to say. |
| 3370 | if std::mem::take(&mut self.cancelling) { | 3392 | if std::mem::take(&mut self.cancelling) { |
| @@ -4238,10 +4260,25 @@ impl Player { | |||
| 4238 | } | 4260 | } |
| 4239 | let (hint, filled) = if let Some(export) = self.exporting() { | 4261 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 4240 | let progress = export.progress(); | 4262 | let progress = export.progress(); |
| 4263 | + let elapsed = self | ||
| 4264 | + .export_started | ||
| 4265 | + .map_or(0., |t| t.elapsed().as_secs_f32()); | ||
| 4266 | + // Two numbers that must both be honest: the one that counts up is | ||
| 4267 | + // measured, the one that counts down is a guess and says so. | ||
| 4268 | + let left = eta_secs(&self.export_marks, elapsed, progress).map_or_else( | ||
| 4269 | + || "estimating…".to_owned(), | ||
| 4270 | + |s| format!("~{} left", clock(s)), | ||
| 4271 | + ); | ||
| 4241 | ( | 4272 | ( |
| 4242 | format!( | 4273 | format!( |
| 4243 | - "EXPORTING {}% · {} · {} — {} cancels", | 4274 | + // Clocks, then the way out, then what is encoding: at the |
| 4275 | + // 640 px floor the tail is what truncation eats, and the | ||
| 4276 | + // codec pair is the one part of this line the card already | ||
| 4277 | + // said. The escape must not be what goes missing. | ||
| 4278 | + "EXPORTING {}% · {} elapsed · {left} — {} cancels · {} · {}", | ||
| 4244 | (progress * 100.) as u32, | 4279 | (progress * 100.) as u32, |
| 4280 | + clock(elapsed), | ||
| 4281 | + key(ActionId::CancelExport), | ||
| 4245 | // The row that was picked; the engine's line below names the | 4282 | // The row that was picked; the engine's line below names the |
| 4246 | // seats alone, since the library is what identifies a codec. | 4283 | // seats alone, since the library is what identifies a codec. |
| 4247 | format_label(self.format), | 4284 | format_label(self.format), |
| @@ -4250,7 +4287,6 @@ impl Player { | |||
| 4250 | export | 4287 | export |
| 4251 | .encoders() | 4288 | .encoders() |
| 4252 | .unwrap_or_else(|| "opening the encoder".to_string()), | 4289 | .unwrap_or_else(|| "opening the encoder".to_string()), |
| 4253 | - key(ActionId::CancelExport) | ||
| 4254 | ), | 4290 | ), |
| 4255 | progress, | 4291 | progress, |
| 4256 | ) | 4292 | ) |
| @@ -8842,6 +8878,57 @@ fn timecode(t: f64, fps: f64) -> String { | |||
| 8842 | ) | 8878 | ) |
| 8843 | } | 8879 | } |
| 8844 | 8880 | ||
| 8881 | +/// Wall clock for a progress line: `M:SS`, minutes past the hour included | ||
| 8882 | +/// rather than an hours field nobody reads on an export. | ||
| 8883 | +fn clock(secs: f32) -> String { | ||
| 8884 | + let secs = secs.max(0.) as u64; | ||
| 8885 | + format!("{}:{:02}", secs / 60, secs % 60) | ||
| 8886 | +} | ||
| 8887 | + | ||
| 8888 | +/// How often a progress mark is worth keeping, how far back the rate is | ||
| 8889 | +/// measured, and the least span that may answer at all. An export crosses | ||
| 8890 | +/// hardware and software segments that run at different speeds, so the | ||
| 8891 | +/// estimate is a window's average and never the instant's. | ||
| 8892 | +const ETA_SAMPLE: f32 = 0.5; | ||
| 8893 | +const ETA_WINDOW: f32 = 8.; | ||
| 8894 | +const ETA_SPAN: f32 = 1.5; | ||
| 8895 | + | ||
| 8896 | +/// Records where the export has got to and forgets what has fallen out of the | ||
| 8897 | +/// window. One mark per `ETA_SAMPLE`, a window's worth kept: a bounded list | ||
| 8898 | +/// whichever way the encode goes. | ||
| 8899 | +fn note_progress(marks: &mut Vec<(f32, f32)>, elapsed: f32, progress: f32) { | ||
| 8900 | + if marks.last().is_none_or(|&(t, _)| elapsed - t >= ETA_SAMPLE) { | ||
| 8901 | + marks.push((elapsed, progress)); | ||
| 8902 | + } | ||
| 8903 | + while marks.len() > 2 && marks[0].0 < elapsed - ETA_WINDOW { | ||
| 8904 | + marks.remove(0); | ||
| 8905 | + } | ||
| 8906 | +} | ||
| 8907 | + | ||
| 8908 | +/// Seconds left at the window's rate, or `None` while nothing measurable has | ||
| 8909 | +/// happened yet -- which the line says as "estimating…" rather than as a | ||
| 8910 | +/// number it would have to take back. | ||
| 8911 | +fn eta_secs(marks: &[(f32, f32)], elapsed: f32, progress: f32) -> Option<f32> { | ||
| 8912 | + // A finished pass is not a guess: it is over. | ||
| 8913 | + if progress >= 1. { | ||
| 8914 | + return Some(0.); | ||
| 8915 | + } | ||
| 8916 | + if progress <= 0. || elapsed < ETA_SPAN { | ||
| 8917 | + return None; | ||
| 8918 | + } | ||
| 8919 | + // Two rates, averaged. The window's follows the encode across a | ||
| 8920 | + // hardware-to-software handover; the whole run's is what keeps a window | ||
| 8921 | + // that is all stall from throwing the number minutes out and back. Neither | ||
| 8922 | + // alone reads well: raw window rate spikes eightfold on either edge of a | ||
| 8923 | + // stall, and the run average alone never notices a segment change. | ||
| 8924 | + let overall = progress / elapsed; | ||
| 8925 | + let recent = marks | ||
| 8926 | + .first() | ||
| 8927 | + .filter(|&&(t, _)| elapsed - t >= ETA_SPAN) | ||
| 8928 | + .map_or(overall, |&(t, p)| (progress - p) / (elapsed - t)); | ||
| 8929 | + Some(2. * (1. - progress) / (recent + overall)) | ||
| 8930 | +} | ||
| 8931 | + | ||
| 8845 | #[cfg(test)] | 8932 | #[cfg(test)] |
| 8846 | mod tests { | 8933 | mod tests { |
| 8847 | use super::{ | 8934 | use super::{ |
| @@ -8863,8 +8950,8 @@ mod tests { | |||
| 8863 | unseen_sources, whole_take, width_frac, window_title, | 8950 | unseen_sources, whole_take, width_frac, window_title, |
| 8864 | }; | 8951 | }; |
| 8865 | use super::{ | 8952 | use super::{ |
| 8866 | - LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows, | 8953 | + ETA_SPAN, LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, clock, eta_secs, file_name, |
| 8867 | - repeats, unscannable, | 8954 | + file_uri, library_rows, note_progress, repeats, unscannable, |
| 8868 | }; | 8955 | }; |
| 8869 | 8956 | ||
| 8870 | /// What the file manager is handed: the parts a path keeps as they are, and | 8957 | /// What the file manager is handed: the parts a path keeps as they are, and |
| @@ -10984,6 +11071,77 @@ mod tests { | |||
| 10984 | ); | 11071 | ); |
| 10985 | } | 11072 | } |
| 10986 | 11073 | ||
| 11074 | + /// The progress line's two clocks, driven the way a repaint drives them: | ||
| 11075 | + /// steady work, a stall where hardware hands over to software, then steady | ||
| 11076 | + /// work again. The estimate may not whipsaw, may not vanish once it has | ||
| 11077 | + /// been given, and must meet the elapsed clock at the end. | ||
| 11078 | + #[test] | ||
| 11079 | + fn the_export_estimate_rides_out_a_stall_and_converges() { | ||
| 11080 | + let (mut marks, mut elapsed, mut progress) = (Vec::new(), 0f32, 0f32); | ||
| 11081 | + // 2%/s, a 12 s stall at 40% -- longer than the window, so the window | ||
| 11082 | + // alone would have nothing left to measure -- and the same rate to the | ||
| 11083 | + // end: 62 s of wall clock for 50 s of work. | ||
| 11084 | + let rate = 0.02; | ||
| 11085 | + let (mut quiet, mut before_stall, mut after_stall, mut last) = (0f32, 0., 0., f32::MAX); | ||
| 11086 | + while progress < 1. { | ||
| 11087 | + let stalled = (20. ..32.).contains(&elapsed); | ||
| 11088 | + note_progress(&mut marks, elapsed, progress); | ||
| 11089 | + // What is really left, to hold every guess against. | ||
| 11090 | + let truth = (1. - progress) / rate + if stalled { 32. - elapsed } else { 0. }; | ||
| 11091 | + match eta_secs(&marks, elapsed, progress) { | ||
| 11092 | + // "estimating…" is only allowed before there is a span to | ||
| 11093 | + // measure, and never again after the first number. | ||
| 11094 | + None => { | ||
| 11095 | + assert!(elapsed < ETA_SPAN + 1., "estimate vanished at {elapsed}"); | ||
| 11096 | + quiet = elapsed; | ||
| 11097 | + } | ||
| 11098 | + Some(left) => { | ||
| 11099 | + // No guess is ever wilder than four times the truth: that | ||
| 11100 | + // is the eightfold spike a raw window rate throws on either | ||
| 11101 | + // edge of the stall. | ||
| 11102 | + assert!(left <= truth * 4., "at {elapsed}s: {left} vs {truth}"); | ||
| 11103 | + // While the rate holds, the answer is the true one and it | ||
| 11104 | + // only ever counts down. | ||
| 11105 | + if elapsed >= 5. && elapsed < 20. { | ||
| 11106 | + assert!((left - truth).abs() <= truth * 0.15, "{elapsed}s: {left}"); | ||
| 11107 | + assert!(left < last, "estimate grew while the rate held"); | ||
| 11108 | + } | ||
| 11109 | + // Eight seconds past the stall it has caught up again. | ||
| 11110 | + if elapsed >= 40. { | ||
| 11111 | + assert!((left - truth).abs() <= truth * 0.25, "{elapsed}s: {left}"); | ||
| 11112 | + assert!(left < last + 0.001, "estimate grew after the stall"); | ||
| 11113 | + } | ||
| 11114 | + if (20. ..20.25).contains(&elapsed) { | ||
| 11115 | + before_stall = left; | ||
| 11116 | + } | ||
| 11117 | + if (31.5..31.75).contains(&elapsed) { | ||
| 11118 | + after_stall = left; | ||
| 11119 | + } | ||
| 11120 | + last = left; | ||
| 11121 | + } | ||
| 11122 | + } | ||
| 11123 | + // A window's worth of marks and no more, however long the encode. | ||
| 11124 | + assert!(marks.len() <= 20, "{} marks", marks.len()); | ||
| 11125 | + elapsed += 0.25; | ||
| 11126 | + if !stalled { | ||
| 11127 | + progress = (progress + rate * 0.25).min(1.); | ||
| 11128 | + } | ||
| 11129 | + } | ||
| 11130 | + assert!(quiet > 0. && quiet < ETA_SPAN + 1.); | ||
| 11131 | + // The stall stretched the guess instead of erasing it, and by more than | ||
| 11132 | + // the stopped clock adds on its own. | ||
| 11133 | + assert!( | ||
| 11134 | + after_stall > before_stall + 12., | ||
| 11135 | + "{before_stall} -> {after_stall}" | ||
| 11136 | + ); | ||
| 11137 | + // Both clocks meet: a finished pass has nothing left. | ||
| 11138 | + assert_eq!(eta_secs(&marks, elapsed, 1.), Some(0.)); | ||
| 11139 | + assert!((elapsed - 62.).abs() < 1., "{elapsed}"); | ||
| 11140 | + assert_eq!(clock(83.4), "1:23"); | ||
| 11141 | + assert_eq!(clock(114.), "1:54"); | ||
| 11142 | + assert_eq!(clock(-1.), "0:00"); | ||
| 11143 | + } | ||
| 11144 | + | ||
| 10987 | #[test] | 11145 | #[test] |
| 10988 | fn every_codec_row_is_offered_or_says_why_not() { | 11146 | fn every_codec_row_is_offered_or_says_why_not() { |
| 10989 | // One row per codec, and the boxes it can go in are the container row's | 11147 | // One row per codec, and the boxes it can go in are the container row's |
| @@ -11728,6 +11886,8 @@ fn main() { | |||
| 11728 | last_target: 0, | 11886 | last_target: 0, |
| 11729 | export: None, | 11887 | export: None, |
| 11730 | cancelling: false, | 11888 | cancelling: false, |
| 11889 | + export_started: None, | ||
| 11890 | + export_marks: Vec::new(), | ||
| 11731 | export_path: out.clone(), | 11891 | export_path: out.clone(), |
| 11732 | project_path: project.clone(), | 11892 | project_path: project.clone(), |
| 11733 | keymap: keymap.clone(), | 11893 | keymap: keymap.clone(), |