From c41624e82d27b2bbfaf46b86f112e766bc461471 Mon Sep 17 00:00:00 2001 From: selikhov Date: Sat, 3 Oct 2026 02:26:58 +0300 Subject: [PATCH] voelin-media: stop copying an image backdrop every frame Replacing a camera's background with an image cloned the whole scaled image (3.7 MB at 720p) for every frame, on the camera's thread, only to mix it and drop it: the mix only needs to read it. The image path had no test; one now checks the image shows behind the subject and the subject stays as it was. Co-Authored-By: Claude Opus 5.5 --- crates/voelin-media/src/studio/segment.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/voelin-media/src/studio/segment.rs b/crates/voelin-media/src/studio/segment.rs index 59c836e..8b68c55 100644 --- a/crates/voelin-media/src/studio/segment.rs +++ b/crates/voelin-media/src/studio/segment.rs @@ -404,8 +404,7 @@ impl BackgroundFilter { let FrameData::Rgba(behind) = &image.data else { return Err(Error::InvalidFrame("the background image is not RGBA".into())); }; - let behind = behind.data.clone(); - mix(&mut self.workers, &mut plane.data, &behind, stride, width, height, &mask); + mix(&mut self.workers, &mut plane.data, &behind.data, stride, width, height, &mask); } Some(Backdrop::Colour(rgba)) => { let rgba = *rgba; @@ -693,6 +692,26 @@ mod tests { assert!(filter.apply(&mut VideoFrame::black_i420(8, 8)).is_err()); } + #[test] + fn an_image_background_shows_behind_the_subject() { + let dir = std::env::temp_dir().join(format!("voelin-backdrop-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("behind.png"); + image::RgbaImage::from_pixel(16, 16, image::Rgba([0, 0, 220, 255])).save(&path).unwrap(); + let mut filter = + BackgroundFilter::with_default_segmenter(Background::Image { path: path.clone() }); + let started = Instant::now(); + let mut frame = flat_rgba(64, 64, [0, 255, 0, 255]); + while pixel(&frame, 0, 0) != [0, 0, 220, 255] { + assert!(started.elapsed() < Duration::from_secs(5), "the image never showed"); + frame = flat_rgba(64, 64, [0, 255, 0, 255]); + filter.apply(&mut frame).unwrap(); + std::thread::sleep(Duration::from_millis(10)); + } + assert_eq!(pixel(&frame, 32, 35), [0, 255, 0, 255], "the subject is untouched"); + std::fs::remove_dir_all(&dir).ok(); + } + #[test] fn a_blurred_background_keeps_the_subject_sharp() { let mut filter =