Repository navigation
feat(cli): moq repair finishes a cut-short mp4 recording #7560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b7352e8
e0bbcc1
5e7fbcd
7f13209
8c7d2a6
b4e5456
77f353c
449151b
2f62f52
0891557
232de06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //! `moq repair <format>`: finish a recording that its recorder left unfinished. | ||
|
|
||
| use std::path::PathBuf; | ||
|
|
||
| use anyhow::Context; | ||
|
|
||
| /// Finish a recording that a crash or second signal left unfinished. | ||
| #[derive(usage_rs::Args, Clone)] | ||
| #[usage(unknown_flags = "error", args_override_self = false)] | ||
| pub struct Args { | ||
| #[usage(subcommand)] | ||
| format: Format, | ||
| } | ||
|
|
||
| impl Args { | ||
| /// Repair the recording in the named format. | ||
| pub async fn run(self) -> anyhow::Result<()> { | ||
| match self.format { | ||
| Format::Mp4(mp4) => mp4.run().await, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The recorder that left the file, one per format that can be cut short. | ||
| #[derive(usage_rs::Subcommands, Clone)] | ||
| enum Format { | ||
| /// Finish an `export mp4` recording that a crash or second signal left fragmented. | ||
| Mp4(Mp4), | ||
| } | ||
|
|
||
| /// `moq repair mp4 <FILE>`. | ||
| #[derive(usage_rs::Args, Clone)] | ||
| #[usage(unknown_flags = "error", args_override_self = false)] | ||
| struct Mp4 { | ||
| /// The recording, finished in place as a regular MP4 that indexes every complete fragment. | ||
| #[usage(arg, value_name = "FILE", value_hint = usage_rs::ValueHint::FilePath)] | ||
| file: PathBuf, | ||
| } | ||
|
|
||
| impl Mp4 { | ||
| /// Index the recording and write its `moov`, or report that it is already finished. | ||
| async fn run(self) -> anyhow::Result<()> { | ||
| let path = self.file; | ||
| let file = std::fs::OpenOptions::new() | ||
| .read(true) | ||
| .write(true) | ||
| .open(&path) | ||
| .with_context(|| format!("failed to open {}", path.display()))?; | ||
| let (file, finish) = tokio::task::spawn_blocking(move || { | ||
| let mut file = file; | ||
| let finish = moq_mux::container::mp4::repair(&mut file); | ||
| (file, finish) | ||
| }) | ||
| .await?; | ||
| let finish = finish.with_context(|| format!("failed to repair {}", path.display()))?; | ||
|
|
||
| match finish { | ||
| Some(finish) => { | ||
| crate::subscribe::write_finish(&mut tokio::fs::File::from_std(file), &finish).await?; | ||
| tracing::info!(path = %path.display(), "recording repaired"); | ||
| } | ||
| None => tracing::info!(path = %path.display(), "recording already finished"), | ||
| } | ||
| Ok(()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1111,6 +1111,70 @@ async fn a_passthrough_export_ends_with_its_redundant_importer() { | |||||||||||||||||||||||||||
| feeding.abort(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /// `repair mp4` finishes a recording whose `export mp4` was killed as a regular MP4, and leaves | ||||||||||||||||||||||||||||
| /// it alone once finished. | ||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||
| #[ignore = "real-time process test: just rs cli-export"] | ||||||||||||||||||||||||||||
| async fn a_killed_mp4_recording_is_repaired() { | ||||||||||||||||||||||||||||
| let relay = relay().await; | ||||||||||||||||||||||||||||
| let dir = tempfile::tempdir().unwrap(); | ||||||||||||||||||||||||||||
| let path = dir.path().join("recording.mp4"); | ||||||||||||||||||||||||||||
| let mut recording = moq(&relay, &[]) | ||||||||||||||||||||||||||||
| .args(["export", "mp4", "--output"]) | ||||||||||||||||||||||||||||
| .arg(&path) | ||||||||||||||||||||||||||||
| .spawn() | ||||||||||||||||||||||||||||
| .expect("spawn export"); | ||||||||||||||||||||||||||||
| let (mut publisher, feeding) = import(&relay); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| kio::clock::timeout(TIMEOUT, async { | ||||||||||||||||||||||||||||
| while std::fs::metadata(&path).map_or(0, |file| file.len()) < 10_000 { | ||||||||||||||||||||||||||||
| kio::clock::sleep(Duration::from_millis(50)).await; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||
| .expect("the recording never grew"); | ||||||||||||||||||||||||||||
| recording.kill().await.unwrap(); | ||||||||||||||||||||||||||||
| let killed = std::fs::read(&path).unwrap(); | ||||||||||||||||||||||||||||
| assert_eq!( | ||||||||||||||||||||||||||||
| boxes(&killed)[1].kind().to_string(), | ||||||||||||||||||||||||||||
| "free", | ||||||||||||||||||||||||||||
| "the killed recording is unfinished" | ||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| let repair = || async { | ||||||||||||||||||||||||||||
| let output = tokio::process::Command::new(env!("CARGO_BIN_EXE_moq")) | ||||||||||||||||||||||||||||
| .args(["repair", "mp4"]) | ||||||||||||||||||||||||||||
| .arg(&path) | ||||||||||||||||||||||||||||
| .output(); | ||||||||||||||||||||||||||||
|
Comment on lines
+1144
to
+1148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear the developer's The Proposed fix
Committable suggestion
|
||||||||||||||||||||||||||||
| let output = kio::clock::timeout(TIMEOUT, output) | ||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||
| .expect("repair never exited") | ||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||
| assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr)); | ||||||||||||||||||||||||||||
| std::fs::read(&path).unwrap() | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| let repaired = repair().await; | ||||||||||||||||||||||||||||
| let boxes = boxes(&repaired); | ||||||||||||||||||||||||||||
| let kinds: Vec<String> = boxes.iter().map(|atom| atom.kind().to_string()).collect(); | ||||||||||||||||||||||||||||
| assert_eq!(kinds, ["ftyp", "mdat", "moov"]); | ||||||||||||||||||||||||||||
| let Some(mp4_atom::Any::Moov(moov)) = boxes.last() else { | ||||||||||||||||||||||||||||
| unreachable!(); | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| assert_eq!(moov.trak.len(), 2, "video and audio"); | ||||||||||||||||||||||||||||
| // Only the reserved box changed before the moov, which replaces any partial fragment. | ||||||||||||||||||||||||||||
| let reserved = u32::from_be_bytes(killed[..4].try_into().unwrap()) as usize; | ||||||||||||||||||||||||||||
| let media = reserved + u64::from_be_bytes(repaired[reserved + 8..reserved + 16].try_into().unwrap()) as usize; | ||||||||||||||||||||||||||||
| assert!(media <= killed.len()); | ||||||||||||||||||||||||||||
| assert_eq!(repaired[..reserved], killed[..reserved]); | ||||||||||||||||||||||||||||
| assert_eq!(repaired[reserved + 16..media], killed[reserved + 16..media]); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| assert_eq!(repair().await, repaired, "a finished recording is left alone"); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| let stdin = feeding.await.unwrap(); | ||||||||||||||||||||||||||||
| drop(stdin); | ||||||||||||||||||||||||||||
| assert!(wait(&mut publisher).await.success()); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /// The top-level boxes of an MP4 file, by kind. | ||||||||||||||||||||||||||||
| fn boxes(file: &[u8]) -> Vec<mp4_atom::Any> { | ||||||||||||||||||||||||||||
| use mp4_atom::DecodeMaybe; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
State that
repair mp4must not run on a recording that is still being written.The shipped
repair mp4takes no lock. If a user runs it whileexport mp4still writes the file,write_finishtruncates the file at the last complete fragment and overwrites the reserved box. The recorder then keeps appending fragments behind a finishedmdatandmoov. That corrupts the recording.quest/m2/mp4-repair-lock.mdtracks the lock, but until it lands the docs should give this condition. The doc guidelines require it: "State a claim's conditions with it." Add one sentence.Proposed wording
As per coding guidelines: "State a claim's conditions with it: opt-in or experimental versions, and the paths it does not cover."
Committable suggestion
Source: Coding guidelines