forge
Explore
Log in

Don't you dare go Hollow

Imported files land in the library, never on the timeline

Browse files at this commit

5cb33d89e8c3b356830324c07dbec1e2a0ea3c6aAhmet Kaan GümüşParent: a3e9dc2
  • feat
    • imported files land in the library, never on the timeline
Show full message
✨ feat(import): imported files land in the library, never on the timeline

An import is not a decision about where a file plays. It registers a source,
notes its native length and stops there; the timeline changes when the user
drags the row onto a lane.

- `PlaybackSession::import`/`import_audio` probe, check and register only,
  returning the source index. No clip, no reseek, and no undo step -- a
  library row changes nothing playable, so there is nothing for `z` to take
  back, and the notice no longer pretends otherwise.
- `PlaybackSession::file_frames` is the native length of a file this session
  has taken in, so a row that has never been placed is placeable at its full
  length. `place_stream_at` takes it from there instead of a `frames`
  argument, and the app drops the clips-derived guesser that returned 0 for a
  source with no clips.
- `PlaybackSession::open_library` is the first import into an empty window:
  the file's canvas, rate and clock over an empty timeline.
- Doors: the Import button, a file dropped on the window and every argv
  argument after the first fill the library. A file *named first* at launch is
  an open, not an import, and still becomes the timeline.
- Fixture + tests: a 25 fps H.264 file, refused by name at both video doors.
crates/app/src/main.rsmodified+138 -170
@@ -19,9 +19,9 @@ use engine::{Clip, ExportHandle, Frame, PlaybackSession};
1919 use gpui::{
2020 AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle,
2121 KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels,
22- Point,
23- RenderImage, SharedString, Size, Stateful, TextAlign, TitlebarOptions, Window, WindowBounds,
24- WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba, size,
22+ Point, RenderImage, SharedString, Size, Stateful, TextAlign, TitlebarOptions, Window,
23+ WindowBounds, WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba,
24+ size,
2525 };
2626
2727 /// Editor chrome: three grays and one accent, all darker than the picture so the
@@ -1446,17 +1446,13 @@ impl Player {
14461446 cx.notify();
14471447 return;
14481448 }
1449- let frames = self.session.as_ref().map_or(0, |session| {
1450- source_frames(lane_clips(session), session.sources(), path)
1451- });
1452- // Zero means nothing on the timeline plays from that file any more
1453- // -- an undone import leaves the source entry behind (project.rs:264)
1454- // -- so there is no length to insert and no file to ask for one.
1455- let placed = match (&mut self.session, frames) {
1456- (Some(session), 1..) => {
1457- session.place_stream_at(session.now(), path, stream, frames, onto)
1458- }
1459- _ => Ok(false),
1449+ // The engine's own length for the file, noted when the import took it
1450+ // in: a row that has never been on a lane is placeable at its full
1451+ // length, which is the whole point of an import that only fills the
1452+ // library.
1453+ let placed = match &mut self.session {
1454+ Some(session) => session.place_stream_at(session.now(), path, stream, onto),
1455+ None => Ok(false),
14601456 };
14611457 match placed {
14621458 Ok(true) => {
@@ -1467,9 +1463,7 @@ impl Player {
14671463 // says which property disagrees, exactly as a refused import does.
14681464 Err(e) => self.notice = Some(format!("NOTHING ADDED — {e}").into()),
14691465 Ok(false) => {
1470- self.notice = Some(
1471- "NOTHING ADDED — no clip on the timeline plays from that file any more".into(),
1472- )
1466+ self.notice = Some("NOTHING ADDED — that file could not be placed here".into())
14731467 }
14741468 }
14751469 cx.notify();
@@ -1488,37 +1482,48 @@ impl Player {
14881482 Some((info.sample_rate, info.channels))
14891483 }
14901484
1491- /// Appends a file to the end of the timeline. A drop is not a key press, so
1492- /// the export guard on the key handler does not cover it and this checks for
1493- /// itself. The engine reseeks, so like a delete it owes the flag reset; a
1494- /// refusal is shown as the engine worded it and changes nothing.
1485+ /// Takes a file into the library and nowhere else: the timeline is not
1486+ /// touched, and the row is dragged onto a lane when it is wanted there. A
1487+ /// drop is not a key press, so the export guard on the key handler does not
1488+ /// cover it and this checks for itself. Nothing moves, so nothing reseeks;
1489+ /// a refusal is shown as the engine worded it and changes nothing.
14951490 fn import(&mut self, path: &std::path::Path, cx: &mut Context<Self>) {
14961491 if self.exporting().is_some() {
14971492 return;
14981493 }
1499- // An empty window has nothing to append to: there the first file *is*
1500- // the timeline and is opened as one, which is the same fork a launch
1501- // makes between its first argument and the rest.
1494+ // An empty window has no library to add to yet: the file opens one, and
1495+ // the timeline under it stays empty, because an import is an import
1496+ // whether or not a session was already up. A file *named at launch* is
1497+ // the other fork -- that one is an open, and it does become the
1498+ // timeline (`main`).
15021499 let text = match self.session.as_mut().map(|session| session.import(path)) {
1503- Some(Ok(())) => {
1504- self.reset_after_reseek();
1505- format!("IMPORTED {}", file_name(path))
1506- }
1500+ Some(Ok(_)) => format!(
1501+ "IMPORTED {} to the library — drag it onto a lane to place it",
1502+ file_name(path)
1503+ ),
15071504 Some(Err(e)) => format!("IMPORT FAILED: {e}"),
1508- None => self.open_media(path),
1505+ None => self.open_media(path, false),
15091506 };
15101507 eprintln!("{text}");
15111508 self.notice = Some(text.into());
15121509 cx.notify();
15131510 }
15141511
1515- /// Takes a file as the whole timeline, which is what an empty window is
1516- /// waiting for. Everything derived from the media -- the clock, the title,
1517- /// where an export and a save go -- is set here, exactly as a launch with a
1518- /// file argument sets it. Paused with its first frame showing, like every
1519- /// other way a timeline arrives.
1520- fn open_media(&mut self, path: &std::path::Path) -> String {
1521- match PlaybackSession::open(path) {
1512+ /// Takes a file as the session an empty window is waiting for. Everything
1513+ /// derived from the media -- the clock, the title, where an export and a
1514+ /// save go -- is set here, exactly as a launch with a file argument sets
1515+ /// it. Paused with its first frame showing, like every other way a timeline
1516+ /// arrives.
1517+ ///
1518+ /// `place` is the difference between the two doors that come here: a file
1519+ /// *opened* is the timeline, one *imported* into an empty window fills the
1520+ /// library and leaves the lanes empty for a drag.
1521+ fn open_media(&mut self, path: &std::path::Path, place: bool) -> String {
1522+ let opened = match place {
1523+ true => PlaybackSession::open(path),
1524+ false => PlaybackSession::open_library(path),
1525+ };
1526+ match opened {
15221527 Ok(session) => {
15231528 self.fps = session.meta().frame_rate;
15241529 // Read before the session moves: a file that plays silent says
@@ -1534,7 +1539,16 @@ impl Player {
15341539 self.project_path = project_path(path);
15351540 self.name = file_name(path).into();
15361541 self.reset_after_reseek();
1537- format!("OPENED {}{}", file_name(path), silent.unwrap_or_default())
1542+ let name = file_name(path);
1543+ // The library is filled and the timeline is empty; the only
1544+ // thing that says so is this line, so it says what to do next.
1545+ let what = match place {
1546+ true => format!("OPENED {name}"),
1547+ false => {
1548+ format!("IMPORTED {name} to the library — drag it onto a lane to place it")
1549+ }
1550+ };
1551+ format!("{what}{}", silent.unwrap_or_default())
15381552 }
15391553 Err(e) => format!("OPEN FAILED: {e}"),
15401554 }
@@ -1553,7 +1567,7 @@ impl Player {
15531567 let picked = picked.await;
15541568 this.update(cx, |this, cx| match picked {
15551569 // The same fork the drop handler makes: a project replaces the
1556- // timeline, media is appended to it.
1570+ // timeline, media joins the library.
15571571 Ok(Some(path)) if is_project(&path) => this.load_project(&path, cx),
15581572 Ok(Some(path)) => this.import(&path, cx),
15591573 // Cancelled: the user already knows what happened.
@@ -2141,7 +2155,7 @@ impl Render for Player {
21412155 return;
21422156 }
21432157 for path in paths.paths() {
2144- // A project replaces the timeline, media is appended to it.
2158+ // A project replaces the timeline, media joins the library.
21452159 if is_project(path) {
21462160 this.load_project(path, cx);
21472161 } else {
@@ -2324,12 +2338,13 @@ impl Player {
23242338 // the door (the import policy, ledger:436), so the session's own meta
23252339 // describes every row and nothing has to be probed to say so.
23262340 let meta = self.session.as_ref().map(PlaybackSession::meta);
2327- let clips: Vec<Clip> = self
2328- .session
2329- .as_ref()
2330- .map_or_else(Vec::new, |session| lane_clips(session).copied().collect());
2341+ // Its own length, not one derived from what is on the lanes: a row
2342+ // imported and never placed is a row with a length, and it is the
2343+ // length a drag would put down.
23312344 let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| {
2332- source_frames(clips.iter(), sources, path)
2345+ self.session
2346+ .as_ref()
2347+ .map_or(0, |session| session.file_frames(path))
23332348 })
23342349 .into_iter()
23352350 .enumerate()
@@ -2461,7 +2476,7 @@ impl Player {
24612476 "import",
24622477 None,
24632478 "Import",
2464- "opens a file chooser — or drop a file on the window".to_string(),
2479+ "adds a file to this list — or drop one on the window".to_string(),
24652480 !exporting,
24662481 cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)),
24672482 )),
@@ -2824,8 +2839,10 @@ impl Player {
28242839 .id("notice")
28252840 .when(exported.is_some(), |d| {
28262841 d.tooltip(|_, cx| {
2827- cx.new(|_| Tip("Open file location — shows the export in the file manager".into()))
2828- .into()
2842+ cx.new(|_| {
2843+ Tip("Open file location — shows the export in the file manager".into())
2844+ })
2845+ .into()
28292846 })
28302847 })
28312848 .flex_none()
@@ -3581,14 +3598,7 @@ impl Player {
35813598 format!("{} – {}", secs(clip.in_frame), secs(clip.out_frame)),
35823599 ),
35833600 ("This clip", secs(clip.len())),
3584- (
3585- "Source duration",
3586- secs(source_frames(
3587- lane_clips(session),
3588- session.sources(),
3589- &source.path,
3590- )),
3591- ),
3601+ ("Source duration", secs(session.file_frames(&source.path))),
35923602 ] {
35933603 rows.push(
35943604 row(rows.len())
@@ -4190,41 +4200,6 @@ fn unseen_paths(sources: &[Source], streams: &HashMap<PathBuf, Vec<StreamInfo>>)
41904200 out
41914201 }
41924202
4193-/// Every lane's clips, which is everything the timeline knows about its sources.
4194-/// Every lane, not the first two: a clip that plays only on `V2` is still a clip
4195-/// the source is used by, and a walk that missed it would call the file unused.
4196-fn lane_clips(session: &PlaybackSession) -> impl Iterator<Item = &Clip> {
4197- session
4198- .lanes()
4199- .into_iter()
4200- .flat_map(|lane| session.lane_clips(lane))
4201-}
4202-
4203-/// How long a source is, as the timeline knows it: the furthest frame any clip
4204-/// plays from *the file*. Exact for a source the timeline still holds whole --
4205-/// which is every source the moment it is imported -- and never longer than the
4206-/// file, since every clip was checked against it, so a clip built from this can
4207-/// always be played. Zero for a file nothing plays from any more.
4208-///
4209-/// By file rather than by source entry, because a second audio stream of a file
4210-/// already on the timeline is a row that has no clips of its own yet, and every
4211-/// stream of a file is the same length -- the picture is what the length is.
4212-///
4213-/// ponytail: a source trimmed on the timeline reads as its trimmed length. The
4214-/// file's own length would need a `Demuxer::open` probe per source, off the
4215-/// render path like the peak decode is -- that is the upgrade path.
4216-fn source_frames<'a>(
4217- clips: impl Iterator<Item = &'a Clip>,
4218- sources: &[Source],
4219- path: &Path,
4220-) -> u32 {
4221- clips
4222- .filter(|clip| sources.get(clip.source).is_some_and(|s| s.path == path))
4223- .map(|clip| clip.out_frame)
4224- .max()
4225- .unwrap_or(0)
4226-}
4227-
42284203 /// Which timeline frame the playhead is on, by the rule the engine's own edits
42294204 /// use (playback.rs `secs_to_frame`): the frame that has started, with the
42304205 /// epsilon that keeps a clock sitting exactly on a boundary from reading as the
@@ -5226,10 +5201,10 @@ mod tests {
52265201 RULER_HIT_H, SELECTED, SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo, Volume,
52275202 WAVE_BPS, WAVE_COL, Wave, applicable, band_label, can_add, cancels_export, color_snap,
52285203 envelope, eq_x, eq_y, export_path, export_settings, format_line, frac_along, frac_down,
5229- frame_at, histogram, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked,
5230- menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due,
5231- show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources,
5232- whole_take, width_frac, window_title,
5204+ frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at,
5205+ mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,
5206+ source_tint, start_frac, timecode, unseen_paths, unseen_sources, whole_take, width_frac,
5207+ window_title,
52335208 };
52345209 use super::{file_name, file_uri, library_rows};
52355210
@@ -5237,7 +5212,10 @@ mod tests {
52375212 /// the ones the bus would otherwise read as something else.
52385213 #[test]
52395214 fn file_uri_encodes_what_a_path_carries() {
5240- assert_eq!(file_uri(std::path::Path::new("/a/b.mp4")), "file:///a/b.mp4");
5215+ assert_eq!(
5216+ file_uri(std::path::Path::new("/a/b.mp4")),
5217+ "file:///a/b.mp4"
5218+ );
52415219 assert_eq!(
52425220 file_uri(std::path::Path::new("/home/x/out dir/my export.mp4")),
52435221 "file:///home/x/out%20dir/my%20export.mp4"
@@ -5260,21 +5238,6 @@ mod tests {
52605238 .join(name)
52615239 }
52625240
5263- /// A clip of `source`, `frames` long, at the top of the timeline. Only the
5264- /// two fields the library reads are meant to be looked at.
5265- fn clip(source: usize, frames: u32) -> Clip {
5266- Clip {
5267- start: 0,
5268- in_frame: 0,
5269- out_frame: frames,
5270- source,
5271- link: None,
5272- eq: None,
5273- color: None,
5274- fit: FitPolicy::default(),
5275- }
5276- }
5277-
52785241 /// A source entry for `path` on `stream`, as the project keeps them.
52795242 fn source(path: &str, stream: usize) -> Source {
52805243 Source {
@@ -5283,46 +5246,51 @@ mod tests {
52835246 }
52845247 }
52855248
5286- /// The whole of the library's row data comes off the clips: which files
5287- /// there are, how long each one is, and therefore which tint each row wears
5288- /// -- one index into one list, so a swatch cannot name a different file from
5289- /// the boxes it is meant to point at.
5249+ /// An import fills the *library* and nothing else -- the whole point of
5250+ /// this door: the row is there at the file's own length before any clip
5251+ /// plays it, wearing the tint the lanes will tint that clip with, and the
5252+ /// timeline is exactly as long as it was. Placing it is the drag, and the
5253+ /// drag is a separate act.
52905254 #[test]
5291- fn a_rows_swatch_and_duration_come_off_the_clips_that_name_it() {
5292- // Source 0 whole, source 1 trimmed to half of what it was, source 2 on
5293- // the audio lane only (its picture was lifted), source 3 imported and
5294- // then undone -- an entry with nothing playing from it.
5295- let sources: Vec<Source> = (0..4).map(|i| source(&format!("/m/{i}.mp4"), 0)).collect();
5296- let video = [clip(0, 150), clip(1, 60)];
5297- let audio = [clip(0, 150), clip(1, 30), clip(2, 90)];
5298- for (row, frames) in [(0, 150), (1, 60), (2, 90), (3, 0)] {
5299- assert_eq!(
5300- source_frames(video.iter().chain(&audio), &sources, &sources[row].path),
5301- frames,
5302- "row {row}"
5303- );
5304- // The swatch is the clip colour, by the same index and the same
5305- // function -- what makes the panel and the lanes one association.
5306- assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]);
5307- }
5308- // The longest clip of a file wins, whichever lane it is in and
5309- // whatever order the lanes are read in.
5310- let one = [source("/m/0.mp4", 0)];
5311- let path = &one[0].path;
5255+ fn an_import_adds_a_row_at_its_own_length_and_leaves_the_lanes_alone() {
5256+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
5257+ session.set_gain(0.0);
5258+ let before = session.timeline_duration();
5259+ let clips: Vec<usize> = session
5260+ .lanes()
5261+ .into_iter()
5262+ .map(|lane| session.lane_clips(lane).len())
5263+ .collect();
5264+ session.import(&asset("test_av2.mp4")).expect("av2 matches");
5265+ assert_eq!(session.sources().len(), 2, "the library grew");
53125266 assert_eq!(
5313- source_frames([clip(0, 10), clip(0, 90)].iter(), &one, path),
5314- 90
5267+ session.timeline_duration(),
5268+ before,
5269+ "an import must not place a clip"
53155270 );
53165271 assert_eq!(
5317- source_frames([clip(0, 90), clip(0, 10)].iter(), &one, path),
5318- 90
5272+ session
5273+ .lanes()
5274+ .into_iter()
5275+ .map(|lane| session.lane_clips(lane).len())
5276+ .collect::<Vec<_>>(),
5277+ clips,
5278+ "no lane moved"
53195279 );
5320- assert_eq!(source_frames([].iter(), &one, path), 0);
5321- // Two audio streams of one file are two source entries and one length:
5322- // the second stream's row is placeable the moment the file is there,
5323- // before any clip names that entry.
5324- let two = [source("/m/0.mp4", 0), source("/m/0.mp4", 1)];
5325- assert_eq!(source_frames([clip(0, 90)].iter(), &two, &two[1].path), 90);
5280+ // The rows the panel draws: one per source, each its file's own length
5281+ // -- source 1 has no clip anywhere and is still 4 s at 30 fps.
5282+ let rows = library_rows(session.sources(), &HashMap::new(), None, |path| {
5283+ session.file_frames(path)
5284+ });
5285+ assert_eq!(rows.len(), 2);
5286+ assert_eq!(rows[0].frames, 150, "5 s at 30 fps");
5287+ assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed");
5288+ // The swatch is the clip colour, by the same index and the same
5289+ // function -- what makes the panel and the lanes one association.
5290+ for row in 0..rows.len() {
5291+ assert_eq!(rows[row].tint, row);
5292+ assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]);
5293+ }
53265294 }
53275295
53285296 /// The window opened on a song and nothing else -- the launch argument, the
@@ -5351,7 +5319,7 @@ mod tests {
53515319 .iter()
53525320 .find(|s| s.index == session.sources()[0].audio_stream)
53535321 .map(|s| (s.sample_rate, s.channels));
5354- let frames = source_frames(lane_clips(&session), session.sources(), &path);
5322+ let frames = session.file_frames(&path);
53555323 let rows = library_rows(session.sources(), &streams, rate, |_| frames);
53565324 assert_eq!(rows.len(), 1);
53575325 assert_eq!(rows[0].name, "test_tone.mp3");
@@ -5363,7 +5331,7 @@ mod tests {
53635331 session.seek(1.0);
53645332 assert!(
53655333 session
5366- .place_stream_at(1.0, &path, 0, frames, Some(Lane::A1))
5334+ .place_stream_at(1.0, &path, 0, Some(Lane::A1))
53675335 .expect("its own file is on this timeline")
53685336 );
53695337 assert_eq!(session.lane_clips(Lane::A1).len(), 2);
@@ -5588,14 +5556,11 @@ mod tests {
55885556 assert!(refusal.contains("audio"), "refusal must name it: {refusal}");
55895557 assert_eq!(session.sources().len(), 1, "a refusal added a row");
55905558 // An accepted one does add a row, and it reads as the whole file: 4 s
5591- // at 30 fps, exactly what was appended.
5559+ // at 30 fps, its own length and not one taken off the lanes.
55925560 session.import(&asset("test_av2.mp4")).expect("av2 matches");
55935561 assert_eq!(session.sources().len(), 2);
55945562 let second = session.sources()[1].path.clone();
5595- assert_eq!(
5596- source_frames(lane_clips(&session), session.sources(), &second),
5597- 120
5598- );
5563+ assert_eq!(session.file_frames(&second), 120);
55995564 assert_eq!(timecode(120. / 30., 30.), "00:00:04:00");
56005565 }
56015566
@@ -5607,21 +5572,22 @@ mod tests {
56075572 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
56085573 session.set_gain(0.0);
56095574 session.import(&asset("test_av2.mp4")).expect("av2 matches");
5610- assert_eq!(session.timeline_duration(), 9.0);
5575+ // In the library only: the 5 s the fixture is, still.
5576+ assert_eq!(session.timeline_duration(), 5.0);
56115577 // Two seconds in, which is inside the first take: the insert splits it.
56125578 session.seek(2.0);
56135579 let second = session.sources()[1].path.clone();
5614- let frames = source_frames(lane_clips(&session), session.sources(), &second);
5580+ let frames = session.file_frames(&second);
56155581 // Through the engine door `insert_source` uses, with the row's own
56165582 // stream: the button, the drop and this are one call.
56175583 assert!(
56185584 session
5619- .place_stream_at(2.0, &second, 0, frames, None)
5585+ .place_stream_at(2.0, &second, 0, None)
56205586 .expect("av2 is already on the timeline")
56215587 );
56225588 // The whole of source 1 went in and nothing was painted over: the
56235589 // timeline is longer by exactly that file.
5624- assert_eq!(session.timeline_duration(), 13.0);
5590+ assert_eq!(session.timeline_duration(), 9.0);
56255591 let (video, audio) = (session.lane_clips(Lane::V1), session.lane_clips(Lane::A1));
56265592 // One take, not a video clip with no sound under it: both lanes hold
56275593 // the same clip at the same place, in the same group.
@@ -5644,21 +5610,18 @@ mod tests {
56445610 PlaybackSession::open(asset("test_multilang.mp4")).expect("open the fixture");
56455611 session.set_gain(0.0);
56465612 let path = session.sources()[0].path.clone();
5647- let frames = source_frames(lane_clips(&session), session.sources(), &path);
5613+ let frames = session.file_frames(&path);
56485614 let end = session.timeline_duration();
56495615 assert!(
56505616 session
5651- .place_stream_at(end, &path, 1, frames, None)
5617+ .place_stream_at(end, &path, 1, None)
56525618 .expect("the French track shares the timeline's parameters")
56535619 );
56545620 assert_eq!(session.sources()[1].audio_stream, 1);
56555621 assert_eq!(session.timeline_duration(), end * 2.0);
56565622 // Both rows are the same file, so both rows are that file's length --
56575623 // the second one before any clip of its own existed.
5658- assert_eq!(
5659- source_frames(lane_clips(&session), session.sources(), &path),
5660- frames
5661- );
5624+ assert_eq!(session.file_frames(&path), frames);
56625625 }
56635626
56645627 /// The trim-a-clip path through the doors the edge drag uses:
@@ -5701,7 +5664,11 @@ mod tests {
57015664
57025665 // ...and dragged back out, as far as the file goes and no further.
57035666 assert!(session.trim_clip(Lane::V1, 0, Edge::End, 9_999));
5704- assert_eq!(session.lane_clips(Lane::V1)[0], whole, "the whole take back");
5667+ assert_eq!(
5668+ session.lane_clips(Lane::V1)[0],
5669+ whole,
5670+ "the whole take back"
5671+ );
57055672
57065673 // The head takes the in-point with it, so what plays at the new start
57075674 // is source frame 10 rather than source frame 0.
@@ -5780,10 +5747,9 @@ mod tests {
57805747 // A library row let go over that row: the same door the Add button
57815748 // uses, told which lane it was let go over.
57825749 let path = session.sources()[0].path.clone();
5783- let frames = source_frames(lane_clips(&session), session.sources(), &path);
57845750 assert!(
57855751 session
5786- .place_stream_at(1.0, &path, 0, frames, Some(v2))
5752+ .place_stream_at(1.0, &path, 0, Some(v2))
57875753 .expect("its own file is on this timeline")
57885754 );
57895755 assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2");
@@ -5809,7 +5775,7 @@ mod tests {
58095775 assert_eq!(mp4_refusal(&session), None, "an empty lane carries nothing");
58105776 assert!(
58115777 session
5812- .place_stream_at(0.0, &path, 0, frames, Some(a2))
5778+ .place_stream_at(0.0, &path, 0, Some(a2))
58135779 .expect("its own file is on this timeline")
58145780 );
58155781 assert_eq!(
@@ -6761,8 +6727,8 @@ mod tests {
67616727 fn main() {
67626728 let arg = std::env::args().nth(1).map(PathBuf::from);
67636729 // A `.edith` restores a whole timeline, anything else *is* the timeline.
6764- // Either way the rest of argv is appended to what comes out. No argument at
6765- // all opens the window empty -- the timeline then arrives by drop or by the
6730+ // Either way the rest of argv is imported into its library. No argument at
6731+ // all opens the window empty -- the library then arrives by drop or by the
67666732 // Import button, and everything below is derived from it at that point
67676733 // instead.
67686734 let mut session = match &arg {
@@ -6792,8 +6758,10 @@ fn main() {
67926758 if let Some(text) = &notice {
67936759 eprintln!("{text}");
67946760 }
6795- // The first file makes the timeline, the rest are appended to it. A refusal
6796- // is not fatal -- the others still load -- but the window must not open
6761+ // The first file makes the timeline -- naming a file to open is a decision
6762+ // about what to watch -- and the rest are imports like any other: rows in
6763+ // the library, dragged onto a lane when they are wanted there. A refusal is
6764+ // not fatal -- the others still load -- but the window must not open
67976765 // silently pretending the file was taken, so the first one seeds the notice.
67986766 for extra in std::env::args().skip(2) {
67996767 // Only reachable with a first argument, so there is a timeline.
crates/engine/src/playback.rsmodified+75 -53
@@ -216,6 +216,33 @@ impl PlaybackSession {
216216 })
217217 }
218218
219+ /// Opens `path` into the **library alone**: everything
220+ /// [`open`](Self::open) scaffolds from it -- the resolution, the frame
221+ /// rate, the clock, the source entry and its length -- over an *empty*
222+ /// timeline. What an import into a window that has no session yet makes,
223+ /// since an import registers a source and never places a clip
224+ /// ([`import`](Self::import)); the file reaches a lane when it is dragged
225+ /// there.
226+ ///
227+ /// Nothing to undo, like the import it stands in for: the lanes start
228+ /// empty rather than being emptied, so no snapshot is pushed.
229+ pub fn open_library(path: impl AsRef<Path>) -> crate::Result<Self> {
230+ let mut session = Self::open(path)?;
231+ // The pair of lanes an opened file comes up with (`Project::single`,
232+ // `open_audio_only`), emptied -- the source list and so the noted
233+ // counts are carried over untouched, which is what keeps a library row
234+ // placeable at its full length.
235+ session.project = Project::from_parts(
236+ session.project.sources().to_vec(),
237+ vec![(LaneKind::Video, Vec::new()), (LaneKind::Audio, Vec::new())],
238+ Vec::new(),
239+ Vec::new(),
240+ )?;
241+ // Onto the emptied mapping: black picture, silence, zero duration.
242+ session.seek(0.);
243+ Ok(session)
244+ }
245+
219246 /// Opens a standalone audio file ([`crate::is_audio`]) as a timeline of its
220247 /// own: one clip on `A1`, nothing on the video lane, and a picture that is
221248 /// the black of an uncovered canvas ([`AUDIO_ONLY_CANVAS`]) for as long as
@@ -775,6 +802,25 @@ impl PlaybackSession {
775802 debug_assert_eq!(self.counts.len(), self.project.sources().len());
776803 }
777804
805+ /// How long the whole of `path` is, in timeline frames: the length noted
806+ /// when it became a source, so a file sitting in the library with no clip
807+ /// anywhere still knows how much of itself there is to place. `0` for a
808+ /// file this session has never taken in.
809+ ///
810+ /// By path and not by stream, because a file is one length however many
811+ /// audio tracks it carries -- the picture's frame count for an mp4, the
812+ /// playing time for a song.
813+ pub fn file_frames(&self, path: &Path) -> u32 {
814+ let at = |wanted: &Path| self.project.sources().iter().position(|s| s.path == wanted);
815+ // A source's own path is canonical ([`Source::new`]), so a caller
816+ // handing one back from [`sources`](Self::sources) -- which is every
817+ // library row -- hits on the first walk and never pays for the
818+ // `canonicalize` syscall the second one makes.
819+ at(path)
820+ .or_else(|| at(&Source::new(path, 0).path))
821+ .map_or(0, |i| self.counts[i])
822+ }
823+
778824 /// The clip the picture is coming from at `timeline_secs`: the lane it sits
779825 /// on and its index there, by the same topmost-lane-wins rule the decoder
780826 /// follows. `None` over a gap and past the end. What a card that grades
@@ -880,7 +926,7 @@ impl PlaybackSession {
880926 self.edit(|p| p.paste(at, clip))
881927 }
882928
883- /// Places `frames` of `path` played on its audio `stream` at
929+ /// Places the whole of `path` played on its audio `stream` at
884930 /// `timeline_secs`, the way [`paste_at`](Self::paste_at) places a copy --
885931 /// the door a library row goes through, and the only way a stream other
886932 /// than the one an import brought in reaches the timeline. The `(file,
@@ -908,20 +954,22 @@ impl PlaybackSession {
908954 timeline_secs: f64,
909955 path: &Path,
910956 stream: usize,
911- frames: u32,
912957 onto: Option<Lane>,
913958 ) -> crate::Result<bool> {
914959 let wanted = Source::new(path, stream);
915- // Another stream of a file whose picture is *already* on the timeline:
916- // that is what a library row is, and it is why nothing here has to
917- // check dimensions or frame rate -- the file passed that at import.
918- if !self.project.sources().iter().any(|s| s.path == wanted.path) {
960+ // Another stream of a file this session has taken in: that is what a
961+ // library row is, and it is why nothing here has to check dimensions or
962+ // frame rate -- the file passed that at import. Its length is the one
963+ // the import noted, so a source that has never been placed is as
964+ // placeable as one that is already on a lane.
965+ let frames = self.file_frames(&wanted.path);
966+ if frames == 0 {
919967 return Err(format!("{} is not on this timeline", path.display()).into());
920968 }
921969 let first = self.first_audio()?;
922970 audio_matches(&wanted, &first)?;
923971 let source = self.project.import(path, stream);
924- self.note_frames(source, frames.max(1));
972+ self.note_frames(source, frames);
925973 let clip = Clip {
926974 start: 0,
927975 in_frame: 0,
@@ -993,15 +1041,24 @@ impl PlaybackSession {
9931041 self.edit(Project::undo)
9941042 }
9951043
996- /// Appends the whole of `path` to the end of the timeline. One undo step,
997- /// and the file becomes a source only if it is not one already.
1044+ /// Takes `path` into the **library**: it becomes a source of this session,
1045+ /// with its length noted ([`file_frames`](Self::file_frames)), and nothing
1046+ /// is placed on any lane. What reaches the timeline is decided afterwards,
1047+ /// by dragging the row onto a lane
1048+ /// ([`place_stream_at`](Self::place_stream_at)) -- importing a file is not
1049+ /// a decision about where it plays.
1050+ ///
1051+ /// The source index, new or the one this file already had: importing twice
1052+ /// registers once. *Not* an undo step -- a source entry alone changes
1053+ /// nothing playable, so there is nothing for [`undo`](Self::undo) to take
1054+ /// back, and nothing reseeks.
9981055 ///
9991056 /// Refused unless it can join the timeline -- same codec, same frame rate,
10001057 /// same audio parameters or both silent; the `Err` names the property that
10011058 /// disagrees, for a caller to show. Nothing is changed by a refusal. A
10021059 /// *resolution* of its own is not a refusal: the clip is placed on the
10031060 /// project canvas by its fit policy ([`PlaybackSession::set_fit`]).
1004- pub fn import(&mut self, path: &Path) -> crate::Result<()> {
1061+ pub fn import(&mut self, path: &Path) -> crate::Result<usize> {
10051062 if crate::is_audio(path) {
10061063 return self.import_audio(path);
10071064 }
@@ -1011,64 +1068,29 @@ impl PlaybackSession {
10111068 // [`place_stream_at`](Self::place_stream_at) is how any other one of
10121069 // its streams reaches the timeline afterwards.
10131070 matches_timeline(&Source::new(path, 0), &meta, &self.meta, &first)?;
1014-
1015- let old_end = self.timeline_duration();
10161071 let source = self.project.import(path, 0);
10171072 self.note_frames(source, meta.frame_count);
1018- // Refused only for an unknown index, and this one just came from `import`.
1019- self.project.append_clip(source, meta.frame_count);
1020- // Reseek like any other edit, even though nothing before the playhead
1021- // moved: the running audio worker's segment list stops at the old end,
1022- // so without this the appended clip would play silent. At EOS the wall
1023- // clock has run on past the timeline, so resume from the join instead
1024- // of wherever it got to -- and the seek is what clears `eos`.
1025- let at = if self.eos { old_end } else { self.now() };
1026- self.seek(at);
1027- Ok(())
1028- }
1029-
1030- /// Appends a standalone audio file to the end of the timeline, on the
1031- /// **audio lane alone**: a song has no picture, so there is nothing to put
1032- /// on the video lane and the timeline shows black under it. Its length is
1033- /// its playing time rounded up to whole frames, which is the only frame
1034- /// count such a source has.
1073+ Ok(source)
1074+ }
1075+
1076+ /// The same registration for a standalone audio file: a song has no
1077+ /// picture, so the length noted for it is its playing time rounded up to
1078+ /// whole frames, which is the only frame count such a source has.
10351079 ///
10361080 /// Refused in the same words as any other import when its rate or layout
10371081 /// disagrees with the timeline's -- one output device, one set of
10381082 /// parameters -- and refused outright on a silent timeline, which has no
10391083 /// device open at all. Nothing is changed by a refusal.
1040- fn import_audio(&mut self, path: &Path) -> crate::Result<()> {
1084+ fn import_audio(&mut self, path: &Path) -> crate::Result<usize> {
10411085 let first = self.first_audio()?;
10421086 // Stream 0, and there is no other: a standalone audio file carries one
10431087 // track (`AudioSession::probe_streams`), so nothing here has a stream
10441088 // to pick the way `place_stream_at` does for an mp4.
10451089 audio_matches(&Source::new(path, 0), &first)?;
10461090 let frames = audio_frames(path, self.meta.frame_rate)?;
1047-
1048- let old_end = self.timeline_duration();
10491091 let source = self.project.import(path, 0);
10501092 self.note_frames(source, frames);
1051- let at = self.project.timeline_frames();
1052- self.project.place(
1053- Lane::A1,
1054- at,
1055- Clip {
1056- start: at,
1057- in_frame: 0,
1058- out_frame: frames,
1059- source,
1060- link: None,
1061- eq: None,
1062- color: None,
1063- fit: FitPolicy::default(),
1064- },
1065- );
1066- // Same reason as a video import: the running audio worker's segment
1067- // list stops at the old end, so without a reseek the appended clip
1068- // would play silent.
1069- let at = if self.eos { old_end } else { self.now() };
1070- self.seek(at);
1071- Ok(())
1093+ Ok(source)
10721094 }
10731095
10741096 /// Applies an edit and, if it took, reseeks onto the new mapping. `seek`
crates/engine/tests/audio_files.rsmodified+41 -11
@@ -32,6 +32,20 @@ fn asset(name: &str) -> PathBuf {
3232 .join(name)
3333 }
3434
35+/// The two acts a file on the timeline now takes, in the order a user does
36+/// them: taken into the library (`PlaybackSession::import` places nothing),
37+/// then dragged onto the end of the timeline.
38+fn import_and_place(session: &mut PlaybackSession, path: &Path) {
39+ session.import(path).expect("the file joins this timeline");
40+ let end = session.timeline_duration();
41+ assert!(
42+ session
43+ .place_stream_at(end, path, 0, None)
44+ .expect("a file just imported is on this timeline"),
45+ "the drag onto the end of the timeline"
46+ );
47+}
48+
3549 /// The message of a call that had to be refused. Not `expect_err`: that wants
3650 /// `Debug` on the success side, and neither a session nor a packet has it.
3751 fn refusal<T>(result: engine::Result<T>) -> String {
@@ -119,11 +133,24 @@ fn duration_comes_from_the_header_or_the_decode() {
119133 fn a_song_joins_a_video_timeline_on_the_audio_lane() {
120134 let mut session = open(asset("test_av.mp4"));
121135 let before = session.timeline_duration();
136+ // The import alone is a library row: the lanes are untouched until it is
137+ // dragged onto one.
122138 session
123139 .import(&asset("test_tone.mp3"))
124140 .expect("import the song");
141+ assert_eq!(session.sources().len(), 2, "the library grew");
142+ assert_eq!(session.lane_clips(Lane::A1).len(), 1, "an import placed it");
143+ assert_eq!(session.timeline_duration(), before);
144+ assert_eq!(session.file_frames(&asset("test_tone.mp3")), 90);
145+ let end = session.timeline_duration();
146+ assert!(
147+ session
148+ .place_stream_at(end, &asset("test_tone.mp3"), 0, None)
149+ .expect("a song just imported is on this timeline")
150+ );
125151
126- // 3 s at 30 fps, appended: the audio lane runs 90 frames past the video.
152+ // 3 s at 30 fps, placed at the end: the audio lane runs 90 frames past the
153+ // video.
127154 assert!(
128155 (session.timeline_duration() - (before + 3.0)).abs() < 0.05,
129156 "{} s of timeline, want {}",
@@ -151,7 +178,7 @@ fn a_rate_the_device_cannot_mix_is_refused_at_the_door() {
151178 refusal(session.import(&asset("test_tone_48k.wav"))),
152179 "audio 48000 Hz 2 ch does not match the timeline's 44100 Hz 2 ch"
153180 );
154- // Refused means unchanged: no source, no clip, no length.
181+ // Refused means unchanged: no library row, no clip, no length.
155182 assert_eq!(session.sources().len(), 1);
156183 assert_eq!(session.lane_clips(Lane::A1).len(), 1);
157184 }
@@ -228,9 +255,7 @@ fn an_audio_only_project_saves_reloads_and_exports_its_sound() {
228255 let mut session = open(asset("test_tone.mp3"));
229256 // A second song joins it, the same way one joins a timeline of video: no
230257 // picture is needed for the import to have something to hold it to.
231- session
232- .import(&asset("test_tone.flac"))
233- .expect("a song joins a song");
258+ import_and_place(&mut session, &asset("test_tone.flac"));
234259 assert_eq!(session.lane_clips(Lane::A1).len(), 2);
235260 assert!(session.lane_clips(Lane::V1).is_empty());
236261 assert!((session.timeline_duration() - 6.0).abs() < 0.1);
@@ -304,18 +329,23 @@ fn an_audio_only_project_saves_reloads_and_exports_its_sound() {
304329 #[test]
305330 fn a_video_joins_a_timeline_a_song_started() {
306331 let mut session = open(asset("test_tone.mp3"));
307- session
308- .import(&asset("test_av.mp4"))
309- .expect("30 fps H.264 matches the audio-only canvas");
332+ import_and_place(&mut session, &asset("test_av.mp4"));
310333 assert_eq!(session.lane_clips(Lane::V1).len(), 1, "a picture at last");
311334 assert_eq!(session.lane_clips(Lane::A1).len(), 2);
312335 // And a file at another rate is refused in the timeline's own words rather
313- // than silently retiming the song already on the lane.
336+ // than silently retiming the song already on the lane -- the sample rate at
337+ // one door, the *frame* rate at the other: the canvas a song scaffolds has
338+ // a frame rate of its own (30 fps), and a 25 fps picture cannot join it.
314339 let e = refusal(session.import(&asset("test_tone_48k.wav")));
315340 assert_eq!(
316341 e,
317342 "audio 48000 Hz 2 ch does not match the timeline's 44100 Hz 2 ch"
318343 );
344+ assert_eq!(
345+ refusal(session.import(&asset("test_25fps.mp4"))),
346+ "25.000 fps does not match the timeline's 30.000 fps"
347+ );
348+ assert_eq!(session.sources().len(), 2, "a refusal left a library row");
319349 }
320350
321351 /// Blocks until the export settles, whichever way it settled.
@@ -362,7 +392,7 @@ fn a_song_survives_a_save_and_a_reload() {
362392 };
363393
364394 let mut session = open(copy("test_av.mp4"));
365- session.import(&copy("test_tone.flac")).expect("import");
395+ import_and_place(&mut session, &copy("test_tone.flac"));
366396 // And one placed at the playhead, the way a library row dropped on the
367397 // audio lane arrives -- so the reload has both doors to restore.
368398 let song = session.lane_clips(Lane::A1)[1];
@@ -430,7 +460,7 @@ fn a_pasted_song_never_lands_on_the_video_lane() {
430460 };
431461
432462 let mut session = open(copy("test_av.mp4"));
433- session.import(&copy("test_tone.flac")).expect("import");
463+ import_and_place(&mut session, &copy("test_tone.flac"));
434464 let song = session.lane_clips(Lane::A1)[1];
435465 let video_before = session.lane_clips(Lane::V1).to_vec();
436466 assert!(session.paste_at(0.0, song), "paste the copied song");
crates/engine/tests/playback.rsmodified+140 -48
@@ -626,6 +626,22 @@ fn edits_keep_the_audio_clock() {
626626 );
627627 }
628628
629+/// The two acts a second file on the timeline now takes, in the order a user
630+/// does them: taken into the library, then dragged onto the end of the
631+/// timeline. An import registers a source and places nothing
632+/// (`PlaybackSession::import`), so every test that wants a second source *on*
633+/// the lanes does both.
634+fn import_and_place(session: &mut PlaybackSession, path: &Path) {
635+ session.import(path).expect("the file joins this timeline");
636+ let end = session.timeline_duration();
637+ assert!(
638+ session
639+ .place_stream_at(end, path, 0, None)
640+ .expect("a file just imported is on this timeline"),
641+ "the drag onto the end of the timeline"
642+ );
643+}
644+
629645 /// Frame count straight from the container, without decoding anything.
630646 fn frame_count(path: &Path) -> u32 {
631647 engine::demux::Demuxer::open(path)
@@ -634,17 +650,28 @@ fn frame_count(path: &Path) -> u32 {
634650 .frame_count
635651 }
636652
637-/// Import: a second file appended to the timeline, played through as one
638-/// stream. No audio needed for the pictures, so this runs anywhere.
653+/// An imported file dragged onto the end of the timeline, played through as
654+/// one stream. No audio needed for the pictures, so this runs anywhere.
639655 #[test]
640-fn import_appends_a_second_source_and_plays_across_the_join() {
656+fn a_placed_import_plays_across_the_join() {
641657 let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4"));
642658 let mut session = open(&av);
643659 let (fps, first) = (session.meta().frame_rate, session.meta().frame_count);
644660 let second = frame_count(&av2);
645661 assert_eq!((first, second), (150, 120), "fixtures changed");
646662
663+ // The import alone fills the library: nothing on the lanes moved.
647664 session.import(&av2).expect("test_av2 matches test_av");
665+ assert_eq!(session.clip_spans().len(), 1, "an import placed a clip");
666+ assert!((session.timeline_duration() - 5.0).abs() < 1e-9);
667+ assert_eq!(session.file_frames(&av2), second, "its own length is noted");
668+ // ...and the drag puts it at the end.
669+ let end = session.timeline_duration();
670+ assert!(
671+ session
672+ .place_stream_at(end, &av2, 0, None)
673+ .expect("a file just imported is on this timeline")
674+ );
648675 assert_eq!(session.clip_spans().len(), 2);
649676 assert_eq!(
650677 session
@@ -653,7 +680,7 @@ fn import_appends_a_second_source_and_plays_across_the_join() {
653680 .map(|&(.., s)| s)
654681 .collect::<Vec<_>>(),
655682 vec![0, 1],
656- "the appended clip plays the imported file"
683+ "the placed clip plays the imported file"
657684 );
658685 assert!(
659686 (session.timeline_duration() - 9.0).abs() < 1e-9,
@@ -721,6 +748,46 @@ fn import_appends_a_second_source_and_plays_across_the_join() {
721748 );
722749 }
723750
751+/// The first import into a window with nothing open: the file fills the library
752+/// and the timeline stays *empty*, and the drag that follows is what puts it on
753+/// a lane -- at the file's own length, which nothing on the timeline could have
754+/// told the session. The whole point of an import that places nothing.
755+#[test]
756+fn the_first_import_opens_a_library_over_an_empty_timeline() {
757+ let av = asset("test_av.mp4");
758+ let mut session = PlaybackSession::open_library(&av).expect("open into the library");
759+ session.set_gain(0.0);
760+ assert!(session.is_empty(), "the timeline must start empty");
761+ assert_eq!(session.timeline_duration(), 0.0);
762+ assert!(session.clip_spans().is_empty());
763+ // But it is a session: the file's own picture, rate and clock, and its row.
764+ assert_eq!(session.sources().len(), 1, "the library row");
765+ assert_eq!(session.resolution(), (1280, 720));
766+ assert_eq!(session.meta().frame_rate, 30.0);
767+ assert_eq!(session.file_frames(&av), 150, "5 s at 30 fps, never placed");
768+ assert!(!session.undo(), "opening a library is not an undo step");
769+
770+ // The drag: the row goes onto the timeline whole, and it plays from there.
771+ assert!(
772+ session
773+ .place_stream_at(0.0, &av, 0, None)
774+ .expect("its own file is on this timeline")
775+ );
776+ assert!(!session.is_empty(), "the drag left the timeline empty");
777+ assert!((session.timeline_duration() - 5.0).abs() < 1e-9);
778+ session.seek(0.0);
779+ let landed = next_frame(&mut session, "the dragged clip");
780+ assert_eq!(landed.index, 0);
781+ assert!(
782+ landed.bgra == source_frame(&av, 0),
783+ "the placed clip is not playing its file"
784+ );
785+ // ...and it is an edit like any other, so one `z` takes it back to empty.
786+ assert!(session.undo(), "undo the drag");
787+ assert!(session.is_empty());
788+ assert_eq!(session.sources().len(), 1, "the row survives the undo");
789+}
790+
724791 /// One timeline, one set of parameters: everything else is refused by name and
725792 /// changes nothing.
726793 #[test]
@@ -746,6 +813,15 @@ fn import_refuses_what_does_not_match() {
746813 "a resolution of its own is not a refusal any more: {err}"
747814 );
748815
816+ // Same codec, same audio, 25 fps: the one property left, named with both
817+ // rates. Mixing rates would mean retiming the timeline itself, and there is
818+ // no resampler for that, so the refusal is the honest answer.
819+ let err = session
820+ .import(&asset("test_25fps.mp4"))
821+ .expect_err("25 fps must not join a 30 fps timeline")
822+ .to_string();
823+ assert_eq!(err, "25.000 fps does not match the timeline's 30.000 fps");
824+
749825 // Same size and rate, no audio track: the timeline has one.
750826 let err = session
751827 .import(&asset("test_baseline.mp4"))
@@ -758,6 +834,7 @@ fn import_refuses_what_does_not_match() {
758834 "a missing file is an error, not a panic"
759835 );
760836 assert_eq!(session.clip_spans().len(), 1, "a refusal changes nothing");
837+ assert_eq!(session.sources().len(), 1, "a refusal left a library row");
761838 assert!((session.timeline_duration() - 5.0).abs() < 1e-9);
762839
763840 // The mirror: audio into a silent timeline is refused just as loudly.
@@ -780,9 +857,7 @@ fn media_of_two_resolutions_share_one_timeline() {
780857 let mut session = open(asset("test_baseline.mp4"));
781858 assert_eq!(session.resolution(), (1280, 720), "source 0's picture");
782859 assert_eq!(session.native_resolution(), (1280, 720));
783- session
784- .import(&asset("test_mismatch.mp4"))
785- .expect("640x360 must join a 1280x720 timeline");
860+ import_and_place(&mut session, &asset("test_mismatch.mp4"));
786861 // 150 frames of the first file, then 60 of the second, at 30 fps.
787862 assert_eq!(session.clip_spans().len(), 2);
788863 assert!((session.timeline_duration() - 7.0).abs() < 1e-9);
@@ -805,9 +880,7 @@ fn media_of_two_resolutions_share_one_timeline() {
805880 #[test]
806881 fn a_fitted_clip_is_letterboxed_and_a_filled_one_is_not() {
807882 let mut session = open(asset("test_baseline.mp4"));
808- session
809- .import(&asset("test_mismatch.mp4"))
810- .expect("640x360 joins");
883+ import_and_place(&mut session, &asset("test_mismatch.mp4"));
811884 // A 4:3 project: both clips are 16:9, so both are letterboxed on it. 960x720
812885 // holds the whole 640x360 picture as 960x540 with 90 rows of bar either way
813886 // (fit_rect's own arithmetic, asserted in scale.rs).
@@ -860,14 +933,23 @@ fn a_project_at_the_media_size_is_the_decoder_untouched() {
860933 );
861934 }
862935
863-/// The same file twice is two clips of one source -- a source index is handed
864-/// out once and reused, which is what keeps clipboard clips valid forever.
936+/// The same file twice is one library row -- a source index is handed out once
937+/// and reused, which is what keeps clipboard clips valid forever -- and two
938+/// drags of that one row are two clips of it.
865939 #[test]
866940 fn importing_one_file_twice_reuses_its_source() {
867941 let mut session = open(asset("test_av.mp4"));
868942 let av2 = asset("test_av2.mp4");
869- session.import(&av2).expect("first import");
870- session.import(&av2).expect("second import");
943+ assert_eq!(session.import(&av2).expect("first import"), 1);
944+ assert_eq!(
945+ session.import(&av2).expect("second import"),
946+ 1,
947+ "the second import is the same row"
948+ );
949+ assert_eq!(session.sources().len(), 2, "one row for one file");
950+ import_and_place(&mut session, &av2);
951+ import_and_place(&mut session, &av2);
952+ assert_eq!(session.sources().len(), 2, "and still one row");
871953 assert_eq!(
872954 session
873955 .clip_spans_by_source()
@@ -884,10 +966,15 @@ fn importing_one_file_twice_reuses_its_source() {
884966 );
885967 }
886968
887-/// Importing after the timeline has been played out revives the session at the
888-/// join, not at wherever the free-running clock got to.
969+/// A library row placed after the timeline has been played out revives the
970+/// session: the placement reseeks like every other edit, and that is what
971+/// clears `eos`. The import itself changes nothing, played out or not.
972+///
973+/// Where it resumes is not asserted, for the reason `paste_at_eos_revives_the_
974+/// session` does not assert it either: an edit reseeks to `now()`, and at EOS
975+/// the free-running clock has gone past the end.
889976 #[test]
890-fn import_at_eos_resumes_into_the_new_clip() {
977+fn a_placement_at_eos_revives_the_session() {
891978 let av2 = asset("test_av2.mp4");
892979 let mut session = open(asset("test_av.mp4"));
893980 let first = session.meta().frame_count;
@@ -897,18 +984,24 @@ fn import_at_eos_resumes_into_the_new_clip() {
897984 assert!(session.is_eos());
898985
899986 session.import(&av2).expect("import at EOS");
900- assert!(!session.is_eos(), "the import did not revive the session");
901- let landed = next_frame(&mut session, "import at EOS");
902- assert_eq!(landed.index, first, "resumed at the join");
903987 assert!(
904- landed.bgra == source_frame(&av2, 0),
905- "the resume is not showing the imported file"
988+ session.is_eos(),
989+ "an import is not an edit: it cannot revive"
990+ );
991+ assert!(
992+ session
993+ .place_stream_at(session.timeline_duration(), &av2, 0, None)
994+ .expect("a file just imported is on this timeline")
995+ );
996+ assert!(
997+ !session.is_eos(),
998+ "the placement did not revive the session"
906999 );
9071000 assert_eq!(
908- drain_to_eof(&mut session).1,
909- Some(first + frame_count(&av2) - 1),
910- "the appended clip did not play out"
1001+ session.timeline_duration(),
1002+ f64::from(first + frame_count(&av2)) / session.meta().frame_rate
9111003 );
1004+ assert_eq!(session.clip_spans().len(), 2);
9121005 }
9131006
9141007 /// A source deleted mid-session: the clip that names it contributes no pictures
@@ -920,7 +1013,7 @@ fn a_vanished_source_is_skipped() {
9201013 std::fs::copy(asset("test_av2.mp4"), &scratch).expect("copy the fixture");
9211014 let mut session = open(asset("test_av.mp4"));
9221015 let first = session.meta().frame_count;
923- session.import(&scratch).expect("import the copy");
1016+ import_and_place(&mut session, &scratch);
9241017 std::fs::remove_file(&scratch).expect("unlink");
9251018
9261019 session.seek(0.0);
@@ -931,9 +1024,9 @@ fn a_vanished_source_is_skipped() {
9311024 assert!(session.is_eos(), "the timeline still ends");
9321025 }
9331026
934-/// Importing while the device is running: the import reseeks under the
935-/// playhead, so what must not happen is a stall -- the joined timeline still
936-/// plays out whole. No audio needed, so this runs anywhere.
1027+/// Importing and placing while the device is running: the placement reseeks
1028+/// under the playhead, so what must not happen is a stall -- the joined
1029+/// timeline still plays out whole. No audio needed, so this runs anywhere.
9371030 #[test]
9381031 fn import_during_play_does_not_stall() {
9391032 let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4"));
@@ -945,9 +1038,9 @@ fn import_during_play_does_not_stall() {
9451038 run_for(&mut session, &mut last_index, Duration::from_millis(300));
9461039 assert!(last_index.is_some(), "no frames before the import");
9471040
948- session.import(&av2).expect("import while playing");
1041+ import_and_place(&mut session, &av2);
9491042 assert_eq!(session.clip_spans().len(), 2);
950- assert!(session.is_playing(), "the import stopped the clock");
1043+ assert!(session.is_playing(), "the placement stopped the clock");
9511044
9521045 // From here on the indices go *backwards* once: `run_for` decodes far ahead
9531046 // of the clock (ledger), and the import reseeks to `now()` -- which is why
@@ -961,38 +1054,37 @@ fn import_during_play_does_not_stall() {
9611054 );
9621055 }
9631056
964-/// An import is *one* undo step: the appended clip goes, the source entry stays
965-/// behind (documented, harmless -- source indexes are forever), and the session
966-/// is still playable on the restored timeline.
1057+/// An import is *not* an undo step -- a library row changes nothing playable,
1058+/// so there is nothing for `z` to take back -- and the placement that follows
1059+/// is one. Undoing it leaves the row in the library, where the user put it.
9671060 #[test]
968-fn undo_after_import_is_one_step() {
1061+fn an_import_is_not_an_undo_step_but_its_placement_is() {
9691062 let av2 = asset("test_av2.mp4");
9701063 let mut session = open(asset("test_av.mp4"));
9711064 session.import(&av2).expect("import");
1065+ assert!(!session.undo(), "an import must not be undoable");
1066+ assert_eq!(session.sources().len(), 2, "and it is still in the library");
1067+
1068+ import_and_place(&mut session, &av2);
9721069 assert_eq!(session.clip_spans().len(), 2);
9731070 assert!((session.timeline_duration() - 9.0).abs() < 1e-9);
9741071
975- assert!(session.undo(), "undo the import");
1072+ assert!(session.undo(), "undo the placement");
9761073 assert_eq!(session.clip_spans().len(), 1, "one step, back to one clip");
9771074 assert!(
9781075 (session.timeline_duration() - 5.0).abs() < 1e-9,
9791076 "undo did not restore the duration: {}",
9801077 session.timeline_duration()
9811078 );
982-
983- // The orphan source is only observable on the project -- `PlaybackSession`
984- // exposes sources through `clip_spans_by_source`, and the undone clip is
985- // exactly the one that named it.
986- let mut project = engine::Project::single(asset("test_av.mp4"), 150);
987- let source = project.import(&av2, 0);
988- assert!(project.append_clip(source, 120));
989- assert!(project.undo());
990- assert_eq!(project.clips().len(), 1);
991- assert_eq!(project.sources().len(), 2, "the source entry stays behind");
1079+ assert_eq!(
1080+ session.sources().len(),
1081+ 2,
1082+ "the library row survives an undo of the clip that used it"
1083+ );
9921084
9931085 // Still playable: the undo reseeked to the (paused) playhead at zero.
9941086 session.play();
995- assert_eq!(next_index(&mut session, "undo of an import"), 0);
1087+ assert_eq!(next_index(&mut session, "undo of a placement"), 0);
9961088 }
9971089
9981090 /// 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() {
10041096 #[ignore = "needs a running PipeWire daemon"]
10051097 fn audio_runs_across_a_source_join() {
10061098 let mut session = open(asset("test_av.mp4"));
1007- session.import(&asset("test_av2.mp4")).expect("import");
1099+ import_and_place(&mut session, &asset("test_av2.mp4"));
10081100 let first = session.meta().frame_count;
10091101
10101102 session.seek(4.5); // half a second before the join
crates/engine/tests/saveload.rsmodified+20 -10
@@ -44,14 +44,13 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
4444 let media = copy_in(&dir, "test_multilang.mp4");
4545 let mut session = PlaybackSession::open(&media).expect("open the fixture");
4646 session.set_gain(0.0); // silent like the rest of the suites
47- let frames = session.clip_at(0).expect("one clip").out_frame;
4847 let end = session.timeline_duration();
4948
5049 // Stream 1 is the French track, same rate and layout as stream 0, so it
5150 // can join this timeline; it lands as a second source of the same file.
5251 assert!(
5352 session
54- .place_stream_at(end, &media, 1, frames, None)
53+ .place_stream_at(end, &media, 1, None)
5554 .expect("stream 1 matches the timeline")
5655 );
5756 assert_eq!(
@@ -83,15 +82,14 @@ fn a_picked_audio_stream_lands_saves_and_comes_back() {
8382 let other = copy_in(&dir, "test_multiaudio.mp4");
8483 let mut session = PlaybackSession::open(&other).expect("open the fixture");
8584 session.set_gain(0.0);
86- let frames = session.clip_at(0).expect("one clip").out_frame;
8785 let err = session
88- .place_stream_at(0.0, &other, 1, frames, None)
86+ .place_stream_at(0.0, &other, 1, None)
8987 .expect_err("22.05 kHz mono cannot join a 44.1 kHz stereo timeline")
9088 .to_string();
9189 assert!(err.contains("22050"), "unhelpful refusal: {err}");
9290 assert!(
9391 session
94- .place_stream_at(0.0, &media, 1, frames, None)
92+ .place_stream_at(0.0, &media, 1, None)
9593 .expect_err("a file that is not a source")
9694 .to_string()
9795 .contains("not on this timeline")
@@ -120,7 +118,9 @@ fn a_trimmed_timeline_saves_and_opens_again() {
120118 session.save_project(&path).expect("save");
121119 let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens");
122120 assert_eq!(
123- loaded.clip_at(0).map(|c| (c.start, c.in_frame, c.out_frame)),
121+ loaded
122+ .clip_at(0)
123+ .map(|c| (c.start, c.in_frame, c.out_frame)),
124124 Some((5, 5, whole.end() / 2)),
125125 "the trimmed range is what came back"
126126 );
@@ -142,9 +142,17 @@ fn a_trimmed_timeline_saves_and_opens_again() {
142142 /// with something to lose in every field.
143143 fn edited(dir: &Path) -> PlaybackSession {
144144 let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open");
145- session
146- .import(&copy_in(dir, "test_av2.mp4"))
147- .expect("import the second file");
145+ // Imported into the library, then dragged onto the end -- the two acts a
146+ // second file on the timeline takes.
147+ let second = copy_in(dir, "test_av2.mp4");
148+ session.import(&second).expect("import the second file");
149+ let end = session.timeline_duration();
150+ assert!(
151+ session
152+ .place_stream_at(end, &second, 0, None)
153+ .expect("a file just imported is on this timeline"),
154+ "drag it onto the end"
155+ );
148156 assert!(session.cut_at(2.0), "cut inside the first file");
149157 assert!(session.cut_at(6.5), "cut inside the second file");
150158 assert!(
@@ -505,10 +513,12 @@ fn a_folder_of_media_and_its_project_can_be_moved() {
505513 fn orphan_sources_are_not_written() {
506514 let dir = scratch("orphans");
507515 let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open");
516+ // An import that was never dragged onto a lane *is* the orphan: a library
517+ // row with no clip naming it. (So is one whose clip was undone -- the same
518+ // entry, reached the other way.)
508519 session
509520 .import(&copy_in(&dir, "test_av2.mp4"))
510521 .expect("import");
511- assert!(session.undo(), "take the import back");
512522 assert_eq!(
513523 session.sources().len(),
514524 2,
scripts/gen_fixtures.shmodified+10 -0
@@ -117,6 +117,16 @@ ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=30:duration=2 \
117117 # has something concrete to refuse.
118118 ffmpeg -y -f lavfi -i testsrc2=size=640x360:rate=30:duration=2 \
119119 -an -c:v libx264 -profile:v baseline -pix_fmt yuv420p assets/test_mismatch.mp4
120+# Frame-rate fixture: test_av.mp4 in every respect a timeline is held to -- same
121+# codec, same profile, same 44.1k stereo AAC -- except that it runs at 25 fps.
122+# The only thing left for an import to refuse is the rate, which is what makes
123+# the refusal's wording testable.
124+ffmpeg -y -f lavfi -i testsrc2=size=1280x720:rate=25:duration=2 \
125+ -f lavfi -i "sine=frequency=440:duration=2" \
126+ -f lavfi -i "sine=frequency=880:duration=2" \
127+ -filter_complex "[1:a][2:a]join=inputs=2:channel_layout=stereo,volume='0.5+0.5*sin(2*PI*t)':eval=frame[a]" \
128+ -map 0:v -map "[a]" -c:v libx264 -profile:v baseline -pix_fmt yuv420p \
129+ -c:a aac -b:a 128k assets/test_25fps.mp4
120130 # Standalone audio fixtures: the same 440/880 Hz tone with the 1 Hz pulse the
121131 # A/V fixture carries, at test_av.mp4's own 44.1k stereo -- so an imported song
122132 # can share a timeline with the video clips and the peaks tests can reuse the