forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

backends.rs403 lines, 17.2 KiBHistoryRawDownload
1//! Which decoder and which encoder a job really runs on -- the introspection a
2//! front-end shows before an operation and while it runs.
3//!
4//! The point of every assertion here is *truthfulness*, not a particular seat:
5//! this box may or may not have a working plugin, and a test that demanded
6//! hardware would only be measuring the machine. What must hold is that the
7//! answer given before an operation is the answer the operation then gives, and
8//! that neither of them ever names a decoder that could not have run (software
9//! HEVC, for one, does not exist here).
10
11use std::path::PathBuf;
12use std::time::{Duration, Instant};
13
14use engine::Codec;
15use engine::decode::{Backend, probe};
16use engine::export::{EncoderSeat, ExportSettings, Format};
17use engine::scratch::Scratch;
18use engine::{ExportHandle, PlaybackSession};
19
20fn asset(name: &str) -> PathBuf {
21 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
22 .join("../../assets")
23 .join(name)
24}
25
26fn out_path(name: &str, ext: &str) -> Scratch {
27 Scratch::file(&format!("ve_backend_{name}"), ext)
28}
29
30/// A still has no coded stream and no decoder to pick, and it must say so
31/// rather than borrowing the picture path's answer.
32#[test]
33fn a_still_probes_as_a_still() {
34 let (codec, backend) = probe(&asset("test_still.png")).expect("probe the still");
35 assert_eq!(codec, None, "an image has no coded stream to name");
36 assert_eq!(backend, Backend::Still);
37}
38
39/// H.264 has both seats, so the probe may legitimately answer either -- but it
40/// must answer one of them, and it must name the codec.
41#[test]
42fn an_h264_source_probes_as_one_of_the_two_seats() {
43 let (codec, backend) = probe(&asset("test_baseline.mp4")).expect("probe the baseline fixture");
44 assert_eq!(codec, Some(Codec::H264));
45 assert!(
46 matches!(backend, Backend::Hardware | Backend::Software),
47 "a probe that returned must have picked a seat, got {backend:?}"
48 );
49}
50
51/// HEVC has a software seat here (`ec-h265`). A box with the plugin answers
52/// hardware; a box without it answers the software seat -- and is never refused,
53/// since the file plays either way.
54#[test]
55fn hevc_probes_as_hardware_or_as_the_software_seat() {
56 match probe(&asset("test_hevc.mkv")) {
57 Ok((codec, backend)) => {
58 assert_eq!(codec, Some(Codec::Hevc));
59 assert!(
60 matches!(backend, Backend::Hardware | Backend::Software),
61 "HEVC probes as a seat that decodes it, got {backend:?}"
62 );
63 }
64 Err(e) => panic!("HEVC must not be refused for want of the plugin: {e}"),
65 }
66}
67
68/// The running session reports the decoder its *worker* opened, and it agrees
69/// with what the probe said before anything played. This is the whole point of
70/// the cell: the worker writes it where the fallback happens, so a hardware
71/// session that dropped to software could only be seen here.
72#[test]
73fn a_playing_session_reports_the_seat_the_probe_named() {
74 let path = asset("test_baseline.mp4");
75 let (_codec, probed) = probe(&path).expect("probe the baseline fixture");
76 let session = PlaybackSession::open(&path).expect("open the fixture");
77 let started = Instant::now();
78 loop {
79 let live = session.decode_backend();
80 if live != Backend::Opening {
81 assert_eq!(live, probed, "the running seat is not the probed one");
82 break;
83 }
84 assert!(
85 started.elapsed() < Duration::from_secs(20),
86 "the worker never published a decode backend"
87 );
88 std::thread::sleep(Duration::from_millis(10));
89 }
90}
91
92/// What the export card says before the button and what the progress line says
93/// during the job are the same two answers, from the same two functions: the
94/// picture's seat is probed by opening the very encoder the export opens, and
95/// the sound's is decided outright. A drift between them is a front-end lying
96/// about the machine, which is exactly what this whole surface is for.
97///
98/// Every picture format, because each one is a different pair of seats: H.264
99/// through `ec-h264` or the plugin, and AV1 through `rav1e` in either
100/// container -- a container is not an encoder, and the card must not say it is.
101#[test]
102fn the_planned_encoders_are_the_ones_the_job_opens() {
103 for (format, ext) in [
104 (Format::Mp4, "mp4"),
105 (Format::Av1, "mkv"),
106 (Format::Av1Mp4, "mp4"),
107 // Both HEVC containers, whose seat is the plugin's where the picture
108 // clears the driver's floor and the software intra encoder where it does
109 // not -- which of the two is this box's business and never this test's.
110 (Format::Hevc, "mkv"),
111 (Format::HevcMp4, "mp4"),
112 ] {
113 let settings = ExportSettings {
114 format,
115 ..ExportSettings::default()
116 };
117 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
118 let planned = format!(
119 "{} · {}",
120 engine::export::planned_video(session.meta(), &settings)
121 .expect("these formats carry picture"),
122 session.planned_audio(format, false)
123 );
124 let out = out_path("planned", ext);
125 let handle = session.export_to_with(&out, &settings);
126 let opened = published_encoders(&handle, ext);
127 // Nothing here wants the file, only the seat it was going to be written
128 // with: cancelling deletes the `.part` the worker had started.
129 handle.cancel();
130 wind_down(&handle);
131 assert_eq!(opened, planned, "{ext}");
132 // ...and the sound is named on both, AV1 included: an AV1 export
133 // carries the timeline's audio now, so a line that said nothing about
134 // it would be the old picture-only lie in a new place.
135 assert!(planned.contains("AAC"), "{ext}: {planned}");
136 if matches!(format, Format::Hevc | Format::HevcMp4) {
137 assert!(
138 planned.contains("ec-h265") || planned.contains("HW encode"),
139 "{ext}: an HEVC job names one of the two HEVC seats: {planned}"
140 );
141 }
142 }
143}
144
145/// The same pin, swept over the two things that *move* the seat under a user's
146/// hand: the picture size (the plugin refuses HEVC below the driver's 384x384
147/// floor and the software intra encoder takes the file) and the seat picked on
148/// the card ([`engine::export::EncoderSeat`]). The export card reads
149/// [`engine::export::planned_video`] for every one of these combinations, so a
150/// size that flips the seat and a card that kept the old line is the exact lie
151/// this sweep exists to catch.
152///
153/// Not swept: the hardware AV1 seat, which is what `Hardware` means for the two
154/// AV1 rows. Opening it hung this box's GPU into a driver reset (2026-08-10) and
155/// a test suite is not the place to find out whether that is fixed -- `av1.rs`
156/// keeps that one `#[ignore]`d and by name -- so `Hardware` is swept over the
157/// formats whose seat is safe to open, and the AV1 pair only over the seats that
158/// open no plugin at all.
159#[test]
160fn the_planned_seat_follows_the_size_and_the_pin() {
161 for (format, ext) in [
162 (Format::Mp4, "mp4"),
163 (Format::Av1, "mkv"),
164 (Format::Av1Mp4, "mp4"),
165 (Format::Hevc, "mkv"),
166 (Format::HevcMp4, "mp4"),
167 ] {
168 for (width, height) in [(320, 180), (1280, 720), (1920, 1080)] {
169 for seat in EncoderSeat::ALL {
170 if seat == EncoderSeat::Hardware && matches!(format, Format::Av1 | Format::Av1Mp4) {
171 continue;
172 }
173 let settings = ExportSettings {
174 format,
175 seat,
176 ..ExportSettings::default()
177 };
178 let mut session =
179 PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
180 // The project resolution is what an export writes, so this is
181 // the very size the card's probe is asked about.
182 session.set_resolution(width, height);
183 let planned = engine::export::planned_video(session.meta(), &settings)
184 .expect("these formats carry picture");
185 // A pinned job is software by definition, whatever the machine
186 // has -- the one seat this file may assert outright.
187 if seat == EncoderSeat::Software {
188 assert!(
189 planned.starts_with("SW encode"),
190 "{ext} {width}x{height}: a pinned job is software, got {planned}"
191 );
192 }
193 let out = out_path("seat", ext);
194 let handle = session.export_to_with(&out, &settings);
195 // `Hardware` on a box with no seat for this size is the one
196 // combination that opens nothing: the job refuses in words and
197 // the card said the same before the button ([`NO_HW_SEAT`]),
198 // which is the claim -- a refusal that fires while a seat *is*
199 // there would be caught by the branch below, where the export
200 // then has to open one.
201 if planned == engine::export::NO_HW_SEAT {
202 // The verdict is read here rather than through `wind_down`,
203 // which takes it out of the handle on its way past.
204 let started = Instant::now();
205 let err = loop {
206 if let Some(result) = handle.result() {
207 break result
208 .expect_err("hardware was demanded and refused")
209 .to_string();
210 }
211 assert!(
212 started.elapsed() < Duration::from_secs(60),
213 "{ext}: a refused export never settled"
214 );
215 std::thread::sleep(Duration::from_millis(10));
216 };
217 assert!(
218 err.contains("no VA-API seat"),
219 "{ext} {width}x{height}: the refusal names the missing seat, got {err}"
220 );
221 assert!(
222 !out.exists(),
223 "{ext} {width}x{height}: a refused export wrote a file"
224 );
225 continue;
226 }
227 let opened = published_encoders(&handle, &format!("{ext} {width}x{height}"));
228 handle.cancel();
229 wind_down(&handle);
230 assert!(
231 opened.starts_with(planned),
232 "{ext} {width}x{height} seat={seat:?}: the card said {planned}, the job opened {opened}"
233 );
234 if seat == EncoderSeat::Hardware {
235 assert!(
236 opened.starts_with("HW encode"),
237 "{ext} {width}x{height}: hardware was demanded and not refused, so the \
238 job runs on it -- got {opened}"
239 );
240 }
241 }
242 }
243 }
244}
245
246/// A WAV job has no picture at all and still names what writes it, so an audio
247/// export is not a progress line with nothing to say.
248#[test]
249fn an_audio_only_job_names_its_encoder() {
250 for format in [Format::Wav, Format::Flac, Format::Mp3, Format::Ogg] {
251 audio_only_job_names_its_encoder(format);
252 }
253}
254
255fn audio_only_job_names_its_encoder(format: Format) {
256 let settings = ExportSettings {
257 format,
258 ..ExportSettings::default()
259 };
260 assert_eq!(
261 engine::export::planned_video(
262 &engine::demux::Demuxer::open(&asset("test_av.mp4"))
263 .expect("open the fixture")
264 .0,
265 &settings
266 ),
267 None,
268 "an audio-only format has no video seat to name"
269 );
270 let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
271 let planned = session.planned_audio(format, false);
272 let out = out_path("audio", format.ext());
273 let handle = session.export_to_with(&out, &settings);
274 let started = Instant::now();
275 let opened = loop {
276 if let Some(line) = handle.encoders() {
277 break line;
278 }
279 assert!(
280 started.elapsed() < Duration::from_secs(60),
281 "no encoder was published"
282 );
283 std::thread::sleep(Duration::from_millis(10));
284 };
285 handle.cancel();
286 wind_down(&handle);
287 let _ = std::fs::remove_file(&out);
288 assert_eq!(opened, planned, "{}", format.ext());
289}
290
291/// A mixed timeline cannot be copied, and the card must say so *before* the
292/// export: a fader and a limiter both live where the lanes are summed, and a
293/// copied AAC packet was never summed at all -- so an mp4 of such a timeline is
294/// decoded and re-encoded, and the line names the encoder that will do it.
295///
296/// The truthfulness rule of this whole file, applied to the one edit that used
297/// to be invisible to it.
298#[test]
299fn a_mixed_timeline_says_it_re_encodes_and_then_does() {
300 let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
301 assert_eq!(
302 session.planned_audio(Format::Mp4, false),
303 "AAC copy",
304 "a timeline nobody has mixed is still a copy"
305 );
306 // A fader off unity...
307 assert!(session.set_lane_gain_db(engine::project::Lane::A1, -3.0));
308 let planned = session.planned_audio(Format::Mp4, false);
309 assert!(planned.contains("encode"), "a faded lane: {planned}");
310 // ...and back at unity it is a copy again: the answer follows the edit, it
311 // is not a latch.
312 assert!(session.set_lane_gain_db(engine::project::Lane::A1, 0.0));
313 assert_eq!(session.planned_audio(Format::Mp4, false), "AAC copy");
314 // ...and the limiter alone does it too.
315 assert!(session.set_limiter(engine::limiter::Limiter {
316 ceiling_db: -1.0,
317 on: true,
318 }));
319 assert_eq!(session.planned_audio(Format::Mp4, false), planned);
320
321 // ...and the job opens exactly that, which is the pin the whole file is
322 // about: the encoders line published by the running export.
323 let out = out_path("mixed", "mp4");
324 let handle = session.export_to_with(
325 &out,
326 &ExportSettings {
327 format: Format::Mp4,
328 ..ExportSettings::default()
329 },
330 );
331 let opened = published_encoders(&handle, "a mixed timeline");
332 assert!(
333 opened.ends_with(&planned),
334 "{opened} does not end in {planned}"
335 );
336 // ...and it is the line the job *settles* on, which is why this one runs to
337 // the end instead of cancelling: an mp4 mixes its sound beside the picture,
338 // and a hardware picture is through first, so the measured codec reaches the
339 // line where the pass is collected under the muxer rather than in the frame
340 // loop. A card left saying "working out the sound…" over a written file is
341 // the same lie as one naming a codec that never ran.
342 let started = Instant::now();
343 while !handle.is_finished() {
344 assert!(
345 started.elapsed() < Duration::from_secs(300),
346 "the export did not finish in 300 s"
347 );
348 std::thread::sleep(Duration::from_millis(10));
349 }
350 handle.result().expect("an outcome").expect("the export");
351 let settled = handle.encoders().expect("a finished job still names its seats");
352 assert!(
353 settled.ends_with(&planned),
354 "the export settled on {settled}, which does not end in {planned}"
355 );
356}
357
358/// The half-line an export shows while its sound is still being mixed on the
359/// thread beside the picture (`engine::export::publish_seats`). An mp4 job
360/// overlaps the two passes, so this is what the *first* line out of one says
361/// about a codec that has not run yet.
362const MIXING: &str = "working out the sound…";
363
364/// The encoders line a running job settles on: published as soon as the picture
365/// seat is open, and completed once the sound has been measured. Waiting for the
366/// second half is what makes this the line a card shows rather than a snapshot
367/// of an export mid-decision -- and, because a job that ends without ever
368/// completing it fails here, it is also the pin on that completion happening at
369/// all, whichever of the two passes finishes first.
370fn published_encoders(handle: &ExportHandle, what: &str) -> String {
371 let started = Instant::now();
372 loop {
373 match handle.encoders() {
374 Some(line) if !line.ends_with(MIXING) => return line,
375 _ => {}
376 }
377 assert!(
378 !handle.is_finished(),
379 "{what}: the export settled without naming its encoders ({:?}): {:?}",
380 handle.encoders(),
381 handle.result().map(|r| r.err())
382 );
383 assert!(
384 started.elapsed() < Duration::from_secs(60),
385 "{what}: no encoder was published"
386 );
387 std::thread::sleep(Duration::from_millis(10));
388 }
389}
390
391/// Waits for a cancelled job to settle, so its `.part` is gone before the test
392/// ends -- and so no worker outlives the process holding a VA-API session.
393fn wind_down(handle: &ExportHandle) {
394 let started = Instant::now();
395 while !handle.is_finished() {
396 assert!(
397 started.elapsed() < Duration::from_secs(60),
398 "a cancelled export did not settle"
399 );
400 std::thread::sleep(Duration::from_millis(10));
401 }
402 let _ = handle.result();
403}