forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-a06a8c6f8aa697616'

Browse files at this commit

7a1c2ad78bfe8f64f4d08148cbd524f735e3a91aAhmet Kaan GümüşParent: d7c614b8a96a5a
crates/engine/src/demux.rsmodified+46 -2
@@ -77,6 +77,31 @@ pub fn is_cancelled(error: &crate::Error) -> bool {
77 error.to_string() == CANCELLED77 error.to_string() == CANCELLED
78 }78 }
79 79
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+
80 /// What the video track is coded with. Not a decoder choice by itself: only105 /// What the video track is coded with. Not a decoder choice by itself: only
81 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]106 /// H.264 has a software decoder here, which is what [`Codec::needs_plugin`]
82 /// says for the others.107 /// says for the others.
@@ -602,7 +627,7 @@ pub fn is_matroska(path: &Path) -> bool {
602 }627 }
603 628
604 pub struct Mp4Demuxer {629 pub struct Mp4Demuxer {
605- reader: Mp4Reader<BufReader<File>>,630+ reader: Mp4Reader<CancelableReader<BufReader<File>>>,
606 track_id: u32,631 track_id: u32,
607 sample_count: u32,632 sample_count: u32,
608 /// 1-based sample id of *frame 0*: the edit list can start the presentation633 /// 1-based sample id of *frame 0*: the edit list can start the presentation
@@ -639,7 +664,7 @@ impl Mp4Demuxer {
639 fn open(path: &Path) -> crate::Result<(VideoMeta, Self)> {664 fn open(path: &Path) -> crate::Result<(VideoMeta, Self)> {
640 let file = File::open(path)?;665 let file = File::open(path)?;
641 let size = file.metadata()?.len();666 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)?;
643 668
644 // In *file* order, out of `moov.traks` rather than `Mp4Reader::tracks`,669 // In *file* order, out of `moov.traks` rather than `Mp4Reader::tracks`,
645 // which is a `HashMap`: iterating that would make "the video track" a670 // which is a `HashMap`: iterating that would make "the video track" a
@@ -4203,6 +4228,25 @@ mod tests {
4203 );4228 );
4204 }4229 }
4205 4230
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+
4206 /// **The lazy index is the same index.** For every Matroska fixture, the4250 /// **The lazy index is the same index.** For every Matroska fixture, the
4207 /// window built off the file's `Cues` and the whole-segment walk it replaced4251 /// window built off the file's `Cues` and the whole-segment walk it replaced
4208 /// agree on: the block list, the frame count, and where every one of the4252 /// agree on: the block list, the frame count, and where every one of the