From 6ef2d394d9405c8fb06528feade24e42ea097ece Mon Sep 17 00:00:00 2001 From: mofeng-git Date: Thu, 26 Mar 2026 23:27:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(video):=20=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E4=B8=A2=E5=BC=83=E5=89=8D=E4=B8=89=E5=B8=A7=E6=97=A0=E6=95=88?= =?UTF-8?q?=20MJPEG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/video/shared_video_pipeline.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/video/shared_video_pipeline.rs b/src/video/shared_video_pipeline.rs index 647a48b8..43f926cf 100644 --- a/src/video/shared_video_pipeline.rs +++ b/src/video/shared_video_pipeline.rs @@ -31,6 +31,9 @@ const AUTO_STOP_GRACE_PERIOD_SECS: u64 = 3; const CAPTURE_TIMEOUT_RESTART_THRESHOLD: u32 = 5; /// Minimum valid frame size for capture const MIN_CAPTURE_FRAME_SIZE: usize = 128; +/// Validate every JPEG frame during startup to avoid poisoning HW decoders +/// with incomplete UVC warm-up frames. +const STARTUP_JPEG_VALIDATE_FRAMES: u64 = 3; /// Validate JPEG header every N frames to reduce overhead const JPEG_VALIDATE_INTERVAL: u64 = 30; /// Throttle repeated encoding errors to avoid log flooding @@ -208,6 +211,11 @@ fn log_encoding_error( } } +fn should_validate_jpeg_frame(validate_counter: u64) -> bool { + validate_counter <= STARTUP_JPEG_VALIDATE_FRAMES + || validate_counter.is_multiple_of(JPEG_VALIDATE_INTERVAL) +} + /// Pipeline statistics #[derive(Debug, Clone, Default)] pub struct SharedVideoPipelineStats { @@ -1463,7 +1471,7 @@ impl SharedVideoPipeline { validate_counter = validate_counter.wrapping_add(1); if pixel_format.is_compressed() - && validate_counter.is_multiple_of(JPEG_VALIDATE_INTERVAL) + && should_validate_jpeg_frame(validate_counter) && !VideoFrame::is_valid_jpeg_bytes(&owned[..frame_size]) { continue; @@ -1788,4 +1796,13 @@ mod tests { let h265 = SharedVideoPipelineConfig::h265(Resolution::HD720, BitratePreset::Speed); assert_eq!(h265.output_codec, VideoEncoderType::H265); } + + #[test] + fn test_startup_jpeg_validation_policy() { + assert!(should_validate_jpeg_frame(1)); + assert!(should_validate_jpeg_frame(2)); + assert!(should_validate_jpeg_frame(3)); + assert!(!should_validate_jpeg_frame(4)); + assert!(should_validate_jpeg_frame(30)); + } }