| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | |
| 14 | use engine::project::{Edge, Lane, LaneKind, Speed, WallBound}; |
| 15 | use engine::scratch::Scratch; |
| 16 | use engine::{AudioSession, PlaybackSession}; |
| 17 | |
| 18 | fn asset(name: &str) -> PathBuf { |
| 19 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 20 | .join("../../assets") |
| 21 | .join(name) |
| 22 | } |
| 23 | |
| 24 | |
| 25 | fn placed(name: &str) -> PlaybackSession { |
| 26 | let mut s = PlaybackSession::open(asset("test_av.mp4")).expect("open the 30 fps scaffold"); |
| 27 | s.set_gain(0.0); |
| 28 | let file = asset(name); |
| 29 | s.import(&file).expect("the file joins the timeline"); |
| 30 | let end = s.timeline_duration(); |
| 31 | assert!( |
| 32 | s.place_stream_at(end, &file, 0, None).expect("the drag"), |
| 33 | "the file is placed" |
| 34 | ); |
| 35 | s |
| 36 | } |
| 37 | |
| 38 | |
| 39 | fn last(s: &PlaybackSession, lane: Lane) -> engine::Clip { |
| 40 | s.lane_clips(lane).last().expect("a clip was placed").clone() |
| 41 | } |
| 42 | |
| 43 | |
| 44 | |
| 45 | fn sound_frames(path: &Path, stream: usize) -> u32 { |
| 46 | let secs = AudioSession::stream_duration_secs(path, stream) |
| 47 | .expect("the stream reads") |
| 48 | .expect("it has a length"); |
| 49 | (secs * 30.0).ceil() as u32 |
| 50 | } |
| 51 | |
| 52 | |
| 53 | fn assert_within_sources(s: &PlaybackSession) { |
| 54 | assert_within_sources_for(s, ""); |
| 55 | } |
| 56 | |
| 57 | fn assert_within_sources_for(s: &PlaybackSession, case: &str) { |
| 58 | for lane in s.lanes() { |
| 59 | for (i, c) in s.lane_clips(lane).iter().enumerate() { |
| 60 | let source = &s.sources()[c.source]; |
| 61 | let wall = match lane.kind { |
| 62 | LaneKind::Audio => sound_frames(&source.path, source.audio_stream), |
| 63 | _ => s.file_frames(&source.path), |
| 64 | }; |
| 65 | assert!( |
| 66 | c.out_frame <= wall, |
| 67 | "{case}{lane:?} clip {i} ends at {} past its {wall} frames", |
| 68 | c.out_frame |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fn save_and_open(s: &PlaybackSession, out: &Path) -> PlaybackSession { |
| 75 | s.save_project(out).expect("the project saves"); |
| 76 | PlaybackSession::open_project(out).expect("the project it saved opens") |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn a_grouped_placement_is_held_to_the_sound_on_both_lanes() { |
| 81 | let s = placed("test_23976fps.mp4"); |
| 82 | let picture = last(&s, Lane::V1); |
| 83 | let sound = last(&s, Lane::A1); |
| 84 | assert_eq!(sound.out_frame, 60, "2.000 s of sound is 60 frames"); |
| 85 | assert_eq!(picture.out_frame, sound.out_frame, "one take, one length"); |
| 86 | assert_within_sources(&s); |
| 87 | |
| 88 | let out = Scratch::file("edith-frames-placed", "edith"); |
| 89 | let back = save_and_open(&s, &out); |
| 90 | assert!(back.load_warnings().is_empty(), "nothing needed clamping: {:?}", back.load_warnings()); |
| 91 | assert_within_sources(&back); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | #[test] |
| 97 | fn odd_speeds_round_trip_through_save_and_load() { |
| 98 | for name in ["test_23976fps.mp4", "test_25fps.mp4"] { |
| 99 | for permille in [333u16, 700, 1100] { |
| 100 | let mut s = placed(name); |
| 101 | let idx = s.lane_clips(Lane::V1).len() - 1; |
| 102 | s.set_speed(Lane::V1, idx, Speed::from_permille(permille)) |
| 103 | .expect("a clip at the end of the timeline can be re-timed"); |
| 104 | let before = last(&s, Lane::V1); |
| 105 | assert_eq!( |
| 106 | before.frames(), |
| 107 | Speed::from_permille(permille).frames(before.len()), |
| 108 | "the footprint is the speed's own arithmetic" |
| 109 | ); |
| 110 | assert_within_sources(&s); |
| 111 | |
| 112 | let out = Scratch::file(&format!("edith-frames-{permille}"), "edith"); |
| 113 | let back = save_and_open(&s, &out); |
| 114 | assert!( |
| 115 | back.load_warnings().is_empty(), |
| 116 | "{name} at {permille}: {:?}", |
| 117 | back.load_warnings() |
| 118 | ); |
| 119 | let after = last(&back, Lane::V1); |
| 120 | assert_eq!( |
| 121 | (after.in_frame, after.out_frame, after.speed), |
| 122 | (before.in_frame, before.out_frame, before.speed), |
| 123 | "{name} at {permille} reopens as it was saved" |
| 124 | ); |
| 125 | assert_within_sources(&back); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | #[test] |
| 133 | fn a_clip_past_its_source_is_clamped_on_load_and_reported() { |
| 134 | let s = placed("test_23976fps.mp4"); |
| 135 | let out = Scratch::file("edith-frames-overrun", "edith"); |
| 136 | s.save_project(&out).expect("saves"); |
| 137 | let text = std::fs::read_to_string(&*out).expect("read the saved file"); |
| 138 | let mut tampered = String::new(); |
| 139 | for line in text.lines() { |
| 140 | let fields: Vec<&str> = line.split(' ').collect(); |
| 141 | let mut fields: Vec<String> = fields.iter().map(|f| f.to_string()).collect(); |
| 142 | if fields.first().map(String::as_str) == Some("audio") |
| 143 | && fields.get(2).map(String::as_str) == Some("150") |
| 144 | && fields.len() > 4 |
| 145 | { |
| 146 | let out_frame: u32 = fields[4].parse().expect("a number"); |
| 147 | fields[4] = (out_frame + 2).to_string(); |
| 148 | } |
| 149 | tampered.push_str(&fields.join(" ")); |
| 150 | tampered.push('\n'); |
| 151 | } |
| 152 | std::fs::write(&*out, tampered).expect("write the tampered file"); |
| 153 | |
| 154 | let back = PlaybackSession::open_project(&out).expect("an overrun project still opens"); |
| 155 | assert_eq!(back.load_warnings().len(), 1, "one clamped clip: {:?}", back.load_warnings()); |
| 156 | assert!(back.load_warnings()[0].contains("clamped to 60")); |
| 157 | assert_eq!(last(&back, Lane::A1).out_frame, 60); |
| 158 | assert_within_sources(&back); |
| 159 | |
| 160 | |
| 161 | let again = Scratch::file("edith-frames-overrun-again", "edith"); |
| 162 | let reopened = save_and_open(&back, &again); |
| 163 | assert!(reopened.load_warnings().is_empty()); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | #[test] |
| 171 | fn a_trim_of_a_sped_clip_stops_on_the_frames_it_can_reach() { |
| 172 | let mut s = placed("test_23976fps.mp4"); |
| 173 | let idx = s.lane_clips(Lane::V1).len() - 1; |
| 174 | s.set_speed(Lane::V1, idx, Speed::from_permille(500)).expect("slow it"); |
| 175 | let start = last(&s, Lane::V1).start; |
| 176 | let end = last(&s, Lane::V1).end(); |
| 177 | let (lo, hi) = s.trim_room(Lane::V1, idx, Edge::End).expect("a clip"); |
| 178 | |
| 179 | |
| 180 | let picture = s.file_frames(&asset("test_23976fps.mp4")); |
| 181 | assert_eq!(hi, start + Speed::from_permille(500).frames(picture)); |
| 182 | assert!(hi >= end, "the picture may reach its own last frame"); |
| 183 | let walls = s.trim_walls(Lane::V1, idx, Edge::End).expect("a clip"); |
| 184 | assert!(walls.hi_media, "the source is the wall, not a neighbour"); |
| 185 | assert!(lo > start); |
| 186 | |
| 187 | |
| 188 | let odd = start + 2 * 10 + 1; |
| 189 | assert!(s.trim_clip(Lane::V1, idx, Edge::End, odd)); |
| 190 | let shorter = last(&s, Lane::V1); |
| 191 | assert!(shorter.end() <= odd, "snapped down, never past the request"); |
| 192 | assert_eq!((shorter.end() - start) % 2, 0, "half speed lands on even offsets"); |
| 193 | assert_within_sources(&s); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | #[test] |
| 202 | fn every_trim_wall_of_a_retimed_take_saves_and_reopens() { |
| 203 | let name = "test_23976fps.mp4"; |
| 204 | for lane in [Lane::V1, Lane::A1] { |
| 205 | for permille in [333u16, 770, 1000, 3900] { |
| 206 | let speed = Speed::from_permille(permille); |
| 207 | for edge in [Edge::End, Edge::Start] { |
| 208 | for shrink_first in [false, true] { |
| 209 | for toward_hi in [false, true] { |
| 210 | let case = format!("{lane:?} {permille} {edge:?} shrink={shrink_first} hi={toward_hi}"); |
| 211 | let mut s = placed(name); |
| 212 | let idx = s.lane_clips(lane).len() - 1; |
| 213 | s.set_speed(lane, idx, speed) |
| 214 | .unwrap_or_else(|e| panic!("{case}: retime: {e}")); |
| 215 | assert_within_sources_for(&s, &format!("{case}: ")); |
| 216 | |
| 217 | let walls = s.trim_walls(lane, idx, edge).expect("a clip"); |
| 218 | if shrink_first { |
| 219 | |
| 220 | let inner = if edge == Edge::Start { walls.hi } else { walls.lo }; |
| 221 | s.trim_clip(lane, idx, edge, inner); |
| 222 | assert_within_sources_for(&s, &format!("{case}: ")); |
| 223 | } |
| 224 | let target = if toward_hi { walls.hi } else { walls.lo }; |
| 225 | s.trim_clip(lane, idx, edge, target); |
| 226 | assert_within_sources_for(&s, &format!("{case}: ")); |
| 227 | |
| 228 | let landed = match edge { |
| 229 | Edge::End => last(&s, lane).end(), |
| 230 | Edge::Start => last(&s, lane).start, |
| 231 | }; |
| 232 | assert_eq!(landed, target, "{case}: the edge lands on the wall it was dragged to"); |
| 233 | |
| 234 | let out = Scratch::file(&format!("edith-sweep-{lane:?}-{permille}-{edge:?}-{shrink_first}-{toward_hi}"), "edith"); |
| 235 | let before = last(&s, lane); |
| 236 | let back = save_and_open(&s, &out); |
| 237 | assert!( |
| 238 | back.load_warnings().is_empty(), |
| 239 | "{case}: reopened with clamps: {:?}", |
| 240 | back.load_warnings() |
| 241 | ); |
| 242 | assert_eq!( |
| 243 | last(&back, lane).out_frame, |
| 244 | before.out_frame, |
| 245 | "{case}: reopens as saved" |
| 246 | ); |
| 247 | assert_within_sources_for(&back, &format!("{case} reopened: ")); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | #[test] |
| 260 | fn trim_bounds_name_the_wall_that_stops_the_edge() { |
| 261 | let file = asset("test_23976fps.mp4"); |
| 262 | let mut s = placed("test_23976fps.mp4"); |
| 263 | let idx = s.lane_clips(Lane::V1).len() - 1; |
| 264 | let end = s.lane_clips(Lane::V1)[idx].end(); |
| 265 | |
| 266 | |
| 267 | |
| 268 | let later = end as f64 / 30.0 + 10.0; |
| 269 | assert!(s.place_stream_at(later, &file, 0, None).expect("the drag")); |
| 270 | let (lo, hi) = s.trim_bounds(Lane::V1, idx, Edge::End).expect("a clip"); |
| 271 | assert_eq!(hi, WallBound::Source, "the source's end, not the clip ten seconds away"); |
| 272 | assert_eq!(lo, WallBound::MinLength); |
| 273 | let walls = s.trim_walls(Lane::V1, idx, Edge::End).expect("a clip"); |
| 274 | assert!(walls.hi_media && walls.hi > end, "the tail is past the clip's end"); |
| 275 | |
| 276 | assert!(s.trim_clip(Lane::V1, idx, Edge::End, walls.hi + 500)); |
| 277 | assert_eq!(s.lane_clips(Lane::V1)[idx].end(), walls.hi, "lands on the source's end"); |
| 278 | assert_within_sources(&s); |
| 279 | let (_, hi_after) = s.trim_bounds(Lane::V1, idx, Edge::End).expect("a clip"); |
| 280 | assert_eq!(hi_after, WallBound::Source); |
| 281 | |
| 282 | |
| 283 | |
| 284 | let mut t = placed("test_23976fps.mp4"); |
| 285 | let j = t.lane_clips(Lane::V1).len() - 1; |
| 286 | let s0 = last(&t, Lane::V1).start; |
| 287 | assert!(t.trim_clip(Lane::V1, j, Edge::End, s0 + 30)); |
| 288 | let cut = s0 + 30; |
| 289 | assert!(t.place_stream_at(cut as f64 / 30.0, &file, 0, None).expect("the drag")); |
| 290 | let (_, hi) = t.trim_bounds(Lane::V1, j, Edge::End).expect("a clip"); |
| 291 | assert_eq!(hi, WallBound::Neighbour(cut), "the clip at frame {cut} sets the wall"); |
| 292 | } |