Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(recording): rewrite drain loop with deadline-based timeout

Richie McIlroy838be12a991c
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index 663fac9e8..2cdb505d1 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -1290,50 +1290,66 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: V
info!("mux-video cancelled, draining remaining frames from channel");
let drain_start = std::time::Instant::now();
let drain_timeout = Duration::from_secs(2);
+ let drain_deadline = tokio::time::Instant::now() + drain_timeout;
let max_drain_frames = 500u64;
let mut drained = 0u64;
let mut skipped = 0u64;
let mut hit_limit = false;
- while let Some(frame) = video_rx.next().await {
- frame_count += 1;
-
- if drain_start.elapsed() > drain_timeout || drained >= max_drain_frames {
+ loop {
+ if drained >= max_drain_frames {
hit_limit = true;
break;
}
- drained += 1;
-
- let timestamp = frame.timestamp();
+ match tokio::time::timeout_at(drain_deadline, video_rx.next()).await {
+ Ok(Some(frame)) => {
+ frame_count += 1;
+ drained += 1;
- if let Some(first_tx) = first_tx.take() {
- let _ = first_tx.send(timestamp);
- }
+ let timestamp = frame.timestamp();
- let raw_duration = match anomaly_tracker.process_timestamp(timestamp, timestamps) {
- Ok(d) => d,
- Err(_) => {
- warn!("Timestamp anomaly during drain, skipping frame");
- skipped += 1;
- continue;
- }
- };
+ if let Some(first_tx) = first_tx.take() {
+ let _ = first_tx.send(timestamp);
+ }
- if anomaly_tracker.take_resync_flag() {
- drift_tracker.reset_baseline();
- }
+ let raw_duration =
+ match anomaly_tracker.process_timestamp(timestamp, timestamps) {
+ Ok(d) => d,
+ Err(_) => {
+ warn!("Timestamp anomaly during drain, skipping frame");
+ skipped += 1;
+ continue;
+ }
+ };
- let raw_wall_clock = timestamps.instant().elapsed();
- let total_pause = shared_pause.total_pause_duration();
- let wall_clock_elapsed = raw_wall_clock.saturating_sub(total_pause);
- let duration = drift_tracker.calculate_timestamp(raw_duration, wall_clock_elapsed);
+ if anomaly_tracker.take_resync_flag() {
+ drift_tracker.reset_baseline();
+ }
- match muxer.lock().await.send_video_frame(frame, duration) {
- Ok(()) => {}
- Err(e) => {
- warn!("Error processing drained frame: {e}");
- skipped += 1;
+ let raw_wall_clock = timestamps.instant().elapsed();
+ let total_pause = shared_pause.total_pause_duration();
+ let wall_clock_elapsed = raw_wall_clock.saturating_sub(total_pause);
+ let duration =
+ drift_tracker.calculate_timestamp(raw_duration, wall_clock_elapsed);
+
+ match muxer.lock().await.send_video_frame(frame, duration) {
+ Ok(()) => {}
+ Err(e) => {
+ warn!("Error processing drained frame: {e}");
+ skipped += 1;
+ }
+ }
+ }
+ Ok(None) => break,
+ Err(_) => {
+ hit_limit = true;
+ warn!(
+ "mux-video drain timed out after {:?}, closing channel",
+ drain_start.elapsed()
+ );
+ video_rx.close();
+ break;
}
}
}