Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(capture): freeze staging while paused and snapshot wall-clock before flush (CodeRabbit #512)

Benjamin Freeman4951864ec544
diff --git a/electron/native/pipewire-capture/src/capture.rs b/electron/native/pipewire-capture/src/capture.rs
index 6bc4b500b..821d6b239 100644
--- a/electron/native/pipewire-capture/src/capture.rs
+++ b/electron/native/pipewire-capture/src/capture.rs
@@ -392,6 +392,16 @@ impl Capture {
/// dmabuf the GPU cannot map) returns `Ok(Dropped)` rather than `Err`, so it
/// costs one frame, not the recording; a genuine encoder error still errors.
pub fn stage(&mut self, frame: &shim::Frame) -> Result<StageOutcome, String> {
+ // A paused recording must not ingest new pixels. The compositor keeps
+ // streaming while the app is paused — pause is app-side — so frames still
+ // arrive here; staging one would move the held picture to POST-pause
+ // content, which `finish`'s tail write would then encode into the file if
+ // a stop follows a pause with no resume. Freeze the staged picture at the
+ // pause instant instead — reported as `Frozen` so it is not counted as a
+ // dropped frame.
+ if self.paused_at.is_some() {
+ return Ok(StageOutcome::Frozen);
+ }
// Zero-copy dmabuf path: import the tiled GPU buffer into an NV12 VAAPI
// surface (the VPP crops a window to its committed rectangle) and hand it
// to the encoder as-is — no swscale. See issue #507.
@@ -584,7 +594,9 @@ impl Capture {
// recording that ended during a quiet spell would be short by that gap.
// Runs even when stopped while PAUSED: `current_index()` freezes at the
// pause boundary, so the active time up to the pause still reaches the
- // timeline (a stop can follow a pause with no resume in between).
+ // timeline (a stop can follow a pause with no resume in between). The
+ // staged picture is the last PRE-pause frame — `stage` is gated on pause —
+ // so this cannot leak post-pause content into the file.
if self.encoder.has_staged_frame() {
let target = self.current_index();
if target >= self.next_index {
@@ -596,6 +608,17 @@ impl Capture {
}
}
+ // Snapshot the active wall-clock time HERE, before the flush below.
+ // Draining audio, the encoder and the mp4 trailer can take tens of ms on
+ // a long recording, and `elapsed_active` keeps ticking through it; reading
+ // it afterwards would make a slow flush look like a timeline divergence
+ // (issue #512). The video timeline (`next_index`) is already frozen at the
+ // tail write above, so this is the moment the two are meant to agree.
+ let wall_clock_ms = self
+ .elapsed_active()
+ .map(|elapsed| elapsed.as_millis() as u64)
+ .unwrap_or(0);
+
// Audio first: whatever is still in the rings is real recorded sound,
// and draining it before the video flush keeps both ending at roughly
// the same timestamp. `flush` lets the mix take unevenly-filled inputs,
@@ -611,10 +634,6 @@ impl Capture {
.finish(|packet| muxer.write(video_track, packet))?;
muxer.finish()?;
- let wall_clock_ms = self
- .elapsed_active()
- .map(|elapsed| elapsed.as_millis() as u64)
- .unwrap_or(0);
Ok(Summary {
path: self.path.clone(),
// From the timeline, not the frame count: the last PTS is
@@ -1178,6 +1197,29 @@ mod tests {
let _ = std::fs::remove_file(&output);
}
+ #[test]
+ fn frames_arriving_while_paused_are_not_staged() {
+ // A paused recording must not ingest new pixels. The compositor keeps
+ // streaming while the app is paused, so frames still arrive; staging one
+ // would move the held picture to post-pause content, which the tail write
+ // in finish() could then encode when a stop follows a pause (#512). Pause
+ // must hold that privacy boundary — the staged picture freezes.
+ let output = std::env::temp_dir().join("openscreen-capture-pause-stage.mp4");
+ let (mut capture, _) =
+ Capture::start(&output, 320, 240, 30, Some(1_000_000), Some(Backend::Software), Vec::new())
+ .expect("start");
+
+ // Pause BEFORE any frame is staged, then a frame arrives during the pause.
+ capture.pause();
+ let staged = capture.stage(&frame(320, 240, shim::constants().video_format_bgrx));
+ assert!(staged.is_ok(), "staging while paused is a no-op, not an error: {staged:?}");
+ assert!(!capture.started(), "a frame received while paused must not start the timeline");
+
+ let summary = capture.finish().expect("finish");
+ assert_eq!(summary.frames, 0, "nothing captured while paused may reach the file");
+ let _ = std::fs::remove_file(&output);
+ }
+
#[test]
fn finishing_while_paused_still_records_the_active_time_before_the_pause() {
// Stop can arrive while paused — the user pauses, then decides to stop