| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use std::path::{Path, PathBuf}; |
| 11 | |
| 12 | use engine::PlaybackSession; |
| 13 | use engine::project::Source; |
| 14 | |
| 15 | fn asset(name: &str) -> PathBuf { |
| 16 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 17 | .join("../../assets") |
| 18 | .join(name) |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | fn scratch(name: &str) -> PathBuf { |
| 24 | let dir = std::env::temp_dir().join(format!("ve_saveload_{name}_{}", std::process::id())); |
| 25 | let _ = std::fs::remove_dir_all(&dir); |
| 26 | std::fs::create_dir_all(&dir).expect("scratch dir"); |
| 27 | std::fs::canonicalize(&dir).expect("canonical scratch dir") |
| 28 | } |
| 29 | |
| 30 | fn copy_in(dir: &Path, name: &str) -> PathBuf { |
| 31 | let to = dir.join(name); |
| 32 | std::fs::copy(asset(name), &to).expect("copy the fixture"); |
| 33 | to |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | #[test] |
| 42 | fn a_picked_audio_stream_lands_saves_and_comes_back() { |
| 43 | let dir = scratch("streams"); |
| 44 | let media = copy_in(&dir, "test_multilang.mp4"); |
| 45 | let mut session = PlaybackSession::open(&media).expect("open the fixture"); |
| 46 | session.set_gain(0.0); |
| 47 | let frames = session.clip_at(0).expect("one clip").out_frame; |
| 48 | let end = session.timeline_duration(); |
| 49 | |
| 50 | |
| 51 | |
| 52 | assert!( |
| 53 | session |
| 54 | .place_stream_at(end, &media, 1, frames) |
| 55 | .expect("stream 1 matches the timeline") |
| 56 | ); |
| 57 | assert_eq!( |
| 58 | session |
| 59 | .sources() |
| 60 | .iter() |
| 61 | .map(|s| s.audio_stream) |
| 62 | .collect::<Vec<_>>(), |
| 63 | [0, 1], |
| 64 | "the same file twice, on two streams" |
| 65 | ); |
| 66 | assert_eq!(session.timeline_duration(), end * 2.0); |
| 67 | |
| 68 | let path = dir.join("langs.edith"); |
| 69 | session.save_project(&path).expect("save"); |
| 70 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 71 | assert!( |
| 72 | text.contains("source 0 test_multilang.mp4") |
| 73 | && text.contains("source 1 test_multilang.mp4"), |
| 74 | "the streams did not reach the file:\n{text}" |
| 75 | ); |
| 76 | |
| 77 | let loaded = PlaybackSession::open_project(&path).expect("reopen the project"); |
| 78 | assert_eq!(loaded.sources(), session.sources()); |
| 79 | assert_eq!(loaded.timeline_duration(), session.timeline_duration()); |
| 80 | |
| 81 | |
| 82 | |
| 83 | let other = copy_in(&dir, "test_multiaudio.mp4"); |
| 84 | let mut session = PlaybackSession::open(&other).expect("open the fixture"); |
| 85 | session.set_gain(0.0); |
| 86 | let frames = session.clip_at(0).expect("one clip").out_frame; |
| 87 | let err = session |
| 88 | .place_stream_at(0.0, &other, 1, frames) |
| 89 | .expect_err("22.05 kHz mono cannot join a 44.1 kHz stereo timeline") |
| 90 | .to_string(); |
| 91 | assert!(err.contains("22050"), "unhelpful refusal: {err}"); |
| 92 | assert!( |
| 93 | session |
| 94 | .place_stream_at(0.0, &media, 1, frames) |
| 95 | .expect_err("a file that is not a source") |
| 96 | .to_string() |
| 97 | .contains("not on this timeline") |
| 98 | ); |
| 99 | assert_eq!(session.sources().len(), 1, "a refusal added a source"); |
| 100 | std::fs::remove_dir_all(&dir).expect("cleanup"); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | fn edited(dir: &Path) -> PlaybackSession { |
| 106 | let mut session = PlaybackSession::open(copy_in(dir, "test_av.mp4")).expect("open"); |
| 107 | session |
| 108 | .import(©_in(dir, "test_av2.mp4")) |
| 109 | .expect("import the second file"); |
| 110 | assert!(session.cut_at(2.0), "cut inside the first file"); |
| 111 | assert!(session.cut_at(6.5), "cut inside the second file"); |
| 112 | assert!(session.delete_clip(1), "drop the second half of the first"); |
| 113 | session.seek(3.0); |
| 114 | assert_eq!(session.timeline_duration(), 6.0); |
| 115 | session |
| 116 | } |
| 117 | |
| 118 | |
| 119 | fn shape(session: &PlaybackSession) -> (Vec<(f64, f64, usize)>, Vec<Source>, f64) { |
| 120 | ( |
| 121 | session.clip_spans_by_source(), |
| 122 | session.sources().to_vec(), |
| 123 | session.timeline_duration(), |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | #[test] |
| 131 | fn a_version_1_project_loads_fully_grouped_and_saves_as_version_3() { |
| 132 | let dir = scratch("v1"); |
| 133 | copy_in(&dir, "test_av.mp4"); |
| 134 | let path = dir.join("old.edith"); |
| 135 | |
| 136 | std::fs::write( |
| 137 | &path, |
| 138 | "edith 1\nplayhead 45\nsource test_av.mp4\nclip 0 30 0\nclip 60 90 0\n", |
| 139 | ) |
| 140 | .expect("write a v1 file"); |
| 141 | |
| 142 | let loaded = PlaybackSession::open_project(&path).expect("a v1 file still opens"); |
| 143 | let fps = loaded.meta().frame_rate; |
| 144 | assert_eq!( |
| 145 | loaded.clip_spans_by_source(), |
| 146 | vec![(0.0, 30.0 / fps, 0), (30.0 / fps, 30.0 / fps, 0)], |
| 147 | "v1 clips queue up, the second starting where the first ended" |
| 148 | ); |
| 149 | assert_eq!( |
| 150 | loaded.lane_spans_by_source(engine::project::Lane::Audio), |
| 151 | loaded.clip_spans_by_source(), |
| 152 | "both lanes, fully grouped: a v1 timeline had no holes" |
| 153 | ); |
| 154 | assert!( |
| 155 | (loaded.now() - 45.0 / fps).abs() < 1.0 / fps, |
| 156 | "playhead kept" |
| 157 | ); |
| 158 | |
| 159 | |
| 160 | let v2 = dir.join("new.edith"); |
| 161 | loaded.save_project(&v2).expect("save"); |
| 162 | let text = std::fs::read_to_string(&v2).expect("read back"); |
| 163 | assert!(text.starts_with("edith 3\n"), "{text}"); |
| 164 | assert_eq!( |
| 165 | text.lines().filter(|l| l.starts_with("video ")).count(), |
| 166 | 2, |
| 167 | "{text}" |
| 168 | ); |
| 169 | assert_eq!( |
| 170 | text.lines().filter(|l| l.starts_with("audio ")).count(), |
| 171 | 2, |
| 172 | "{text}" |
| 173 | ); |
| 174 | let again = PlaybackSession::open_project(&v2).expect("open the v2 file"); |
| 175 | assert_eq!(shape(&again), shape(&loaded)); |
| 176 | |
| 177 | let third = dir.join("third.edith"); |
| 178 | again.save_project(&third).expect("save"); |
| 179 | assert_eq!(std::fs::read(&third).expect("read"), text.as_bytes()); |
| 180 | |
| 181 | |
| 182 | |
| 183 | let v2 = dir.join("v2.edith"); |
| 184 | std::fs::write( |
| 185 | &v2, |
| 186 | "edith 2\nplayhead 0\nsource test_av.mp4\nvideo 0 0 30 0 0\naudio 0 0 30 0 0\n", |
| 187 | ) |
| 188 | .expect("write a v2 file"); |
| 189 | let loaded = PlaybackSession::open_project(&v2).expect("a v2 file still opens"); |
| 190 | assert_eq!(loaded.sources().len(), 1); |
| 191 | assert_eq!(loaded.sources()[0].audio_stream, 0); |
| 192 | loaded.save_project(&v2).expect("save"); |
| 193 | assert!( |
| 194 | std::fs::read_to_string(&v2) |
| 195 | .expect("read back") |
| 196 | .contains("source 0 test_av.mp4"), |
| 197 | "a re-saved v2 project says the stream it always meant" |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | #[test] |
| 202 | fn a_saved_project_reopens_as_the_same_timeline() { |
| 203 | let dir = scratch("round_trip"); |
| 204 | let saved = edited(&dir); |
| 205 | let path = dir.join("edit.edith"); |
| 206 | saved.save_project(&path).expect("save"); |
| 207 | |
| 208 | let mut loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 209 | assert_eq!(shape(&loaded), shape(&saved), "clips, sources and duration"); |
| 210 | assert!( |
| 211 | (loaded.now() - saved.now()).abs() < 1.0 / loaded.meta().frame_rate, |
| 212 | "playhead landed at {:.3}s, saved at {:.3}s", |
| 213 | loaded.now(), |
| 214 | saved.now() |
| 215 | ); |
| 216 | assert!(!loaded.is_playing(), "a loaded project starts paused"); |
| 217 | assert!(!loaded.undo(), "history is not saved"); |
| 218 | |
| 219 | |
| 220 | |
| 221 | let again = dir.join("again.edith"); |
| 222 | loaded |
| 223 | .save_project(&again) |
| 224 | .expect("save the loaded project"); |
| 225 | assert_eq!( |
| 226 | std::fs::read(&path).unwrap(), |
| 227 | std::fs::read(&again).unwrap(), |
| 228 | "save -> load -> save is not a fixed point" |
| 229 | ); |
| 230 | assert!(!path.with_extension("edith.part").exists(), "left a .part"); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn a_folder_of_media_and_its_project_can_be_moved() { |
| 235 | let from = scratch("relocate_from"); |
| 236 | let saved = edited(&from); |
| 237 | saved.save_project(&from.join("edit.edith")).expect("save"); |
| 238 | drop(saved); |
| 239 | |
| 240 | let to = scratch("relocate_to"); |
| 241 | for name in ["test_av.mp4", "test_av2.mp4", "edit.edith"] { |
| 242 | std::fs::copy(from.join(name), to.join(name)).expect("copy"); |
| 243 | } |
| 244 | |
| 245 | std::fs::remove_dir_all(&from).expect("remove the original folder"); |
| 246 | |
| 247 | let moved = PlaybackSession::open_project(&to.join("edit.edith")).expect("open the copy"); |
| 248 | assert_eq!(moved.timeline_duration(), 6.0); |
| 249 | assert!( |
| 250 | moved.sources().iter().all(|s| s.path.starts_with(&to)), |
| 251 | "sources did not follow the folder: {:?}", |
| 252 | moved.sources() |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn orphan_sources_are_not_written() { |
| 258 | let dir = scratch("orphans"); |
| 259 | let mut session = PlaybackSession::open(copy_in(&dir, "test_av.mp4")).expect("open"); |
| 260 | session |
| 261 | .import(©_in(&dir, "test_av2.mp4")) |
| 262 | .expect("import"); |
| 263 | assert!(session.undo(), "take the import back"); |
| 264 | assert_eq!( |
| 265 | session.sources().len(), |
| 266 | 2, |
| 267 | "the orphan entry stays in-session" |
| 268 | ); |
| 269 | |
| 270 | let path = dir.join("orphan.edith"); |
| 271 | session.save_project(&path).expect("save"); |
| 272 | let text = std::fs::read_to_string(&path).expect("read back"); |
| 273 | assert_eq!( |
| 274 | text.lines().filter(|l| l.starts_with("source ")).count(), |
| 275 | 1, |
| 276 | "the orphan reached the file:\n{text}" |
| 277 | ); |
| 278 | |
| 279 | |
| 280 | |
| 281 | std::fs::remove_file(dir.join("test_av2.mp4")).expect("unlink the orphan"); |
| 282 | let loaded = PlaybackSession::open_project(&path).expect("open the project"); |
| 283 | assert_eq!(loaded.sources().len(), 1); |
| 284 | assert_eq!(loaded.timeline_duration(), 5.0); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn a_source_that_vanished_is_refused_by_name() { |
| 289 | let dir = scratch("missing"); |
| 290 | let session = edited(&dir); |
| 291 | let path = dir.join("edit.edith"); |
| 292 | session.save_project(&path).expect("save"); |
| 293 | drop(session); |
| 294 | |
| 295 | let gone = dir.join("test_av2.mp4"); |
| 296 | std::fs::remove_file(&gone).expect("unlink"); |
| 297 | let err = PlaybackSession::open_project(&path) |
| 298 | .err() |
| 299 | .expect("a project whose source is gone must not open") |
| 300 | .to_string(); |
| 301 | assert!( |
| 302 | err.contains("test_av2.mp4"), |
| 303 | "the refusal must name the file: {err}" |
| 304 | ); |
| 305 | |
| 306 | |
| 307 | std::fs::remove_file(dir.join("test_av.mp4")).expect("unlink"); |
| 308 | let err = PlaybackSession::open_project(&path) |
| 309 | .err() |
| 310 | .expect("no first source, no session") |
| 311 | .to_string(); |
| 312 | assert!(err.contains("test_av.mp4"), "{err}"); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn a_source_that_shrank_is_refused_by_clip() { |
| 317 | let dir = scratch("shrunk"); |
| 318 | copy_in(&dir, "test_av.mp4"); |
| 319 | let path = dir.join("shrunk.edith"); |
| 320 | |
| 321 | |
| 322 | std::fs::write( |
| 323 | &path, |
| 324 | "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\nclip 30 10000 0\n", |
| 325 | ) |
| 326 | .expect("write"); |
| 327 | let err = PlaybackSession::open_project(&path) |
| 328 | .err() |
| 329 | .expect("a clip past the end of its file must not open") |
| 330 | .to_string(); |
| 331 | assert!( |
| 332 | err.contains("clip 1") && err.contains("test_av.mp4") && err.contains("150"), |
| 333 | "the refusal must name the clip, the file and its length: {err}" |
| 334 | ); |
| 335 | |
| 336 | |
| 337 | std::fs::write( |
| 338 | &path, |
| 339 | "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 150 0\n", |
| 340 | ) |
| 341 | .expect("write"); |
| 342 | let whole = PlaybackSession::open_project(&path).expect("the exact length is legal"); |
| 343 | assert_eq!(whole.timeline_duration(), 5.0); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | #[test] |
| 357 | fn a_refused_project_does_not_hang_on_its_own_decoder() { |
| 358 | let dir = scratch("refusal_hang"); |
| 359 | copy_in(&dir, "test_av.mp4"); |
| 360 | let path = dir.join("refused.edith"); |
| 361 | let mut text = String::from("edith 1\nplayhead 0\n"); |
| 362 | for _ in 0..1024 { |
| 363 | text.push_str("source test_av.mp4\n"); |
| 364 | } |
| 365 | |
| 366 | text.push_str("clip 0 150 0\nclip 0 10000 0\n"); |
| 367 | std::fs::write(&path, &text).expect("write"); |
| 368 | |
| 369 | let (tx, rx) = std::sync::mpsc::sync_channel(1); |
| 370 | let probe = path.clone(); |
| 371 | std::thread::spawn(move || { |
| 372 | let err = PlaybackSession::open_project(&probe) |
| 373 | .err() |
| 374 | .map(|e| e.to_string()); |
| 375 | let _ = tx.send(err); |
| 376 | }); |
| 377 | match rx.recv_timeout(std::time::Duration::from_secs(60)) { |
| 378 | Ok(Some(err)) => assert!(err.contains("150"), "refused, but not by name: {err}"), |
| 379 | Ok(None) => panic!("a clip past the end of its file must not open"), |
| 380 | Err(_) => panic!( |
| 381 | "open_project hung on a refusal: its own decode worker is parked in \ |
| 382 | send and the teardown is waiting for it" |
| 383 | ), |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | #[test] |
| 388 | fn a_project_that_names_itself_is_refused() { |
| 389 | let dir = scratch("self_reference"); |
| 390 | let path = dir.join("ouroboros.edith"); |
| 391 | std::fs::write( |
| 392 | &path, |
| 393 | "edith 1\nplayhead 0\nsource ouroboros.edith\nclip 0 30 0\n", |
| 394 | ) |
| 395 | .expect("write"); |
| 396 | let err = PlaybackSession::open_project(&path) |
| 397 | .err() |
| 398 | .expect("a project is not a video") |
| 399 | .to_string(); |
| 400 | assert!(err.contains("ouroboros.edith"), "{err}"); |
| 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn malformed_files_are_numbered_errors_and_never_panics() { |
| 405 | let dir = scratch("malformed"); |
| 406 | copy_in(&dir, "test_av.mp4"); |
| 407 | let path = dir.join("bad.edith"); |
| 408 | let good = "edith 1\nplayhead 0\nsource test_av.mp4\nclip 0 30 0\n"; |
| 409 | |
| 410 | for (text, want) in [ |
| 411 | ( |
| 412 | "edith 4\nsource 0 test_av.mp4\nvideo 0 0 30 0 -\n", |
| 413 | "line 1", |
| 414 | ), |
| 415 | |
| 416 | ("edith 3\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 2"), |
| 417 | ("not a project at all\n", "line 1"), |
| 418 | |
| 419 | ("edith 2\nsource test_av.mp4\nclip 0 30 0\n", "line 3"), |
| 420 | ("edith 1\nsource test_av.mp4\nvideo 0 0 30 0 -\n", "line 3"), |
| 421 | ("edith 2\nsource test_av.mp4\nvideo 0 0 30 0\n", "line 3"), |
| 422 | ( |
| 423 | "edith 2\nsource test_av.mp4\nvideo 0 0 30 0 -\nvideo 10 0 30 0 -\n", |
| 424 | "line 4", |
| 425 | ), |
| 426 | ("edith 1\nsource test_av.mp4\nclip 0 30\n", "line 3"), |
| 427 | ("edith 1\nsource test_av.mp4\nclip 0 30 4\n", "line 3"), |
| 428 | ("edith 1\nsource test_av.mp4\nclip 30 30 0\n", "line 3"), |
| 429 | ("edith 1\nsource test_av.mp4\n\nclip 0 30 0\n", "line 3"), |
| 430 | ("edith 1\nsource test_av.mp4\nwhat 1\n", "line 3"), |
| 431 | |
| 432 | |
| 433 | ( |
| 434 | "edith 2\nsource test_av.mp4\nvideo 4294967290 0 30 0 -\n", |
| 435 | "line 3", |
| 436 | ), |
| 437 | ] { |
| 438 | std::fs::write(&path, text).expect("write"); |
| 439 | let err = PlaybackSession::open_project(&path) |
| 440 | .err() |
| 441 | .unwrap_or_else(|| panic!("accepted {text:?}")) |
| 442 | .to_string(); |
| 443 | assert!(err.starts_with(want), "{text:?} -> {err}"); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | for cut in 0..good.len() { |
| 449 | std::fs::write(&path, &good[..cut]).expect("write"); |
| 450 | let _ = PlaybackSession::open_project(&path); |
| 451 | } |
| 452 | |
| 453 | std::fs::remove_file(&path).expect("unlink"); |
| 454 | assert!(PlaybackSession::open_project(&path).is_err()); |
| 455 | } |
| 456 | |
| 457 | #[test] |
| 458 | fn a_source_that_no_longer_matches_the_timeline_is_refused_in_import_words() { |
| 459 | let dir = scratch("mismatch"); |
| 460 | copy_in(&dir, "test_av.mp4"); |
| 461 | std::fs::copy(asset("test_mismatch.mp4"), dir.join("test_av2.mp4")).expect("substitute"); |
| 462 | let path = dir.join("swapped.edith"); |
| 463 | std::fs::write( |
| 464 | &path, |
| 465 | "edith 1\nplayhead 0\nsource test_av.mp4\nsource test_av2.mp4\nclip 0 30 0\nclip 0 30 1\n", |
| 466 | ) |
| 467 | .expect("write"); |
| 468 | let err = PlaybackSession::open_project(&path) |
| 469 | .err() |
| 470 | .expect("a source that stopped matching must not open") |
| 471 | .to_string(); |
| 472 | |
| 473 | assert_eq!( |
| 474 | err, |
| 475 | format!( |
| 476 | "source {}: 640x360 does not match the timeline's 1280x720", |
| 477 | dir.join("test_av2.mp4").display() |
| 478 | ) |
| 479 | ); |
| 480 | } |