From f9bfd5d305bc351f3b9a921dc23a7c497edd804d Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 25 Oct 2026 03:26:13 +0300 Subject: [PATCH] Check the test pattern's structure, not its pixels within a tolerance rectangle_span, the pixel check of local_preview_decodes_the_pattern and test_pattern_through_stream_tasks, asked every pixel further than two columns from the rectangle's edges to be within 24 per channel of the rectangle's colour or of the background. The streamer's VP8 speed control lowers the quality when the encoder falls behind, and a flat colour then moves by 30 and more per channel (seen: [33, 31, 101] and [50, 33, 48] for the background [24, 40, 72], [255, 85, 5] for the rectangle [240, 90, 30]), still nowhere near the other colour. A tolerance of 40 was tried and reverted: it only moves the load at which it fails. A pixel is now whichever of the pattern's two colours it is nearer to, and what the test is about is the row's structure: background, one run of the rectangle, background. The first and last pixel that are the rectangle's give the span; everything between them, but the two blurred columns at each end, has to be the rectangle's too, and the callers still check the span's width and that it moves. A stray rectangle-coloured pixel in the background stretches the span over background and fails there. Before, the two tests failed 4 and 2 of 100 runs each beside 32 busy loops; see docs/testing/load.md for the runs after. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-core/src/media.rs | 36 ++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 12 deletions(-) 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) }