forge
Explore
Log in

Don't you dare go Hollow

A stand-in being made can be stopped

Browse files at this commit

e534575ef08c4a17322435a9b12ff819be1bc016Ahmet Kaan GümüşParent: 2995059
  • fix
    • a stand-in being made can be stopped
Show full message
🐛 fix(ui): a stand-in being made can be stopped

A proxy is a whole film through a decoder and an encoder, started
without being asked for, and until now there was nothing that could stop
one: the row said "proxy 12%" for as long as it took, on a machine
somebody was trying to cut on.

So the row that shows the encode running carries the way out of it: a ×
beside the percentage, the subtitle row's button and the subtitle row's
rule -- it stops the click there, since the row under it picks a file
and stopping an encode is not a way of choosing one.

The engine already had the stop (`proxy::Job::cancel`, the export's own
checkpoints), so nothing new was invented for it. What is new is the two
states between the click and the end: `Cancelling` holds the job while
the worker winds down -- it gives up at its next frame and deletes its
own half-written file -- and `Cancelled` is where it settles, kept in
the map so no repaint starts the encode that was just stopped all over
again.

The worker's own answer is what settles it, which is what makes the
three claims checkable rather than hopeful: nothing is left under the
name a finished stand-in would have (the cache is looked up by existence
alone, so a file left there is a cut made on a truncated picture), the
`.part` goes with it, and an encode that *beat* the stop settles as the
ready stand-in it is rather than being deleted underneath the switch
that is playing it. Asking twice is asking once.

The new test in tests/export.rs is those four in one pass, on the
software seat: stop it mid-encode, wait for the worker, then look at the
directory.
crates/app/src/importing.rsmodified+15 -0
@@ -76,6 +76,16 @@ pub(crate) enum Proxy {
76 Native,76 Native,
77 /// Being made, in the background, while the film keeps playing.77 /// Being made, in the background, while the film keeps playing.
78 Making(engine::proxy::Job),78 Making(engine::proxy::Job),
79+ /// Stopped by hand ([`Player::cancel_proxy`]) and not yet gone: the worker
80+ /// gives up at its next frame and deletes the half-written file itself, and
81+ /// the job is held until it does. Kept as a state rather than dropped
82+ /// because the encode may still *beat* the cancel -- a stand-in that is
83+ /// already written is a file in the cache and is reported as one.
84+ Cancelling(engine::proxy::Job),
85+ /// Stopped, and the worker has gone. Not [`Self::Failed`]: nothing went
86+ /// wrong, somebody asked for it -- and it stays in the map so no repaint
87+ /// starts the encode that was just stopped over again.
88+ Cancelled,
79 /// There is one in the cache. What the switch actually plays.89 /// There is one in the cache. What the switch actually plays.
80 Ready,90 Ready,
81 /// It could not be made: the film itself is what plays, and the row says91 /// It could not be made: the film itself is what plays, and the row says
@@ -93,6 +103,11 @@ impl Proxy {
93 // Rounded down: a bar that says 100% while the encoder is still103 // Rounded down: a bar that says 100% while the encoder is still
94 // writing is the one number a progress line must never show.104 // writing is the one number a progress line must never show.
95 Self::Making(job) => format!("proxy {}%", (job.progress() * 100.) as u32),105 Self::Making(job) => format!("proxy {}%", (job.progress() * 100.) as u32),
106+ // Its own word while the worker is still winding down: "no proxy"
107+ // the instant the × is clicked would be a claim about a file that
108+ // is at that moment still being written.
109+ Self::Cancelling(_) => "proxy stopping…".to_string(),
110+ Self::Cancelled => "no proxy — stopped".to_string(),
96 Self::Ready => "proxy".to_string(),111 Self::Ready => "proxy".to_string(),
97 Self::Failed => "no proxy".to_string(),112 Self::Failed => "no proxy".to_string(),
98 }113 }
crates/app/src/player/library.rsmodified+69 -12
@@ -313,37 +313,87 @@ impl Player {
313 /// Nothing waits for it: until a proxy is ready the film itself is what313 /// Nothing waits for it: until a proxy is ready the film itself is what
314 /// plays, switch or no switch ([`engine::PlaybackSession::picture_path`]),314 /// plays, switch or no switch ([`engine::PlaybackSession::picture_path`]),
315 /// so a failure costs a line and no more.315 /// so a failure costs a line and no more.
316+ ///
317+ /// A stopped one settles here too ([`Player::cancel_proxy`]), which is what
318+ /// makes the stop honest: the worker's own answer is what says the encode
319+ /// is over and its half-written file gone -- and where the encode *beat*
320+ /// the cancel, the answer is a finished stand-in and it is reported as one
321+ /// rather than thrown away.
316 pub(crate) fn poll_proxies(&mut self, cx: &mut Context<Self>) {322 pub(crate) fn poll_proxies(&mut self, cx: &mut Context<Self>) {
317- let done: Vec<(PathBuf, engine::Result<PathBuf>)> = self323+ let done: Vec<(PathBuf, bool, engine::Result<PathBuf>)> = self
318 .proxies324 .proxies
319 .iter()325 .iter()
320 .filter_map(|(path, state)| match state {326 .filter_map(|(path, state)| match state {
321- Proxy::Making(job) => Some((path.clone(), job.outcome()?)),327+ Proxy::Making(job) => Some((path.clone(), false, job.outcome()?)),
328+ Proxy::Cancelling(job) => Some((path.clone(), true, job.outcome()?)),
322 _ => None,329 _ => None,
323 })330 })
324 .collect();331 .collect();
325- for (path, outcome) in done {332+ for (path, stopped, outcome) in done {
326- let text = match &outcome {333+ let text = match (&outcome, stopped) {
327- Ok(_) => format!(334+ (Ok(_), _) => format!(
328 "PROXY READY for {} — Proxies on cuts on it",335 "PROXY READY for {} — Proxies on cuts on it",
329 file_name(&path)336 file_name(&path)
330 ),337 ),
331- Err(e) => format!(338+ // Not a failure and not worded as one: the worker gave up
339+ // because it was asked to, and it took its half-written file
340+ // with it.
341+ (Err(_), true) => format!(
342+ "PROXY STOPPED for {} — nothing of it was kept, and the film itself is what \
343+ plays",
344+ file_name(&path)
345+ ),
346+ (Err(e), false) => format!(
332 "PROXY FAILED for {} — {e} — the film itself is what plays",347 "PROXY FAILED for {} — {e} — the film itself is what plays",
333 file_name(&path)348 file_name(&path)
334 ),349 ),
335 };350 };
336 eprintln!("{text}");351 eprintln!("{text}");
337 self.notify_user(text.into());352 self.notify_user(text.into());
338- let state = match outcome {353+ let state = match (outcome, stopped) {
339- Ok(_) => Proxy::Ready,354+ (Ok(_), _) => Proxy::Ready,
340- Err(_) => Proxy::Failed,355+ (Err(_), true) => Proxy::Cancelled,
356+ (Err(_), false) => Proxy::Failed,
341 };357 };
342 self.proxies.insert(path, state);358 self.proxies.insert(path, state);
343 cx.notify();359 cx.notify();
344 }360 }
345 }361 }
346 362
363+ /// The × on a library row while its stand-in is being made: the worker is
364+ /// asked to give up at its next frame and delete the half-written file it
365+ /// was writing ([`engine::proxy::Job::cancel`]), and the row says so until
366+ /// it has. A whole feature film re-encoded is minutes of a machine the cut
367+ /// is being made on, and there was no way to take it back.
368+ ///
369+ /// Nothing is deleted from here and nothing waits: the film itself is what
370+ /// plays throughout ([`engine::PlaybackSession::picture_path`] stats the
371+ /// cache and finds nothing), so a stop costs a line and no more. Asking
372+ /// twice is asking once -- the second click finds a state that is not
373+ /// [`Proxy::Making`] and answers nothing -- and a cancel that arrives after
374+ /// the encode has already written its file settles as the *ready* stand-in
375+ /// it is ([`Player::poll_proxies`]), never as a deletion of one in use.
376+ pub(crate) fn cancel_proxy(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {
377+ // Taken out of its slot and put back one state on, rather than removed:
378+ // a path that left this map is a path the next repaint starts an encode
379+ // for all over again ([`Player::cache_media`]).
380+ let Some(slot) = self.proxies.get_mut(path) else {
381+ return;
382+ };
383+ if !matches!(slot, Proxy::Making(_)) {
384+ return;
385+ }
386+ let Proxy::Making(job) = std::mem::replace(slot, Proxy::Cancelled) else {
387+ unreachable!("the slot was just read as one being made")
388+ };
389+ job.cancel();
390+ *slot = Proxy::Cancelling(job);
391+ let text = format!("STOPPING the stand-in for {}…", file_name(path));
392+ eprintln!("{text}");
393+ self.notify_user(text.into());
394+ cx.notify();
395+ }
396+
347 /// What the Proxies button says after the state: how far the stand-ins397 /// What the Proxies button says after the state: how far the stand-ins
348 /// have got, since "on" with nothing made yet and "on" with the whole398 /// have got, since "on" with nothing made yet and "on" with the whole
349 /// library standing in are two different things to be looking at. Empty399 /// library standing in are two different things to be looking at. Empty
@@ -377,19 +427,26 @@ impl Player {
377 427
378 /// Whether any stand-in is still being made -- what keeps the frame loop428 /// Whether any stand-in is still being made -- what keeps the frame loop
379 /// alive while one is, so its percentage moves on screen.429 /// alive while one is, so its percentage moves on screen.
430+ ///
431+ /// One being stopped counts: its worker is still running until it reaches
432+ /// the frame it gives up at, and the repaint that notices *that* is this
433+ /// same loop -- without it a stopped row would sit on "stopping…" until
434+ /// something else happened to repaint the window.
380 pub(crate) fn making_proxies(&self) -> bool {435 pub(crate) fn making_proxies(&self) -> bool {
381 self.proxies436 self.proxies
382 .values()437 .values()
383- .any(|p| matches!(p, Proxy::Making(_)))438+ .any(|p| matches!(p, Proxy::Making(_) | Proxy::Cancelling(_)))
384 }439 }
385 440
386 /// How many films are between "shall we?" and "done": the number the start441 /// How many films are between "shall we?" and "done": the number the start
387 /// of another is held against ([`PROXIES_AT_ONCE`]). A header being read442 /// of another is held against ([`PROXIES_AT_ONCE`]). A header being read
388- /// counts, because the answer may be an encode.443+ /// counts, because the answer may be an encode -- and so does one still
444+ /// winding down from a stop, which is an encoder that has not let go of the
445+ /// seat yet.
389 pub(crate) fn in_flight_proxies(&self) -> usize {446 pub(crate) fn in_flight_proxies(&self) -> usize {
390 self.proxies447 self.proxies
391 .values()448 .values()
392- .filter(|p| matches!(p, Proxy::Asked | Proxy::Making(_)))449+ .filter(|p| matches!(p, Proxy::Asked | Proxy::Making(_) | Proxy::Cancelling(_)))
393 .count()450 .count()
394 }451 }
395 452
crates/app/src/ui/library.rsmodified+37 -0
@@ -122,6 +122,18 @@ impl Player {
122 .get(&row.path)122 .get(&row.path)
123 .map_or_else(String::new, Proxy::detail),123 .map_or_else(String::new, Proxy::detail),
124 );124 );
125+ // A stand-in being made is minutes of this machine, and the row
126+ // that says how far it has got is where the way out of it belongs.
127+ // Gone the moment the worker settles, like the percentage beside
128+ // it: there is nothing left to stop.
129+ let making = matches!(self.proxies.get(&row.path), Some(Proxy::Making(_)));
130+ let stop_path = row.path.clone();
131+ let stop_tip: SharedString = format!(
132+ "Stop making the stand-in for {} — nothing of it is kept, and the film itself is \
133+ what plays",
134+ row.name
135+ )
136+ .into();
125 let usable = row.unusable.is_none();137 let usable = row.unusable.is_none();
126 let (path, stream) = (row.path.clone(), row.stream);138 let (path, stream) = (row.path.clone(), row.stream);
127 let dragged = (path.clone(), stream);139 let dragged = (path.clone(), stream);
@@ -219,6 +231,31 @@ impl Player {
219 .child(under),231 .child(under),
220 ),232 ),
221 )233 )
234+ // The way out of the encode, on the row that shows it running.
235+ // A `HIT_MIN` target and the subtitle row's ×, and it stops the
236+ // click there: the row under it picks the file, and stopping an
237+ // encode is not a way of choosing what to place.
238+ .when(making, |d| {
239+ d.child(
240+ div()
241+ .id(("proxy-stop", i))
242+ .flex_none()
243+ .w(px(HIT_MIN))
244+ .h_full()
245+ .flex()
246+ .items_center()
247+ .justify_center()
248+ .rounded(px(3.))
249+ .cursor_pointer()
250+ .hover(|s| s.bg(rgb(BG_HOVER())))
251+ .tooltip(move |_, cx| cx.new(|_| Tip(stop_tip.clone())).into())
252+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
253+ cx.stop_propagation();
254+ this.cancel_proxy(&stop_path, cx);
255+ }))
256+ .child("×"),
257+ )
258+ })
222 })259 })
223 .collect();260 .collect();
224 div()261 div()
crates/engine/tests/export.rsmodified+106 -0
@@ -1029,6 +1029,112 @@ fn a_proxy_is_picture_only_every_frame_a_starting_point_and_cached() {
1029 assert_eq!(engine::proxy::cached(&source), None);1029 assert_eq!(engine::proxy::cached(&source), None);
1030 }1030 }
1031 1031
1032+/// The way out of a stand-in nobody wants to wait for -- a whole film through a
1033+/// decoder and an encoder, started without being asked for, and until now with
1034+/// nothing that could stop it.
1035+///
1036+/// What a stop has to be worth trusting, all of it here: the worker really
1037+/// gives up (it settles, so its encoder is closed and its thread is gone), the
1038+/// half-written `.part` goes with it, and **nothing is left under the name a
1039+/// finished stand-in would have** -- which is the invariant that keeps a cut
1040+/// from being made on a truncated picture, since the cache is looked up by
1041+/// existence alone ([`engine::proxy::cached`]).
1042+///
1043+/// ...and the two edges: asking twice is asking once, and a stop that arrives
1044+/// on a stand-in *already written* must not take that file away -- it is what
1045+/// the switch is at that moment playing.
1046+///
1047+/// Software seat, like every default test here ([`pin_software`]).
1048+#[test]
1049+fn a_stopped_proxy_leaves_neither_a_stand_in_nor_half_of_one() {
1050+ let _seat = pin_software();
1051+ let source = asset("test_av.mp4");
1052+ let out = engine::proxy::path_for(&source).expect("a cache directory");
1053+ let part = part_path(&out);
1054+ // A stand-in left by an earlier run would make this a cache hit, and a
1055+ // cache hit is the one job with nothing to stop.
1056+ let _ = std::fs::remove_file(&out);
1057+ let _ = std::fs::remove_file(&part);
1058+
1059+ let job = engine::proxy::generate(&source).expect("start the proxy");
1060+ // Stopped with the encoder really running: a flag set before the worker has
1061+ // opened anything tests the flag and not the stop.
1062+ let opening = Instant::now();
1063+ while job.encoder().is_none() && !job.is_finished() {
1064+ assert!(
1065+ opening.elapsed() < Duration::from_secs(120),
1066+ "the encoder never opened"
1067+ );
1068+ std::thread::sleep(Duration::from_millis(10));
1069+ }
1070+ assert!(
1071+ !job.is_finished(),
1072+ "the fixture finished before it could be stopped: this test needs a film \
1073+ the software seat takes a moment over"
1074+ );
1075+ // Twice, because a second click on the × is a thing that happens.
1076+ job.cancel();
1077+ job.cancel();
1078+ let stopping = Instant::now();
1079+ while !job.is_finished() {
1080+ assert!(
1081+ stopping.elapsed() < Duration::from_secs(120),
1082+ "the worker kept encoding after the stop: {:.0}% in",
1083+ job.progress() * 100.
1084+ );
1085+ std::thread::sleep(Duration::from_millis(20));
1086+ }
1087+ let refusal = job
1088+ .outcome()
1089+ .expect("a settled job has an outcome")
1090+ .err()
1091+ .expect("a stopped proxy is not a proxy");
1092+ assert!(
1093+ refusal.to_string().contains("cancelled"),
1094+ "not the cancellation: {refusal}"
1095+ );
1096+ assert!(
1097+ !out.exists(),
1098+ "a stopped proxy left {} behind, and the cache is looked up by existence",
1099+ out.display()
1100+ );
1101+ assert!(
1102+ !part.exists(),
1103+ "a stopped proxy left the half-written {} behind",
1104+ part.display()
1105+ );
1106+ assert_eq!(
1107+ engine::proxy::cached(&source),
1108+ None,
1109+ "the cache offers a stand-in that was never finished"
1110+ );
1111+ // Past the end it is still idempotent, and the outcome is handed out once.
1112+ job.cancel();
1113+ assert!(job.outcome().is_none(), "the outcome came out twice");
1114+ assert!(!out.exists());
1115+
1116+ // The other edge, at no cost: a job for a film whose stand-in is already
1117+ // written is the cache-hit job, which is exactly what a stop racing the
1118+ // last instant of an encode settles into -- and cancelling it must leave
1119+ // the file alone, because that file is what the picture is coming off.
1120+ std::fs::write(&out, b"a stand-in that is already written").expect("seed the cache");
1121+ let hit = engine::proxy::generate(&source).expect("ask again");
1122+ assert!(hit.is_finished(), "a cached stand-in is done on the spot");
1123+ hit.cancel();
1124+ assert!(
1125+ out.is_file(),
1126+ "a stop deleted a finished stand-in the switch was playing"
1127+ );
1128+ assert_eq!(engine::proxy::cached(&source), Some(out.clone()));
1129+ assert_eq!(
1130+ hit.outcome().expect("cached outcome").expect("cached path"),
1131+ out,
1132+ "the stop turned a finished stand-in into a failure"
1133+ );
1134+
1135+ std::fs::remove_file(&out).expect("clean the cache entry up");
1136+}
1137+
1032 /// The same file on the **hardware** seat, asking the one question the software1138 /// The same file on the **hardware** seat, asking the one question the software
1033 /// twin above cannot: is what the GPU wrote really every-frame-a-starting-point?1139 /// twin above cannot: is what the GPU wrote really every-frame-a-starting-point?
1034 ///1140 ///