Merge branch 'worktree-agent-ab8e2cda98faad2e0'
Browse files at this commit6 changed files +275 -81
- crates/app/src/main.rs +12 -2
- crates/engine/src/edith.rs +25 -19
- crates/engine/src/export.rs +11 -0
- crates/engine/src/playback.rs +60 -28
- crates/engine/src/project.rs +67 -32
- crates/engine/tests/playback.rs +100 -0
crates/app/src/main.rsmodified+12 -2
| @@ -988,7 +988,7 @@ impl Player { | |||
| 988 | _ => false, | 988 | _ => false, |
| 989 | }; | 989 | }; |
| 990 | if selected.is_some() && !deleted { | 990 | if selected.is_some() && !deleted { |
| 991 | - self.notice = Some("NOTHING DELETED — the timeline cannot be emptied".into()); | 991 | + self.notice = Some("NOTHING DELETED — that clip is no longer there".into()); |
| 992 | } | 992 | } |
| 993 | if deleted { | 993 | if deleted { |
| 994 | self.reset_after_reseek(); | 994 | self.reset_after_reseek(); |
| @@ -1008,7 +1008,7 @@ impl Player { | |||
| 1008 | if session.lift_clip(lane, idx) { | 1008 | if session.lift_clip(lane, idx) { |
| 1009 | self.reset_after_reseek(); | 1009 | self.reset_after_reseek(); |
| 1010 | } else { | 1010 | } else { |
| 1011 | - self.notice = Some("NOTHING LIFTED — the timeline cannot be emptied".into()); | 1011 | + self.notice = Some("NOTHING LIFTED — that half is no longer there".into()); |
| 1012 | } | 1012 | } |
| 1013 | } | 1013 | } |
| 1014 | (Some(_), None) => { | 1014 | (Some(_), None) => { |
| @@ -1609,6 +1609,16 @@ impl Player { | |||
| 1609 | cx.notify(); | 1609 | cx.notify(); |
| 1610 | return; | 1610 | return; |
| 1611 | }; | 1611 | }; |
| 1612 | + // An emptied timeline is a timeline; it is simply not a file. Refused by | ||
| 1613 | + // name here rather than written as a project of no frames -- and the | ||
| 1614 | + // engine refuses it again on the worker (`export::start`), so a caller | ||
| 1615 | + // that is not this button cannot get past it either. Two fences on | ||
| 1616 | + // purpose: this one is the one with a keystroke to blame. | ||
| 1617 | + if session.is_empty() { | ||
| 1618 | + self.notice = Some("NOTHING TO EXPORT — the timeline is empty".into()); | ||
| 1619 | + cx.notify(); | ||
| 1620 | + return; | ||
| 1621 | + } | ||
| 1612 | session.pause(); | 1622 | session.pause(); |
| 1613 | self.export = Some(session.export_to_with(&self.export_path, &settings)); | 1623 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 1614 | // The card has been answered; the progress line takes the panel from | 1624 | // The card has been answered; the progress line takes the panel from |
crates/engine/src/edith.rsmodified+25 -19
| @@ -504,9 +504,9 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> { | |||
| 504 | } | 504 | } |
| 505 | } | 505 | } |
| 506 | } | 506 | } |
| 507 | - if doc.lanes.iter().all(|(_, clips)| clips.is_empty()) { | 507 | + // A file whose lanes are all empty *is* a project -- the emptied timeline, |
| 508 | - return Err("no clips: an empty timeline is not a project".into()); | 508 | + // saved. What it may not be is laneless; [`crate::Project::from_parts`] |
| 509 | - } | 509 | + // refuses that one, by the name it has there. |
| 510 | Ok(doc) | 510 | Ok(doc) |
| 511 | } | 511 | } |
| 512 | 512 | ||
| @@ -870,13 +870,10 @@ mod tests { | |||
| 870 | ] { | 870 | ] { |
| 871 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); | 871 | assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want); |
| 872 | } | 872 | } |
| 873 | - // Lanes and nothing on them is not a project. | 873 | + // Lanes and nothing on them is the emptied timeline, saved: a project. |
| 874 | - assert_eq!( | 874 | + let empty = parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir).expect("parse"); |
| 875 | - parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir) | 875 | + assert!(empty.lanes.iter().all(|(_, clips)| clips.is_empty())); |
| 876 | - .unwrap_err() | 876 | + assert_eq!(empty.lanes.len(), 2, "and its lanes survive the round trip"); |
| 877 | - .to_string(), | ||
| 878 | - "no clips: an empty timeline is not a project" | ||
| 879 | - ); | ||
| 880 | } | 877 | } |
| 881 | 878 | ||
| 882 | /// One band, spelled the way an eq line spells it. | 879 | /// One band, spelled the way an eq line spells it. |
| @@ -1539,19 +1536,28 @@ mod tests { | |||
| 1539 | } | 1536 | } |
| 1540 | } | 1537 | } |
| 1541 | 1538 | ||
| 1539 | + /// The emptied timeline writes and reads back: every lane declared, no clip | ||
| 1540 | + /// on any of them, and the source that gave the project its frame rate still | ||
| 1541 | + /// named. Nothing about the format changes for it -- an empty lane already | ||
| 1542 | + /// had a line of its own. | ||
| 1542 | #[test] | 1543 | #[test] |
| 1543 | - fn a_project_without_clips_is_not_a_project() { | 1544 | + fn a_project_without_clips_round_trips() { |
| 1544 | let dir = PathBuf::from("/proj"); | 1545 | let dir = PathBuf::from("/proj"); |
| 1546 | + let source = Source { | ||
| 1547 | + path: PathBuf::from("/proj/a.mp4"), | ||
| 1548 | + audio_stream: 0, | ||
| 1549 | + }; | ||
| 1550 | + let bytes = flat(&dir, &[source.clone()], &two(Vec::new(), Vec::new()), 0); | ||
| 1551 | + let back = parse(&bytes, &dir).expect("an emptied timeline is a project"); | ||
| 1552 | + assert_eq!(back.sources, vec![source]); | ||
| 1553 | + assert_eq!(back.lanes, two(Vec::new(), Vec::new())); | ||
| 1554 | + assert_eq!(back.playhead, 0); | ||
| 1555 | + // Older dialects, where the two lanes are implied rather than declared. | ||
| 1545 | assert_eq!( | 1556 | assert_eq!( |
| 1546 | - parse(b"edith 1\nsource a.mp4\n", &dir) | 1557 | + parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes, |
| 1547 | - .unwrap_err() | 1558 | + two(Vec::new(), Vec::new()) |
| 1548 | - .to_string(), | ||
| 1549 | - "no clips: an empty timeline is not a project" | ||
| 1550 | - ); | ||
| 1551 | - assert_eq!( | ||
| 1552 | - parse(b"edith 1\n", &dir).unwrap_err().to_string(), | ||
| 1553 | - "no clips: an empty timeline is not a project" | ||
| 1554 | ); | 1559 | ); |
| 1560 | + assert!(parse(b"edith 1\n", &dir).expect("a project of nothing").sources.is_empty()); | ||
| 1555 | } | 1561 | } |
| 1556 | 1562 | ||
| 1557 | /// Every truncation of a good file either parses or refuses; none panics. | 1563 | /// Every truncation of a good file either parses or refuses; none panics. |
crates/engine/src/export.rsmodified+11 -0
| @@ -165,7 +165,18 @@ pub fn start( | |||
| 165 | // The rename is the last step and the only one that publishes a file | 165 | // The rename is the last step and the only one that publishes a file |
| 166 | // under the name the caller asked for; it stays on the same directory, | 166 | // under the name the caller asked for; it stays on the same directory, |
| 167 | // so it is atomic. | 167 | // so it is atomic. |
| 168 | + // | ||
| 169 | + // An emptied timeline is a legal project and an illegal file: no | ||
| 170 | + // picture, no sound, and a muxer that is never created because no coded | ||
| 171 | + // frame arrives. Refused by name here -- before the format is even | ||
| 172 | + // looked at, so an mp4 and a WAV refuse in the same words -- rather than | ||
| 173 | + // left to write a file of no frames that nothing opens. Every caller of | ||
| 174 | + // `start` comes through this, the app's own door | ||
| 175 | + // (`PlaybackSession::is_empty`) being only the first fence. | ||
| 168 | let written = match settings.format { | 176 | let written = match settings.format { |
| 177 | + _ if project.timeline_frames() == 0 => { | ||
| 178 | + Err("the timeline is empty: there is nothing to export".into()) | ||
| 179 | + } | ||
| 169 | Format::Mp4 => run(&project, &meta, &part, &worker, &settings), | 180 | Format::Mp4 => run(&project, &meta, &part, &worker, &settings), |
| 170 | format => run_audio(&project, &meta, &part, &worker, format), | 181 | format => run_audio(&project, &meta, &part, &worker, format), |
| 171 | }; | 182 | }; |
crates/engine/src/playback.rsmodified+60 -28
| @@ -128,7 +128,13 @@ pub struct PlaybackSession { | |||
| 128 | /// clip index: a `split` cuts the clip under a running worker, and only the | 128 | /// clip index: a `split` cuts the clip under a running worker, and only the |
| 129 | /// mapping survives that. A span with no source is a *gap*, and the worker | 129 | /// mapping survives that. A span with no source is a *gap*, and the worker |
| 130 | /// feeding it emits black frames indexed from zero. | 130 | /// feeding it emits black frames indexed from zero. |
| 131 | - span: Span, | 131 | + /// |
| 132 | + /// `None` is the *emptied* timeline -- no clip on any lane, so there is no | ||
| 133 | + /// stretch to be inside of. It is a state, not a failure: the picture is | ||
| 134 | + /// black ([`start_span`](Self::start_span) feeds one black frame for it), | ||
| 135 | + /// the sound is silence, the duration is zero, and placing anything reseeks | ||
| 136 | + /// straight back out of it. | ||
| 137 | + span: Option<Span>, | ||
| 132 | /// The last clip has been played out; see [`PlaybackSession::is_eos`]. | 138 | /// The last clip has been played out; see [`PlaybackSession::is_eos`]. |
| 133 | eos: bool, | 139 | eos: bool, |
| 134 | } | 140 | } |
| @@ -174,7 +180,7 @@ impl PlaybackSession { | |||
| 174 | // One clip per lane covering the file, so timeline == source until the | 180 | // One clip per lane covering the file, so timeline == source until the |
| 175 | // first edit -- and the range opened above is exactly that clip's. | 181 | // first edit -- and the range opened above is exactly that clip's. |
| 176 | let project = Project::single(&path, meta.frame_count); | 182 | let project = Project::single(&path, meta.frame_count); |
| 177 | - let span = project.composite_span_at(0).expect("never empty"); | 183 | + let span = project.composite_span_at(0); |
| 178 | Ok(Self { | 184 | Ok(Self { |
| 179 | meta, | 185 | meta, |
| 180 | native: (meta.width, meta.height), | 186 | native: (meta.width, meta.height), |
| @@ -290,7 +296,7 @@ impl PlaybackSession { | |||
| 290 | 296 | ||
| 291 | let playhead = doc.playhead; | 297 | let playhead = doc.playhead; |
| 292 | let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)?; | 298 | let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)?; |
| 293 | - let span = project.composite_span_at(0).expect("never empty"); | 299 | + let span = project.composite_span_at(0); |
| 294 | // Last, because it is the one thing here that cannot be taken back: the | 300 | // Last, because it is the one thing here that cannot be taken back: the |
| 295 | // feeder thread outlives the `Audio` value (it holds its own clones) and | 301 | // feeder thread outlives the `Audio` value (it holds its own clones) and |
| 296 | // only a session's `drop` retires it, so a refusal above this line would | 302 | // only a session's `drop` retires it, so a refusal above this line would |
| @@ -370,9 +376,12 @@ impl PlaybackSession { | |||
| 370 | match self.frames.try_recv() { | 376 | match self.frames.try_recv() { |
| 371 | Ok(mut frame) => { | 377 | Ok(mut frame) => { |
| 372 | // A gap's worker indexes from zero, a decoder's from its in | 378 | // A gap's worker indexes from zero, a decoder's from its in |
| 373 | - // point: `base` is whichever this span started at. | 379 | + // point: `base` is whichever this span started at. An empty |
| 374 | - let base = self.span.from.map_or(0, |(_, in_frame)| in_frame); | 380 | + // timeline has no span, and its one black frame is frame 0. |
| 375 | - frame.index = self.span.start + frame.index.saturating_sub(base); | 381 | + let (start, base) = self |
| 382 | + .span | ||
| 383 | + .map_or((0, 0), |s| (s.start, s.from.map_or(0, |(_, i)| i))); | ||
| 384 | + frame.index = start + frame.index.saturating_sub(base); | ||
| 376 | return Some(frame); | 385 | return Some(frame); |
| 377 | } | 386 | } |
| 378 | Err(TryRecvError::Empty) => return None, | 387 | Err(TryRecvError::Empty) => return None, |
| @@ -415,35 +424,47 @@ impl PlaybackSession { | |||
| 415 | /// than remembered as a clip index, because a `split` while playing cuts the | 424 | /// than remembered as a clip index, because a `split` while playing cuts the |
| 416 | /// clip under the running worker and only the mapping stays true. | 425 | /// clip under the running worker and only the mapping stays true. |
| 417 | fn next_clip(&mut self) -> bool { | 426 | fn next_clip(&mut self) -> bool { |
| 418 | - let next = self.span.end(); | 427 | + // No span is the emptied timeline: it was played out the moment its one |
| 428 | + // black frame went by, and there is no "next" to walk to. | ||
| 429 | + let next = match self.span { | ||
| 430 | + Some(span) => span.end(), | ||
| 431 | + None => return false, | ||
| 432 | + }; | ||
| 419 | let Some(span) = self.project.composite_span_at(next) else { | 433 | let Some(span) = self.project.composite_span_at(next) else { |
| 420 | return false; | 434 | return false; |
| 421 | }; | 435 | }; |
| 422 | // We only get here on a disconnect, so the old worker has already | 436 | // We only get here on a disconnect, so the old worker has already |
| 423 | // returned; cancel anyway, so `retire` treats every path alike. | 437 | // returned; cancel anyway, so `retire` treats every path alike. |
| 424 | self.worker.cancel(); | 438 | self.worker.cancel(); |
| 425 | - self.start_span(span); | 439 | + self.start_span(Some(span)); |
| 426 | true | 440 | true |
| 427 | } | 441 | } |
| 428 | 442 | ||
| 429 | /// Points the video worker at `span`: a decoder over its source range, or a | 443 | /// Points the video worker at `span`: a decoder over its source range, or a |
| 430 | - /// black-frame generator for a gap. The old worker must already have been | 444 | + /// black-frame generator for a gap -- and for `None`, the emptied timeline, |
| 431 | - /// cancelled -- this is the half both `seek` and `next_clip` share. | 445 | + /// which is one frame of black so the viewer shows the nothing that is |
| 446 | + /// there rather than the last picture of the clip that was deleted. The old | ||
| 447 | + /// worker must already have been cancelled -- this is the half both `seek` | ||
| 448 | + /// and `next_clip` share. | ||
| 432 | /// | 449 | /// |
| 433 | /// A source that will not open leaves the *span* installed anyway: the | 450 | /// A source that will not open leaves the *span* installed anyway: the |
| 434 | /// timeline still moves, there are simply no more pictures, and the | 451 | /// timeline still moves, there are simply no more pictures, and the |
| 435 | /// disconnected receiver carries the session on to the next span. | 452 | /// disconnected receiver carries the session on to the next span. |
| 436 | - fn start_span(&mut self, span: Span) { | 453 | + fn start_span(&mut self, span: Option<Span>) { |
| 437 | - let opened = match span.from { | 454 | + let opened = match span { |
| 438 | // The grade is the composite's at this frame -- the same clip the | 455 | // The grade is the composite's at this frame -- the same clip the |
| 439 | // span itself came from -- and it is constant across the span, so | 456 | // span itself came from -- and it is constant across the span, so |
| 440 | // the worker carries it and every frame it converts wears it. | 457 | // the worker carries it and every frame it converts wears it. |
| 441 | - Some((source, in_frame)) => DecodeSession::open_worker( | 458 | + Some(Span { |
| 459 | + start, | ||
| 460 | + len, | ||
| 461 | + from: Some((source, in_frame)), | ||
| 462 | + }) => DecodeSession::open_worker( | ||
| 442 | &self.project.sources()[source].path, | 463 | &self.project.sources()[source].path, |
| 443 | in_frame, | 464 | in_frame, |
| 444 | - in_frame + span.len, | 465 | + in_frame + len, |
| 445 | self.project | 466 | self.project |
| 446 | - .composite_color_at(span.start) | 467 | + .composite_color_at(start) |
| 447 | .copied() | 468 | .copied() |
| 448 | .unwrap_or_default(), | 469 | .unwrap_or_default(), |
| 449 | // ...and the canvas it is placed on: the project's resolution | 470 | // ...and the canvas it is placed on: the project's resolution |
| @@ -452,15 +473,18 @@ impl PlaybackSession { | |||
| 452 | Composer::new( | 473 | Composer::new( |
| 453 | self.meta.width, | 474 | self.meta.width, |
| 454 | self.meta.height, | 475 | self.meta.height, |
| 455 | - self.project.composite_fit_at(span.start), | 476 | + self.project.composite_fit_at(start), |
| 456 | ), | 477 | ), |
| 457 | ) | 478 | ) |
| 458 | .map(|(_, stream)| stream) | 479 | .map(|(_, stream)| stream) |
| 459 | - .inspect_err(|e| eprintln!("timeline frame {}: video open failed: {e}", span.start)), | 480 | + .inspect_err(|e| eprintln!("timeline frame {start}: video open failed: {e}")), |
| 460 | - None => Ok(DecodeSession::open_black( | 481 | + // A gap: black for as long as it runs. An emptied timeline has no |
| 482 | + // span at all and gets one frame of it -- enough to put black on | ||
| 483 | + // screen, and it ends where the timeline does, at once. | ||
| 484 | + gap => Ok(DecodeSession::open_black( | ||
| 461 | self.meta.width, | 485 | self.meta.width, |
| 462 | self.meta.height, | 486 | self.meta.height, |
| 463 | - span.len, | 487 | + gap.map_or(1, |s| s.len), |
| 464 | )), | 488 | )), |
| 465 | }; | 489 | }; |
| 466 | if let Ok(stream) = opened { | 490 | if let Ok(stream) = opened { |
| @@ -480,6 +504,14 @@ impl PlaybackSession { | |||
| 480 | f64::from(self.project.timeline_frames()) / self.meta.frame_rate | 504 | f64::from(self.project.timeline_frames()) / self.meta.frame_rate |
| 481 | } | 505 | } |
| 482 | 506 | ||
| 507 | + /// Whether no lane holds anything: the emptied timeline, which plays black | ||
| 508 | + /// and silent and is zero seconds long. A state, not a failure -- but the | ||
| 509 | + /// one a caller with nothing to render (an export) has to refuse by name | ||
| 510 | + /// rather than write a file of no frames. | ||
| 511 | + pub fn is_empty(&self) -> bool { | ||
| 512 | + self.project.timeline_frames() == 0 | ||
| 513 | + } | ||
| 514 | + | ||
| 483 | /// `(start, len)` per clip in timeline seconds, in order: what a clips lane | 515 | /// `(start, len)` per clip in timeline seconds, in order: what a clips lane |
| 484 | /// needs to lay itself out. Selection is the caller's business. | 516 | /// needs to lay itself out. Selection is the caller's business. |
| 485 | pub fn clip_spans(&self) -> Vec<(f64, f64)> { | 517 | pub fn clip_spans(&self) -> Vec<(f64, f64)> { |
| @@ -575,8 +607,9 @@ impl PlaybackSession { | |||
| 575 | 607 | ||
| 576 | /// Lifts one lane's clip out, leaving a gap: black frames on the video lane, | 608 | /// Lifts one lane's clip out, leaving a gap: black frames on the video lane, |
| 577 | /// silence on the audio one, and nothing else moves. Reseeks, because what | 609 | /// silence on the audio one, and nothing else moves. Reseeks, because what |
| 578 | - /// the playhead sits on has changed. `false` for a bad index and for the | 610 | + /// the playhead sits on has changed. `false` for a bad index -- the lift of |
| 579 | - /// lift that would leave the whole timeline empty. | 611 | + /// the last placement there is empties the timeline, which is a state |
| 612 | + /// ([`is_empty`](Self::is_empty)) and one undo away. | ||
| 580 | pub fn lift_clip(&mut self, lane: Lane, idx: usize) -> bool { | 613 | pub fn lift_clip(&mut self, lane: Lane, idx: usize) -> bool { |
| 581 | self.edit(|p| p.lift(lane, idx)) | 614 | self.edit(|p| p.lift(lane, idx)) |
| 582 | } | 615 | } |
| @@ -785,7 +818,8 @@ impl PlaybackSession { | |||
| 785 | /// on every lane. Unlike a split this *does* move every following frame, so | 818 | /// on every lane. Unlike a split this *does* move every following frame, so |
| 786 | /// the session reseeks to wherever the playhead now points. | 819 | /// the session reseeks to wherever the playhead now points. |
| 787 | /// [`lift_clip`](Self::lift_clip) is the one that leaves a hole instead. | 820 | /// [`lift_clip`](Self::lift_clip) is the one that leaves a hole instead. |
| 788 | - /// `false` for a bad index or the last remaining clip. | 821 | + /// `false` for a bad index; the last remaining clip goes like any other and |
| 822 | + /// leaves the timeline empty ([`is_empty`](Self::is_empty)). | ||
| 789 | /// | 823 | /// |
| 790 | /// The lane travels because the index is a lane's own: `V2`'s third clip is | 824 | /// The lane travels because the index is a lane's own: `V2`'s third clip is |
| 791 | /// not `V1`'s, and a front-end that could only say "the third clip" would | 825 | /// not `V1`'s, and a front-end that could only say "the third clip" would |
| @@ -976,12 +1010,10 @@ impl PlaybackSession { | |||
| 976 | // [`retire`](Self::retire), which is what keeps a scrub off the price | 1010 | // [`retire`](Self::retire), which is what keeps a scrub off the price |
| 977 | // of a VA-API init. | 1011 | // of a VA-API init. |
| 978 | self.worker.cancel(); | 1012 | self.worker.cancel(); |
| 979 | - // `target` is inside the timeline (never empty), so this always spans; | 1013 | + // The span runs from `target` to the end of whatever it landed in -- a |
| 980 | - // the span runs from there to the end of whatever it landed in -- a | 1014 | + // clip, or a gap, which starts a black-frame worker instead. `None` is |
| 981 | - // clip, or a gap, which starts a black-frame worker instead. | 1015 | + // the emptied timeline, black too, and `start_span` says so. |
| 982 | - if let Some(span) = self.project.composite_span_at(target) { | 1016 | + self.start_span(self.project.composite_span_at(target)); |
| 983 | - self.start_span(span); | ||
| 984 | - } | ||
| 985 | self.eos = false; | 1017 | self.eos = false; |
| 986 | 1018 | ||
| 987 | let mut audio_running = false; | 1019 | let mut audio_running = false; |
crates/engine/src/project.rsmodified+67 -32
| @@ -299,10 +299,16 @@ impl Project { | |||
| 299 | /// [`Project::undo`] is `false` until the first edit of the new session. | 299 | /// [`Project::undo`] is `false` until the first edit of the new session. |
| 300 | /// This is the one door untrusted parts come in through, so every invariant | 300 | /// This is the one door untrusted parts come in through, so every invariant |
| 301 | /// every other constructor keeps is checked here, by name and in release: | 301 | /// every other constructor keeps is checked here, by name and in release: |
| 302 | - /// every lane empty, an empty clip, a clip naming a source (or an equalizer, | 302 | + /// no lanes at all, an empty clip, a clip naming a source (or an equalizer, |
| 303 | /// or a colour) that is not there, a clip whose end overflows, a lane that is | 303 | /// or a colour) that is not there, a clip whose end overflows, a lane that is |
| 304 | /// unsorted or self-overlapping, and the grouping rules of [`Clip::link`] | 304 | /// unsorted or self-overlapping, and the grouping rules of [`Clip::link`] |
| 305 | /// below. | 305 | /// below. |
| 306 | + /// | ||
| 307 | + /// A project whose lanes are all *empty* is not among them: an emptied | ||
| 308 | + /// timeline is a project like any other -- it plays black and silent, it | ||
| 309 | + /// saves, and it loads back. What a project cannot be is laneless, because | ||
| 310 | + /// [`Project::lanes`] is what a front-end lays out and what every `Lane` | ||
| 311 | + /// handle indexes into. | ||
| 306 | pub fn from_parts( | 312 | pub fn from_parts( |
| 307 | sources: Vec<Source>, | 313 | sources: Vec<Source>, |
| 308 | lanes: Vec<(LaneKind, Vec<Clip>)>, | 314 | lanes: Vec<(LaneKind, Vec<Clip>)>, |
| @@ -315,8 +321,8 @@ impl Project { | |||
| 315 | if let Some(bad) = color.iter().position(|p| !color_finite(p)) { | 321 | if let Some(bad) = color.iter().position(|p| !color_finite(p)) { |
| 316 | return Err(format!("color {bad} holds a value that is not a finite number").into()); | 322 | return Err(format!("color {bad} holds a value that is not a finite number").into()); |
| 317 | } | 323 | } |
| 318 | - if lanes.iter().all(|(_, clips)| clips.is_empty()) { | 324 | + if lanes.is_empty() { |
| 319 | - return Err("every lane is empty: that is not a project".into()); | 325 | + return Err("no lanes at all: that is not a project".into()); |
| 320 | } | 326 | } |
| 321 | let lanes: Vec<LaneData> = lanes | 327 | let lanes: Vec<LaneData> = lanes |
| 322 | .into_iter() | 328 | .into_iter() |
| @@ -421,9 +427,8 @@ impl Project { | |||
| 421 | /// that could not be taken back would be the one edit with no way out. | 427 | /// that could not be taken back would be the one edit with no way out. |
| 422 | /// Nothing plays differently until something is placed on it. | 428 | /// Nothing plays differently until something is placed on it. |
| 423 | /// | 429 | /// |
| 424 | - /// The new lane being empty is fine even though [`Project::from_parts`] | 430 | + /// An empty lane is a lane like any other -- so is a whole project of them |
| 425 | - /// refuses an all-empty project: the lanes that were there still hold the | 431 | + /// ([`Project::from_parts`]); nothing plays until something is placed. |
| 426 | - /// timeline, and "no lane holds anything" is what that door refuses. | ||
| 427 | pub fn add_lane(&mut self, kind: LaneKind) -> Lane { | 432 | pub fn add_lane(&mut self, kind: LaneKind) -> Lane { |
| 428 | self.snapshot(); | 433 | self.snapshot(); |
| 429 | self.lanes.push(LaneData { | 434 | self.lanes.push(LaneData { |
| @@ -525,6 +530,11 @@ impl Project { | |||
| 525 | /// [`set_color`](Project::set_color) leaves settings nothing plays behind, | 530 | /// [`set_color`](Project::set_color) leaves settings nothing plays behind, |
| 526 | /// and this is the one moment they can go -- the indexes that survive it are | 531 | /// and this is the one moment they can go -- the indexes that survive it are |
| 527 | /// only the ones a clip names. | 532 | /// only the ones a clip names. |
| 533 | + /// | ||
| 534 | + /// One exception, and it is the emptied timeline's: a project no clip plays | ||
| 535 | + /// from still keeps source 0. It is the file a session is scaffolded from -- | ||
| 536 | + /// its frame rate is the timeline's and is written nowhere else -- so a save | ||
| 537 | + /// that pruned it would write a project that cannot be loaded back at all. | ||
| 528 | pub fn without_orphan_sources(&self) -> Parts { | 538 | pub fn without_orphan_sources(&self) -> Parts { |
| 529 | let mut moved = vec![None; self.sources.len()]; | 539 | let mut moved = vec![None; self.sources.len()]; |
| 530 | let mut sources = Vec::new(); | 540 | let mut sources = Vec::new(); |
| @@ -566,6 +576,9 @@ impl Project { | |||
| 566 | }); | 576 | }); |
| 567 | } | 577 | } |
| 568 | } | 578 | } |
| 579 | + if sources.is_empty() { | ||
| 580 | + sources.extend(self.sources.first().cloned()); | ||
| 581 | + } | ||
| 569 | ( | 582 | ( |
| 570 | sources, | 583 | sources, |
| 571 | lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), | 584 | lanes.into_iter().map(|l| (l.kind, l.clips)).collect(), |
| @@ -1108,13 +1121,12 @@ impl Project { | |||
| 1108 | } | 1121 | } |
| 1109 | 1122 | ||
| 1110 | /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or | 1123 | /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or |
| 1111 | - /// silence, and nothing else moves. Refused for an out-of-range index (which | 1124 | + /// silence, and nothing else moves. Refused only for an out-of-range index |
| 1112 | - /// a lane that is not there always is) and for the lift that would leave | 1125 | + /// (which a lane that is not there always is) -- lifting the last placement |
| 1113 | - /// *every* lane empty -- an empty timeline is the front-end's state, not a | 1126 | + /// there is leaves an *empty* timeline, which is a state the project holds |
| 1114 | - /// project's (see the never-empty invariant). | 1127 | + /// like any other and an undo brings back. |
| 1115 | pub fn lift(&mut self, lane: Lane, idx: usize) -> bool { | 1128 | pub fn lift(&mut self, lane: Lane, idx: usize) -> bool { |
| 1116 | - let placed: usize = self.lanes.iter().map(|l| l.clips.len()).sum(); | 1129 | + if idx >= self.lane(lane).len() { |
| 1117 | - if idx >= self.lane(lane).len() || placed == 1 { | ||
| 1118 | return false; | 1130 | return false; |
| 1119 | } | 1131 | } |
| 1120 | self.snapshot(); | 1132 | self.snapshot(); |
| @@ -1124,23 +1136,17 @@ impl Project { | |||
| 1124 | 1136 | ||
| 1125 | /// Cut the timeline frames `[at, at + len)` out of *every* lane and close | 1137 | /// Cut the timeline frames `[at, at + len)` out of *every* lane and close |
| 1126 | /// the hole: everything after slides back by `len`. The rippling delete -- | 1138 | /// the hole: everything after slides back by `len`. The rippling delete -- |
| 1127 | - /// [`lift`](Project::lift) is the one that leaves a gap. Refused for an | 1139 | + /// [`lift`](Project::lift) is the one that leaves a gap. Refused only for an |
| 1128 | - /// empty range and when it would leave every lane empty. | 1140 | + /// empty range; a delete that leaves nothing behind empties the timeline, |
| 1141 | + /// which is a state like any other and one undo away. | ||
| 1129 | pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool { | 1142 | pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool { |
| 1130 | if len == 0 { | 1143 | if len == 0 { |
| 1131 | return false; | 1144 | return false; |
| 1132 | } | 1145 | } |
| 1133 | - let survivors: usize = self | 1146 | + // Nothing reaches past `at`: there is nothing to cut and nothing to |
| 1134 | - .lanes | 1147 | + // slide back, and a delete that changes nothing must not cost an undo |
| 1135 | - .iter() | 1148 | + // step (see [`snapshot`](Project::snapshot)). |
| 1136 | - .map(|l| { | 1149 | + if !self.lanes.iter().flat_map(|l| &l.clips).any(|c| c.end() > at) { |
| 1137 | - l.clips | ||
| 1138 | - .iter() | ||
| 1139 | - .filter(|c| c.start < at || c.end() > at + len) | ||
| 1140 | - .count() | ||
| 1141 | - }) | ||
| 1142 | - .sum(); | ||
| 1143 | - if survivors == 0 { | ||
| 1144 | return false; | 1150 | return false; |
| 1145 | } | 1151 | } |
| 1146 | self.snapshot(); | 1152 | self.snapshot(); |
| @@ -1931,7 +1937,11 @@ mod tests { | |||
| 1931 | 1937 | ||
| 1932 | assert!(!p.delete(2), "index past the end"); | 1938 | assert!(!p.delete(2), "index past the end"); |
| 1933 | assert!(p.delete(1)); | 1939 | assert!(p.delete(1)); |
| 1934 | - assert!(!p.delete(0), "the last remaining clip stays"); | 1940 | + assert!(p.delete(0), "and the last remaining clip goes too"); |
| 1941 | + assert_eq!(p.clips().len(), 0); | ||
| 1942 | + assert_eq!(p.timeline_frames(), 0, "the timeline is emptiable"); | ||
| 1943 | + assert!(!p.delete(0), "there is nothing left to delete"); | ||
| 1944 | + assert!(p.undo(), "one gesture, one undo"); | ||
| 1935 | assert_eq!(p.clips().len(), 1); | 1945 | assert_eq!(p.clips().len(), 1); |
| 1936 | } | 1946 | } |
| 1937 | 1947 | ||
| @@ -1982,13 +1992,28 @@ mod tests { | |||
| 1982 | } | 1992 | } |
| 1983 | } | 1993 | } |
| 1984 | 1994 | ||
| 1995 | + /// The timeline is emptiable: the last placement comes off like any other, | ||
| 1996 | + /// the project holds "nothing on any lane" as a state, and one undo per | ||
| 1997 | + /// gesture brings it back. | ||
| 1985 | #[test] | 1998 | #[test] |
| 1986 | - fn the_last_clip_of_the_last_lane_cannot_be_lifted() { | 1999 | + fn the_last_clip_of_the_last_lane_lifts_and_undoes() { |
| 1987 | let mut p = Project::single(FILE, 9); | 2000 | let mut p = Project::single(FILE, 9); |
| 1988 | assert!(p.lift(Lane::A1, 0), "a silent timeline is fine"); | 2001 | assert!(p.lift(Lane::A1, 0), "a silent timeline is fine"); |
| 1989 | - assert!(!p.lift(Lane::V1, 0), "an empty one is not"); | 2002 | + assert!(p.lift(Lane::V1, 0), "and an empty one is a timeline too"); |
| 2003 | + assert_eq!(p.timeline_frames(), 0); | ||
| 2004 | + assert_eq!(p.composite_span_at(0), None, "nothing to show"); | ||
| 2005 | + assert!(p.audio_segments_from(0, FPS).len() == 1, "one all-gap list"); | ||
| 1990 | assert!(!p.lift(Lane::A1, 0), "index past the end"); | 2006 | assert!(!p.lift(Lane::A1, 0), "index past the end"); |
| 1991 | - assert_eq!(p.timeline_frames(), 9); | 2007 | + assert!(p.undo(), "and the last lift comes back"); |
| 2008 | + assert_eq!(p.lane_spans(Lane::V1), vec![(0, 9)]); | ||
| 2009 | + // A saved-and-loaded empty timeline is a project like any other, and it | ||
| 2010 | + // still names the file its frame rate came from. | ||
| 2011 | + assert!(p.lift(Lane::V1, 0)); | ||
| 2012 | + let (sources, lanes, eq, color) = p.without_orphan_sources(); | ||
| 2013 | + assert_eq!(sources.len(), 1, "source 0 survives an emptied timeline"); | ||
| 2014 | + let back = Project::from_parts(sources, lanes, eq, color).expect("an empty project loads"); | ||
| 2015 | + assert_eq!(back.timeline_frames(), 0); | ||
| 2016 | + assert_eq!(back.lanes().len(), 2, "and it kept its lanes"); | ||
| 1992 | } | 2017 | } |
| 1993 | 2018 | ||
| 1994 | /// What a keyboard selection walks: one answer per lane, gaps included -- | 2019 | /// What a keyboard selection walks: one answer per lane, gaps included -- |
| @@ -2121,7 +2146,16 @@ mod tests { | |||
| 2121 | assert_eq!(shape(&p)[1], shape(&p)[0]); | 2146 | assert_eq!(shape(&p)[1], shape(&p)[0]); |
| 2122 | assert_eq!(p.timeline_frames(), 5); | 2147 | assert_eq!(p.timeline_frames(), 5); |
| 2123 | assert!(!p.ripple_delete(0, 0), "an empty range is not a delete"); | 2148 | assert!(!p.ripple_delete(0, 0), "an empty range is not a delete"); |
| 2124 | - assert!(!p.ripple_delete(0, 100), "and one that empties every lane"); | 2149 | + assert!(p.undo()); |
| 2150 | + assert_eq!(shape(&p), shape(&three())); | ||
| 2151 | + // One that takes everything is a delete like any other: the timeline | ||
| 2152 | + // empties, and the undo it cost brings it back whole. | ||
| 2153 | + assert!(p.ripple_delete(0, 100), "the timeline is emptiable"); | ||
| 2154 | + assert_eq!(p.timeline_frames(), 0); | ||
| 2155 | + assert!( | ||
| 2156 | + !p.ripple_delete(0, 100), | ||
| 2157 | + "and a delete with nothing left to take is not an undo step" | ||
| 2158 | + ); | ||
| 2125 | assert!(p.undo()); | 2159 | assert!(p.undo()); |
| 2126 | assert_eq!(shape(&p), shape(&three())); | 2160 | assert_eq!(shape(&p), shape(&three())); |
| 2127 | } | 2161 | } |
| @@ -2572,7 +2606,8 @@ mod tests { | |||
| 2572 | assert!(p.split(4), "...and is editable from there"); | 2606 | assert!(p.split(4), "...and is editable from there"); |
| 2573 | assert!(p.undo()); | 2607 | assert!(p.undo()); |
| 2574 | 2608 | ||
| 2575 | - // A lane may be empty; every lane may not, and neither may no lane. | 2609 | + // A lane may be empty, and so may every lane -- an emptied timeline is a |
| 2610 | + // project. No lane at all is not one: there would be nothing to place on. | ||
| 2576 | assert!( | 2611 | assert!( |
| 2577 | Project::from_parts( | 2612 | Project::from_parts( |
| 2578 | sources.clone(), | 2613 | sources.clone(), |
| @@ -2589,7 +2624,7 @@ mod tests { | |||
| 2589 | Vec::new(), | 2624 | Vec::new(), |
| 2590 | Vec::new(), | 2625 | Vec::new(), |
| 2591 | ) | 2626 | ) |
| 2592 | - .is_err() | 2627 | + .is_ok() |
| 2593 | ); | 2628 | ); |
| 2594 | assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new(), Vec::new()).is_err()); | 2629 | assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new(), Vec::new()).is_err()); |
| 2595 | let bad: [Vec<Clip>; 5] = [ | 2630 | let bad: [Vec<Clip>; 5] = [ |
crates/engine/tests/playback.rsmodified+100 -0
| @@ -11,6 +11,7 @@ use std::path::{Path, PathBuf}; | |||
| 11 | use std::thread::sleep; | 11 | use std::thread::sleep; |
| 12 | use std::time::{Duration, Instant}; | 12 | use std::time::{Duration, Instant}; |
| 13 | 13 | ||
| 14 | +use engine::export::{ExportSettings, Format}; | ||
| 14 | use engine::project::Lane; | 15 | use engine::project::Lane; |
| 15 | use engine::{DecodeSession, Frame, PlaybackSession}; | 16 | use engine::{DecodeSession, Frame, PlaybackSession}; |
| 16 | 17 | ||
| @@ -1122,3 +1123,102 @@ fn an_audio_gap_is_silent_and_the_clock_keeps_counting() { | |||
| 1122 | ); | 1123 | ); |
| 1123 | eprintln!("audio gap: clock at {now:.3}s of {duration:.3}s, frame {last_index:?}"); | 1124 | eprintln!("audio gap: clock at {now:.3}s of {duration:.3}s, frame {last_index:?}"); |
| 1124 | } | 1125 | } |
| 1126 | + | ||
| 1127 | +/// The emptied timeline, end to end: the last clip deletes like any other, what | ||
| 1128 | +/// is left plays as black and silence with a duration of zero, it saves and | ||
| 1129 | +/// loads back as the project it is, and one undo brings the clip back. Silent | ||
| 1130 | +/// fixture, so this runs anywhere. | ||
| 1131 | +#[test] | ||
| 1132 | +fn an_emptied_timeline_plays_black_saves_loads_and_undoes() { | ||
| 1133 | + let path = asset("test_baseline.mp4"); | ||
| 1134 | + let mut session = open(&path); | ||
| 1135 | + let whole = session.timeline_duration(); | ||
| 1136 | + assert!(whole > 0.0); | ||
| 1137 | + | ||
| 1138 | + // The sole take goes -- picture and sound of it, on every lane at once. | ||
| 1139 | + assert!( | ||
| 1140 | + session.delete_clip(Lane::V1, 0), | ||
| 1141 | + "the last clip deletes like any other" | ||
| 1142 | + ); | ||
| 1143 | + assert!(session.is_empty(), "and the timeline is empty"); | ||
| 1144 | + assert_eq!(session.timeline_duration(), 0.0); | ||
| 1145 | + assert!(session.clip_spans().is_empty()); | ||
| 1146 | + assert_eq!(session.lanes().len(), 2, "the lanes are still there"); | ||
| 1147 | + | ||
| 1148 | + // It scrubs: every seek lands at zero and shows black rather than the | ||
| 1149 | + // picture of the clip that was deleted. | ||
| 1150 | + for t in [0.0, -1.0, whole, 1e9] { | ||
| 1151 | + session.seek(t); | ||
| 1152 | + assert_eq!(session.now(), 0.0, "seek to {t} on an empty timeline"); | ||
| 1153 | + let deadline = Instant::now() + Duration::from_secs(5); | ||
| 1154 | + let mut black = None; | ||
| 1155 | + while black.is_none() && Instant::now() < deadline { | ||
| 1156 | + session.tick(); | ||
| 1157 | + black = session.try_frame(); | ||
| 1158 | + sleep(Duration::from_millis(4)); | ||
| 1159 | + } | ||
| 1160 | + let frame = black.expect("an empty timeline shows one black frame"); | ||
| 1161 | + assert_eq!(frame.index, 0); | ||
| 1162 | + assert!( | ||
| 1163 | + frame.bgra.chunks_exact(4).all(|p| p[..3] == [0, 0, 0]), | ||
| 1164 | + "and it is black" | ||
| 1165 | + ); | ||
| 1166 | + } | ||
| 1167 | + // It plays: nothing to show, so it is at its end at once, and nothing hangs. | ||
| 1168 | + session.play(); | ||
| 1169 | + let mut last_index = None; | ||
| 1170 | + run_for(&mut session, &mut last_index, Duration::from_millis(200)); | ||
| 1171 | + assert!(session.is_eos(), "an empty timeline is played out at once"); | ||
| 1172 | + session.pause(); | ||
| 1173 | + | ||
| 1174 | + // Exporting nothing is a refusal in words, not a file of no frames -- and | ||
| 1175 | + // in the *same* words whichever format asked, because the fence sits in | ||
| 1176 | + // `export::start` ahead of the format, not in the mp4 path alone. | ||
| 1177 | + for format in [Format::Mp4, Format::Wav, Format::Flac] { | ||
| 1178 | + let out = std::env::temp_dir().join(format!("ve_nothing_{}", std::process::id())); | ||
| 1179 | + let handle = session.export_to_with( | ||
| 1180 | + &out, | ||
| 1181 | + &ExportSettings { | ||
| 1182 | + format, | ||
| 1183 | + ..ExportSettings::default() | ||
| 1184 | + }, | ||
| 1185 | + ); | ||
| 1186 | + let deadline = Instant::now() + Duration::from_secs(10); | ||
| 1187 | + let err = loop { | ||
| 1188 | + if let Some(result) = handle.result() { | ||
| 1189 | + break result.expect_err("an empty timeline cannot be exported"); | ||
| 1190 | + } | ||
| 1191 | + assert!(Instant::now() < deadline, "{format:?} export never settled"); | ||
| 1192 | + sleep(Duration::from_millis(10)); | ||
| 1193 | + }; | ||
| 1194 | + assert_eq!( | ||
| 1195 | + err.to_string(), | ||
| 1196 | + "the timeline is empty: there is nothing to export", | ||
| 1197 | + "{format:?}" | ||
| 1198 | + ); | ||
| 1199 | + assert!(!out.exists(), "{format:?} wrote a file anyway"); | ||
| 1200 | + } | ||
| 1201 | + | ||
| 1202 | + // It saves and loads back -- still a project, still two lanes, and still | ||
| 1203 | + // naming the file whose frame rate the timeline counts in. | ||
| 1204 | + let dir = std::env::temp_dir().join(format!("ve_empty_{}", std::process::id())); | ||
| 1205 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1206 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 1207 | + let project = dir.join("empty.edith"); | ||
| 1208 | + session | ||
| 1209 | + .save_project(&project) | ||
| 1210 | + .expect("save an empty timeline"); | ||
| 1211 | + let loaded = PlaybackSession::open_project(&project).expect("load it back"); | ||
| 1212 | + assert!(loaded.is_empty(), "what came back is the empty timeline"); | ||
| 1213 | + assert_eq!(loaded.lanes().len(), 2); | ||
| 1214 | + assert_eq!(loaded.sources().len(), 1, "source 0 is kept, orphan or not"); | ||
| 1215 | + assert_eq!(loaded.meta().frame_rate, session.meta().frame_rate); | ||
| 1216 | + drop(loaded); | ||
| 1217 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1218 | + | ||
| 1219 | + // ...and one gesture is one undo: the take comes back whole. | ||
| 1220 | + assert!(session.undo(), "the delete undoes"); | ||
| 1221 | + assert!(!session.is_empty()); | ||
| 1222 | + assert!((session.timeline_duration() - whole).abs() < 1e-9); | ||
| 1223 | + assert_eq!(session.clip_spans().len(), 1); | ||
| 1224 | +} | ||