diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 5528139..7193422 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -19,9 +19,9 @@ use engine::{Clip, ExportHandle, Frame, PlaybackSession}; use gpui::{ AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, - Point, - RenderImage, SharedString, Size, Stateful, TextAlign, TitlebarOptions, Window, WindowBounds, - WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba, size, + Point, RenderImage, SharedString, Size, Stateful, TextAlign, TitlebarOptions, Window, + WindowBounds, WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba, + size, }; /// Editor chrome: three grays and one accent, all darker than the picture so the @@ -1446,17 +1446,13 @@ impl Player { cx.notify(); return; } - let frames = self.session.as_ref().map_or(0, |session| { - source_frames(lane_clips(session), session.sources(), path) - }); - // Zero means nothing on the timeline plays from that file any more - // -- an undone import leaves the source entry behind (project.rs:264) - // -- so there is no length to insert and no file to ask for one. - let placed = match (&mut self.session, frames) { - (Some(session), 1..) => { - session.place_stream_at(session.now(), path, stream, frames, onto) - } - _ => Ok(false), + // The engine's own length for the file, noted when the import took it + // in: a row that has never been on a lane is placeable at its full + // length, which is the whole point of an import that only fills the + // library. + let placed = match &mut self.session { + Some(session) => session.place_stream_at(session.now(), path, stream, onto), + None => Ok(false), }; match placed { Ok(true) => { @@ -1467,9 +1463,7 @@ impl Player { // says which property disagrees, exactly as a refused import does. Err(e) => self.notice = Some(format!("NOTHING ADDED — {e}").into()), Ok(false) => { - self.notice = Some( - "NOTHING ADDED — no clip on the timeline plays from that file any more".into(), - ) + self.notice = Some("NOTHING ADDED — that file could not be placed here".into()) } } cx.notify(); @@ -1488,37 +1482,48 @@ impl Player { Some((info.sample_rate, info.channels)) } - /// Appends a file to the end of the timeline. A drop is not a key press, so - /// the export guard on the key handler does not cover it and this checks for - /// itself. The engine reseeks, so like a delete it owes the flag reset; a - /// refusal is shown as the engine worded it and changes nothing. + /// Takes a file into the library and nowhere else: the timeline is not + /// touched, and the row is dragged onto a lane when it is wanted there. A + /// drop is not a key press, so the export guard on the key handler does not + /// cover it and this checks for itself. Nothing moves, so nothing reseeks; + /// a refusal is shown as the engine worded it and changes nothing. fn import(&mut self, path: &std::path::Path, cx: &mut Context) { if self.exporting().is_some() { return; } - // An empty window has nothing to append to: there the first file *is* - // the timeline and is opened as one, which is the same fork a launch - // makes between its first argument and the rest. + // An empty window has no library to add to yet: the file opens one, and + // the timeline under it stays empty, because an import is an import + // whether or not a session was already up. A file *named at launch* is + // the other fork -- that one is an open, and it does become the + // timeline (`main`). let text = match self.session.as_mut().map(|session| session.import(path)) { - Some(Ok(())) => { - self.reset_after_reseek(); - format!("IMPORTED {}", file_name(path)) - } + Some(Ok(_)) => format!( + "IMPORTED {} to the library — drag it onto a lane to place it", + file_name(path) + ), Some(Err(e)) => format!("IMPORT FAILED: {e}"), - None => self.open_media(path), + None => self.open_media(path, false), }; eprintln!("{text}"); self.notice = Some(text.into()); cx.notify(); } - /// Takes a file as the whole timeline, which is what an empty window is - /// waiting for. Everything derived from the media -- the clock, the title, - /// where an export and a save go -- is set here, exactly as a launch with a - /// file argument sets it. Paused with its first frame showing, like every - /// other way a timeline arrives. - fn open_media(&mut self, path: &std::path::Path) -> String { - match PlaybackSession::open(path) { + /// Takes a file as the session an empty window is waiting for. Everything + /// derived from the media -- the clock, the title, where an export and a + /// save go -- is set here, exactly as a launch with a file argument sets + /// it. Paused with its first frame showing, like every other way a timeline + /// arrives. + /// + /// `place` is the difference between the two doors that come here: a file + /// *opened* is the timeline, one *imported* into an empty window fills the + /// library and leaves the lanes empty for a drag. + fn open_media(&mut self, path: &std::path::Path, place: bool) -> String { + let opened = match place { + true => PlaybackSession::open(path), + false => PlaybackSession::open_library(path), + }; + match opened { Ok(session) => { self.fps = session.meta().frame_rate; // Read before the session moves: a file that plays silent says @@ -1534,7 +1539,16 @@ impl Player { self.project_path = project_path(path); self.name = file_name(path).into(); self.reset_after_reseek(); - format!("OPENED {}{}", file_name(path), silent.unwrap_or_default()) + let name = file_name(path); + // The library is filled and the timeline is empty; the only + // thing that says so is this line, so it says what to do next. + let what = match place { + true => format!("OPENED {name}"), + false => { + format!("IMPORTED {name} to the library — drag it onto a lane to place it") + } + }; + format!("{what}{}", silent.unwrap_or_default()) } Err(e) => format!("OPEN FAILED: {e}"), } @@ -1553,7 +1567,7 @@ impl Player { let picked = picked.await; this.update(cx, |this, cx| match picked { // The same fork the drop handler makes: a project replaces the - // timeline, media is appended to it. + // timeline, media joins the library. Ok(Some(path)) if is_project(&path) => this.load_project(&path, cx), Ok(Some(path)) => this.import(&path, cx), // Cancelled: the user already knows what happened. @@ -2141,7 +2155,7 @@ impl Render for Player { return; } for path in paths.paths() { - // A project replaces the timeline, media is appended to it. + // A project replaces the timeline, media joins the library. if is_project(path) { this.load_project(path, cx); } else { @@ -2324,12 +2338,13 @@ impl Player { // the door (the import policy, ledger:436), so the session's own meta // describes every row and nothing has to be probed to say so. let meta = self.session.as_ref().map(PlaybackSession::meta); - let clips: Vec = self - .session - .as_ref() - .map_or_else(Vec::new, |session| lane_clips(session).copied().collect()); + // Its own length, not one derived from what is on the lanes: a row + // imported and never placed is a row with a length, and it is the + // length a drag would put down. let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| { - source_frames(clips.iter(), sources, path) + self.session + .as_ref() + .map_or(0, |session| session.file_frames(path)) }) .into_iter() .enumerate() @@ -2461,7 +2476,7 @@ impl Player { "import", None, "Import", - "opens a file chooser — or drop a file on the window".to_string(), + "adds a file to this list — or drop one on the window".to_string(), !exporting, cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)), )), @@ -2824,8 +2839,10 @@ impl Player { .id("notice") .when(exported.is_some(), |d| { d.tooltip(|_, cx| { - cx.new(|_| Tip("Open file location — shows the export in the file manager".into())) - .into() + cx.new(|_| { + Tip("Open file location — shows the export in the file manager".into()) + }) + .into() }) }) .flex_none() @@ -3581,14 +3598,7 @@ impl Player { format!("{} – {}", secs(clip.in_frame), secs(clip.out_frame)), ), ("This clip", secs(clip.len())), - ( - "Source duration", - secs(source_frames( - lane_clips(session), - session.sources(), - &source.path, - )), - ), + ("Source duration", secs(session.file_frames(&source.path))), ] { rows.push( row(rows.len()) @@ -4190,41 +4200,6 @@ fn unseen_paths(sources: &[Source], streams: &HashMap>) out } -/// Every lane's clips, which is everything the timeline knows about its sources. -/// Every lane, not the first two: a clip that plays only on `V2` is still a clip -/// the source is used by, and a walk that missed it would call the file unused. -fn lane_clips(session: &PlaybackSession) -> impl Iterator { - session - .lanes() - .into_iter() - .flat_map(|lane| session.lane_clips(lane)) -} - -/// How long a source is, as the timeline knows it: the furthest frame any clip -/// plays from *the file*. Exact for a source the timeline still holds whole -- -/// which is every source the moment it is imported -- and never longer than the -/// file, since every clip was checked against it, so a clip built from this can -/// always be played. Zero for a file nothing plays from any more. -/// -/// By file rather than by source entry, because a second audio stream of a file -/// already on the timeline is a row that has no clips of its own yet, and every -/// stream of a file is the same length -- the picture is what the length is. -/// -/// ponytail: a source trimmed on the timeline reads as its trimmed length. The -/// file's own length would need a `Demuxer::open` probe per source, off the -/// render path like the peak decode is -- that is the upgrade path. -fn source_frames<'a>( - clips: impl Iterator, - sources: &[Source], - path: &Path, -) -> u32 { - clips - .filter(|clip| sources.get(clip.source).is_some_and(|s| s.path == path)) - .map(|clip| clip.out_frame) - .max() - .unwrap_or(0) -} - /// Which timeline frame the playhead is on, by the rule the engine's own edits /// use (playback.rs `secs_to_frame`): the frame that has started, with the /// epsilon that keeps a clock sitting exactly on a boundary from reading as the @@ -5226,10 +5201,10 @@ mod tests { RULER_HIT_H, SELECTED, SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave, applicable, band_label, can_add, cancels_export, color_snap, envelope, eq_x, eq_y, export_path, export_settings, format_line, frac_along, frac_down, - frame_at, histogram, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, - menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, - show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, - whole_take, width_frac, window_title, + frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at, + mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label, + source_tint, start_frac, timecode, unseen_paths, unseen_sources, whole_take, width_frac, + window_title, }; use super::{file_name, file_uri, library_rows}; @@ -5237,7 +5212,10 @@ mod tests { /// the ones the bus would otherwise read as something else. #[test] fn file_uri_encodes_what_a_path_carries() { - assert_eq!(file_uri(std::path::Path::new("/a/b.mp4")), "file:///a/b.mp4"); + assert_eq!( + file_uri(std::path::Path::new("/a/b.mp4")), + "file:///a/b.mp4" + ); assert_eq!( file_uri(std::path::Path::new("/home/x/out dir/my export.mp4")), "file:///home/x/out%20dir/my%20export.mp4" @@ -5260,21 +5238,6 @@ mod tests { .join(name) } - /// A clip of `source`, `frames` long, at the top of the timeline. Only the - /// two fields the library reads are meant to be looked at. - fn clip(source: usize, frames: u32) -> Clip { - Clip { - start: 0, - in_frame: 0, - out_frame: frames, - source, - link: None, - eq: None, - color: None, - fit: FitPolicy::default(), - } - } - /// A source entry for `path` on `stream`, as the project keeps them. fn source(path: &str, stream: usize) -> Source { Source { @@ -5283,46 +5246,51 @@ mod tests { } } - /// The whole of the library's row data comes off the clips: which files - /// there are, how long each one is, and therefore which tint each row wears - /// -- one index into one list, so a swatch cannot name a different file from - /// the boxes it is meant to point at. + /// An import fills the *library* and nothing else -- the whole point of + /// this door: the row is there at the file's own length before any clip + /// plays it, wearing the tint the lanes will tint that clip with, and the + /// timeline is exactly as long as it was. Placing it is the drag, and the + /// drag is a separate act. #[test] - fn a_rows_swatch_and_duration_come_off_the_clips_that_name_it() { - // Source 0 whole, source 1 trimmed to half of what it was, source 2 on - // the audio lane only (its picture was lifted), source 3 imported and - // then undone -- an entry with nothing playing from it. - let sources: Vec = (0..4).map(|i| source(&format!("/m/{i}.mp4"), 0)).collect(); - let video = [clip(0, 150), clip(1, 60)]; - let audio = [clip(0, 150), clip(1, 30), clip(2, 90)]; - for (row, frames) in [(0, 150), (1, 60), (2, 90), (3, 0)] { - assert_eq!( - source_frames(video.iter().chain(&audio), &sources, &sources[row].path), - frames, - "row {row}" - ); - // The swatch is the clip colour, by the same index and the same - // function -- what makes the panel and the lanes one association. - assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]); - } - // The longest clip of a file wins, whichever lane it is in and - // whatever order the lanes are read in. - let one = [source("/m/0.mp4", 0)]; - let path = &one[0].path; + fn an_import_adds_a_row_at_its_own_length_and_leaves_the_lanes_alone() { + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let before = session.timeline_duration(); + let clips: Vec = session + .lanes() + .into_iter() + .map(|lane| session.lane_clips(lane).len()) + .collect(); + session.import(&asset("test_av2.mp4")).expect("av2 matches"); + assert_eq!(session.sources().len(), 2, "the library grew"); assert_eq!( - source_frames([clip(0, 10), clip(0, 90)].iter(), &one, path), - 90 + session.timeline_duration(), + before, + "an import must not place a clip" ); assert_eq!( - source_frames([clip(0, 90), clip(0, 10)].iter(), &one, path), - 90 + session + .lanes() + .into_iter() + .map(|lane| session.lane_clips(lane).len()) + .collect::>(), + clips, + "no lane moved" ); - assert_eq!(source_frames([].iter(), &one, path), 0); - // Two audio streams of one file are two source entries and one length: - // the second stream's row is placeable the moment the file is there, - // before any clip names that entry. - let two = [source("/m/0.mp4", 0), source("/m/0.mp4", 1)]; - assert_eq!(source_frames([clip(0, 90)].iter(), &two, &two[1].path), 90); + // The rows the panel draws: one per source, each its file's own length + // -- source 1 has no clip anywhere and is still 4 s at 30 fps. + let rows = library_rows(session.sources(), &HashMap::new(), None, |path| { + session.file_frames(path) + }); + assert_eq!(rows.len(), 2); + assert_eq!(rows[0].frames, 150, "5 s at 30 fps"); + assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed"); + // The swatch is the clip colour, by the same index and the same + // function -- what makes the panel and the lanes one association. + for row in 0..rows.len() { + assert_eq!(rows[row].tint, row); + assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]); + } } /// The window opened on a song and nothing else -- the launch argument, the @@ -5351,7 +5319,7 @@ mod tests { .iter() .find(|s| s.index == session.sources()[0].audio_stream) .map(|s| (s.sample_rate, s.channels)); - let frames = source_frames(lane_clips(&session), session.sources(), &path); + let frames = session.file_frames(&path); let rows = library_rows(session.sources(), &streams, rate, |_| frames); assert_eq!(rows.len(), 1); assert_eq!(rows[0].name, "test_tone.mp3"); @@ -5363,7 +5331,7 @@ mod tests { session.seek(1.0); assert!( session - .place_stream_at(1.0, &path, 0, frames, Some(Lane::A1)) + .place_stream_at(1.0, &path, 0, Some(Lane::A1)) .expect("its own file is on this timeline") ); assert_eq!(session.lane_clips(Lane::A1).len(), 2); @@ -5588,14 +5556,11 @@ mod tests { assert!(refusal.contains("audio"), "refusal must name it: {refusal}"); assert_eq!(session.sources().len(), 1, "a refusal added a row"); // An accepted one does add a row, and it reads as the whole file: 4 s - // at 30 fps, exactly what was appended. + // at 30 fps, its own length and not one taken off the lanes. session.import(&asset("test_av2.mp4")).expect("av2 matches"); assert_eq!(session.sources().len(), 2); let second = session.sources()[1].path.clone(); - assert_eq!( - source_frames(lane_clips(&session), session.sources(), &second), - 120 - ); + assert_eq!(session.file_frames(&second), 120); assert_eq!(timecode(120. / 30., 30.), "00:00:04:00"); } @@ -5607,21 +5572,22 @@ mod tests { let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); session.set_gain(0.0); session.import(&asset("test_av2.mp4")).expect("av2 matches"); - assert_eq!(session.timeline_duration(), 9.0); + // In the library only: the 5 s the fixture is, still. + assert_eq!(session.timeline_duration(), 5.0); // Two seconds in, which is inside the first take: the insert splits it. session.seek(2.0); let second = session.sources()[1].path.clone(); - let frames = source_frames(lane_clips(&session), session.sources(), &second); + let frames = session.file_frames(&second); // Through the engine door `insert_source` uses, with the row's own // stream: the button, the drop and this are one call. assert!( session - .place_stream_at(2.0, &second, 0, frames, None) + .place_stream_at(2.0, &second, 0, None) .expect("av2 is already on the timeline") ); // The whole of source 1 went in and nothing was painted over: the // timeline is longer by exactly that file. - assert_eq!(session.timeline_duration(), 13.0); + assert_eq!(session.timeline_duration(), 9.0); let (video, audio) = (session.lane_clips(Lane::V1), session.lane_clips(Lane::A1)); // One take, not a video clip with no sound under it: both lanes hold // the same clip at the same place, in the same group. @@ -5644,21 +5610,18 @@ mod tests { PlaybackSession::open(asset("test_multilang.mp4")).expect("open the fixture"); session.set_gain(0.0); let path = session.sources()[0].path.clone(); - let frames = source_frames(lane_clips(&session), session.sources(), &path); + let frames = session.file_frames(&path); let end = session.timeline_duration(); assert!( session - .place_stream_at(end, &path, 1, frames, None) + .place_stream_at(end, &path, 1, None) .expect("the French track shares the timeline's parameters") ); assert_eq!(session.sources()[1].audio_stream, 1); assert_eq!(session.timeline_duration(), end * 2.0); // Both rows are the same file, so both rows are that file's length -- // the second one before any clip of its own existed. - assert_eq!( - source_frames(lane_clips(&session), session.sources(), &path), - frames - ); + assert_eq!(session.file_frames(&path), frames); } /// The trim-a-clip path through the doors the edge drag uses: @@ -5701,7 +5664,11 @@ mod tests { // ...and dragged back out, as far as the file goes and no further. assert!(session.trim_clip(Lane::V1, 0, Edge::End, 9_999)); - assert_eq!(session.lane_clips(Lane::V1)[0], whole, "the whole take back"); + assert_eq!( + session.lane_clips(Lane::V1)[0], + whole, + "the whole take back" + ); // The head takes the in-point with it, so what plays at the new start // is source frame 10 rather than source frame 0. @@ -5780,10 +5747,9 @@ mod tests { // A library row let go over that row: the same door the Add button // uses, told which lane it was let go over. let path = session.sources()[0].path.clone(); - let frames = source_frames(lane_clips(&session), session.sources(), &path); assert!( session - .place_stream_at(1.0, &path, 0, frames, Some(v2)) + .place_stream_at(1.0, &path, 0, Some(v2)) .expect("its own file is on this timeline") ); assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2"); @@ -5809,7 +5775,7 @@ mod tests { assert_eq!(mp4_refusal(&session), None, "an empty lane carries nothing"); assert!( session - .place_stream_at(0.0, &path, 0, frames, Some(a2)) + .place_stream_at(0.0, &path, 0, Some(a2)) .expect("its own file is on this timeline") ); assert_eq!( @@ -6761,8 +6727,8 @@ mod tests { fn main() { let arg = std::env::args().nth(1).map(PathBuf::from); // A `.edith` restores a whole timeline, anything else *is* the timeline. - // Either way the rest of argv is appended to what comes out. No argument at - // all opens the window empty -- the timeline then arrives by drop or by the + // Either way the rest of argv is imported into its library. No argument at + // all opens the window empty -- the library then arrives by drop or by the // Import button, and everything below is derived from it at that point // instead. let mut session = match &arg { @@ -6792,8 +6758,10 @@ fn main() { if let Some(text) = ¬ice { eprintln!("{text}"); } - // The first file makes the timeline, the rest are appended to it. A refusal - // is not fatal -- the others still load -- but the window must not open + // The first file makes the timeline -- naming a file to open is a decision + // about what to watch -- and the rest are imports like any other: rows in + // the library, dragged onto a lane when they are wanted there. A refusal is + // not fatal -- the others still load -- but the window must not open // silently pretending the file was taken, so the first one seeds the notice. for extra in std::env::args().skip(2) { // Only reachable with a first argument, so there is a timeline. diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index b99fe9d..c8235af 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -216,6 +216,33 @@ impl PlaybackSession { }) } + /// Opens `path` into the **library alone**: everything + /// [`open`](Self::open) scaffolds from it -- the resolution, the frame + /// rate, the clock, the source entry and its length -- over an *empty* + /// timeline. What an import into a window that has no session yet makes, + /// since an import registers a source and never places a clip + /// ([`import`](Self::import)); the file reaches a lane when it is dragged + /// there. + /// + /// Nothing to undo, like the import it stands in for: the lanes start + /// empty rather than being emptied, so no snapshot is pushed. + pub fn open_library(path: impl AsRef) -> crate::Result { + let mut session = Self::open(path)?; + // The pair of lanes an opened file comes up with (`Project::single`, + // `open_audio_only`), emptied -- the source list and so the noted + // counts are carried over untouched, which is what keeps a library row + // placeable at its full length. + session.project = Project::from_parts( + session.project.sources().to_vec(), + vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())], + Vec::new(), + Vec::new(), + )?; + // Onto the emptied mapping: black picture, silence, zero duration. + session.seek(0.); + Ok(session) + } + /// Opens a standalone audio file ([`crate::is_audio`]) as a timeline of its /// own: one clip on `A1`, nothing on the video lane, and a picture that is /// the black of an uncovered canvas ([`AUDIO_ONLY_CANVAS`]) for as long as @@ -775,6 +802,25 @@ impl PlaybackSession { debug_assert_eq!(self.counts.len(), self.project.sources().len()); } + /// How long the whole of `path` is, in timeline frames: the length noted + /// when it became a source, so a file sitting in the library with no clip + /// anywhere still knows how much of itself there is to place. `0` for a + /// file this session has never taken in. + /// + /// By path and not by stream, because a file is one length however many + /// audio tracks it carries -- the picture's frame count for an mp4, the + /// playing time for a song. + pub fn file_frames(&self, path: &Path) -> u32 { + let at = |wanted: &Path| self.project.sources().iter().position(|s| s.path == wanted); + // A source's own path is canonical ([`Source::new`]), so a caller + // handing one back from [`sources`](Self::sources) -- which is every + // library row -- hits on the first walk and never pays for the + // `canonicalize` syscall the second one makes. + at(path) + .or_else(|| at(&Source::new(path, 0).path)) + .map_or(0, |i| self.counts[i]) + } + /// The clip the picture is coming from at `timeline_secs`: the lane it sits /// on and its index there, by the same topmost-lane-wins rule the decoder /// follows. `None` over a gap and past the end. What a card that grades @@ -880,7 +926,7 @@ impl PlaybackSession { self.edit(|p| p.paste(at, clip)) } - /// Places `frames` of `path` played on its audio `stream` at + /// Places the whole of `path` played on its audio `stream` at /// `timeline_secs`, the way [`paste_at`](Self::paste_at) places a copy -- /// the door a library row goes through, and the only way a stream other /// than the one an import brought in reaches the timeline. The `(file, @@ -908,20 +954,22 @@ impl PlaybackSession { timeline_secs: f64, path: &Path, stream: usize, - frames: u32, onto: Option, ) -> crate::Result { let wanted = Source::new(path, stream); - // Another stream of a file whose picture is *already* on the timeline: - // that is what a library row is, and it is why nothing here has to - // check dimensions or frame rate -- the file passed that at import. - if !self.project.sources().iter().any(|s| s.path == wanted.path) { + // Another stream of a file this session has taken in: that is what a + // library row is, and it is why nothing here has to check dimensions or + // frame rate -- the file passed that at import. Its length is the one + // the import noted, so a source that has never been placed is as + // placeable as one that is already on a lane. + let frames = self.file_frames(&wanted.path); + if frames == 0 { return Err(format!("{} is not on this timeline", path.display()).into()); } let first = self.first_audio()?; audio_matches(&wanted, &first)?; let source = self.project.import(path, stream); - self.note_frames(source, frames.max(1)); + self.note_frames(source, frames); let clip = Clip { start: 0, in_frame: 0, @@ -993,15 +1041,24 @@ impl PlaybackSession { self.edit(Project::undo) } - /// Appends the whole of `path` to the end of the timeline. One undo step, - /// and the file becomes a source only if it is not one already. + /// Takes `path` into the **library**: it becomes a source of this session, + /// with its length noted ([`file_frames`](Self::file_frames)), and nothing + /// is placed on any lane. What reaches the timeline is decided afterwards, + /// by dragging the row onto a lane + /// ([`place_stream_at`](Self::place_stream_at)) -- importing a file is not + /// a decision about where it plays. + /// + /// The source index, new or the one this file already had: importing twice + /// registers once. *Not* an undo step -- a source entry alone changes + /// nothing playable, so there is nothing for [`undo`](Self::undo) to take + /// back, and nothing reseeks. /// /// Refused unless it can join the timeline -- same codec, same frame rate, /// same audio parameters or both silent; the `Err` names the property that /// disagrees, for a caller to show. Nothing is changed by a refusal. A /// *resolution* of its own is not a refusal: the clip is placed on the /// project canvas by its fit policy ([`PlaybackSession::set_fit`]). - pub fn import(&mut self, path: &Path) -> crate::Result<()> { + pub fn import(&mut self, path: &Path) -> crate::Result { if crate::is_audio(path) { return self.import_audio(path); } @@ -1011,64 +1068,29 @@ impl PlaybackSession { // [`place_stream_at`](Self::place_stream_at) is how any other one of // its streams reaches the timeline afterwards. matches_timeline(&Source::new(path, 0), &meta, &self.meta, &first)?; - - let old_end = self.timeline_duration(); let source = self.project.import(path, 0); self.note_frames(source, meta.frame_count); - // Refused only for an unknown index, and this one just came from `import`. - self.project.append_clip(source, meta.frame_count); - // Reseek like any other edit, even though nothing before the playhead - // moved: the running audio worker's segment list stops at the old end, - // so without this the appended clip would play silent. At EOS the wall - // clock has run on past the timeline, so resume from the join instead - // of wherever it got to -- and the seek is what clears `eos`. - let at = if self.eos { old_end } else { self.now() }; - self.seek(at); - Ok(()) - } - - /// Appends a standalone audio file to the end of the timeline, on the - /// **audio lane alone**: a song has no picture, so there is nothing to put - /// on the video lane and the timeline shows black under it. Its length is - /// its playing time rounded up to whole frames, which is the only frame - /// count such a source has. + Ok(source) + } + + /// The same registration for a standalone audio file: a song has no + /// picture, so the length noted for it is its playing time rounded up to + /// whole frames, which is the only frame count such a source has. /// /// Refused in the same words as any other import when its rate or layout /// disagrees with the timeline's -- one output device, one set of /// parameters -- and refused outright on a silent timeline, which has no /// device open at all. Nothing is changed by a refusal. - fn import_audio(&mut self, path: &Path) -> crate::Result<()> { + fn import_audio(&mut self, path: &Path) -> crate::Result { let first = self.first_audio()?; // Stream 0, and there is no other: a standalone audio file carries one // track (`AudioSession::probe_streams`), so nothing here has a stream // to pick the way `place_stream_at` does for an mp4. audio_matches(&Source::new(path, 0), &first)?; let frames = audio_frames(path, self.meta.frame_rate)?; - - let old_end = self.timeline_duration(); let source = self.project.import(path, 0); self.note_frames(source, frames); - let at = self.project.timeline_frames(); - self.project.place( - Lane::A1, - at, - Clip { - start: at, - in_frame: 0, - out_frame: frames, - source, - link: None, - eq: None, - color: None, - fit: FitPolicy::default(), - }, - ); - // Same reason as a video import: the running audio worker's segment - // list stops at the old end, so without a reseek the appended clip - // would play silent. - let at = if self.eos { old_end } else { self.now() }; - self.seek(at); - Ok(()) + Ok(source) } /// Applies an edit and, if it took, reseeks onto the new mapping. `seek` diff --git a/crates/engine/tests/audio_files.rs b/crates/engine/tests/audio_files.rs index c68c113..9146981 100644 --- a/crates/engine/tests/audio_files.rs +++ b/crates/engine/tests/audio_files.rs @@ -32,6 +32,20 @@ fn asset(name: &str) -> PathBuf { .join(name) } +/// The two acts a file on the timeline now takes, in the order a user does +/// them: taken into the library (`PlaybackSession::import` places nothing), +/// then dragged onto the end of the timeline. +fn import_and_place(session: &mut PlaybackSession, path: &Path) { + session.import(path).expect("the file joins this timeline"); + let end = session.timeline_duration(); + assert!( + session + .place_stream_at(end, path, 0, None) + .expect("a file just imported is on this timeline"), + "the drag onto the end of the timeline" + ); +} + /// The message of a call that had to be refused. Not `expect_err`: that wants /// `Debug` on the success side, and neither a session nor a packet has it. fn refusal(result: engine::Result) -> String { @@ -119,11 +133,24 @@ fn duration_comes_from_the_header_or_the_decode() { fn a_song_joins_a_video_timeline_on_the_audio_lane() { let mut session = open(asset("test_av.mp4")); let before = session.timeline_duration(); + // The import alone is a library row: the lanes are untouched until it is + // dragged onto one. session .import(&asset("test_tone.mp3")) .expect("import the song"); + assert_eq!(session.sources().len(), 2, "the library grew"); + assert_eq!(session.lane_clips(Lane::A1).len(), 1, "an import placed it"); + assert_eq!(session.timeline_duration(), before); + assert_eq!(session.file_frames(&asset("test_tone.mp3")), 90); + let end = session.timeline_duration(); + assert!( + session + .place_stream_at(end, &asset("test_tone.mp3"), 0, None) + .expect("a song just imported is on this timeline") + ); - // 3 s at 30 fps, appended: the audio lane runs 90 frames past the video. + // 3 s at 30 fps, placed at the end: the audio lane runs 90 frames past the + // video. assert!( (session.timeline_duration() - (before + 3.0)).abs() < 0.05, "{} s of timeline, want {}", @@ -151,7 +178,7 @@ fn a_rate_the_device_cannot_mix_is_refused_at_the_door() { refusal(session.import(&asset("test_tone_48k.wav"))), "audio 48000 Hz 2 ch does not match the timeline's 44100 Hz 2 ch" ); - // Refused means unchanged: no source, no clip, no length. + // Refused means unchanged: no library row, no clip, no length. assert_eq!(session.sources().len(), 1); assert_eq!(session.lane_clips(Lane::A1).len(), 1); } @@ -228,9 +255,7 @@ fn an_audio_only_project_saves_reloads_and_exports_its_sound() { let mut session = open(asset("test_tone.mp3")); // A second song joins it, the same way one joins a timeline of video: no // picture is needed for the import to have something to hold it to. - session - .import(&asset("test_tone.flac")) - .expect("a song joins a song"); + import_and_place(&mut session, &asset("test_tone.flac")); assert_eq!(session.lane_clips(Lane::A1).len(), 2); assert!(session.lane_clips(Lane::V1).is_empty()); assert!((session.timeline_duration() - 6.0).abs() < 0.1); @@ -304,18 +329,23 @@ fn an_audio_only_project_saves_reloads_and_exports_its_sound() { #[test] fn a_video_joins_a_timeline_a_song_started() { let mut session = open(asset("test_tone.mp3")); - session - .import(&asset("test_av.mp4")) - .expect("30 fps H.264 matches the audio-only canvas"); + import_and_place(&mut session, &asset("test_av.mp4")); assert_eq!(session.lane_clips(Lane::V1).len(), 1, "a picture at last"); assert_eq!(session.lane_clips(Lane::A1).len(), 2); // And a file at another rate is refused in the timeline's own words rather - // than silently retiming the song already on the lane. + // than silently retiming the song already on the lane -- the sample rate at + // one door, the *frame* rate at the other: the canvas a song scaffolds has + // a frame rate of its own (30 fps), and a 25 fps picture cannot join it. let e = refusal(session.import(&asset("test_tone_48k.wav"))); assert_eq!( e, "audio 48000 Hz 2 ch does not match the timeline's 44100 Hz 2 ch" ); + assert_eq!( + refusal(session.import(&asset("test_25fps.mp4"))), + "25.000 fps does not match the timeline's 30.000 fps" + ); + assert_eq!(session.sources().len(), 2, "a refusal left a library row"); } /// Blocks until the export settles, whichever way it settled. @@ -362,7 +392,7 @@ fn a_song_survives_a_save_and_a_reload() { }; let mut session = open(copy("test_av.mp4")); - session.import(©("test_tone.flac")).expect("import"); + import_and_place(&mut session, ©("test_tone.flac")); // And one placed at the playhead, the way a library row dropped on the // audio lane arrives -- so the reload has both doors to restore. let song = session.lane_clips(Lane::A1)[1]; @@ -430,7 +460,7 @@ fn a_pasted_song_never_lands_on_the_video_lane() { }; let mut session = open(copy("test_av.mp4")); - session.import(©("test_tone.flac")).expect("import"); + import_and_place(&mut session, ©("test_tone.flac")); let song = session.lane_clips(Lane::A1)[1]; let video_before = session.lane_clips(Lane::V1).to_vec(); assert!(session.paste_at(0.0, song), "paste the copied song"); diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index 81c64f7..ea97884 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -626,6 +626,22 @@ fn edits_keep_the_audio_clock() { ); } +/// The two acts a second file on the timeline now takes, in the order a user +/// does them: taken into the library, then dragged onto the end of the +/// timeline. An import registers a source and places nothing +/// (`PlaybackSession::import`), so every test that wants a second source *on* +/// the lanes does both. +fn import_and_place(session: &mut PlaybackSession, path: &Path) { + session.import(path).expect("the file joins this timeline"); + let end = session.timeline_duration(); + assert!( + session + .place_stream_at(end, path, 0, None) + .expect("a file just imported is on this timeline"), + "the drag onto the end of the timeline" + ); +} + /// Frame count straight from the container, without decoding anything. fn frame_count(path: &Path) -> u32 { engine::demux::Demuxer::open(path) @@ -634,17 +650,28 @@ fn frame_count(path: &Path) -> u32 { .frame_count } -/// Import: a second file appended to the timeline, played through as one -/// stream. No audio needed for the pictures, so this runs anywhere. +/// An imported file dragged onto the end of the timeline, played through as +/// one stream. No audio needed for the pictures, so this runs anywhere. #[test] -fn import_appends_a_second_source_and_plays_across_the_join() { +fn a_placed_import_plays_across_the_join() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); let mut session = open(&av); let (fps, first) = (session.meta().frame_rate, session.meta().frame_count); let second = frame_count(&av2); assert_eq!((first, second), (150, 120), "fixtures changed"); + // The import alone fills the library: nothing on the lanes moved. session.import(&av2).expect("test_av2 matches test_av"); + assert_eq!(session.clip_spans().len(), 1, "an import placed a clip"); + assert!((session.timeline_duration() - 5.0).abs() < 1e-9); + assert_eq!(session.file_frames(&av2), second, "its own length is noted"); + // ...and the drag puts it at the end. + let end = session.timeline_duration(); + assert!( + session + .place_stream_at(end, &av2, 0, None) + .expect("a file just imported is on this timeline") + ); assert_eq!(session.clip_spans().len(), 2); assert_eq!( session @@ -653,7 +680,7 @@ fn import_appends_a_second_source_and_plays_across_the_join() { .map(|&(.., s)| s) .collect::>(), vec![0, 1], - "the appended clip plays the imported file" + "the placed clip plays the imported file" ); assert!( (session.timeline_duration() - 9.0).abs() < 1e-9, @@ -721,6 +748,46 @@ fn import_appends_a_second_source_and_plays_across_the_join() { ); } +/// The first import into a window with nothing open: the file fills the library +/// and the timeline stays *empty*, and the drag that follows is what puts it on +/// a lane -- at the file's own length, which nothing on the timeline could have +/// told the session. The whole point of an import that places nothing. +#[test] +fn the_first_import_opens_a_library_over_an_empty_timeline() { + let av = asset("test_av.mp4"); + let mut session = PlaybackSession::open_library(&av).expect("open into the library"); + session.set_gain(0.0); + assert!(session.is_empty(), "the timeline must start empty"); + assert_eq!(session.timeline_duration(), 0.0); + assert!(session.clip_spans().is_empty()); + // But it is a session: the file's own picture, rate and clock, and its row. + assert_eq!(session.sources().len(), 1, "the library row"); + assert_eq!(session.resolution(), (1280, 720)); + assert_eq!(session.meta().frame_rate, 30.0); + assert_eq!(session.file_frames(&av), 150, "5 s at 30 fps, never placed"); + assert!(!session.undo(), "opening a library is not an undo step"); + + // The drag: the row goes onto the timeline whole, and it plays from there. + assert!( + session + .place_stream_at(0.0, &av, 0, None) + .expect("its own file is on this timeline") + ); + assert!(!session.is_empty(), "the drag left the timeline empty"); + assert!((session.timeline_duration() - 5.0).abs() < 1e-9); + session.seek(0.0); + let landed = next_frame(&mut session, "the dragged clip"); + assert_eq!(landed.index, 0); + assert!( + landed.bgra == source_frame(&av, 0), + "the placed clip is not playing its file" + ); + // ...and it is an edit like any other, so one `z` takes it back to empty. + assert!(session.undo(), "undo the drag"); + assert!(session.is_empty()); + assert_eq!(session.sources().len(), 1, "the row survives the undo"); +} + /// One timeline, one set of parameters: everything else is refused by name and /// changes nothing. #[test] @@ -746,6 +813,15 @@ fn import_refuses_what_does_not_match() { "a resolution of its own is not a refusal any more: {err}" ); + // Same codec, same audio, 25 fps: the one property left, named with both + // rates. Mixing rates would mean retiming the timeline itself, and there is + // no resampler for that, so the refusal is the honest answer. + let err = session + .import(&asset("test_25fps.mp4")) + .expect_err("25 fps must not join a 30 fps timeline") + .to_string(); + assert_eq!(err, "25.000 fps does not match the timeline's 30.000 fps"); + // Same size and rate, no audio track: the timeline has one. let err = session .import(&asset("test_baseline.mp4")) @@ -758,6 +834,7 @@ fn import_refuses_what_does_not_match() { "a missing file is an error, not a panic" ); assert_eq!(session.clip_spans().len(), 1, "a refusal changes nothing"); + assert_eq!(session.sources().len(), 1, "a refusal left a library row"); assert!((session.timeline_duration() - 5.0).abs() < 1e-9); // The mirror: audio into a silent timeline is refused just as loudly. @@ -780,9 +857,7 @@ fn media_of_two_resolutions_share_one_timeline() { let mut session = open(asset("test_baseline.mp4")); assert_eq!(session.resolution(), (1280, 720), "source 0's picture"); assert_eq!(session.native_resolution(), (1280, 720)); - session - .import(&asset("test_mismatch.mp4")) - .expect("640x360 must join a 1280x720 timeline"); + import_and_place(&mut session, &asset("test_mismatch.mp4")); // 150 frames of the first file, then 60 of the second, at 30 fps. assert_eq!(session.clip_spans().len(), 2); assert!((session.timeline_duration() - 7.0).abs() < 1e-9); @@ -805,9 +880,7 @@ fn media_of_two_resolutions_share_one_timeline() { #[test] fn a_fitted_clip_is_letterboxed_and_a_filled_one_is_not() { let mut session = open(asset("test_baseline.mp4")); - session - .import(&asset("test_mismatch.mp4")) - .expect("640x360 joins"); + import_and_place(&mut session, &asset("test_mismatch.mp4")); // A 4:3 project: both clips are 16:9, so both are letterboxed on it. 960x720 // holds the whole 640x360 picture as 960x540 with 90 rows of bar either way // (fit_rect's own arithmetic, asserted in scale.rs). @@ -860,14 +933,23 @@ fn a_project_at_the_media_size_is_the_decoder_untouched() { ); } -/// The same file twice is two clips of one source -- a source index is handed -/// out once and reused, which is what keeps clipboard clips valid forever. +/// The same file twice is one library row -- a source index is handed out once +/// and reused, which is what keeps clipboard clips valid forever -- and two +/// drags of that one row are two clips of it. #[test] fn importing_one_file_twice_reuses_its_source() { let mut session = open(asset("test_av.mp4")); let av2 = asset("test_av2.mp4"); - session.import(&av2).expect("first import"); - session.import(&av2).expect("second import"); + assert_eq!(session.import(&av2).expect("first import"), 1); + assert_eq!( + session.import(&av2).expect("second import"), + 1, + "the second import is the same row" + ); + assert_eq!(session.sources().len(), 2, "one row for one file"); + import_and_place(&mut session, &av2); + import_and_place(&mut session, &av2); + assert_eq!(session.sources().len(), 2, "and still one row"); assert_eq!( session .clip_spans_by_source() @@ -884,10 +966,15 @@ fn importing_one_file_twice_reuses_its_source() { ); } -/// Importing after the timeline has been played out revives the session at the -/// join, not at wherever the free-running clock got to. +/// A library row placed after the timeline has been played out revives the +/// session: the placement reseeks like every other edit, and that is what +/// clears `eos`. The import itself changes nothing, played out or not. +/// +/// Where it resumes is not asserted, for the reason `paste_at_eos_revives_the_ +/// session` does not assert it either: an edit reseeks to `now()`, and at EOS +/// the free-running clock has gone past the end. #[test] -fn import_at_eos_resumes_into_the_new_clip() { +fn a_placement_at_eos_revives_the_session() { let av2 = asset("test_av2.mp4"); let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; @@ -897,18 +984,24 @@ fn import_at_eos_resumes_into_the_new_clip() { assert!(session.is_eos()); session.import(&av2).expect("import at EOS"); - assert!(!session.is_eos(), "the import did not revive the session"); - let landed = next_frame(&mut session, "import at EOS"); - assert_eq!(landed.index, first, "resumed at the join"); assert!( - landed.bgra == source_frame(&av2, 0), - "the resume is not showing the imported file" + session.is_eos(), + "an import is not an edit: it cannot revive" + ); + assert!( + session + .place_stream_at(session.timeline_duration(), &av2, 0, None) + .expect("a file just imported is on this timeline") + ); + assert!( + !session.is_eos(), + "the placement did not revive the session" ); assert_eq!( - drain_to_eof(&mut session).1, - Some(first + frame_count(&av2) - 1), - "the appended clip did not play out" + session.timeline_duration(), + f64::from(first + frame_count(&av2)) / session.meta().frame_rate ); + assert_eq!(session.clip_spans().len(), 2); } /// A source deleted mid-session: the clip that names it contributes no pictures @@ -920,7 +1013,7 @@ fn a_vanished_source_is_skipped() { std::fs::copy(asset("test_av2.mp4"), &scratch).expect("copy the fixture"); let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; - session.import(&scratch).expect("import the copy"); + import_and_place(&mut session, &scratch); std::fs::remove_file(&scratch).expect("unlink"); session.seek(0.0); @@ -931,9 +1024,9 @@ fn a_vanished_source_is_skipped() { assert!(session.is_eos(), "the timeline still ends"); } -/// Importing while the device is running: the import reseeks under the -/// playhead, so what must not happen is a stall -- the joined timeline still -/// plays out whole. No audio needed, so this runs anywhere. +/// Importing and placing while the device is running: the placement reseeks +/// under the playhead, so what must not happen is a stall -- the joined +/// timeline still plays out whole. No audio needed, so this runs anywhere. #[test] fn import_during_play_does_not_stall() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); @@ -945,9 +1038,9 @@ fn import_during_play_does_not_stall() { run_for(&mut session, &mut last_index, Duration::from_millis(300)); assert!(last_index.is_some(), "no frames before the import"); - session.import(&av2).expect("import while playing"); + import_and_place(&mut session, &av2); assert_eq!(session.clip_spans().len(), 2); - assert!(session.is_playing(), "the import stopped the clock"); + assert!(session.is_playing(), "the placement stopped the clock"); // From here on the indices go *backwards* once: `run_for` decodes far ahead // of the clock (ledger), and the import reseeks to `now()` -- which is why @@ -961,38 +1054,37 @@ fn import_during_play_does_not_stall() { ); } -/// An import is *one* undo step: the appended clip goes, the source entry stays -/// behind (documented, harmless -- source indexes are forever), and the session -/// is still playable on the restored timeline. +/// An import is *not* an undo step -- a library row changes nothing playable, +/// so there is nothing for `z` to take back -- and the placement that follows +/// is one. Undoing it leaves the row in the library, where the user put it. #[test] -fn undo_after_import_is_one_step() { +fn an_import_is_not_an_undo_step_but_its_placement_is() { let av2 = asset("test_av2.mp4"); let mut session = open(asset("test_av.mp4")); session.import(&av2).expect("import"); + assert!(!session.undo(), "an import must not be undoable"); + assert_eq!(session.sources().len(), 2, "and it is still in the library"); + + import_and_place(&mut session, &av2); assert_eq!(session.clip_spans().len(), 2); assert!((session.timeline_duration() - 9.0).abs() < 1e-9); - assert!(session.undo(), "undo the import"); + assert!(session.undo(), "undo the placement"); assert_eq!(session.clip_spans().len(), 1, "one step, back to one clip"); assert!( (session.timeline_duration() - 5.0).abs() < 1e-9, "undo did not restore the duration: {}", session.timeline_duration() ); - - // The orphan source is only observable on the project -- `PlaybackSession` - // exposes sources through `clip_spans_by_source`, and the undone clip is - // exactly the one that named it. - let mut project = engine::Project::single(asset("test_av.mp4"), 150); - let source = project.import(&av2, 0); - assert!(project.append_clip(source, 120)); - assert!(project.undo()); - assert_eq!(project.clips().len(), 1); - assert_eq!(project.sources().len(), 2, "the source entry stays behind"); + assert_eq!( + session.sources().len(), + 2, + "the library row survives an undo of the clip that used it" + ); // Still playable: the undo reseeked to the (paused) playhead at zero. session.play(); - assert_eq!(next_index(&mut session, "undo of an import"), 0); + assert_eq!(next_index(&mut session, "undo of a placement"), 0); } /// The audio path across a source join: one worker spans both files, so the @@ -1004,7 +1096,7 @@ fn undo_after_import_is_one_step() { #[ignore = "needs a running PipeWire daemon"] fn audio_runs_across_a_source_join() { let mut session = open(asset("test_av.mp4")); - session.import(&asset("test_av2.mp4")).expect("import"); + import_and_place(&mut session, &asset("test_av2.mp4")); let first = session.meta().frame_count; session.seek(4.5); // half a second before the join diff --git a/crates/engine/tests/saveload.rs b/crates/engine/tests/saveload.rs index eb828ab..80f3bd3 100644 --- a/crates/engine/tests/saveload.rs +++ b/crates/engine/tests/saveload.rs @@ -44,14 +44,13 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { let media = copy_in(&dir, "test_multilang.mp4"); let mut session = PlaybackSession::open(&media).expect("open the fixture"); session.set_gain(0.0); // silent like the rest of the suites - let frames = session.clip_at(0).expect("one clip").out_frame; let end = session.timeline_duration(); // Stream 1 is the French track, same rate and layout as stream 0, so it // can join this timeline; it lands as a second source of the same file. assert!( session - .place_stream_at(end, &media, 1, frames, None) + .place_stream_at(end, &media, 1, None) .expect("stream 1 matches the timeline") ); assert_eq!( @@ -83,15 +82,14 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() { let other = copy_in(&dir, "test_multiaudio.mp4"); let mut session = PlaybackSession::open(&other).expect("open the fixture"); session.set_gain(0.0); - let frames = session.clip_at(0).expect("one clip").out_frame; let err = session - .place_stream_at(0.0, &other, 1, frames, None) + .place_stream_at(0.0, &other, 1, None) .expect_err("22.05 kHz mono cannot join a 44.1 kHz stereo timeline") .to_string(); assert!(err.contains("22050"), "unhelpful refusal: {err}"); assert!( session - .place_stream_at(0.0, &media, 1, frames, None) + .place_stream_at(0.0, &media, 1, None) .expect_err("a file that is not a source") .to_string() .contains("not on this timeline") @@ -120,7 +118,9 @@ fn a_trimmed_timeline_saves_and_opens_again() { session.save_project(&path).expect("save"); let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens"); assert_eq!( - loaded.clip_at(0).map(|c| (c.start, c.in_frame, c.out_frame)), + loaded + .clip_at(0) + .map(|c| (c.start, c.in_frame, c.out_frame)), Some((5, 5, whole.end() / 2)), "the trimmed range is what came back" ); @@ -142,9 +142,17 @@ fn a_trimmed_timeline_saves_and_opens_again() { /// with something to lose in every field. fn edited(dir: &Path) -> PlaybackSession { let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open"); - session - .import(©_in(dir, "test_av2.mp4")) - .expect("import the second file"); + // Imported into the library, then dragged onto the end -- the two acts a + // second file on the timeline takes. + let second = copy_in(dir, "test_av2.mp4"); + session.import(&second).expect("import the second file"); + let end = session.timeline_duration(); + assert!( + session + .place_stream_at(end, &second, 0, None) + .expect("a file just imported is on this timeline"), + "drag it onto the end" + ); assert!(session.cut_at(2.0), "cut inside the first file"); assert!(session.cut_at(6.5), "cut inside the second file"); assert!( @@ -505,10 +513,12 @@ fn a_folder_of_media_and_its_project_can_be_moved() { fn orphan_sources_are_not_written() { let dir = scratch("orphans"); let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open"); + // An import that was never dragged onto a lane *is* the orphan: a library + // row with no clip naming it. (So is one whose clip was undone -- the same + // entry, reached the other way.) session .import(©_in(&dir, "test_av2.mp4")) .expect("import"); - assert!(session.undo(), "take the import back"); assert_eq!( session.sources().len(), 2, diff --git a/scripts/gen_fixtures.sh b/scripts/gen_fixtures.sh index 1393b08..2de9fe9 100755 --- a/scripts/gen_fixtures.sh +++ b/scripts/gen_fixtures.sh @@ -117,6 +117,16 @@ ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \ # has something concrete to refuse. ffmpeg -y -f lavfi -i testsrc2=size=640x360:rate=30:duration=2 \ -an -c:v libx264 -profile:v baseline -pix_fmt yuv420p assets/test_mismatch.mp4 +# Frame-rate fixture: test_av.mp4 in every respect a timeline is held to -- same +# codec, same profile, same 44.1k stereo AAC -- except that it runs at 25 fps. +# The only thing left for an import to refuse is the rate, which is what makes +# the refusal's wording testable. +ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=25:duration=2 \ + -f lavfi -i "sine=frequency=440:duration=2" \ + -f lavfi -i "sine=frequency=880:duration=2" \ + -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \ + -map 0:v -map "[a]" -c:v libx264 -profile:v baseline -pix_fmt yuv420p \ + -c:a aac -b:a 128k assets/test_25fps.mp4 # Standalone audio fixtures: the same 440/880 Hz tone with the 1 Hz pulse the # A/V fixture carries, at test_av.mp4's own 44.1k stereo -- so an imported song # can share a timeline with the video clips and the peaks tests can reuse the