Repository navigation
fix(live recorder): index each MP4 file so browsers start it at once - #1107
Conversation
A fragmented MP4 without a sidx makes FFmpeg's demuxer, and so Chrome's <video>, read every moof before it reports metadata: over HTTP, the whole file. Live Recorder files on S3 took 20-30 s to start (#1105). The fragment sink now leaves room for a sidx after the init segment, held by a free box, notes each fragment's offset and time as it writes it, and writes the sidx into that room when the file closes: at a split, a new init segment, EOS or a stop. A file that never closes plays as before. The room is sized from the split duration, or 16384 fragments without one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… safely Review findings on the MP4 index: - Room for 5 fragments a second of split duration, not 2: short GOPs, audio alone or a video stall give 1.5-2 a second, measured. - Write the sidx first and shorten the free header last, so a write that stops short leaves the file as it was rather than with a broken index. - Record a fragment in the index only after its write succeeded. - Unit tests for write_index: layout and coverage, and an index that does not fit leaving the file untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
srperens
left a comment
There was a problem hiding this comment.
Area: Live Recorder, MP4 file finalization
The PR leaves a free room after moov and writes a sidx into it when each file closes. The approach looks right: isofmp4mux cannot write a sidx (it is still a TODO upstream in gst-plugins-rs fmp4mux), nothing copies the whole file, and a crash leaves a plain free box. Offsets checked: the first-fragment guard (fragment_sink.rs:749), the 64-bit v1 fields, the 2 GB cap (766-769), and durations taken as timestamp differences so they do not drift (770).
Neighbours: #1057 conflicts in fragment_sink.rs, at the close in PausedToReady (318) and in open_next_file (337). Whichever lands second must keep finish_file at both. assert_indexed in the split and stop tests (live_recorder_test.rs:597, 797) would catch a dropped call. #1075 touches keepalive.rs only. Closes #1105.
- An unsplit file past
UNSPLIT_INDEX_ENTRIES(16,384, fragment_sink.rs:66) gets no index, and the only sign is a GStreamer warning (416-421). WithFRAGMENT_DURATIONat 1 s (mod.rs:60), audio-only or a 1 s GOP reaches that in about 4.5 h. The PR says so, but an operator cannot see it. - The new tests use statically registered
isofmp4muxandrequire_elements, so they run in CI and do not skip. Check (Linux) was still pending when I looked.
| header.extend_from_slice(b"free"); | ||
| file.seek(SeekFrom::Start(index.at + free_len as u64)) | ||
| .and_then(|_| file.write_all(&sidx)) | ||
| .and_then(|_| file.sync_data()) |
There was a problem hiding this comment.
This sync_data() is the ordering barrier between writing the index and shortening the free header. It runs on the muxer's streaming thread with the sink's state lock held, at every split (open_next_file, 337, reached from handle, which takes the lock at 605) and in the EOS handler (718-722). fdatasync flushes every dirty page of the file, not only the index. On a slow disk or a network mount the muxer can stall at each split, and on stop drain() gives EOS only 3 s (571-575). The ordering matters only for power loss: after a process crash both writes are already in the page cache. Not measured.
| String::from_utf8_lossy(kind) | ||
| ); | ||
| assert!( | ||
| be32(entry + 4) > 0, |
There was a problem hiding this comment.
The end-to-end check asserts only that each duration is > 0. Nothing compares the sidx times (earliest presentation time, summed durations, timescale) with what isofmp4mux actually wrote, and the unit tests use hand-built times. A misread tfdt, trun or trex in real muxer output would still pass. A check that the summed durations match the demuxed duration, to within one fragment, would guard it.
Closes #1105
Problem
Live Recorder's fragmented MP4 files (
ftyp+moov, thenstyp/moof/mdatfragments) have no segment index. FFmpeg's MP4 demuxer, which Chrome's<video>uses for progressive playback, then reads everymoofin the file before it reports metadata. Over HTTP that means reading the whole file. Open Live measured 31 s before a Live Recorder file started in Studio from S3, against 0.35 s for a regular Recorder file (#1105).Change
sidxright after the init segment, held by afreebox.sidx, covering every fragment to the last byte of the file, right before the first fragment. The rest of the room stays a smallerfreebox.freeheader is shortened as the last write, so a write that stops short leaves the file as it was.freebox.Rejected:
mfraat the end of the file. It is simpler to append, but FFmpeg does not read it by default; measured, it reads the whole file just the same.Evidence
sidxis read in full before metadata; with one, it is not. Measured locally with a 60 s audio+video recording made through the block, served over HTTP at 12 Mbit/s with 100 ms per request. On the 10.5 MB file:loadedmetadata: 220 ms with the index, 6.8 s with thesidxrenamed tofree.ffmpeg -f nullwithout errors.mfradoes not help: anffmpeg -movflags frag_keyframeremux withmfrastill read 16.6 MB of 16.6 MB.assert_indexedinsplit_files_each_play_on_their_own(EOS path, split files) anda_stop_without_eos_still_writes_the_last_fragment(stop path, no split duration) checks that thesidxsits betweenmoovand the firstmoof, that every entry lands on a fragment, and that the sizes add up to the file length. With index writing turned off, both fail withhas no index.more_fragments_than_room_leaves_the_file_untouched.trun/tfhd/trexduration parsing works: unit testa_fragment_ends_where_its_longest_track_ends.Tests
Ran on macOS (arm64), GStreamer from Homebrew:
cargo test --test live_recorder_test: 18 passed.cargo test --lib live_recorder: 10 passed (mp4_boxes, mkv_cluster, fragment_sink).cargo clippy --all-targetsandcargo fmt: clean.Not run: the full workspace test suite.
For the reviewer
sidxrefers to the first track inmoovand indexes whole multi-track fragments, which is the usual practice (MP4Box does the same).tfdtand uses thesidxtimes only to find a fragment when seeking; seek results measured with and without the index were the same. I kept it this way because during a video stall a fragment can carry no video at all, and then the reference track has no time for it.write_indextests.Out of scope
🤖 Generated with Claude Code