Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Invalid frame data / stride on diff sizes

Richie McIlroyac56f5c44116
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index ef76cf189..f564219b1 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -2,7 +2,7 @@
"name": "@cap/desktop",
"type": "module",
"scripts": {
- "dev": "RUST_BACKTRACE=1 dotenv -e ../../.env -- tauri dev",
+ "dev": "RUST_BACKTRACE=full dotenv -e ../../.env -- tauri dev",
"localdev": "vinxi dev --port 3001",
"build": "vinxi build",
"tauri": "tauri"
diff --git a/apps/desktop/src-tauri/src/display.rs b/apps/desktop/src-tauri/src/display.rs
index 9d22d3df9..8dc3fe1c1 100644
--- a/apps/desktop/src-tauri/src/display.rs
+++ b/apps/desktop/src-tauri/src/display.rs
@@ -195,8 +195,7 @@ 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;
+ let mut last_valid_frame: Option<ffmpeg::frame::Video> = None;
loop {
if controller.is_stopped() {
@@ -219,28 +218,28 @@ pub async fn start_capturing(
match frame {
Ok(Frame::BGRA(frame)) => {
- if let Some(rgb_frame) =
- bgra_frame(&frame.data, capture_size[0], capture_size[1])
- {
+ if let Some(rgb_frame) = bgra_frame(
+ &frame.data,
+ capture_size[0],
+ capture_size[1],
+ last_valid_frame.as_ref(),
+ ) {
// Successfully created RGB frame
- last_rgb_frame = Some(rgb_frame.clone());
+ last_valid_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 create valid frame, using last valid frame.");
+ if let Some(ref last_frame) = last_valid_frame {
+ let mut yuv_frame = Video::empty();
+ scaler.run(last_frame, &mut yuv_frame).unwrap();
+ encoder.encode_frame(yuv_frame, frame.display_time / 1_000_000);
+ } else {
+ 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 8dc1bf9ea..ddd1fd595 100644
--- a/apps/desktop/src-tauri/src/encoder.rs
+++ b/apps/desktop/src-tauri/src/encoder.rs
@@ -1,6 +1,11 @@
use std::path::PathBuf;
-use ffmpeg::{self as ffmpeg, Dictionary};
+use ffmpeg::{
+ self as ffmpeg,
+ format::{self as avformat, context::Output, Pixel},
+ frame::Video,
+ Dictionary,
+};
macro_rules! dict {
( $($key:expr => $value:expr),* $(,)*) => ({
@@ -179,21 +184,37 @@ 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;
+pub fn bgra_frame(
+ data: &[u8],
+ width: u32,
+ height: u32,
+ previous_frame: Option<&ffmpeg::frame::Video>,
+) -> Option<ffmpeg::frame::Video> {
+ let expected_size = (width as usize) * (height as usize) * 4; // 4 bytes per pixel for BGRA
- if bytes.len() != expected_size {
- // Unexpected frame data size
+ if data.len() != expected_size {
println!(
- "Unexpected frame data size: expected {}, got {}",
+ "Invalid frame size: expected {}, got {}",
expected_size,
- bytes.len()
+ data.len()
);
- return None;
+ return previous_frame.cloned();
}
- let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::BGRA, width, height);
- frame.data_mut(0).copy_from_slice(bytes);
+ let mut frame = Video::new(Pixel::BGRA, width, height);
+
+ let frame_linesize = frame.stride(0) as usize;
+ let bytes_per_pixel = 4; // For BGRA format
+
+ let expected_linesize = (width as usize) * bytes_per_pixel;
+
+ // Copy data line by line considering linesize differences
+ for (src_row, dst_row) in data
+ .chunks_exact(expected_linesize)
+ .zip(frame.data_mut(0).chunks_exact_mut(frame_linesize))
+ {
+ dst_row[..expected_linesize].copy_from_slice(src_row);
+ }
Some(frame)
}