Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/bin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ and leaves it fragmented, minus that fragment. Unlike `fmp4`, a rendition that
joins mid-recording is kept, though only the finished file indexes it.
`moq repair mp4 <file>` finishes a recording that a crash or second signal left
fragmented, in place and the same way: it indexes every fragment written in full,
late renditions included, and cuts a partial one at the end.
late renditions included, and cuts a partial one at the end. Finishing syncs to
disk in an order that keeps the file playable if it is itself cut short, and
`repair` also finishes such a file. It leaves a file alone only when its index is
exactly what its fragments hold.

The recorder holds an exclusive file lock on its output while it records, and the
OS drops the lock with a killed recorder. `repair` refuses a file it can't lock,
Expand Down
11 changes: 0 additions & 11 deletions quest/m1/precut-fixes/mp4-finish-repair.md

This file was deleted.

26 changes: 14 additions & 12 deletions rs/moq-cli/src/repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ struct Mp4 {
}

impl Mp4 {
/// Index the recording and write its `moov`, or report that it is already finished.
/// Index the recording and write its `moov`, or report that it is already finished with
/// exactly that `moov`.
async fn run(self) -> anyhow::Result<()> {
let path = self.file;
let file = std::fs::OpenOptions::new()
Expand All @@ -55,20 +56,21 @@ impl Mp4 {
path.display()
)
})?;
let (file, finish) = tokio::task::spawn_blocking(move || {
let repaired = tokio::task::spawn_blocking(move || {
let mut file = file;
let finish = moq_mux::container::mp4::repair(&mut file);
(file, finish)
let Some(finish) = moq_mux::container::mp4::repair(&mut file)? else {
return anyhow::Ok(false);
};
finish.write(&mut file)?;
Ok(true)
})
.await?;
let finish = finish.with_context(|| format!("failed to repair {}", path.display()))?;
.await?
.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"),
if repaired {
tracing::info!(path = %path.display(), "recording repaired");
} else {
tracing::info!(path = %path.display(), "recording already finished");
}
Ok(())
}
Expand Down
22 changes: 7 additions & 15 deletions rs/moq-cli/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hang::catalog::{AudioCodecKind, VideoCodecKind};
use moq_mux::catalog::{self, CatalogFormat, Stream};
use moq_mux::select;
use moq_net::track::Delay;
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
use tokio::io::AsyncWriteExt;
use tokio_util::sync::CancellationToken;

/// Container format written on the export (sink) side.
Expand Down Expand Up @@ -425,7 +425,12 @@ impl Subscribe {

match mp4.finish()? {
Some(trailer) => {
write_finish(&mut file, &trailer).await?;
// Converting drops the error of a write still in flight, which flushing returns.
file.flush().await?;
let mut file = file.into_std().await;
tokio::task::spawn_blocking(move || trailer.write(&mut file))
.await?
.with_context(|| format!("failed to finish {}", path.display()))?;
tracing::info!(path = %path.display(), "recording finished");
}
// Nothing reached the file, so there is no recording to keep.
Expand Down Expand Up @@ -623,16 +628,3 @@ impl Subscribe {
Ok(())
}
}

/// Make the writes that turn a recording into a regular MP4: the moov where the media ends,
/// then the mdat header over the reserved box.
pub async fn write_finish(file: &mut tokio::fs::File, finish: &moq_mux::container::mp4::Finish) -> std::io::Result<()> {
file.set_len(finish.moov_offset).await?;
file.seek(std::io::SeekFrom::Start(finish.moov_offset)).await?;
file.write_all(&finish.moov).await?;
// The header points readers past the media at the moov, so the moov must be on disk first.
file.sync_data().await?;
file.seek(std::io::SeekFrom::Start(finish.mdat_offset)).await?;
file.write_all(&finish.mdat).await?;
file.sync_all().await
}
Loading