Skip to content

Commit a2351f3

Browse files
committed
handle bad frames
1 parent d5dbff4 commit a2351f3

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

examples/local_video/src/publisher.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ async fn run(args: Args, ctrl_c_received: Arc<AtomicBool>) -> Result<()> {
259259
let mut sum_sleep_ms = 0.0;
260260
let mut sum_iter_ms = 0.0;
261261
let mut logged_mjpeg_fallback = false;
262+
let mut consecutive_capture_errors: u32 = 0;
263+
const MAX_CONSECUTIVE_CAPTURE_ERRORS: u32 = 30;
262264
loop {
263265
if ctrl_c_received.load(Ordering::Acquire) {
264266
break;
@@ -270,7 +272,29 @@ async fn run(args: Args, ctrl_c_received: Arc<AtomicBool>) -> Result<()> {
270272

271273
// Get frame as RGB24 (decoded by nokhwa if needed)
272274
let t0 = Instant::now();
273-
let frame_buf = camera.frame()?;
275+
let frame_buf = match camera.frame() {
276+
Ok(buf) => {
277+
consecutive_capture_errors = 0;
278+
buf
279+
}
280+
Err(e) => {
281+
consecutive_capture_errors += 1;
282+
if consecutive_capture_errors >= MAX_CONSECUTIVE_CAPTURE_ERRORS {
283+
return Err(anyhow::anyhow!(
284+
"Camera capture failed {} consecutive times, last error: {}",
285+
consecutive_capture_errors,
286+
e
287+
));
288+
}
289+
log::warn!(
290+
"Frame capture error (attempt {}/{}): {} -- skipping frame",
291+
consecutive_capture_errors,
292+
MAX_CONSECUTIVE_CAPTURE_ERRORS,
293+
e
294+
);
295+
continue;
296+
}
297+
};
274298
let t1 = Instant::now();
275299
let (stride_y, stride_u, stride_v) = frame.buffer.strides();
276300
let (data_y, data_u, data_v) = frame.buffer.data_mut();

0 commit comments

Comments
 (0)