diff --git a/quest/m1/explicit-state/README.md b/quest/m1/explicit-state/README.md index 2850e41ae87..a58a2d69da5 100644 --- a/quest/m1/explicit-state/README.md +++ b/quest/m1/explicit-state/README.md @@ -76,7 +76,6 @@ which collapses that state and waits on [Drop set](/quest/m1/drop-set.md). - [Publish capture phases](/quest/m1/explicit-state/publish-capture.md) - dismissing the screen-share picker turns sharing off - [js/qmux close](/quest/m1/explicit-state/qmux-close.md) - a graceful remote close ends qmux streams cleanly - [FFI handle state](/quest/m1/explicit-state/ffi-state.md) - FFI handles hold one state each: video follow under one lock, requests answered once, drivers Cancelled after shutdown -- [A failed finish is final](/quest/m1/explicit-state/finish-final.md) - producers and importers are never half-finished, and refuse input after finish - [Track and group end-of-life](/quest/m1/explicit-state/track-end.md) - moq-net's track and group outcomes are one enum each, in Rust and JS - [moq-net phase data](/quest/m1/explicit-state/net-phases.md) - moq-net's session and subscription state machines keep each phase's data in its variant - [moq-mux and moq-gst lifecycles](/quest/m1/explicit-state/mux-gst.md) - export sources, TS and fMP4 export tracks, and gst pads are explicit state diff --git a/quest/m1/explicit-state/finish-final.md b/quest/m1/explicit-state/finish-final.md deleted file mode 100644 index 46daf52f24d..00000000000 --- a/quest/m1/explicit-state/finish-final.md +++ /dev/null @@ -1,51 +0,0 @@ -# [M] A failed finish is final - -## Goal - -No media producer or importer can be left half-finished: - -- A finish that fails leaves the object terminally failed and reports the - cause. It never leaves the object marked finished with its tracks unended, - and never leaves it usable with part of its pipeline torn down. -- Every container importer refuses input after finish in the same way. -- Where the type system can express it, finishing consumes the object, as - the moq-rtmp and moq-srt publishers already do. - -## Plan - -The shared decisions are in the [epic](/quest/m1/explicit-state/README.md). - -Bugs to reproduce and fix: - -- **TS import** (`rs/moq-mux/src/container/ts/import.rs`): `finish` sets - `finished = true` before the fallible `ensure!` checks, the reservation - release, and `finish_stream` for each PID. `Drop` then never retries, so an - input whose sections or private PES have no media timestamp leaves its - streams unfinished. `decode` also never checks `finished`. -- **Audio encode** (`rs/moq-audio/src/encode/producer.rs`): `end()` takes the - resampler before its fallible steps and clears `encoder` only at the end. - After a failure, `write` still passes its `encoder.is_none()` guard and - sends PCM at the input rate straight to the codec, and retrying `finish()` - republishes the terminal packets. -- **JSON window** (`rs/moq-json/src/window/producer.rs`): `finish` sets - `finished` before `track.finish()` can fail, and a retry then returns - `Ok`. After `abort`, `ensure_open` reports `Closed` instead of the cause. - The snapshot producer's design, `aborted: Option` plus openness read - from the track, is the one to match. - -Instances to sweep: - -- Importer lifecycles (`mkv/import.rs`, `fmp4/import.rs`, `ts/import.rs`): - MKV refuses `decode` after finish, but fMP4 and TS don't. MKV also overloads - `initial_reservation == None` with three meanings. Recommended: a shared - `enum Input { Open, Failed(Error), Finished }`. -- `rs/moq-mux/src/container/producer.rs`: `close()` resets the group's - fields only after its fallible steps. -- `rs/moq-transcode/src/rung.rs`: `pending_cut` is taken before the cut that - can fail. -- The moq-json `Track.group` invariant: clearing it must also reset the - encoder. - -The importer and producer `finish` signatures are public API. A move to -`finish(self)` is breaking, so if this child takes it, it leaves the epic and -becomes its own m2 quest under the epic's rule. diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index 1943c94941d..f9d51c29bec 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -76,8 +76,9 @@ impl Default for Options { /// a subscriber that opens the catalog before any frames arrive still sees the /// track. pub struct Producer { - // None after a successful finish: keep the track and catalog rendition, not the codec. - encoder: Option, + /// The codec while input is accepted. Ending keeps the track and catalog rendition, not the + /// codec. + codec: Codec, bitrate: moq_net::bandwidth::Rate, input: Input, /// Converts the input layout to the codec's, when they differ. @@ -107,6 +108,33 @@ pub struct Producer { activity: Activity, } +/// The codec while the producer accepts input, or how it ended. +enum Codec { + Live(Encoder), + /// [`finish`](Producer::finish) published every packet and ended the track. + Finished, + /// Ending failed, aborting the track: every later call reports the cause. + Failed(Error), +} + +impl Codec { + fn get(&self) -> Result<&Encoder, Error> { + match self { + Self::Live(encoder) => Ok(encoder), + Self::Finished => Err(moq_net::Error::Closed.into()), + Self::Failed(err) => Err(err.clone()), + } + } + + fn get_mut(&mut self) -> Result<&mut Encoder, Error> { + match self { + Self::Live(encoder) => Ok(encoder), + Self::Finished => Err(moq_net::Error::Closed.into()), + Self::Failed(err) => Err(err.clone()), + } + } +} + struct Terminal { packets: Vec, end: Timestamp, @@ -216,7 +244,7 @@ impl Reserved { active: true, reset_pending: false, bitrate, - encoder: Some(registered.encoder), + codec: Codec::Live(registered.encoder), input: registered.input, remix: registered.remix, resampler: registered.resampler, @@ -301,7 +329,7 @@ impl Producer { /// Retune the live encoder to `bitrate`. pub fn set_bitrate(&mut self, bitrate: moq_net::bandwidth::Rate) -> Result<(), Error> { - let encoder = self.encoder.as_mut().ok_or(moq_net::Error::Closed)?; + let encoder = self.codec.get_mut()?; let previous = encoder.bitrate(); encoder.set_bitrate(bitrate)?; // Advertise the configured target in place, so encoded-byte estimates cannot replace it. @@ -317,7 +345,7 @@ impl Producer { } fn encoder(&self) -> Result<&Encoder, Error> { - self.encoder.as_ref().ok_or(moq_net::Error::Closed.into()) + self.codec.get() } /// Re-anchor the timeline to the next frame's timestamp, dropping any @@ -328,7 +356,7 @@ impl Producer { /// `reanchor`. If the codec had started, a marker group is published before /// the next packet so subscribers jump the playhead. pub fn reanchor(&mut self) { - if self.encoder.as_ref().is_some_and(Encoder::started) || !self.backlog.is_empty() { + if self.codec.get().is_ok_and(Encoder::started) || !self.backlog.is_empty() { self.pending_discontinuity = true; } self.reset_state(); @@ -341,7 +369,7 @@ impl Producer { self.pending.clear(); self.reset_pending = true; if self.backlog.is_empty() - && let Some(encoder) = self.encoder.as_mut() + && let Ok(encoder) = self.codec.get_mut() { encoder.reset(); } @@ -370,11 +398,9 @@ impl Producer { /// produced, which [`activity`](Self::activity) reports. /// /// Writes after a successful [`finish`](Self::finish) fail with - /// [`moq_net::Error::Closed`]. + /// [`moq_net::Error::Closed`], and after a failed one with its cause. pub fn write(&mut self, frame: &Frame) -> Result<(), Error> { - if self.encoder.is_none() { - return Err(moq_net::Error::Closed.into()); - } + self.encoder()?; let timestamp_us = u64::try_from(frame.timestamp.as_micros()) .map_err(|_| Error::Unsupported(format!("frame timestamp {:?} out of range", frame.timestamp)))?; let anchor_us = *self.anchor_us.get_or_insert(timestamp_us); @@ -457,7 +483,7 @@ impl Producer { self.track.discontinuity()?; self.decoder_boundary = true; } - let encoder = self.encoder.as_mut().ok_or(moq_net::Error::Closed)?; + let encoder = self.codec.get_mut()?; if reset { encoder.reset(); } @@ -613,7 +639,8 @@ impl Producer { /// Borrows rather than consumes, so a later [`abort`](Self::abort) can still /// run after a successful finish. The codec and input scratch are released, while the /// track and its catalog rendition stay published. Writes and bitrate changes then - /// fail with [`moq_net::Error::Closed`]. + /// fail with [`moq_net::Error::Closed`]. A finish that fails ends the track with an error + /// instead, and every later call returns its cause. pub fn finish(&mut self) -> Result<(), Error> { self.end(None) } @@ -626,9 +653,29 @@ impl Producer { } fn end(&mut self, err: Option) -> Result<(), Error> { - if self.encoder.is_none() { - return Ok(()); + match &self.codec { + Codec::Live(_) => {} + Codec::Finished => return Ok(()), + Codec::Failed(cause) => return Err(cause.clone()), } + let result = self.drain(err); + self.codec = match &result { + Ok(()) => Codec::Finished, + Err(cause) => { + // Every published packet stays readable, then subscribers get an error rather than an + // end that would claim the source was published whole. + let _ = self.track.fail(moq_net::StreamError::Internal.into()); + Codec::Failed(cause.clone()) + } + }; + self.pending = Vec::new(); + self.remix = None; + self.resampler = None; + result + } + + /// Publish everything still held, then end the track with `err` or cleanly. + fn drain(&mut self, err: Option) -> Result<(), Error> { // Preserve the final retained history after capture ends, even without live demand. self.active = true; while let Some(raw) = self.backlog.pop() { @@ -636,7 +683,7 @@ impl Producer { self.encode_frame(&raw.samples, raw.timestamp, raw.reset, raw.discontinuity)?; } if self.reset_pending - && let Some(encoder) = self.encoder.as_mut() + && let Ok(encoder) = self.codec.get_mut() { encoder.reset(); } @@ -666,11 +713,7 @@ impl Producer { let start = Self::timestamp(anchor_us, self.frames_produced, delay, codec_rate)?; // The source ends where it ends: priming only moves the packets carrying it. let end = Self::timestamp(anchor_us, self.frames_produced + source_frames as u64, 0, codec_rate)?; - let finish = self - .encoder - .as_mut() - .ok_or(moq_net::Error::Closed)? - .drain(&self.pending)?; + let finish = self.codec.get_mut()?.drain(&self.pending)?; let discard_padding = finish.discard_padding(); let packets = finish.into_packets(); @@ -698,9 +741,6 @@ impl Producer { None => self.track.finish()?, Some(err) => self.track.fail(err)?, } - self.encoder = None; - self.pending = Vec::new(); - self.remix = None; Ok(()) } @@ -1387,6 +1427,40 @@ mod tests { assert_eq!(packets, 11); } + /// A failed finish is final: writes are refused with its cause rather than reaching the codec + /// at the input rate, now that the resampler is gone, and a retry reports the cause rather than + /// publishing the terminal packets again. + #[tokio::test] + async fn a_failed_finish_refuses_later_calls() { + let input = Input { + format: Format::F32, + sample_rate: 44_100, + layout: Layout::Mono, + }; + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, moq_mux::catalog::Config::default()).unwrap(); + let options = Options { + track: Some("audio".to_string()), + config: Config::new(48_000, Layout::Mono), + ..Options::default() + }; + let mut producer = Producer::new(&mut broadcast, catalog, input, &options).unwrap(); + let frame = |samples: usize| { + let data: Vec = vec![0.25f32; samples].iter().flat_map(|s| s.to_le_bytes()).collect(); + Frame::new(data.into(), moq_net::Timestamp::ZERO) + }; + producer.write(&frame(4_410)).unwrap(); + + // Another handle ends the track, so finishing cannot publish the tail. + producer.track.track().clone().abort(moq_net::Error::Cancel).unwrap(); + let cause = producer.finish().unwrap_err().to_string(); + + // Short of a codec frame, so nothing but the guard could refuse it. + let err = producer.write(&frame(100)).unwrap_err(); + assert_eq!(err.to_string(), cause); + assert_eq!(producer.finish().unwrap_err().to_string(), cause); + } + /// `reanchor` promises to drop buffered samples, and the resampler buffers /// samples of its own. Leaving those behind let `finish` flush pre-reset audio /// onto the track, stamped at an anchor that no longer exists. diff --git a/rs/moq-json/src/lib.rs b/rs/moq-json/src/lib.rs index 587a64d613e..77daab3fd41 100644 --- a/rs/moq-json/src/lib.rs +++ b/rs/moq-json/src/lib.rs @@ -26,6 +26,7 @@ mod diff; mod merge; mod refresh; pub mod snapshot; +mod state; pub mod stream; mod strict; pub mod window; diff --git a/rs/moq-json/src/snapshot/mod.rs b/rs/moq-json/src/snapshot/mod.rs index 9a6f522378a..df8d84266d4 100644 --- a/rs/moq-json/src/snapshot/mod.rs +++ b/rs/moq-json/src/snapshot/mod.rs @@ -744,6 +744,29 @@ mod test { )); } + /// Once this producer finished, it refuses edits even below an end another handle declared + /// further on, rather than opening a group nothing would finish. + #[test] + fn a_finished_producer_refuses_edits_below_a_declared_end() { + let mut track = moq_net::broadcast::Info::new() + .produce() + .create_track("test", moq_net::track::Info::default()) + .unwrap(); + let mut producer = Producer::::new(track.clone(), cfg(false)); + producer.update(&json!({ "a": 1 })).unwrap(); + track.finish_at(moq_net::track::Position::group(10), None).unwrap(); + producer.finish().unwrap(); + + assert!(matches!( + producer.update(&json!({ "a": 2 })), + Err(crate::Error::Net(moq_net::Error::Closed)) + )); + assert!(matches!( + producer.modify(None), + Err(crate::Error::Net(moq_net::Error::Closed)) + )); + } + #[test] fn modify_composes_independent_owners() { // Mirrors the catalog use case: separate owners each edit their own field through the guard. diff --git a/rs/moq-json/src/snapshot/producer.rs b/rs/moq-json/src/snapshot/producer.rs index eef6c72fbc2..346a2103ca3 100644 --- a/rs/moq-json/src/snapshot/producer.rs +++ b/rs/moq-json/src/snapshot/producer.rs @@ -81,7 +81,7 @@ impl Producer { }, encoder: Encoder::new(config), at: None, - aborted: None, + state: crate::state::State::Open, })), _marker: PhantomData, } @@ -301,17 +301,14 @@ struct Inner { /// older timestamp than its group's first frame, and a new group may not start before that one. at: Option, - /// Why this producer aborted the track, so [`Producer::modify`] can report the cause rather than - /// the closed track it left behind. - aborted: Option, + /// Whether this producer still publishes, or how it ended the track. + state: crate::state::State, } impl Inner { /// Refuse further edits once the track can't take another group. fn open(&self) -> Result<()> { - if let Some(err) = &self.aborted { - return Err(err.clone()); - } + self.state.check()?; // The same test `append_group` applies: a boundary declared ahead of the live edge still // admits the groups below it. The largest counts every group, even an empty one, so the @@ -340,7 +337,7 @@ impl Inner { self.track.group = None; // Idempotent: a track that is already closed keeps its first reason. let _ = self.track.inner.clone().abort(reason); - self.aborted = Some(err); + self.state.abort(err); } /// Finish the open group, leaving the next update to open a replacement with a full snapshot. @@ -358,6 +355,7 @@ impl Inner { impl Inner { fn update(&mut self, payload: Timed<&T>) -> Result> { + self.open()?; let timestamp = self.track.rescale(payload.at)?; // Split the borrow so `frame` can hold the encoder while `track` is written through. let Inner { track, encoder, .. } = self; @@ -394,11 +392,22 @@ impl Inner { } fn finish(&mut self) -> Result<()> { + match &self.state { + crate::state::State::Open => {} + crate::state::State::Finished => return Ok(()), + crate::state::State::Aborted(err) => return Err(err.clone()), + } // The open group goes with the track, so the encoder must not keep emitting deltas into it. // Any further update fails on the closed track, but it has to fail as an error rather than by // writing a delta with no group to hold it. self.encoder.reset(); - self.track.finish() + if let Err(err) = self.track.finish() { + // Half-finished, the track could still take groups, so it ends with the cause instead. + self.abort(err.clone()); + return Err(err); + } + self.state = crate::state::State::Finished; + Ok(()) } } @@ -493,7 +502,10 @@ impl Track { if let Some(mut group) = self.group.take() { group.finish()?; } - self.inner.finish()?; + // Another handle may have declared the end already, which stands. + if self.inner.finished().is_none() { + self.inner.finish()?; + } Ok(()) } } diff --git a/rs/moq-json/src/state.rs b/rs/moq-json/src/state.rs new file mode 100644 index 00000000000..7752ca87c51 --- /dev/null +++ b/rs/moq-json/src/state.rs @@ -0,0 +1,30 @@ +/// Whether a producer still publishes, or how it ended its track. +/// +/// Another handle may end the track first, so the track alone can't say what this producer did: +/// its later calls report its own outcome. +#[derive(Default)] +pub(crate) enum State { + #[default] + Open, + Finished, + /// The first cause wins. + Aborted(crate::Error), +} + +impl State { + /// Refuse the call once this producer ended the track. + pub(crate) fn check(&self) -> crate::Result<()> { + match self { + Self::Open => Ok(()), + Self::Finished => Err(moq_net::Error::Closed.into()), + Self::Aborted(err) => Err(err.clone()), + } + } + + /// Record an abort, unless an earlier one already gave the cause. + pub(crate) fn abort(&mut self, err: crate::Error) { + if !matches!(self, Self::Aborted(_)) { + *self = Self::Aborted(err); + } + } +} diff --git a/rs/moq-json/src/window/mod.rs b/rs/moq-json/src/window/mod.rs index 4aed9546cfe..27b572ac235 100644 --- a/rs/moq-json/src/window/mod.rs +++ b/rs/moq-json/src/window/mod.rs @@ -873,6 +873,70 @@ mod test { )); } + /// A finish that fails is final: the track aborts, and a retry reports the failure rather than + /// claiming the track finished. + #[test] + fn a_failed_finish_is_final() { + let track = moq_net::broadcast::Info::new() + .produce() + .create_track("test", moq_net::track::Info::default()) + .unwrap(); + let mut producer = Producer::::new(track.clone(), ProducerConfig::default()); + producer.push(&rec(1)).unwrap(); + // Another handle ends the track, so finishing it fails. + track.abort(moq_net::Error::Cancel).unwrap(); + + let err = producer.finish().unwrap_err().to_string(); + assert_eq!(producer.finish().unwrap_err().to_string(), err); + assert_eq!(producer.push(&rec(2)).unwrap_err().to_string(), err); + } + + /// A track another handle already ended still has its open group closed by this producer's + /// finish, which then refuses edits even though the end leaves room below it. + #[test] + fn finish_closes_the_group_on_a_track_already_ended() { + let track = moq_net::broadcast::Info::new() + .produce() + .create_track("test", moq_net::track::Info::default()) + .unwrap(); + let mut subscriber = track.subscribe(None).ordered(); + let mut producer = Producer::::new(track.clone(), ProducerConfig::default()); + producer.push(&rec(1)).unwrap(); + track.finish().unwrap(); + producer.finish().unwrap(); + + let waiter = kio::Waiter::noop(); + let Poll::Ready(Ok(Some(mut group))) = subscriber.poll_next_group(&waiter) else { + panic!("expected the window group"); + }; + while let Poll::Ready(Ok(Some(_))) = group.poll_read_frame(&waiter) {} + assert!( + matches!(group.poll_read_frame(&waiter), Poll::Ready(Ok(None))), + "the group ends rather than waiting for a producer that finished" + ); + assert!(matches!( + producer.push(&rec(2)), + Err(crate::Error::Net(moq_net::Error::Closed)) + )); + } + + /// Clones of an aborted producer report the abort's cause, not just a closed track. + #[test] + fn edits_after_an_abort_report_its_cause() { + let (mut producer, _track) = producer(ProducerConfig::default()); + producer.push(&rec(1)).unwrap(); + producer.clone().abort(moq_net::Error::Cancel).unwrap(); + + assert!(matches!( + producer.push(&rec(2)), + Err(crate::Error::Net(moq_net::Error::Cancel)) + )); + assert!(matches!( + producer.finish(), + Err(crate::Error::Net(moq_net::Error::Cancel)) + )); + } + #[test] fn writes_after_another_clone_finishes_are_rejected() { let (mut producer, _track) = producer(ProducerConfig::default()); diff --git a/rs/moq-json/src/window/producer.rs b/rs/moq-json/src/window/producer.rs index 10fdd074267..c75db7b7694 100644 --- a/rs/moq-json/src/window/producer.rs +++ b/rs/moq-json/src/window/producer.rs @@ -48,7 +48,7 @@ impl Producer { }, encoder: Encoder::new(config), at: None, - finished: false, + state: crate::state::State::Open, })), _marker: PhantomData, } @@ -106,7 +106,8 @@ impl Producer { /// /// Borrows rather than consumes, matching snapshot and stream, so the handle stays /// usable afterwards (reads, a second finish). Writes after this fail with - /// [`moq_net::Error::Closed`]. + /// [`moq_net::Error::Closed`]. A finish that fails aborts the track instead, and every later + /// call returns its cause. pub fn finish(&mut self) -> Result<()> { self.inner.lock().unwrap().finish() } @@ -114,9 +115,9 @@ impl Producer { /// Abort the track with the given error, which consumers see in place of a clean end. /// /// Consumes the handle, since nothing can be published afterwards. Clones sharing the track see - /// it closed: their next edit fails with [`moq_net::Error::Closed`]. + /// it closed: their next edit fails with `err`. pub fn abort(self, err: moq_net::Error) -> Result<()> { - self.inner.lock().unwrap().abort(err); + self.inner.lock().unwrap().abort(err.into()); Ok(()) } } @@ -147,7 +148,8 @@ struct Inner { /// older timestamp than its group's first frame, and a new group may not start before that one. at: Option, - finished: bool, + /// Whether this producer still publishes, or how it ended the track. + state: crate::state::State, } impl Inner { @@ -193,25 +195,53 @@ impl Inner { } fn finish(&mut self) -> Result<()> { - if self.finished { - return Ok(()); + match &self.state { + crate::state::State::Open => {} + crate::state::State::Finished => return Ok(()), + crate::state::State::Aborted(err) => return Err(err.clone()), + } + // The open group goes with the track, so the encoder must not keep appending ops to it. + self.encoder.reset(); + if let Err(err) = self.track.finish() { + // Half-finished, the track could still take groups, so it ends with the cause instead. + self.abort(err.clone()); + return Err(err); } - self.finished = true; - self.track.finish() + self.state = crate::state::State::Finished; + Ok(()) } - fn abort(&mut self, err: moq_net::Error) { - self.finished = true; + /// Abort the track with `err`, which every later call reports. The first cause wins. + fn abort(&mut self, err: crate::Error) { + // The track carries a moq-net error; anything else has no wire code, so consumers get a + // generic failure while the exact cause stays here. + let reason = match &err { + crate::Error::Net(err) => err.clone(), + _ => moq_net::StreamError::Internal.into(), + }; + self.encoder.reset(); // The group first: aborting the track leaves an already-pulled group consumer independent. if let Some(group) = self.track.group.take() { - let _ = group.abort(err.clone()); + let _ = group.abort(reason.clone()); } // Idempotent: a track that is already closed keeps its first reason. - let _ = self.track.inner.clone().abort(err); + let _ = self.track.inner.clone().abort(reason); + self.state.abort(err); } + /// Refuse further edits once the track can't take another group. fn ensure_open(&self) -> Result<()> { - if self.finished { + self.state.check()?; + // The same test `append_group` applies: a boundary declared ahead of the live edge still + // admits the groups below it. The largest counts every group, even an empty one, so the + // next group is the one after it. + let track = &self.track.inner; + let next = track.largest().map_or(0, |largest| largest.group.saturating_add(1)); + if track.is_closed() + || track + .finished() + .is_some_and(|end| moq_net::track::Position::group(next) >= end) + { return Err(moq_net::Error::Closed.into()); } Ok(()) @@ -237,7 +267,8 @@ impl Inner { struct Track { inner: moq_net::track::Producer, - /// The group an op would be appended to, open after a header. + /// The group an op would be appended to, open after a header. Whatever clears it resets the + /// encoder too, or the encoder's next op would have no group to land in. group: Option, /// The sequence of the newest group written, which a refresh has to pass. @@ -298,7 +329,10 @@ impl Track { if let Some(mut group) = self.group.take() { group.finish()?; } - self.inner.finish()?; + // Another handle may have declared the end already, which stands. + if self.inner.finished().is_none() { + self.inner.finish()?; + } Ok(()) } } diff --git a/rs/moq-mux/src/container/flv/import.rs b/rs/moq-mux/src/container/flv/import.rs index aeccfa3a019..893746c3eaa 100644 --- a/rs/moq-mux/src/container/flv/import.rs +++ b/rs/moq-mux/src/container/flv/import.rs @@ -84,6 +84,9 @@ pub struct Import { video: BTreeMap, /// Demuxed audio tracks keyed by RTMP track id. audio: BTreeMap, + + /// Whether input is still accepted: [`finish`](Self::finish) ends it. + input: crate::container::Input, } /// The demuxed video track plus its current catalog config, so a repeated @@ -114,6 +117,7 @@ impl Import { dropped_metadata: 0, video: BTreeMap::new(), audio: BTreeMap::new(), + input: crate::container::Input::Open, } } @@ -135,7 +139,11 @@ impl Import { /// Append `buf` to the internal scratch and demux every whole tag it now /// completes. The buffer is fully consumed; a trailing partial tag is retained /// for the next call. + /// + /// Input after [`finish`](Self::finish) is refused with [`Finished`](crate::Error::Finished), + /// or with the cause of a finish that failed. pub fn decode(&mut self, data: &[u8]) -> crate::Result<()> { + self.input.check()?; self.buffer.extend_from_slice(data); match self.drain() { @@ -598,6 +606,7 @@ impl Import { /// Close the current group on every track and reopen at `sequence`. pub fn seek(&mut self, sequence: u64) -> crate::Result<()> { + self.input.check()?; for stream in self.video.values_mut() { stream.track.seek(sequence)?; } @@ -608,18 +617,31 @@ impl Import { } /// Finish every track, flushing the current group. + /// + /// Every track ends even when one fails to, aborting with the cause, and a finish that failed + /// returns the same error to every later call. Calling it again after a success does nothing. pub fn finish(&mut self) -> crate::Result<()> { + match &self.input { + crate::container::Input::Open => {} + crate::container::Input::Finished => return Ok(()), + crate::container::Input::Refused(err) | crate::container::Input::Failed(err) => return Err(err.clone()), + } // No input follows, so the buffer that waited for the rest of a tag is done. self.buffer = BytesMut::new(); // No frame follows to anchor the clock, so publish the declared track set now. self.initial_reservation = None; + let mut result = Ok(()); for stream in self.video.values_mut() { - stream.track.finish()?; + result = result.and(stream.track.end()); } for stream in self.audio.values_mut() { - stream.track.finish()?; + result = result.and(stream.track.end()); } - Ok(()) + self.input = match &result { + Ok(()) => crate::container::Input::Finished, + Err(err) => crate::container::Input::Failed(err.clone()), + }; + result } /// Abort every track with `err`, so consumers see the real cause instead of the diff --git a/rs/moq-mux/src/container/flv/import_test.rs b/rs/moq-mux/src/container/flv/import_test.rs index fd9daba5984..ac5fd2b25f2 100644 --- a/rs/moq-mux/src/container/flv/import_test.rs +++ b/rs/moq-mux/src/container/flv/import_test.rs @@ -1009,3 +1009,17 @@ fn unsupported_codecs_fail_at_the_public_importer_boundary() { } } } + +/// Input after finish is refused, as every importer refuses it, rather than publishing new tracks. +#[test] +fn input_after_finish_is_refused() { + let mut producer = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut producer, crate::catalog::Config::default()).unwrap(); + let mut importer = Import::new(producer, catalog.reserve()); + importer.finish().unwrap(); + importer.finish().unwrap(); + + let err = importer.decode(&synth_flv()).unwrap_err(); + assert!(matches!(err, crate::Error::Finished), "{err:?}"); + assert!(catalog.snapshot().video.renditions.is_empty(), "no track was declared"); +} diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index d11125c7c14..c225718f8f2 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -78,8 +78,9 @@ pub struct Import { buffer: BytesMut, body: Option, - // A refused chunk may contain unread bytes, so no later chunk can resume its framing. - failed: Option, + // Whether input is still accepted. A refused chunk may contain unread bytes, so no later chunk + // can resume its framing. + input: crate::container::Input, // A segment boundary (a styp or an explicit `cut()`) waiting to land on the next fragment. pending_cut: bool, @@ -201,10 +202,15 @@ impl Passthrough { Ok(()) } - /// Close the open group and finish the track. + /// Close the open group and finish the track, or abort both with the cause when that fails. fn finish(&mut self) -> crate::Result<()> { - self.close()?; - self.track.finish()?; + if let Err(err) = self.close().and_then(|()| Ok(self.track.finish()?)) { + if let Some(group) = self.group.take() { + let _ = group.abort(err.reason()); + } + let _ = self.track.clone().abort(err.reason()); + return Err(err); + } // A track that ended before measuring declares what it saw, and no longer holds the join // interval back. if let Some(mut member) = self.member.take() { @@ -281,7 +287,7 @@ impl Import { broadcast, buffer: BytesMut::new(), body: None, - failed: None, + input: crate::container::Input::Open, pending_cut: false, segment: 0, dropped_metadata: 0, @@ -307,9 +313,7 @@ impl Import { /// it follows the same segmentation as video. A passthrough video track rolls there as well, /// while an unwrapped one opens a group at each sync sample. pub fn cut(&mut self) -> Result<()> { - if let Some(err) = &self.failed { - return Err(err.clone()); - } + self.input.check()?; self.pending_cut = true; Ok(()) } @@ -364,9 +368,7 @@ impl Import { /// The refusing call may leave bytes unread, so even box-aligned callers must replace the /// importer to resume. [`finish`](Self::finish) still ends the tracks with what was accepted. pub fn decode(&mut self, data: &[u8]) -> Result<()> { - if let Some(err) = &self.failed { - return Err(err.clone()); - } + self.input.check()?; let result = self.drain(data); if let Err(err) = &result { if let Some(Body::Media(mut mdat)) = self.body.take() { @@ -375,7 +377,7 @@ impl Import { self.buffer = BytesMut::new(); self.moof = None; self.deferred = Deferred::default(); - self.failed = Some(err.clone()); + self.input = crate::container::Input::Refused(err.clone()); } result } @@ -527,7 +529,7 @@ impl Import { return Err(Error::TooManyTracks(moov.trak.len()).into()); } // Only `finish()` releases the reservation before a moov, since a fragment needs one. - let reserved = self.initial_reservation.clone().ok_or(Error::MoovAfterFinish)?; + let reserved = self.initial_reservation.clone().ok_or(crate::Error::Finished)?; for trak in &moov.trak { let track_id = trak.tkhd.track_id; @@ -1685,7 +1687,15 @@ impl Import { /// their groups, every track still finishes, and this returns /// [`UnexpectedEof`](mp4_atom::Error::UnexpectedEof). After a decode error, this ends the /// accepted media without reading more input; it does not clear that error for later decodes. + /// Either way every track has ended. A finish that failed returns the same error to every + /// later call; one that succeeded refuses later input with + /// [`Finished`](crate::Error::Finished), or with the decode error it followed. pub fn finish(&mut self) -> Result<()> { + match &self.input { + crate::container::Input::Finished => return Ok(()), + crate::container::Input::Failed(err) => return Err(err.clone()), + crate::container::Input::Open | crate::container::Input::Refused(_) => {} + } // No input follows, so the buffer that waited for the rest of an atom is done. self.buffer = BytesMut::new(); // A seek or discontinuity waiting on the truncated fragment has no fragment left to land on. @@ -1697,23 +1707,28 @@ impl Import { } _ => false, }; - self.finish_tracks()?; - if truncated { - return Err(mp4_atom::Error::UnexpectedEof.into()); - } - Ok(()) - } - - fn finish_tracks(&mut self) -> Result<()> { + let mut result = match truncated { + true => Err(mp4_atom::Error::UnexpectedEof.into()), + false => Ok(()), + }; + // End every track even on refusal, keeping the first error. A track whose finish fails + // aborts with the cause, so none is left open. for track in self.text.values_mut() { - track.producer.finish()?; + result = result.and(track.producer.end()); } // No frame follows to anchor the clock, so publish the declared track set now. self.initial_reservation = None; for track in self.tracks.values_mut() { - media!(&mut track.output, producer => producer.finish()?, passthrough => passthrough.finish()?); + result = + result.and(media!(&mut track.output, producer => producer.end(), passthrough => passthrough.end())); } - Ok(()) + match &result { + // A refusal keeps refusing input with its own cause. + Ok(()) if matches!(self.input, crate::container::Input::Refused(_)) => {} + Ok(()) => self.input = crate::container::Input::Finished, + Err(err) => self.input = crate::container::Input::Failed(err.clone()), + } + result } /// Abort all tracks with `err` instead of finishing, so subscribers see the real @@ -1751,9 +1766,7 @@ impl Import { /// A fragment whose samples are still arriving owns its groups until its last sample, so the /// seek waits for it to finish publishing. pub fn seek(&mut self, sequence: u64) -> Result<()> { - if let Some(err) = &self.failed { - return Err(err.clone()); - } + self.input.check()?; if self.publishing() { self.deferred.seek = Some(sequence); return Ok(()); @@ -1786,9 +1799,7 @@ impl Import { /// rewinds, before publishing any of it. Like [`seek`](Self::seek), it waits for a fragment /// still arriving to finish publishing. pub fn discontinuity(&mut self) -> Result<()> { - if let Some(err) = &self.failed { - return Err(err.clone()); - } + self.input.check()?; if self.publishing() { self.deferred.discontinuity = true; return Ok(()); diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index da326f51d4e..fd57e1e88bb 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -2900,10 +2900,7 @@ fn a_moov_after_finish_is_refused() { fmp4.finish().unwrap(); let err = fmp4.decode(&init).unwrap_err(); - assert!( - matches!(err, crate::Error::Cmaf(crate::container::fmp4::Error::MoovAfterFinish)), - "{err:?}" - ); + assert!(matches!(err, crate::Error::Finished), "{err:?}"); let snapshot = catalog.snapshot(); assert!(snapshot.video.renditions.is_empty() && snapshot.audio.renditions.is_empty()); } diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index 9fb800207fa..5cf8e68694a 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -207,10 +207,6 @@ pub enum Error { #[error("duplicate moov")] DuplicateMoov, - /// A moov arrived after `finish()`, so the tracks it declares could never finish. - #[error("moov after finish")] - MoovAfterFinish, - /// The track fragment has no trun box. #[error("missing trun")] MissingTrun, diff --git a/rs/moq-mux/src/container/input.rs b/rs/moq-mux/src/container/input.rs new file mode 100644 index 00000000000..3fb27d6c749 --- /dev/null +++ b/rs/moq-mux/src/container/input.rs @@ -0,0 +1,25 @@ +/// Where an importer's input stands, checked before any call touches a track. +#[derive(Debug, Default)] +pub(crate) enum Input { + /// Accepting input. + #[default] + Open, + /// A chunk was refused part way, so no later chunk can continue it. `finish` still ends the + /// tracks with what was accepted. + Refused(crate::Error), + /// Finishing failed, ending every track: every later call reports this cause. + Failed(crate::Error), + /// `finish` ended every track. + Finished, +} + +impl Input { + /// Refuse the call unless the input is still open. + pub(crate) fn check(&self) -> crate::Result<()> { + match self { + Self::Open => Ok(()), + Self::Refused(err) | Self::Failed(err) => Err(err.clone()), + Self::Finished => Err(crate::Error::Finished), + } + } +} diff --git a/rs/moq-mux/src/container/mkv/import.rs b/rs/moq-mux/src/container/mkv/import.rs index 9b16fd16b3d..af47f50f382 100644 --- a/rs/moq-mux/src/container/mkv/import.rs +++ b/rs/moq-mux/src/container/mkv/import.rs @@ -119,11 +119,9 @@ pub struct Import { hvcc: Option, /// A seek waiting for the streaming block to end, since its later frames continue its group. seek: Option, - /// Whether `finish()` ended the input, even if it failed before the input drained. - finished: bool, - /// Why a chunk was refused. The rest of that chunk was never read, so later input cannot - /// continue it. - failed: Option, + /// Whether input is still accepted. A refused chunk was never read to its end, so later input + /// cannot continue it. + input: crate::container::Input, /// Whether the Tracks element has been processed. tracks_seen: bool, @@ -199,13 +197,6 @@ impl Media { } } - fn finish(&mut self) -> crate::Result<()> { - match self { - Self::Video(track) => track.finish(), - Self::Audio(track) => track.finish(), - } - } - fn end(&mut self) -> crate::Result<()> { match self { Self::Video(track) => track.end(), @@ -328,8 +319,7 @@ impl Import { block: None, hvcc: None, seek: None, - finished: false, - failed: None, + input: crate::container::Input::Open, tracks_seen: false, timestamp_scale_ns: DEFAULT_TIMESTAMP_SCALE_NS, cluster_timestamp: 0, @@ -355,15 +345,10 @@ impl Import { /// A refused chunk fails the import: later input is refused with the same error, and /// [`finish`](Self::finish) ends the tracks with what was accepted. pub fn decode(&mut self, data: &[u8]) -> Result<()> { - if self.finished && !data.is_empty() { - return Err(Error::TracksAfterFinish.into()); - } - if let Some(err) = &self.failed { - return Err(err.clone()); - } + self.input.check()?; let result = self.drain(data); if let Err(err) = &result { - self.failed = Some(err.clone()); + self.input = crate::container::Input::Refused(err.clone()); } result } @@ -556,8 +541,7 @@ impl Import { } id::TRACK_ENTRY => { let children = self.fields.pop().unwrap_or_default(); - // Only `finish()` releases the reservation before Tracks, since a block needs a track. - let reserved = self.initial_reservation.clone().ok_or(Error::TracksAfterFinish)?; + let reserved = self.initial_reservation.clone().ok_or(Error::LateTracks)?; self.add_track(&reserved, children)?; } id::VIDEO | id::AUDIO | id::COLOUR | id::MASTERING_METADATA | id::PROJECTION => { @@ -911,6 +895,7 @@ impl Import { /// control is intentionally not exposed. While a block streams in, the seek waits for it to end, /// since its later frames continue its group. pub fn seek(&mut self, sequence: u64) -> Result<()> { + self.input.check()?; if self.block.is_some() { self.seek = Some(sequence); return Ok(()); @@ -924,12 +909,20 @@ impl Import { /// Finish all tracks, flushing current groups. /// /// Input that ends inside an element other than an unknown-size Segment or Cluster is refused as - /// truncated, but the tracks still end with what was accepted. + /// truncated, but the tracks still end with what was accepted, as they do after a refused + /// [`decode`](Self::decode). Either way every track has ended. A finish that failed returns + /// the same error to every later call; one that succeeded refuses later input with + /// [`Finished`](crate::Error::Finished), or with the refusal it followed. pub fn finish(&mut self) -> Result<()> { - self.finished = true; - let mut result = match self.failed { - None => self.drain_eof(), - Some(_) => Ok(()), + let mut result = match &self.input { + crate::container::Input::Finished => return Ok(()), + crate::container::Input::Failed(err) => return Err(err.clone()), + crate::container::Input::Refused(err) => { + // A frame the refusal cut short fails its group with the reason. + self.abort_block(err.reason()); + Ok(()) + } + crate::container::Input::Open => self.drain_eof(), }; if let Err(err) = &result { // A frame cut short fails its group with the reason, rather than waiting for the rest. @@ -938,8 +931,15 @@ impl Import { // No frame follows to anchor the clock, so publish the declared track set now. self.initial_reservation = None; for track in self.tracks.values_mut() { - // Finish every track even on refusal, keeping the first error. - result = result.and(track.track.finish()); + // End every track even on refusal, keeping the first error. A track whose finish fails + // aborts with the cause, so none is left open. + result = result.and(track.track.end()); + } + match &result { + // A refusal keeps refusing input with its own cause. + Ok(()) if matches!(self.input, crate::container::Input::Refused(_)) => {} + Ok(()) => self.input = crate::container::Input::Finished, + Err(err) => self.input = crate::container::Input::Failed(err.clone()), } result } @@ -1630,10 +1630,7 @@ mod tests { } import.finish().unwrap(); import.finish().unwrap(); - assert!(matches!( - import.decode(&[0]), - Err(crate::Error::Mkv(Error::TracksAfterFinish)) - )); + assert!(matches!(import.decode(&[0]), Err(crate::Error::Finished))); } /// A WebM prefix declaring `entries`, leaving an unknown-size Segment and Cluster open. @@ -2215,17 +2212,16 @@ mod tests { } } + /// A failed finish is final: later input and a retried finish report its cause. #[test] - fn input_after_a_failed_finish_is_refused() { + fn a_failed_finish_reports_its_cause() { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); let mut import = Import::new(broadcast, catalog.reserve()); // An EBML header declaring a byte it never sends. import.decode(&[0x1a, 0x45, 0xdf, 0xa3, 0x81]).unwrap(); assert!(matches!(import.finish(), Err(crate::Error::Mkv(Error::Truncated)))); - assert!(matches!( - import.decode(&[0]), - Err(crate::Error::Mkv(Error::TracksAfterFinish)) - )); + assert!(matches!(import.decode(&[0]), Err(crate::Error::Mkv(Error::Truncated)))); + assert!(matches!(import.finish(), Err(crate::Error::Mkv(Error::Truncated)))); } } diff --git a/rs/moq-mux/src/container/mkv/import_test.rs b/rs/moq-mux/src/container/mkv/import_test.rs index 221f53970a4..90756e1e0eb 100644 --- a/rs/moq-mux/src/container/mkv/import_test.rs +++ b/rs/moq-mux/src/container/mkv/import_test.rs @@ -1141,10 +1141,7 @@ fn tracks_after_finish_are_refused() { mkv.finish().unwrap(); let err = mkv.decode(&data).unwrap_err(); - assert!( - matches!(err, crate::Error::Mkv(crate::container::mkv::Error::TracksAfterFinish)), - "{err:?}" - ); + assert!(matches!(err, crate::Error::Finished), "{err:?}"); assert!(catalog.snapshot().audio.renditions.is_empty(), "no track was declared"); } diff --git a/rs/moq-mux/src/container/mkv/mod.rs b/rs/moq-mux/src/container/mkv/mod.rs index 3d18029e778..3ff33d7c681 100644 --- a/rs/moq-mux/src/container/mkv/mod.rs +++ b/rs/moq-mux/src/container/mkv/mod.rs @@ -71,9 +71,10 @@ pub enum Error { #[error("timestamp overflow")] TimestampOverflow, - /// Input arrived after `finish()`, so any tracks it declares could never finish. - #[error("Tracks after finish")] - TracksAfterFinish, + /// A TrackEntry ended after the catalog was published, by the first block or by a Tracks + /// element declaring none, so its rendition could never join the catalog's first snapshot. + #[error("Tracks after the catalog was published")] + LateTracks, /// The TrackEntry has no TrackNumber. #[error("TrackEntry missing TrackNumber")] diff --git a/rs/moq-mux/src/container/mod.rs b/rs/moq-mux/src/container/mod.rs index a1110a298da..67eee9272a0 100644 --- a/rs/moq-mux/src/container/mod.rs +++ b/rs/moq-mux/src/container/mod.rs @@ -16,6 +16,7 @@ mod consumer; mod declared; mod decode; mod group; +mod input; mod producer; mod source; #[cfg(test)] @@ -33,6 +34,7 @@ pub mod ts; pub use consumer::{Consumer, DEFAULT_MAX_BUFFER, Playout}; pub use group::GroupConsumer; +pub(crate) use input::Input; pub use producer::Producer; pub(crate) use source::{ExportSource, Resume}; diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index 6d823fa1ab2..abdf535bd04 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -712,12 +712,34 @@ where self.estimator.cut(end); self.claim(); - let tail_end = marker_at.filter(|_| !self.reordered); - self.flush_buffer(tail_end)?; - + // The group's own state ends with it, whether or not closing it succeeds. + let reordered = std::mem::take(&mut self.reordered); // Where this group's content stops: the duration marker when we write one, else the // caller's bound, else the furthest point we wrote. - let reached = marker_at.or(end).max(self.end); + let reached = marker_at.or(end).max(self.end.take()); + self.last_duration = None; + self.previous_timestamp = None; + + let tail_end = marker_at.filter(|_| !reordered); + if let Err(err) = self.close_group(tail_end, reached) { + // A group that fails to close fails with the cause, rather than staying open for the next + // write to continue. + self.buffer.clear(); + if let Some(group) = self.group.take() { + let _ = group.abort(err.reason()); + } + return Err(err); + } + self.publish_estimate() + } + + /// Flush the buffer into the open group and finish it, raising the live edge to `reached`. + fn close_group( + &mut self, + tail_end: Option, + reached: Option, + ) -> crate::Result<()> { + self.flush_buffer(tail_end)?; if let Some(group) = self.group.as_mut() { self.container.finish_group(group, tail_end)?; } @@ -728,11 +750,6 @@ where self.raise_live_edge(end); } } - self.end = None; - self.last_duration = None; - self.previous_timestamp = None; - self.reordered = false; - self.publish_estimate()?; Ok(()) } @@ -930,7 +947,11 @@ where } fn end_with(&mut self, err: Option) -> crate::Result<()> { - self.cut(None)?; + if let Err(cause) = self.cut(None) { + // A track that cannot close its last group ends with the cause rather than staying open. + let _ = self.inner.clone().abort(cause.reason()); + return Err(cause); + } match err { None => self.inner.finish()?, Some(err) => { @@ -941,7 +962,7 @@ where self.inner.finish_at(moq_net::track::Position::group(next), Some(err))?; } } - self.publish_estimate()?; + let published = self.publish_estimate(); // A track that ended before measuring declares what it saw, and no longer holds the join // interval back. if let Some(mut member) = self.member.take() { @@ -949,7 +970,7 @@ where } // A finished track may be kept around, but its media stops holding the other tracks back. self.estimator.discontinuity(); - Ok(()) + published } /// [`finish`](Self::finish) the track unless it already ended, for an owner about to drop it. diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 0380f58f3ca..022603f25d1 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -243,9 +243,8 @@ pub struct Import { described: bool, /// Carried PIDs already warned about a TS-scrambled packet. scrambled: HashSet, - /// Whether [`finish`](Self::finish) or [`abort`](Self::abort) ended the input, even if finishing - /// failed part way. - finished: bool, + /// Whether input is still accepted: [`finish`](Self::finish) or [`abort`](Self::abort) ends it. + input: crate::container::Input, } impl Import { @@ -322,7 +321,7 @@ impl Import { alone: false, described: false, scrambled: HashSet::new(), - finished: false, + input: crate::container::Input::Open, } } @@ -426,7 +425,11 @@ impl Import { /// half a group's bytes. A PAT naming another `transport_stream_id` or program number /// restarts too, at that PAT. Every later call repeats the restart; [`finish`](Self::finish) /// ends the instance before the step. + /// + /// Input after [`finish`](Self::finish) is refused with [`Finished`](crate::Error::Finished), + /// or with the cause of a finish that failed. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { + self.input.check()?; let mut demux = std::mem::take(&mut self.demux); let result = self.decode_with(&mut demux, data); self.demux = demux; @@ -1903,12 +1906,33 @@ impl Import { } /// Flush any buffered PES and finish every track. + /// + /// A finish that fails aborts every track with the cause, and later calls return it again. + /// A finish that succeeded is final: calling it again does nothing. pub fn finish(&mut self) -> anyhow::Result<()> { + match &self.input { + crate::container::Input::Open => {} + crate::container::Input::Finished => return Ok(()), + crate::container::Input::Refused(err) | crate::container::Input::Failed(err) => { + return Err(err.clone().into()); + } + } + let result = self.end(); + match &result { + Ok(()) => self.input = crate::container::Input::Finished, + // The copy keeps the whole chain in its message, since only the first report can move the + // original. + Err(err) => self.fail(crate::Error::from(anyhow::anyhow!("{err:#}"))), + } + result + } + + /// Flush the input and finish every track, stopping at the first failure. + fn end(&mut self) -> anyhow::Result<()> { // A step still being judged never completed: what it held routes as read. - if !self.finished && self.steps.settle() { + if self.steps.settle() { self.decode(&[])?; } - self.finished = true; self.demux.finish(); for pid in self.headers.keys().copied().collect::>() { self.damage(pid, &anyhow::anyhow!("PES header truncated at end of input"))?; @@ -2023,7 +2047,14 @@ impl Import { /// Unlike a drop, this leaves the tracks' HLS records to the catalog, whose abort fails them /// rather than ending them as a complete recording. pub fn abort(mut self, err: moq_net::Error) { - self.finished = true; + self.fail(err.into()); + } + + /// Abort every track with `cause`, which later calls report, and release the catalog: nothing + /// is left to wait for. + pub(super) fn fail(&mut self, cause: crate::Error) { + let err = cause.reason(); + self.input = crate::container::Input::Failed(cause); for stream in std::mem::take(&mut self.streams).into_values() { stream.abort(err.clone()); } @@ -2035,13 +2066,15 @@ impl Import { super::si::Capture::new(self.broadcast.clone(), self.catalog.clone()), ); si.abort(err); + self.mux_rate_reservation = None; + self.initial_reservation = None; } } impl Drop for Import { /// End the input unless it already ended. fn drop(&mut self) { - if !self.finished + if matches!(self.input, crate::container::Input::Open) && let Err(err) = self.finish() { tracing::warn!(%err, "failed to finish a dropped TS import"); @@ -8634,7 +8667,6 @@ pub(super) mod test { if clock == "none" { let err = import.finish().unwrap_err(); assert!(err.to_string().contains("without a media timestamp"), "{clock}: {err}"); - assert_eq!(import.sections[&0x21].held.len(), 1); } else { import.finish().unwrap(); assert!(import.sections[&0x21].held.is_empty()); @@ -8644,6 +8676,64 @@ pub(super) mod test { } } + /// A finish that fails is final: every track aborts with the cause rather than staying open, + /// and later calls report it instead of finishing again or accepting input. + #[tokio::test] + async fn a_failed_finish_aborts_every_track() { + use crate::catalog::hang::Catalog; + use crate::container::ts::catalog::Ext; + + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let consumer = broadcast.consume(); + let catalog = crate::catalog::Producer::new( + &mut broadcast, + crate::catalog::Config::default().with_catalog(Catalog::::default()), + ) + .unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import + .decode(&synth_pmt( + &[ + (StreamType::Mpeg2Video, PCR_PID), + (StreamType::Dts8ChannelLosslessAudio, 0x21), + ], + true, + )) + .unwrap(); + import.decode(&pcr_packet(PCR_PID, 27_000_000)).unwrap(); + // A section no media timestamp ever places. + import.decode(&packet(true, 0, 0, &CUE)).unwrap(); + let name = catalog.snapshot().ext.mpegts.tracks.keys().next().unwrap().clone(); + let mut track = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); + + let err = import.finish().unwrap_err(); + assert!(err.to_string().contains("without a media timestamp"), "{err}"); + let ended = kio::clock::timeout(Duration::from_millis(50), track.recv_group()) + .await + .expect("the track ends"); + assert!(ended.is_err(), "the track aborts rather than finishing"); + + let retried = import.finish().unwrap_err(); + assert!(retried.to_string().contains("without a media timestamp"), "{retried}"); + let refused = import.decode(&pcr_packet(PCR_PID, 54_000_000)).unwrap_err(); + assert!(refused.to_string().contains("without a media timestamp"), "{refused}"); + } + + /// Input after a finish that succeeded is refused, as every importer refuses it. + #[test] + fn input_after_finish_is_refused() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + import.finish().unwrap(); + import.finish().unwrap(); + let err = import.decode(&pcr_packet(PCR_PID, 27_000_000)).unwrap_err(); + assert!( + matches!(err.downcast_ref::(), Some(crate::Error::Finished)), + "{err}" + ); + } + #[test] fn unanchored_sections_cannot_cross_a_clock_break() { use crate::catalog::hang::Catalog; diff --git a/rs/moq-mux/src/container/ts/passthrough.rs b/rs/moq-mux/src/container/ts/passthrough.rs index fbf253a246d..190f3484276 100644 --- a/rs/moq-mux/src/container/ts/passthrough.rs +++ b/rs/moq-mux/src/container/ts/passthrough.rs @@ -162,6 +162,9 @@ pub struct Passthrough { all_random_access: bool, mux_rate: Meter, + + /// Whether input is still accepted: [`finish`](Self::finish) ends it. + input: crate::container::Input, } /// Owns the `m2ts` catalog section once written, removing it however the track ends, as the @@ -266,6 +269,7 @@ impl Passthrough { group: None, all_random_access: true, mux_rate: Meter::default(), + input: crate::container::Input::Open, }) } @@ -288,6 +292,7 @@ impl Passthrough { /// Append `data` and publish every whole packet it completes. A trailing partial packet is /// kept for the next call. pub fn decode(&mut self, data: &[u8]) -> crate::Result<()> { + self.input.check()?; self.scratch.extend_from_slice(data); if !self.sized { self.probe_packet_size(false)?; @@ -299,7 +304,31 @@ impl Passthrough { /// /// No later PCR will arrive, so the objects since the last one are timed at the rate of the /// interval before it. The catalog keeps the `m2ts` section until the importer drops. + /// + /// A finish that fails ends the track with an error, and every later call returns its cause. + /// Calling it again after a success does nothing, and later input is refused with + /// [`Finished`](crate::Error::Finished). pub fn finish(&mut self) -> crate::Result<()> { + match &self.input { + crate::container::Input::Open => {} + crate::container::Input::Finished => return Ok(()), + crate::container::Input::Refused(err) | crate::container::Input::Failed(err) => return Err(err.clone()), + } + let result = self.end(); + match &result { + Ok(()) => self.input = crate::container::Input::Finished, + Err(err) => { + // The objects already written stay readable, then subscribers get an error. + let _ = self.media.fail(err.reason()); + self.reservation = None; + self.input = crate::container::Input::Failed(err.clone()); + } + } + result + } + + /// Publish the rest of the input and finish the track, stopping at the first failure. + fn end(&mut self) -> crate::Result<()> { if !self.sized { self.probe_packet_size(true)?; self.packets()?; diff --git a/rs/moq-mux/src/container/ts/passthrough_test.rs b/rs/moq-mux/src/container/ts/passthrough_test.rs index 386e8e42e82..0da27d32b75 100644 --- a/rs/moq-mux/src/container/ts/passthrough_test.rs +++ b/rs/moq-mux/src/container/ts/passthrough_test.rs @@ -843,3 +843,16 @@ async fn an_overriding_clock_paces_on_its_own_pcrs() { "a clock no program names marks no program's decoding boundary" ); } + +/// Input after finish is refused, as every importer refuses it. +#[test] +fn input_after_finish_is_refused() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let mut passthrough = Passthrough::new(broadcast, catalog.reserve()).unwrap(); + passthrough.finish().unwrap(); + passthrough.finish().unwrap(); + + let err = passthrough.decode(KYRION_H264).unwrap_err(); + assert!(matches!(err, crate::Error::Finished), "{err:?}"); +} diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index 7111834f2ce..cd7f931af00 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -80,6 +80,8 @@ pub struct Programs { due: health::Due, /// The packets on PIDs no program reads. unclaimed: Unclaimed, + /// Whether input is still accepted: [`finish`](Self::finish) ends it. + input: crate::container::Input, } /// `PMT_error_2`: a PMT recurs at least every 0.5 s of program clock. @@ -324,11 +326,16 @@ impl Programs { deadline: None, due: health::Due::default(), unclaimed: Unclaimed::default(), + input: crate::container::Input::Open, } } /// Demux a chunk of the multiplex. A trailing partial packet is retained for the next call. + /// + /// Input after [`finish`](Self::finish) is refused with [`Finished`](crate::Error::Finished), + /// or with the cause of a finish that failed. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { + self.input.check()?; let mut demux = std::mem::take(&mut self.demux); let result = self.decode_with(&mut demux, data); self.demux = demux; @@ -879,7 +886,33 @@ impl Programs { /// tracks stay servable while this importer is held, so hand both to a /// [`Linger`](crate::Linger) to keep them up for late subscribers. Fails when the input /// ended before its PAT listed a program or any program declared a track. + /// + /// A finish that fails aborts every program with the cause, and later calls return it again. + /// A finish that succeeded is final: calling it again returns the same broadcasts. pub fn finish(&mut self) -> anyhow::Result> { + match &self.input { + crate::container::Input::Open => {} + crate::container::Input::Finished => return Ok(self.broadcasts()), + crate::container::Input::Refused(err) | crate::container::Input::Failed(err) => { + return Err(err.clone().into()); + } + } + match self.end() { + Ok(()) => { + self.input = crate::container::Input::Finished; + Ok(self.broadcasts()) + } + Err(err) => { + // The copy keeps the whole chain in its message, since only the first report can move + // the original. + self.fail(crate::Error::from(anyhow::anyhow!("{err:#}"))); + Err(err) + } + } + } + + /// Finish every program, stopping at the first failure. + fn end(&mut self) -> anyhow::Result<()> { self.demux.finish(); anyhow::ensure!(self.pat.is_some(), "input ended before its PAT listed a program"); let mut announced = self.announced; @@ -889,19 +922,29 @@ impl Programs { program.catalog.finish()?; } anyhow::ensure!(announced, "input ended before any program declared a track"); - Ok(self - .programs + Ok(()) + } + + /// Each running program's broadcast. + fn broadcasts(&self) -> Vec { + self.programs .iter() .map(|program| program.route.broadcast().clone()) - .collect()) + .collect() } /// Abort every program's catalog and media tracks with `err`. Consumes the importer. - pub fn abort(self, err: moq_net::Error) { - for program in self.programs { - program.catalog.abort(err.clone()); - program.import.abort(err.clone()); + pub fn abort(mut self, err: moq_net::Error) { + self.fail(err.into()); + } + + /// Abort every program's catalog and media tracks with `cause`, which later calls report. + fn fail(&mut self, cause: crate::Error) { + for program in &mut self.programs { + program.catalog.clone().abort(cause.reason()); + program.import.fail(cause.clone()); } + self.input = crate::container::Input::Failed(cause); } } diff --git a/rs/moq-mux/src/error.rs b/rs/moq-mux/src/error.rs index f4f916e2e23..dd9fffac8d4 100644 --- a/rs/moq-mux/src/error.rs +++ b/rs/moq-mux/src/error.rs @@ -294,6 +294,10 @@ pub enum Error { /// The latest media the export read from the rendition's previous track. read: std::time::Duration, }, + + /// An importer was given input after it finished. + #[error("input after finish")] + Finished, } impl Error { diff --git a/rs/moq-transcode/src/rung.rs b/rs/moq-transcode/src/rung.rs index 6011caddbe5..7e71136b925 100644 --- a/rs/moq-transcode/src/rung.rs +++ b/rs/moq-transcode/src/rung.rs @@ -867,17 +867,20 @@ impl Encoder { && *signalled != color { let (sink, _) = self.open.take().expect("checked above"); - encoded = sink.finish().await?; - // The new parameter sets only reach a decoder on an IDR. + // The new parameter sets only reach a decoder on an IDR. Owed before the old encoder + // drains, so a drain that fails still leaves the next encoder owing it. self.pending_cut = true; + encoded = sink.finish().await?; } let sink = match &mut self.open { Some((sink, _)) => sink, None => { let mut sink = rung.encode(color).await?; rung.rendition.opened(sink.applied()); - if std::mem::take(&mut self.pending_cut) { + // Cleared only once applied: a cut that fails is still owed to the next encoder. + if self.pending_cut { sink.cut().await?; + self.pending_cut = false; } &mut self.open.insert((sink, color)).0 }