Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

wip: Try reduce memory in encoding

Richie McIlroyc543ce3eeee1
diff --git a/apps/desktop/src-tauri/src/camera.rs b/apps/desktop/src-tauri/src/camera.rs
index c5e3e208e..5b3e36344 100644
--- a/apps/desktop/src-tauri/src/camera.rs
+++ b/apps/desktop/src-tauri/src/camera.rs
@@ -3,7 +3,7 @@ use specta::Type;
use std::{
path::PathBuf,
sync::Arc,
- time::{Instant, SystemTime},
+ time::{Duration, Instant, SystemTime},
};
use tokio::sync::{oneshot, watch};
@@ -87,47 +87,63 @@ pub async fn start_capturing(
)
.unwrap();
- loop {
- handle.block_on(frames_rx.changed());
+ let mut last_frame_time = Instant::now();
+ let frame_duration = Duration::from_secs_f64(1.0 / 30.0);
+ loop {
if controller.is_stopped() {
println!("Stopping receiving camera frames");
break;
}
- let frame = frames_rx.borrow_and_update();
- let Some((frame, timestamp)) = frame.as_ref() else {
- continue;
- };
+ // Use a non-blocking approach to check for changes
+ if frames_rx.has_changed().unwrap_or(false) {
+ let frame = frames_rx.borrow_and_update();
+ let Some((frame, timestamp)) = frame.as_ref() else {
+ continue;
+ };
- if controller.is_paused() {
- continue;
- }
+ if controller.is_paused() {
+ continue;
+ }
- if let Some(start_tx) = start_tx.take() {
- start_tx.send(Instant::now()).unwrap();
- }
+ if let Some(start_tx) = start_tx.take() {
+ start_tx.send(Instant::now()).unwrap();
+ }
- if !*start_writing_rx.borrow_and_update() {
- continue;
- }
+ if !*start_writing_rx.borrow_and_update() {
+ continue;
+ }
+
+ let now = Instant::now();
+ if now.duration_since(last_frame_time) < frame_duration {
+ std::thread::sleep(frame_duration - now.duration_since(last_frame_time));
+ }
+ last_frame_time = Instant::now();
+
+ let yuyv422_frame = uyvy422_frame(
+ frame.buffer(),
+ frame.resolution().width(),
+ frame.resolution().height(),
+ );
+
+ let mut yuv_frame = ffmpeg::util::frame::Video::empty();
+ if let Err(e) = scaler.run(&yuyv422_frame, &mut yuv_frame) {
+ eprintln!("Error scaling frame: {:?}", e);
+ continue;
+ }
- let yuyv422_frame = uyvy422_frame(
- frame.buffer(),
- frame.resolution().width(),
- frame.resolution().height(),
- );
-
- let mut yuv_frame = ffmpeg::util::frame::Video::empty();
- scaler.run(&yuyv422_frame, &mut yuv_frame).unwrap();
-
- encoder.encode_frame(
- yuv_frame,
- timestamp
- .duration_since(SystemTime::UNIX_EPOCH)
- .unwrap()
- .as_millis() as u64,
- );
+ encoder.encode_frame(
+ yuv_frame,
+ timestamp
+ .duration_since(SystemTime::UNIX_EPOCH)
+ .unwrap()
+ .as_millis() as u64,
+ );
+ } else {
+ // If no new frame, sleep for a short duration to avoid busy-waiting
+ std::thread::sleep(Duration::from_millis(1));
+ }
}
encoder.close();
diff --git a/apps/desktop/src-tauri/src/encoder.rs b/apps/desktop/src-tauri/src/encoder.rs
index cd2567899..ce16c1a28 100644
--- a/apps/desktop/src-tauri/src/encoder.rs
+++ b/apps/desktop/src-tauri/src/encoder.rs
@@ -93,25 +93,31 @@ impl H264Encoder {
(timestamp - start_time) as i64
} else {
self.start_time = Some(timestamp);
-
0
};
(delta_time as f64 / (1000.0 / self.fps)).round() as i64
};
- // we should probably do something better than just dropping frames lol
+ // Drop frames that are too old
if pts <= last_frame_pts {
return;
}
- // duplicate previous frame if this frame is >1 frame in the future
- while pts - 1 > last_frame_pts {
- last_frame_pts = last_frame_pts + 1;
+ // Limit the number of frames to duplicate
+ let max_duplicate_frames = 5;
+ let frames_to_duplicate = std::cmp::min(pts - last_frame_pts - 1, max_duplicate_frames);
+
+ // Duplicate previous frame if this frame is >1 frame in the future
+ for _ in 0..frames_to_duplicate {
+ last_frame_pts += 1;
if let Some(last_frame) = &mut self.last_frame {
last_frame.set_pts(Some(last_frame_pts));
- self.context.send_frame(&last_frame).unwrap();
+ if let Err(e) = self.context.send_frame(last_frame) {
+ eprintln!("Error sending duplicate frame: {:?}", e);
+ break;
+ }
}
self.receive_and_process_packets();
@@ -122,7 +128,9 @@ impl H264Encoder {
frame.set_pts(Some(0));
}
- self.context.send_frame(&frame).unwrap();
+ if let Err(e) = self.context.send_frame(&frame) {
+ eprintln!("Error sending frame: {:?}", e);
+ }
self.last_frame = Some(frame);
self.receive_and_process_packets();
@@ -137,14 +145,10 @@ impl H264Encoder {
self.output.stream(self.stream_index).unwrap().time_base(),
);
- // println!(
- // "writing packet - dts: {:?}, pts: {:?}, flags: {:?}",
- // encoded.dts(),
- // encoded.pts(),
- // encoded.flags()
- // );
-
- encoded.write_interleaved(&mut self.output).unwrap();
+ if let Err(e) = encoded.write_interleaved(&mut self.output) {
+ eprintln!("Error writing packet: {:?}", e);
+ break;
+ }
}
}