Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: validate clean recording fragments while remuxing
Richie McIlroy82a12907501b
diff --git a/crates/enc-ffmpeg/src/remux.rs b/crates/enc-ffmpeg/src/remux.rsindex c7be19764..949cdfd9f 100644--- a/crates/enc-ffmpeg/src/remux.rs+++ b/crates/enc-ffmpeg/src/remux.rs@@ -3,7 +3,7 @@ use std::{io::Write,path::{Path, PathBuf},ptr,- sync::atomic::{AtomicI32, Ordering},+ sync::atomic::{AtomicI32, AtomicU64, Ordering},time::Duration,};@@ -13,6 +13,7 @@ use ffmpeg::{ChannelLayout, codec as avcodec, format as avformat, packet::Mut asuse crate::audio::opus::{OpusEncoder, OpusEncoderError};static ORIGINAL_LOG_LEVEL: AtomicI32 = AtomicI32::new(-1);+static VALIDATED_AGGREGATE_SEQUENCE: AtomicU64 = AtomicU64::new(0);const SEEK_PROBE_PACKET_LIMIT: usize = 240;const SEEK_PROBE_PADDING_US: i64 = 250_000;@@ -53,6 +54,8 @@ pub enum RemuxError {AudioInfo(#[from] AudioInfoError),#[error("Concat demuxer not found")]ConcatDemuxerNotFound,+ #[error("Video validation failed: {0}")]+ VideoValidation(&'static str),}pub fn concatenate_video_fragments(fragments: &[PathBuf], output: &Path) -> Result<(), RemuxError> {@@ -153,55 +156,68 @@ fn open_input_with_format(}}-fn remux_streams(- ictx: &mut avformat::context::Input,- octx: &mut avformat::context::Output,-) -> Result<(), RemuxError> {- let mut stream_mapping: Vec<Option<usize>> = Vec::new();- let mut output_stream_index = 0usize;+struct StreamRemuxer {+ stream_mapping: Vec<Option<usize>>,+ last_dts: Vec<i64>,+ dts_offset: Vec<i64>,+}- for input_stream in ictx.streams() {- let codec_params = input_stream.parameters();- let medium = codec_params.medium();+impl StreamRemuxer {+ fn new(+ ictx: &avformat::context::Input,+ octx: &mut avformat::context::Output,+ ) -> Result<Self, RemuxError> {+ let mut stream_mapping = Vec::new();+ let mut output_stream_index = 0usize;- if medium == ffmpeg::media::Type::Video || medium == ffmpeg::media::Type::Audio {- stream_mapping.push(Some(output_stream_index));- output_stream_index += 1;+ for input_stream in ictx.streams() {+ let codec_params = input_stream.parameters();+ let medium = codec_params.medium();- let mut output_stream = octx.add_stream(None)?;- output_stream.set_parameters(codec_params);- unsafe {- (*output_stream.as_mut_ptr()).time_base = (*input_stream.as_ptr()).time_base;+ if medium == ffmpeg::media::Type::Video || medium == ffmpeg::media::Type::Audio {+ stream_mapping.push(Some(output_stream_index));+ output_stream_index += 1;++ let mut output_stream = octx.add_stream(None)?;+ output_stream.set_parameters(codec_params);+ unsafe {+ (*output_stream.as_mut_ptr()).time_base = (*input_stream.as_ptr()).time_base;+ }+ } else {+ stream_mapping.push(None);}- } else {- stream_mapping.push(None);}- }- octx.write_header()?;-- let mut last_dts: Vec<i64> = vec![i64::MIN; output_stream_index];- let mut dts_offset: Vec<i64> = vec![0; output_stream_index];+ octx.write_header()?;- for (input_stream, packet) in ictx.packets() {- let input_stream_index = input_stream.index();+ Ok(Self {+ stream_mapping,+ last_dts: vec![i64::MIN; output_stream_index],+ dts_offset: vec![0; output_stream_index],+ })+ }- if let Some(Some(output_index)) = stream_mapping.get(input_stream_index) {+ fn write_packet(+ &mut self,+ mut packet: ffmpeg::Packet,+ input_time_base: ffmpeg::Rational,+ octx: &mut avformat::context::Output,+ ) -> Result<(), RemuxError> {+ if let Some(Some(output_index)) = self.stream_mapping.get(packet.stream()) {let output_index = *output_index;- let mut packet = packet;- let input_time_base = input_stream.time_base();let output_time_base = octx.stream(output_index).unwrap().time_base();packet.rescale_ts(input_time_base, output_time_base);let current_dts = packet.dts().unwrap_or(0);- if last_dts[output_index] != i64::MIN && current_dts <= last_dts[output_index] {- dts_offset[output_index] = last_dts[output_index] - current_dts + 1;+ if self.last_dts[output_index] != i64::MIN && current_dts <= self.last_dts[output_index]+ {+ self.dts_offset[output_index] = self.last_dts[output_index] - current_dts + 1;}- let adjusted_dts = current_dts + dts_offset[output_index];- let adjusted_pts = packet.pts().map(|pts| pts + dts_offset[output_index]);+ let adjusted_dts = current_dts + self.dts_offset[output_index];+ let adjusted_pts = packet.pts().map(|pts| pts + self.dts_offset[output_index]);unsafe {(*packet.as_mut_ptr()).dts = adjusted_dts;@@ -210,13 +226,24 @@ fn remux_streams(}}- last_dts[output_index] = adjusted_dts;+ self.last_dts[output_index] = adjusted_dts;packet.set_stream(output_index);packet.set_position(-1);packet.write_interleaved(octx)?;}+ Ok(())+ }+}++fn remux_streams(+ ictx: &mut avformat::context::Input,+ octx: &mut avformat::context::Output,+) -> Result<(), RemuxError> {+ let mut remuxer = StreamRemuxer::new(ictx, octx)?;+ for (input_stream, packet) in ictx.packets() {+ remuxer.write_packet(packet, input_stream.time_base(), octx)?;}octx.write_trailer()?;@@ -224,6 +251,84 @@ fn remux_streams(Ok(())}+fn remux_streams_validated_with_reader(+ ictx: &mut avformat::context::Input,+ octx: &mut avformat::context::Output,+ mut read_packet: impl FnMut(+ &mut ffmpeg::Packet,+ &mut avformat::context::Input,+ ) -> Result<(), ffmpeg::Error>,+) -> Result<(), RemuxError> {+ let stream = ictx+ .streams()+ .best(ffmpeg::media::Type::Video)+ .ok_or(RemuxError::VideoValidation("No video stream found"))?;+ let video_index = stream.index();+ let context = avcodec::Context::from_parameters(stream.parameters())?;+ let codec = ffmpeg::decoder::find(context.id())+ .ok_or(RemuxError::VideoValidation("Video decoder unavailable"))?;+ let mut decoder = context.decoder();+ decoder.check(avcodec::decoder::Check::EXPLODE | avcodec::decoder::Check::CRC);+ let mut decoder = decoder.open_as(codec)?;+ let mut frame = unsafe { ffmpeg::Frame::empty() };+ let mut decoded = false;+ let mut remuxer = StreamRemuxer::new(ictx, octx)?;++ loop {+ let mut packet = ffmpeg::Packet::empty();+ match read_packet(&mut packet, ictx) {+ Ok(()) => {}+ Err(ffmpeg::Error::Eof) => break,+ Err(error) => return Err(error.into()),+ }+ if packet.stream() == video_index {+ if packet.is_corrupt() {+ return Err(RemuxError::VideoValidation("Corrupt video packet"));+ }+ if !decoded {+ decoder.send_packet(&packet)?;+ loop {+ match decoder.receive_frame(&mut frame) {+ Ok(()) if !frame.is_corrupt() => decoded = true,+ Ok(()) => {+ return Err(RemuxError::VideoValidation("Corrupt decoded video frame"));+ }+ Err(ffmpeg::Error::Other { errno }) if errno == ffmpeg::ffi::EAGAIN => {+ break;+ }+ Err(error) => return Err(error.into()),+ }+ }+ }+ }+ let input_time_base = ictx+ .stream(packet.stream())+ .ok_or(ffmpeg::Error::StreamNotFound)?+ .time_base();+ remuxer.write_packet(packet, input_time_base, octx)?;+ }++ decoder.send_eof()?;+ loop {+ match decoder.receive_frame(&mut frame) {+ Ok(()) if !frame.is_corrupt() => decoded = true,+ Ok(()) => {+ return Err(RemuxError::VideoValidation("Corrupt decoded video frame"));+ }+ Err(ffmpeg::Error::Eof) => break,+ Err(error) => return Err(error.into()),+ }+ }+ if !decoded {+ return Err(RemuxError::VideoValidation(+ "Video contains no decoded frames",+ ));+ }++ octx.write_trailer()?;+ Ok(())+}+fn concatenate_with_concat_demuxer(concat_list_path: &Path,output: &Path,@@ -339,7 +444,7 @@ pub fn probe_video_seek_points(path: &Path, sample_count: usize) -> Result<(), S}fn probe_video_can_decode_inner(path: &Path) -> Result<bool, String> {- let input = avformat::input(path).map_err(|e| format!("Failed to open file: {e}"))?;+ let mut input = avformat::input(path).map_err(|e| format!("Failed to open file: {e}"))?;let input_stream = input.streams()@@ -356,8 +461,6 @@ fn probe_video_can_decode_inner(path: &Path) -> Result<bool, String> {let stream_index = input_stream.index();- let mut input = avformat::input(path).map_err(|e| format!("Failed to reopen file: {e}"))?;-let mut frame = ffmpeg::frame::Video::empty();let mut packets_tried = 0;const MAX_PACKETS: usize = 100;@@ -755,6 +858,77 @@ pub fn concatenate_m4s_segments_with_init(result}+pub fn concatenate_m4s_segments_with_init_validated(+ init_path: &Path,+ segments: &[PathBuf],+ output: &Path,+) -> Result<(), RemuxError> {+ concatenate_m4s_segments_with_init_validated_with_reader(+ init_path,+ segments,+ output,+ ffmpeg::Packet::read,+ )+}++fn concatenate_m4s_segments_with_init_validated_with_reader(+ init_path: &Path,+ segments: &[PathBuf],+ output: &Path,+ read_packet: impl FnMut(+ &mut ffmpeg::Packet,+ &mut avformat::context::Input,+ ) -> Result<(), ffmpeg::Error>,+) -> Result<(), RemuxError> {+ if segments.is_empty() {+ return Err(RemuxError::NoFragments);+ }+ for path in std::iter::once(init_path).chain(segments.iter().map(PathBuf::as_path)) {+ if !path.exists() {+ return Err(RemuxError::FragmentNotFound(path.to_path_buf()));+ }+ }++ let sequence = VALIDATED_AGGREGATE_SEQUENCE.fetch_add(1, Ordering::Relaxed);+ let combined_path = output.with_extension(format!(+ "validated-combined-{}-{sequence}.mp4",+ std::process::id()+ ));+ let mut options = std::fs::OpenOptions::new();+ options.write(true).create_new(true);+ #[cfg(unix)]+ {+ use std::os::unix::fs::OpenOptionsExt;+ options.mode(0o600);+ }+ let mut combined_file = options.open(&combined_path)?;+ let result = (|| {+ for path in std::iter::once(init_path).chain(segments.iter().map(PathBuf::as_path)) {+ let mut source = std::fs::File::open(path)?;+ std::io::copy(&mut source, &mut combined_file)?;+ }+ drop(combined_file);++ let mut ictx = avformat::input(&combined_path)?;+ let mut octx = avformat::output(output)?;+ remux_streams_validated_with_reader(&mut ictx, &mut octx, read_packet)+ })();+ let cleanup = std::fs::remove_file(&combined_path);+ match result {+ Ok(()) => cleanup.map_err(Into::into),+ Err(error) => {+ if let Err(cleanup_error) = cleanup {+ tracing::warn!(+ "failed to remove validated combined file {}: {}",+ combined_path.display(),+ cleanup_error+ );+ }+ Err(error)+ }+ }+}+fn remux_to_regular_mp4(input_path: &Path, output_path: &Path) -> Result<(), RemuxError> {let mut ictx = avformat::input(input_path)?;let mut octx = avformat::output(output_path)?;@@ -976,9 +1150,242 @@ fn merge_video_audio_inner(mod tests {use super::{build_seek_probe_positions, concat_fragment_entry, concatenate_audio_to_ogg,- concatenate_video_fragments, probe_video_pts_ladder, write_concat_list,+ concatenate_m4s_segments_with_init, concatenate_m4s_segments_with_init_validated,+ concatenate_m4s_segments_with_init_validated_with_reader, concatenate_video_fragments,+ probe_video_pts_ladder, write_concat_list,+ };+ use std::{+ path::{Path, PathBuf},+ time::Duration,};- use std::{path::Path, time::Duration};++ fn encode_test_segments(directory: &Path) -> (PathBuf, Vec<PathBuf>) {+ use crate::segmented_stream::{SegmentedVideoEncoder, SegmentedVideoEncoderConfig};++ ffmpeg::init().unwrap();+ let mut encoder = SegmentedVideoEncoder::init(+ directory.to_path_buf(),+ cap_media_info::VideoInfo {+ pixel_format: cap_media_info::Pixel::NV12,+ width: 320,+ height: 240,+ time_base: ffmpeg::Rational(1, 1_000_000),+ frame_rate: ffmpeg::Rational(30, 1),+ },+ SegmentedVideoEncoderConfig::default(),+ )+ .unwrap();+ for index in 0..320 {+ 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);+ encoder+ .queue_frame(frame, Duration::from_micros(index * 1_000_000 / 30))+ .unwrap();+ }+ encoder.finish().unwrap();+ let segments: Vec<_> = encoder+ .completed_segments()+ .iter()+ .map(|segment| segment.path.clone())+ .collect();+ assert!(segments.len() >= 2);+ (encoder.init_segment_path(), segments)+ }++ #[derive(Debug, PartialEq)]+ struct TestVideoPacket {+ pts: Option<i64>,+ dts: Option<i64>,+ duration: i64,+ data: Vec<u8>,+ }++ fn video_packet_contents(path: &Path) -> (ffmpeg::Rational, Vec<TestVideoPacket>) {+ let mut input = ffmpeg::format::input(path).unwrap();+ let stream = input.streams().best(ffmpeg::media::Type::Video).unwrap();+ let index = stream.index();+ let time_base = stream.time_base();+ let mut packets = Vec::new();+ loop {+ let mut packet = ffmpeg::Packet::empty();+ match packet.read(&mut input) {+ Ok(()) => {}+ Err(ffmpeg::Error::Eof) => break,+ Err(error) => panic!("Failed to read video packet: {error}"),+ }+ if packet.stream() == index {+ packets.push(TestVideoPacket {+ pts: packet.pts(),+ dts: packet.dts(),+ duration: packet.duration(),+ data: packet.data().unwrap().to_vec(),+ });+ }+ }+ (time_base, packets)+ }++ fn decoded_video_frame_count(path: &Path) -> usize {+ let mut input = ffmpeg::format::input(path).unwrap();+ let stream = input.streams().best(ffmpeg::media::Type::Video).unwrap();+ let index = stream.index();+ let mut decoder = ffmpeg::codec::Context::from_parameters(stream.parameters())+ .unwrap()+ .decoder()+ .video()+ .unwrap();+ let mut frame = ffmpeg::frame::Video::empty();+ let mut count = 0;+ for (stream, packet) in input.packets() {+ if stream.index() != index {+ continue;+ }+ decoder.send_packet(&packet).unwrap();+ loop {+ match decoder.receive_frame(&mut frame) {+ Ok(()) => {+ assert!(!frame.is_corrupt());+ count += 1;+ }+ Err(ffmpeg::Error::Other { errno }) if errno == ffmpeg::ffi::EAGAIN => {+ break;+ }+ Err(error) => panic!("Failed to decode video frame: {error}"),+ }+ }+ }+ decoder.send_eof().unwrap();+ loop {+ match decoder.receive_frame(&mut frame) {+ Ok(()) => {+ assert!(!frame.is_corrupt());+ count += 1;+ }+ Err(ffmpeg::Error::Eof) => break,+ Err(error) => panic!("Failed to drain video decoder: {error}"),+ }+ }+ count+ }++ fn assert_no_validated_aggregate(directory: &Path) {+ for entry in std::fs::read_dir(directory).unwrap() {+ assert!(+ !entry+ .unwrap()+ .file_name()+ .to_string_lossy()+ .contains(".validated-combined-")+ );+ }+ }++ #[test]+ fn validated_m4s_concat_preserves_packets_timestamps_and_all_frames() {+ let directory = tempfile::tempdir().unwrap();+ let (init, segments) = encode_test_segments(&directory.path().join("source"));+ let output = directory.path().join("validated.mp4");+ let legacy = directory.path().join("legacy.mp4");+ concatenate_m4s_segments_with_init(&init, &segments, &legacy).unwrap();+ concatenate_m4s_segments_with_init_validated(&init, &segments, &output).unwrap();++ assert_eq!(+ video_packet_contents(&output),+ video_packet_contents(&legacy)+ );+ assert_eq!(decoded_video_frame_count(&output), 320);+ assert!(super::probe_video_can_decode(&output).unwrap());+ super::probe_video_seek_points(&output, 8).unwrap();+ assert_no_validated_aggregate(directory.path());+ }++ #[test]+ fn validated_m4s_concat_rejects_undecodable_payload_and_retains_sources() {+ let directory = tempfile::tempdir().unwrap();+ let (init, segments) = encode_test_segments(&directory.path().join("source"));+ let init_bytes = std::fs::read(&init).unwrap();+ let mut damaged_segments = Vec::new();+ for segment in &segments {+ let mut bytes = std::fs::read(segment).unwrap();+ let mut position = 0;+ let mut payloads = 0;+ while position < bytes.len() {+ let size =+ u32::from_be_bytes(bytes[position..position + 4].try_into().unwrap()) as usize;+ assert!(size >= 8 && size <= bytes.len() - position);+ if &bytes[position + 4..position + 8] == b"mdat" {+ bytes[position + 8..position + size].fill(0);+ payloads += 1;+ }+ position += size;+ }+ assert!(payloads > 0);+ std::fs::write(segment, &bytes).unwrap();+ damaged_segments.push(bytes);+ }+ let output = directory.path().join("invalid.mp4");+ assert!(concatenate_m4s_segments_with_init_validated(&init, &segments, &output).is_err());+ assert_eq!(std::fs::read(&init).unwrap(), init_bytes);+ for (segment, expected) in segments.iter().zip(damaged_segments) {+ assert_eq!(std::fs::read(segment).unwrap(), expected);+ }+ assert_no_validated_aggregate(directory.path());+ }++ #[test]+ fn validated_m4s_concat_rejects_late_packet_read_errors() {+ let directory = tempfile::tempdir().unwrap();+ let (init, segments) = encode_test_segments(&directory.path().join("source"));+ let output = directory.path().join("read-error.mp4");+ let mut packets_read = 0;+ let result = concatenate_m4s_segments_with_init_validated_with_reader(+ &init,+ &segments,+ &output,+ |packet, input| match packet.read(input) {+ Ok(()) => {+ packets_read += 1;+ Ok(())+ }+ Err(ffmpeg::Error::Eof) => Err(ffmpeg::Error::InvalidData),+ Err(error) => Err(error),+ },+ );+ assert_eq!(packets_read, 320);+ assert!(matches!(+ result,+ Err(super::RemuxError::Ffmpeg(ffmpeg::Error::InvalidData))+ ));+ assert_no_validated_aggregate(directory.path());+ }++ #[test]+ fn validated_m4s_concat_rejects_corrupt_packets_after_valid_frames() {+ let directory = tempfile::tempdir().unwrap();+ let (init, segments) = encode_test_segments(&directory.path().join("source"));+ let output = directory.path().join("corrupt-packet.mp4");+ let mut packets_read = 0;+ let result = concatenate_m4s_segments_with_init_validated_with_reader(+ &init,+ &segments,+ &output,+ |packet, input| {+ packet.read(input)?;+ packets_read += 1;+ if packets_read == 160 {+ packet.set_flags(packet.flags() | ffmpeg::packet::Flags::CORRUPT);+ }+ Ok(())+ },+ );+ assert_eq!(packets_read, 160);+ assert!(matches!(+ result,+ Err(super::RemuxError::VideoValidation("Corrupt video packet"))+ ));+ assert_no_validated_aggregate(directory.path());+ }#[test]fn concat_entries_resolve_fragments_from_the_list_directory() {diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rsindex 49e6842b1..a676a11dd 100644--- a/crates/recording/src/recovery.rs+++ b/crates/recording/src/recovery.rs@@ -5,9 +5,10 @@ use std::{use cap_enc_ffmpeg::fragmented_mp4::tail_is_complete;use cap_enc_ffmpeg::remux::{- concatenate_audio_to_ogg, concatenate_m4s_segments_with_init, concatenate_video_fragments,- get_media_duration, get_video_fps, merge_video_audio, probe_media_valid,- probe_video_can_decode, probe_video_seek_points, remux_file,+ concatenate_audio_to_ogg, concatenate_m4s_segments_with_init,+ concatenate_m4s_segments_with_init_validated, concatenate_video_fragments, get_media_duration,+ get_video_fps, merge_video_audio, probe_media_valid, probe_video_can_decode,+ probe_video_seek_points, remux_file,};use cap_project::{AudioMeta, Cursors, MultipleSegment, MultipleSegments, ProjectConfiguration, RecordingMeta,@@ -1010,25 +1011,32 @@ impl RecoveryManager {video_validation: VideoValidation,) -> Result<(), RecoveryError> {for segment in &recording.recoverable_segments {- validate_recovery_video_inputs(- &segment.display_fragments,- segment.display_init_segment.as_deref(),- &recording.project_path,- video_validation,- )?;- let display_dir = recording.project_path.join(format!(- "content/segments/segment-{}/display",- segment.index- ));- for (_, init, fragments) in Self::collect_respawn_groups(&display_dir, None) {+ if video_validation == VideoValidation::Full || segment.display_init_segment.is_none() {validate_recovery_video_inputs(- &fragments,- Some(&init),+ &segment.display_fragments,+ segment.display_init_segment.as_deref(),&recording.project_path,video_validation,)?;}- if let Some(fragments) = &segment.camera_fragments {+ let display_dir = recording.project_path.join(format!(+ "content/segments/segment-{}/display",+ segment.index+ ));+ if video_validation == VideoValidation::Full || segment.display_init_segment.is_none() {+ for (_, init, fragments) in Self::collect_respawn_groups(&display_dir, None) {+ validate_recovery_video_inputs(+ &fragments,+ Some(&init),+ &recording.project_path,+ video_validation,+ )?;+ }+ }+ if let Some(fragments) = &segment.camera_fragments+ && (video_validation == VideoValidation::Full+ || segment.camera_init_segment.is_none())+ {validate_recovery_video_inputs(fragments,segment.camera_init_segment.as_deref(),@@ -1168,7 +1176,7 @@ impl RecoveryManager {finalization_info!("Moving single display fragment to {:?}", display_output);std::fs::rename(source, &display_output)?;}- Self::validate_required_video(&display_output, "display")?;+ Self::validate_required_video(&display_output, "display", video_validation)?;if display_dir.exists()&& let Err(e) = std::fs::remove_dir_all(&display_dir){@@ -1176,13 +1184,20 @@ impl RecoveryManager {}} else if !segment.display_fragments.is_empty() {let finalize_result = if display_dir.exists() {- Self::finalize_to_progressive_mp4(&display_dir, &display_output).map(|_| ())+ Self::finalize_to_progressive_mp4_with_validation(+ &display_dir,+ &display_output,+ None,+ video_validation,+ )+ .map(|_| ())} else {Self::finalize_fragments_to_progressive_mp4(&segment.display_fragments,segment.display_init_segment.as_deref(),&display_output,"display",+ video_validation,)};@@ -1225,13 +1240,14 @@ impl RecoveryManager {if camera_frags[0] != camera_output {std::fs::rename(&camera_frags[0], &camera_output)?;}- Self::validate_required_video(&camera_output, "camera")?;+ Self::validate_required_video(&camera_output, "camera", video_validation)?;} else {Self::finalize_fragments_to_progressive_mp4(camera_frags,segment.camera_init_segment.as_deref(),&camera_output,"camera",+ video_validation,)?;}let camera_dir = segment_dir.join("camera");@@ -1348,11 +1364,13 @@ impl RecoveryManager {let dir = recording.project_path.join(format!("content/segments/segment-{}", segment.index));- validate_recovery_track(- &dir.join("display.mp4"),- ffmpeg::media::Type::Video,- video_validation,- )?;+ if video_validation == VideoValidation::Full {+ validate_recovery_track(+ &dir.join("display.mp4"),+ ffmpeg::media::Type::Video,+ video_validation,+ )?;+ }for (fragments, name, kind) in [(segment.camera_fragments.as_ref(),@@ -1370,7 +1388,10 @@ impl RecoveryManager {ffmpeg::media::Type::Audio,),] {- if fragments.is_some() {+ if fragments.is_some()+ && (kind != ffmpeg::media::Type::Video+ || video_validation == VideoValidation::Full)+ {validate_recovery_track(&dir.join(name), kind, video_validation)?;}}@@ -1531,14 +1552,22 @@ impl RecoveryManager {}Self::rescue_pending_tmp_fragments(&display, None);let video = Self::find_complete_fragments_with_init(&display);- validate_recovery_video_inputs(- &video.fragments,- video.init_segment.as_deref(),- &workspace,- video_validation,- )?;- Self::finalize_instant_staged(&display, &audio, &final_output)?;- validate_recovery_track(&final_output, ffmpeg::media::Type::Video, video_validation)?;+ if video_validation == VideoValidation::Full || video.init_segment.is_none() {+ validate_recovery_video_inputs(+ &video.fragments,+ video.init_segment.as_deref(),+ &workspace,+ video_validation,+ )?;+ }+ Self::finalize_instant_staged(&display, &audio, &final_output, video_validation)?;+ if video_validation == VideoValidation::Full {+ validate_recovery_track(+ &final_output,+ ffmpeg::media::Type::Video,+ video_validation,+ )?;+ }if expected_audio {validate_recovered_track(&final_output, ffmpeg::media::Type::Audio)?;}@@ -1571,9 +1600,15 @@ impl RecoveryManager {display_dir: &Path,audio_dir: &Path,output: &Path,+ video_validation: VideoValidation,) -> Result<PathBuf, RecoveryError> {if !audio_dir.exists() {- return Self::finalize_to_progressive_mp4(display_dir, output);+ return Self::finalize_to_progressive_mp4_with_validation(+ display_dir,+ output,+ None,+ video_validation,+ );}Self::rescue_pending_tmp_fragments(audio_dir, None);@@ -1602,7 +1637,12 @@ impl RecoveryManager {let merged_output = parent.join(format!("{stem}.merged.mp4"));let result = (|| {- Self::finalize_to_progressive_mp4(display_dir, &video_output)?;+ Self::finalize_to_progressive_mp4_with_validation(+ display_dir,+ &video_output,+ None,+ video_validation,+ )?;Self::finalize_audio_fragments_to_progressive_mp4(&audio_info.fragments,audio_info.init_segment.as_deref(),@@ -1611,7 +1651,7 @@ impl RecoveryManager {)?;merge_video_audio(&video_output, &audio_output, &merged_output).map_err(RecoveryError::MediaMerge)?;- Self::validate_required_video(&merged_output, "display")?;+ Self::validate_required_video(&merged_output, "display", video_validation)?;replace_file(&merged_output, output)?;Ok(output.to_path_buf())})();@@ -1629,6 +1669,20 @@ impl RecoveryManager {fragmented_dir: &Path,output: &Path,health_tx: Option<&HealthSender>,+ ) -> Result<PathBuf, RecoveryError> {+ Self::finalize_to_progressive_mp4_with_validation(+ fragmented_dir,+ output,+ health_tx,+ VideoValidation::Full,+ )+ }++ fn finalize_to_progressive_mp4_with_validation(+ fragmented_dir: &Path,+ output: &Path,+ health_tx: Option<&HealthSender>,+ video_validation: VideoValidation,) -> Result<PathBuf, RecoveryError> {Self::rescue_pending_tmp_fragments(fragmented_dir, health_tx);@@ -1645,6 +1699,7 @@ impl RecoveryManager {info.init_segment.as_deref(),output,"display",+ video_validation,)?;return Ok(output.to_path_buf());}@@ -1663,6 +1718,7 @@ impl RecoveryManager {info.init_segment.as_deref(),&main_tmp,"display",+ video_validation,)?;temp_paths.push(main_tmp.clone());group_outputs.push(main_tmp);@@ -1674,6 +1730,7 @@ impl RecoveryManager {Some(init.as_path()),&group_tmp,&format!("display respawn-{n}"),+ video_validation,)?;temp_paths.push(group_tmp.clone());group_outputs.push(group_tmp);@@ -1693,7 +1750,7 @@ impl RecoveryManager {}concat_result?;- Self::validate_required_video(output, "display")?;+ Self::validate_required_video(output, "display", video_validation)?;Ok(output.to_path_buf())}@@ -1703,6 +1760,7 @@ impl RecoveryManager {init_segment: Option<&Path>,output: &Path,label: &str,+ video_validation: VideoValidation,) -> Result<(), RecoveryError> {if fragments.is_empty() {return Err(RecoveryError::NoRecoverableSegments);@@ -1714,8 +1772,15 @@ impl RecoveryManager {fragments.len(),output);- concatenate_m4s_segments_with_init(init_path, fragments, output)- .map_err(RecoveryError::VideoConcat)?;+ match video_validation {+ VideoValidation::Bounded => {+ concatenate_m4s_segments_with_init_validated(init_path, fragments, output)+ }+ VideoValidation::Full => {+ concatenate_m4s_segments_with_init(init_path, fragments, output)+ }+ }+ .map_err(RecoveryError::VideoConcat)?;} else {finalization_info!("Concatenating {} {label} fragments to {:?}",@@ -1725,7 +1790,7 @@ impl RecoveryManager {concatenate_video_fragments(fragments, output).map_err(RecoveryError::VideoConcat)?;}- Self::validate_required_video(output, label)?;+ Self::validate_required_video(output, label, video_validation)?;Ok(())}@@ -1759,10 +1824,16 @@ impl RecoveryManager {Ok(())}- fn validate_required_video(path: &Path, label: &str) -> Result<(), RecoveryError> {+ fn validate_required_video(+ path: &Path,+ label: &str,+ video_validation: VideoValidation,+ ) -> Result<(), RecoveryError> {finalization_info!("Validating finalized {} video: {:?}", label, path);- Self::ensure_video_decodes(path, label)?;+ if video_validation == VideoValidation::Full {+ Self::ensure_video_decodes(path, label)?;+ }if let Err(seek_error) = probe_video_seek_points(path, EXPORT_SEEK_PROBE_SAMPLE_COUNT) {finalization_info!(@@ -1772,6 +1843,9 @@ impl RecoveryManager {);Self::normalize_recovered_video(path, label)?;}+ if video_validation == VideoValidation::Bounded {+ validate_recovery_track(path, ffmpeg::media::Type::Video, video_validation)?;+ }Ok(())}diff --git a/crates/recording/tests/recovery.rs b/crates/recording/tests/recovery.rsindex 500947f9f..9472d1838 100644--- a/crates/recording/tests/recovery.rs+++ b/crates/recording/tests/recovery.rs@@ -1447,6 +1447,111 @@ fn finalize_to_progressive_mp4_includes_respawn_fragments() {);}+#[test]+fn finalize_needs_remux_progressive_display_rejects_invalid_respawn_without_mutation() {+ test_utils::init_tracing();+ let recording = TestRecording::new().unwrap();+ let display = recording.create_display_dir(0).unwrap();+ write_synthetic_fragments(&display, 180, Duration::from_secs(2));+ let progressive = recording.create_segment_dir(0).unwrap().join("display.mp4");+ RecoveryManager::finalize_to_progressive_mp4(&display, &progressive).unwrap();+ std::fs::remove_dir_all(&display).unwrap();+ let respawn = display.join("respawn-1");+ std::fs::create_dir_all(&respawn).unwrap();+ write_synthetic_fragments(&respawn, 180, Duration::from_secs(2));+ assert_valid_synthetic_fragments(&respawn);+ std::fs::write(respawn.join("init.mp4"), b"invalid respawn init").unwrap();+ recording+ .write_recording_meta(StudioRecordingStatus::NeedsRemux)+ .unwrap();++ let incomplete = RecoveryManager::inspect_recording(recording.path()).unwrap();+ let segment = &incomplete.recoverable_segments[0];+ assert_eq!(segment.display_fragments, vec![progressive]);+ assert!(segment.display_init_segment.is_none());+ let before = recovery_input_bytes(recording.path());++ assert!(RecoveryManager::finalize(&incomplete).is_err());+ assert_eq!(recovery_input_bytes(recording.path()), before);+}++#[test]+fn finalize_needs_remux_includes_validated_respawn_groups() {+ test_utils::init_tracing();+ let recording = TestRecording::new().unwrap();+ let display = recording.create_display_dir(0).unwrap();+ let respawn = display.join("respawn-1");+ std::fs::create_dir_all(&respawn).unwrap();+ for directory in [&display, &respawn] {+ let mut encoder = SegmentedVideoEncoder::init(+ directory.to_path_buf(),+ synthetic_video_info(),+ SegmentedVideoEncoderConfig {+ segment_duration: Duration::from_secs(2),+ ..Default::default()+ },+ )+ .unwrap();+ for index in 0..180 {+ encoder+ .queue_frame(+ make_synthetic_video_frame(320, 240),+ Duration::from_nanos(index * 1_000_000_000 / 30),+ )+ .unwrap();+ }+ encoder.finish().unwrap();+ }+ assert_valid_synthetic_fragments(&display);+ assert_valid_synthetic_fragments(&respawn);+ let control = TempDir::new().unwrap();+ let control_display = control.path().join("display");+ copy_dir_recursive(&display, &control_display).unwrap();+ let legacy_output = control.path().join("legacy.mp4");+ RecoveryManager::finalize_to_progressive_mp4(&control_display, &legacy_output).unwrap();+ let legacy_duration = cap_enc_ffmpeg::remux::get_media_duration(&legacy_output).unwrap();+ recording+ .write_recording_meta(StudioRecordingStatus::NeedsRemux)+ .unwrap();+ let incomplete = RecoveryManager::inspect_recording(recording.path()).unwrap();+ assert!(+ incomplete.recoverable_segments[0]+ .display_init_segment+ .is_some()+ );+ let before = recovery_input_bytes(recording.path());++ let recovered = RecoveryManager::finalize(&incomplete).unwrap();+ assert!(matches!(+ recovered.meta.status(),+ StudioRecordingStatus::Complete+ ));+ let output = recording+ .path()+ .join("content/segments/segment-0/display.mp4");+ assert!(probe_video_can_decode(&output).unwrap());+ probe_video_seek_points(&output, 8).unwrap();+ let duration = cap_enc_ffmpeg::remux::get_media_duration(&output).unwrap();+ assert!(+ (11.5..=12.5).contains(&duration.as_secs_f64()),+ "validated duration {duration:?}, legacy duration {legacy_duration:?}"+ );+ assert_eq!(duration, legacy_duration);+ let backup = std::fs::read_dir(recording.path())+ .unwrap()+ .map(|entry| entry.unwrap().path())+ .find(|path| path.join("original-segments").is_dir())+ .unwrap();+ for (path, bytes) in &before {+ if let Ok(relative) = path.strip_prefix("content/segments") {+ assert_eq!(+ &std::fs::read(backup.join("original-segments").join(relative)).unwrap(),+ bytes+ );+ }+ }+}+#[test]fn finalize_to_progressive_mp4_rescues_pending_tmp_fragments_in_respawn_dir() {test_utils::init_tracing();