Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(encoder): drop frames with mismatched dimensions to protect AVAssetWriter

Richie McIlroy12a71ab3311d
diff --git a/crates/enc-avfoundation/src/mp4.rs b/crates/enc-avfoundation/src/mp4.rs
index 13bafb729..6bae5e8c8 100644
--- a/crates/enc-avfoundation/src/mp4.rs
+++ b/crates/enc-avfoundation/src/mp4.rs
@@ -20,6 +20,9 @@ pub struct MP4Encoder {
audio_input: Option<arc::R<av::AssetWriterInput>>,
audio_resampler: Option<resampling::Context>,
audio_output_rate: u32,
+ expected_width: usize,
+ expected_height: usize,
+ dimension_mismatch_count: u64,
last_frame_timestamp: Option<Duration>,
pause_timestamp: Option<Duration>,
timestamp_offset: Duration,
@@ -341,6 +344,9 @@ impl MP4Encoder {
}
Ok(Self {
+ expected_width: video_config.width as usize,
+ expected_height: video_config.height as usize,
+ dimension_mismatch_count: 0,
config: video_config,
audio_input,
audio_resampler,
@@ -379,6 +385,27 @@ impl MP4Encoder {
return Err(QueueFrameError::NotReadyForMore);
}
+ if let Some(image_buf) = frame.image_buf() {
+ let frame_width = image_buf.width();
+ let frame_height = image_buf.height();
+ if frame_width != self.expected_width || frame_height != self.expected_height {
+ self.dimension_mismatch_count += 1;
+ if self.dimension_mismatch_count <= 5
+ || self.dimension_mismatch_count.is_multiple_of(100)
+ {
+ warn!(
+ expected_width = self.expected_width,
+ expected_height = self.expected_height,
+ frame_width,
+ frame_height,
+ mismatch_count = self.dimension_mismatch_count,
+ "Frame dimension mismatch, dropping frame to protect writer"
+ );
+ }
+ return Ok(());
+ }
+ }
+
if !self.is_writing {
self.is_writing = true;
self.asset_writer