| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | use std::fs::File; |
| 22 | use std::io::BufReader; |
| 23 | use std::path::{Path, PathBuf}; |
| 24 | use std::sync::{Mutex, Once}; |
| 25 | use std::time::{Duration, Instant}; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | static PROXY_FILM: Mutex<()> = Mutex::new(()); |
| 31 | |
| 32 | use engine::export::ExportSettings; |
| 33 | use engine::hw::HwEncoder; |
| 34 | use engine::mux::{Mp4Muxer, VideoParams, parameter_sets}; |
| 35 | use engine::project::{Lane, Source, Speed}; |
| 36 | use engine::scale::FitPolicy; |
| 37 | use engine::scratch::Scratch; |
| 38 | use engine::{DecodeSession, ExportHandle, PlaybackSession, Project}; |
| 39 | |
| 40 | const FPS: f64 = 30.0; |
| 41 | |
| 42 | fn asset(name: &str) -> PathBuf { |
| 43 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 44 | .join("../../assets") |
| 45 | .join(name) |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | fn out_path(name: &str) -> Scratch { |
| 52 | Scratch::file(&format!("ve_export_{name}"), "mp4") |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | fn part_path(out: &Path) -> PathBuf { |
| 58 | PathBuf::from(format!("{}.part", out.display())) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | static SEAT: std::sync::RwLock<()> = std::sync::RwLock::new(()); |
| 73 | |
| 74 | type Shared = std::sync::RwLockReadGuard<'static, ()>; |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #[must_use = "the seat pin holds only while its guard is alive"] |
| 81 | fn pin_software() -> Shared { |
| 82 | let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 83 | static PIN: Once = Once::new(); |
| 84 | PIN.call_once(|| unsafe { |
| 85 | std::env::set_var("VE_SW", "1"); |
| 86 | std::env::set_var("VE_SW_ENC", "1"); |
| 87 | }); |
| 88 | borrowed |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | #[must_use = "the seat pin holds only while its guard is alive"] |
| 95 | fn pin_hardware() -> Shared { |
| 96 | let borrowed = SEAT.read().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 97 | static PIN: Once = Once::new(); |
| 98 | PIN.call_once(|| unsafe { |
| 99 | std::env::remove_var("VE_SW"); |
| 100 | std::env::remove_var("VE_SW_ENC"); |
| 101 | }); |
| 102 | borrowed |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | fn edited(source: &Path) -> PlaybackSession { |
| 108 | let mut session = PlaybackSession::open(source).expect("open source"); |
| 109 | assert!(session.cut_at(30.0 / FPS), "cut at frame 30"); |
| 110 | assert!(session.cut_at(60.0 / FPS), "cut at frame 60"); |
| 111 | assert!(session.delete_clip(Lane::V1, 1), "drop the middle clip"); |
| 112 | assert_eq!(session.clip_spans().len(), 2); |
| 113 | session |
| 114 | } |
| 115 | |
| 116 | fn wait(handle: &ExportHandle, limit: Duration) -> engine::Result<()> { |
| 117 | let started = Instant::now(); |
| 118 | while !handle.is_finished() { |
| 119 | assert!( |
| 120 | started.elapsed() < limit, |
| 121 | "export did not finish in {limit:?}" |
| 122 | ); |
| 123 | std::thread::sleep(Duration::from_millis(20)); |
| 124 | } |
| 125 | handle.result().expect("a finished export has an outcome") |
| 126 | } |
| 127 | |
| 128 | |
| 129 | fn decode_all(path: &Path) -> Vec<Vec<u8>> { |
| 130 | let (_, frames) = DecodeSession::open(path).expect("open export"); |
| 131 | frames.into_iter().map(|frame| frame.bgra).collect() |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | fn ffmpeg_complaints(path: &Path) -> Option<String> { |
| 139 | let said = std::process::Command::new("ffmpeg") |
| 140 | .args(["-v", "error", "-i"]) |
| 141 | .arg(path) |
| 142 | .args(["-f", "null", "-"]) |
| 143 | .output() |
| 144 | .ok()?; |
| 145 | Some(String::from_utf8_lossy(&said.stderr).trim().to_string()) |
| 146 | } |
| 147 | |
| 148 | |
| 149 | fn frame_at(path: &Path, index: u32) -> Vec<u8> { |
| 150 | let (_, frames, _) = DecodeSession::open_at(path, index).expect("open source"); |
| 151 | frames.recv().expect("frame present").bgra |
| 152 | } |
| 153 | |
| 154 | fn mean_abs_diff(a: &[u8], b: &[u8]) -> f64 { |
| 155 | assert_eq!(a.len(), b.len(), "frame sizes differ"); |
| 156 | a.iter() |
| 157 | .zip(b) |
| 158 | .map(|(x, y)| f64::from(x.abs_diff(*y))) |
| 159 | .sum::<f64>() |
| 160 | / a.len() as f64 |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | fn round_trip(name: &str, limit: Duration) { |
| 166 | let source = asset("test_baseline.mp4"); |
| 167 | let mut session = edited(&source); |
| 168 | session.pause(); |
| 169 | let out = out_path(name); |
| 170 | |
| 171 | let started = Instant::now(); |
| 172 | let handle = session.export_to(&out); |
| 173 | wait(&handle, limit).expect("export"); |
| 174 | let spent = started.elapsed(); |
| 175 | println!( |
| 176 | "{name}: 120 frames in {:.2} s = {:.2} ms/frame", |
| 177 | spent.as_secs_f64(), |
| 178 | spent.as_secs_f64() * 1000.0 / 120.0 |
| 179 | ); |
| 180 | assert_eq!(handle.progress(), 1.0, "finished at full progress"); |
| 181 | |
| 182 | let (meta, _) = engine::demux::Demuxer::open(&out).expect("reopen export"); |
| 183 | assert_eq!(meta.frame_count, 120, "timeline frames written"); |
| 184 | assert_eq!((meta.width, meta.height), (1280, 720)); |
| 185 | assert_eq!(meta.frame_rate.round(), FPS); |
| 186 | |
| 187 | let file = File::open(&out).unwrap(); |
| 188 | let size = file.metadata().unwrap().len(); |
| 189 | let reader = mp4::Mp4Reader::read_header(BufReader::new(file), size).unwrap(); |
| 190 | let duration = reader.duration().as_secs_f64(); |
| 191 | assert!( |
| 192 | (duration - 120.0 / FPS).abs() <= 1.0 / FPS, |
| 193 | "duration {duration} s is more than a frame off 4.0 s" |
| 194 | ); |
| 195 | |
| 196 | let frames = decode_all(&out); |
| 197 | assert_eq!(frames.len(), 120, "every written frame decodes back"); |
| 198 | |
| 199 | |
| 200 | for (timeline, source_frame) in [(0u32, 0u32), (59, 89), (119, 149)] { |
| 201 | let diff = mean_abs_diff(&frames[timeline as usize], &frame_at(&source, source_frame)); |
| 202 | println!("timeline {timeline} vs source {source_frame}: mean abs diff {diff:.2}"); |
| 203 | assert!( |
| 204 | diff < 6.0, |
| 205 | "timeline frame {timeline} drifted by {diff:.2} from source frame {source_frame}" |
| 206 | ); |
| 207 | } |
| 208 | std::fs::remove_file(&out).unwrap(); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn exports_an_edited_timeline_in_software() { |
| 213 | let _seat = pin_software(); |
| 214 | round_trip("sw", Duration::from_secs(600)); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | #[test] |
| 220 | #[ignore] |
| 221 | fn exports_an_edited_timeline_in_hardware() { |
| 222 | let _seat = pin_hardware(); |
| 223 | round_trip("hw", Duration::from_secs(60)); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | #[test] |
| 234 | fn exports_at_the_project_resolution_with_the_watched_geometry() { |
| 235 | let _seat = pin_software(); |
| 236 | let clip = |source: usize, start: u32| engine::Clip { |
| 237 | fade_in: 0, |
| 238 | fade_out: 0, |
| 239 | transition_out: 0, |
| 240 | start, |
| 241 | in_frame: 0, |
| 242 | out_frame: 10, |
| 243 | source, |
| 244 | link: Some(source as u32), |
| 245 | eq: None, |
| 246 | color: None, |
| 247 | transform: None, |
| 248 | fit: FitPolicy::default(), |
| 249 | speed: Speed::NORMAL, |
| 250 | }; |
| 251 | let clips = vec![clip(0, 0), clip(1, 10)]; |
| 252 | let project = Project::from_parts( |
| 253 | vec![ |
| 254 | Source::new(asset("test_baseline.mp4"), 0), |
| 255 | Source::new(asset("test_mismatch.mp4"), 0), |
| 256 | ], |
| 257 | vec![ |
| 258 | (engine::project::LaneKind::Video, clips.clone()), |
| 259 | (engine::project::LaneKind::Audio, clips), |
| 260 | ], |
| 261 | Vec::new(), |
| 262 | Vec::new(), |
| 263 | Vec::new(), |
| 264 | ) |
| 265 | .expect("a two-source project"); |
| 266 | |
| 267 | |
| 268 | let meta = engine::VideoMeta { |
| 269 | width: 960, |
| 270 | height: 720, |
| 271 | frame_rate: FPS, |
| 272 | frame_count: 20, |
| 273 | codec: engine::Codec::H264, |
| 274 | color: Default::default(), |
| 275 | }; |
| 276 | let out = out_path("mixed"); |
| 277 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 278 | wait(&handle, Duration::from_secs(600)).expect("export"); |
| 279 | |
| 280 | let (written, _) = engine::demux::Demuxer::open(&out).expect("reopen export"); |
| 281 | assert_eq!( |
| 282 | (written.width, written.height), |
| 283 | (960, 720), |
| 284 | "an export is the project's size, not any source's" |
| 285 | ); |
| 286 | assert_eq!(written.frame_count, 20); |
| 287 | |
| 288 | let frames = decode_all(&out); |
| 289 | assert_eq!(frames.len(), 20); |
| 290 | |
| 291 | |
| 292 | |
| 293 | let mean = |frame: &[u8], y: usize| { |
| 294 | frame[y * 960 * 4..][..960 * 4] |
| 295 | .chunks_exact(4) |
| 296 | .flat_map(|px| [px[0], px[1], px[2]]) |
| 297 | .map(f64::from) |
| 298 | .sum::<f64>() |
| 299 | / (960.0 * 3.0) |
| 300 | }; |
| 301 | for (at, what) in [(5usize, "the 1280x720 clip"), (15, "the 640x360 clip")] { |
| 302 | let frame = &frames[at]; |
| 303 | assert!(mean(frame, 2) < 8.0, "{what}: top bar is not black"); |
| 304 | assert!(mean(frame, 717) < 8.0, "{what}: bottom bar is not black"); |
| 305 | assert!( |
| 306 | mean(frame, 360) > 20.0, |
| 307 | "{what}: the picture area is black, so nothing was composed" |
| 308 | ); |
| 309 | } |
| 310 | std::fs::remove_file(&out).unwrap(); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | #[test] |
| 319 | fn exports_the_audio_stream_the_timeline_plays() { |
| 320 | let _seat = pin_software(); |
| 321 | let multi = asset("test_multiaudio.mp4"); |
| 322 | let (meta, _) = engine::demux::Demuxer::open(&multi).expect("open the fixture"); |
| 323 | let whole = engine::Clip { |
| 324 | fade_in: 0, |
| 325 | fade_out: 0, |
| 326 | transition_out: 0, |
| 327 | start: 0, |
| 328 | in_frame: 0, |
| 329 | out_frame: meta.frame_count, |
| 330 | source: 0, |
| 331 | link: Some(0), |
| 332 | eq: None, |
| 333 | color: None, |
| 334 | transform: None, |
| 335 | fit: FitPolicy::default(), |
| 336 | speed: Speed::NORMAL, |
| 337 | }; |
| 338 | |
| 339 | |
| 340 | |
| 341 | let project = Project::from_parts( |
| 342 | vec![Source { |
| 343 | path: multi.clone(), |
| 344 | audio_stream: 1, |
| 345 | }], |
| 346 | vec![ |
| 347 | (engine::project::LaneKind::Video, vec![whole]), |
| 348 | (engine::project::LaneKind::Audio, vec![whole]), |
| 349 | ], |
| 350 | Vec::new(), |
| 351 | Vec::new(), |
| 352 | Vec::new(), |
| 353 | ) |
| 354 | .expect("a one-source project on stream 1"); |
| 355 | let out = out_path("stream1"); |
| 356 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 357 | wait(&handle, Duration::from_secs(600)).expect("export"); |
| 358 | |
| 359 | let (audio, _) = engine::AudioSession::open(&out) |
| 360 | .expect("reopen export audio") |
| 361 | .expect("the export has audio"); |
| 362 | assert_eq!( |
| 363 | (audio.sample_rate, audio.channels), |
| 364 | (22_050, 1), |
| 365 | "the export carries stream 0's parameters, not the stream that played" |
| 366 | ); |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | let segs = [(Some(0), 0.0, f64::from(meta.frame_count) / FPS)]; |
| 372 | let copy = |stream| { |
| 373 | engine::AudioSession::copy_multi_streams(&[(multi.clone(), stream)], &segs) |
| 374 | .expect("copy") |
| 375 | .expect("has audio") |
| 376 | .1 |
| 377 | .into_iter() |
| 378 | .map(|p| p.bytes) |
| 379 | .collect::<Vec<_>>() |
| 380 | }; |
| 381 | let (one, zero) = (copy(1), copy(0)); |
| 382 | assert_ne!(one, zero, "the two streams are not the same bytes"); |
| 383 | |
| 384 | let file = File::open(&out).unwrap(); |
| 385 | let size = file.metadata().unwrap().len(); |
| 386 | let mut reader = mp4::Mp4Reader::read_header(BufReader::new(file), size).unwrap(); |
| 387 | let track = *reader |
| 388 | .tracks() |
| 389 | .keys() |
| 390 | .find(|id| reader.sample_count(**id).unwrap_or(0) as usize == one.len()) |
| 391 | .unwrap_or(&2); |
| 392 | let written: Vec<Vec<u8>> = (1..=reader.sample_count(track).expect("audio track")) |
| 393 | .map(|id| { |
| 394 | reader |
| 395 | .read_sample(track, id) |
| 396 | .expect("sample") |
| 397 | .expect("present") |
| 398 | .bytes |
| 399 | .to_vec() |
| 400 | }) |
| 401 | .collect(); |
| 402 | assert_eq!(written, one, "the exported packets are stream 1's"); |
| 403 | std::fs::remove_file(&out).unwrap(); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | fn two_sources() -> Project { |
| 410 | let (av, _) = engine::demux::Demuxer::open(&asset("test_av.mp4")).expect("open test_av"); |
| 411 | let (av2, _) = engine::demux::Demuxer::open(&asset("test_av2.mp4")).expect("open test_av2"); |
| 412 | assert_eq!((av.width, av.height), (av2.width, av2.height), "policy a"); |
| 413 | |
| 414 | let mut project = Project::single(asset("test_av.mp4"), av.frame_count); |
| 415 | let second = project.import(asset("test_av2.mp4"), 0); |
| 416 | assert_eq!(second, 1, "a second file is a second source"); |
| 417 | assert!(project.append_clip(second, av2.frame_count)); |
| 418 | assert!(project.split(120), "cut a second before the end of test_av"); |
| 419 | assert!(project.delete(1), "drop test_av's tail"); |
| 420 | assert_eq!(project.timeline_frames(), 120 + av2.frame_count); |
| 421 | assert_eq!( |
| 422 | project.clips().iter().map(|c| c.source).collect::<Vec<_>>(), |
| 423 | vec![0, 1] |
| 424 | ); |
| 425 | project |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | fn multi_source_round_trip(name: &str, limit: Duration) { |
| 432 | let project = two_sources(); |
| 433 | let total = project.timeline_frames(); |
| 434 | let (meta, _) = engine::demux::Demuxer::open(&asset("test_av.mp4")).unwrap(); |
| 435 | let out = out_path(name); |
| 436 | |
| 437 | let started = Instant::now(); |
| 438 | let handle = engine::export::start(project.clone(), meta, &out, &ExportSettings::default(), None); |
| 439 | wait(&handle, limit).expect("export"); |
| 440 | println!( |
| 441 | "{name}: {total} frames of two sources in {:.2} s", |
| 442 | started.elapsed().as_secs_f64() |
| 443 | ); |
| 444 | |
| 445 | let (written, _) = engine::demux::Demuxer::open(&out).expect("reopen export"); |
| 446 | assert_eq!(written.frame_count, total, "timeline frames written"); |
| 447 | assert_eq!((written.width, written.height), (1280, 720)); |
| 448 | |
| 449 | let frames = decode_all(&out); |
| 450 | assert_eq!( |
| 451 | frames.len() as u32, |
| 452 | total, |
| 453 | "every written frame decodes back" |
| 454 | ); |
| 455 | |
| 456 | for (timeline, source, source_frame) in [ |
| 457 | (0u32, "test_av.mp4", 0u32), |
| 458 | (119, "test_av.mp4", 119), |
| 459 | (120, "test_av2.mp4", 0), |
| 460 | (total - 1, "test_av2.mp4", total - 121), |
| 461 | ] { |
| 462 | let diff = mean_abs_diff( |
| 463 | &frames[timeline as usize], |
| 464 | &frame_at(&asset(source), source_frame), |
| 465 | ); |
| 466 | println!("timeline {timeline} vs {source} {source_frame}: mean abs diff {diff:.2}"); |
| 467 | assert!( |
| 468 | diff < 6.0, |
| 469 | "timeline frame {timeline} drifted by {diff:.2} from {source} frame {source_frame}" |
| 470 | ); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | let file = File::open(&out).unwrap(); |
| 480 | let size = file.metadata().unwrap().len(); |
| 481 | let reader = mp4::Mp4Reader::read_header(BufReader::new(file), size).unwrap(); |
| 482 | let packets = reader.sample_count(2).expect("audio track"); |
| 483 | assert_eq!(packets, 346, "packet total across the source join"); |
| 484 | let wanted = f64::from(total) / FPS * 44_100.0; |
| 485 | assert!( |
| 486 | (f64::from(packets * 1024) - wanted).abs() < 3.0 * 1024.0, |
| 487 | "audio length {} samples drifted from {wanted}", |
| 488 | packets * 1024 |
| 489 | ); |
| 490 | let (audio, _) = engine::AudioSession::open(&out) |
| 491 | .expect("reopen export audio") |
| 492 | .expect("export has an audio track"); |
| 493 | assert_eq!((audio.sample_rate, audio.channels), (44_100, 2)); |
| 494 | std::fs::remove_file(&out).unwrap(); |
| 495 | } |
| 496 | |
| 497 | #[test] |
| 498 | fn exports_two_sources_as_one_timeline_in_software() { |
| 499 | let _seat = pin_software(); |
| 500 | multi_source_round_trip("multi_sw", Duration::from_secs(900)); |
| 501 | } |
| 502 | |
| 503 | #[test] |
| 504 | #[ignore] |
| 505 | fn exports_two_sources_as_one_timeline_in_hardware() { |
| 506 | let _seat = pin_hardware(); |
| 507 | multi_source_round_trip("multi_hw", Duration::from_secs(60)); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | #[test] |
| 515 | fn a_higher_bitrate_writes_a_bigger_file() { |
| 516 | let _seat = pin_software(); |
| 517 | let mut sizes = Vec::new(); |
| 518 | for bitrate in [1_000_000u64, 4_000_000, 8_000_000] { |
| 519 | let mut session = edited(&asset("test_baseline.mp4")); |
| 520 | session.pause(); |
| 521 | let out = out_path(&format!("bitrate_{bitrate}")); |
| 522 | let settings = ExportSettings { |
| 523 | bitrate: Some(bitrate), |
| 524 | seat: engine::export::EncoderSeat::Software, |
| 525 | ..Default::default() |
| 526 | }; |
| 527 | let handle = session.export_to_with(&out, &settings); |
| 528 | wait(&handle, Duration::from_secs(900)).expect("export"); |
| 529 | let size = std::fs::metadata(&out).expect("export exists").len(); |
| 530 | println!("{} Mbps: {size} bytes", bitrate / 1_000_000); |
| 531 | sizes.push(size); |
| 532 | std::fs::remove_file(&out).unwrap(); |
| 533 | } |
| 534 | assert!( |
| 535 | sizes[0] < sizes[1] && sizes[1] < sizes[2], |
| 536 | "file sizes {sizes:?} are not strictly growing with the bitrate" |
| 537 | ); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | #[test] |
| 545 | fn a_vanished_source_fails_the_export() { |
| 546 | let _seat = pin_software(); |
| 547 | let baseline = asset("test_baseline.mp4"); |
| 548 | let doomed = Scratch::file("ve_export_vanished_source", "mp4"); |
| 549 | std::fs::copy(&baseline, &doomed).expect("copy a second source"); |
| 550 | let (meta, _) = engine::demux::Demuxer::open(&baseline).unwrap(); |
| 551 | |
| 552 | |
| 553 | let mut project = Project::single(&baseline, meta.frame_count); |
| 554 | assert!(project.split(5)); |
| 555 | assert!(project.delete(1)); |
| 556 | let second = project.import(&doomed, 0); |
| 557 | assert!(project.append_clip(second, meta.frame_count)); |
| 558 | assert!(project.split(10)); |
| 559 | assert!(project.delete(2)); |
| 560 | assert_eq!(project.timeline_frames(), 10); |
| 561 | |
| 562 | std::fs::remove_file(&doomed).expect("unlink the second source"); |
| 563 | let out = out_path("vanished"); |
| 564 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 565 | let result = wait(&handle, Duration::from_secs(60)); |
| 566 | let error = result.expect_err("an export of a missing file cannot succeed"); |
| 567 | println!("vanished source: {error}"); |
| 568 | assert!( |
| 569 | !out.exists(), |
| 570 | "a failed export still wrote {}", |
| 571 | out.display() |
| 572 | ); |
| 573 | assert!(!part_path(&out).exists(), "the .part outlived the failure"); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | |
| 578 | #[test] |
| 579 | fn cancelling_leaves_no_file() { |
| 580 | let _seat = pin_software(); |
| 581 | let mut session = edited(&asset("test_baseline.mp4")); |
| 582 | session.pause(); |
| 583 | let out = out_path("cancel"); |
| 584 | |
| 585 | let handle = session.export_to(&out); |
| 586 | let started = Instant::now(); |
| 587 | while handle.progress() < 0.1 { |
| 588 | assert!( |
| 589 | started.elapsed() < Duration::from_secs(60), |
| 590 | "export never got going" |
| 591 | ); |
| 592 | assert!( |
| 593 | !handle.is_finished(), |
| 594 | "export finished before it was cancelled" |
| 595 | ); |
| 596 | std::thread::sleep(Duration::from_millis(10)); |
| 597 | } |
| 598 | handle.cancel(); |
| 599 | let result = wait(&handle, Duration::from_secs(5)); |
| 600 | assert!(result.is_err(), "a cancelled export is an error"); |
| 601 | assert!(!out.exists(), "no partial file survives a cancel"); |
| 602 | assert!(!part_path(&out).exists(), "the .part is cleaned up too"); |
| 603 | } |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | #[test] |
| 614 | fn cancelling_after_the_last_frame_leaves_no_file() { |
| 615 | let _seat = pin_software(); |
| 616 | let out = out_path("late"); |
| 617 | let part = part_path(&out); |
| 618 | for attempt in 1..=5 { |
| 619 | let mut session = edited(&asset("test_baseline.mp4")); |
| 620 | session.pause(); |
| 621 | let handle = session.export_to(&out); |
| 622 | |
| 623 | |
| 624 | let started = Instant::now(); |
| 625 | while handle.progress() < 0.9 && !handle.is_finished() { |
| 626 | assert!( |
| 627 | started.elapsed() < Duration::from_secs(600), |
| 628 | "export stalled" |
| 629 | ); |
| 630 | std::thread::sleep(Duration::from_millis(20)); |
| 631 | } |
| 632 | while handle.progress() < 1.0 && !handle.is_finished() { |
| 633 | std::hint::spin_loop(); |
| 634 | } |
| 635 | handle.cancel(); |
| 636 | |
| 637 | let result = wait(&handle, Duration::from_secs(30)); |
| 638 | assert!(!part.exists(), "attempt {attempt} left a .part behind"); |
| 639 | if result.is_err() { |
| 640 | assert!(!out.exists(), "a late cancel still wrote {}", out.display()); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | assert_eq!( |
| 645 | engine::demux::Demuxer::open(&out).unwrap().0.frame_count, |
| 646 | 120 |
| 647 | ); |
| 648 | std::fs::remove_file(&out).unwrap(); |
| 649 | } |
| 650 | panic!("cancel never landed in the drain/audio/finish window"); |
| 651 | } |
| 652 | |
| 653 | |
| 654 | |
| 655 | #[test] |
| 656 | #[ignore] |
| 657 | fn exports_audio_alongside_the_video() { |
| 658 | let _seat = pin_hardware(); |
| 659 | let source = asset("test_av.mp4"); |
| 660 | let mut session = PlaybackSession::open(&source).expect("open source"); |
| 661 | |
| 662 | |
| 663 | assert!(session.cut_at(2.0)); |
| 664 | session.pause(); |
| 665 | let timeline = session.timeline_duration(); |
| 666 | let out = out_path("av"); |
| 667 | |
| 668 | let handle = session.export_to(&out); |
| 669 | wait(&handle, Duration::from_secs(600)).expect("export"); |
| 670 | |
| 671 | let file = File::open(&out).unwrap(); |
| 672 | let size = file.metadata().unwrap().len(); |
| 673 | let reader = mp4::Mp4Reader::read_header(BufReader::new(file), size).unwrap(); |
| 674 | let packets = reader.sample_count(2).expect("audio track"); |
| 675 | let expected = engine::AudioSession::copy_segments(&source, &[(0.0, 2.0), (2.0, 5.0)]) |
| 676 | .unwrap() |
| 677 | .expect("source has audio") |
| 678 | .1 |
| 679 | .len() as u32; |
| 680 | assert_eq!(packets, expected, "every copied packet reached the file"); |
| 681 | |
| 682 | |
| 683 | let samples = f64::from(packets * 1024); |
| 684 | let wanted = timeline * 44_100.0; |
| 685 | println!("audio: {packets} packets = {samples} samples for {wanted} of timeline"); |
| 686 | assert!( |
| 687 | (samples - wanted).abs() < 3.0 * 1024.0, |
| 688 | "audio length {samples} drifted from {wanted}" |
| 689 | ); |
| 690 | |
| 691 | let (meta, chunks) = engine::AudioSession::open(&out) |
| 692 | .expect("reopen export audio") |
| 693 | .expect("export has an audio track"); |
| 694 | assert_eq!(meta.sample_rate, 44_100); |
| 695 | assert_eq!(meta.channels, 2); |
| 696 | let decoded: usize = chunks.into_iter().map(|c| c.samples.len()).sum(); |
| 697 | assert!( |
| 698 | decoded as f64 / 2.0 > wanted - 3.0 * 1024.0, |
| 699 | "only {decoded} samples decoded back out" |
| 700 | ); |
| 701 | |
| 702 | let (video, _) = engine::demux::Demuxer::open(&out).unwrap(); |
| 703 | assert_eq!(video.frame_count, 150, "video track unaffected by the cut"); |
| 704 | std::fs::remove_file(&out).unwrap(); |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | #[test] |
| 711 | #[ignore] |
| 712 | fn hardware_access_units_repack_into_mp4() { |
| 713 | let (w, h) = (640u32, 480u32); |
| 714 | let mut encoder = |
| 715 | HwEncoder::open(w, h, 30, 1, 4_000_000).expect("no hardware encode plugin/driver"); |
| 716 | let out = out_path("hwmux"); |
| 717 | let mut muxer: Option<Mp4Muxer> = None; |
| 718 | let mut written = 0; |
| 719 | for index in 0..10u32 { |
| 720 | let (y, u, v) = synthetic(index, w as usize, h as usize); |
| 721 | let Some(au) = encoder.encode(&y, &u, &v, w, h, false).expect("encode") else { |
| 722 | continue; |
| 723 | }; |
| 724 | if muxer.is_none() { |
| 725 | let (sps, pps) = parameter_sets(au).expect("first access unit carries SPS+PPS"); |
| 726 | muxer = Some( |
| 727 | Mp4Muxer::create( |
| 728 | &out, |
| 729 | &VideoParams { |
| 730 | width: w, |
| 731 | height: h, |
| 732 | frame_rate: 30.0, |
| 733 | sps, |
| 734 | pps, |
| 735 | }, |
| 736 | None, |
| 737 | ) |
| 738 | .expect("create"), |
| 739 | ); |
| 740 | } |
| 741 | muxer.as_mut().unwrap().write_video_au(au).expect("write"); |
| 742 | written += 1; |
| 743 | } |
| 744 | while let Some(au) = encoder.drain().expect("drain") { |
| 745 | muxer.as_mut().unwrap().write_video_au(au).expect("write"); |
| 746 | written += 1; |
| 747 | } |
| 748 | muxer.unwrap().finish().expect("finish"); |
| 749 | assert_eq!(written, 10); |
| 750 | |
| 751 | let (meta, mut demuxer) = engine::demux::Demuxer::open(&out).unwrap(); |
| 752 | assert_eq!(meta.frame_count, 10); |
| 753 | let mut decoder = rusty_h264::Decoder::new(); |
| 754 | let mut decoded = 0; |
| 755 | while let Some(au) = demuxer.next_access_unit().unwrap() { |
| 756 | if decoder |
| 757 | .decode(&au) |
| 758 | .expect("software decode of the remuxed stream") |
| 759 | .is_some() |
| 760 | { |
| 761 | decoded += 1; |
| 762 | } |
| 763 | } |
| 764 | assert_eq!(decoded, 10, "every remuxed picture decodes again"); |
| 765 | std::fs::remove_file(&out).unwrap(); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | |
| 770 | fn synthetic(index: u32, width: usize, height: usize) -> (Vec<u8>, Vec<u8>, Vec<u8>) { |
| 771 | let (cw, ch) = (width.div_ceil(2), height.div_ceil(2)); |
| 772 | let mut y = vec![0u8; width * height]; |
| 773 | for row in 0..height { |
| 774 | for col in 0..width { |
| 775 | y[row * width + col] = (row + col + index as usize * 5) as u8; |
| 776 | } |
| 777 | } |
| 778 | ( |
| 779 | y, |
| 780 | vec![64u8.wrapping_add(index as u8); cw * ch], |
| 781 | vec![192u8.wrapping_sub(index as u8); cw * ch], |
| 782 | ) |
| 783 | } |
| 784 | |
| 785 | |
| 786 | |
| 787 | |
| 788 | |
| 789 | |
| 790 | #[test] |
| 791 | fn a_leading_audio_gap_keeps_the_sound_after_it_in_place() { |
| 792 | let _seat = pin_software(); |
| 793 | let source = asset("test_av.mp4"); |
| 794 | let mut session = PlaybackSession::open(&source).expect("open source"); |
| 795 | assert!(session.cut_at(1.0), "cut at 1 s"); |
| 796 | assert!(session.lift_clip(Lane::A1, 0), "lift the first second"); |
| 797 | session.pause(); |
| 798 | let out = out_path("leading_gap"); |
| 799 | let handle = session.export_to(&out); |
| 800 | wait(&handle, Duration::from_secs(900)).expect("export"); |
| 801 | |
| 802 | let (_, chunks) = engine::AudioSession::open(&out) |
| 803 | .expect("reopen export audio") |
| 804 | .expect("export has an audio track"); |
| 805 | let mut pcm: Vec<f32> = Vec::new(); |
| 806 | while let Ok(chunk) = chunks.recv_timeout(Duration::from_secs(10)) { |
| 807 | pcm.resize(chunk.start_sample as usize * 2, 0.0); |
| 808 | pcm.extend_from_slice(&chunk.samples); |
| 809 | } |
| 810 | |
| 811 | |
| 812 | |
| 813 | let block = 1024 * 2; |
| 814 | let peak = |b: &[f32]| b.iter().fold(0.0f32, |m, s| m.max(s.abs())); |
| 815 | let loud = peak(&pcm) / 4.0; |
| 816 | let onset = pcm |
| 817 | .chunks(block) |
| 818 | .position(|b| peak(b) > loud) |
| 819 | .expect("the export has no sound at all") as f64 |
| 820 | * 1024.0; |
| 821 | println!("leading gap: sound starts at sample {onset} of an expected 44100"); |
| 822 | assert!( |
| 823 | (onset - 44_100.0).abs() <= 1024.0, |
| 824 | "the sound after the gap is at {onset}, not at the 1 s hole's end" |
| 825 | ); |
| 826 | |
| 827 | let quiet = peak(&pcm[..(0.9 * 44_100.0) as usize * 2]); |
| 828 | assert!(quiet < 1e-3, "the lifted second is not silent: {quiet}"); |
| 829 | |
| 830 | std::fs::remove_file(&out).unwrap(); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | #[test] |
| 839 | fn exports_a_gapped_timeline_as_black_and_silence() { |
| 840 | let _seat = pin_software(); |
| 841 | let source = asset("test_av.mp4"); |
| 842 | let mut session = PlaybackSession::open(&source).expect("open source"); |
| 843 | assert!(session.cut_at(1.0), "split at 1 s"); |
| 844 | assert!(session.cut_at(2.0), "split at 2 s"); |
| 845 | |
| 846 | |
| 847 | assert!(session.lift_clip(Lane::V1, 1)); |
| 848 | assert!(session.lift_clip(Lane::A1, 1)); |
| 849 | session.pause(); |
| 850 | let timeline = session.timeline_duration(); |
| 851 | let frames = (timeline * FPS).round() as u32; |
| 852 | let out = out_path("gapped"); |
| 853 | |
| 854 | let handle = session.export_to(&out); |
| 855 | wait(&handle, Duration::from_secs(900)).expect("export"); |
| 856 | |
| 857 | |
| 858 | let (video, _) = engine::demux::Demuxer::open(&out).unwrap(); |
| 859 | assert_eq!( |
| 860 | video.frame_count, frames, |
| 861 | "a gap is encoded, not skipped: {} frames for a {timeline:.3}s timeline", |
| 862 | video.frame_count |
| 863 | ); |
| 864 | let pictures = decode_all(&out); |
| 865 | assert_eq!(pictures.len() as u32, frames); |
| 866 | let (hole, hole_end) = ((1.0 * FPS) as usize, (2.0 * FPS) as usize); |
| 867 | |
| 868 | |
| 869 | let black = [0u8, 0, 0, 255].repeat(pictures[0].len() / 4); |
| 870 | for (i, picture) in pictures.iter().enumerate() { |
| 871 | let diff = mean_abs_diff(picture, &black); |
| 872 | if (hole..hole_end).contains(&i) { |
| 873 | assert!( |
| 874 | diff < 4.0, |
| 875 | "frame {i} of the gap is not black (MAE {diff:.2})" |
| 876 | ); |
| 877 | } |
| 878 | } |
| 879 | let lit = mean_abs_diff(&pictures[0], &black); |
| 880 | assert!(lit > 4.0, "the clips around the gap came out black too"); |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | let file = File::open(&out).unwrap(); |
| 886 | let size = file.metadata().unwrap().len(); |
| 887 | let reader = mp4::Mp4Reader::read_header(BufReader::new(file), size).unwrap(); |
| 888 | let count = reader.sample_count(2).expect("audio track"); |
| 889 | let samples = f64::from(count * 1024); |
| 890 | let wanted = timeline * 44_100.0; |
| 891 | println!("gapped export: {count} packets = {samples} samples for {wanted}"); |
| 892 | assert!( |
| 893 | (samples - wanted).abs() < 3.0 * 1024.0, |
| 894 | "audio length {samples} drifted from {wanted} across the gap" |
| 895 | ); |
| 896 | |
| 897 | |
| 898 | let (_, chunks) = engine::AudioSession::open(&out) |
| 899 | .expect("reopen export audio") |
| 900 | .expect("export has an audio track"); |
| 901 | let mut pcm: Vec<f32> = Vec::new(); |
| 902 | while let Ok(chunk) = chunks.recv_timeout(Duration::from_secs(10)) { |
| 903 | pcm.resize(chunk.start_sample as usize * 2, 0.0); |
| 904 | pcm.extend_from_slice(&chunk.samples); |
| 905 | } |
| 906 | let window = |from: f64, to: f64| { |
| 907 | let (a, b) = ((from * 44_100.0) as usize * 2, (to * 44_100.0) as usize * 2); |
| 908 | pcm[a.min(pcm.len())..b.min(pcm.len())] |
| 909 | .iter() |
| 910 | .fold(0.0f32, |m, s| m.max(s.abs())) |
| 911 | }; |
| 912 | |
| 913 | |
| 914 | let quiet = window(1.2, 1.8); |
| 915 | assert!(quiet < 1e-3, "the gap is not silent: {quiet}"); |
| 916 | assert!(window(0.1, 0.9) > 1e-2, "the sound before it went missing"); |
| 917 | assert!(window(2.2, 4.9) > 1e-2, "the sound after it went missing"); |
| 918 | |
| 919 | std::fs::remove_file(&out).unwrap(); |
| 920 | } |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | #[test] |
| 929 | fn a_proxy_is_picture_only_every_frame_a_starting_point_and_cached() { |
| 930 | let _film = PROXY_FILM.lock().unwrap_or_else(|e| e.into_inner()); |
| 931 | let _seat = pin_software(); |
| 932 | let source = asset("test_av.mp4"); |
| 933 | let out = engine::proxy::path_for(&source).expect("a cache directory"); |
| 934 | |
| 935 | |
| 936 | |
| 937 | let _ = std::fs::remove_file(&out); |
| 938 | |
| 939 | let job = engine::proxy::generate(&source).expect("start the proxy"); |
| 940 | assert_eq!(job.path(), out, "the job writes where the key says"); |
| 941 | let started = Instant::now(); |
| 942 | while !job.is_finished() { |
| 943 | assert!( |
| 944 | started.elapsed() < Duration::from_secs(900), |
| 945 | "the proxy did not finish: {:.0}% in", |
| 946 | job.progress() * 100.0 |
| 947 | ); |
| 948 | std::thread::sleep(Duration::from_millis(20)); |
| 949 | } |
| 950 | let made = job |
| 951 | .outcome() |
| 952 | .expect("a finished job has an outcome") |
| 953 | .expect("the proxy"); |
| 954 | assert_eq!(made, out); |
| 955 | assert_eq!(job.progress(), 1.0, "finished at full progress"); |
| 956 | |
| 957 | let (source_meta, _) = engine::demux::Demuxer::open(&source).expect("open the fixture"); |
| 958 | let (meta, _) = engine::demux::Demuxer::open(&made).expect("open the proxy"); |
| 959 | assert_eq!( |
| 960 | (meta.width, meta.height), |
| 961 | engine::proxy::size_for(&source_meta), |
| 962 | "the proxy is coded at the size the rule picked" |
| 963 | ); |
| 964 | assert_eq!(meta.codec, engine::Codec::H264, "proxies are H.264"); |
| 965 | assert_eq!( |
| 966 | meta.frame_count, source_meta.frame_count, |
| 967 | "a stand-in as long as the film, or every cut lands elsewhere" |
| 968 | ); |
| 969 | |
| 970 | |
| 971 | |
| 972 | let syncs = engine::demux::sync_points(&made); |
| 973 | assert_eq!( |
| 974 | syncs.len() as u32, |
| 975 | meta.frame_count, |
| 976 | "{} of {} frames are starting points -- the proxy is not intra-only", |
| 977 | syncs.len(), |
| 978 | meta.frame_count |
| 979 | ); |
| 980 | |
| 981 | |
| 982 | assert!( |
| 983 | engine::AudioSession::open(&made) |
| 984 | .expect("probe the proxy for sound") |
| 985 | .is_none(), |
| 986 | "a proxy carries no audio track" |
| 987 | ); |
| 988 | |
| 989 | |
| 990 | let session = PlaybackSession::open(&made).expect("play the proxy"); |
| 991 | assert_eq!(session.meta().frame_count, source_meta.frame_count); |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | let mut session = PlaybackSession::open(&source).expect("open the film"); |
| 997 | |
| 998 | |
| 999 | |
| 1000 | let film = session.sources()[0].path.clone(); |
| 1001 | assert_eq!(session.picture_path(0), film, "off: the film itself"); |
| 1002 | session.set_proxies(true); |
| 1003 | assert!(session.proxies()); |
| 1004 | assert_eq!(session.picture_path(0), made, "on: the stand-in"); |
| 1005 | assert_eq!( |
| 1006 | session.sources()[0].path, |
| 1007 | film, |
| 1008 | "the project still names the film, and that is what an export reads" |
| 1009 | ); |
| 1010 | |
| 1011 | |
| 1012 | let started = Instant::now(); |
| 1013 | while session.try_frame().is_none() { |
| 1014 | assert!( |
| 1015 | started.elapsed() < Duration::from_secs(10), |
| 1016 | "no picture after switching to the stand-in" |
| 1017 | ); |
| 1018 | std::thread::sleep(Duration::from_millis(10)); |
| 1019 | } |
| 1020 | session.set_proxies(false); |
| 1021 | assert_eq!(session.picture_path(0), film, "and back again"); |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | let project = Scratch::file("ve_proxy_switch", "edith"); |
| 1026 | session.set_proxies(true); |
| 1027 | session.save_project(&project).expect("save"); |
| 1028 | let text = std::fs::read_to_string(&project).expect("read the project back"); |
| 1029 | assert!(text.starts_with("edith 20\n"), "{text}"); |
| 1030 | assert!(text.contains("\nproxy on\n"), "{text}"); |
| 1031 | let reopened = PlaybackSession::open_project(&project).expect("load"); |
| 1032 | assert!(reopened.proxies(), "the switch came back on"); |
| 1033 | assert_eq!(reopened.picture_path(0), made, "and it is substituting"); |
| 1034 | |
| 1035 | |
| 1036 | let again = engine::proxy::generate(&source).expect("ask again"); |
| 1037 | assert!(again.is_finished(), "a cached proxy is done on the spot"); |
| 1038 | assert_eq!( |
| 1039 | again.outcome().expect("cached outcome").expect("cached path"), |
| 1040 | out |
| 1041 | ); |
| 1042 | assert_eq!(engine::proxy::cached(&source), Some(out.clone())); |
| 1043 | |
| 1044 | std::fs::remove_file(&out).expect("clean the cache entry up"); |
| 1045 | assert_eq!(engine::proxy::cached(&source), None); |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | #[test] |
| 1065 | fn a_stopped_proxy_leaves_neither_a_stand_in_nor_half_of_one() { |
| 1066 | let _film = PROXY_FILM.lock().unwrap_or_else(|e| e.into_inner()); |
| 1067 | let _seat = pin_software(); |
| 1068 | let source = asset("test_av.mp4"); |
| 1069 | let out = engine::proxy::path_for(&source).expect("a cache directory"); |
| 1070 | let part = part_path(&out); |
| 1071 | |
| 1072 | |
| 1073 | let _ = std::fs::remove_file(&out); |
| 1074 | let _ = std::fs::remove_file(&part); |
| 1075 | |
| 1076 | let job = engine::proxy::generate(&source).expect("start the proxy"); |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | let opening = Instant::now(); |
| 1082 | while job.encoder().is_none() && !job.is_finished() { |
| 1083 | assert!( |
| 1084 | opening.elapsed() < Duration::from_secs(600), |
| 1085 | "the encoder never opened" |
| 1086 | ); |
| 1087 | std::thread::sleep(Duration::from_millis(10)); |
| 1088 | } |
| 1089 | assert!( |
| 1090 | !job.is_finished(), |
| 1091 | "the fixture finished before it could be stopped: this test needs a film \ |
| 1092 | the software seat takes a moment over" |
| 1093 | ); |
| 1094 | |
| 1095 | job.cancel(); |
| 1096 | job.cancel(); |
| 1097 | let stopping = Instant::now(); |
| 1098 | while !job.is_finished() { |
| 1099 | assert!( |
| 1100 | stopping.elapsed() < Duration::from_secs(120), |
| 1101 | "the worker kept encoding after the stop: {:.0}% in", |
| 1102 | job.progress() * 100. |
| 1103 | ); |
| 1104 | std::thread::sleep(Duration::from_millis(20)); |
| 1105 | } |
| 1106 | let refusal = job |
| 1107 | .outcome() |
| 1108 | .expect("a settled job has an outcome") |
| 1109 | .err() |
| 1110 | .expect("a stopped proxy is not a proxy"); |
| 1111 | assert!( |
| 1112 | refusal.to_string().contains("cancelled"), |
| 1113 | "not the cancellation: {refusal}" |
| 1114 | ); |
| 1115 | assert!( |
| 1116 | !out.exists(), |
| 1117 | "a stopped proxy left {} behind, and the cache is looked up by existence", |
| 1118 | out.display() |
| 1119 | ); |
| 1120 | assert!( |
| 1121 | !part.exists(), |
| 1122 | "a stopped proxy left the half-written {} behind", |
| 1123 | part.display() |
| 1124 | ); |
| 1125 | assert_eq!( |
| 1126 | engine::proxy::cached(&source), |
| 1127 | None, |
| 1128 | "the cache offers a stand-in that was never finished" |
| 1129 | ); |
| 1130 | |
| 1131 | job.cancel(); |
| 1132 | assert!(job.outcome().is_none(), "the outcome came out twice"); |
| 1133 | assert!(!out.exists()); |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | std::fs::write(&out, b"a stand-in that is already written").expect("seed the cache"); |
| 1140 | let hit = engine::proxy::generate(&source).expect("ask again"); |
| 1141 | assert!(hit.is_finished(), "a cached stand-in is done on the spot"); |
| 1142 | hit.cancel(); |
| 1143 | assert!( |
| 1144 | out.is_file(), |
| 1145 | "a stop deleted a finished stand-in the switch was playing" |
| 1146 | ); |
| 1147 | assert_eq!(engine::proxy::cached(&source), Some(out.clone())); |
| 1148 | assert_eq!( |
| 1149 | hit.outcome().expect("cached outcome").expect("cached path"), |
| 1150 | out, |
| 1151 | "the stop turned a finished stand-in into a failure" |
| 1152 | ); |
| 1153 | |
| 1154 | std::fs::remove_file(&out).expect("clean the cache entry up"); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | #[test] |
| 1172 | #[ignore = "needs the VA-API plugin"] |
| 1173 | fn a_hardware_proxy_is_every_frame_a_starting_point_too() { |
| 1174 | let _seat = pin_hardware(); |
| 1175 | let source = asset("test_av.mp4"); |
| 1176 | let out = engine::proxy::path_for(&source).expect("a cache directory"); |
| 1177 | let _ = std::fs::remove_file(&out); |
| 1178 | let job = engine::proxy::generate(&source).expect("start the proxy"); |
| 1179 | let started = Instant::now(); |
| 1180 | while !job.is_finished() { |
| 1181 | assert!( |
| 1182 | started.elapsed() < Duration::from_secs(900), |
| 1183 | "the proxy did not finish" |
| 1184 | ); |
| 1185 | std::thread::sleep(Duration::from_millis(20)); |
| 1186 | } |
| 1187 | let made = job |
| 1188 | .outcome() |
| 1189 | .expect("a finished job has an outcome") |
| 1190 | .expect("the proxy"); |
| 1191 | let seat = job.encoder().unwrap_or_default(); |
| 1192 | println!("hardware proxy seat: {seat}"); |
| 1193 | |
| 1194 | |
| 1195 | assert!( |
| 1196 | seat.contains("HW encode"), |
| 1197 | "not the hardware seat ({seat}): LD_LIBRARY_PATH must name the plugin" |
| 1198 | ); |
| 1199 | let (meta, _) = engine::demux::Demuxer::open(&made).expect("open the proxy"); |
| 1200 | let syncs = engine::demux::sync_points(&made); |
| 1201 | println!("{} of {} frames are starting points", syncs.len(), meta.frame_count); |
| 1202 | assert_eq!( |
| 1203 | syncs.len() as u32, |
| 1204 | meta.frame_count, |
| 1205 | "the hardware seat wrote {} starting points in {} frames", |
| 1206 | syncs.len(), |
| 1207 | meta.frame_count |
| 1208 | ); |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | let frames = decode_all(&made); |
| 1215 | println!("decoded back {} of {} pictures", frames.len(), meta.frame_count); |
| 1216 | assert_eq!( |
| 1217 | frames.len() as u32, |
| 1218 | meta.frame_count, |
| 1219 | "the proxy demuxes but does not decode" |
| 1220 | ); |
| 1221 | assert!( |
| 1222 | frames.iter().all(|f| f.iter().any(|&b| b != 0)), |
| 1223 | "a decoded picture came back empty" |
| 1224 | ); |
| 1225 | match ffmpeg_complaints(&made) { |
| 1226 | Some(said) => assert!(said.is_empty(), "ffmpeg read the proxy and said:\n{said}"), |
| 1227 | None => eprintln!("no ffmpeg: skipping the outside decoder's word on the proxy"), |
| 1228 | } |
| 1229 | std::fs::remove_file(&out).expect("clean the cache entry up"); |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | #[test] |
| 1245 | #[ignore = "needs the VA-API plugin and a real library"] |
| 1246 | fn a_proxy_of_real_4k_film_is_made_faster_than_it_plays() { |
| 1247 | let _seat = pin_hardware(); |
| 1248 | let Some(film) = engine::real_library::film("hevc_4k_hdr") else { |
| 1249 | return; |
| 1250 | }; |
| 1251 | let out = engine::proxy::path_for(&film).expect("a cache directory"); |
| 1252 | let _ = std::fs::remove_file(&out); |
| 1253 | let (meta, _) = engine::demux::Demuxer::open(&film).expect("open the film"); |
| 1254 | let duration = f64::from(meta.frame_count) / meta.frame_rate; |
| 1255 | println!( |
| 1256 | "film: {}x{} {:.3} fps, {duration:.0}s", |
| 1257 | meta.width, meta.height, meta.frame_rate |
| 1258 | ); |
| 1259 | |
| 1260 | let job = engine::proxy::generate(&film).expect("start the proxy"); |
| 1261 | let window = Duration::from_secs(60); |
| 1262 | let started = Instant::now(); |
| 1263 | while started.elapsed() < window && !job.is_finished() { |
| 1264 | std::thread::sleep(Duration::from_millis(200)); |
| 1265 | } |
| 1266 | let elapsed = started.elapsed().as_secs_f64(); |
| 1267 | let made = f64::from(job.progress()) * duration; |
| 1268 | let seat = job.encoder().unwrap_or_default(); |
| 1269 | println!( |
| 1270 | "proxy: {made:.0}s of film in {elapsed:.0}s = {:.2}x realtime, seat {seat}", |
| 1271 | made / elapsed |
| 1272 | ); |
| 1273 | |
| 1274 | |
| 1275 | assert!( |
| 1276 | seat.contains("HW encode"), |
| 1277 | "not the hardware seat ({seat}): LD_LIBRARY_PATH must name the plugin" |
| 1278 | ); |
| 1279 | |
| 1280 | |
| 1281 | |
| 1282 | |
| 1283 | |
| 1284 | |
| 1285 | assert!( |
| 1286 | made / elapsed >= 2.0, |
| 1287 | "{:.2}x realtime is under the 2x floor this shipped at", |
| 1288 | made / elapsed |
| 1289 | ); |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | job.cancel(); |
| 1294 | let stopped = Instant::now(); |
| 1295 | while !job.is_finished() { |
| 1296 | assert!( |
| 1297 | stopped.elapsed() < Duration::from_secs(30), |
| 1298 | "the proxy did not stop when cancelled" |
| 1299 | ); |
| 1300 | std::thread::sleep(Duration::from_millis(50)); |
| 1301 | } |
| 1302 | assert!(job.outcome().expect("an outcome").is_err(), "cancelled"); |
| 1303 | assert!( |
| 1304 | !out.exists(), |
| 1305 | "a cancelled proxy left {} behind", |
| 1306 | out.display() |
| 1307 | ); |
| 1308 | assert!(!part_path(&out).exists(), "a cancelled proxy left its .part"); |
| 1309 | assert_eq!(engine::proxy::cached(&film), None); |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | |
| 1334 | |
| 1335 | |
| 1336 | |
| 1337 | |
| 1338 | fn injected<T>(abort: Option<&str>, hang: Option<&str>, body: impl FnOnce() -> T) -> T { |
| 1339 | |
| 1340 | |
| 1341 | injected_with(abort, hang, Some("1500"), body) |
| 1342 | } |
| 1343 | |
| 1344 | |
| 1345 | |
| 1346 | |
| 1347 | fn injected_with<T>( |
| 1348 | abort: Option<&str>, |
| 1349 | hang: Option<&str>, |
| 1350 | timeout_ms: Option<&str>, |
| 1351 | body: impl FnOnce() -> T, |
| 1352 | ) -> T { |
| 1353 | let _exclusive = SEAT.write().unwrap_or_else(|poisoned| poisoned.into_inner()); |
| 1354 | let restore = std::env::var_os("VE_SW_ENC"); |
| 1355 | unsafe { |
| 1356 | std::env::set_var("VE_SW", "1"); |
| 1357 | std::env::remove_var("VE_SW_ENC"); |
| 1358 | std::env::set_var("VE_HW_TEST_FAKE", "1"); |
| 1359 | std::env::set_var("VE_HW_CHILD_BIN", env!("CARGO_BIN_EXE_hw-encode-child")); |
| 1360 | match timeout_ms { |
| 1361 | Some(ms) => std::env::set_var("VE_HW_TIMEOUT_MS", ms), |
| 1362 | None => std::env::remove_var("VE_HW_TIMEOUT_MS"), |
| 1363 | } |
| 1364 | match abort { |
| 1365 | Some(at) => std::env::set_var("VE_HW_TEST_ABORT", at), |
| 1366 | None => std::env::remove_var("VE_HW_TEST_ABORT"), |
| 1367 | } |
| 1368 | match hang { |
| 1369 | Some(at) => std::env::set_var("VE_HW_TEST_HANG", at), |
| 1370 | None => std::env::remove_var("VE_HW_TEST_HANG"), |
| 1371 | } |
| 1372 | } |
| 1373 | let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(body)); |
| 1374 | unsafe { |
| 1375 | for var in [ |
| 1376 | "VE_HW_TEST_FAKE", |
| 1377 | "VE_HW_CHILD_BIN", |
| 1378 | "VE_HW_TIMEOUT_MS", |
| 1379 | "VE_HW_TEST_ABORT", |
| 1380 | "VE_HW_TEST_HANG", |
| 1381 | ] { |
| 1382 | std::env::remove_var(var); |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | |
| 1387 | if let Some(pinned) = restore { |
| 1388 | std::env::set_var("VE_SW_ENC", pinned); |
| 1389 | } |
| 1390 | } |
| 1391 | match out { |
| 1392 | Ok(value) => value, |
| 1393 | Err(panic) => std::panic::resume_unwind(panic), |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | |
| 1399 | fn short_edit(source: &Path, frames: u32) -> (Project, engine::VideoMeta) { |
| 1400 | let (meta, _) = engine::demux::Demuxer::open(source).expect("open source"); |
| 1401 | let mut project = Project::single(source, meta.frame_count); |
| 1402 | assert!(project.split(frames)); |
| 1403 | assert!(project.delete(1)); |
| 1404 | assert_eq!(project.timeline_frames(), frames); |
| 1405 | (project, meta) |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | #[test] |
| 1416 | fn a_hardware_encoder_killed_mid_export_costs_the_file_nothing() { |
| 1417 | let source = asset("test_baseline.mp4"); |
| 1418 | let (project, meta) = short_edit(&source, 20); |
| 1419 | let out = out_path("hw_child_abort"); |
| 1420 | |
| 1421 | injected(Some("3"), None, || { |
| 1422 | |
| 1423 | |
| 1424 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 1425 | wait(&handle, Duration::from_secs(180)).expect("the export survived its encoder"); |
| 1426 | }); |
| 1427 | |
| 1428 | let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); |
| 1429 | assert_eq!(written.frame_count, 20, "every timeline frame was written"); |
| 1430 | assert_eq!(decode_all(&out).len(), 20, "and every one of them decodes"); |
| 1431 | if let Some(said) = ffmpeg_complaints(&out) { |
| 1432 | assert!( |
| 1433 | said.is_empty(), |
| 1434 | "ffmpeg read the fallback's file badly: {said}" |
| 1435 | ); |
| 1436 | } |
| 1437 | assert!(!part_path(&out).exists(), "the .part outlived the export"); |
| 1438 | std::fs::remove_file(&out).unwrap(); |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | |
| 1443 | |
| 1444 | |
| 1445 | #[test] |
| 1446 | fn a_hardware_encoder_killed_mid_export_never_pulls_the_bar_backwards() { |
| 1447 | let source = asset("test_baseline.mp4"); |
| 1448 | let (project, meta) = short_edit(&source, 20); |
| 1449 | let out = out_path("hw_child_abort_progress"); |
| 1450 | |
| 1451 | let seen = injected(Some("3"), None, || { |
| 1452 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 1453 | |
| 1454 | |
| 1455 | let started = Instant::now(); |
| 1456 | let mut seen = vec![handle.progress()]; |
| 1457 | while !handle.is_finished() { |
| 1458 | assert!( |
| 1459 | started.elapsed() < Duration::from_secs(180), |
| 1460 | "export did not finish" |
| 1461 | ); |
| 1462 | let now = handle.progress(); |
| 1463 | if now != *seen.last().unwrap() { |
| 1464 | seen.push(now); |
| 1465 | } |
| 1466 | std::thread::sleep(Duration::from_millis(1)); |
| 1467 | } |
| 1468 | handle |
| 1469 | .result() |
| 1470 | .expect("a finished export has an outcome") |
| 1471 | .expect("the export survived its encoder"); |
| 1472 | seen.push(handle.progress()); |
| 1473 | seen |
| 1474 | }); |
| 1475 | |
| 1476 | assert!( |
| 1477 | seen.windows(2).all(|pair| pair[1] >= pair[0]), |
| 1478 | "the bar went backwards across the fallback: {seen:?}" |
| 1479 | ); |
| 1480 | assert_eq!( |
| 1481 | seen.last().copied(), |
| 1482 | Some(1.0), |
| 1483 | "a finished export leaves a full bar: {seen:?}" |
| 1484 | ); |
| 1485 | std::fs::remove_file(&out).unwrap(); |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | fn audio_pass_threads() -> usize { |
| 1494 | std::fs::read_dir("/proc/self/task") |
| 1495 | .into_iter() |
| 1496 | .flatten() |
| 1497 | .flatten() |
| 1498 | .filter(|thread| { |
| 1499 | std::fs::read_to_string(thread.path().join("comm")) |
| 1500 | .is_ok_and(|name| name.trim() == "export-audio") |
| 1501 | }) |
| 1502 | .count() |
| 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | fn busiest_audio_pass(project: Project, meta: engine::VideoMeta, out: &Path) -> usize { |
| 1507 | let handle = engine::export::start(project, meta, out, &ExportSettings::default(), None); |
| 1508 | let started = Instant::now(); |
| 1509 | let mut most = 0; |
| 1510 | while !handle.is_finished() { |
| 1511 | assert!( |
| 1512 | started.elapsed() < Duration::from_secs(180), |
| 1513 | "export did not finish" |
| 1514 | ); |
| 1515 | most = most.max(audio_pass_threads()); |
| 1516 | std::thread::sleep(Duration::from_millis(1)); |
| 1517 | } |
| 1518 | handle |
| 1519 | .result() |
| 1520 | .expect("a finished export has an outcome") |
| 1521 | .expect("the export was written"); |
| 1522 | most |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | #[test] |
| 1532 | fn a_hardware_encoder_killed_mid_export_leaves_no_audio_pass_behind() { |
| 1533 | |
| 1534 | |
| 1535 | let source = asset("test_ac3.mp4"); |
| 1536 | let out = out_path("hw_child_abort_audio"); |
| 1537 | |
| 1538 | let (project, meta) = short_edit(&source, 40); |
| 1539 | let alone = { |
| 1540 | let _seat = pin_software(); |
| 1541 | busiest_audio_pass(project, meta, &out) |
| 1542 | }; |
| 1543 | std::fs::remove_file(&out).unwrap(); |
| 1544 | |
| 1545 | let (project, meta) = short_edit(&source, 40); |
| 1546 | let across = injected(Some("3"), None, || busiest_audio_pass(project, meta, &out)); |
| 1547 | |
| 1548 | println!("audio pass threads: {alone} alone, {across} across the fallback"); |
| 1549 | assert!( |
| 1550 | across <= alone, |
| 1551 | "the fallback ran {across} audio-pass threads where one pass runs {alone}" |
| 1552 | ); |
| 1553 | let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); |
| 1554 | assert_eq!(written.frame_count, 40, "every timeline frame was written"); |
| 1555 | std::fs::remove_file(&out).unwrap(); |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | |
| 1567 | #[test] |
| 1568 | fn a_wedged_child_is_waited_out_by_its_own_measure_not_the_ceiling() { |
| 1569 | let (width, height) = (320u32, 240u32); |
| 1570 | |
| 1571 | injected_with(None, Some("10"), None, || { |
| 1572 | let mut encoder = |
| 1573 | HwEncoder::open(width, height, 30, 1, 1_000_000).expect("the stand-in seat opened"); |
| 1574 | let (y, u, v) = synthetic(0, width as usize, height as usize); |
| 1575 | for frame in 0..10 { |
| 1576 | assert!( |
| 1577 | encoder.encode(&y, &u, &v, width, height, false).is_ok(), |
| 1578 | "picture {frame} is before the wedge" |
| 1579 | ); |
| 1580 | } |
| 1581 | let started = Instant::now(); |
| 1582 | let wedged = encoder |
| 1583 | .encode(&y, &u, &v, width, height, false) |
| 1584 | .expect_err("a child that stops answering is not an encode"); |
| 1585 | let waited = started.elapsed(); |
| 1586 | println!("wedge at the shipping default: contained in {waited:.2?}"); |
| 1587 | assert!(engine::hwproc::is_lost(&wedged), "{wedged}"); |
| 1588 | assert!( |
| 1589 | waited < Duration::from_secs(6), |
| 1590 | "waited {waited:.2?} on a wedged child at the shipping default" |
| 1591 | ); |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | assert!(waited >= Duration::from_secs(2), "waited only {waited:.2?}"); |
| 1596 | }); |
| 1597 | } |
| 1598 | |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | #[test] |
| 1603 | fn a_hardware_encoder_killed_at_its_open_is_simply_no_seat() { |
| 1604 | let source = asset("test_baseline.mp4"); |
| 1605 | let (project, meta) = short_edit(&source, 12); |
| 1606 | let out = out_path("hw_child_abort_init"); |
| 1607 | |
| 1608 | injected(Some("init"), None, || { |
| 1609 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 1610 | wait(&handle, Duration::from_secs(180)).expect("the export survived the open"); |
| 1611 | }); |
| 1612 | |
| 1613 | let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); |
| 1614 | assert_eq!(written.frame_count, 12); |
| 1615 | std::fs::remove_file(&out).unwrap(); |
| 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | #[test] |
| 1621 | fn a_hardware_encoder_killed_at_the_drain_costs_the_file_nothing() { |
| 1622 | let source = asset("test_baseline.mp4"); |
| 1623 | let (project, meta) = short_edit(&source, 12); |
| 1624 | let out = out_path("hw_child_abort_drain"); |
| 1625 | |
| 1626 | injected(Some("drain"), None, || { |
| 1627 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 1628 | wait(&handle, Duration::from_secs(180)).expect("the export survived the drain"); |
| 1629 | }); |
| 1630 | |
| 1631 | let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); |
| 1632 | assert_eq!(written.frame_count, 12); |
| 1633 | assert_eq!(decode_all(&out).len(), 12); |
| 1634 | std::fs::remove_file(&out).unwrap(); |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | #[test] |
| 1643 | fn a_hardware_encoder_that_stops_answering_is_killed_and_replaced() { |
| 1644 | let source = asset("test_baseline.mp4"); |
| 1645 | let (project, meta) = short_edit(&source, 12); |
| 1646 | let out = out_path("hw_child_hang"); |
| 1647 | |
| 1648 | let started = Instant::now(); |
| 1649 | injected(None, Some("2"), || { |
| 1650 | let handle = engine::export::start(project, meta, &out, &ExportSettings::default(), None); |
| 1651 | wait(&handle, Duration::from_secs(180)).expect("the export outlived the wedge"); |
| 1652 | }); |
| 1653 | println!( |
| 1654 | "hung child: contained in {:.2} s", |
| 1655 | started.elapsed().as_secs_f64() |
| 1656 | ); |
| 1657 | |
| 1658 | let (written, _) = engine::demux::Demuxer::open(&out).expect("the export is a file"); |
| 1659 | assert_eq!(written.frame_count, 12); |
| 1660 | assert_eq!(decode_all(&out).len(), 12); |
| 1661 | std::fs::remove_file(&out).unwrap(); |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | #[test] |
| 1674 | fn an_export_that_asked_for_the_gpu_by_name_refuses_instead_of_falling_back() { |
| 1675 | let source = asset("test_baseline.mp4"); |
| 1676 | let (project, meta) = short_edit(&source, 12); |
| 1677 | let out = out_path("hw_child_named_seat"); |
| 1678 | |
| 1679 | let error = injected(Some("2"), None, || { |
| 1680 | let settings = ExportSettings { |
| 1681 | seat: engine::export::EncoderSeat::Hardware, |
| 1682 | ..Default::default() |
| 1683 | }; |
| 1684 | let handle = engine::export::start(project, meta, &out, &settings, None); |
| 1685 | wait(&handle, Duration::from_secs(180)) |
| 1686 | .expect_err("a named hardware seat that died is not written again in software") |
| 1687 | }); |
| 1688 | |
| 1689 | println!("named hardware seat: {error}"); |
| 1690 | assert!( |
| 1691 | engine::hwproc::is_lost(&error), |
| 1692 | "the refusal has to name the hardware failure: {error}" |
| 1693 | ); |
| 1694 | assert!( |
| 1695 | !out.exists(), |
| 1696 | "a refused export still wrote {}", |
| 1697 | out.display() |
| 1698 | ); |
| 1699 | assert!(!part_path(&out).exists(), "the .part outlived the refusal"); |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | |
| 1704 | |
| 1705 | |
| 1706 | |
| 1707 | #[test] |
| 1708 | fn a_dead_encoder_answers_every_later_call_the_same_way() { |
| 1709 | let (width, height) = (320u32, 240u32); |
| 1710 | injected(Some("1"), None, || { |
| 1711 | let mut encoder = |
| 1712 | HwEncoder::open(width, height, 30, 1, 1_000_000).expect("the stand-in seat opened"); |
| 1713 | let (y, u, v) = synthetic(0, width as usize, height as usize); |
| 1714 | assert!( |
| 1715 | encoder.encode(&y, &u, &v, width, height, false).is_ok(), |
| 1716 | "the first picture is before the injection point" |
| 1717 | ); |
| 1718 | let died = encoder |
| 1719 | .encode(&y, &u, &v, width, height, false) |
| 1720 | .expect_err("the second picture kills the child"); |
| 1721 | assert!( |
| 1722 | engine::hwproc::is_lost(&died), |
| 1723 | "an encoder death must say so: {died}" |
| 1724 | ); |
| 1725 | |
| 1726 | |
| 1727 | let again = Instant::now(); |
| 1728 | let repeat = encoder.drain().expect_err("the seat stays dead"); |
| 1729 | assert!(engine::hwproc::is_lost(&repeat), "{repeat}"); |
| 1730 | assert!( |
| 1731 | again.elapsed() < Duration::from_millis(500), |
| 1732 | "a dead seat answered only after waiting" |
| 1733 | ); |
| 1734 | }); |
| 1735 | } |