Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Stride issue on specific resolutions

Richie McIlroyaadd896e7c53
diff --git a/apps/desktop/src-tauri/src/display.rs b/apps/desktop/src-tauri/src/display.rs
index e928f21c2..78425a652 100644
--- a/apps/desktop/src-tauri/src/display.rs
+++ b/apps/desktop/src-tauri/src/display.rs
@@ -182,6 +182,9 @@ pub async fn start_capturing(
let mut start_time_tx = Some(tx);
+ // Hold the last valid RGB frame
+ let mut last_rgb_frame: Option<ffmpeg::frame::Video> = None;
+
loop {
if controller.is_stopped() {
break;
@@ -203,12 +206,29 @@ pub async fn start_capturing(
match frame {
Ok(Frame::BGRA(frame)) => {
- let rgb_frame = bgra_frame(&frame.data, capture_size[0], capture_size[1]);
-
- let mut yuv_frame = Video::empty();
- scaler.run(&rgb_frame, &mut yuv_frame).unwrap();
-
- encoder.encode_frame(yuv_frame, frame.display_time / 1_000_000);
+ if let Some(rgb_frame) =
+ bgra_frame(&frame.data, capture_size[0], capture_size[1])
+ {
+ // Successfully created RGB frame
+ last_rgb_frame = Some(rgb_frame.clone());
+
+ let mut yuv_frame = Video::empty();
+ scaler.run(&rgb_frame, &mut yuv_frame).unwrap();
+
+ encoder.encode_frame(yuv_frame, frame.display_time / 1_000_000);
+ } else if let Some(ref rgb_frame) = last_rgb_frame {
+ // Use the last valid frame
+ println!("Using last valid frame due to unexpected frame size.");
+
+ let mut yuv_frame = Video::empty();
+ scaler.run(rgb_frame, &mut yuv_frame).unwrap();
+
+ // Update the display time to maintain synchronization
+ encoder.encode_frame(yuv_frame, frame.display_time / 1_000_000);
+ } else {
+ // No previous frame to use
+ println!("No valid frame available to use.");
+ }
}
_ => println!("Failed to get frame"),
}
diff --git a/apps/desktop/src-tauri/src/encoder.rs b/apps/desktop/src-tauri/src/encoder.rs
index 627b1a13c..df0dd5311 100644
--- a/apps/desktop/src-tauri/src/encoder.rs
+++ b/apps/desktop/src-tauri/src/encoder.rs
@@ -145,21 +145,6 @@ impl H264Encoder {
}
}
-pub fn bgra_frame(bytes: &[u8], width: u32, height: u32) -> ffmpeg::frame::Video {
- let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, width, height);
-
- let stride = frame.stride(0);
-
- for (in_line, out_line) in bytes
- .chunks(width as usize * 4)
- .zip(frame.data_mut(0).chunks_mut(stride))
- {
- out_line[0..(width as usize * 4)].copy_from_slice(in_line);
- }
-
- frame
-}
-
pub fn uyvy422_frame(bytes: &[u8], width: u32, height: u32) -> ffmpeg::frame::Video {
let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::UYVY422, width, height);
@@ -187,6 +172,25 @@ pub fn nv12_frame(bytes: &[u8], width: u32, height: u32) -> ffmpeg::frame::Video
frame
}
+pub fn bgra_frame(bytes: &[u8], width: u32, height: u32) -> Option<ffmpeg::frame::Video> {
+ let expected_size = (width * height * 4) as usize;
+
+ if bytes.len() != expected_size {
+ // Unexpected frame data size
+ println!(
+ "Unexpected frame data size: expected {}, got {}",
+ expected_size,
+ bytes.len()
+ );
+ return None;
+ }
+
+ let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, width, height);
+ frame.data_mut(0).copy_from_slice(bytes);
+
+ Some(frame)
+}
+
pub struct MP3Encoder {
pub output: ffmpeg::format::context::Output,
pub context: ffmpeg::encoder::Audio,