forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

decode_failure.rs161 lines, 6.6 KiBHistoryRawDownload
1//! A decode failure reaches the user, the way a lost sound device already does.
2//!
3//! A worker that ran to the end of a file and a worker that gave up on a
4//! corrupt packet both drop the sender, and `try_frame` returns `None` for
5//! either. The app read that as end-of-stream, so a file with a damaged middle
6//! froze on its last good picture and the transport said ENDED: no notice, no
7//! banner, and nothing on screen to say the film had stopped being the film.
8//! The sound path has had a reason channel for a device that dies mid-session
9//! all along; this is that channel's picture-side twin.
10//!
11//! The fixture is **built here**: `test_av.mp4`'s `moov` sits *after* its `mdat`,
12//! so truncating the file would take the index with it. What is done instead is
13//! what a download that dies mid-transfer leaves behind -- the tail of the
14//! sample data overwritten, the container index intact and still claiming every
15//! frame. The header and the first seconds stay valid, so frames really do
16//! arrive before the failure, which is the whole shape of the bug.
17//!
18//! ```text
19//! cargo test -p engine --test decode_failure -- --test-threads=1
20//! ```
21
22use std::path::PathBuf;
23use std::time::{Duration, Instant};
24
25use engine::PlaybackSession;
26use engine::scratch::Scratch;
27
28fn asset(name: &str) -> PathBuf {
29 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
30 .join("../../assets")
31 .join(name)
32}
33
34/// Software everywhere, so the suite proves the path every machine has.
35fn pin_software() {
36 static PIN: std::sync::Once = std::sync::Once::new();
37 PIN.call_once(|| {
38 engine::envcfg::set("VE_SW", "1");
39 engine::envcfg::set("VE_SW_ENC", "1");
40 });
41}
42
43/// `test_av.mp4` with the last eighth of its **sample data** overwritten, and
44/// the file otherwise untouched: `moov` still claims every frame the header
45/// ever did, and the demuxer hands the decoder exactly what it always handed
46/// it.
47///
48/// The damage is bounded to the `mdat` payload on purpose. `moov` sits *after*
49/// `mdat` in this fixture, so a damage that ran to the end of the file would
50/// take the index with it and leave a file that will not open at all -- a
51/// different defect, reached before a single frame is decoded and invisible to
52/// the thing this test is about.
53///
54/// Returns the scratch guard too, so the directory goes when the test ends.
55fn damaged() -> (Scratch, PathBuf) {
56 let bytes = std::fs::read(asset("test_av.mp4")).expect("the fixture");
57 let (start, end) = mdat_payload(&bytes).expect("an mdat payload");
58 let mut damaged_bytes = bytes;
59 let tail = start + (end - start) * 7 / 8;
60 damaged_bytes[tail..end].fill(0);
61 let scratch = Scratch::file("ve_decode_failure", "mp4");
62 let path = PathBuf::from(&*scratch);
63 std::fs::write(&path, &damaged_bytes).expect("write the damaged fixture");
64 (scratch, path)
65}
66
67/// The `mdat` box's payload, start and end: the walk a container parser does,
68/// four bytes at a time, and the reason the fixture is damaged rather than
69/// truncated.
70fn mdat_payload(bytes: &[u8]) -> Option<(usize, usize)> {
71 let mut at = 0usize;
72 while at + 8 <= bytes.len() {
73 let size = u32::from_be_bytes(bytes[at..at + 4].try_into().ok()?) as usize;
74 let kind = &bytes[at + 4..at + 8];
75 if size < 8 {
76 return None;
77 }
78 if kind == b"mdat" {
79 return Some((at + 8, (at + size).min(bytes.len())));
80 }
81 at += size;
82 }
83 None
84}
85
86/// Plays `path` to its end and answers what the transport would show and what
87/// the session says went wrong.
88fn play_to_the_end(path: &std::path::Path) -> (u32, Option<String>) {
89 let mut session = PlaybackSession::open(path).expect("open the file");
90 // A seek rather than a play: this wants the whole span decoded whatever the
91 // clock says, and a paused session still decodes -- which is what a scrub
92 // does, and is the same worker either way.
93 session.seek(0.0);
94 let started = Instant::now();
95 let mut frames = 0u32;
96 // The loop runs on the session's own end-of-stream flag rather than on the
97 // frame channel, because **that is the bug**: a closed channel and a clean
98 // end of file are one event to a caller that only has the channel, and the
99 // app reads that event as the film finishing. Draining on the flag is what
100 // a fixed caller does, and the reason has to be found somewhere else.
101 while !session.is_eos() {
102 if let Some(frame) = session.try_frame() {
103 frames = frame.index + 1;
104 }
105 assert!(
106 started.elapsed() < Duration::from_secs(60),
107 "the session never reported an end of stream"
108 );
109 std::thread::sleep(Duration::from_millis(1));
110 }
111 // The worker sets the reason as it gives up, which can be a moment after
112 // the flag it also sets; a fixed caller waits for the answer rather than
113 // racing it.
114 let deadline = Instant::now() + Duration::from_secs(10);
115 while session.video_failure_reason().is_none() && Instant::now() < deadline {
116 std::thread::sleep(Duration::from_millis(5));
117 }
118 (frames, session.video_failure_reason())
119}
120
121/// The defect: a file whose middle is damaged produces real frames, then stops,
122/// and says why. Before this the second half was silent -- the reason was an
123/// `eprintln!` on a decode worker's stderr, and the only thing the caller could
124/// see was a closed channel, which it read as the film finishing.
125#[test]
126fn a_damaged_file_says_why_it_stopped() {
127 pin_software();
128 let (_scratch, path) = damaged();
129 let (frames, reason) = play_to_the_end(&path);
130
131 assert!(
132 frames > 0,
133 "the fixture must really decode before it fails, or this proves nothing \
134 about a failure that happens mid-stream"
135 );
136 let reason = reason.expect(
137 "a file that stopped short of its end has to say why: before this the \
138 reason was an eprintln on a decode worker and the caller could only \
139 see a closed channel",
140 );
141 assert!(
142 !reason.is_empty(),
143 "an empty reason is not a reason a notice can print"
144 );
145}
146
147/// The other half, and the one that keeps the notice from becoming noise: a
148/// file that plays all the way through says **nothing**. A reason channel that
149/// fires on every film is a channel nobody reads, which is how the audio
150/// device-loss notice earns its place on the strip.
151#[test]
152fn a_file_that_plays_to_its_end_says_nothing() {
153 pin_software();
154 let path = asset("test_av.mp4");
155 let (frames, reason) = play_to_the_end(&path);
156 assert!(frames > 0, "the control must actually play");
157 assert_eq!(
158 reason, None,
159 "a film that reached its own end is not a failure"
160 );
161}