Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Stop muxer on queue_frame fail (#1163)

Brendan Allan35d0ad26f056
diff --git a/crates/enc-ffmpeg/src/mux/mp4.rs b/crates/enc-ffmpeg/src/mux/mp4.rs
index e129afb51..9d152ccbb 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;
+ return Ok(());
}
- 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 a0eb78e6a..7d078493d 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<ffmpeg::software::scaling::Context>,
}
+#[derive(thiserror::Error, Debug)]
+pub enum QueueFrameError {
+ #[error("Converter: {0}")]
+ Converter(ffmpeg::Error),
+ #[error("Encode: {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 f793b8cbb..28f9a889e 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 dd602159d..1615c15a5 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};
@@ -172,14 +173,19 @@ impl Muxer for WindowsMuxer {
use scap_ffmpeg::AsFFmpeg;
- encoder.queue_frame(
+ if let Err(e) =
frame
.as_ffmpeg()
- .map_err(|e| format!("FrameAsFFmpeg: {e}"))
- .unwrap(),
- time,
- &mut output,
- );
+ .context("frame as_ffmpeg")
+ .and_then(|frame| {
+ encoder
+ .queue_frame(frame, time, &mut output)
+ .context("queue_frame")
+ })
+ {
+ error!("{e}");
+ return;
+ }
}
}
}