Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: recover complete Studio fragment prefixes without altering sources
Richie McIlroyaa2024b299a6
diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rsindex 710684fbe..6654bfe9a 100644--- a/crates/recording/src/recovery.rs+++ b/crates/recording/src/recovery.rs@@ -5,7 +5,7 @@ use std::{};use cap_enc_ffmpeg::RelocatableSource;-use cap_enc_ffmpeg::fragmented_mp4::tail_is_complete;+use cap_enc_ffmpeg::fragmented_mp4::{read_fragment_prefix, tail_is_complete};use cap_enc_ffmpeg::remux::{concatenate_audio_to_ogg, concatenate_m4s_segments_with_init,concatenate_m4s_segments_with_init_validated, concatenate_video_fragments, get_media_duration,@@ -381,7 +381,7 @@ impl RecoveryManager {.unwrap_or(0);let display_dir = segment_path.join("display");- let display_info = Self::find_complete_fragments_with_init(&display_dir);+ let display_info = Self::find_studio_fragments_with_init(&display_dir);let mut display_fragments = display_info.fragments;let mut display_init_segment = display_info.init_segment;@@ -403,7 +403,7 @@ impl RecoveryManager {let camera_dir = segment_path.join("camera");let (camera_fragments, camera_init_segment) = {- let camera_info = Self::find_complete_fragments_with_init(&camera_dir);+ let camera_info = Self::find_studio_fragments_with_init(&camera_dir);if camera_info.fragments.is_empty() {(Self::probe_single_file(&segment_path.join("camera.mp4")).map(|p| vec![p]),@@ -456,6 +456,46 @@ impl RecoveryManager {})}+ fn find_studio_fragments_with_init(dir: &Path) -> FragmentsInfo {+ let mut info = Self::find_complete_fragments_with_init(dir);+ let Some(init) = info.init_segment.clone().or_else(|| {+ let path = dir.join("init.mp4");+ path.is_file().then_some(path)+ }) else {+ return info;+ };+ if let Ok(entries) = std::fs::read_dir(dir) {+ for entry in entries.flatten() {+ let path = entry.path();+ let Some(name) = path.file_name().and_then(|name| name.to_str()) else {+ continue;+ };+ let Some(final_name) = name.strip_suffix(".tmp") else {+ continue;+ };+ if Self::m4s_fragment_index(&dir.join(final_name)).is_none()+ || dir.join(final_name).exists()+ || !entry.file_type().is_ok_and(|kind| kind.is_file())+ {+ continue;+ }+ if read_fragment_prefix(&init, &path).is_ok() {+ info.fragments.push(path);+ }+ }+ }+ info.fragments.sort_by_key(|path| {+ let path = if path.extension().is_some_and(|extension| extension == "tmp") {+ path.with_extension("")+ } else {+ path.clone()+ };+ Self::m4s_fragment_index(&path)+ });+ info.init_segment = Some(init);+ info+ }+fn find_complete_fragments(dir: &Path) -> Vec<PathBuf> {Self::find_complete_fragments_with_init(dir).fragments}@@ -638,6 +678,7 @@ impl RecoveryManager {fn collect_respawn_groups(dir: &Path,health_tx: Option<&HealthSender>,+ recover_complete_prefixes: bool,) -> Vec<(u32, PathBuf, Vec<PathBuf>)> {let Ok(entries) = std::fs::read_dir(dir) else {return Vec::new();@@ -667,7 +708,7 @@ impl RecoveryManager {continue;}- Self::rescue_pending_tmp_fragments(&respawn_dir, health_tx);+ Self::rescue_pending_tmp_fragments(&respawn_dir, health_tx, recover_complete_prefixes);let Ok(dir_entries) = std::fs::read_dir(&respawn_dir) else {debug!(@@ -720,7 +761,11 @@ impl RecoveryManager {groups}- fn rescue_pending_tmp_fragments(dir: &Path, health_tx: Option<&HealthSender>) {+ fn rescue_pending_tmp_fragments(+ dir: &Path,+ health_tx: Option<&HealthSender>,+ recover_complete_prefixes: bool,+ ) {const MIN_VALID_TMP_SIZE: u64 = 100;let Ok(entries) = std::fs::read_dir(dir) else {@@ -742,7 +787,11 @@ impl RecoveryManager {continue;}let corrupt_marker = dir.join(format!("{name}.corrupt"));- if corrupt_marker.exists() {+ if corrupt_marker.exists()+ && (!recover_complete_prefixes+ || std::fs::read_to_string(&corrupt_marker).ok().as_deref()+ != Some("truncated_fragment"))+ {continue;}let final_name = name.trim_end_matches(".tmp");@@ -750,6 +799,33 @@ impl RecoveryManager {if final_path.exists() {continue;}+ if recover_complete_prefixes && dir.join("init.mp4").is_file() {+ let result =+ read_fragment_prefix(&dir.join("init.mp4"), &path).and_then(|prefix| {+ Self::copy_recovered_prefix(&path, &final_path, prefix.length)+ });+ match result {+ Ok(()) => {+ finalization_info!(+ "Recovered complete fragment prefix from {}",+ path.display()+ );+ }+ Err(error) => {+ warn!(path = %path.display(), %error, "Cannot recover fragment prefix");+ if let Some(health_tx) = health_tx {+ emit_health(+ health_tx,+ PipelineHealthEvent::RecoveryFragmentCorrupt {+ path: path.display().to_string(),+ reason: error.to_string(),+ },+ );+ }+ }+ }+ continue;+ }match tail_is_complete(&path) {Ok(true) => {}Ok(false) => {@@ -803,6 +879,39 @@ impl RecoveryManager {}}+ fn copy_recovered_prefix(+ source: &Path,+ destination: &Path,+ length: u64,+ ) -> std::io::Result<()> {+ use std::io::Read;++ let nonce = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map_err(std::io::Error::other)?+ .as_nanos();+ let temporary =+ destination.with_extension(format!("recovery-{}-{nonce}", std::process::id()));+ let result = (|| {+ let source = std::fs::File::open(source)?;+ let mut output = std::fs::OpenOptions::new()+ .create_new(true)+ .write(true)+ .open(&temporary)?;+ if std::io::copy(&mut source.take(length), &mut output)? != length {+ return Err(std::io::Error::new(+ std::io::ErrorKind::UnexpectedEof,+ "Fragment changed during recovery",+ ));+ }+ output.sync_all()?;+ drop(output);+ std::fs::hard_link(&temporary, destination)+ })();+ let _ = std::fs::remove_file(&temporary);+ result+ }+fn is_video_file(path: &Path) -> bool {path.extension().map(|e| e.eq_ignore_ascii_case("mp4") || e.eq_ignore_ascii_case("m4s"))@@ -940,7 +1049,16 @@ impl RecoveryManager {let mut total = Duration::ZERO;for fragment in fragments {- if let Some(duration) = get_media_duration(fragment) {+ let prefix_duration = fragment+ .extension()+ .is_some_and(|extension| extension == "tmp")+ .then(|| {+ read_fragment_prefix(&fragment.with_file_name("init.mp4"), fragment)+ .ok()+ .map(|prefix| prefix.duration)+ })+ .flatten();+ if let Some(duration) = prefix_duration.or_else(|| get_media_duration(fragment)) {total += duration;}}@@ -1121,9 +1239,30 @@ impl RecoveryManager {}}validate_recovery_manifests(&staged.join("content"))?;+ let segments = staged.join("content/segments");+ for entry in std::fs::read_dir(segments)? {+ let entry = entry?;+ if entry.file_type()?.is_dir() {+ for track in ["display", "camera"] {+ let directory = entry.path().join(track);+ Self::rescue_pending_tmp_fragments(&directory, None, true);+ }+ }+ }let analyzing = Instant::now();let staged_recording = Self::analyze_incomplete(&staged, &staged_meta).ok_or(RecoveryError::NoRecoverableSegments)?;+ if staged_recording.recoverable_segments.iter().any(|segment| {+ segment+ .display_fragments+ .iter()+ .chain(segment.camera_fragments.iter().flatten())+ .any(|path| path.extension().is_some_and(|extension| extension == "tmp"))+ }) {+ return Err(RecoveryError::Validation(+ "Failed to stage a complete fragment prefix".into(),+ ));+ }debug!(elapsed_ms = analyzing.elapsed().as_secs_f64() * 1000.0,"Studio finalization staged analysis ready"@@ -1224,7 +1363,7 @@ impl RecoveryManager {segment.index));if video_validation == VideoValidation::Full || segment.display_init_segment.is_none() {- for (_, init, fragments) in Self::collect_respawn_groups(&display_dir, None) {+ for (_, init, fragments) in Self::collect_respawn_groups(&display_dir, None, true) {validate_recovery_video_inputs(&fragments,Some(&init),@@ -1379,6 +1518,7 @@ impl RecoveryManager {&display_output,None,video_validation,+ true,).map(|_| ())} else {@@ -1900,7 +2040,7 @@ impl RecoveryManager {"Missing required Instant audio".into(),));}- Self::rescue_pending_tmp_fragments(&display, None);+ Self::rescue_pending_tmp_fragments(&display, None, false);let video = Self::find_complete_fragments_with_init(&display);if video_validation == VideoValidation::Full || video.init_segment.is_none() {validate_recovery_video_inputs(@@ -1962,11 +2102,12 @@ impl RecoveryManager {output,None,video_validation,+ false,)?;return Ok(None);}- Self::rescue_pending_tmp_fragments(audio_dir, None);+ Self::rescue_pending_tmp_fragments(audio_dir, None, false);let audio_info = Self::find_complete_fragments_with_init(audio_dir);if audio_info.fragments.is_empty() {return Err(RecoveryError::Validation(@@ -2013,6 +2154,7 @@ impl RecoveryManager {&video_output,None,video_validation,+ false,);let audio = match audio {Some(handle) => handle.join().unwrap_or_else(|_| {@@ -2079,6 +2221,7 @@ impl RecoveryManager {output,health_tx,VideoValidation::Full,+ true,)}@@ -2087,15 +2230,17 @@ impl RecoveryManager {output: &Path,health_tx: Option<&HealthSender>,video_validation: VideoValidation,+ recover_complete_prefixes: bool,) -> Result<PathBuf, RecoveryError> {- Self::rescue_pending_tmp_fragments(fragmented_dir, health_tx);+ Self::rescue_pending_tmp_fragments(fragmented_dir, health_tx, recover_complete_prefixes);let info = Self::find_complete_fragments_with_init(fragmented_dir);if info.fragments.is_empty() {return Err(RecoveryError::NoRecoverableSegments);}- let respawn_groups = Self::collect_respawn_groups(fragmented_dir, health_tx);+ let respawn_groups =+ Self::collect_respawn_groups(fragmented_dir, health_tx, recover_complete_prefixes);if respawn_groups.is_empty() {Self::finalize_fragments_to_progressive_mp4(@@ -6942,3 +7087,112 @@ mod transactional_recovery_tests {#[cfg(test)]mod preparing_canonical_probe;++#[cfg(test)]+mod timed_fragment_tests {+ use super::*;++ fn boxed(kind: &[u8; 4], values: &[u32]) -> Vec<u8> {+ let mut bytes = (8 + values.len() as u32 * 4).to_be_bytes().to_vec();+ bytes.extend(kind);+ bytes.extend(values.iter().flat_map(|value| value.to_be_bytes()));+ bytes+ }++ fn container(kind: &[u8; 4], children: &[Vec<u8>]) -> Vec<u8> {+ let payload = children.concat();+ let mut bytes = (8 + payload.len() as u32).to_be_bytes().to_vec();+ bytes.extend(kind);+ bytes.extend(payload);+ bytes+ }++ fn fixture(directory: &Path) -> (PathBuf, Vec<u8>) {+ let track = container(+ b"trak",+ &[+ boxed(b"tkhd", &[0, 0, 0, 1]),+ container(b"mdia", &[boxed(b"mdhd", &[0, 0, 0, 30])]),+ ],+ );+ let defaults = container(b"mvex", &[boxed(b"trex", &[0, 1, 1, 1, 4, 0])]);+ std::fs::write(+ directory.join("init.mp4"),+ container(b"moov", &[track, defaults]),+ )+ .unwrap();+ let moof = container(+ b"moof",+ &[container(+ b"traf",+ &[+ boxed(b"tfhd", &[0x20000, 1]),+ boxed(b"tfdt", &[0, 0]),+ boxed(b"trun", &[1, 60, 76]),+ ],+ )],+ );+ assert_eq!(moof.len(), 68);+ let mut complete = moof;+ complete.extend(boxed(b"mdat", &[42; 60]));+ let path = directory.join("segment_001.m4s.tmp");+ let mut partial = complete.clone();+ partial.extend(boxed(b"moof", &[0; 10]));+ partial.extend(&boxed(b"mdat", &[0; 60])[..24]);+ std::fs::write(&path, partial).unwrap();+ (path, complete)+ }++ #[test]+ fn discovers_pending_prefix_without_mutating_source() {+ let directory = tempfile::tempdir().unwrap();+ let (source, _) = fixture(directory.path());+ let before = recovery_snapshot(directory.path()).unwrap();+ let info = RecoveryManager::find_studio_fragments_with_init(directory.path());+ assert_eq!(info.fragments, [source]);+ assert_eq!(recovery_snapshot(directory.path()).unwrap(), before);+ }++ #[test]+ fn studio_recovery_keeps_complete_prefix_and_original_bytes() {+ let directory = tempfile::tempdir().unwrap();+ let (source, complete) = fixture(directory.path());+ let original = std::fs::read(&source).unwrap();+ let marker = directory.path().join("segment_001.m4s.tmp.corrupt");+ std::fs::write(&marker, "truncated_fragment").unwrap();+ RecoveryManager::rescue_pending_tmp_fragments(directory.path(), None, true);+ let output = directory.path().join("segment_001.m4s");+ assert_eq!(std::fs::read(&output).unwrap(), complete);+ assert_eq!(std::fs::read(&source).unwrap(), original);+ RecoveryManager::rescue_pending_tmp_fragments(directory.path(), None, true);+ assert_eq!(std::fs::read(&output).unwrap(), complete);+ assert_eq!(+ RecoveryManager::find_studio_fragments_with_init(directory.path()).fragments,+ [output]+ );+ }++ #[test]+ fn instant_recovery_keeps_existing_partial_tail_policy() {+ let directory = tempfile::tempdir().unwrap();+ let (source, _) = fixture(directory.path());+ let original = std::fs::read(&source).unwrap();+ RecoveryManager::rescue_pending_tmp_fragments(directory.path(), None, false);+ assert!(!directory.path().join("segment_001.m4s").exists());+ assert_eq!(std::fs::read(source).unwrap(), original);+ }++ #[test]+ fn prefix_copy_never_overwrites_an_existing_destination() {+ let directory = tempfile::tempdir().unwrap();+ let (source, complete) = fixture(directory.path());+ let output = directory.path().join("segment_001.m4s");+ std::fs::write(&output, b"existing").unwrap();+ assert!(+ RecoveryManager::copy_recovered_prefix(&source, &output, complete.len() as u64)+ .is_err()+ );+ assert_eq!(std::fs::read(output).unwrap(), b"existing");+ assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 3);+ }+}