forge
Explore
Log in

Don't you dare go Hollow

Cancel an mp4 import mid-moov, same as the mkv arm

Browse files at this commit

8a96a5a9416618de58a71ba056ec005f018a22aeAhmet Kaan GümüşParent: 9dc7454
  • fix
    • cancel an mp4 import mid-moov, same as the mkv arm
Show full message
🐛 fix(engine): cancel an mp4 import mid-moov, same as the mkv arm

Mp4Reader::read_header ran the whole moov parse uncancellable while
the Matroska arm's walk polled the flag per element -- a cancelled
import of a big mp4 blocked until the header finished (debts #57/#58).
Wraps the BufReader<File> handed to Mp4Reader in a CancelableReader
that checks demux::cancelled() on every read(), surfacing a "read
cancelled" io::Error the mp4 crate propagates as-is, so is_cancelled
classifies it the same way as the mkv path. No fork of the mp4 crate.
crates/engine/src/demux.rsmodified+46 -2
@@ -77,6 +77,31 @@ pub fn is_cancelled(error: &crate::Error) -> bool {
7777 error.to_string() == CANCELLED
7878 }
7979
80+/// The mp4 arm's half of [`cancelled`]: the Matroska walk polls the flag itself
81+/// between elements, but `Mp4Reader::read_header` is the `mp4` crate's own loop
82+/// over the `moov`, called once and not returned to until the whole header is
83+/// parsed. This is the same poll wearing a `Read`/`Seek` face instead, so a
84+/// cancel mid-`moov` surfaces exactly where the crate next asks this file for
85+/// bytes -- as an [`io::Error`](std::io::Error) that `Mp4Reader::read_header`
86+/// has no choice but to propagate, in [`CANCELLED`]'s own words so
87+/// [`is_cancelled`] tells it apart from the file the same way either arm does.
88+struct CancelableReader<R>(R);
89+
90+impl<R: Read> Read for CancelableReader<R> {
91+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
92+ if cancelled() {
93+ return Err(std::io::Error::other(CANCELLED));
94+ }
95+ self.0.read(buf)
96+ }
97+}
98+
99+impl<R: Seek> Seek for CancelableReader<R> {
100+ fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
101+ self.0.seek(pos)
102+ }
103+}
104+
80105 /// What the video track is coded with. Not a decoder choice by itself: only
81106 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]
82107 /// says for the others.
@@ -602,7 +627,7 @@ pub fn is_matroska(path: &Path) -> bool {
602627 }
603628
604629 pub struct Mp4Demuxer {
605- reader: Mp4Reader<BufReader<File>>,
630+ reader: Mp4Reader<CancelableReader<BufReader<File>>>,
606631 track_id: u32,
607632 sample_count: u32,
608633 /// 1-based sample id of *frame 0*: the edit list can start the presentation
@@ -639,7 +664,7 @@ impl Mp4Demuxer {
639664 fn open(path: &Path) -> crate::Result<(VideoMeta, Self)> {
640665 let file = File::open(path)?;
641666 let size = file.metadata()?.len();
642- let reader = Mp4Reader::read_header(BufReader::new(file), size)?;
667+ let reader = Mp4Reader::read_header(CancelableReader(BufReader::new(file)), size)?;
643668
644669 // In *file* order, out of `moov.traks` rather than `Mp4Reader::tracks`,
645670 // which is a `HashMap`: iterating that would make "the video track" a
@@ -4203,6 +4228,25 @@ mod tests {
42034228 );
42044229 }
42054230
4231+ /// **A cancelled mp4 open stops reading the header too.** The `mp4` crate
4232+ /// has no cancel of its own -- [`CancelableReader`] is what stands in --
4233+ /// so this pre-sets the flag before [`Mp4Demuxer::open`] ever calls
4234+ /// `Mp4Reader::read_header` and asserts the open comes back [`is_cancelled`]
4235+ /// rather than a parsed header. On the reader that only wraps `BufReader`
4236+ /// (pre-[`CancelableReader`]) the flag is never polled and this test fails
4237+ /// with `Ok(_)`.
4238+ #[test]
4239+ fn a_cancelled_mp4_open_gives_up_and_says_so() {
4240+ let path = asset("test_av.mp4");
4241+ let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
4242+ let opened = with_cancel(&flag, || Mp4Demuxer::open(&path));
4243+ let error = opened.err().expect("a cancelled open must not parse a header");
4244+ assert!(
4245+ is_cancelled(&error),
4246+ "a cancelled mp4 open reported {error} -- a verdict about the file"
4247+ );
4248+ }
4249+
42064250 /// **The lazy index is the same index.** For every Matroska fixture, the
42074251 /// window built off the file's `Cues` and the whole-segment walk it replaced
42084252 /// agree on: the block list, the frame count, and where every one of the