diff --git a/crates/voelin-core/src/media.rs b/crates/voelin-core/src/media.rs index 57fb22b..dc2a395 100644 --- a/crates/voelin-core/src/media.rs +++ b/crates/voelin-core/src/media.rs @@ -3728,15 +3728,35 @@ mod tests { assert!((60..=68).contains(&(b - a + 1)), "{spans:?}"); } assert_ne!(spans.first(), spans.last(), "the rectangle moves"); - let stats = preview.stats(); - assert!(stats.decoded >= 10 && stats.error.is_none(), "{stats:?}"); - assert_eq!(stats.codec, Some(Codec::Vp8)); + let (before_first, first) = (Instant::now(), preview.stats()); + let after_first = Instant::now(); + assert!(first.decoded >= 10 && first.error.is_none(), "{first:?}"); + assert_eq!(first.codec, Some(Codec::Vp8)); assert_eq!(preview.streamer().backend(), "synthetic"); - // Rates over the second since: the pattern's rate, its bitrate. + // The rates are the counters over the time since the call before. How + // long that was the test does not say (a busy machine stretches a second, + // and the rate of pictures it manages is not what is tested), but it lies + // between the two spans the test timed around it. tokio::time::sleep(Duration::from_secs(1)).await; - let stats = preview.stats(); - assert!((20..=40).contains(&stats.fps), "{stats:?}"); - assert!((100_000..5_000_000).contains(&stats.bitrate), "{stats:?}"); + let before_second = Instant::now(); + let second = preview.stats(); + let after_second = Instant::now(); + let longest = after_second.duration_since(before_first).as_secs_f64(); + let shortest = before_second.duration_since(after_first).as_secs_f64(); + let frames = (second.decoded - first.decoded) as f64; + let bits = (second.bytes - first.bytes) as f64 * 8.0; + assert!(frames > 0.0, "no picture in the second: {first:?} {second:?}"); + // (The pictures per second are rounded, the bits per second cut off.) + let fps = f64::from(second.fps); + assert!( + (frames / longest - 0.5..=frames / shortest + 0.5).contains(&fps), + "{frames} pictures in {shortest} to {longest} s are not {fps} a second" + ); + let bitrate = second.bitrate as f64; + assert!( + (bits / longest - 1.0..=bits / shortest).contains(&bitrate), + "{bits} bits in {shortest} to {longest} s are not {bitrate} a second" + ); } /// A VP8 decoder for the pipeline tests: fails (with errors, or giving