From 5493f6b2d41961307ec4ac25bcc6707090b1baf1 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:00:20 +0000 Subject: [PATCH 01/10] fix(enc-avfoundation): report NoFrames when a writer never got a frame --- crates/enc-avfoundation/src/mp4.rs | 57 +++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/enc-avfoundation/src/mp4.rs b/crates/enc-avfoundation/src/mp4.rs index 930d6d23b0f..e82002fde88 100644 --- a/crates/enc-avfoundation/src/mp4.rs +++ b/crates/enc-avfoundation/src/mp4.rs @@ -983,9 +983,14 @@ impl MP4Encoder { &mut self, timestamp: Option, ) -> Result, FinishError> { - if !self.session_started || self.is_finishing { + if self.is_finishing { return Err(FinishError::NotWriting); } + // No frame ever reached the writer, so it never started and there is + // nothing to finalize: an empty output, not a broken one. + if !self.session_started { + return Err(FinishError::NoFrames); + } let mut finish_timestamp = timestamp; @@ -4413,6 +4418,56 @@ mod tests { } } } + + /// A resumed Studio segment whose screen capture delivered no frame + /// before Pause. The writer never started, so finish reports an empty + /// output (`NoFrames`) rather than `NotWriting`, the error that failed + /// the whole recording. Paused or not, with or without an audio input. + #[test] + fn finish_without_any_frame_reports_no_frames() { + for (paused, audio) in [(false, false), (true, false), (false, true), (true, true)] { + let output = test_output_path("finish_without_any_frame"); + let audio_config = audio.then(wireless_audio_config); + let mut encoder = + MP4Encoder::init(output.clone(), valid_video_config(), audio_config, None).unwrap(); + if paused { + encoder.pause(); + } + let result = encoder.finish(Some(Duration::from_secs(1))); + drop(encoder); + let _ = std::fs::remove_file(&output); + assert!( + matches!(result, Err(FinishError::NoFrames)), + "paused {paused}, audio {audio}: {result:?}" + ); + } + } + + /// One frame is still a playable segment. + #[test] + fn finish_after_a_single_frame_writes_a_playable_file() { + let output = test_output_path("finish_after_a_single_frame"); + let config = VideoInfo::from_raw_ffmpeg(ffmpeg::format::Pixel::NV12, 1280, 720, 30); + let mut encoder = MP4Encoder::init(output.clone(), config, None, None).unwrap(); + let pool = create_pixel_buffer_pool(1280, 720); + let frame = create_test_video_frame(&pool, 0, 33_333); + assert!(queue_video_frame_with_retry(&mut encoder, frame, Duration::ZERO).unwrap()); + encoder.finish(Some(Duration::from_micros(33_333))).unwrap(); + drop(encoder); + + let mut input = ffmpeg::format::input(&output).unwrap(); + let index = input + .streams() + .best(ffmpeg::media::Type::Video) + .unwrap() + .index(); + let packets = input + .packets() + .filter(|(stream, _)| stream.index() == index) + .count(); + let _ = std::fs::remove_file(&output); + assert_eq!(packets, 1); + } } trait SampleBufExt { From 1ed734db3d84620f148733108e5d39c1936c2b63 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:00:20 +0000 Subject: [PATCH 02/10] fix(recording): mark empty AVFoundation output as NoVideoFramesCaptured --- crates/recording/src/output_pipeline/core.rs | 7 ++ crates/recording/src/output_pipeline/macos.rs | 64 +++++++++++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs index a071cec0e3e..8296ebe2220 100644 --- a/crates/recording/src/output_pipeline/core.rs +++ b/crates/recording/src/output_pipeline/core.rs @@ -5152,6 +5152,13 @@ impl PipelineStopSignal { #[error("Screen capture stopped from macOS sharing controls")] pub struct PipelineStoppedByUser; +/// The muxer never received a video frame, so the track has no media to +/// finalize: an empty output rather than a broken one. Studio leaves such a +/// segment out and keeps the rest of the recording. +#[derive(Debug, thiserror::Error)] +#[error("No video frames were captured")] +pub struct NoVideoFramesCaptured; + #[derive(Clone, Debug)] pub struct PipelineDoneError(Arc); diff --git a/crates/recording/src/output_pipeline/macos.rs b/crates/recording/src/output_pipeline/macos.rs index 0fd2e282a9b..7d09bfbe4de 100644 --- a/crates/recording/src/output_pipeline/macos.rs +++ b/crates/recording/src/output_pipeline/macos.rs @@ -1,8 +1,8 @@ use crate::{ output_pipeline::{ AudioFrame, AudioMuxer, BlockingThreadFinish, DiskSpaceMonitor, DiskSpacePollResult, - HealthSender, Muxer, PipelineHealthEvent, SharedHealthSender, TaskPool, VideoFrame, - VideoMuxer, emit_health, wait_for_blocking_thread_finish, + HealthSender, Muxer, NoVideoFramesCaptured, PipelineHealthEvent, SharedHealthSender, + TaskPool, VideoFrame, VideoMuxer, emit_health, wait_for_blocking_thread_finish, }, sources::screen_capture, }; @@ -810,7 +810,17 @@ impl Muxer for AVFoundationMp4Muxer { match state.encoder.lock() { Ok(mut encoder) => { if let Err(e) = encoder.finish(Some(timestamp)) { - if video_thread_timed_out && video_frames > 0 { + if matches!(e, cap_enc_avfoundation::FinishError::NoFrames) { + warn!( + video_frames, + video_frames_dropped_busy, + audio_frames, + "MP4 encoder wrote no video frames; the output is empty" + ); + if finish_error.is_none() { + finish_error = Some(anyhow::Error::new(NoVideoFramesCaptured)); + } + } else if video_thread_timed_out && video_frames > 0 { warn!( video_frames, "Best-effort encoder finalize failed after thread timeout, \ @@ -1416,7 +1426,12 @@ impl Muxer for AVFoundationCameraMuxer { match state.encoder.lock() { Ok(mut encoder) => { if let Err(e) = encoder.finish(Some(timestamp)) { - let err = anyhow!("Failed to finish camera MP4 encoder: {e}"); + let err = if matches!(e, cap_enc_avfoundation::FinishError::NoFrames) { + anyhow::Error::new(NoVideoFramesCaptured) + .context("Camera MP4 encoder wrote no video frames") + } else { + anyhow!("Failed to finish camera MP4 encoder: {e}") + }; warn!("{err:#}"); if finish_error.is_none() { finish_error = Some(err); @@ -1544,6 +1559,47 @@ impl AudioMuxer for AVFoundationCameraMuxer { mod tests { use super::*; + /// The writers behind a resumed Studio segment that got no screen frame + /// before Pause, and a segment whose camera got no frame. They report an + /// empty output the recorder can leave out, not a failed one. + #[tokio::test] + async fn finishing_without_a_frame_reports_no_video_frames() { + let directory = tempfile::tempdir().unwrap(); + let info = VideoInfo::from_raw(cap_media_info::RawVideoFormat::Bgra, 1280, 720, 30); + let mut tasks = TaskPool::default(); + + let mut screen = ::setup( + AVFoundationMp4MuxerConfig::default(), + directory.path().join("display.mp4"), + Some(info), + None, + Arc::new(AtomicBool::new(false)), + &mut tasks, + ) + .await + .unwrap(); + let error = screen.finish(Duration::from_secs(1)).unwrap_err(); + assert!(error.is::(), "screen: {error:#}"); + + let mut camera = ::setup( + AVFoundationCameraMuxerConfig::default(), + directory.path().join("camera.mp4"), + Some(info), + None, + Arc::new(AtomicBool::new(true)), + &mut tasks, + ) + .await + .unwrap(); + let error = camera.finish(Duration::from_secs(1)).unwrap_err(); + assert!( + error + .chain() + .any(|cause| cause.is::()), + "camera: {error:#}" + ); + } + mod mp4_muxer_buffer_size { use super::*; From a9285607e6118871c81bfd4011bd7cee781642dc Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:00:20 +0000 Subject: [PATCH 03/10] fix(recording): keep a Studio recording when a segment captures no frames --- crates/recording/src/studio_recording.rs | 399 ++++++++++++++++++++++- 1 file changed, 393 insertions(+), 6 deletions(-) diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 23cd756a4f1..04820a6bbd8 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -19,8 +19,8 @@ use crate::{ feeds::{camera::CameraFeedLock, microphone::MicrophoneFeedLock}, ffmpeg::{FragmentedAudioMuxer, FragmentedAudioMuxerConfig, OggMuxer, OggMuxerConfig}, output_pipeline::{ - AudioAnchor, AudioGapSummary, DoneFut, FinishedOutputPipeline, OutputPipeline, - OutputPipelineStopOutcome, PipelineDoneError, + AudioAnchor, AudioGapSummary, DoneFut, FinishedOutputPipeline, NoVideoFramesCaptured, + OutputPipeline, OutputPipelineStopOutcome, PipelineDoneError, }, screen_capture::ScreenCaptureConfig, sources::{self, screen_capture}, @@ -125,6 +125,68 @@ fn studio_capture_stopped(error: &anyhow::Error) -> bool { .any(|cause| cause.downcast_ref::().is_some()) } +/// A track whose muxer never got a video frame has empty media, not broken +/// media. +fn captured_no_video(error: &anyhow::Error) -> bool { + error.chain().any(|cause| { + cause.is::() + || cause + .downcast_ref::() + .is_some_and(PipelineDoneError::is_caused_by::) + }) +} + +/// The screen pipeline of a segment stopped without a single frame (a +/// resumed segment paused before capture delivered anything). It has no +/// video to show, so the segment is left out instead of failing the whole +/// recording. +fn screen_captured_nothing(outcome: &OutputPipelineStopOutcome) -> bool { + match &outcome.media_error { + Some(error) => captured_no_video(error), + None => { + outcome.finished.video_frame_count == 0 + && outcome.finished.video_timestamp_span.is_none() + } + } +} + +/// Markers are captured against segment indices; a segment left out for +/// having no screen frames shifts every later segment down by one. A marker +/// pressed inside such a segment lands at the end of the segment before it. +fn markers_without_empty_segments( + markers: Vec, + empty_segments: &[u32], +) -> Vec { + if empty_segments.is_empty() { + return markers; + } + let position = |segment: u32| { + segment + - empty_segments + .iter() + .filter(|&&empty| empty < segment) + .count() as u32 + }; + markers + .into_iter() + .filter_map(|marker| { + if !empty_segments.contains(&marker.segment) { + return Some(RecordedMarker { + segment: position(marker.segment), + offset: marker.offset, + }); + } + let before = (0..marker.segment) + .rev() + .find(|segment| !empty_segments.contains(segment))?; + Some(RecordedMarker { + segment: position(before), + offset: None, + }) + }) + .collect() +} + fn accumulate_finalization_error(failure: &mut Option, result: anyhow::Result<()>) { if let Err(error) = result { *failure = Some(match failure.take() { @@ -370,6 +432,8 @@ pub struct Actor { #[cfg(target_os = "linux")] resume_cleanup_error: Option, segments: Vec, + /// Indices of segments left out because their screen got no frame. + empty_segments: Vec, completion_tx: watch::Sender>>, // Resolved once at recording start: the display can be disconnected, or its // mode changed, by the time the recording stops. @@ -414,6 +478,7 @@ impl Actor { async fn stop_pipeline( &mut self, pipeline: Pipeline, + index: u32, segment_start_time: f64, ) -> anyhow::Result<(Cursors, u32)> { tracing::info!("pipeline shutting down"); @@ -425,6 +490,7 @@ impl Actor { mut pipeline, mut media_error, all_tracks_stopped, + screen_empty, } = stopped?; self.all_tracks_stopped &= all_tracks_stopped; @@ -467,6 +533,15 @@ impl Actor { let camera_device_id = self.segment_factory.camera_device_id(); let mic_device_id = self.segment_factory.mic_device_id(); + if screen_empty && media_error.is_none() { + warn!( + segment = index, + "Studio segment captured no screen frames; leaving it out and keeping the rest of the recording" + ); + self.empty_segments.push(index); + return Ok(cursors); + } + self.segments.push(RecordingSegment { start: segment_start_time, end: segment_stop_time, @@ -601,7 +676,10 @@ impl Actor { tokio::time::sleep_until(deadline.into()).await; } - match self.stop_pipeline(pipeline, segment_start_time).await { + match self + .stop_pipeline(pipeline, index, segment_start_time) + .await + { Ok((cursors, _)) => cursors, Err(error) if studio_capture_stopped(&error) => { let error = self.preserve_failure_evidence(error); @@ -650,6 +728,18 @@ impl Actor { }) }; + if self.segments.is_empty() && !self.empty_segments.is_empty() { + let error = anyhow!(known_failure.unwrap_or_else(|| { + "Screen capture delivered no frames, so nothing was recorded".to_string() + })); + let error = if self.all_tracks_stopped { + anyhow::Error::new(StudioCaptureStoppedError::new(error)) + } else { + error + }; + return Err(self.preserve_failure_evidence(error)); + } + self.update_diagnostic_segment_count(); self.markers.stopped(); let recording = stop_recording( @@ -662,7 +752,7 @@ impl Actor { pretty_name: self.pretty_name.as_deref(), }, known_failure, - self.markers.snapshot(), + markers_without_empty_segments(self.markers.snapshot(), &self.empty_segments), ) .await; let recording = match recording { @@ -731,7 +821,7 @@ impl Message for Actor { diagnostic.stage("pausing"); } let stopped = self - .stop_pipeline(pipeline, segment_start_time) + .stop_pipeline(pipeline, index, segment_start_time) .await .context("stop_pipeline"); match stopped { @@ -1315,6 +1405,8 @@ struct PipelineStopOutcome { pipeline: FinishedPipeline, media_error: Option, all_tracks_stopped: bool, + /// The screen got no frame in this segment; see [`screen_captured_nothing`]. + screen_empty: bool, } fn classify_pipeline_stop_errors( @@ -1664,6 +1756,19 @@ impl Pipeline { )); } + let screen_empty = screen.as_ref().is_ok_and(screen_captured_nothing); + // A camera with no frame in this segment is left out of it, like a + // segment recorded with the camera off. + let camera = camera.filter(|result| { + let empty = result + .as_ref() + .is_ok_and(|outcome| outcome.media_error.as_ref().is_some_and(captured_no_video)); + if empty { + warn!("Studio camera captured no frames in this segment; leaving it out"); + } + !empty + }); + let stop_errors = [ ("display", screen.as_ref().err()), ( @@ -1691,7 +1796,8 @@ impl Pipeline { screen .as_ref() .ok() - .and_then(|outcome| outcome.media_error.as_ref()), + .and_then(|outcome| outcome.media_error.as_ref()) + .filter(|_| !screen_empty), ), ( "microphone", @@ -1763,6 +1869,7 @@ impl Pipeline { }, media_error, all_tracks_stopped: stop_errors.is_empty(), + screen_empty, }) } @@ -1843,6 +1950,15 @@ impl Pipeline { let track_failures = self.track_failures.clone(); self.watcher_task = Some(tokio::spawn(async move { while let Some((track, required, res)) = futures.next().await { + // Only a finishing muxer reports this: the track ended with + // empty media, which Stop handles, not a failure. + let res = match res { + Err(error) if error.is_caused_by::() => { + warn!(?track, "Recording track captured no video frames"); + Ok(()) + } + result => result, + }; #[cfg(any(target_os = "linux", windows))] let res = match res { Ok(()) if !stopping.load(std::sync::atomic::Ordering::Acquire) => { @@ -2526,6 +2642,7 @@ async fn spawn_studio_recording_actor( }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), segment_factory: segment_pipeline_factory, #[cfg(target_os = "linux")] resume_attempt: None, @@ -4200,6 +4317,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), #[cfg(windows)] cancel_error: None, segment_factory, @@ -4310,6 +4428,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), #[cfg(windows)] cancel_error: None, segment_factory, @@ -4361,6 +4480,7 @@ mod tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), #[cfg(windows)] cancel_error: None, segment_factory, @@ -4563,6 +4683,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), #[cfg(windows)] cancel_error: None, segment_factory, @@ -4637,6 +4758,7 @@ mod tests { }), all_tracks_stopped: false, terminal_stop_failure: None, + empty_segments: Vec::new(), segment_factory, segments: vec![RecordingSegment { start: 0.0, @@ -4809,6 +4931,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), #[cfg(windows)] cancel_error: None, segment_factory: factory, @@ -4885,6 +5008,85 @@ mod tests { } } + /// A user reported a Studio recording lost after Pause logged + /// `Failed to finish MP4 encoder: NotWriting`: the resumed segment's + /// screen had not delivered a frame. Pause and Stop must succeed and + /// keep every recorded segment, leaving the empty one out. + #[tokio::test] + async fn a_resumed_segment_without_screen_frames_is_left_out() { + for (stop_while_recording, reports_empty) in + [(false, true), (true, true), (false, false), (true, false)] + { + let case = format!( + "stop_while_recording {stop_while_recording}, reports_empty {reports_empty}" + ); + let temp = tempfile::tempdir().unwrap(); + let senders = Arc::new(std::sync::Mutex::new(Vec::new())); + let prepare_senders = senders.clone(); + let display = temp.path().join("content/segments/segment-2/display.mp4"); + let handle = paused_handle( + temp.path(), + Arc::new(move |_, _| { + let senders = prepare_senders.clone(); + let display = display.clone(); + async move { + let (tx, rx) = flume::bounded(4); + senders.lock().unwrap().push(tx); + let timestamps = Timestamps::now(); + Ok(Pipeline { + start_time: timestamps, + start_gate: None, + screen: empty_screen_pipeline( + display, + timestamps, + rx, + reports_empty, + ) + .await?, + microphone: None, + camera: None, + system_audio: None, + cursor: None, + track_failures: Default::default(), + watcher_task: None, + #[cfg(windows)] + stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), + }) + } + .boxed() + }), + ); + handle.resume().await.unwrap(); + if !stop_while_recording { + handle.pause().await.unwrap_or_else(|error| { + panic!("{case}: pausing an empty segment failed the recording: {error:#}") + }); + assert!(handle.is_paused().await.unwrap(), "{case}"); + } + let report = handle.stop_with_intent(StudioStopIntent::Preserve).await; + assert!(report.accepted_intent && report.stop_acknowledged, "{case}"); + let completed = report + .result + .unwrap_or_else(|error| panic!("{case}: stop failed: {error}")); + assert!( + matches!(completed.meta.status(), StudioRecordingStatus::Complete), + "{case}: {:?}", + completed.meta.status() + ); + let StudioRecordingMeta::MultipleSegments { inner } = completed.meta else { + panic!("{case}: expected saved segments"); + }; + let kept: Vec<_> = inner + .segments + .iter() + .map(|segment| segment.display.path.as_str().to_string()) + .collect(); + assert_eq!(kept, ["saved-0.m4s", "saved-1.m4s"], "{case}"); + assert!(handle.done_fut().await.is_ok(), "{case}"); + drop(senders); + } + } + #[tokio::test] async fn failed_resume_waits_for_owned_cleanup_before_stop() { let temp = tempfile::tempdir().unwrap(); @@ -5072,6 +5274,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), segment_factory: factory, resume_attempt: None, resume_generation: 0, @@ -5904,6 +6107,79 @@ mod tests { } } + /// Finishes like the AVFoundation writer: no frame means empty output. + #[derive(Default)] + struct EmptyVideoMuxer { + frames: u64, + } + + impl Muxer for EmptyVideoMuxer { + type Config = (); + + async fn setup( + _config: Self::Config, + _output_path: PathBuf, + _video_config: Option, + _audio_config: Option, + _pause_flag: Arc, + _tasks: &mut TaskPool, + ) -> anyhow::Result { + Ok(Self::default()) + } + + fn finish(&mut self, _timestamp: Duration) -> anyhow::Result> { + if self.frames == 0 { + return Err(anyhow::Error::new(NoVideoFramesCaptured)); + } + Ok(Ok(())) + } + } + + impl AudioMuxer for EmptyVideoMuxer { + fn send_audio_frame( + &mut self, + _frame: crate::output_pipeline::AudioFrame, + _timestamp: Duration, + ) -> anyhow::Result<()> { + Ok(()) + } + } + + impl VideoMuxer for EmptyVideoMuxer { + type VideoFrame = TestVideoFrame; + + fn send_video_frame( + &mut self, + _frame: Self::VideoFrame, + _timestamp: Duration, + ) -> anyhow::Result<()> { + self.frames += 1; + Ok(()) + } + } + + /// The screen pipeline of a segment paused before capture delivered a + /// frame: built with a writer that reports empty output (AVFoundation) + /// or one that finishes cleanly with nothing in it (fragmented). + async fn empty_screen_pipeline( + path: PathBuf, + timestamps: Timestamps, + rx: flume::Receiver, + reports_empty: bool, + ) -> anyhow::Result { + let builder = OutputPipeline::builder(path) + .with_video::>(ChannelVideoSourceConfig::new( + test_video_info(), + rx, + )) + .with_timestamps(timestamps); + if reports_empty { + builder.build::(()).await + } else { + builder.build::(()).await + } + } + #[derive(Clone, Copy)] struct FailingAudioMuxerConfig { fail_after_frame: u64, @@ -6717,6 +6993,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), segment_factory, segments: Vec::new(), completion_tx, @@ -6922,6 +7199,115 @@ mod tests { ); } + /// A segment paused before its screen and camera delivered a frame. Stop + /// confirms capture stopped and reports the screen as empty, with no + /// media error, no track failure and no failed completion. The camera is + /// left out of the segment and the microphone is kept. + #[tokio::test] + async fn stop_reports_a_screen_and_camera_that_captured_nothing_as_empty() { + for reports_empty in [true, false] { + let temp_dir = tempfile::tempdir().unwrap(); + let timestamps = Timestamps::now(); + let (_screen_tx, screen_rx) = flume::bounded(4); + let (_camera_tx, camera_rx) = flume::bounded(4); + let (completion_tx, completion_rx) = watch::channel(None); + let (mut microphone_tx, microphone_rx) = futures::channel::mpsc::channel(4); + let screen = empty_screen_pipeline( + temp_dir.path().join("display.mp4"), + timestamps, + screen_rx, + reports_empty, + ) + .await + .unwrap(); + let camera = empty_screen_pipeline( + temp_dir.path().join("camera.mp4"), + timestamps, + camera_rx, + true, + ) + .await + .unwrap(); + let microphone = OutputPipeline::builder(temp_dir.path().join("audio-input.ogg")) + .with_audio_source::(ChannelAudioSourceConfig::new( + test_audio_info(), + microphone_rx, + )) + .with_timestamps(timestamps) + .build::(FailingAudioMuxerConfig { + fail_after_frame: u64::MAX, + }) + .await + .unwrap(); + let mut pipeline = Pipeline { + start_time: timestamps, + start_gate: None, + screen, + microphone: Some(microphone), + camera: Some(camera), + system_audio: None, + cursor: None, + track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), + watcher_task: None, + #[cfg(any(target_os = "linux", windows))] + stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), + }; + pipeline.spawn_watcher(completion_tx); + microphone_tx + .try_send(crate::output_pipeline::AudioFrame::new( + test_audio_info().empty_frame(960), + Timestamp::Instant(timestamps.instant() + Duration::from_millis(20)), + )) + .unwrap(); + + let stopped = pipeline.stop().await.unwrap(); + assert!(stopped.screen_empty, "reports_empty {reports_empty}"); + assert!( + stopped.media_error.is_none(), + "reports_empty {reports_empty}: {:?}", + stopped.media_error + ); + assert!(stopped.all_tracks_stopped); + assert!(stopped.pipeline.camera.is_none()); + assert!(stopped.pipeline.microphone.is_some()); + assert!(stopped.pipeline.track_failures.is_empty()); + assert!(completion_rx.borrow().is_none()); + } + } + + #[test] + fn markers_follow_the_segments_kept_around_empty_ones() { + let marker = |segment, offset| RecordedMarker { segment, offset }; + assert_eq!( + markers_without_empty_segments( + vec![ + marker(0, Some(1.0)), + marker(1, Some(0.5)), + marker(1, None), + marker(2, Some(2.0)), + marker(4, None), + ], + &[1, 3], + ), + vec![ + marker(0, Some(1.0)), + marker(0, None), + marker(0, None), + marker(1, Some(2.0)), + marker(2, None), + ] + ); + assert_eq!( + markers_without_empty_segments(vec![marker(0, Some(0.2)), marker(1, Some(0.3))], &[0]), + vec![marker(0, Some(0.3))] + ); + let untouched = vec![marker(0, Some(0.2)), marker(3, None)]; + assert_eq!( + markers_without_empty_segments(untouched.clone(), &[]), + untouched + ); + } + #[test] fn recording_metadata_keeps_the_given_project_name() { let name = "Untitled (Window) 2026-11-06 03.29 PM"; @@ -6999,6 +7385,7 @@ mod windows_cancel_tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: Vec::new(), cancel_error: error, segment_factory, segments: Vec::new(), From 128dd81404245e81764880c30425dabe82e1c334 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:00:20 +0000 Subject: [PATCH 04/10] test(recording): real-screen check for a segment without screen frames --- .../tests/hardware_studio_empty_segment.rs | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 crates/recording/tests/hardware_studio_empty_segment.rs diff --git a/crates/recording/tests/hardware_studio_empty_segment.rs b/crates/recording/tests/hardware_studio_empty_segment.rs new file mode 100644 index 00000000000..03ce0dfdc23 --- /dev/null +++ b/crates/recording/tests/hardware_studio_empty_segment.rs @@ -0,0 +1,185 @@ +#![cfg(target_os = "macos")] + +//! A resumed Studio segment whose screen capture delivers no frames. The +//! AVFoundation writer never starts, so its finish used to fail with +//! `NotWriting` and the pause failed the whole recording. Lives in its own +//! test binary because `cap_fail` switches are process-wide. +//! +//! Set `CAP_EMPTY_SEGMENT_OUT` to keep the recordings for an export check. + +use cap_enc_ffmpeg::remux::{probe_media_valid, probe_video_can_decode}; +use cap_project::{RecordingMeta, StudioRecordingMeta, StudioRecordingStatus}; +use cap_recording::sources::screen_capture::ScreenCaptureTarget; +use cap_recording::{SendableShareableContent, studio_recording}; +use std::{path::PathBuf, time::Duration}; +use tempfile::TempDir; + +const SKIP_SCREEN_FRAMES: &str = "cap-recording::screen_capture video frame skip"; + +fn init() { + tracing_subscriber::fmt() + .with_env_filter( + tracing_subscriber::EnvFilter::from_default_env() + .add_directive(tracing::Level::INFO.into()), + ) + .with_test_writer() + .try_init() + .ok(); + ffmpeg::init().expect("failed to initialize ffmpeg"); +} + +#[cfg(debug_assertions)] +#[tokio::test] +async fn studio_segment_without_screen_frames_keeps_the_recording_with_real_screen() { + init(); + let shareable_content: SendableShareableContent = cidre::sc::ShareableContent::current() + .await + .expect("Screen Recording permission is required") + .into(); + let temp = TempDir::new().unwrap(); + let root = std::env::var_os("CAP_EMPTY_SEGMENT_OUT") + .map(PathBuf::from) + .unwrap_or_else(|| temp.path().to_path_buf()); + + // No fail point: Pause straight after Resume can beat the first frame of + // the new capture stream. Every pause must still succeed. + for fragmented in [false, true] { + let recording_dir = root.join(format!("quick-pause-fragmented-{fragmented}.cap")); + let _ = std::fs::remove_dir_all(&recording_dir); + std::fs::create_dir_all(&recording_dir).unwrap(); + let handle = studio_recording::Actor::builder( + recording_dir.clone(), + ScreenCaptureTarget::Display { + id: scap_targets::Display::primary().id(), + }, + ) + .with_fragmented(fragmented) + .with_system_audio(true) + .with_max_fps(30) + .with_keyboard_capture(false) + .build(Some(shareable_content.clone())) + .await + .unwrap(); + tokio::time::sleep(Duration::from_secs(1)).await; + const QUICK_PAUSES: usize = 8; + for attempt in 0..QUICK_PAUSES { + handle.pause().await.unwrap_or_else(|error| { + panic!("fragmented {fragmented}: pause {attempt} failed the recording: {error:#}") + }); + handle.resume().await.unwrap(); + } + tokio::time::sleep(Duration::from_secs(1)).await; + handle.stop().await.unwrap_or_else(|error| { + panic!("fragmented {fragmented}: stop after quick pauses failed: {error:#}") + }); + let meta = RecordingMeta::load_for_project(&recording_dir).unwrap(); + let studio = meta.studio_meta().unwrap(); + let status = studio.status(); + let StudioRecordingMeta::MultipleSegments { inner } = studio else { + panic!("fragmented {fragmented}: not a multi-segment Studio recording"); + }; + assert!( + !matches!(status, StudioRecordingStatus::Failed { .. }), + "fragmented {fragmented}: status {status:?}" + ); + eprintln!( + "quick pauses, fragmented {fragmented}: kept {} of {} segments, status {status:?}", + inner.segments.len(), + QUICK_PAUSES + 1 + ); + } + + for fragmented in [false, true] { + let recording_dir = root.join(format!("empty-segment-fragmented-{fragmented}.cap")); + let _ = std::fs::remove_dir_all(&recording_dir); + std::fs::create_dir_all(&recording_dir).unwrap(); + let handle = studio_recording::Actor::builder( + recording_dir.clone(), + ScreenCaptureTarget::Display { + id: scap_targets::Display::primary().id(), + }, + ) + .with_fragmented(fragmented) + .with_system_audio(true) + .with_max_fps(30) + .with_keyboard_capture(false) + .build(Some(shareable_content.clone())) + .await + .unwrap(); + + tokio::time::sleep(Duration::from_secs(2)).await; + handle.pause().await.unwrap(); + + cap_fail::set_fail(SKIP_SCREEN_FRAMES, true); + let resumed = handle.resume().await; + tokio::time::sleep(Duration::from_millis(1500)).await; + let pause_started = std::time::Instant::now(); + let paused = handle.pause().await; + let pause_ms = pause_started.elapsed().as_secs_f64() * 1000.0; + cap_fail::set_fail(SKIP_SCREEN_FRAMES, false); + eprintln!("fragmented {fragmented}: empty pause took {pause_ms:.0} ms"); + resumed.unwrap(); + paused.unwrap_or_else(|error| { + panic!( + "fragmented {fragmented}: pausing a segment with no screen frames failed: {error:#}" + ) + }); + + handle.resume().await.unwrap(); + tokio::time::sleep(Duration::from_secs(2)).await; + handle.stop().await.unwrap_or_else(|error| { + panic!("fragmented {fragmented}: stop after an empty segment failed: {error:#}") + }); + + let meta = RecordingMeta::load_for_project(&recording_dir).unwrap(); + let studio = meta.studio_meta().unwrap(); + let status = studio.status(); + let StudioRecordingMeta::MultipleSegments { inner } = studio else { + panic!("fragmented {fragmented}: not a multi-segment Studio recording"); + }; + assert!( + !matches!(status, StudioRecordingStatus::Failed { .. }), + "fragmented {fragmented}: status {status:?}" + ); + let kept: Vec<_> = inner + .segments + .iter() + .map(|segment| segment.display.path.as_str().to_string()) + .collect(); + assert_eq!( + kept, + if fragmented { + [ + "content/segments/segment-0/display", + "content/segments/segment-2/display", + ] + } else { + [ + "content/segments/segment-0/display.mp4", + "content/segments/segment-2/display.mp4", + ] + }, + "fragmented {fragmented}: the two recorded segments are kept and the empty one is left out" + ); + for segment in &inner.segments { + assert!( + segment.system_audio.is_some(), + "fragmented {fragmented}: a kept segment lost its audio" + ); + if !fragmented { + let display = segment.display.path.to_path(&recording_dir); + assert!(probe_media_valid(&display), "{}", display.display()); + assert!( + probe_video_can_decode(&display).unwrap_or(false), + "{}", + display.display() + ); + } + } + assert!( + recording_dir.join("content/segments/segment-1").is_dir(), + "fragmented {fragmented}: the empty segment's files stay on disk" + ); + eprintln!("fragmented {fragmented}: kept {kept:?}, status {status:?}"); + } +} From aebea6a57ec0b203b0a8ec1424259749813a9223 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:45:06 +0000 Subject: [PATCH 05/10] fix(recording): reuse an empty segment's index so recovery can finalize --- crates/recording/src/markers.rs | 71 ++++++++ crates/recording/src/studio_recording.rs | 212 ++++++++++------------- 2 files changed, 167 insertions(+), 116 deletions(-) diff --git a/crates/recording/src/markers.rs b/crates/recording/src/markers.rs index b566cfa09a5..e123b6045dd 100644 --- a/crates/recording/src/markers.rs +++ b/crates/recording/src/markers.rs @@ -131,6 +131,35 @@ impl RecordingMarkers { } } + /// The segment was left out of the recording (its screen captured no + /// frame) and the next segment takes its index. Markers pressed in it + /// move to the end of the segment before, or go when it was the first. + pub(crate) fn segment_dropped(&self, segment: u32) { + let revision = { + let mut state = self.lock(); + state.segments.retain(|clock| clock.segment != segment); + if !state.markers.iter().any(|marker| marker.segment == segment) { + return; + } + let before = segment.checked_sub(1); + state.markers = std::mem::take(&mut state.markers) + .into_iter() + .filter_map(|marker| { + if marker.segment != segment { + return Some(marker); + } + before.map(|segment| RecordedMarker { + segment, + offset: None, + }) + }) + .collect(); + state.revision += 1; + state.revision + }; + self.persist(revision); + } + pub(crate) fn stopped(&self) { self.lock().stopped_at.get_or_insert_with(Instant::now); } @@ -408,6 +437,48 @@ mod tests { assert_eq!(times[3], (1, 5.0)); } + #[test] + fn a_dropped_segment_hands_its_markers_to_the_segment_before() { + let markers = RecordingMarkers::new(None); + let start = Timestamps::now(); + markers.segment_started(0, start, None); + markers.add(start.instant() + Duration::from_millis(500)); + markers.segment_paused(0); + let resumed = Timestamps::now(); + markers.segment_started(1, resumed, None); + markers.add(resumed.instant() + Duration::from_millis(1)); + markers.segment_paused(1); + + markers.segment_dropped(1); + let kept = markers.snapshot(); + assert_eq!(kept.len(), 2); + assert_eq!(kept[0].segment, 0); + assert!(kept[0].offset.is_some()); + assert_eq!( + kept[1], + RecordedMarker { + segment: 0, + offset: None + } + ); + + let reused = Timestamps::now(); + markers.segment_started(1, reused, None); + let placed = markers + .add(reused.instant() + Duration::from_millis(1)) + .unwrap(); + assert_eq!(placed, 3); + assert_eq!(markers.snapshot()[2].segment, 1); + assert!(markers.snapshot()[2].offset.is_some()); + + let first = RecordingMarkers::new(None); + let start = Timestamps::now(); + first.segment_started(0, start, None); + first.add(start.instant() + Duration::from_millis(1)); + first.segment_dropped(0); + assert!(first.snapshot().is_empty()); + } + #[test] fn recovery_renumbers_segments_it_kept() { let recorded = [ diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 04820a6bbd8..6ae18d286c6 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -150,43 +150,6 @@ fn screen_captured_nothing(outcome: &OutputPipelineStopOutcome) -> bool { } } -/// Markers are captured against segment indices; a segment left out for -/// having no screen frames shifts every later segment down by one. A marker -/// pressed inside such a segment lands at the end of the segment before it. -fn markers_without_empty_segments( - markers: Vec, - empty_segments: &[u32], -) -> Vec { - if empty_segments.is_empty() { - return markers; - } - let position = |segment: u32| { - segment - - empty_segments - .iter() - .filter(|&&empty| empty < segment) - .count() as u32 - }; - markers - .into_iter() - .filter_map(|marker| { - if !empty_segments.contains(&marker.segment) { - return Some(RecordedMarker { - segment: position(marker.segment), - offset: marker.offset, - }); - } - let before = (0..marker.segment) - .rev() - .find(|segment| !empty_segments.contains(segment))?; - Some(RecordedMarker { - segment: position(before), - offset: None, - }) - }) - .collect() -} - fn accumulate_finalization_error(failure: &mut Option, result: anyhow::Result<()>) { if let Err(error) = result { *failure = Some(match failure.take() { @@ -432,8 +395,8 @@ pub struct Actor { #[cfg(target_os = "linux")] resume_cleanup_error: Option, segments: Vec, - /// Indices of segments left out because their screen got no frame. - empty_segments: Vec, + /// Segments left out because their screen got no frame. + empty_segments: u32, completion_tx: watch::Sender>>, // Resolved once at recording start: the display can be disconnected, or its // mode changed, by the time the recording stops. @@ -480,7 +443,7 @@ impl Actor { pipeline: Pipeline, index: u32, segment_start_time: f64, - ) -> anyhow::Result<(Cursors, u32)> { + ) -> anyhow::Result<((Cursors, u32), SegmentKept)> { tracing::info!("pipeline shutting down"); let stopped = pipeline.stop().await; @@ -534,12 +497,8 @@ impl Actor { let mic_device_id = self.segment_factory.mic_device_id(); if screen_empty && media_error.is_none() { - warn!( - segment = index, - "Studio segment captured no screen frames; leaving it out and keeping the rest of the recording" - ); - self.empty_segments.push(index); - return Ok(cursors); + self.leave_out_empty_segment(index)?; + return Ok((cursors, SegmentKept::No)); } self.segments.push(RecordingSegment { @@ -557,7 +516,37 @@ impl Actor { return Err(error.context(message)); } - Ok(cursors) + Ok((cursors, SegmentKept::Yes)) + } + + /// A segment whose screen captured no frame is left out of the recording + /// and gives its index to the next segment, so the folders under + /// `content/segments` stay numbered like the saved segments, which + /// recovery and finalization rely on. Its other files are kept under + /// `content/empty-segments`. + fn leave_out_empty_segment(&mut self, index: u32) -> anyhow::Result<()> { + let segments_dir = &self.segment_factory.segments_dir; + let source = segments_dir.join(format!("segment-{index}")); + if source.exists() { + let target_dir = segments_dir.with_file_name("empty-segments"); + let target = target_dir.join(format!("segment-{index}-{}", self.empty_segments)); + std::fs::create_dir_all(&target_dir) + .and_then(|()| std::fs::rename(&source, &target)) + .with_context(|| { + format!("Could not move aside segment {index}, which captured no screen frames") + })?; + } + warn!( + segment = index, + "Studio segment captured no screen frames; leaving it out and keeping the rest of the recording" + ); + self.empty_segments += 1; + self.segment_factory.index = index; + if index == 0 { + self.segment_factory.base_inputs.start_gate = None; + } + self.markers.segment_dropped(index); + Ok(()) } #[cfg(windows)] @@ -680,7 +669,7 @@ impl Actor { .stop_pipeline(pipeline, index, segment_start_time) .await { - Ok((cursors, _)) => cursors, + Ok(((cursors, _), _)) => cursors, Err(error) if studio_capture_stopped(&error) => { let error = self.preserve_failure_evidence(error); let error = match ctx.actor_ref().stop_gracefully().await { @@ -728,7 +717,7 @@ impl Actor { }) }; - if self.segments.is_empty() && !self.empty_segments.is_empty() { + if self.segments.is_empty() && self.empty_segments > 0 { let error = anyhow!(known_failure.unwrap_or_else(|| { "Screen capture delivered no frames, so nothing was recorded".to_string() })); @@ -752,7 +741,7 @@ impl Actor { pretty_name: self.pretty_name.as_deref(), }, known_failure, - markers_without_empty_segments(self.markers.snapshot(), &self.empty_segments), + self.markers.snapshot(), ) .await; let recording = match recording { @@ -825,9 +814,12 @@ impl Message for Actor { .await .context("stop_pipeline"); match stopped { - Ok((cursors, next_cursor_id)) if self.all_tracks_stopped => { + Ok(((cursors, next_cursor_id), kept)) if self.all_tracks_stopped => { self.state = Some(ActorState::Paused { - next_index: index + 1, + next_index: match kept { + SegmentKept::Yes => index + 1, + SegmentKept::No => index, + }, cursors, next_cursor_id, }); @@ -1386,7 +1378,6 @@ struct Pipeline { pub cursor: Option, pub track_failures: SharedTrackFailures, pub watcher_task: Option>, - #[cfg(any(target_os = "linux", windows))] stopping: Arc, } @@ -1401,6 +1392,13 @@ struct FinishedPipeline { pub track_failures: Vec, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum SegmentKept { + Yes, + /// Left out for having no screen frames; its index is reused. + No, +} + struct PipelineStopOutcome { pipeline: FinishedPipeline, media_error: Option, @@ -1713,7 +1711,6 @@ impl Pipeline { pub async fn stop(mut self) -> anyhow::Result { let stop_started = Instant::now(); let epoch = self.epoch(); - #[cfg(any(target_os = "linux", windows))] self.stopping .store(true, std::sync::atomic::Ordering::Release); #[cfg(target_os = "macos")] @@ -1945,15 +1942,17 @@ impl Pipeline { .into_iter() .flatten() .collect::>(); - #[cfg(any(target_os = "linux", windows))] let stopping = self.stopping.clone(); let track_failures = self.track_failures.clone(); self.watcher_task = Some(tokio::spawn(async move { while let Some((track, required, res)) = futures.next().await { - // Only a finishing muxer reports this: the track ended with - // empty media, which Stop handles, not a failure. + // Empty media at Pause or Stop is left out by Stop; a track + // that ends on its own before Stop is still a failure. let res = match res { - Err(error) if error.is_caused_by::() => { + Err(error) + if error.is_caused_by::() + && stopping.load(std::sync::atomic::Ordering::Acquire) => + { warn!(?track, "Recording track captured no video frames"); Ok(()) } @@ -2642,7 +2641,7 @@ async fn spawn_studio_recording_actor( }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, segment_factory: segment_pipeline_factory, #[cfg(target_os = "linux")] resume_attempt: None, @@ -3920,7 +3919,6 @@ async fn create_segment_pipeline( system_audio, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }) } @@ -4317,7 +4315,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4428,7 +4426,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4480,7 +4478,7 @@ mod tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4674,7 +4672,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(windows)] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }, index: 0, @@ -4683,7 +4680,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4758,7 +4755,7 @@ mod tests { }), all_tracks_stopped: false, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, segment_factory, segments: vec![RecordingSegment { start: 0.0, @@ -4931,7 +4928,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory: factory, @@ -5030,6 +5027,9 @@ mod tests { let senders = prepare_senders.clone(); let display = display.clone(); async move { + let segment_dir = display.parent().unwrap(); + std::fs::create_dir_all(segment_dir)?; + std::fs::write(segment_dir.join("audio-input.ogg"), b"audio")?; let (tx, rx) = flume::bounded(4); senders.lock().unwrap().push(tx); let timestamps = Timestamps::now(); @@ -5049,19 +5049,23 @@ mod tests { cursor: None, track_failures: Default::default(), watcher_task: None, - #[cfg(windows)] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }) } .boxed() }), ); - handle.resume().await.unwrap(); - if !stop_while_recording { - handle.pause().await.unwrap_or_else(|error| { - panic!("{case}: pausing an empty segment failed the recording: {error:#}") - }); - assert!(handle.is_paused().await.unwrap(), "{case}"); + let empty_segments = if stop_while_recording { 1 } else { 2 }; + for _ in 0..empty_segments { + handle.resume().await.unwrap(); + if !stop_while_recording { + handle.pause().await.unwrap_or_else(|error| { + panic!( + "{case}: pausing an empty segment failed the recording: {error:#}" + ) + }); + assert!(handle.is_paused().await.unwrap(), "{case}"); + } } let report = handle.stop_with_intent(StudioStopIntent::Preserve).await; assert!(report.accepted_intent && report.stop_acknowledged, "{case}"); @@ -5083,6 +5087,20 @@ mod tests { .collect(); assert_eq!(kept, ["saved-0.m4s", "saved-1.m4s"], "{case}"); assert!(handle.done_fut().await.is_ok(), "{case}"); + assert!( + !temp.path().join("content/segments/segment-2").exists(), + "{case}: the next segment can take the empty one's index" + ); + for moved in 0..empty_segments { + assert_eq!( + std::fs::read(temp.path().join(format!( + "content/empty-segments/segment-2-{moved}/audio-input.ogg" + ))) + .unwrap(), + b"audio", + "{case}: the empty segment's other files are kept" + ); + } drop(senders); } } @@ -5274,7 +5292,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, segment_factory: factory, resume_attempt: None, resume_generation: 0, @@ -5735,7 +5753,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(target_os = "linux")] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }) } @@ -5843,7 +5860,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; @@ -5966,6 +5982,7 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, + stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; let stopping = tokio::spawn(pipeline.stop()); @@ -6265,7 +6282,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; pipeline.spawn_start_time_persister(); @@ -6355,7 +6371,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; let pipeline = Pipeline { @@ -6993,7 +7008,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, segment_factory, segments: Vec::new(), completion_tx, @@ -7129,7 +7144,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; pipeline.spawn_watcher(completion_tx); @@ -7249,7 +7263,6 @@ mod tests { cursor: None, track_failures: Arc::new(std::sync::Mutex::new(Vec::new())), watcher_task: None, - #[cfg(any(target_os = "linux", windows))] stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), }; pipeline.spawn_watcher(completion_tx); @@ -7275,39 +7288,6 @@ mod tests { } } - #[test] - fn markers_follow_the_segments_kept_around_empty_ones() { - let marker = |segment, offset| RecordedMarker { segment, offset }; - assert_eq!( - markers_without_empty_segments( - vec![ - marker(0, Some(1.0)), - marker(1, Some(0.5)), - marker(1, None), - marker(2, Some(2.0)), - marker(4, None), - ], - &[1, 3], - ), - vec![ - marker(0, Some(1.0)), - marker(0, None), - marker(0, None), - marker(1, Some(2.0)), - marker(2, None), - ] - ); - assert_eq!( - markers_without_empty_segments(vec![marker(0, Some(0.2)), marker(1, Some(0.3))], &[0]), - vec![marker(0, Some(0.3))] - ); - let untouched = vec![marker(0, Some(0.2)), marker(3, None)]; - assert_eq!( - markers_without_empty_segments(untouched.clone(), &[]), - untouched - ); - } - #[test] fn recording_metadata_keeps_the_given_project_name() { let name = "Untitled (Window) 2026-11-06 03.29 PM"; @@ -7385,7 +7365,7 @@ mod windows_cancel_tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, - empty_segments: Vec::new(), + empty_segments: 0, cancel_error: error, segment_factory, segments: Vec::new(), From 4a6f182720031408c241e792a0ea99e200cb8f1a Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:45:06 +0000 Subject: [PATCH 06/10] test(recording): finalize and open the real-screen recordings --- .../tests/hardware_studio_empty_segment.rs | 58 ++++++++++++++----- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/crates/recording/tests/hardware_studio_empty_segment.rs b/crates/recording/tests/hardware_studio_empty_segment.rs index 03ce0dfdc23..725c07c0a45 100644 --- a/crates/recording/tests/hardware_studio_empty_segment.rs +++ b/crates/recording/tests/hardware_studio_empty_segment.rs @@ -28,6 +28,34 @@ fn init() { ffmpeg::init().expect("failed to initialize ffmpeg"); } +/// What the app does after Stop: remux a fragmented recording, then every +/// saved segment's display must open. Returns the number of saved segments. +fn finalize_and_open(recording_dir: &std::path::Path, fragmented: bool) -> usize { + cap_recording::recovery::RecoveryManager::remux_if_needed(recording_dir).unwrap_or_else( + |error| panic!("fragmented {fragmented}: finalizing the recording failed: {error}"), + ); + let meta = RecordingMeta::load_for_project(recording_dir).unwrap(); + let studio = meta.studio_meta().unwrap(); + let status = studio.status(); + assert!( + matches!(status, StudioRecordingStatus::Complete), + "fragmented {fragmented}: status after finalizing {status:?}" + ); + let StudioRecordingMeta::MultipleSegments { inner } = studio else { + panic!("fragmented {fragmented}: not a multi-segment Studio recording"); + }; + for segment in &inner.segments { + let display = segment.display.path.to_path(recording_dir); + assert!(probe_media_valid(&display), "{}", display.display()); + assert!( + probe_video_can_decode(&display).unwrap_or(false), + "{}", + display.display() + ); + } + inner.segments.len() +} + #[cfg(debug_assertions)] #[tokio::test] async fn studio_segment_without_screen_frames_keeps_the_recording_with_real_screen() { @@ -82,9 +110,11 @@ async fn studio_segment_without_screen_frames_keeps_the_recording_with_real_scre !matches!(status, StudioRecordingStatus::Failed { .. }), "fragmented {fragmented}: status {status:?}" ); + let saved = inner.segments.len(); + let opened = finalize_and_open(&recording_dir, fragmented); + assert_eq!(opened, saved, "fragmented {fragmented}"); eprintln!( - "quick pauses, fragmented {fragmented}: kept {} of {} segments, status {status:?}", - inner.segments.len(), + "quick pauses, fragmented {fragmented}: kept {saved} of {} segments, status {status:?}", QUICK_PAUSES + 1 ); } @@ -151,12 +181,12 @@ async fn studio_segment_without_screen_frames_keeps_the_recording_with_real_scre if fragmented { [ "content/segments/segment-0/display", - "content/segments/segment-2/display", + "content/segments/segment-1/display", ] } else { [ "content/segments/segment-0/display.mp4", - "content/segments/segment-2/display.mp4", + "content/segments/segment-1/display.mp4", ] }, "fragmented {fragmented}: the two recorded segments are kept and the empty one is left out" @@ -166,20 +196,18 @@ async fn studio_segment_without_screen_frames_keeps_the_recording_with_real_scre segment.system_audio.is_some(), "fragmented {fragmented}: a kept segment lost its audio" ); - if !fragmented { - let display = segment.display.path.to_path(&recording_dir); - assert!(probe_media_valid(&display), "{}", display.display()); - assert!( - probe_video_can_decode(&display).unwrap_or(false), - "{}", - display.display() - ); - } } assert!( - recording_dir.join("content/segments/segment-1").is_dir(), - "fragmented {fragmented}: the empty segment's files stay on disk" + recording_dir + .join("content/empty-segments/segment-1-0") + .is_dir(), + "fragmented {fragmented}: the empty segment's files are kept aside" + ); + assert!( + !recording_dir.join("content/segments/segment-2").exists(), + "fragmented {fragmented}: the next segment took the empty one's index" ); + assert_eq!(finalize_and_open(&recording_dir, fragmented), 2); eprintln!("fragmented {fragmented}: kept {kept:?}, status {status:?}"); } } From f4b2f6770e1f5353adc1040fcd1db5cca32ee303 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:45:06 +0000 Subject: [PATCH 07/10] test(enc-avfoundation): drop a comment that repeats the test name --- crates/enc-avfoundation/src/mp4.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/enc-avfoundation/src/mp4.rs b/crates/enc-avfoundation/src/mp4.rs index e82002fde88..81c5cec391e 100644 --- a/crates/enc-avfoundation/src/mp4.rs +++ b/crates/enc-avfoundation/src/mp4.rs @@ -4443,7 +4443,6 @@ mod tests { } } - /// One frame is still a playable segment. #[test] fn finish_after_a_single_frame_writes_a_playable_file() { let output = test_output_path("finish_after_a_single_frame"); From c8bf59e4b3d86532e1dbbe1840f397e310099e01 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:15:01 +0000 Subject: [PATCH 08/10] fix(enc-ffmpeg): keep a lone durationless frame playable when remuxing --- crates/enc-ffmpeg/src/remux.rs | 66 ++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/crates/enc-ffmpeg/src/remux.rs b/crates/enc-ffmpeg/src/remux.rs index 625a8b1a474..a0f7ca4d0e9 100644 --- a/crates/enc-ffmpeg/src/remux.rs +++ b/crates/enc-ffmpeg/src/remux.rs @@ -264,6 +264,7 @@ struct StreamRemuxer { stream_mapping: Vec>, last_dts: Vec, dts_offset: Vec, + video_frame_rate: Vec>, } impl StreamRemuxer { @@ -272,6 +273,7 @@ impl StreamRemuxer { octx: &mut avformat::context::Output, ) -> Result { let mut stream_mapping = Vec::new(); + let mut video_frame_rate = Vec::new(); let mut output_stream_index = 0usize; for input_stream in ictx.streams() { @@ -281,6 +283,11 @@ impl StreamRemuxer { if medium == ffmpeg::media::Type::Video || medium == ffmpeg::media::Type::Audio { stream_mapping.push(Some(output_stream_index)); output_stream_index += 1; + video_frame_rate.push( + (medium == ffmpeg::media::Type::Video) + .then(|| input_stream.avg_frame_rate()) + .filter(|rate| rate.numerator() > 0 && rate.denominator() > 0), + ); let mut output_stream = octx.add_stream(None)?; output_stream.set_parameters(codec_params); @@ -298,6 +305,7 @@ impl StreamRemuxer { stream_mapping, last_dts: vec![i64::MIN; output_stream_index], dts_offset: vec![0; output_stream_index], + video_frame_rate, }) } @@ -313,6 +321,20 @@ impl StreamRemuxer { packet.rescale_ts(input_time_base, output_time_base); + // Fragmented recordings carry no sample durations. The muxer + // derives every duration from the next sample, except the last + // one; a lone frame would end up with a zero-length track whose + // only sample readers discard. Give a first video packet without + // a duration one frame. + if self.last_dts[output_index] == i64::MIN + && packet.duration() <= 0 + && let Some(rate) = self.video_frame_rate[output_index] + { + packet.set_duration( + ffmpeg::Rescale::rescale(&1i64, rate.invert(), output_time_base).max(1), + ); + } + let current_dts = packet.dts().unwrap_or(0); if self.last_dts[output_index] != i64::MIN && current_dts <= self.last_dts[output_index] @@ -1486,6 +1508,12 @@ mod tests { }; fn encode_test_segments(directory: &Path) -> (PathBuf, Vec) { + let (init, segments) = encode_test_frames(directory, 320); + assert!(segments.len() >= 2); + (init, segments) + } + + fn encode_test_frames(directory: &Path, frames: u64) -> (PathBuf, Vec) { use crate::segmented_stream::{SegmentedVideoEncoder, SegmentedVideoEncoderConfig}; ffmpeg::init().unwrap(); @@ -1501,7 +1529,7 @@ mod tests { SegmentedVideoEncoderConfig::default(), ) .unwrap(); - for index in 0..320 { + for index in 0..frames { let mut frame = ffmpeg::frame::Video::new(ffmpeg::format::Pixel::NV12, 320, 240); frame.data_mut(0).fill(32 + (index % 192) as u8); frame.data_mut(1).fill(128); @@ -1515,7 +1543,6 @@ mod tests { .iter() .map(|segment| segment.path.clone()) .collect(); - assert!(segments.len() >= 2); (encoder.init_segment_path(), segments) } @@ -1635,6 +1662,41 @@ mod tests { let _ = plain; } + /// A Studio segment paused right after it resumed can hold one frame. + /// Its fragment has no sample durations, so the progressive file used to + /// get a zero-length track whose only frame readers discard, and + /// finalizing failed the whole recording. + #[test] + fn remux_of_a_single_durationless_frame_stays_playable() { + let directory = tempfile::tempdir().unwrap(); + let (init, segments) = encode_test_frames(&directory.path().join("source"), 1); + let single = directory.path().join("single.mp4"); + concatenate_m4s_segments_with_init(&init, &segments, &single).unwrap(); + // The macOS Studio fragments carry no sample durations. + let output = directory.path().join("output.mp4"); + { + let mut ictx = ffmpeg::format::input(&single).unwrap(); + let mut octx = ffmpeg::format::output(&output).unwrap(); + let mut remuxer = super::StreamRemuxer::new(&ictx, &mut octx).unwrap(); + for (stream, mut packet) in ictx.packets() { + packet.set_duration(0); + remuxer + .write_packet(packet, stream.time_base(), &mut octx) + .unwrap(); + } + octx.write_trailer().unwrap(); + } + assert_eq!(decoded_video_frame_count(&output), 1); + assert!(super::probe_video_can_decode(&output).unwrap()); + super::probe_video_seek_points(&output, 8).unwrap(); + let (time_base, packets) = video_packet_contents(&output); + assert_eq!(packets.len(), 1); + assert_eq!( + packets[0].duration, + i64::from(time_base.denominator()) / (30 * i64::from(time_base.numerator())) + ); + } + #[test] fn validated_m4s_concat_preserves_packets_timestamps_and_all_frames() { let directory = tempfile::tempdir().unwrap(); From 6a39dcd8217b6915e6a9da03053a5b6d271bd888 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:58:37 +0000 Subject: [PATCH 09/10] chore: retrigger CI From cbfc1ce84a0d34276f7b7d470a4f4ac6a89451c2 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 17 Nov 2026 02:16:57 +0000 Subject: [PATCH 10/10] test(enc-ffmpeg): durationless frames keep their timestamps through remux --- crates/enc-ffmpeg/src/remux.rs | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/enc-ffmpeg/src/remux.rs b/crates/enc-ffmpeg/src/remux.rs index a0f7ca4d0e9..8a80a57620b 100644 --- a/crates/enc-ffmpeg/src/remux.rs +++ b/crates/enc-ffmpeg/src/remux.rs @@ -1697,6 +1697,46 @@ mod tests { ); } + /// The fallback only lengthens a lone frame: with more frames, a long + /// gap after the first (a static screen right after Resume) keeps every + /// timestamp where it was. + #[test] + fn remux_of_durationless_frames_keeps_their_timestamps() { + let directory = tempfile::tempdir().unwrap(); + let (init, segments) = encode_test_frames(&directory.path().join("source"), 3); + let source = directory.path().join("source.mp4"); + concatenate_m4s_segments_with_init(&init, &segments, &source).unwrap(); + let (source_time_base, source_packets) = video_packet_contents(&source); + let frame = i64::from(source_time_base.denominator()) + / (30 * i64::from(source_time_base.numerator())); + let stamps = [0, 24 * frame, 25 * frame]; + assert!(source_packets.iter().all(|packet| packet.pts == packet.dts)); + + let output = directory.path().join("output.mp4"); + { + let mut ictx = ffmpeg::format::input(&source).unwrap(); + let mut octx = ffmpeg::format::output(&output).unwrap(); + let mut remuxer = super::StreamRemuxer::new(&ictx, &mut octx).unwrap(); + for (index, (stream, mut packet)) in ictx.packets().enumerate() { + packet.set_pts(Some(stamps[index])); + packet.set_dts(Some(stamps[index])); + packet.set_duration(0); + remuxer + .write_packet(packet, stream.time_base(), &mut octx) + .unwrap(); + } + octx.write_trailer().unwrap(); + } + let (time_base, packets) = video_packet_contents(&output); + assert_eq!(time_base, source_time_base); + assert_eq!( + packets.iter().map(|packet| packet.pts).collect::>(), + stamps.map(Some) + ); + assert_eq!(packets[0].duration, 24 * frame); + assert_eq!(decoded_video_frame_count(&output), 3); + } + #[test] fn validated_m4s_concat_preserves_packets_timestamps_and_all_frames() { let directory = tempfile::tempdir().unwrap();