diff --git a/crates/enc-avfoundation/src/mp4.rs b/crates/enc-avfoundation/src/mp4.rs index 930d6d23b0f..81c5cec391e 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,55 @@ 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:?}" + ); + } + } + + #[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 { diff --git a/crates/enc-ffmpeg/src/remux.rs b/crates/enc-ffmpeg/src/remux.rs index 625a8b1a474..8a80a57620b 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,81 @@ 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())) + ); + } + + /// 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(); 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/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::*; diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 54440c3e061..60898b8c34a 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,31 @@ 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() + } + } +} + fn accumulate_finalization_error(failure: &mut Option, result: anyhow::Result<()>) { if let Err(error) = result { *failure = Some(match failure.take() { @@ -370,6 +395,8 @@ pub struct Actor { #[cfg(target_os = "linux")] resume_cleanup_error: Option, 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. @@ -414,8 +441,9 @@ impl Actor { async fn stop_pipeline( &mut self, 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; @@ -425,6 +453,7 @@ impl Actor { mut pipeline, mut media_error, all_tracks_stopped, + screen_empty, } = stopped?; self.all_tracks_stopped &= all_tracks_stopped; @@ -467,6 +496,11 @@ 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() { + self.leave_out_empty_segment(index)?; + return Ok((cursors, SegmentKept::No)); + } + self.segments.push(RecordingSegment { start: segment_start_time, end: segment_stop_time, @@ -482,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)] @@ -601,8 +665,11 @@ impl Actor { tokio::time::sleep_until(deadline.into()).await; } - match self.stop_pipeline(pipeline, segment_start_time).await { - Ok((cursors, _)) => cursors, + 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); let error = match ctx.actor_ref().stop_gracefully().await { @@ -650,6 +717,18 @@ impl Actor { }) }; + 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() + })); + 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( @@ -731,13 +810,16 @@ 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 { - 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, }); @@ -1310,10 +1392,19 @@ 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, all_tracks_stopped: bool, + /// The screen got no frame in this segment; see [`screen_captured_nothing`]. + screen_empty: bool, } fn classify_pipeline_stop_errors( @@ -1662,6 +1753,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()), ( @@ -1689,7 +1793,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", @@ -1761,6 +1866,7 @@ impl Pipeline { }, media_error, all_tracks_stopped: stop_errors.is_empty(), + screen_empty, }) } @@ -1840,6 +1946,18 @@ 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 { + // 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::() + && stopping.load(std::sync::atomic::Ordering::Acquire) => + { + warn!(?track, "Recording track captured no video frames"); + Ok(()) + } + result => result, + }; // ScreenCaptureKit ends the screen pipeline cleanly when its source goes away // (display asleep or unplugged, window closed, sharing stopped). The other tracks // are cancelled with it, so without a completion the app would keep showing a @@ -2540,6 +2658,7 @@ async fn spawn_studio_recording_actor( }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, segment_factory: segment_pipeline_factory, #[cfg(target_os = "linux")] resume_attempt: None, @@ -4213,6 +4332,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4323,6 +4443,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4374,6 +4495,7 @@ mod tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4575,6 +4697,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory, @@ -4649,6 +4772,7 @@ mod tests { }), all_tracks_stopped: false, terminal_stop_failure: None, + empty_segments: 0, segment_factory, segments: vec![RecordingSegment { start: 0.0, @@ -4821,6 +4945,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, #[cfg(windows)] cancel_error: None, segment_factory: factory, @@ -4897,6 +5022,106 @@ 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 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(); + 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, + stopping: Arc::new(std::sync::atomic::AtomicBool::new(false)), + }) + } + .boxed() + }), + ); + 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}"); + 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}"); + 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); + } + } + #[tokio::test] async fn failed_resume_waits_for_owned_cleanup_before_stop() { let temp = tempfile::tempdir().unwrap(); @@ -5084,6 +5309,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, segment_factory: factory, resume_attempt: None, resume_generation: 0, @@ -5544,7 +5770,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)), }) } @@ -6006,6 +6231,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, @@ -6817,6 +7115,7 @@ mod tests { }), all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, segment_factory, segments: Vec::new(), completion_tx, @@ -7021,6 +7320,81 @@ 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, + 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 recording_metadata_keeps_the_given_project_name() { let name = "Untitled (Window) 2026-11-06 03.29 PM"; @@ -7098,6 +7472,7 @@ mod windows_cancel_tests { state: None, all_tracks_stopped: true, terminal_stop_failure: None, + empty_segments: 0, cancel_error: error, segment_factory, segments: Vec::new(), 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..725c07c0a45 --- /dev/null +++ b/crates/recording/tests/hardware_studio_empty_segment.rs @@ -0,0 +1,213 @@ +#![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"); +} + +/// 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() { + 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:?}" + ); + 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 {saved} of {} segments, status {status:?}", + 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-1/display", + ] + } else { + [ + "content/segments/segment-0/display.mp4", + "content/segments/segment-1/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" + ); + } + assert!( + 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:?}"); + } +}