| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use engine::PlaybackSession; |
| 13 | use engine::project::{Lane, Source}; |
| 14 | use engine::scratch::Scratch; |
| 15 | |
| 16 | fn asset(name: &str) -> PathBuf { |
| 17 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 18 | .join("../../assets") |
| 19 | .join(name) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | fn scratch(name: &str) -> Scratch { |
| 24 | Scratch::dir(&format!("ve_saveload_{name}")) |
| 25 | } |
| 26 | |
| 27 | fn copy_in(dir: &Path, name: &str) -> PathBuf { |
| 28 | let to = dir.join(name); |
| 29 | std::fs::copy(asset(name), &to).expect("copy the fixture"); |
| 30 | to |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | #[test] |
| 39 | fn a_picked_audio_stream_lands_saves_and_comes_back() { |
| 40 | let dir = scratch("streams"); |
| 41 | let media = copy_in(&dir, "test_multilang.mp4"); |
| 42 | let mut session = PlaybackSession::open(&media).expect("open the fixture"); |
| 43 | session.set_gain(0.0); |
| 44 | let end = session.timeline_duration(); |
| 45 | |
| 46 | |
| 47 | |
| 48 | assert!( |
| 49 | session |
| 50 | .place_stream_at(end, &media, 1, None) |
| 51 | .expect("stream 1 matches the timeline") |
| 52 | ); |
| 53 | assert_eq!( |
| 54 | session |
| 55 | .sources() |
| 56 | .iter() |
| 57 | .map(|s| s.audio_stream) |
| 58 | .collect::<Vec<_>>(), |
| 59 | [0, 1], |
| 60 | "the same file twice, on two streams" |
| 61 | ); |
| 62 | assert_eq!(session.timeline_duration(), end * 2.0); |
| 63 | |
| 64 | let path = dir.join("langs.edith"); |
| 65 | session.save_project(&path).expect("save"); |
| 66 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 67 | assert!( |
| 68 | text.contains("source 0 test_multilang.mp4") |
| 69 | && text.contains("source 1 test_multilang.mp4"), |
| 70 | "the streams did not reach the file:\n{text}" |
| 71 | ); |
| 72 | |
| 73 | let loaded = PlaybackSession::open_project(&path).expect("reopen the project"); |
| 74 | assert_eq!(loaded.sources(), session.sources()); |
| 75 | assert_eq!(loaded.timeline_duration(), session.timeline_duration()); |
| 76 | |
| 77 | |
| 78 | |
| 79 | let other = copy_in(&dir, "test_multiaudio.mp4"); |
| 80 | let mut session = PlaybackSession::open(&other).expect("open the fixture"); |
| 81 | session.set_gain(0.0); |
| 82 | |
| 83 | |
| 84 | let err = session |
| 85 | .place_stream_at(0.0, &other, 1, None) |
| 86 | .expect_err("a mono stream cannot join a stereo timeline") |
| 87 | .to_string(); |
| 88 | assert_eq!(err, "audio 1 ch does not match the timeline's 2 ch"); |
| 89 | assert!( |
| 90 | session |
| 91 | .place_stream_at(0.0, &media, 1, None) |
| 92 | .expect_err("a file that is not a source") |
| 93 | .to_string() |
| 94 | .contains("not on this timeline") |
| 95 | ); |
| 96 | assert_eq!(session.sources().len(), 1, "a refusal added a source"); |
| 97 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | #[test] |
| 105 | fn both_proxy_switches_survive_a_save_and_a_reopen() { |
| 106 | let dir = scratch("proxyswitches"); |
| 107 | let media = copy_in(&dir, "test_av.mp4"); |
| 108 | let mut session = PlaybackSession::open(&media).expect("open the fixture"); |
| 109 | session.set_gain(0.0); |
| 110 | assert!(session.auto_proxies(), "a fresh project makes them by itself"); |
| 111 | assert!(!session.proxies(), "and is cut on the film itself"); |
| 112 | |
| 113 | session.set_proxies(true); |
| 114 | session.set_auto_proxies(false); |
| 115 | let path = dir.join("switches.edith"); |
| 116 | session.save_project(&path).expect("save"); |
| 117 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 118 | assert!(text.contains("proxy on\nautoproxy off\n"), "{text}"); |
| 119 | |
| 120 | let loaded = PlaybackSession::open_project(&path).expect("reopen"); |
| 121 | assert!(loaded.proxies(), "cut on the stand-ins came back"); |
| 122 | assert!(!loaded.auto_proxies(), "and so did making none of them"); |
| 123 | |
| 124 | |
| 125 | session.set_auto_proxies(true); |
| 126 | session.save_project(&path).expect("save"); |
| 127 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 128 | assert!(!text.contains("autoproxy"), "on is written by saying nothing"); |
| 129 | assert!( |
| 130 | PlaybackSession::open_project(&path) |
| 131 | .expect("reopen") |
| 132 | .auto_proxies() |
| 133 | ); |
| 134 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[test] |
| 142 | fn the_encoder_seat_survives_a_save_and_a_reopen() { |
| 143 | use engine::export::EncoderSeat; |
| 144 | |
| 145 | let dir = scratch("encoderseat"); |
| 146 | let media = copy_in(&dir, "test_av.mp4"); |
| 147 | let mut session = PlaybackSession::open(&media).expect("open the fixture"); |
| 148 | session.set_gain(0.0); |
| 149 | assert_eq!( |
| 150 | session.encoder_seat(), |
| 151 | EncoderSeat::Auto, |
| 152 | "a fresh project takes the seat this machine has" |
| 153 | ); |
| 154 | |
| 155 | let path = dir.join("seat.edith"); |
| 156 | for seat in [EncoderSeat::Software, EncoderSeat::Hardware] { |
| 157 | session.set_encoder_seat(seat); |
| 158 | session.save_project(&path).expect("save"); |
| 159 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 160 | assert!(text.contains(&format!("encoder {}\n", seat.name())), "{text}"); |
| 161 | assert_eq!( |
| 162 | PlaybackSession::open_project(&path) |
| 163 | .expect("reopen") |
| 164 | .encoder_seat(), |
| 165 | seat |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | session.set_encoder_seat(EncoderSeat::Auto); |
| 171 | session.save_project(&path).expect("save"); |
| 172 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 173 | assert!(!text.contains("encoder"), "auto is written by saying nothing"); |
| 174 | assert_eq!( |
| 175 | PlaybackSession::open_project(&path) |
| 176 | .expect("reopen") |
| 177 | .encoder_seat(), |
| 178 | EncoderSeat::Auto |
| 179 | ); |
| 180 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | #[test] |
| 188 | fn a_trimmed_timeline_saves_and_opens_again() { |
| 189 | use engine::project::Edge; |
| 190 | |
| 191 | let dir = scratch("trim"); |
| 192 | let media = copy_in(&dir, "test_av.mp4"); |
| 193 | let mut session = PlaybackSession::open(&media).expect("open the fixture"); |
| 194 | session.set_gain(0.0); |
| 195 | let whole = session.clip_at(0).expect("one clip"); |
| 196 | |
| 197 | assert!(session.trim_clip(Lane::V1, 0, Edge::End, whole.end() / 2)); |
| 198 | assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 5)); |
| 199 | let path = dir.join("trimmed.edith"); |
| 200 | session.save_project(&path).expect("save"); |
| 201 | let loaded = PlaybackSession::open_project(&path).expect("a trimmed project opens"); |
| 202 | assert_eq!( |
| 203 | loaded |
| 204 | .clip_at(0) |
| 205 | .map(|c| (c.start, c.in_frame, c.out_frame)), |
| 206 | Some((5, 5, whole.end() / 2)), |
| 207 | "the trimmed range is what came back" |
| 208 | ); |
| 209 | |
| 210 | |
| 211 | |
| 212 | assert!(session.trim_clip(Lane::V1, 0, Edge::End, u32::MAX)); |
| 213 | assert_eq!( |
| 214 | session.clip_at(0).expect("still one clip").out_frame, |
| 215 | whole.out_frame, |
| 216 | "as far as the file goes and not one frame further" |
| 217 | ); |
| 218 | session.save_project(&path).expect("save"); |
| 219 | PlaybackSession::open_project(&path).expect("and it still opens"); |
| 220 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | fn edited(dir: &Path) -> PlaybackSession { |
| 226 | let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open"); |
| 227 | |
| 228 | |
| 229 | let second = copy_in(dir, "test_av2.mp4"); |
| 230 | session.import(&second).expect("import the second file"); |
| 231 | let end = session.timeline_duration(); |
| 232 | assert!( |
| 233 | session |
| 234 | .place_stream_at(end, &second, 0, None) |
| 235 | .expect("a file just imported is on this timeline"), |
| 236 | "drag it onto the end" |
| 237 | ); |
| 238 | assert!(session.cut_at(2.0), "cut inside the first file"); |
| 239 | assert!(session.cut_at(6.5), "cut inside the second file"); |
| 240 | assert!( |
| 241 | session.delete_clip(Lane::V1, 1), |
| 242 | "drop the second half of the first" |
| 243 | ); |
| 244 | session.seek(3.0); |
| 245 | assert_eq!(session.timeline_duration(), 6.0); |
| 246 | session |
| 247 | } |
| 248 | |
| 249 | |
| 250 | fn shape(session: &PlaybackSession) -> (Vec<(f64, f64, usize)>, Vec<Source>, f64) { |
| 251 | ( |
| 252 | session.clip_spans_by_source(), |
| 253 | session.sources().to_vec(), |
| 254 | session.timeline_duration(), |
| 255 | ) |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | #[test] |
| 262 | fn a_version_1_project_loads_fully_grouped_and_saves_as_version_7() { |
| 263 | let dir = scratch("v1"); |
| 264 | copy_in(&dir, "test_av.mp4"); |
| 265 | let path = dir.join("old.edith"); |
| 266 | |
| 267 | std::fs::write( |
| 268 | &path, |
| 269 | "edith 1\nplayhead 45\nsource test_av.mp4\nclip 0 30 0\nclip 60 90 0\n", |
| 270 | ) |
| 271 | .expect("write a v1 file"); |
| 272 | |
| 273 | let loaded = PlaybackSession::open_project(&path).expect("a v1 file still opens"); |
| 274 | let fps = loaded.meta().frame_rate; |
| 275 | assert_eq!( |
| 276 | loaded.clip_spans_by_source(), |
| 277 | vec![(0.0, 30.0 / fps, 0), (30.0 / fps, 30.0 / fps, 0)], |
| 278 | "v1 clips queue up, the second starting where the first ended" |
| 279 | ); |
| 280 | assert_eq!( |
| 281 | loaded.lane_spans_by_source(engine::project::Lane::A1), |
| 282 | loaded.clip_spans_by_source(), |
| 283 | "both lanes, fully grouped: a v1 timeline had no holes" |
| 284 | ); |
| 285 | assert!( |
| 286 | (loaded.now() - 45.0 / fps).abs() < 1.0 / fps, |
| 287 | "playhead kept" |
| 288 | ); |
| 289 | |
| 290 | |
| 291 | let v2 = dir.join("new.edith"); |
| 292 | loaded.save_project(&v2).expect("save"); |
| 293 | let text = std::fs::read_to_string(&v2).expect("read back"); |
| 294 | assert!(text.starts_with("edith 20\n"), "{text}"); |
| 295 | assert!( |
| 296 | text.contains("\nresolution 1280 720\n"), |
| 297 | "a project with no resolution of its own is saved at source 0's: {text}" |
| 298 | ); |
| 299 | assert_eq!( |
| 300 | text.lines().filter(|l| l.starts_with("video ")).count(), |
| 301 | 2, |
| 302 | "{text}" |
| 303 | ); |
| 304 | assert_eq!( |
| 305 | text.lines().filter(|l| l.starts_with("audio ")).count(), |
| 306 | 2, |
| 307 | "{text}" |
| 308 | ); |
| 309 | let again = PlaybackSession::open_project(&v2).expect("open the v2 file"); |
| 310 | assert_eq!(shape(&again), shape(&loaded)); |
| 311 | |
| 312 | let third = dir.join("third.edith"); |
| 313 | again.save_project(&third).expect("save"); |
| 314 | assert_eq!(std::fs::read(&third).expect("read"), text.as_bytes()); |
| 315 | |
| 316 | |
| 317 | |
| 318 | let v2 = dir.join("v2.edith"); |
| 319 | std::fs::write( |
| 320 | &v2, |
| 321 | "edith 2\nplayhead 0\nsource test_av.mp4\nvideo 0 0 30 0 0\naudio 0 0 30 0 0\n", |
| 322 | ) |
| 323 | .expect("write a v2 file"); |
| 324 | let loaded = PlaybackSession::open_project(&v2).expect("a v2 file still opens"); |
| 325 | assert_eq!(loaded.sources().len(), 1); |
| 326 | assert_eq!(loaded.sources()[0].audio_stream, 0); |
| 327 | loaded.save_project(&v2).expect("save"); |
| 328 | assert!( |
| 329 | std::fs::read_to_string(&v2) |
| 330 | .expect("read back") |
| 331 | .contains("source 0 test_av.mp4"), |
| 332 | "a re-saved v2 project says the stream it always meant" |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | #[test] |
| 341 | fn a_version_4_project_holds_more_than_two_lanes() { |
| 342 | use engine::project::{Lane, LaneKind}; |
| 343 | |
| 344 | let dir = scratch("v4_lanes"); |
| 345 | copy_in(&dir, "test_av.mp4"); |
| 346 | let path = dir.join("many.edith"); |
| 347 | |
| 348 | |
| 349 | |
| 350 | let text = "edith 4\nplayhead 0\nsource 0 test_av.mp4\n\ |
| 351 | video 1 0 0 30 0 3\naudio 1\nvideo 2 40 0 20 0 -\naudio 2 0 0 30 0 3\n"; |
| 352 | std::fs::write(&path, text).expect("write a v4 file"); |
| 353 | |
| 354 | let loaded = PlaybackSession::open_project(&path).expect("a four-lane project opens"); |
| 355 | let v2 = Lane::new(LaneKind::Video, 1); |
| 356 | let a2 = Lane::new(LaneKind::Audio, 1); |
| 357 | assert_eq!(loaded.lane_clips(Lane::V1).len(), 1); |
| 358 | assert!(loaded.lane_clips(Lane::A1).is_empty(), "A1 holds nothing"); |
| 359 | assert_eq!( |
| 360 | loaded |
| 361 | .lane_clips(v2) |
| 362 | .iter() |
| 363 | .map(|c| (c.start, c.end())) |
| 364 | .collect::<Vec<_>>(), |
| 365 | vec![(40, 60)], |
| 366 | "V2 is a lane of its own, gap and all" |
| 367 | ); |
| 368 | assert_eq!( |
| 369 | loaded.lane_clips(a2)[0].link, |
| 370 | loaded.lane_clips(Lane::V1)[0].link, |
| 371 | "the group spans V1 and A2, which are not a pair" |
| 372 | ); |
| 373 | assert_eq!(loaded.timeline_duration(), 60.0 / loaded.meta().frame_rate); |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | let again = dir.join("again.edith"); |
| 379 | loaded.save_project(&again).expect("save"); |
| 380 | assert_eq!( |
| 381 | std::fs::read_to_string(&again).expect("read back"), |
| 382 | "edith 16\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ |
| 383 | video 1 0 0 30 0 3 - - fit 1000\naudio 1\n\ |
| 384 | video 2 40 0 20 0 - - - fit 1000\naudio 2 0 0 30 0 3 - - fit 1000\n", |
| 385 | "a four-lane project is written as it was read, three versions on" |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | #[test] |
| 394 | fn a_version_5_project_carries_per_clip_equalizers() { |
| 395 | let dir = scratch("v5_eq"); |
| 396 | copy_in(&dir, "test_av.mp4"); |
| 397 | let path = dir.join("eq.edith"); |
| 398 | |
| 399 | let text = "edith 5\nplayhead 0\nsource 0 test_av.mp4\n\ |
| 400 | eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ |
| 401 | eq 12000.0:6.25:0.5:hs\n\ |
| 402 | video 1 0 0 30 0 - 0\nvideo 1 30 30 60 0 - 1\n\ |
| 403 | audio 1 0 0 30 0 - 0\naudio 1 30 30 60 0 - -\n"; |
| 404 | std::fs::write(&path, text).expect("write a v5 file"); |
| 405 | |
| 406 | let loaded = PlaybackSession::open_project(&path).expect("a v5 project opens"); |
| 407 | assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2); |
| 408 | let again = dir.join("again.edith"); |
| 409 | loaded.save_project(&again).expect("save"); |
| 410 | assert_eq!( |
| 411 | std::fs::read_to_string(&again).expect("read back"), |
| 412 | "edith 16\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ |
| 413 | eq 80.0:-3.0:0.707:ls 1000.0:4.5:1.0:pk\n\ |
| 414 | eq 12000.0:6.25:0.5:hs\n\ |
| 415 | video 1 0 0 30 0 - 0 - fit 1000\nvideo 1 30 30 60 0 - 1 - fit 1000\n\ |
| 416 | audio 1 0 0 30 0 - 0 - fit 1000\naudio 1 30 30 60 0 - - - fit 1000\n", |
| 417 | "the equalizer table and every clip's index survive a round trip" |
| 418 | ); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | #[test] |
| 427 | fn a_version_6_project_carries_per_clip_colours() { |
| 428 | let dir = scratch("v6_color"); |
| 429 | copy_in(&dir, "test_av.mp4"); |
| 430 | let path = dir.join("color.edith"); |
| 431 | |
| 432 | |
| 433 | let text = "edith 6\nplayhead 0\nsource 0 test_av.mp4\n\ |
| 434 | eq 80.0:-3.0:0.707:ls\n\ |
| 435 | color 0.1:1.2:0.9:-0.3\n\ |
| 436 | color -0.25:1.0:0.0:0.5\n\ |
| 437 | video 1 0 0 30 0 - 0 0\nvideo 1 30 30 60 0 - - 1\n\ |
| 438 | audio 1 0 0 30 0 - - 0\naudio 1 30 30 60 0 - - -\n"; |
| 439 | std::fs::write(&path, text).expect("write a v6 file"); |
| 440 | |
| 441 | let loaded = PlaybackSession::open_project(&path).expect("a v6 project opens"); |
| 442 | assert_eq!(loaded.lane_clips(engine::project::Lane::V1).len(), 2); |
| 443 | let again = dir.join("again.edith"); |
| 444 | loaded.save_project(&again).expect("save"); |
| 445 | assert_eq!( |
| 446 | std::fs::read_to_string(&again).expect("read back"), |
| 447 | "edith 16\nplayhead 0\nresolution 1280 720\nfps 30.0\nsource 0 test_av.mp4\n\ |
| 448 | eq 80.0:-3.0:0.707:ls\n\ |
| 449 | color 0.1:1.2:0.9:-0.3\n\ |
| 450 | color -0.25:1.0:0.0:0.5\n\ |
| 451 | video 1 0 0 30 0 - 0 0 fit 1000\nvideo 1 30 30 60 0 - - 1 fit 1000\n\ |
| 452 | audio 1 0 0 30 0 - - 0 fit 1000\naudio 1 30 30 60 0 - - - fit 1000\n", |
| 453 | "the colour table and every clip's index survive a round trip" |
| 454 | ); |
| 455 | |
| 456 | |
| 457 | assert_eq!(loaded.resolution(), (1280, 720)); |
| 458 | assert_eq!( |
| 459 | loaded.fit_of(engine::project::Lane::V1, 0), |
| 460 | engine::scale::FitPolicy::Fit |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | #[test] |
| 469 | fn a_version_7_project_carries_a_resolution_and_fit_policies() { |
| 470 | let dir = scratch("v7_resolution"); |
| 471 | copy_in(&dir, "test_av.mp4"); |
| 472 | let path = dir.join("mixed.edith"); |
| 473 | let text = "edith 7\nplayhead 0\nresolution 960 720\nsource 0 test_av.mp4\n\ |
| 474 | video 1 0 0 30 0 - - - fill\nvideo 1 30 30 60 0 - - - center\n\ |
| 475 | audio 1 0 0 60 0 - - - fit\n"; |
| 476 | std::fs::write(&path, text).expect("write a v7 file"); |
| 477 | |
| 478 | let loaded = PlaybackSession::open_project(&path).expect("a v7 project opens"); |
| 479 | assert_eq!( |
| 480 | loaded.resolution(), |
| 481 | (960, 720), |
| 482 | "the project's own size, which is not the media's 1280x720" |
| 483 | ); |
| 484 | assert_eq!( |
| 485 | loaded.native_resolution(), |
| 486 | (1280, 720), |
| 487 | "...and the media's own size is still known" |
| 488 | ); |
| 489 | let v1 = engine::project::Lane::V1; |
| 490 | assert_eq!(loaded.fit_of(v1, 0), engine::scale::FitPolicy::Fill); |
| 491 | assert_eq!(loaded.fit_of(v1, 1), engine::scale::FitPolicy::Center); |
| 492 | |
| 493 | let again = dir.join("again.edith"); |
| 494 | loaded.save_project(&again).expect("save"); |
| 495 | assert_eq!( |
| 496 | std::fs::read_to_string(&again).expect("read back"), |
| 497 | "edith 16\nplayhead 0\nresolution 960 720\nfps 30.0\nsource 0 test_av.mp4\n\ |
| 498 | video 1 0 0 30 0 - - - fill 1000\nvideo 1 30 30 60 0 - - - center 1000\n\ |
| 499 | audio 1 0 0 60 0 - - - fit 1000\n", |
| 500 | "a v7 project is written back as the v8 it now is: the same clips, each \ |
| 501 | saying it plays at real time, which is what it always played at" |
| 502 | ); |
| 503 | |
| 504 | |
| 505 | for (bad, want) in [ |
| 506 | ( |
| 507 | "edith 7\nresolution 0 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n", |
| 508 | "0x720 is not a picture", |
| 509 | ), |
| 510 | ( |
| 511 | "edith 7\nresolution 960 720\nresolution 960 720\nsource 0 test_av.mp4\n\ |
| 512 | video 1 0 0 30 0 - - - fit\n", |
| 513 | "resolution belongs once", |
| 514 | ), |
| 515 | |
| 516 | |
| 517 | ( |
| 518 | "edith 7\nresolution 4294967295 4294967295\nsource 0 test_av.mp4\n\ |
| 519 | video 1 0 0 30 0 - - - fit\n", |
| 520 | "4294967295x4294967295 is not a picture", |
| 521 | ), |
| 522 | ( |
| 523 | "edith 7\nresolution 7681 1080\nsource 0 test_av.mp4\n\ |
| 524 | video 1 0 0 30 0 - - - fit\n", |
| 525 | "7681x1080 is not a picture", |
| 526 | ), |
| 527 | ( |
| 528 | "edith 7\nresolution 960 720\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - squish\n", |
| 529 | "not a fit policy", |
| 530 | ), |
| 531 | ] { |
| 532 | let path = dir.join("bad.edith"); |
| 533 | std::fs::write(&path, bad).expect("write"); |
| 534 | let err = PlaybackSession::open_project(&path) |
| 535 | .err() |
| 536 | .expect("a malformed v7 file must be refused") |
| 537 | .to_string(); |
| 538 | assert!(err.contains(want), "wanted {want:?}, got {err:?}"); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | #[test] |
| 543 | fn a_saved_project_reopens_as_the_same_timeline() { |
| 544 | let dir = scratch("round_trip"); |
| 545 | let saved = edited(&dir); |
| 546 | let path = dir.join("edit.edith"); |
| 547 | saved.save_project(&path).expect("save"); |
| 548 | |
| 549 | let mut loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 550 | assert_eq!(shape(&loaded), shape(&saved), "clips, sources and duration"); |
| 551 | assert!( |
| 552 | (loaded.now() - saved.now()).abs() < 1.0 / loaded.meta().frame_rate, |
| 553 | "playhead landed at {:.3}s, saved at {:.3}s", |
| 554 | loaded.now(), |
| 555 | saved.now() |
| 556 | ); |
| 557 | assert!(!loaded.is_playing(), "a loaded project starts paused"); |
| 558 | assert!(!loaded.undo(), "history is not saved"); |
| 559 | |
| 560 | |
| 561 | |
| 562 | let again = dir.join("again.edith"); |
| 563 | loaded |
| 564 | .save_project(&again) |
| 565 | .expect("save the loaded project"); |
| 566 | assert_eq!( |
| 567 | std::fs::read(&path).unwrap(), |
| 568 | std::fs::read(&again).unwrap(), |
| 569 | "save -> load -> save is not a fixed point" |
| 570 | ); |
| 571 | assert!(!path.with_extension("edith.part").exists(), "left a .part"); |
| 572 | } |
| 573 | |
| 574 | #[test] |
| 575 | fn a_folder_of_media_and_its_project_can_be_moved() { |
| 576 | let from = scratch("relocate_from"); |
| 577 | let saved = edited(&from); |
| 578 | saved.save_project(&from.join("edit.edith")).expect("save"); |
| 579 | drop(saved); |
| 580 | |
| 581 | let to = scratch("relocate_to"); |
| 582 | for name in ["test_av.mp4", "test_av2.mp4", "edit.edith"] { |
| 583 | std::fs::copy(from.join(name), to.join(name)).expect("copy"); |
| 584 | } |
| 585 | |
| 586 | std::fs::remove_dir_all(&from).expect("remove the original folder"); |
| 587 | |
| 588 | let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy"); |
| 589 | assert_eq!(moved.timeline_duration(), 6.0); |
| 590 | assert!( |
| 591 | moved.sources().iter().all(|s| s.path.starts_with(&to)), |
| 592 | "sources did not follow the folder: {:?}", |
| 593 | moved.sources() |
| 594 | ); |
| 595 | } |
| 596 | |
| 597 | #[test] |
| 598 | fn orphan_sources_are_not_written() { |
| 599 | let dir = scratch("orphans"); |
| 600 | let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open"); |
| 601 | |
| 602 | |
| 603 | |
| 604 | session |
| 605 | .import(©_in(&dir, "test_av2.mp4")) |
| 606 | .expect("import"); |
| 607 | assert_eq!( |
| 608 | session.sources().len(), |
| 609 | 2, |
| 610 | "the orphan entry stays in-session" |
| 611 | ); |
| 612 | |
| 613 | let path = dir.join("orphan.edith"); |
| 614 | session.save_project(&path).expect("save"); |
| 615 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 616 | assert_eq!( |
| 617 | text.lines().filter(|l| l.starts_with("source ")).count(), |
| 618 | 1, |
| 619 | "the orphan reached the file:\n{text}" |
| 620 | ); |
| 621 | |
| 622 | |
| 623 | |
| 624 | std::fs::remove_file(dir.join("test_av2.mp4")).expect("unlink the orphan"); |
| 625 | let loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 626 | assert_eq!(loaded.sources().len(), 1); |
| 627 | assert_eq!(loaded.timeline_duration(), 5.0); |
| 628 | } |
| 629 | |
| 630 | #[test] |
| 631 | fn a_source_that_vanished_is_refused_by_name() { |
| 632 | let dir = scratch("missing"); |
| 633 | let session = edited(&dir); |
| 634 | let path = dir.join("edit.edith"); |
| 635 | session.save_project(&path).expect("save"); |
| 636 | drop(session); |
| 637 | |
| 638 | let gone = dir.join("test_av2.mp4"); |
| 639 | std::fs::remove_file(&gone).expect("unlink"); |
| 640 | let err = PlaybackSession::open_project(&path) |
| 641 | .err() |
| 642 | .expect("a project whose source is gone must not open") |
| 643 | .to_string(); |
| 644 | assert!( |
| 645 | err.contains("test_av2.mp4"), |
| 646 | "the refusal must name the file: {err}" |
| 647 | ); |
| 648 | |
| 649 | |
| 650 | std::fs::remove_file(dir.join("test_av.mp4")).expect("unlink"); |
| 651 | let err = PlaybackSession::open_project(&path) |
| 652 | .err() |
| 653 | .expect("no first source, no session") |
| 654 | .to_string(); |
| 655 | assert!(err.contains("test_av.mp4"), "{err}"); |
| 656 | } |
| 657 | |
| 658 | #[test] |
| 659 | fn a_source_that_shrank_is_refused_by_clip() { |
| 660 | let dir = scratch("shrunk"); |
| 661 | copy_in(&dir, "test_av.mp4"); |
| 662 | let path = dir.join("shrunk.edith"); |
| 663 | |
| 664 | |
| 665 | std::fs::write( |
| 666 | &path, |
| 667 | "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\nclip 30 10000 0\n", |
| 668 | ) |
| 669 | .expect("write"); |
| 670 | let err = PlaybackSession::open_project(&path) |
| 671 | .err() |
| 672 | .expect("a clip past the end of its file must not open") |
| 673 | .to_string(); |
| 674 | assert!( |
| 675 | err.contains("clip 1") && err.contains("test_av.mp4") && err.contains("150"), |
| 676 | "the refusal must name the clip, the file and its length: {err}" |
| 677 | ); |
| 678 | |
| 679 | |
| 680 | std::fs::write( |
| 681 | &path, |
| 682 | "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 150 0\n", |
| 683 | ) |
| 684 | .expect("write"); |
| 685 | let whole = PlaybackSession::open_project(&path).expect("the exact length is legal"); |
| 686 | assert_eq!(whole.timeline_duration(), 5.0); |
| 687 | } |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | #[test] |
| 700 | fn a_refused_project_does_not_hang_on_its_own_decoder() { |
| 701 | let dir = scratch("refusal_hang"); |
| 702 | copy_in(&dir, "test_av.mp4"); |
| 703 | let path = dir.join("refused.edith"); |
| 704 | let mut text = String::from("edith 1\nplayhead 0\n"); |
| 705 | for _ in 0..1024 { |
| 706 | text.push_str("source test_av.mp4\n"); |
| 707 | } |
| 708 | |
| 709 | text.push_str("clip 0 150 0\nclip 0 10000 0\n"); |
| 710 | std::fs::write(&path, &text).expect("write"); |
| 711 | |
| 712 | let (tx, rx) = std::sync::mpsc::sync_channel(1); |
| 713 | let probe = path.clone(); |
| 714 | std::thread::spawn(move || { |
| 715 | let err = PlaybackSession::open_project(&probe) |
| 716 | .err() |
| 717 | .map(|e| e.to_string()); |
| 718 | let _ = tx.send(err); |
| 719 | }); |
| 720 | match rx.recv_timeout(std::time::Duration::from_secs(60)) { |
| 721 | Ok(Some(err)) => assert!(err.contains("150"), "refused, but not by name: {err}"), |
| 722 | Ok(None) => panic!("a clip past the end of its file must not open"), |
| 723 | Err(_) => panic!( |
| 724 | "open_project hung on a refusal: its own decode worker is parked in \ |
| 725 | send and the teardown is waiting for it" |
| 726 | ), |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | #[test] |
| 731 | fn a_project_that_names_itself_is_refused() { |
| 732 | let dir = scratch("self_reference"); |
| 733 | let path = dir.join("ouroboros.edith"); |
| 734 | std::fs::write( |
| 735 | &path, |
| 736 | "edith 1\nplayhead 0\nsource ouroboros.edith\nclip 0 30 0\n", |
| 737 | ) |
| 738 | .expect("write"); |
| 739 | let err = PlaybackSession::open_project(&path) |
| 740 | .err() |
| 741 | .expect("a project is not a video") |
| 742 | .to_string(); |
| 743 | assert!(err.contains("ouroboros.edith"), "{err}"); |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn malformed_files_are_numbered_errors_and_never_panics() { |
| 748 | let dir = scratch("malformed"); |
| 749 | copy_in(&dir, "test_av.mp4"); |
| 750 | let path = dir.join("bad.edith"); |
| 751 | let good = "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\n"; |
| 752 | |
| 753 | for (text, want) in [ |
| 754 | ( |
| 755 | "edith 17\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - - - fit\n", |
| 756 | "line 1", |
| 757 | ), |
| 758 | |
| 759 | ( |
| 760 | "edith 5\nsource 0 test_av.mp4\nvideo 1 0 0 30 0 - 0\n", |
| 761 | "line 3", |
| 762 | ), |
| 763 | ( |
| 764 | "edith 5\nsource 0 test_av.mp4\neq 80.0:0.0:0.707:band\nvideo 1 0 0 30 0 - 0\n", |
| 765 | "line 3", |
| 766 | ), |
| 767 | |
| 768 | ( |
| 769 | "edith 4\nsource 0 test_av.mp4\nvideo 2 0 0 30 0 -\n", |
| 770 | "line 3", |
| 771 | ), |
| 772 | |
| 773 | ("edith 3\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 2"), |
| 774 | ("not a project at all\n", "line 1"), |
| 775 | |
| 776 | ("edith 2\nsource test_av.mp4\nclip 0 30 0\n", "line 3"), |
| 777 | ("edith 1\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 3"), |
| 778 | ("edith 2\nsource test_av.mp4\nvideo 0 0 30 0\n", "line 3"), |
| 779 | ( |
| 780 | "edith 2\nsource test_av.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n", |
| 781 | "line 4", |
| 782 | ), |
| 783 | ("edith 1\nsource test_av.mp4\nclip 0 30\n", "line 3"), |
| 784 | ("edith 1\nsource test_av.mp4\nclip 0 30 4\n", "line 3"), |
| 785 | ("edith 1\nsource test_av.mp4\nclip 30 30 0\n", "line 3"), |
| 786 | ("edith 1\nsource test_av.mp4\n\nclip 0 30 0\n", "line 3"), |
| 787 | ("edith 1\nsource test_av.mp4\nwhat 1\n", "line 3"), |
| 788 | |
| 789 | |
| 790 | ( |
| 791 | "edith 2\nsource test_av.mp4\nvideo 4294967290 0 30 0 -\n", |
| 792 | "line 3", |
| 793 | ), |
| 794 | ] { |
| 795 | std::fs::write(&path, text).expect("write"); |
| 796 | let err = PlaybackSession::open_project(&path) |
| 797 | .err() |
| 798 | .unwrap_or_else(|| panic!("accepted {text:?}")) |
| 799 | .to_string(); |
| 800 | assert!(err.starts_with(want), "{text:?} -> {err}"); |
| 801 | } |
| 802 | |
| 803 | |
| 804 | |
| 805 | for cut in 0..good.len() { |
| 806 | std::fs::write(&path, &good[..cut]).expect("write"); |
| 807 | let _ = PlaybackSession::open_project(&path); |
| 808 | } |
| 809 | |
| 810 | std::fs::remove_file(&path).expect("unlink"); |
| 811 | assert!(PlaybackSession::open_project(&path).is_err()); |
| 812 | } |
| 813 | |
| 814 | #[test] |
| 815 | fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { |
| 816 | let dir = scratch("mismatch"); |
| 817 | copy_in(&dir, "test_av.mp4"); |
| 818 | std::fs::copy(asset("test_ac3.mp4"), dir.join("test_av2.mp4")).expect("substitute"); |
| 819 | let path = dir.join("swapped.edith"); |
| 820 | std::fs::write( |
| 821 | &path, |
| 822 | "edith 1\nplayhead 0\nsource test_av.mp4\nsource test_av2.mp4\nclip 0 30 0\nclip 0 30 1\n", |
| 823 | ) |
| 824 | .expect("write"); |
| 825 | let err = PlaybackSession::open_project(&path) |
| 826 | .err() |
| 827 | .expect("a source that stopped matching must not open") |
| 828 | .to_string(); |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | assert_eq!( |
| 837 | err, |
| 838 | format!( |
| 839 | "source {}: audio 1 ch does not match the timeline's 2 ch", |
| 840 | dir.join("test_av2.mp4").display() |
| 841 | ) |
| 842 | ); |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | #[test] |
| 849 | fn the_mix_survives_a_save_and_an_open() { |
| 850 | use engine::project::{Lane, LaneKind}; |
| 851 | let dir = scratch("mix"); |
| 852 | let source = copy_in(&dir, "test_av.mp4"); |
| 853 | let mut saved = PlaybackSession::open(&source).expect("open the fixture"); |
| 854 | let a2 = saved.add_lane(LaneKind::Audio); |
| 855 | assert!(saved.set_lane_gain_db(Lane::A1, -6.0)); |
| 856 | assert!(saved.set_lane_gain_db(a2, 3.0)); |
| 857 | let limiter = engine::limiter::Limiter { |
| 858 | ceiling_db: -2.0, |
| 859 | on: true, |
| 860 | }; |
| 861 | assert!(saved.set_limiter(limiter)); |
| 862 | let path = dir.join("mix.edith"); |
| 863 | saved.save_project(&path).expect("save"); |
| 864 | |
| 865 | let mut loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 866 | assert_eq!(loaded.lane_gain_db(Lane::A1), -6.0, "A1's fader"); |
| 867 | assert_eq!(loaded.lane_gain_db(a2), 3.0, "A2's own, not A1's"); |
| 868 | assert_eq!(loaded.limiter(), limiter); |
| 869 | assert!(!loaded.undo(), "a loaded mix is not an undo step"); |
| 870 | |
| 871 | let again = dir.join("again.edith"); |
| 872 | loaded |
| 873 | .save_project(&again) |
| 874 | .expect("save the loaded project"); |
| 875 | assert_eq!( |
| 876 | std::fs::read(&path).unwrap(), |
| 877 | std::fs::read(&again).unwrap(), |
| 878 | "save -> load -> save is not a fixed point" |
| 879 | ); |
| 880 | } |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | |
| 886 | #[test] |
| 887 | fn the_tone_map_preset_survives_a_save_and_an_open() { |
| 888 | use engine::tonemap::Preset; |
| 889 | let dir = scratch("tone"); |
| 890 | let source = copy_in(&dir, "test_av.mp4"); |
| 891 | let mut saved = PlaybackSession::open(&source).expect("open the fixture"); |
| 892 | assert_eq!(saved.tone(), Preset::Reference, "nobody has picked yet"); |
| 893 | assert!(saved.set_tone(Preset::Vivid)); |
| 894 | let path = dir.join("tone.edith"); |
| 895 | saved.save_project(&path).expect("save"); |
| 896 | |
| 897 | let mut loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 898 | assert_eq!(loaded.tone(), Preset::Vivid); |
| 899 | assert!(!loaded.undo(), "a loaded rendition is not an undo step"); |
| 900 | |
| 901 | let again = dir.join("again.edith"); |
| 902 | loaded |
| 903 | .save_project(&again) |
| 904 | .expect("save the loaded project"); |
| 905 | assert_eq!( |
| 906 | std::fs::read(&path).unwrap(), |
| 907 | std::fs::read(&again).unwrap(), |
| 908 | "save -> load -> save is not a fixed point" |
| 909 | ); |
| 910 | |
| 911 | |
| 912 | |
| 913 | let old = dir.join("old.edith"); |
| 914 | std::fs::write( |
| 915 | &old, |
| 916 | "edith 10\nplayhead 0\nresolution 1920 1080\nsource 0 test_av.mp4\n\ |
| 917 | video 1 0 0 24 0 - - - fit 1000\naudio 1 0 0 24 0 - - - fit 1000\n", |
| 918 | ) |
| 919 | .expect("write a v10 project"); |
| 920 | let older = PlaybackSession::open_project(&old).expect("open the v10 project"); |
| 921 | assert_eq!(older.tone(), Preset::Reference); |
| 922 | } |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | #[test] |
| 931 | fn a_song_only_project_comes_back_at_the_rate_it_names() { |
| 932 | let dir = scratch("song_fps"); |
| 933 | copy_in(&dir, "test_tone.mp3"); |
| 934 | let path = dir.join("song.edith"); |
| 935 | std::fs::write( |
| 936 | &path, |
| 937 | "edith 10\nplayhead 0\nresolution 1920 1080\nfps 24.0\nsource 0 test_tone.mp3\n\ |
| 938 | video 1\naudio 1 0 0 24 0 - - - fit 1000\n", |
| 939 | ) |
| 940 | .expect("write a v9 song project"); |
| 941 | let loaded = PlaybackSession::open_project(&path).expect("a song-only project opens"); |
| 942 | assert_eq!(loaded.meta().frame_rate, 24.0, "the rate the file names"); |
| 943 | assert_eq!(loaded.timeline_duration(), 1.0, "one second at 24 fps"); |
| 944 | |
| 945 | |
| 946 | |
| 947 | let old = dir.join("song8.edith"); |
| 948 | std::fs::write( |
| 949 | &old, |
| 950 | "edith 8\nplayhead 0\nresolution 1920 1080\nsource 0 test_tone.mp3\n\ |
| 951 | video 1\naudio 1 0 0 24 0 - - - fit 1000\n", |
| 952 | ) |
| 953 | .expect("write a v8 song project"); |
| 954 | let loaded = PlaybackSession::open_project(&old).expect("a v8 song project opens"); |
| 955 | assert_eq!(loaded.meta().frame_rate, 30.0, "the canvas rate, as before"); |
| 956 | } |