| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | use std::path::PathBuf; |
| 17 | |
| 18 | use engine::PlaybackSession; |
| 19 | use engine::project::{Lane, Title}; |
| 20 | use engine::text::TextStyle; |
| 21 | |
| 22 | fn asset(name: &str) -> PathBuf { |
| 23 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 24 | .join("../../assets") |
| 25 | .join(name) |
| 26 | } |
| 27 | |
| 28 | |
| 29 | type LaneClips = Vec<(u32, u32, u32, u32, u32, u32, u32)>; |
| 30 | |
| 31 | |
| 32 | #[derive(Debug, PartialEq, Clone)] |
| 33 | struct Shape { |
| 34 | lanes: Vec<(Lane, LaneClips)>, |
| 35 | titles: Vec<(u32, u32, u32, u32)>, |
| 36 | } |
| 37 | |
| 38 | fn shape(s: &PlaybackSession) -> Shape { |
| 39 | let lanes = s |
| 40 | .lanes() |
| 41 | .into_iter() |
| 42 | .filter(|l| l.kind != engine::project::LaneKind::Subtitle) |
| 43 | .map(|lane| { |
| 44 | let clips = s |
| 45 | .lane_clips(lane) |
| 46 | .iter() |
| 47 | .map(|c| { |
| 48 | ( |
| 49 | c.start, |
| 50 | c.frames(), |
| 51 | c.in_frame, |
| 52 | c.transition_out, |
| 53 | c.fade_in, |
| 54 | c.fade_out, |
| 55 | c.out_frame, |
| 56 | ) |
| 57 | }) |
| 58 | .collect(); |
| 59 | (lane, clips) |
| 60 | }) |
| 61 | .collect(); |
| 62 | let titles = s |
| 63 | .titles() |
| 64 | .iter() |
| 65 | .map(|t: &Title| (t.start, t.frames, t.fade_in, t.fade_out)) |
| 66 | .collect(); |
| 67 | Shape { lanes, titles } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | fn edit() -> PlaybackSession { |
| 72 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 73 | s.set_gain(0.0); |
| 74 | assert!(s.cut_at(0.5), "cut at frame 15"); |
| 75 | assert!(s.cut_at(2.1), "cut at frame 63"); |
| 76 | assert!(s.cut_at(3.7), "cut at frame 111"); |
| 77 | assert!(s.set_transition_out(Lane::V1, 0, 15), "a 15-frame dissolve"); |
| 78 | assert!(s.set_fade_in(Lane::V1, 1, 7), "a 7-frame fade in"); |
| 79 | assert!(s.set_fade_out(Lane::A1, 2, 9), "a 9-frame fade out"); |
| 80 | s.add_title(Title { |
| 81 | start: 45, |
| 82 | frames: 50, |
| 83 | text: "T".into(), |
| 84 | style: TextStyle::default(), |
| 85 | fade_in: 9, |
| 86 | fade_out: 6, |
| 87 | in_anim: Default::default(), |
| 88 | out_anim: Default::default(), |
| 89 | anim_frames: 0, |
| 90 | keys: Vec::new(), |
| 91 | }) |
| 92 | .expect("title"); |
| 93 | s |
| 94 | } |
| 95 | |
| 96 | |
| 97 | fn within(a: &Shape, b: &Shape, tol: u32) -> Result<(), String> { |
| 98 | if a.lanes.len() != b.lanes.len() || a.titles.len() != b.titles.len() { |
| 99 | return Err(format!("shape changed: {a:?} vs {b:?}")); |
| 100 | } |
| 101 | for ((la, ca), (lb, cb)) in a.lanes.iter().zip(&b.lanes) { |
| 102 | if la != lb || ca.len() != cb.len() { |
| 103 | return Err(format!("lane {la:?} changed: {ca:?} vs {cb:?}")); |
| 104 | } |
| 105 | for (x, y) in ca.iter().zip(cb) { |
| 106 | let d = [ |
| 107 | x.0.abs_diff(y.0), |
| 108 | x.1.abs_diff(y.1), |
| 109 | x.2.abs_diff(y.2), |
| 110 | x.3.abs_diff(y.3), |
| 111 | x.4.abs_diff(y.4), |
| 112 | x.5.abs_diff(y.5), |
| 113 | ]; |
| 114 | if d.iter().any(|&d| d > tol) { |
| 115 | return Err(format!("{la:?} clip {x:?} came back {y:?}")); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | for (x, y) in a.titles.iter().zip(&b.titles) { |
| 120 | let d = [ |
| 121 | x.0.abs_diff(y.0), |
| 122 | x.1.abs_diff(y.1), |
| 123 | x.2.abs_diff(y.2), |
| 124 | x.3.abs_diff(y.3), |
| 125 | ]; |
| 126 | if d.iter().any(|&d| d > tol) { |
| 127 | return Err(format!("title {x:?} came back {y:?}")); |
| 128 | } |
| 129 | } |
| 130 | Ok(()) |
| 131 | } |
| 132 | |
| 133 | fn round_trip(via: f64) -> (Shape, Shape) { |
| 134 | let mut s = edit(); |
| 135 | let before = shape(&s); |
| 136 | assert!(s.set_frame_rate(via), "to {via}"); |
| 137 | assert!(s.set_frame_rate(30.0), "back to 30"); |
| 138 | (before, shape(&s)) |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn a_30_25_30_round_trip_lands_within_a_frame() { |
| 143 | let (before, after) = round_trip(25.0); |
| 144 | within(&before, &after, 1).expect("within one frame"); |
| 145 | } |
| 146 | |
| 147 | #[test] |
| 148 | fn a_30_24_30_round_trip_lands_within_a_frame() { |
| 149 | let (before, after) = round_trip(24.0); |
| 150 | within(&before, &after, 1).expect("within one frame"); |
| 151 | } |
| 152 | |
| 153 | #[test] |
| 154 | fn audio_clips_keep_their_source_length_through_a_round_trip() { |
| 155 | let (before, after) = round_trip(25.0); |
| 156 | for (lane, clips) in &before.lanes { |
| 157 | if lane.kind != engine::project::LaneKind::Audio { |
| 158 | continue; |
| 159 | } |
| 160 | let back = after.lanes.iter().find(|(l, _)| l == lane).expect("lane"); |
| 161 | for (x, y) in clips.iter().zip(&back.1) { |
| 162 | assert!( |
| 163 | x.1.abs_diff(y.1) <= 1, |
| 164 | "audio clip {x:?} came back {y:?}: more than a frame" |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | fn on_grid_edit() -> PlaybackSession { |
| 174 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open"); |
| 175 | s.set_gain(0.0); |
| 176 | assert!(s.cut_at(1.0), "cut at frame 30"); |
| 177 | assert!(s.cut_at(3.0), "cut at frame 90"); |
| 178 | assert!(s.cut_at(4.0), "cut at frame 120"); |
| 179 | assert!(s.set_transition_out(Lane::V1, 0, 30), "a 30-frame dissolve"); |
| 180 | assert!(s.set_fade_in(Lane::V1, 1, 30), "a 30-frame fade in"); |
| 181 | assert!(s.set_fade_out(Lane::A1, 2, 30), "a 30-frame fade out"); |
| 182 | s.add_title(Title { |
| 183 | start: 60, |
| 184 | frames: 60, |
| 185 | text: "T".into(), |
| 186 | style: TextStyle::default(), |
| 187 | fade_in: 30, |
| 188 | fade_out: 30, |
| 189 | in_anim: Default::default(), |
| 190 | out_anim: Default::default(), |
| 191 | anim_frames: 0, |
| 192 | keys: Vec::new(), |
| 193 | }) |
| 194 | .expect("title"); |
| 195 | s |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn an_on_grid_edit_comes_back_exactly_through_25_and_24() { |
| 200 | for via in [25.0, 24.0] { |
| 201 | let mut s = on_grid_edit(); |
| 202 | let before = shape(&s); |
| 203 | assert!(s.set_frame_rate(via)); |
| 204 | assert!(s.set_frame_rate(30.0)); |
| 205 | let after = shape(&s); |
| 206 | assert_eq!(before, after, "30 -> {via} -> 30 is exact on the grid"); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn a_second_round_trip_does_not_drift() { |
| 212 | let mut s = edit(); |
| 213 | assert!(s.set_frame_rate(25.0)); |
| 214 | assert!(s.set_frame_rate(30.0)); |
| 215 | let once = shape(&s); |
| 216 | assert!(s.set_frame_rate(25.0)); |
| 217 | assert!(s.set_frame_rate(30.0)); |
| 218 | assert_eq!(shape(&s), once, "a second 30 -> 25 -> 30 lands where the first did"); |
| 219 | } |