From b1ca335bed76aa322ff2b8c51d89f35766d63128 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 9 Oct 2025 18:50:42 +0800 Subject: [PATCH 1/5] error muxer on queue_frame fail --- crates/enc-ffmpeg/src/mux/mp4.rs | 9 +++-- crates/enc-ffmpeg/src/video/h264.rs | 40 ++++++++++----------- crates/enc-ffmpeg/src/video/mod.rs | 2 +- crates/recording/src/output_pipeline/win.rs | 15 ++++---- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/crates/enc-ffmpeg/src/mux/mp4.rs b/crates/enc-ffmpeg/src/mux/mp4.rs index e129afb51a..e570616992 100644 --- a/crates/enc-ffmpeg/src/mux/mp4.rs +++ b/crates/enc-ffmpeg/src/mux/mp4.rs @@ -5,6 +5,7 @@ use tracing::{info, trace}; use crate::{ audio::AudioEncoder, + h264, video::{H264Encoder, H264EncoderError}, }; @@ -70,12 +71,16 @@ impl MP4File { RawVideoFormat::YUYV420 } - pub fn queue_video_frame(&mut self, frame: frame::Video, timestamp: Duration) { + pub fn queue_video_frame( + &mut self, + frame: frame::Video, + timestamp: Duration, + ) -> Result<(), h264::QueueFrameError> { if self.is_finished { return; } - self.video.queue_frame(frame, timestamp, &mut self.output); + self.video.queue_frame(frame, timestamp, &mut self.output) } pub fn queue_audio_frame(&mut self, frame: frame::Audio) { diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs index a0eb78e6ac..3a9221e30d 100644 --- a/crates/enc-ffmpeg/src/video/h264.rs +++ b/crates/enc-ffmpeg/src/video/h264.rs @@ -143,6 +143,14 @@ pub struct H264Encoder { converter: Option, } +#[derive(thiserror::Error, Debug)] +pub enum QueueFrameError { + #[error("Converter: {0}")] + Converter(ffmpeg::Error), + #[error("Converter: {0}")] + Encode(ffmpeg::Error), +} + impl H264Encoder { const TIME_BASE: i32 = 90000; @@ -155,36 +163,26 @@ impl H264Encoder { mut frame: frame::Video, timestamp: Duration, output: &mut format::context::Output, - ) { + ) -> Result<(), QueueFrameError> { self.base .update_pts(&mut frame, timestamp, &mut self.encoder); let frame = if let Some(converter) = &mut self.converter { let mut new_frame = frame::Video::empty(); - match converter.run(&frame, &mut new_frame) { - Ok(_) => { - new_frame.set_pts(frame.pts()); - new_frame - } - Err(e) => { - tracing::error!( - "Failed to convert frame: {} from format {:?} to {:?}", - e, - frame.format(), - converter.output().format - ); - // Return early as we can't process this frame - return; - } - } + converter + .run(&frame, &mut new_frame) + .map_err(QueueFrameError::Converter)?; + new_frame.set_pts(frame.pts()); + new_frame } else { frame }; - if let Err(e) = self.base.send_frame(&frame, output, &mut self.encoder) { - tracing::error!("Failed to send frame to encoder: {:?}", e); - return; - } + self.base + .send_frame(&frame, output, &mut self.encoder) + .map_err(QueueFrameError::Encode)?; + + Ok(()) } pub fn finish(&mut self, output: &mut format::context::Output) { diff --git a/crates/enc-ffmpeg/src/video/mod.rs b/crates/enc-ffmpeg/src/video/mod.rs index f793b8cbb0..28f9a889e8 100644 --- a/crates/enc-ffmpeg/src/video/mod.rs +++ b/crates/enc-ffmpeg/src/video/mod.rs @@ -1,2 +1,2 @@ -mod h264; +pub mod h264; pub use h264::*; diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index dd602159de..ca0764d1de 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -172,14 +172,13 @@ impl Muxer for WindowsMuxer { use scap_ffmpeg::AsFFmpeg; - encoder.queue_frame( - frame - .as_ffmpeg() - .map_err(|e| format!("FrameAsFFmpeg: {e}")) - .unwrap(), - time, - &mut output, - ); + encoder + .queue_frame( + frame.as_ffmpeg().context("frame as_ffmpeg")?, + time, + &mut output, + ) + .context("queue_frame")?; } } } From 54532079757878e67a6596aeb48c30688f4db172 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 9 Oct 2025 19:36:49 +0800 Subject: [PATCH 2/5] fix macos build --- crates/enc-ffmpeg/src/mux/mp4.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/enc-ffmpeg/src/mux/mp4.rs b/crates/enc-ffmpeg/src/mux/mp4.rs index e570616992..9d152ccbb1 100644 --- a/crates/enc-ffmpeg/src/mux/mp4.rs +++ b/crates/enc-ffmpeg/src/mux/mp4.rs @@ -77,7 +77,7 @@ impl MP4File { timestamp: Duration, ) -> Result<(), h264::QueueFrameError> { if self.is_finished { - return; + return Ok(()); } self.video.queue_frame(frame, timestamp, &mut self.output) From 37063e7ba171305426294e256e6e47a02c4d5e3a Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 9 Oct 2025 21:23:48 +0800 Subject: [PATCH 3/5] import anyhow --- crates/recording/src/output_pipeline/win.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index ca0764d1de..e73301d835 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -1,4 +1,5 @@ use crate::{AudioFrame, AudioMuxer, Muxer, TaskPool, VideoMuxer, screen_capture}; +use anyhow::Context; use anyhow::anyhow; use cap_enc_ffmpeg::AACEncoder; use cap_media_info::{AudioInfo, VideoInfo}; From edf5e83a4dd2d139b19296906f957c8bf29b5989 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 9 Oct 2025 21:51:07 +0800 Subject: [PATCH 4/5] log and return instead of return error --- crates/recording/src/output_pipeline/win.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/recording/src/output_pipeline/win.rs b/crates/recording/src/output_pipeline/win.rs index e73301d835..1615c15a5f 100644 --- a/crates/recording/src/output_pipeline/win.rs +++ b/crates/recording/src/output_pipeline/win.rs @@ -173,13 +173,19 @@ impl Muxer for WindowsMuxer { use scap_ffmpeg::AsFFmpeg; - encoder - .queue_frame( - frame.as_ffmpeg().context("frame as_ffmpeg")?, - time, - &mut output, - ) - .context("queue_frame")?; + if let Err(e) = + frame + .as_ffmpeg() + .context("frame as_ffmpeg") + .and_then(|frame| { + encoder + .queue_frame(frame, time, &mut output) + .context("queue_frame") + }) + { + error!("{e}"); + return; + } } } } From 6189dff66c7194ab53f4c99274f9b8fc5e5a4427 Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Thu, 9 Oct 2025 23:52:37 +0800 Subject: [PATCH 5/5] Update crates/enc-ffmpeg/src/video/h264.rs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- crates/enc-ffmpeg/src/video/h264.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/enc-ffmpeg/src/video/h264.rs b/crates/enc-ffmpeg/src/video/h264.rs index 3a9221e30d..7d078493d7 100644 --- a/crates/enc-ffmpeg/src/video/h264.rs +++ b/crates/enc-ffmpeg/src/video/h264.rs @@ -147,7 +147,7 @@ pub struct H264Encoder { pub enum QueueFrameError { #[error("Converter: {0}")] Converter(ffmpeg::Error), - #[error("Converter: {0}")] + #[error("Encode: {0}")] Encode(ffmpeg::Error), }