test(media): make cancelled draft-switch regression deterministic (#4255)

* test(media): make cancelled draft-switch regression deterministic

* test(media): assert a submitted picture before cancelling the mode switch

---------

Co-authored-by: Fredrik Hallin <fredrik.hallin@guidelinegeo.com>
This commit is contained in:
FredApps
2026-11-29 06:06:57 +01:00
committed by GitHub
parent cb871c8a17
commit 45660841d5
+28 -14
View File
@@ -991,12 +991,12 @@ fn simple_mp4_apv_profile(file: &Mp4File, track: &isobmff::Track) -> Option<craf
}
fn open_isobmff(name: &str, source: SharedBytes) -> Option<Result<SharedSource>> {
open_isobmff_with_workers(name, source, worker_cap())
open_isobmff_with_workers(name, source, worker_cap()).map(|result| result.map(|source| source as SharedSource))
}
/// [`open_isobmff`] with an explicit decoder worker cap. Tests use it to force the
/// frame-threaded path; production always passes [`worker_cap`].
fn open_isobmff_with_workers(name: &str, source: SharedBytes, workers: usize) -> Option<Result<SharedSource>> {
/// [`open_isobmff`] with an explicit decoder worker cap. Tests select worker counts
/// for threaded playback or deterministic reordering; production passes [`worker_cap`].
fn open_isobmff_with_workers(name: &str, source: SharedBytes, workers: usize) -> Option<Result<Arc<CraftMediaSource>>> {
let file = isobmff::open(source.clone()).ok()?;
let video_idx = file.tracks.iter().position(|t| t.kind == isobmff::TrackKind::Video && !t.samples.is_empty() && !t.entries.is_empty() && t.timescale > 0);
let audio_candidates: Vec<(usize, usize, CodecParams, u64)> = file
@@ -5120,29 +5120,43 @@ mod tests {
}
#[test]
fn cancelled_mode_switch_does_not_hide_in_flight_draft_pictures() {
fn cancelled_mode_switch_keeps_the_submitted_picture_boundary() {
use std::sync::atomic::{AtomicBool, Ordering};
let movie = generated_h264_movie(96, 96, 2);
let in_flight = *movie.b_frames.iter().find(|&&i| (31..40).contains(&i)).expect("an in-flight B picture");
// To emit frame 30, the decoder has submitted the next reference picture (33),
// but not necessarily the B pictures (31 and 32) that precede it in presentation order.
let in_flight = 33;
let source = Arc::new(CountingKeyReads { bytes: movie.bytes, keyframes: movie.keyframes, reads: vec![std::sync::atomic::AtomicUsize::new(0)] });
let media = open_isobmff_with_workers("cancelled-switch.mp4", source.clone(), 8).expect("shared route").expect("opens");
// One worker leaves the reference picture in the presentation reorder buffer without making
// the submitted-picture boundary depend on how quickly background workers finish.
let media = open_isobmff_with_workers("cancelled-switch.mp4", source.clone(), 1).expect("shared route").expect("opens");
source.reads[0].store(0, Ordering::Relaxed);
// Leave draft pictures in flight.
// Leave a picture submitted in draft mode pending in the reorder buffer.
let draft = media.video_frame(FrameRequest { time: Tick::from_units(30, 25), scale: 0.25 }).expect("draft frame 30");
assert_eq!(draft.pts, Tick::from_units(30, 25));
assert_eq!(source.reads[0].load(Ordering::Relaxed), 1, "the draft switch re-read the keyframe");
// A full-quality request changes the decoder mode, then is cancelled before feeding any
// packet. The draft pictures submitted before the switch are still in flight.
// packet. Pictures submitted before the switch are still in flight.
let cancel = Arc::new(AtomicBool::new(true));
{
let state = media.video.as_ref().unwrap().lock().unwrap();
if let VideoState::Packet(state) = &*state {
let (index, pts) = media.file.target_sample(0, Tick::from_units(in_flight, 25)).expect("pending picture");
assert!(index < state.next_decode_idx, "the fixture must have submitted the target");
assert!(state.emitted_max_pts.is_some_and(|max| max < pts), "the fixture must not have emitted the target");
} else {
panic!("expected a packet decoder");
}
}
let cancelled = craft_media_source::cancel::with_cancel(&cancel, || media.video_frame(FrameRequest::full(Tick::from_units(80, 25))));
assert!(matches!(cancelled, Err(craft_media_source::MediaError::Cancelled)), "{cancelled:?}");
assert_eq!(source.reads[0].load(Ordering::Relaxed), 1, "the cancelled request read nothing");
// The full request must restart for the in-flight draft picture instead of falling back to
// an earlier full-quality frame.
let full = media.video_frame(FrameRequest::full(Tick::from_units(in_flight as i64, 25))).expect("full B picture");
assert_eq!(full.pts, Tick::from_units(in_flight as i64, 25));
assert_eq!(source.reads[0].load(Ordering::Relaxed), 2, "the full request reused an in-flight draft picture");
// The source conservatively restarts for any picture submitted before the full-quality
// switch: the pending picture could be draft, and cancellation must not erase that boundary.
let full = media.video_frame(FrameRequest::full(Tick::from_units(in_flight, 25))).expect("full pending picture");
assert_eq!(full.pts, Tick::from_units(in_flight, 25));
assert_eq!(source.reads[0].load(Ordering::Relaxed), 2, "the full request forgot the submitted-picture boundary");
}
#[test]