A stand-in switch on every row — on, off, and one encoder at a time
Browse files at this commit3da7bd9085f27d0f1a88e0bd74fc1ed40047164dParent: 99354cc- feat
- a stand-in switch on every row — on, off, and one encoder at a time
5 changed files +477 -47
- crates/app/src/layout.rs +4 -0
- crates/app/src/player/export.rs +10 -30
- crates/app/src/player/library.rs +157 -0
- crates/app/src/ui/library.rs +88 -16
- crates/engine/src/proxy.rs +218 -1
crates/app/src/layout.rsmodified+4 -0
| @@ -73,6 +73,10 @@ pub(crate) const ROW_H: f32 = 32.; | ||
| 73 | 73 | /// The tint swatch down the left of a row: the same colour that source's clips |
| 74 | 74 | /// wear in the lanes, which is the whole of the panel<->timeline association. |
| 75 | 75 | pub(crate) const SWATCH_W: f32 = 4.; |
| 76 | +/// How far the encode has come, drawn under the stop square on a library row. | |
| 77 | +/// Inside a `HIT_MIN` cell with a little air each side, so the bar never reaches | |
| 78 | +/// the edge of the target it belongs to. | |
| 79 | +pub(crate) const PROXY_BAR_W: f32 = HIT_MIN - 10.; | |
| 76 | 80 | pub(crate) const CONTROL_H: f32 = 28.; |
| 77 | 81 | /// The volume slider beside its button: a hundred steps across it, so a pixel |
| 78 | 82 | /// is finer than a step and the drag reads as continuous. |
crates/app/src/player/export.rsmodified+10 -30
| @@ -124,6 +124,11 @@ impl Player { | ||
| 124 | 124 | // stand-in through here at the next repaint, which is the only door |
| 125 | 125 | // there is. Left unseen meanwhile, so nothing is missed by having been |
| 126 | 126 | // imported while the switch was off. |
| 127 | + // Gathered here and started at the bottom of this function: the list is | |
| 128 | + // read off the session, and starting one takes the whole player | |
| 129 | + // ([`Player::start_proxy_for`], which the row's switch calls too), so | |
| 130 | + // the two cannot be the same statement. | |
| 131 | + let mut starting: Vec<PathBuf> = Vec::new(); | |
| 127 | 132 | for path in proxies_to_start( |
| 128 | 133 | session.auto_proxies(), |
| 129 | 134 | session.proxies(), |
| @@ -135,38 +140,10 @@ impl Player { | ||
| 135 | 140 | // otherwise start ten encodes fighting over the one hardware seat. |
| 136 | 141 | // The unstarted ones simply stay out of the map, which is what |
| 137 | 142 | // brings them back here ([`unseen_paths`]). |
| 138 | - if self.in_flight_proxies() >= PROXIES_AT_ONCE { | |
| 143 | + if self.in_flight_proxies() + starting.len() >= PROXIES_AT_ONCE { | |
| 139 | 144 | break; |
| 140 | 145 | } |
| 141 | - self.proxies.insert(path.clone(), Proxy::Asked); | |
| 142 | - let started = cx.background_executor().spawn({ | |
| 143 | - let path = path.clone(); | |
| 144 | - async move { engine::proxy::generate_if_wanted(&path) } | |
| 145 | - }); | |
| 146 | - cx.spawn(async move |this, cx| { | |
| 147 | - let started = started.await; | |
| 148 | - this.update(cx, |this, cx| { | |
| 149 | - let state = match started { | |
| 150 | - Ok(Some(job)) => Proxy::Making(job), | |
| 151 | - // A film that needs none keeps the state it was | |
| 152 | - // inserted with, which is what it is. | |
| 153 | - Ok(None) => Proxy::Native, | |
| 154 | - Err(e) => { | |
| 155 | - let text = format!( | |
| 156 | - "NO PROXY for {} — {e} — the film itself is what plays", | |
| 157 | - file_name(&path) | |
| 158 | - ); | |
| 159 | - eprintln!("{text}"); | |
| 160 | - this.notify_user(text.into()); | |
| 161 | - Proxy::Failed | |
| 162 | - } | |
| 163 | - }; | |
| 164 | - this.proxies.insert(path, state); | |
| 165 | - cx.notify(); | |
| 166 | - }) | |
| 167 | - .ok(); | |
| 168 | - }) | |
| 169 | - .detach(); | |
| 146 | + starting.push(path); | |
| 170 | 147 | } |
| 171 | 148 | for key in unseen_sources(session.sources(), &self.waves) { |
| 172 | 149 | self.waves.insert(key.clone(), Wave::Loading); |
| @@ -200,6 +177,9 @@ impl Player { | ||
| 200 | 177 | }) |
| 201 | 178 | .detach(); |
| 202 | 179 | } |
| 180 | + for path in starting { | |
| 181 | + self.start_proxy_for(&path, cx); | |
| 182 | + } | |
| 203 | 183 | } |
| 204 | 184 | |
| 205 | 185 | /// Probes what an export would open, once per (settings, resolution, cuts) |
crates/app/src/player/library.rsmodified+157 -0
| @@ -360,6 +360,160 @@ impl Player { | ||
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | + /// Starts the stand-in for one file: the slot is taken the moment the ask | |
| 364 | + /// goes out, so a repaint mid-encode cannot start a second one, and what | |
| 365 | + /// comes back off the worker settles the slot. | |
| 366 | + /// | |
| 367 | + /// One body, two doors: the sweep [`Player::cache_media`] runs over | |
| 368 | + /// everything the session has not seen, and the switch on a library row | |
| 369 | + /// ([`Player::toggle_proxy`]) for the one file under the pointer. The cap on | |
| 370 | + /// how many run at once belongs to the sweep -- a person who asks for *this* | |
| 371 | + /// film's stand-in has asked, and is told by the row how far it has got. | |
| 372 | + pub(crate) fn start_proxy_for(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { | |
| 373 | + let path = path.to_path_buf(); | |
| 374 | + self.proxies.insert(path.clone(), Proxy::Asked); | |
| 375 | + let started = cx.background_executor().spawn({ | |
| 376 | + let path = path.clone(); | |
| 377 | + async move { engine::proxy::generate_if_wanted(&path) } | |
| 378 | + }); | |
| 379 | + cx.spawn(async move |this, cx| { | |
| 380 | + let started = started.await; | |
| 381 | + this.update(cx, |this, cx| { | |
| 382 | + // Switched off while the header was being read | |
| 383 | + // ([`Player::toggle_proxy`]): the slot is no longer the `Asked` | |
| 384 | + // this put there, so what came back is *stopped as it lands* | |
| 385 | + // rather than installed over the stop. Without this the second | |
| 386 | + // half of a click made during the ask is an encoder nothing in | |
| 387 | + // the map can reach -- and it finishes minutes later and puts | |
| 388 | + // back the stand-in somebody switched off. | |
| 389 | + let stopped = !matches!(this.proxies.get(&path), Some(Proxy::Asked)); | |
| 390 | + let state = match started { | |
| 391 | + // Cancelled and *held*, exactly as a stop of a running one | |
| 392 | + // is: the worker's own answer is what settles it, so an | |
| 393 | + // encode that beat the stop is reported as the finished | |
| 394 | + // stand-in it is ([`Player::poll_proxies`]). | |
| 395 | + Ok(Some(job)) if stopped => { | |
| 396 | + job.cancel(); | |
| 397 | + Proxy::Cancelling(job) | |
| 398 | + } | |
| 399 | + // ...and a stop of something that was never going to be | |
| 400 | + // encoded is simply the film itself, which is what it was. | |
| 401 | + Ok(None) | Err(_) if stopped => Proxy::Cancelled, | |
| 402 | + Ok(Some(job)) => Proxy::Making(job), | |
| 403 | + // A film that needs none keeps the state it was inserted | |
| 404 | + // with, which is what it is. | |
| 405 | + Ok(None) => Proxy::Native, | |
| 406 | + Err(e) => { | |
| 407 | + let text = format!( | |
| 408 | + "NO PROXY for {} — {e} — the film itself is what plays", | |
| 409 | + file_name(&path) | |
| 410 | + ); | |
| 411 | + eprintln!("{text}"); | |
| 412 | + this.notify_user(text.into()); | |
| 413 | + Proxy::Failed | |
| 414 | + } | |
| 415 | + }; | |
| 416 | + this.proxies.insert(path, state); | |
| 417 | + cx.notify(); | |
| 418 | + }) | |
| 419 | + .ok(); | |
| 420 | + }) | |
| 421 | + .detach(); | |
| 422 | + } | |
| 423 | + | |
| 424 | + /// The switch on a library row: this file's stand-in on, or off. One | |
| 425 | + /// control for the whole life of the thing, because a row with a separate | |
| 426 | + /// start, stop and delete is three buttons for one fact. | |
| 427 | + /// | |
| 428 | + /// * being made -- the ask is to *stop*, which is | |
| 429 | + /// [`Player::cancel_proxy`]'s half-written-file-and-all path and never a | |
| 430 | + /// deletion; | |
| 431 | + /// * asked for, and the worker has not answered yet -- the same stop, made | |
| 432 | + /// before there is a job to stop: the slot is marked and the answer is | |
| 433 | + /// cancelled as it lands ([`Player::start_proxy_for`]). Never a second | |
| 434 | + /// start, which is what two encoders on one `.part` path used to be; | |
| 435 | + /// * there is one -- the file in the cache goes ([`engine::proxy::delete`]), | |
| 436 | + /// and the slot is left [`Proxy::Cancelled`] rather than emptied: an empty | |
| 437 | + /// slot is what the next repaint's sweep starts an encode from | |
| 438 | + /// ([`proxies_to_start`] passes on everything the map has not seen), so | |
| 439 | + /// emptying it would rebuild the stand-in a second after it was turned | |
| 440 | + /// off. A delete that could not happen leaves the row *on*, because the | |
| 441 | + /// file is still there and still what plays; | |
| 442 | + /// * anything else -- start one. | |
| 443 | + /// | |
| 444 | + /// `showed_stop` is what the row was drawing when the pointer went down: | |
| 445 | + /// a stand-in that finished between that paint and this click is a click on | |
| 446 | + /// a **stop**, and a stop settles as the ready stand-in it is rather than | |
| 447 | + /// deleting a file nobody asked to lose ([`Player::cancel_proxy`]'s own | |
| 448 | + /// promise). | |
| 449 | + /// | |
| 450 | + /// The off is remembered past the window ([`engine::proxy::set_off`]): the | |
| 451 | + /// sweep runs again at every launch, and a switch that forgets is one that | |
| 452 | + /// re-encodes tomorrow what was turned off today. | |
| 453 | + pub(crate) fn toggle_proxy( | |
| 454 | + &mut self, | |
| 455 | + path: &std::path::Path, | |
| 456 | + showed_stop: bool, | |
| 457 | + cx: &mut Context<Self>, | |
| 458 | + ) { | |
| 459 | + match self.proxies.get(path) { | |
| 460 | + Some(Proxy::Making(_)) => self.cancel_proxy(path, cx), | |
| 461 | + // Already winding down: the second click on a stop is nothing. | |
| 462 | + Some(Proxy::Cancelling(_)) => {} | |
| 463 | + Some(Proxy::Asked) => { | |
| 464 | + engine::proxy::set_off(path, true); | |
| 465 | + self.proxies.insert(path.to_path_buf(), Proxy::Cancelled); | |
| 466 | + let text = format!("STOPPING the stand-in for {}…", file_name(path)); | |
| 467 | + eprintln!("{text}"); | |
| 468 | + self.notify_user(text.into()); | |
| 469 | + cx.notify(); | |
| 470 | + } | |
| 471 | + // The encode beat the click: what is under the pointer is a stop, | |
| 472 | + // and the film has a stand-in now. | |
| 473 | + Some(Proxy::Ready) if showed_stop => { | |
| 474 | + let text = format!( | |
| 475 | + "PROXY READY for {} — it finished before the stop; click again to delete it", | |
| 476 | + file_name(path) | |
| 477 | + ); | |
| 478 | + eprintln!("{text}"); | |
| 479 | + self.notify_user(text.into()); | |
| 480 | + cx.notify(); | |
| 481 | + } | |
| 482 | + Some(Proxy::Ready) => { | |
| 483 | + let text = match engine::proxy::delete(path) { | |
| 484 | + Ok(gone) => { | |
| 485 | + engine::proxy::set_off(path, true); | |
| 486 | + self.proxies.insert(path.to_path_buf(), Proxy::Cancelled); | |
| 487 | + match gone { | |
| 488 | + true => format!( | |
| 489 | + "PROXY OFF for {} — the film itself is what plays", | |
| 490 | + file_name(path) | |
| 491 | + ), | |
| 492 | + false => { | |
| 493 | + format!("NO PROXY for {} — nothing to delete", file_name(path)) | |
| 494 | + } | |
| 495 | + } | |
| 496 | + } | |
| 497 | + // The file is still on the disk and [`engine::proxy::cached`] | |
| 498 | + // is still handing it to playback, so the row stays on: the | |
| 499 | + // one thing it must never do is say off over a proxy that is | |
| 500 | + // playing. | |
| 501 | + Err(e) => format!( | |
| 502 | + "PROXY KEPT for {} — {e} — it could not be deleted and is still what plays", | |
| 503 | + file_name(path) | |
| 504 | + ), | |
| 505 | + }; | |
| 506 | + eprintln!("{text}"); | |
| 507 | + self.notify_user(text.into()); | |
| 508 | + cx.notify(); | |
| 509 | + } | |
| 510 | + _ => { | |
| 511 | + engine::proxy::set_off(path, false); | |
| 512 | + self.start_proxy_for(path, cx); | |
| 513 | + } | |
| 514 | + } | |
| 515 | + } | |
| 516 | + | |
| 363 | 517 | /// The × on a library row while its stand-in is being made: the worker is |
| 364 | 518 | /// asked to give up at its next frame and delete the half-written file it |
| 365 | 519 | /// was writing ([`engine::proxy::Job::cancel`]), and the row says so until |
| @@ -388,6 +542,9 @@ impl Player { | ||
| 388 | 542 | }; |
| 389 | 543 | job.cancel(); |
| 390 | 544 | *slot = Proxy::Cancelling(job); |
| 545 | + // Remembered past this window, for [`Player::toggle_proxy`]'s reason: | |
| 546 | + // the launch sweep would otherwise start again what was just stopped. | |
| 547 | + engine::proxy::set_off(path, true); | |
| 391 | 548 | let text = format!("STOPPING the stand-in for {}…", file_name(path)); |
| 392 | 549 | eprintln!("{text}"); |
| 393 | 550 | self.notify_user(text.into()); |
crates/app/src/ui/library.rsmodified+88 -16
| @@ -126,15 +126,55 @@ impl Player { | ||
| 126 | 126 | // that says how far it has got is where the way out of it belongs. |
| 127 | 127 | // Gone the moment the worker settles, like the percentage beside |
| 128 | 128 | // it: there is nothing left to stop. |
| 129 | - let making = matches!(self.proxies.get(&row.path), Some(Proxy::Making(_))); | |
| 129 | + // The switch, and what it is showing. One control for the whole | |
| 130 | + // life of a stand-in ([`Player::toggle_proxy`]): a filled dot for | |
| 131 | + // one that exists, a stop square while one is being made -- with | |
| 132 | + // how far it has got drawn under it -- and a hollow dot for a row | |
| 133 | + // that has none. Never an ×: that shape means the file leaves the | |
| 134 | + // library, and taking a *stand-in* off a row is not that. | |
| 135 | + let (glyph, proxy_on, progress, waiting) = match self.proxies.get(&row.path) { | |
| 136 | + Some(Proxy::Ready) => ("●", true, None, false), | |
| 137 | + Some(Proxy::Making(job)) => ("■", true, Some(job.progress()), false), | |
| 138 | + // Asked for and already winding down: the same square, dimmed, | |
| 139 | + // so a second click does not look like the first one missed. | |
| 140 | + Some(Proxy::Cancelling(_)) => ("■", true, None, true), | |
| 141 | + Some(Proxy::Asked) => ("○", false, None, true), | |
| 142 | + _ => ("○", false, None, false), | |
| 143 | + }; | |
| 144 | + // What the row is drawing right now, carried into the click: a | |
| 145 | + // stand-in that finishes between this paint and the pointer going | |
| 146 | + // down must not turn a stop into a deletion | |
| 147 | + // ([`Player::toggle_proxy`]). | |
| 148 | + let showed_stop = glyph == "■"; | |
| 130 | 149 | 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 | - ) | |
| 150 | + let stop_tip: SharedString = match self.proxies.get(&row.path) { | |
| 151 | + Some(Proxy::Making(_)) => format!( | |
| 152 | + "Stop making the stand-in for {} — nothing of it is kept, and the film itself \ | |
| 153 | + is what plays", | |
| 154 | + row.name | |
| 155 | + ), | |
| 156 | + Some(Proxy::Cancelling(_)) => { | |
| 157 | + format!("Stopping the stand-in for {}…", row.name) | |
| 158 | + } | |
| 159 | + Some(Proxy::Ready) => format!( | |
| 160 | + "Stand-in ON for {} — click to delete it; the film itself is what plays after \ | |
| 161 | + that, and nothing of the film is touched", | |
| 162 | + row.name | |
| 163 | + ), | |
| 164 | + _ => format!("Stand-in OFF for {} — click to make one", row.name), | |
| 165 | + } | |
| 136 | 166 | .into(); |
| 137 | 167 | let usable = row.unusable.is_none(); |
| 168 | + // What is left of the row for words: the switch and the gap before | |
| 169 | + // it, on the rows that carry one. Both lines are held to it -- the | |
| 170 | + // name alone was, and the detail line under it ran on under the | |
| 171 | + // switch and was cut mid-word -- and an unusable row draws no switch | |
| 172 | + // and so pays nothing for one. | |
| 173 | + let text_w = row_text_w(width) | |
| 174 | + - match usable { | |
| 175 | + true => HIT_MIN + 6., | |
| 176 | + false => 0., | |
| 177 | + }; | |
| 138 | 178 | let (path, stream) = (row.path.clone(), row.stream); |
| 139 | 179 | let dragged = (path.clone(), stream); |
| 140 | 180 | let menu_path = path.clone(); |
| @@ -221,39 +261,71 @@ impl Player { | ||
| 221 | 261 | div() |
| 222 | 262 | .truncate() |
| 223 | 263 | .text_size(px(11.)) |
| 224 | - .child(clip_middle(&name, row_text_w(width))), | |
| 264 | + // Less the stand-in switch and the gap before | |
| 265 | + // it: a media row carries one where a subtitle | |
| 266 | + // row -- built to the same `row_text_w` -- does | |
| 267 | + // not, so the difference is taken here. | |
| 268 | + .child(clip_middle(&name, text_w)), | |
| 225 | 269 | ) |
| 226 | 270 | .child( |
| 227 | 271 | div() |
| 228 | 272 | .truncate() |
| 229 | 273 | .text_size(px(10.)) |
| 230 | 274 | .text_color(rgb(FG_SECONDARY())) |
| 231 | - .child(under), | |
| 275 | + .child(clip_middle(&under, text_w)), | |
| 232 | 276 | ), |
| 233 | 277 | ) |
| 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| { | |
| 278 | + // The stand-in switch, on every row that can carry one. A | |
| 279 | + // `HIT_MIN` target that stops the click there: the row under it | |
| 280 | + // picks the file, and turning a stand-in on or off is not a way | |
| 281 | + // of choosing what to place. The selection language the rest of | |
| 282 | + // the window speaks says which way it is set -- lit like a | |
| 283 | + // picked row when there is one, dimmed like an unusable one | |
| 284 | + // when there is not. | |
| 285 | + .when(usable, |d| { | |
| 239 | 286 | d.child( |
| 240 | 287 | div() |
| 241 | - .id(("proxy-stop", i)) | |
| 288 | + .id(("proxy-toggle", i)) | |
| 242 | 289 | .flex_none() |
| 243 | 290 | .w(px(HIT_MIN)) |
| 244 | 291 | .h_full() |
| 245 | 292 | .flex() |
| 293 | + .flex_col() | |
| 246 | 294 | .items_center() |
| 247 | 295 | .justify_center() |
| 296 | + .gap(px(2.)) | |
| 248 | 297 | .rounded(px(3.)) |
| 298 | + .text_size(px(10.)) | |
| 299 | + .when(proxy_on, |d| d.bg(rgb(BG_SELECTED()))) | |
| 300 | + .when(!proxy_on, |d| d.text_color(rgb(FG_SECONDARY()))) | |
| 301 | + .when(waiting, |d| d.opacity(0.55)) | |
| 249 | 302 | .cursor_pointer() |
| 250 | 303 | .hover(|s| s.bg(rgb(BG_HOVER()))) |
| 251 | 304 | .tooltip(move |_, cx| cx.new(|_| Tip(stop_tip.clone())).into()) |
| 252 | 305 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 253 | 306 | cx.stop_propagation(); |
| 254 | - this.cancel_proxy(&stop_path, cx); | |
| 307 | + this.toggle_proxy(&stop_path, showed_stop, cx); | |
| 255 | 308 | })) |
| 256 | - .child("×"), | |
| 309 | + .child(glyph) | |
| 310 | + // How far the encode has come, under the stop it is | |
| 311 | + // stopped with: a bar rather than only the row's | |
| 312 | + // "proxy 37%", so the control reads as a job that is | |
| 313 | + // running and not as a thing that removes something. | |
| 314 | + .children(progress.map(|p| { | |
| 315 | + div() | |
| 316 | + .flex_none() | |
| 317 | + .w(px(PROXY_BAR_W)) | |
| 318 | + .h(px(2.)) | |
| 319 | + .rounded(px(1.)) | |
| 320 | + .bg(rgb(BG_RAISED())) | |
| 321 | + .child( | |
| 322 | + div() | |
| 323 | + .h_full() | |
| 324 | + .w(px(PROXY_BAR_W * p.clamp(0., 1.))) | |
| 325 | + .rounded(px(1.)) | |
| 326 | + .bg(rgb(ACCENT_PRIMARY())), | |
| 327 | + ) | |
| 328 | + })), | |
| 257 | 329 | ) |
| 258 | 330 | }) |
| 259 | 331 | }) |
crates/engine/src/proxy.rsmodified+218 -1
| @@ -223,6 +223,114 @@ pub fn cached(source: &Path) -> Option<PathBuf> { | ||
| 223 | 223 | path_for(source).filter(|p| p.is_file()) |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | +/// Deletes the stand-in of `source`, if there is one, and says whether a file | |
| 227 | +/// went. The one *by hand* eviction: [`sweep`] takes the oldest to stay under a | |
| 228 | +/// cap, this takes the one that was asked for. | |
| 229 | +/// | |
| 230 | +/// The source itself is never touched -- what is deleted is [`path_for`]'s | |
| 231 | +/// answer, a name in the cache directory and nothing else -- and deleting one | |
| 232 | +/// under a session that is playing off it costs a re-open: [`cached`] stats, | |
| 233 | +/// finds nothing, and the film itself is what plays from the next span on | |
| 234 | +/// ([`crate::PlaybackSession::picture_path`]). | |
| 235 | +/// Three answers and not two, because they are three different rows: `Ok(true)` | |
| 236 | +/// is a stand-in that went, `Ok(false)` is one that was never there (a no-op, | |
| 237 | +/// and the switch is honestly off afterwards), and `Err` is a file that is | |
| 238 | +/// **still on the disk** -- a read-only cache directory, a filesystem gone | |
| 239 | +/// away. A caller that folded the last two together drew the row as OFF over a | |
| 240 | +/// proxy [`cached`] went on handing to playback. | |
| 241 | +pub fn delete(source: &Path) -> std::io::Result<bool> { | |
| 242 | + let Some(path) = path_for(source) else { | |
| 243 | + return Ok(false); | |
| 244 | + }; | |
| 245 | + match std::fs::remove_file(&path) { | |
| 246 | + Ok(()) => { | |
| 247 | + eprintln!("proxy cache: deleted {}", path.display()); | |
| 248 | + Ok(true) | |
| 249 | + } | |
| 250 | + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(false), | |
| 251 | + Err(e) => Err(e), | |
| 252 | + } | |
| 253 | +} | |
| 254 | + | |
| 255 | +/// The mark beside a cache entry that says its film was switched *off* by hand, | |
| 256 | +/// and the one thing about a stand-in that outlives the window: without it the | |
| 257 | +/// next launch's sweep re-encodes the very proxy somebody just turned off. | |
| 258 | +/// | |
| 259 | +/// Keyed exactly as the stand-in itself is ([`path_for`]) and living beside it, | |
| 260 | +/// which is what makes it answer for a film that is in no project at all -- the | |
| 261 | +/// `.edith` file knows only the sources of one timeline, and the switch is on a | |
| 262 | +/// library row that may belong to none. Derived and disposable like everything | |
| 263 | +/// else in this directory: a re-encoded source names another key and is asked | |
| 264 | +/// again, and deleting the cache forgets the choice, which costs one click. | |
| 265 | +/// | |
| 266 | +/// corner-cut: the choice therefore does not survive a cache wipe or an edit to | |
| 267 | +/// the source file. Upgrade path is a list in the `.edith`, which needs the | |
| 268 | +/// project format to carry per-source flags. | |
| 269 | +fn off_marker(source: &Path) -> Option<PathBuf> { | |
| 270 | + Some(path_for(source)?.with_extension("off")) | |
| 271 | +} | |
| 272 | + | |
| 273 | +/// Whether this film's stand-in was switched off by hand ([`off_marker`]). | |
| 274 | +pub fn is_off(source: &Path) -> bool { | |
| 275 | + off_marker(source).is_some_and(|p| p.exists()) | |
| 276 | +} | |
| 277 | + | |
| 278 | +/// Writes or clears that mark. Best effort: a cache directory that cannot be | |
| 279 | +/// written is a choice that does not survive the session, never a refused | |
| 280 | +/// click. | |
| 281 | +pub fn set_off(source: &Path, off: bool) { | |
| 282 | + let Some(marker) = off_marker(source) else { | |
| 283 | + return; | |
| 284 | + }; | |
| 285 | + match off { | |
| 286 | + true => { | |
| 287 | + if let Some(dir) = marker.parent() { | |
| 288 | + let _ = std::fs::create_dir_all(dir); | |
| 289 | + } | |
| 290 | + let _ = std::fs::write(&marker, b""); | |
| 291 | + } | |
| 292 | + false => { | |
| 293 | + let _ = std::fs::remove_file(&marker); | |
| 294 | + } | |
| 295 | + } | |
| 296 | +} | |
| 297 | + | |
| 298 | +/// The stand-ins being written *right now*, one entry per live encoder, held | |
| 299 | +/// for the life of the [`Job`]. | |
| 300 | +/// | |
| 301 | +/// The one-encoder-per-film rule lives here rather than only in the window that | |
| 302 | +/// draws the switch: two starts for one film race over one `.part` path, and | |
| 303 | +/// the second unlinks the first's file and leaves it writing into an orphaned | |
| 304 | +/// inode with nothing left holding its handle -- an encode nobody can stop, | |
| 305 | +/// which finishes minutes later and puts back a proxy somebody had switched | |
| 306 | +/// off. A guarantee that lives in the caller is a guarantee every future caller | |
| 307 | +/// has to re-make. | |
| 308 | +static IN_FLIGHT: std::sync::Mutex<Vec<PathBuf>> = std::sync::Mutex::new(Vec::new()); | |
| 309 | + | |
| 310 | +/// A held seat in [`IN_FLIGHT`], given up when the job that owns it is dropped. | |
| 311 | +struct Claim(PathBuf); | |
| 312 | + | |
| 313 | +impl Drop for Claim { | |
| 314 | + fn drop(&mut self) { | |
| 315 | + let mut held = IN_FLIGHT.lock().unwrap_or_else(|e| e.into_inner()); | |
| 316 | + if let Some(i) = held.iter().position(|p| *p == self.0) { | |
| 317 | + held.swap_remove(i); | |
| 318 | + } | |
| 319 | + } | |
| 320 | +} | |
| 321 | + | |
| 322 | +/// Takes the seat for `out`, or `None` where an encoder already has it. | |
| 323 | +fn claim(out: &Path) -> Option<Claim> { | |
| 324 | + let mut held = IN_FLIGHT.lock().unwrap_or_else(|e| e.into_inner()); | |
| 325 | + match held.iter().any(|p| p == out) { | |
| 326 | + true => None, | |
| 327 | + false => { | |
| 328 | + held.push(out.to_path_buf()); | |
| 329 | + Some(Claim(out.to_path_buf())) | |
| 330 | + } | |
| 331 | + } | |
| 332 | +} | |
| 333 | + | |
| 226 | 334 | /// A proxy being made -- or one that was already there, which is the same thing |
| 227 | 335 | /// to whoever is waiting for a path. |
| 228 | 336 | /// |
| @@ -234,6 +342,9 @@ pub struct Job { | ||
| 234 | 342 | out: PathBuf, |
| 235 | 343 | /// `None` for a cache hit: there was nothing to run. |
| 236 | 344 | handle: Option<ExportHandle>, |
| 345 | + /// The seat this encoder holds ([`IN_FLIGHT`]), given back when the job | |
| 346 | + /// goes. `None` for a cache hit, which codes nothing. | |
| 347 | + _claim: Option<Claim>, | |
| 237 | 348 | } |
| 238 | 349 | |
| 239 | 350 | impl Job { |
| @@ -319,6 +430,13 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> { | ||
| 319 | 430 | if crate::is_audio(source) || crate::is_image(source) { |
| 320 | 431 | return Ok(None); |
| 321 | 432 | } |
| 433 | + // Switched off by hand, in this session or in one last week ([`set_off`]): | |
| 434 | + // the sweep that runs at every launch comes through here, and without this | |
| 435 | + // it re-encodes the stand-in somebody just turned off. Answered before the | |
| 436 | + // header is read, so a library of switched-off films costs no disk at all. | |
| 437 | + if is_off(source) { | |
| 438 | + return Ok(None); | |
| 439 | + } | |
| 322 | 440 | match started(source, wanted) { |
| 323 | 441 | Err(e) if NoVideoTrack::is_it(&e) => Ok(None), |
| 324 | 442 | other => other, |
| @@ -330,8 +448,16 @@ pub fn generate_if_wanted(source: &Path) -> crate::Result<Option<Job>> { | ||
| 330 | 448 | fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Option<Job>> { |
| 331 | 449 | let out = path_for(source).ok_or("no cache directory to keep a proxy in")?; |
| 332 | 450 | if out.is_file() { |
| 333 | - return Ok(Some(Job { out, handle: None })); | |
| 451 | + return Ok(Some(Job { | |
| 452 | + out, | |
| 453 | + handle: None, | |
| 454 | + _claim: None, | |
| 455 | + })); | |
| 334 | 456 | } |
| 457 | + // One encoder per film, taken before the header is read and given back when | |
| 458 | + // the job is dropped ([`IN_FLIGHT`]): the second start would unlink the | |
| 459 | + // first's `.part` below and orphan an encode nothing can stop. | |
| 460 | + let claim = claim(&out).ok_or("a stand-in for this film is already being made")?; | |
| 335 | 461 | // The source's own header: its rate and length are the proxy's (a stand-in |
| 336 | 462 | // that ran at another rate or stopped early would put every cut on another |
| 337 | 463 | // frame), and only its picture size changes. |
| @@ -402,6 +528,7 @@ fn started(source: &Path, only_if: fn(&VideoMeta) -> bool) -> crate::Result<Opti | ||
| 402 | 528 | Ok(Some(Job { |
| 403 | 529 | out, |
| 404 | 530 | handle: Some(handle), |
| 531 | + _claim: Some(claim), | |
| 405 | 532 | })) |
| 406 | 533 | } |
| 407 | 534 | |
| @@ -626,6 +753,96 @@ mod tests { | ||
| 626 | 753 | assert_eq!(cached(&file), None, "nothing written, nothing cached"); |
| 627 | 754 | } |
| 628 | 755 | |
| 756 | + /// Turning one off deletes the stand-in and *only* the stand-in: the film | |
| 757 | + /// it was made from is what plays afterwards, so it had better still be | |
| 758 | + /// there. | |
| 759 | + #[test] | |
| 760 | + fn delete_takes_the_proxy_and_never_the_source() { | |
| 761 | + let dir = crate::scratch::Scratch::dir("proxy-delete"); | |
| 762 | + let file = dir.join("a.mp4"); | |
| 763 | + std::fs::write(&file, b"the film itself").expect("write"); | |
| 764 | + let proxy = path_for(&file).expect("a key"); | |
| 765 | + std::fs::create_dir_all(proxy.parent().expect("a cache dir")).expect("cache dir"); | |
| 766 | + std::fs::write(&proxy, b"a stand-in").expect("write the proxy"); | |
| 767 | + assert_eq!(cached(&file).as_ref(), Some(&proxy), "one in the cache"); | |
| 768 | + assert_eq!(delete(&file).ok(), Some(true), "the stand-in went"); | |
| 769 | + assert_eq!(cached(&file), None, "and is gone from the cache"); | |
| 770 | + assert!(file.is_file(), "the film itself is untouched"); | |
| 771 | + assert_eq!(delete(&file).ok(), Some(false), "nothing left to take"); | |
| 772 | + assert!(file.is_file(), "still untouched"); | |
| 773 | + } | |
| 774 | + | |
| 775 | + /// A delete that *failed* is not a delete: the row above it draws itself | |
| 776 | + /// from this answer, and folding "could not" into "was not there" drew a | |
| 777 | + /// switch as OFF over a proxy still on the disk and still being played. | |
| 778 | + #[test] | |
| 779 | + fn a_delete_that_could_not_happen_says_so() { | |
| 780 | + let dir = crate::scratch::Scratch::dir("proxy-delete-refused"); | |
| 781 | + let file = dir.join("a.mp4"); | |
| 782 | + std::fs::write(&file, b"the film itself").expect("write"); | |
| 783 | + let proxy = path_for(&file).expect("a key"); | |
| 784 | + std::fs::create_dir_all(&proxy).expect("something an unlink will refuse"); | |
| 785 | + // An unlink that fails for anything but "it was not there" -- a | |
| 786 | + // read-only cache directory in the field, a directory in this test -- | |
| 787 | + // is a stand-in still on the disk, and it is answered as one. | |
| 788 | + let answer = delete(&file); | |
| 789 | + assert!(answer.is_err(), "a refused unlink reported as a delete"); | |
| 790 | + assert!(proxy.is_dir(), "and what could not be deleted is still there"); | |
| 791 | + std::fs::remove_dir(&proxy).expect("clean up"); | |
| 792 | + // ...against the honest no-op, which *is* an off switch: nothing there. | |
| 793 | + assert_eq!(delete(&file).ok(), Some(false), "nothing to take"); | |
| 794 | + assert!(file.is_file(), "the film itself, throughout"); | |
| 795 | + } | |
| 796 | + | |
| 797 | + /// The switch turned off outlives the window: the mark sits beside the | |
| 798 | + /// cache entry, so the sweep at the next launch answers `None` for that | |
| 799 | + /// film instead of re-encoding the stand-in somebody just deleted. | |
| 800 | + #[test] | |
| 801 | + fn a_film_switched_off_by_hand_stays_off() { | |
| 802 | + let dir = crate::scratch::Scratch::dir("proxy-off-mark"); | |
| 803 | + let file = dir.join("a.mp4"); | |
| 804 | + std::fs::write(&file, b"the film itself").expect("write"); | |
| 805 | + assert!(!is_off(&file), "nothing has been switched off yet"); | |
| 806 | + set_off(&file, true); | |
| 807 | + assert!(is_off(&file), "the mark is beside the cache entry"); | |
| 808 | + // Which is what the sweep asks -- and it does not even read the header. | |
| 809 | + assert!( | |
| 810 | + matches!(generate_if_wanted(&file), Ok(None)), | |
| 811 | + "a switched-off film was started again" | |
| 812 | + ); | |
| 813 | + set_off(&file, false); | |
| 814 | + assert!(!is_off(&file), "switched back on, the mark is gone"); | |
| 815 | + // The mark is keyed like the stand-in: a source that changed is a | |
| 816 | + // different film and is asked again. | |
| 817 | + set_off(&file, true); | |
| 818 | + // Taken back by hand before the key moves: the scratch directory takes | |
| 819 | + // the film with it, and a mark left in the real cache would outlive the | |
| 820 | + // test that wrote it. | |
| 821 | + let stale = off_marker(&file).expect("a key"); | |
| 822 | + std::fs::write(&file, b"re-encoded").expect("rewrite"); | |
| 823 | + filetime_bump(&file); | |
| 824 | + assert!(!is_off(&file), "a changed source kept the old answer"); | |
| 825 | + std::fs::remove_file(stale).expect("the old mark"); | |
| 826 | + } | |
| 827 | + | |
| 828 | + /// One encoder per film, in the engine and not only in the window that | |
| 829 | + /// draws the switch: the seat is taken for as long as the job lives, and a | |
| 830 | + /// second start for the same stand-in is refused rather than allowed to | |
| 831 | + /// unlink the first one's half-written file. | |
| 832 | + #[test] | |
| 833 | + fn one_encoder_per_stand_in() { | |
| 834 | + let dir = crate::scratch::Scratch::dir("proxy-one-encoder"); | |
| 835 | + let out = dir.join("f00d.mp4"); | |
| 836 | + let first = claim(&out).expect("the seat was free"); | |
| 837 | + assert!(claim(&out).is_none(), "two encoders on one path"); | |
| 838 | + // Another film is another seat. | |
| 839 | + let other = claim(&dir.join("beef.mp4")).expect("a different stand-in"); | |
| 840 | + drop(first); | |
| 841 | + let again = claim(&out).expect("the seat came back with the job"); | |
| 842 | + drop((again, other)); | |
| 843 | + assert!(claim(&out).is_some(), "the seat was never given back"); | |
| 844 | + } | |
| 845 | + | |
| 629 | 846 | /// `std::fs` has no set-mtime, and a rewrite inside the same nanosecond is |
| 630 | 847 | /// possible on a fast machine -- so the stamp is moved by hand through the |
| 631 | 848 | /// one door that does it without a dependency. |