End-to-end mp4 interleave proof on the live export path
Browse files at this commit2f0f29881c28b17acfe0767829a68af1022cd9c7Parent: 4ba4015- test
- end-to-end mp4 interleave proof on the live export path
1 changed file +81 -0
crates/engine/tests/export.rsmodified+81 -0
| @@ -911,6 +911,87 @@ fn assert_no_single_stream_span_over(path: &Path, seconds: f64) { | |||
| 911 | } | 911 | } |
| 912 | } | 912 | } |
| 913 | 913 | ||
| 914 | +/// The first `pts_time` ffprobe reports for stream kind `select` ("v" or | ||
| 915 | +/// "a") in `path` -- just enough of [`assert_no_single_stream_span_over`]'s | ||
| 916 | +/// ffprobe call, restricted to one stream, to check A/V sync independently of | ||
| 917 | +/// the interleave order. | ||
| 918 | +fn first_pts(path: &Path, select: &str) -> f64 { | ||
| 919 | + let output = std::process::Command::new("ffprobe") | ||
| 920 | + .args([ | ||
| 921 | + "-v", | ||
| 922 | + "error", | ||
| 923 | + "-select_streams", | ||
| 924 | + select, | ||
| 925 | + "-show_entries", | ||
| 926 | + "packet=pts_time", | ||
| 927 | + "-of", | ||
| 928 | + "csv=p=0", | ||
| 929 | + ]) | ||
| 930 | + .arg(path) | ||
| 931 | + .output() | ||
| 932 | + .expect("run ffprobe"); | ||
| 933 | + assert!( | ||
| 934 | + output.status.success(), | ||
| 935 | + "ffprobe failed: {}", | ||
| 936 | + String::from_utf8_lossy(&output.stderr) | ||
| 937 | + ); | ||
| 938 | + String::from_utf8_lossy(&output.stdout) | ||
| 939 | + .lines() | ||
| 940 | + .find(|line| !line.is_empty()) | ||
| 941 | + .expect("stream has at least one packet") | ||
| 942 | + .parse() | ||
| 943 | + .expect("pts_time") | ||
| 944 | +} | ||
| 945 | + | ||
| 946 | +/// An mp4 export used to write every video sample and only then the whole | ||
| 947 | +/// audio track: byte-correct, and silent for as long as the picture took on | ||
| 948 | +/// any player that reads the file as it streams in rather than seeking to the | ||
| 949 | +/// end first. This proves the interleave holds on the real, threaded export | ||
| 950 | +/// path end to end -- `mp4_interleaves_a_mix_that_lands_early_under_the_rest_of_the_picture` | ||
| 951 | +/// above pins the same boundary by writing straight to `Mp4Muxer`, forcing the | ||
| 952 | +/// mix to land early rather than racing the mixer thread for it. | ||
| 953 | +#[test] | ||
| 954 | +fn mp4_audio_interleaves_with_the_picture() { | ||
| 955 | + let _seat = pin_software(); | ||
| 956 | + let source = asset("test_av.mp4"); | ||
| 957 | + let mut session = PlaybackSession::open(&source).expect("open source"); | ||
| 958 | + // A cut, so the audio copy runs as two segments and the join is real -- | ||
| 959 | + // the same edit `exports_audio_alongside_the_video` makes, reused here so | ||
| 960 | + // the interleave is proven on a timeline that was actually cut, not a | ||
| 961 | + // straight passthrough. | ||
| 962 | + assert!(session.cut_at(2.0)); | ||
| 963 | + session.pause(); | ||
| 964 | + let timeline = session.timeline_duration(); | ||
| 965 | + let out = out_path("interleave-e2e"); | ||
| 966 | + | ||
| 967 | + let handle = session.export_to(&out); | ||
| 968 | + wait(&handle, Duration::from_secs(600)).expect("export"); | ||
| 969 | + | ||
| 970 | + // A/V sync: the first audio sample starts at movie time zero either way -- | ||
| 971 | + // interleaving moves *where in the file* a packet sits, never what `stts` | ||
| 972 | + // says it plays at. | ||
| 973 | + assert_eq!(first_pts(&out, "a"), 0.0, "first audio pts is media time zero"); | ||
| 974 | + | ||
| 975 | + assert_no_single_stream_span_over(&out, 2.0); | ||
| 976 | + | ||
| 977 | + // Nothing else about the file moved: same duration, same sample counts, | ||
| 978 | + // to within the packet the two sibling tests above already allow. | ||
| 979 | + let (video_meta, _) = engine::demux::Demuxer::open(&out).unwrap(); | ||
| 980 | + assert_eq!(video_meta.frame_count, 150, "video track unaffected by interleaving"); | ||
| 981 | + let (audio_meta, chunks) = engine::AudioSession::open(&out) | ||
| 982 | + .unwrap() | ||
| 983 | + .expect("export has an audio track"); | ||
| 984 | + assert_eq!((audio_meta.sample_rate, audio_meta.channels), (44_100, 2)); | ||
| 985 | + let decoded: usize = chunks.into_iter().map(|c| c.samples.len()).sum(); | ||
| 986 | + let wanted = timeline * f64::from(audio_meta.sample_rate); | ||
| 987 | + assert!( | ||
| 988 | + (decoded as f64 / f64::from(audio_meta.channels) - wanted).abs() < 3.0 * 1024.0, | ||
| 989 | + "audio length {decoded} drifted from {wanted} samples of timeline" | ||
| 990 | + ); | ||
| 991 | + | ||
| 992 | + std::fs::remove_file(&out).unwrap(); | ||
| 993 | +} | ||
| 994 | + | ||
| 914 | /// A timeline whose *first* second of sound was lifted. A reader drops the | 995 | /// A timeline whose *first* second of sound was lifted. A reader drops the |
| 915 | /// first packet of an AAC track as encoder priming, so a track that opens on a | 996 | /// first packet of an AAC track as encoder priming, so a track that opens on a |
| 916 | /// hole has to carry one extra packet of silence for it to drop -- without it | 997 | /// hole has to carry one extra packet of silence for it to drop -- without it |