forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

sequences.rs191 lines, 9.0 KiBHistoryRawDownload
1//! Sequences and compound clips through the session: nesting keeps the picture and the sound
2//! the flat timeline had, the project file round-trips several sequences (v45) and writes the
3//! old dialect when there is only one, and every step undoes.
4//!
5//! ```text
6//! cargo test -p engine --release --test sequences
7//! ```
8
9use std::path::PathBuf;
10
11use engine::PlaybackSession;
12use engine::project::{Lane, LaneKind};
13use engine::scratch::Scratch;
14
15fn asset(name: &str) -> PathBuf {
16 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../assets").join(name)
17}
18
19fn cut_session() -> PlaybackSession {
20 let mut s = PlaybackSession::open(asset("test_25fps.mp4")).expect("open the fixture");
21 s.set_gain(0.0);
22 assert!(s.cut_at(0.4));
23 assert!(s.cut_at(0.8));
24 s
25}
26
27fn starts(s: &PlaybackSession, lane: Lane) -> Vec<(u32, u32, u32, usize)> {
28 s.lane_clips(lane).iter().map(|c| (c.start, c.in_frame, c.out_frame, c.source)).collect()
29}
30
31#[test]
32fn a_nested_render_is_the_flat_render() {
33 let flat = cut_session();
34 let mut nested = cut_session();
35 let n = nested.nest(&[(Lane::V1, 1), (Lane::V1, 2), (Lane::A1, 1), (Lane::A1, 2)], Some("Tail")).expect("nest");
36 assert_eq!(n.clips.len(), 2, "one compound clip each for the picture and the sound");
37 assert_eq!(nested.lane_clips(Lane::V1).len(), 2, "the head and the compound");
38 assert!(nested.compound_at(Lane::V1, 1).is_some());
39 for frame in [0u32, 9, 10, 15, 19, 20, 24] {
40 let a = flat.compose_at(frame, 320, 180).expect("flat frame");
41 let b = nested.compose_at(frame, 320, 180).expect("nested frame");
42 assert_eq!(a.bgra, b.bgra, "frame {frame} differs between the flat and the nested timeline");
43 }
44 let (fp, _) = flat.export_snapshot();
45 let (np, _) = nested.export_snapshot();
46 let fps = flat.meta().frame_rate;
47 assert_eq!(fp.audio_segments_from(0, fps), np.audio_segments_from(0, fps), "the sound is the same segments");
48 assert_eq!(fp.timeline_frames(), np.timeline_frames());
49}
50
51#[test]
52fn sequences_round_trip_through_the_file() {
53 let dir = Scratch::dir("ve_sequences_roundtrip");
54 let media = dir.join("test_25fps.mp4");
55 std::fs::copy(asset("test_25fps.mp4"), &media).expect("copy");
56 let mut s = PlaybackSession::open(&media).expect("open");
57 s.set_gain(0.0);
58 assert!(s.cut_at(0.4));
59 let n = s.nest(&[(Lane::V1, 1), (Lane::A1, 1)], Some("Back half")).expect("nest");
60 let other = s.new_sequence(Some("Scratch pad")).expect("new");
61 s.switch_sequence(other).expect("switch");
62 assert!(s.place_at(Lane::V1, 0.0, engine::Clip::source_range(0, 0, 10)));
63 s.switch_sequence(0).expect("back");
64 let path = dir.join("p.edith");
65 s.save_project(&path).expect("save");
66 let text = std::fs::read_to_string(&path).expect("read");
67 assert!(text.starts_with("edith 45\n"), "{}", text.lines().next().unwrap_or(""));
68 assert!(text.contains("\nseqactive 0 Sequence 1\n"), "{text}");
69 assert!(text.contains("\nseq 1 Back half\n") && text.contains("\nendseq\n"), "{text}");
70
71 let back = PlaybackSession::open_project(&path).expect("reopen");
72 let names: Vec<String> = back.sequences().iter().map(|r| r.name.clone()).collect();
73 assert_eq!(names, ["Sequence 1", "Scratch pad", "Back half"], "the tab order is kept");
74 assert_eq!(back.active_sequence(), 0);
75 assert_eq!(starts(&back, Lane::V1), starts(&s, Lane::V1));
76 assert_eq!(starts(&back, Lane::A1), starts(&s, Lane::A1));
77 assert_eq!(back.compound_at(Lane::V1, 1), Some(n.sequence));
78 // The parked ones came back whole.
79 let mut back = back;
80 back.switch_sequence(other).expect("switch");
81 assert_eq!(starts(&back, Lane::V1), vec![(0, 0, 10, 0)]);
82 back.switch_sequence(n.sequence).expect("switch");
83 assert_eq!(back.lane_clips(Lane::V1).len(), 1);
84 assert_eq!(back.lane_clips(Lane::A1).len(), 1);
85 // And the render of the reopened project is the render that was saved.
86 back.switch_sequence(0).expect("switch");
87 let a = s.compose_at(15, 160, 90).expect("frame");
88 let b = back.compose_at(15, 160, 90).expect("frame");
89 assert_eq!(a.bgra, b.bgra);
90}
91
92#[test]
93fn one_sequence_writes_the_old_dialect_and_an_old_file_is_one_sequence() {
94 let dir = Scratch::dir("ve_sequences_old");
95 let media = dir.join("test_25fps.mp4");
96 std::fs::copy(asset("test_25fps.mp4"), &media).expect("copy");
97 let s = PlaybackSession::open(&media).expect("open");
98 s.set_gain(0.0);
99 let path = dir.join("one.edith");
100 s.save_project(&path).expect("save");
101 let text = std::fs::read_to_string(&path).expect("read");
102 assert!(!text.starts_with("edith 45") && !text.contains("seq"), "{text}");
103 // A v44 file is read as it always was.
104 let old = text.replacen(text.lines().next().unwrap_or(""), "edith 44", 1);
105 let p44 = dir.join("v44.edith");
106 std::fs::write(&p44, old).expect("write");
107 let back = PlaybackSession::open_project(&p44).expect("open v44");
108 assert_eq!(back.sequences().len(), 1);
109 assert!(back.sequences()[0].active);
110}
111
112#[test]
113fn nest_break_apart_and_undo_through_the_session() {
114 let mut s = cut_session();
115 let before = (starts(&s, Lane::V1), starts(&s, Lane::A1));
116 s.nest(&[(Lane::V1, 0), (Lane::V1, 1), (Lane::A1, 0), (Lane::A1, 1)], None).expect("nest");
117 assert_eq!(s.sequences().len(), 2);
118 s.break_apart(&[(Lane::V1, 0)]).expect("break apart");
119 assert_eq!((starts(&s, Lane::V1), starts(&s, Lane::A1)), before, "nest then break apart is the identity");
120 assert!(s.undo());
121 assert!(s.compound_at(Lane::V1, 0).is_some(), "undo of the break apart brings the compound back");
122 assert!(s.undo());
123 assert_eq!((starts(&s, Lane::V1), starts(&s, Lane::A1)), before);
124 assert_eq!(s.sequences().len(), 1, "and the sequence the nest made goes with it");
125 assert!(s.redo());
126 assert_eq!(s.sequences().len(), 2);
127}
128
129#[test]
130fn a_sequence_cannot_contain_itself() {
131 let mut s = cut_session();
132 let n = s.nest(&[(Lane::V1, 0), (Lane::A1, 0)], Some("Inner")).expect("nest");
133 s.switch_sequence(n.sequence).expect("open it");
134 let err = s.place_sequence_at(0, Lane::V1, 5.0).unwrap_err().to_string();
135 assert!(err.contains("inside itself"), "{err}");
136 let err = s.place_sequence_at(n.sequence, Lane::V1, 5.0).unwrap_err().to_string();
137 assert!(err.contains("cannot contain itself"), "{err}");
138 s.switch_sequence(0).expect("back");
139 assert!(s.delete_sequence(n.sequence).is_err(), "a played sequence is not deleted");
140 let _ = LaneKind::Video;
141}
142
143#[test]
144fn nesting_the_picture_takes_its_linked_sound_along() {
145 let mut s = cut_session();
146 let n = s.nest(&[(Lane::V1, 1)], Some("Middle")).expect("nest");
147 assert_eq!(n.clips.len(), 2, "the linked sound goes into the compound with the picture");
148 assert!(s.compound_at(Lane::V1, 1).is_some());
149 assert!(s.compound_at(Lane::A1, 1).is_some(), "no detached sound is left behind");
150}
151
152#[test]
153fn a_compounds_own_placement_and_grade_reach_the_picture() {
154 use engine::color::ColorParams;
155 use engine::transform::TransformParams;
156 let place = TransformParams { pos_x: 0.1, scale: 0.7, opacity: 0.5, ..TransformParams::default() };
157 let grade = ColorParams { brightness: 0.2, saturation: 0.5, ..ColorParams::default() };
158 let mut flat = cut_session();
159 for i in [1, 2] {
160 assert!(flat.set_transform(Lane::V1, i, Some(place)));
161 assert!(flat.set_color(Lane::V1, i, Some(grade)));
162 }
163 let mut nested = cut_session();
164 nested.nest(&[(Lane::V1, 1), (Lane::V1, 2), (Lane::A1, 1), (Lane::A1, 2)], None).expect("nest");
165 let plain = nested.compose_at(12, 320, 180).expect("plain frame");
166 assert!(nested.set_transform(Lane::V1, 1, Some(place)));
167 assert!(nested.set_color(Lane::V1, 1, Some(grade)));
168 for frame in [12u32, 20] {
169 let a = flat.compose_at(frame, 320, 180).expect("flat frame");
170 let b = nested.compose_at(frame, 320, 180).expect("nested frame");
171 assert_eq!(a.bgra, b.bgra, "frame {frame}: the compound's own look is the look of its clips");
172 }
173 assert_ne!(plain.bgra, nested.compose_at(12, 320, 180).expect("frame").bgra, "and it does change the picture");
174}
175
176#[test]
177fn a_compounds_pieces_and_a_sources_nested_use_are_visible() {
178 let mut s = cut_session();
179 let n = s.nest(&[(Lane::V1, 1), (Lane::V1, 2), (Lane::A1, 1), (Lane::A1, 2)], Some("Tail")).expect("nest");
180 // The source is played by clips inside the new sequence, not by the active one's own clips.
181 assert_eq!(s.source_uses_parked(0), vec![(n.sequence, 4)]);
182 let compound = s.lane_clips(Lane::V1)[1].clone();
183 let pieces = s.compound_pieces(LaneKind::Video, &compound);
184 assert_eq!(pieces.len(), 2, "the two cut pieces of the picture");
185 assert_eq!(pieces[0].start, 0, "placed relative to the compound's own start");
186 assert!(pieces.iter().all(|p| p.source == 0));
187 assert_eq!(pieces.iter().map(|p| p.frames()).sum::<u32>(), compound.frames());
188 let sound = s.lane_clips(Lane::A1)[1].clone();
189 assert_eq!(s.compound_pieces(LaneKind::Audio, &sound).len(), 2, "and the pieces of the sound");
190 assert!(s.compound_pieces(LaneKind::Video, &s.lane_clips(Lane::V1)[0].clone()).is_empty(), "a plain clip has none");
191}