diff --git a/crates/voelin-media/src/studio/mod.rs b/crates/voelin-media/src/studio/mod.rs index a6d62d8..5a00624 100644 --- a/crates/voelin-media/src/studio/mod.rs +++ b/crates/voelin-media/src/studio/mod.rs @@ -1180,19 +1180,32 @@ mod tests { scenes } + /// The first preview frame that `wanted` takes. The preview keeps the latest + /// frame only, so few older ones are waiting or on their way: the number of + /// frames looked at is the bound (the work), and the wait for each, which a + /// busy machine can stretch, only ends a hang. + fn preview_until( + preview: &Handoff, + wanted: impl Fn(&VideoFrame) -> bool, + ) -> Arc { + for _ in 0..20 { + let frame = preview.wait_timeout(Duration::from_secs(30)).expect("a preview frame"); + if wanted(&frame) { + return frame; + } + } + panic!("the preview never showed what was asked for"); + } + #[tokio::test] async fn it_composes_into_the_preview_and_follows_commands() { let studio = Studio::start(one_colour_scene()).await.unwrap(); let preview = studio.preview(); studio.apply(Command::SetPreview { width: 32, height: 24, fps: 60 }).await.unwrap(); - let started = Instant::now(); - let frame = loop { - if let Some(frame) = preview.wait_timeout(Duration::from_millis(200)) { - break frame; - } - assert!(started.elapsed() < Duration::from_secs(5), "no preview frame"); - }; - assert_eq!((frame.width, frame.height), (32, 24)); + // The studio composes from the moment it starts, at the size and rate the + // preview has then (480x270, 15 fps): a frame of that size can be the + // first one there is. + let frame = preview_until(&preview, |f| (f.width, f.height) == (32, 24)); let crate::frame::FrameData::Rgba(p) = &frame.data else { panic!("not RGBA") }; assert_eq!(&p.data[..4], &[9, 8, 7, 255]); @@ -1211,15 +1224,11 @@ mod tests { .unwrap(); studio.apply(Command::SetActiveScene { scene: break_id }).await.unwrap(); assert_eq!(studio.status().active_scene, break_id); - // The new scene's source draws. - let frame = loop { - let frame = preview.wait_timeout(Duration::from_secs(2)).expect("a preview frame"); - let crate::frame::FrameData::Rgba(p) = &frame.data else { panic!() }; - if p.data[..3] == [1, 2, 3] { - break frame; - } - assert!(started.elapsed() < Duration::from_secs(10), "the scene never switched"); - }; + // The new scene's source draws, from the first frame composed after the + // command. + let frame = preview_until(&preview, |f| { + matches!(&f.data, crate::frame::FrameData::Rgba(p) if p.data[..3] == [1, 2, 3]) + }); drop(frame); studio.apply(Command::RemoveScene { scene: break_id }).await.unwrap();