diff --git a/crates/voelin-core/src/media.rs b/crates/voelin-core/src/media.rs index 38db2da..57fb22b 100644 --- a/crates/voelin-core/src/media.rs +++ b/crates/voelin-core/src/media.rs @@ -3257,9 +3257,13 @@ impl LocalPreview { } /// The first and last column of the rectangle in the middle row of a -/// decoded test pattern. Checks that pixels further than two columns from -/// its edges (which the codec blurs) have the colour of the rectangle or -/// the background. +/// decoded test pattern, after checking the row's structure: background, one +/// run of the rectangle, background. A pixel is whichever of the pattern's two +/// colours it is nearer to; it is not asked to be within some distance of one, +/// because an encoder that falls behind lowers its quality (the VP8 speed +/// control of the streamer does) and then moves a flat colour by 30 and more +/// per channel, still nowhere near the other colour. The two columns at each +/// edge, which the codec blurs, may be either. #[cfg(all(test, feature = "media-desktop"))] pub(crate) fn rectangle_span(picture: &VideoFrame) -> (u32, u32) { use voelin_media::capture::synthetic::{BACKGROUND, RECT_COLOR}; @@ -3270,15 +3274,23 @@ pub(crate) fn rectangle_span(picture: &VideoFrame) -> (u32, u32) { let i = ((y * picture.width + x) * 4) as usize; [rgba[i], rgba[i + 1], rgba[i + 2]] }; - let close = |p: [u8; 3], c: [u8; 3]| p.iter().zip(c).all(|(a, b)| a.abs_diff(b) <= 24); - let inside: Vec = (0..picture.width).filter(|&x| close(pixel(x), RECT_COLOR)).collect(); - let (&a, &b) = inside.first().zip(inside.last()).expect("no rectangle in the middle row"); - for x in 0..picture.width { - if x + 2 < a || x > b + 2 { - assert!(close(pixel(x), BACKGROUND), "pixel {x},{y} is {:?}", pixel(x)); - } else if (a + 2..=b.saturating_sub(2)).contains(&x) { - assert!(close(pixel(x), RECT_COLOR), "pixel {x},{y} is {:?}", pixel(x)); - } + let distance = |p: [u8; 3], c: [u8; 3]| -> u32 { + p.iter().zip(c).map(|(a, b)| u32::from(a.abs_diff(b)).pow(2)).sum() + }; + let rectangle = |x: u32| { + let p = pixel(x); + distance(p, RECT_COLOR) < distance(p, BACKGROUND) + }; + let a = (0..picture.width).find(|&x| rectangle(x)).expect("no rectangle in the middle row"); + let b = (0..picture.width).rfind(|&x| rectangle(x)).unwrap(); + // One run: everything between its ends, but the blurred edges, is the + // rectangle's. Whatever is outside is the background by the choice of a and b. + for x in a + 2..=b.saturating_sub(2) { + assert!( + rectangle(x), + "pixel {x},{y} is {:?}, in the middle of the rectangle (columns {a} to {b})", + pixel(x) + ); } (a, b) }