diff --git a/demo/carla/bridge/Cargo.lock b/demo/carla/bridge/Cargo.lock index 4c3b9c6d67f..ee7790e162d 100644 --- a/demo/carla/bridge/Cargo.lock +++ b/demo/carla/bridge/Cargo.lock @@ -1291,7 +1291,6 @@ dependencies = [ name = "moq-mux" version = "0.10.11" dependencies = [ - "anyhow", "base64", "bitstream-io", "bytes", diff --git a/doc/lib/rs/moq-mux.md b/doc/lib/rs/moq-mux.md index 3c5ad76bced..7fb91774ee8 100644 --- a/doc/lib/rs/moq-mux.md +++ b/doc/lib/rs/moq-mux.md @@ -36,9 +36,10 @@ identity changes, such as an SPS with a new profile or level or an audio header with a new sample rate, continues under a new track name (`/` for the codec importers) while the old track ends; everything else, such as the coded size, updates the rendition in place. A -timeline that rewinds ends the import. The MPEG-TS import ends at a clock step -onto another timeline with `ts::Restart`, which carries the input from the step -for the caller to publish as a new broadcast. fMP4 export fixes its track +timeline that rewinds ends the import. The MPEG-TS import ends with an error at +a clock step onto another timeline, or a PAT naming another program; +`ts::Republish` carries on past it, publishing what follows as a new instance of +the broadcast. fMP4 export fixes its track set at the init segment, so a new rendition or a changed configuration ends the export. The [MPEG-TS guide](/bin/ts) covers the MPEG-TS specifics: one program per broadcast, private PES, passthrough, acquisition, and loss. diff --git a/nix/Cargo.nix b/nix/Cargo.nix index f8e84c5543c..be1a7ba601f 100644 --- a/nix/Cargo.nix +++ b/nix/Cargo.nix @@ -19805,10 +19805,6 @@ rec { "Luke Curley " ]; dependencies = [ - { - name = "anyhow"; - packageId = "anyhow"; - } { name = "base64"; packageId = "base64 0.23.1"; @@ -19928,6 +19924,10 @@ rec { } ]; devDependencies = [ + { + name = "anyhow"; + packageId = "anyhow"; + } { name = "bench-harness"; packageId = "bench-harness"; diff --git a/quest/m2/README.md b/quest/m2/README.md index 851f61e0371..3ade2756464 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -54,8 +54,8 @@ removal, a rename, or a wire, catalog, or format change that is free until `mpegts`, one strict rule); [the archive as version 2](/quest/m2/archive-v2.md); [declared ends](/quest/m2/time-addressing/consumers.md), back from m3; - [finish](/quest/m2/finish-sweep.md), [TS outcomes](/quest/m2/ts-typed-outcomes.md), - and [moq-json writes](/quest/m2/json-write-at.md). + [finish](/quest/m2/finish-sweep.md), and + [moq-json writes](/quest/m2/json-write-at.md). - **Native media and gateways.** [The video producer](/quest/m2/video-producer.md), [the hole wait](/quest/m2/delay-hole-wait.md), [device and cursor](/quest/m2/native-media-config.md), diff --git a/quest/m2/finish-sweep.md b/quest/m2/finish-sweep.md index e5befe53efd..0fcc14aab29 100644 --- a/quest/m2/finish-sweep.md +++ b/quest/m2/finish-sweep.md @@ -74,4 +74,3 @@ importers, `moq_mux::import::{Track, TrackStream}`, and ## Related - [The frame-taking video producer](/quest/m2/video-producer.md) - renames the video type this changes -- [TS import reports typed outcomes](/quest/m2/ts-typed-outcomes.md) - changes the TS importers' `finish` error type diff --git a/quest/m2/ts-typed-outcomes.md b/quest/m2/ts-typed-outcomes.md deleted file mode 100644 index 50a345d8f32..00000000000 --- a/quest/m2/ts-typed-outcomes.md +++ /dev/null @@ -1,78 +0,0 @@ -# [M] TS import reports typed outcomes, and no caller downcasts an error - -## Goal - -`moq_mux::container::ts` reports what happened through types. Its public -functions return a `ts::Error` enum, not `anyhow`; a source switch comes -back as a value (`Restart`), not as an error that carries the unread -input; a stream with several programs is a variant a caller matches on. -`moq_mux::Error::Other` is gone. The callers, moq-cli (`import ts`, -`import srt`, and `import rtsp`) and moq-srt, read the typed result and -contain no `downcast_ref` or `is::()` on an error. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): - -- ✅ Typed TS outcomes: a `ts::Error` enum, restart as a value, - `Error::Other` dropped, and the CLI stops downcasting, rtsp included - (rejected: keep `anyhow` inside and expose only the two outcomes as - typed values; keep as is). AGENTS.md: "Never match on errors; it's - brittle", and `rs/AGENTS.md` asks libraries for `thiserror`. - -Facts found by the audit: - -- `rs/moq-mux/src/container/ts/import.rs`: `pub fn decode(&mut self, data: - &[u8]) -> anyhow::Result<()>`, with `seek` and `finish` the same. The - internal `decode_or_restart` beside it already returns - `anyhow::Result>`, the shape the public one should have. - `programs.rs` (`decode`, `finish`) and `republish.rs` (`decode`, - `finish`) return `anyhow` too. -- `MultipleProgramsError` (`import.rs`) is a second outcome sent as an - error, and `moq_mux::Error::Other(Arc)` - (`rs/moq-mux/src/error.rs`) exists to carry it; 8 sites in - `rs/moq-mux/src` build or name `Other`. -- Callers that match on the error: - `rs/moq-cli/src/publish.rs` (`err.is::()`), - `rs/moq-cli/src/srt.rs` (the same through two error layers), - `rs/moq-srt/src/ts.rs` (`downcast_ref::()`), - and `rs/moq-cli/src/rtsp.rs` (`downcast_ref::()`). -- [moq-dev/moq#8831](https://github.com/moq-dev/moq/pull/8831) moved the - `Restart` handling into one `ts::Republish` but left the signatures. - -For the PR: - -- `ts::Error` is a `thiserror` enum, `#[non_exhaustive]`, with a variant - for several programs that carries what the caller prints today, and - variants for the failures the callers or tests tell apart. Do not - enumerate every internal `anyhow!`: a damaged packet that the importer - already counts and survives is not an error, and what remains groups - into a handful of causes. It converts into `moq_mux::Error` by a typed - variant, which is what lets `Other` go. -- `Import::decode` returns `Result, ts::Error>`; - `Programs` and `Republish` return the same error. Internal helpers may - keep `anyhow` only if the boundary maps every case; prefer converting - them, since 18 public `ts` functions name `anyhow::Result` today. -- moq-rtsp: give the CLI what it downcasts for as a typed result from the - call it makes, so `rs/moq-cli/src/rtsp.rs` matches a `moq_rtsp::Error` - it was handed, not one it dug out of an `anyhow` chain. -- Out of scope: `rs/moq-cli/src/play/media.rs` downcasts the CLI's own - private `Unplayable`, and `rs/moq-srt/src/dial.rs` reads a libsrt - rejection from an `io::Error`; neither crosses a library API of ours. -- [moq-dev/moq#8830](https://github.com/moq-dev/moq/pull/8830) is open on - the TS export; it does not touch these signatures. - -Tests: the existing TS import tests assert on variants where they assert -on message text today; a multi-program input returns the variant from -`decode`; a source switch returns `Some(Restart)` and the importer keeps -decoding the next source. - -Public API: breaking for Rust: the return types of `ts::Import`, -`ts::Programs`, and `ts::Republish`; `ts::MultipleProgramsError` becomes a -variant; `moq_mux::Error::Other` is removed. Wire: none. CLI: messages may -change wording, exit codes do not. - -## Related - -- [A multi-program TS import survives a source switch](/quest/m5/ts-multi-program-restart.md) - deferred; would add a restart value to `Programs` -- [Finishing an importer or the video producer returns a Finished handle](/quest/m2/finish-sweep.md) - the other change to importer signatures diff --git a/quest/m3/rtsp-final-error.md b/quest/m3/rtsp-final-error.md new file mode 100644 index 00000000000..bf419599a0d --- /dev/null +++ b/quest/m3/rtsp-final-error.md @@ -0,0 +1,27 @@ +# [S] An RTSP failure says whether redialing can fix it + +## Goal + +`moq import rtsp` stops redialing a camera that refused access or offers +nothing publishable, without matching `moq_rtsp::Error` variants. moq-rtsp +reports whether a failure is final, and the CLI's redial loop +(`rs/moq-cli/src/rtsp.rs`) asks it rather than listing variants itself, per +AGENTS.md's "never match on errors". Which failures redial stays as today. + +## Plan + +Decided 2026-11-16 (maintainer, on +[moq-dev/moq#8939](https://github.com/moq-dev/moq/pull/8939)): the CLI's +`retries` helper, which matches `Unauthorized` and `NoSupportedStream`, goes. + +- Add `moq_rtsp::Error::is_final`, shaped like `moq_auth::Error::is_final_grant` + (from [moq-dev/moq#8933](https://github.com/moq-dev/moq/pull/8933)): the one + list of failures that asking again cannot fix, kept next to the variants so + a new one is classified where it is added. It lists `Unauthorized`, + `NoSupportedStream` and `InvalidUrl`: no redial fixes a URL, though + `Client::new` already refuses one before the loop, so the loop's behavior + does not change. +- The CLI redials on `!err.is_final()`, and its `session_failures_retry` test + moves to moq-rtsp as a test of `is_final`. + +Public API: additive on moq-rtsp (`Error::is_final`). Wire: none. diff --git a/quest/m5/ts-multi-program-restart.md b/quest/m5/ts-multi-program-restart.md index 4487bcd914a..325adedcc3f 100644 --- a/quest/m5/ts-multi-program-restart.md +++ b/quest/m5/ts-multi-program-restart.md @@ -40,8 +40,13 @@ import. rules for PAT changes that aren't a source switch (a moved PMT PID, a program added or removed) as they are. -[Typed TS outcomes](/quest/m2/ts-typed-outcomes.md) makes `Import`'s restart -a returned value; give `Programs` the same shape, not a second one. - -Public API: possibly additive on `Programs` (a restart result like -`Import`'s). Wire: none. +The restart stays out of the public API (maintainer, 2026-11-16, on +[moq-dev/moq#8939](https://github.com/moq-dev/moq/pull/8939)): the public +`ts::Import::decode` fails at a step with `ts::Error::Restarted`, and only +`ts::Republish` sees the restart as a value, through the `pub(super)` +`Import::decode_or_restart`. Give `Programs` the same shape, not a second one: +it reads the restart through a private method and follows it itself, never by +matching the error. + +Public API: possibly additive on `Programs` (a report of each program it +republished, like `ts::Republished`). Wire: none. diff --git a/rs/moq-cli/src/publish.rs b/rs/moq-cli/src/publish.rs index b35afacfd3b..9a96708ef53 100644 --- a/rs/moq-cli/src/publish.rs +++ b/rs/moq-cli/src/publish.rs @@ -294,11 +294,12 @@ impl FinishedDecoder { } /// Point a multi-program refusal at the flag that resolves it. -fn suggest_program(err: anyhow::Error) -> anyhow::Error { - if err.is::() { - err.context("choose one with `--program `, or publish each with `--program all`") - } else { - err +fn suggest_program(err: ts::Error) -> anyhow::Error { + let multiplex = matches!(err, ts::Error::MultiplePrograms { .. }); + let err = anyhow::Error::from(err); + match multiplex { + true => err.context("choose one with `--program `, or publish each with `--program all`"), + false => err, } } diff --git a/rs/moq-cli/src/rtsp.rs b/rs/moq-cli/src/rtsp.rs index 40cba3436df..b6659e3d937 100644 --- a/rs/moq-cli/src/rtsp.rs +++ b/rs/moq-cli/src/rtsp.rs @@ -46,8 +46,9 @@ pub async fn connect_import(target: ImportTarget, url: Url) -> anyhow::Result<() Ok(moq_rtsp::End::Jumped) if published => continue, Ok(moq_rtsp::End::Jumped) => tracing::warn!(%url, "RTSP timeline jumped before publishing"), Ok(_) => tracing::info!(%url, "RTSP session ended"), - Err(err) if is_fatal(&err) => return Err(err), - Err(err) => tracing::warn!(%url, "RTSP session failed: {err:#}"), + Err(Failure::Session(err)) if retries(&err) => tracing::warn!(%url, "RTSP session failed: {err}"), + Err(Failure::Session(err)) => return Err(err.into()), + Err(Failure::Local(err)) => return Err(err), } tracing::info!(%url, ?backoff, "RTSP reconnecting"); @@ -56,24 +57,41 @@ pub async fn connect_import(target: ImportTarget, url: Url) -> anyhow::Result<() } } -/// Retrying cannot fix these: refused access, nothing publishable, or a local failure (such as -/// an invalid broadcast name) rather than one from the RTSP session. -fn is_fatal(err: &anyhow::Error) -> bool { +/// Why a run failed: its RTSP session, or something local such as an invalid broadcast name, which +/// a redial cannot fix. +enum Failure { + Session(moq_rtsp::Error), + Local(anyhow::Error), +} + +impl std::fmt::Display for Failure { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Session(err) => write!(f, "{err}"), + Self::Local(err) => write!(f, "{err:#}"), + } + } +} + +/// Whether redialing can fix a session failure: anything but refused access or nothing publishable. +fn retries(err: &moq_rtsp::Error) -> bool { !matches!( - err.downcast_ref::(), - Some(err) if !matches!(err, moq_rtsp::Error::Unauthorized(_) | moq_rtsp::Error::NoSupportedStream(_)) + err, + moq_rtsp::Error::Unauthorized(_) | moq_rtsp::Error::NoSupportedStream(_) ) } /// Publish one session as one broadcast, announced once its catalog names a track, setting /// `published` then. -async fn run(target: &ImportTarget, client: &moq_rtsp::Client, published: &mut bool) -> anyhow::Result { +async fn run(target: &ImportTarget, client: &moq_rtsp::Client, published: &mut bool) -> Result { // Each run is a new route under a fresh instance, so a redial replaces the last run. let route = target .origin .route(&target.name, moq_net::origin::RouteOptions::hidden()) - .context("failed to create broadcast")?; - let mut catalog = moq_mux::catalog::Producer::new(&mut route.broadcast().clone(), target.ingest.catalog())?; + .context("failed to create broadcast") + .map_err(Failure::Local)?; + let mut catalog = moq_mux::catalog::Producer::new(&mut route.broadcast().clone(), target.ingest.catalog()) + .map_err(|err| Failure::Local(err.into()))?; // A relay asks for a broadcast's tracks the moment it is announced, so wait for the // first keyframe to put them in the catalog. @@ -90,8 +108,8 @@ async fn run(target: &ImportTarget, client: &moq_rtsp::Client, published: &mut b }; let end = tokio::select! { - end = client.pull(route.broadcast(), &catalog) => end.map_err(anyhow::Error::from), - err = announce => err, + end = client.pull(route.broadcast(), &catalog) => end.map_err(Failure::Session), + err = announce => err.map_err(Failure::Local), }; // Closed at once even after a clean end: the pull redials and publishes the next run under a // new instance, so a run's end is not the content's, and a retained run's finished catalog @@ -99,12 +117,12 @@ async fn run(target: &ImportTarget, client: &moq_rtsp::Client, published: &mut b let finished = match &end { Ok(_) => catalog.finish(), Err(err) => { - catalog.abort(moq_net::Error::Transport(format!("{err:#}"))); + catalog.abort(moq_net::Error::Transport(err.to_string())); Ok(()) } }; route.broadcast().close(); - finished?; + finished.map_err(|err| Failure::Local(err.into()))?; end } @@ -112,14 +130,13 @@ async fn run(target: &ImportTarget, client: &moq_rtsp::Client, published: &mut b mod tests { use super::*; - /// Only a failure of the RTSP session itself is worth redialing. + /// A session failure is worth redialing unless the server refused access or offers nothing + /// publishable. #[test] - fn only_session_failures_retry() { - let retry = |err: anyhow::Error| !is_fatal(&err); - assert!(retry(moq_rtsp::Error::Rtsp("connection reset".into()).into())); - assert!(retry(moq_rtsp::Error::Stalled(Duration::from_secs(10)).into())); - assert!(!retry(moq_rtsp::Error::Unauthorized("401".into()).into())); - assert!(!retry(moq_rtsp::Error::NoSupportedStream("audio/pcmu".into()).into())); - assert!(!retry(anyhow::anyhow!("failed to create broadcast"))); + fn session_failures_retry() { + assert!(retries(&moq_rtsp::Error::Rtsp("connection reset".into()))); + assert!(retries(&moq_rtsp::Error::Stalled(Duration::from_secs(10)))); + assert!(!retries(&moq_rtsp::Error::Unauthorized("401".into()))); + assert!(!retries(&moq_rtsp::Error::NoSupportedStream("audio/pcmu".into()))); } } diff --git a/rs/moq-cli/src/srt.rs b/rs/moq-cli/src/srt.rs index 8ea5f8b50ee..3fbf9f75b84 100644 --- a/rs/moq-cli/src/srt.rs +++ b/rs/moq-cli/src/srt.rs @@ -77,8 +77,12 @@ impl ImportArgs { /// Point a multi-program refusal at the flag that resolves it. fn suggest_program(err: moq_srt::Error) -> anyhow::Error { - let multiplex = matches!(&err, moq_srt::Error::Mux(moq_mux::Error::Other(inner)) - if inner.is::()); + let multiplex = matches!( + err, + moq_srt::Error::Mux(moq_mux::Error::Ts( + moq_mux::container::ts::Error::MultiplePrograms { .. } + )) + ); let err = anyhow::Error::from(err); if multiplex { err.context("choose one with `--program `, or publish each with `--program all`") diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index 1810f50783e..9ee90acce09 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -25,7 +25,6 @@ async = ["kio/async", "moq-net/async"] fuzz = [] [dependencies] -anyhow = { workspace = true } base64 = { workspace = true } bitstream-io = { workspace = true } bytes = { workspace = true } @@ -55,6 +54,7 @@ url = { workspace = true } webm-iterable = "0.7" [dev-dependencies] +anyhow = { workspace = true } criterion = { package = "bench-harness", path = "../bench-harness" } futures = { workspace = true } # The tests await hang's catalog subscription. diff --git a/rs/moq-mux/src/codec/aac/adts.rs b/rs/moq-mux/src/codec/aac/adts.rs index 57e11967d89..558dac9aa2a 100644 --- a/rs/moq-mux/src/codec/aac/adts.rs +++ b/rs/moq-mux/src/codec/aac/adts.rs @@ -5,8 +5,6 @@ //! AudioSpecificConfig, so import strips ADTS headers (and synthesizes a //! config from the first one) while export re-frames raw AAC into ADTS. -use anyhow::Context; - use super::SAMPLE_RATES; /// An invalid ADTS header or frame. @@ -28,6 +26,18 @@ pub enum Error { /// A packet does not contain exactly one complete ADTS frame. #[error("ADTS packet must contain exactly one complete frame")] PacketLength, + /// An audioObjectType outside the four ADTS's 2-bit profile can carry. + #[error("audioObjectType {0} not representable in ADTS")] + ObjectType(u8), + /// A sample rate outside ADTS's table, which has no explicit-rate escape. + #[error("sample rate {0} not representable in ADTS")] + Rate(u32), + /// A channelConfiguration past ADTS's 3 bits. + #[error("channelConfiguration {0} not representable in ADTS")] + ChannelConfig(u8), + /// A frame past ADTS's 13-bit frame length. + #[error("AAC frame of {0} bytes too large for ADTS framing")] + TooLarge(usize), } /// Bytes needed to attempt a header parse: the fixed header without a CRC. @@ -105,22 +115,22 @@ pub(crate) fn write_header( sample_rate: u32, channel_config: u8, raw_len: usize, -) -> anyhow::Result<[u8; 7]> { +) -> Result<[u8; 7], Error> { // ADTS `profile` is the 2-bit audioObjectType - 1, so only Main, LC, SSR, and LTP fit. - anyhow::ensure!( - (1..=4).contains(&object_type), - "audioObjectType {object_type} not representable in ADTS" - ); + if !(1..=4).contains(&object_type) { + return Err(Error::ObjectType(object_type)); + } let profile = object_type - 1; let freq_index = freq_index_from_rate(sample_rate)?; // ADTS has 3 bits for it; the higher configurations only fit an AudioSpecificConfig. - anyhow::ensure!( - channel_config < 8, - "channelConfiguration {channel_config} not representable in ADTS" - ); + if channel_config >= 8 { + return Err(Error::ChannelConfig(channel_config)); + } let frame_len = raw_len + 7; - anyhow::ensure!(frame_len < (1 << 13), "AAC frame too large for ADTS framing"); + if frame_len >= 1 << 13 { + return Err(Error::TooLarge(frame_len)); + } let mut h = [0u8; 7]; h[0] = 0xFF; @@ -135,14 +145,14 @@ pub(crate) fn write_header( Ok(h) } -fn freq_index_from_rate(sample_rate: u32) -> anyhow::Result { +fn freq_index_from_rate(sample_rate: u32) -> Result { // ADTS has no explicit-rate escape (unlike AudioSpecificConfig), so a rate // outside the table can't be represented. Fail rather than mislabel it. SAMPLE_RATES .iter() .position(|&r| r == sample_rate) .map(|i| i as u8) - .with_context(|| format!("sample rate {sample_rate} not representable in ADTS")) + .ok_or(Error::Rate(sample_rate)) } #[cfg(test)] diff --git a/rs/moq-mux/src/codec/aac/mod.rs b/rs/moq-mux/src/codec/aac/mod.rs index d36c38ebea2..6f26edebdc1 100644 --- a/rs/moq-mux/src/codec/aac/mod.rs +++ b/rs/moq-mux/src/codec/aac/mod.rs @@ -77,6 +77,10 @@ pub enum Error { /// Whole bytes follow the AudioSpecificConfig's last field. #[error("{0} bytes after the AudioSpecificConfig")] TrailingBytes(usize), + + /// An ADTS header or frame is invalid, or cannot carry the frame. + #[error("adts: {0}")] + Adts(#[from] adts::Error), } /// A result using this module's error type. diff --git a/rs/moq-mux/src/codec/annexb.rs b/rs/moq-mux/src/codec/annexb.rs index 3b22261b234..c5b413dbc07 100644 --- a/rs/moq-mux/src/codec/annexb.rs +++ b/rs/moq-mux/src/codec/annexb.rs @@ -546,7 +546,7 @@ impl<'a, T: Buf + AsRef<[u8]> + 'a> Iterator for NalIterator<'a, T> { /// by a `length_size`-byte big-endian length) into Annex-B by replacing every /// length prefix with a 4-byte start code. This is the inverse of the /// length-prefixing done by [`crate::codec::h264::Avc1`] / [`crate::codec::h265::Hvc1`]. -pub fn length_prefixed_to_annexb(data: &[u8], length_size: usize, out: &mut Vec) -> anyhow::Result<()> { +pub fn length_prefixed_to_annexb(data: &[u8], length_size: usize, out: &mut Vec) -> Result<()> { for nal in length_prefixed_nals(data, length_size)? { out.extend_from_slice(&START_CODE); out.extend_from_slice(nal?); diff --git a/rs/moq-mux/src/codec/video.rs b/rs/moq-mux/src/codec/video.rs index d73b8ba9c3c..81697a25890 100644 --- a/rs/moq-mux/src/codec/video.rs +++ b/rs/moq-mux/src/codec/video.rs @@ -180,14 +180,6 @@ pub(crate) struct Hrd { pub cpb_size: u64, } -/// A sequence header that reorders deeper than the track already declared. -#[derive(Debug, thiserror::Error)] -#[error("video reorder depth {reorder} exceeds the {declared} the track declared")] -struct Deepened { - declared: u8, - reorder: u8, -} - /// The catalog-publishing state a video importer overlays onto every config it resolves. /// /// Holds the caller's hint and the last config published (to dedupe re-publishes). Not generic @@ -265,7 +257,7 @@ impl Catalog { if let Some(declared) = declared { match config.reorder { Some(reorder) if reorder > declared => { - return Err(anyhow::Error::new(Deepened { declared, reorder }).into()); + return Err(crate::container::ReorderError::Deepened { declared, reorder }.into()); } _ => config.reorder = Some(declared), } diff --git a/rs/moq-mux/src/container/declared.rs b/rs/moq-mux/src/container/declared.rs index 4d614c535ee..c4348598968 100644 --- a/rs/moq-mux/src/container/declared.rs +++ b/rs/moq-mux/src/container/declared.rs @@ -12,7 +12,7 @@ pub(crate) fn depth(config: &VideoConfig) -> crate::Result> { }; let max = crate::codec::video::max_reorder(config); if u32::from(reorder) > max { - return Err(ClockError::Depth { + return Err(ReorderError::Depth { reorder, max, codec: config.codec.to_string(), @@ -89,21 +89,21 @@ impl DeclaredClock { /// Hold the group's next frame in decode order, presented at `pts` ticks, read from `source`. pub(crate) fn push(&mut self, item: T, pts: u64, source: moq_net::Timestamp) -> crate::Result<()> { if self.held.len() > self.depth { - return Err(ClockError::Undrained.into()); + return Err(ReorderError::Undrained.into()); } let source = source.as_nanos()?; // Only `highest` matters: a frame below all of it reorders past the depth. let mut pts = pts; for &(tick, prior) in &self.highest { if prior < source && tick >= pts { - pts = tick.checked_add(1).ok_or(ClockError::Tie)?; + pts = tick.checked_add(1).ok_or(ReorderError::Tie)?; } } if (self.highest.iter()).any(|&(tick, prior)| prior >= source && tick <= pts) { - return Err(ClockError::Tie.into()); + return Err(ReorderError::Tie.into()); } if self.reorders_past(pts) { - return Err(ClockError::Reordered(self.depth).into()); + return Err(ReorderError::Reordered(self.depth).into()); } let at = self.highest.partition_point(|&(prior, _)| prior <= pts); self.highest.insert(at, (pts, source)); @@ -133,33 +133,50 @@ impl DeclaredClock { }; let (item, pts) = self.held.pop_front().expect("the held queue is nonempty"); if dts > i128::from(pts) || self.last.is_some_and(|last| dts <= last) { - return Err(ClockError::Presentation.into()); + return Err(ReorderError::Presentation.into()); } self.last = Some(dts); Ok(Some((item, dts, pts))) } } -#[derive(Debug, thiserror::Error)] -enum ClockError { +/// Video whose frames do not fit the reorder depth it declares, so no decode clock can be +/// authored for it. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +#[non_exhaustive] +pub enum ReorderError { + /// The declared depth is deeper than the codec and level allow. #[error("video reorder depth {reorder} exceeds the {max} that {codec} can reach")] - Depth { reorder: u8, max: u32, codec: String }, + Depth { + /// The declared depth. + reorder: u8, + /// The deepest the codec and level allow. + max: u32, + /// The codec string. + codec: String, + }, + /// A sequence header declares a deeper reorder than the track already declared. + #[error("video reorder depth {reorder} exceeds the {declared} the track declared")] + Deepened { + /// The depth the track declared. + declared: u8, + /// The deeper depth the sequence header declares. + reorder: u8, + }, + /// A frame presents earlier than the declared depth lets it. #[error("video reorders past its declared depth {0}")] Reordered(usize), + /// The presentation slots leave no decode time at or before a frame's presentation. #[error("video presentation slots cannot author increasing DTS at or before PTS")] Presentation, + /// Two frames present at one timestamp. #[error("video presents two frames at one timestamp")] Tie, + /// A frame was read before the clock released the ones ahead of it. #[error("the decode clock must be drained before reading another frame")] Undrained, } -impl From for crate::Error { - fn from(err: ClockError) -> Self { - anyhow::Error::new(err).into() - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/rs/moq-mux/src/container/flv/import.rs b/rs/moq-mux/src/container/flv/import.rs index 6f90e553453..bb8db3c8f63 100644 --- a/rs/moq-mux/src/container/flv/import.rs +++ b/rs/moq-mux/src/container/flv/import.rs @@ -22,8 +22,17 @@ use std::collections::BTreeMap; -use anyhow::Context; use bytes::{Buf, Bytes, BytesMut}; + +/// Return [`Malformed`](super::Error::Malformed), formatted, unless `cond` holds. +macro_rules! ensure { + ($cond:expr, $($arg:tt)*) => { + if !$cond { + return Err(super::Error::Malformed(format!($($arg)*)).into()); + } + }; +} + use hang::catalog::{AAC, AudioCodec, AudioConfig, VideoConfig}; use super::{ @@ -152,23 +161,23 @@ impl Import { self.input.check()?; self.buffer.extend_from_slice(data); - self.drain().map_err(typed) + self.drain() } - fn drain(&mut self) -> anyhow::Result<()> { + fn drain(&mut self) -> crate::Result<()> { if !self.header_seen { if self.buffer.len() < FILE_HEADER_LEN { return Ok(()); } - anyhow::ensure!(&self.buffer[0..3] == b"FLV", "not an FLV stream"); + ensure!(&self.buffer[0..3] == b"FLV", "not an FLV stream"); // data_offset is where the body starts (>= 9). It's followed by the // 4-byte PreviousTagSize0 before the first tag. let data_offset = u32::from_be_bytes([self.buffer[5], self.buffer[6], self.buffer[7], self.buffer[8]]) as usize; - anyhow::ensure!(data_offset >= FILE_HEADER_LEN, "invalid FLV data offset"); + ensure!(data_offset >= FILE_HEADER_LEN, "invalid FLV data offset"); // The header is tiny in practice (9 bytes). Cap it so a crafted offset // can't force unbounded buffering before the first tag is reached. - anyhow::ensure!(data_offset <= MAX_DATA_OFFSET, "FLV data offset too large"); + ensure!(data_offset <= MAX_DATA_OFFSET, "FLV data offset too large"); if self.buffer.len() < data_offset + PREV_TAG_SIZE_LEN { return Ok(()); } @@ -207,7 +216,7 @@ impl Import { Ok(()) } - fn handle_video(&mut self, body: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn handle_video(&mut self, body: &[u8], timestamp: u64) -> crate::Result<()> { let Some(&first) = body.first() else { return Ok(()); }; @@ -220,10 +229,10 @@ impl Import { let frame_type = first >> 4; let codec_id = first & 0x0f; if codec_id != VIDEO_CODEC_AVC { - return Err(crate::Error::UnknownFormat(format!("FLV video CodecID {codec_id}")).into()); + return Err(crate::Error::UnknownFormat(format!("FLV video CodecID {codec_id}"))); } - anyhow::ensure!(body.len() >= 5, "AVC video tag too short"); + ensure!(body.len() >= 5, "AVC video tag too short"); let avc_packet_type = body[1]; let composition_time = read_i24(&body[2..5]); let data = &body[5..]; @@ -243,7 +252,7 @@ impl Import { } /// Handle an enhanced-RTMP (FourCC) video tag, single-track or multitrack. - fn handle_video_enhanced(&mut self, first: u8, body: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn handle_video_enhanced(&mut self, first: u8, body: &[u8], timestamp: u64) -> crate::Result<()> { let keyframe = (first >> 4) & 0x07 == FRAME_TYPE_KEY; let packet_type = first & 0x0f; @@ -251,7 +260,7 @@ impl Import { return self.handle_video_multitrack(&body[1..], keyframe, timestamp); } - anyhow::ensure!(body.len() >= 5, "enhanced video tag too short for FourCC"); + ensure!(body.len() >= 5, "enhanced video tag too short for FourCC"); let fourcc: [u8; 4] = body[1..5].try_into().expect("slice is 4 bytes"); self.handle_video_track(DEFAULT_TRACK_ID, &fourcc, packet_type, keyframe, &body[5..], timestamp) } @@ -260,10 +269,10 @@ impl Import { /// [`handle_video_track`](Self::handle_video_track). /// /// `data` starts at the multitrack framing byte (after the ex-header byte). - fn handle_video_multitrack(&mut self, data: &[u8], keyframe: bool, timestamp: u64) -> anyhow::Result<()> { + fn handle_video_multitrack(&mut self, data: &[u8], keyframe: bool, timestamp: u64) -> crate::Result<()> { let mut cursor = data; let header = split_multitrack_header(&mut cursor, "video")?; - anyhow::ensure!( + ensure!( header.packet_type != VIDEO_PACKET_MULTITRACK, "nested multitrack video tag" ); @@ -295,9 +304,9 @@ impl Import { keyframe: bool, payload: &[u8], timestamp: u64, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { if !matches!(fourcc, b"avc1" | b"hvc1" | b"av01" | b"vp09") { - return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {fourcc:?}")).into()); + return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {fourcc:?}"))); } match packet_type { VIDEO_PACKET_SEQUENCE_START => { @@ -309,7 +318,7 @@ impl Import { // redundant with the key-frame header we configure from. b"vp09" => return Ok(()), other => { - return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {other:?}")).into()); + return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {other:?}"))); } }; self.init_video(track_id, config) @@ -319,7 +328,7 @@ impl Import { // and the always-zero-offset av01/vp09 do not. let has_cts = packet_type == VIDEO_PACKET_CODED_FRAMES && matches!(fourcc, b"hvc1" | b"avc1"); let (data, cts) = if has_cts { - anyhow::ensure!(payload.len() >= 3, "enhanced CodedFrames missing composition time"); + ensure!(payload.len() >= 3, "enhanced CodedFrames missing composition time"); (&payload[3..], read_i24(&payload[0..3])) } else { (payload, 0) @@ -347,7 +356,7 @@ impl Import { } } - fn handle_audio(&mut self, body: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn handle_audio(&mut self, body: &[u8], timestamp: u64) -> crate::Result<()> { let Some(&first) = body.first() else { return Ok(()); }; @@ -364,10 +373,12 @@ impl Import { return self.write_audio(DEFAULT_TRACK_ID, frame, timestamp); } if sound_format != AUDIO_FORMAT_AAC { - return Err(crate::Error::UnknownFormat(format!("FLV audio SoundFormat {sound_format}")).into()); + return Err(crate::Error::UnknownFormat(format!( + "FLV audio SoundFormat {sound_format}" + ))); } - anyhow::ensure!(body.len() >= 2, "AAC audio tag too short"); + ensure!(body.len() >= 2, "AAC audio tag too short"); let aac_packet_type = body[1]; let data = &body[2..]; @@ -379,24 +390,24 @@ impl Import { } /// Handle an enhanced-RTMP (FourCC) audio tag, single-track or multitrack. - fn handle_audio_enhanced(&mut self, first: u8, body: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn handle_audio_enhanced(&mut self, first: u8, body: &[u8], timestamp: u64) -> crate::Result<()> { let packet_type = first & 0x0f; if packet_type == AUDIO_PACKET_MULTITRACK { return self.handle_audio_multitrack(&body[1..], timestamp); } - anyhow::ensure!(body.len() >= 5, "enhanced audio tag too short for FourCC"); + ensure!(body.len() >= 5, "enhanced audio tag too short for FourCC"); let fourcc: [u8; 4] = body[1..5].try_into().expect("slice is 4 bytes"); self.handle_audio_track(DEFAULT_TRACK_ID, &fourcc, packet_type, &body[5..], timestamp) } /// Split a multitrack audio tag into its per-track payloads and route each to /// [`handle_audio_track`](Self::handle_audio_track). - fn handle_audio_multitrack(&mut self, data: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn handle_audio_multitrack(&mut self, data: &[u8], timestamp: u64) -> crate::Result<()> { let mut cursor = data; let header = split_multitrack_header(&mut cursor, "audio")?; - anyhow::ensure!( + ensure!( header.packet_type != AUDIO_PACKET_MULTITRACK, "nested multitrack audio tag" ); @@ -426,9 +437,9 @@ impl Import { packet_type: u8, payload: &[u8], timestamp: u64, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { if !matches!(fourcc, b"Opus" | b"mp4a" | b".mp3" | b"ac-3" | b"ec-3") { - return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {fourcc:?}")).into()); + return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {fourcc:?}"))); } match packet_type { AUDIO_PACKET_SEQUENCE_START => { @@ -438,7 +449,7 @@ impl Import { // MP3 / AC-3 / E-AC-3 are verbatim with no sequence header; they // configure from the first frame. Anything else is unsupported. other => { - return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {other:?}")).into()); + return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {other:?}"))); } }; self.init_audio(track_id, config) @@ -474,15 +485,14 @@ impl Import { dts: u64, composition_time: i32, keyframe: bool, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { // FLV stores DTS in the tag; PTS is DTS plus the composition offset. let pts_ms = (dts as i64) + (composition_time as i64); if pts_ms < 0 { return Err(crate::Error::NegativeFlvPts { dts_ms: dts, composition_time_ms: composition_time, - } - .into()); + }); } if !self.video.contains_key(&track_id) { tracing::debug!("video frame before sequence header, dropping"); @@ -508,7 +518,7 @@ impl Import { } /// Write one audio frame as its own group, so the relay can forward it immediately. - fn write_audio(&mut self, track_id: u8, data: &[u8], timestamp: u64) -> anyhow::Result<()> { + fn write_audio(&mut self, track_id: u8, data: &[u8], timestamp: u64) -> crate::Result<()> { if !self.audio.contains_key(&track_id) { tracing::debug!("audio frame before config, dropping"); return Ok(()); @@ -533,20 +543,17 @@ impl Import { } /// Refuse all later input when publishing failed, so the caller can tell it from a bad tag. - fn refuse(&mut self, result: crate::Result) -> anyhow::Result { - result.map_err(|err| { - self.input = crate::container::Input::Refused(err.clone()); - err.into() - }) + fn refuse(&mut self, result: crate::Result) -> crate::Result { + result.inspect_err(|err| self.input = crate::container::Input::Refused(err.clone())) } /// (Re)build the video track `track_id` for `config`, unless it matches the current one. - fn init_video(&mut self, track_id: u8, config: VideoConfig) -> anyhow::Result<()> { - let result = self.build_video(track_id, config).map_err(typed); + fn init_video(&mut self, track_id: u8, config: VideoConfig) -> crate::Result<()> { + let result = self.build_video(track_id, config); self.refuse(result) } - fn build_video(&mut self, track_id: u8, mut config: VideoConfig) -> anyhow::Result<()> { + fn build_video(&mut self, track_id: u8, mut config: VideoConfig) -> crate::Result<()> { config.container = self.container.clone(); if self.video.get(&track_id).is_some_and(|s| s.config == config) { return Ok(()); @@ -574,12 +581,12 @@ impl Import { } /// (Re)build the audio track `track_id` for `config`, unless it matches the current one. - fn init_audio(&mut self, track_id: u8, config: AudioConfig) -> anyhow::Result<()> { - let result = self.build_audio(track_id, config).map_err(typed); + fn init_audio(&mut self, track_id: u8, config: AudioConfig) -> crate::Result<()> { + let result = self.build_audio(track_id, config); self.refuse(result) } - fn build_audio(&mut self, track_id: u8, mut config: AudioConfig) -> anyhow::Result<()> { + fn build_audio(&mut self, track_id: u8, mut config: AudioConfig) -> crate::Result<()> { config.container = self.container.clone(); if self.audio.get(&track_id).is_some_and(|s| s.config == config) { return Ok(()); @@ -599,7 +606,7 @@ impl Import { } /// Retire any existing video track `track_id` and allocate a fresh one. - fn replace_video(&mut self, track_id: u8) -> anyhow::Result { + fn replace_video(&mut self, track_id: u8) -> crate::Result { if let Some(old) = self.video.remove(&track_id) { self.retire(old.track)?; } @@ -609,7 +616,7 @@ impl Import { } /// Retire any existing audio track `track_id` and allocate a fresh one. - fn replace_audio(&mut self, track_id: u8) -> anyhow::Result { + fn replace_audio(&mut self, track_id: u8) -> crate::Result { if let Some(old) = self.audio.remove(&track_id) { self.retire(old.track)?; } @@ -623,8 +630,8 @@ impl Import { fn retire( &self, mut track: crate::container::Producer, - ) -> anyhow::Result<()> { - Ok(track.end()?) + ) -> crate::Result<()> { + track.end() } /// Close the current group on every track and reopen at `start`. @@ -745,14 +752,14 @@ struct MultitrackTrack<'a> { /// Parse the multitrack framing byte and shared FourCC, advancing `cursor` past /// them. `cursor` starts at the framing byte (after the ex-header byte). -fn split_multitrack_header(cursor: &mut &[u8], kind: &str) -> anyhow::Result { +fn split_multitrack_header(cursor: &mut &[u8], kind: &str) -> crate::Result { let (&framing, rest) = cursor .split_first() - .with_context(|| format!("multitrack {kind} tag missing framing byte"))?; + .ok_or_else(|| super::Error::Malformed(format!("multitrack {kind} tag missing framing byte")))?; *cursor = rest; let multitrack_type = framing >> 4; let packet_type = framing & 0x0f; - anyhow::ensure!( + ensure!( matches!( multitrack_type, MULTITRACK_ONE_TRACK | MULTITRACK_MANY_TRACKS | MULTITRACK_MANY_TRACKS_MANY_CODECS @@ -763,7 +770,10 @@ fn split_multitrack_header(cursor: &mut &[u8], kind: &str) -> anyhow::Result( cursor: &mut &'a [u8], header: &MultitrackHeader, kind: &str, -) -> anyhow::Result> { +) -> crate::Result> { let fourcc = match header.shared_fourcc { Some(fourcc) => fourcc, - None => take_fourcc(cursor).with_context(|| format!("multitrack {kind} missing per-track FourCC"))?, + None => take_fourcc(cursor) + .ok_or_else(|| super::Error::Malformed(format!("multitrack {kind} missing per-track FourCC")))?, }; let (&track_id, rest) = cursor .split_first() - .with_context(|| format!("multitrack {kind} missing track id"))?; + .ok_or_else(|| super::Error::Malformed(format!("multitrack {kind} missing track id")))?; *cursor = rest; let payload = if header.multitrack_type == MULTITRACK_ONE_TRACK { std::mem::take(cursor) } else { - anyhow::ensure!(cursor.len() >= 3, "multitrack {kind} missing track size"); + ensure!(cursor.len() >= 3, "multitrack {kind} missing track size"); let size = read_u24(&cursor[0..3]) as usize; *cursor = &cursor[3..]; - anyhow::ensure!( + ensure!( cursor.len() >= size, "multitrack {kind} track size {size} exceeds remaining {}", cursor.len() @@ -824,7 +835,7 @@ fn take_fourcc(cursor: &mut &[u8]) -> Option<[u8; 4]> { } /// Build an audio config for AAC from an `AudioSpecificConfig`. -fn config_from_asc(asc_bytes: &[u8]) -> anyhow::Result { +fn config_from_asc(asc_bytes: &[u8]) -> crate::Result { let mut cursor = asc_bytes; let cfg = crate::codec::aac::Config::parse(&mut cursor)?; let mut config = AudioConfig::new(AAC { profile: cfg.profile }, cfg.sample_rate, cfg.channel_count); @@ -833,13 +844,13 @@ fn config_from_asc(asc_bytes: &[u8]) -> anyhow::Result { } /// Build an audio config for MP3 from a frame header (config is in band). -fn config_from_mp3(frame: &[u8]) -> anyhow::Result { +fn config_from_mp3(frame: &[u8]) -> crate::Result { let cfg = crate::codec::mp3::Config::parse(frame)?; Ok(AudioConfig::new(AudioCodec::Mp3, cfg.sample_rate, cfg.channel_count)) } /// Build an audio config for AC-3 from a sync frame header. -fn config_from_ac3(frame: &[u8]) -> anyhow::Result { +fn config_from_ac3(frame: &[u8]) -> crate::Result { let header = crate::codec::ac3::parse_header(frame)?; Ok(AudioConfig::new( AudioCodec::Ac3, @@ -849,7 +860,7 @@ fn config_from_ac3(frame: &[u8]) -> anyhow::Result { } /// Build an audio config for E-AC-3 from a sync frame header. -fn config_from_eac3(frame: &[u8]) -> anyhow::Result { +fn config_from_eac3(frame: &[u8]) -> crate::Result { let header = crate::codec::eac3::parse_header(frame)?; Ok(AudioConfig::new( AudioCodec::Ec3, @@ -857,8 +868,3 @@ fn config_from_eac3(frame: &[u8]) -> anyhow::Result { header.channel_count, )) } - -/// The typed error inside `err`, or `err` wrapped as [`Other`](crate::Error::Other). -fn typed(err: anyhow::Error) -> crate::Error { - err.downcast().unwrap_or_else(crate::Error::from) -} diff --git a/rs/moq-mux/src/container/flv/mod.rs b/rs/moq-mux/src/container/flv/mod.rs index b543b26637b..3573d4b364c 100644 --- a/rs/moq-mux/src/container/flv/mod.rs +++ b/rs/moq-mux/src/container/flv/mod.rs @@ -26,7 +26,7 @@ mod import; pub use export::*; pub use import::*; -/// FLV export errors. +/// FLV import and export errors. #[derive(Debug, Clone, thiserror::Error)] #[non_exhaustive] pub enum Error { @@ -73,6 +73,11 @@ pub enum Error { /// The audio codec has no FLV or enhanced-RTMP mapping. #[error("FLV export does not support audio codec {0}")] UnsupportedAudioCodec(String), + + /// The FLV stream an import reads breaks the format, or uses a codec or layout it does not + /// carry. + #[error("{0}")] + Malformed(String), } #[cfg(test)] diff --git a/rs/moq-mux/src/container/mkv/import.rs b/rs/moq-mux/src/container/mkv/import.rs index 89dd5d29fc1..46f0a6e8539 100644 --- a/rs/moq-mux/src/container/mkv/import.rs +++ b/rs/moq-mux/src/container/mkv/import.rs @@ -737,7 +737,7 @@ impl Import { }; chunk = rest; block.remaining -= 1; - let Some(sizes) = lacing.push(byte, block.remaining).map_err(anyhow::Error::new)? else { + let Some(sizes) = lacing.push(byte, block.remaining)? else { continue; }; let fixed = lacing.block; @@ -783,11 +783,7 @@ impl Import { .ok_or(Error::TimestampOverflow)?; let duration = match laced { - true => Some( - track - .default_duration - .ok_or_else(|| anyhow::Error::new(LacingError::MissingDuration))?, - ), + true => Some(track.default_duration.ok_or(Error::LacingDuration)?), false => None, }; // Validate the last timestamp before publishing any part of this block. @@ -871,7 +867,7 @@ impl Import { } .into()); } - BlockState::Lacing(_) => return Err(anyhow::Error::new(LacingError::Invalid).into()), + BlockState::Lacing(_) => return Err(Error::InvalidLacing.into()), BlockState::Frames(frames) => { // The frame sizes add up to the body, so every frame was written as its bytes arrived. debug_assert!(frames.open.is_none() && frames.index == frames.sizes.len()); @@ -1502,21 +1498,13 @@ fn build_av1_config(codec_private: Option<&Bytes>) -> Result { Ok(config) } -#[derive(Debug, thiserror::Error)] -enum LacingError { - #[error("invalid MKV lacing")] - Invalid, - #[error("MKV lacing requires a positive TrackEntry DefaultDuration")] - MissingDuration, -} - impl Lacing { /// Take the next lace header byte, returning every frame's size once the header ends. /// `remaining` counts the body bytes after `byte`; the last frame takes what the others leave. // webm-iterable's splitter uses unchecked indexing and size arithmetic on malformed laces, so // sizes and signed EBML deltas stay checked here. - fn push(&mut self, byte: u8, remaining: u64) -> std::result::Result>, LacingError> { - let invalid = LacingError::Invalid; + fn push(&mut self, byte: u8, remaining: u64) -> std::result::Result>, Error> { + let invalid = Error::InvalidLacing; let Some(count) = self.count else { let count = usize::from(byte) + 1; if count < 2 { @@ -1573,8 +1561,8 @@ impl Lacing { .sizes .iter() .try_fold(0u64, |sum, size| sum.checked_add(*size)) - .ok_or(LacingError::Invalid)?; - let last = remaining.checked_sub(sum).ok_or(LacingError::Invalid)?; + .ok_or(Error::InvalidLacing)?; + let last = remaining.checked_sub(sum).ok_or(Error::InvalidLacing)?; let mut sizes = std::mem::take(&mut self.sizes); sizes.push(last); Ok(Some(sizes)) diff --git a/rs/moq-mux/src/container/mkv/mod.rs b/rs/moq-mux/src/container/mkv/mod.rs index 3ff33d7c681..e48757945aa 100644 --- a/rs/moq-mux/src/container/mkv/mod.rs +++ b/rs/moq-mux/src/container/mkv/mod.rs @@ -63,6 +63,14 @@ pub enum Error { #[error("invalid Block payload")] InvalidBlock, + /// A laced Block's lace sizes do not fit its payload. + #[error("invalid MKV lacing")] + InvalidLacing, + + /// A laced Block's track has no DefaultDuration to time the frames after its first. + #[error("MKV lacing requires a positive TrackEntry DefaultDuration")] + LacingDuration, + /// The block timestamp precedes the start of the timeline. #[error("negative block timestamp")] NegativeBlockTimestamp, diff --git a/rs/moq-mux/src/container/mod.rs b/rs/moq-mux/src/container/mod.rs index 7b50b537f77..02c14ffbaed 100644 --- a/rs/moq-mux/src/container/mod.rs +++ b/rs/moq-mux/src/container/mod.rs @@ -14,6 +14,7 @@ use bytes::Bytes; mod consumer; mod declared; +pub use declared::ReorderError; mod decode; mod group; mod input; diff --git a/rs/moq-mux/src/container/ts/damage.rs b/rs/moq-mux/src/container/ts/damage.rs index 5f165d49f5f..6cdc4ce46b0 100644 --- a/rs/moq-mux/src/container/ts/damage.rs +++ b/rs/moq-mux/src/container/ts/damage.rs @@ -27,7 +27,7 @@ impl Burst { } impl Log { - pub fn record(&mut self, pid: u16, error: &anyhow::Error) { + pub fn record(&mut self, pid: u16, error: &super::Error) { let now = kio::clock::now(); self.flush(now); let first = match self.bursts.entry(pid) { @@ -88,7 +88,7 @@ mod tests { reason = "the benchmark measures host CPU time" )] async fn benchmark_damage_pid_fanout() { - let error = anyhow::anyhow!("broken PES"); + let error = super::super::Error::Malformed("broken PES".into()); println!("pids,damaged_units,ns_per_unit"); for pids in [1_u16, 64, 8192] { for units in [1, 16, 128] { @@ -117,7 +117,7 @@ mod tests { #[tracing_test::traced_test] async fn a_quiet_pid_starts_a_new_burst_between_summary_ticks() { let mut log = Log::default(); - let error = anyhow::anyhow!("broken PES"); + let error = super::super::Error::Malformed("broken PES".into()); log.record(97, &error); tokio::time::advance(stats::Log::INTERVAL / 2).await; log.record(97, &error); @@ -143,7 +143,7 @@ mod tests { #[tracing_test::traced_test] async fn a_continuous_burst_logs_one_summary_per_interval() { let mut log = Log::default(); - let error = anyhow::anyhow!("broken PES"); + let error = super::super::Error::Malformed("broken PES".into()); log.record(97, &error); for _ in 0..4 { tokio::time::advance(stats::Log::INTERVAL / 2).await; diff --git a/rs/moq-mux/src/container/ts/error.rs b/rs/moq-mux/src/container/ts/error.rs new file mode 100644 index 00000000000..2bc36e7d104 --- /dev/null +++ b/rs/moq-mux/src/container/ts/error.rs @@ -0,0 +1,174 @@ +/// Why a TS import or export stopped. +/// +/// A damaged packet, PES, or access unit is not one: the importer counts it in its +/// [`stats`](super::stats) and carries on. +#[derive(Clone, Debug, thiserror::Error)] +#[non_exhaustive] +pub enum Error { + /// A PAT listed more than one program, and the import was not told which one to take. + /// + /// Importing them all onto one broadcast would put unrelated clocks on one timeline, so the + /// import stops instead. Pick one with [`Import::with_program`](super::Import::with_program), or + /// import each as its own broadcast with [`Programs`](super::Programs). + #[error("transport stream carries {} programs ({})", .programs.len(), list(.programs))] + MultiplePrograms { + /// The program numbers the PAT lists, in PAT order. + programs: Vec, + }, + + /// The PAT does not list the program the import was told to take. + #[error("transport stream has no program {program}; its PAT lists {}", list(.programs))] + MissingProgram { + /// The selected program number. + program: u16, + /// The program numbers the PAT lists, in PAT order. + programs: Vec, + }, + + /// The source's clock stepped onto another timeline, or its PAT named another program, so + /// what follows is another broadcast. [`Republish`](super::Republish) carries on into a new + /// instance instead. + #[error("the source {0}; what follows is a new broadcast")] + Restarted(super::Step), + + /// The input ended before it declared anything to publish. + #[error("input ended before {0}")] + Empty(&'static str), + + /// The input breaks the MPEG-TS or a codec's rules past what the import or export recovers + /// from: a TS byte stream for an import, a broadcast's catalog and frames for an export. + #[error("{0}")] + Malformed(String), + + /// The input is well-formed but needs what this import or export does not carry, such as a + /// codec, a layout change, or more buffer or rate than its configuration allows. + #[error("{0}")] + Unsupported(String), + + /// Parsing the media or publishing it failed. + #[error(transparent)] + Mux(Box), +} + +/// A result whose failure is a TS [`Error`]. +pub(super) type Result = std::result::Result; + +fn list(programs: &[u16]) -> String { + programs.iter().map(u16::to_string).collect::>().join(", ") +} + +impl From for Error { + /// A TS error that crossed into [`crate::Error`] comes back out as itself. + fn from(err: crate::Error) -> Self { + match err { + crate::Error::Ts(err) => err, + err => Self::Mux(Box::new(err)), + } + } +} + +impl From for Error { + fn from(err: super::schedule::Error) -> Self { + match err { + super::schedule::Error::Unset => Self::Malformed(err.to_string()), + err => Self::Unsupported(err.to_string()), + } + } +} + +// Flattened to its message so `mpeg2ts` stays out of this crate's public API. The export meets it +// only on a value out of a field's range. +impl From for Error { + fn from(err: mpeg2ts::Error) -> Self { + Self::Malformed(crate::error::message(err)) + } +} + +/// Every error a TS function's `?` meets on its way to [`Error::Mux`]. +macro_rules! mux_errors { + ($($ty:ty),* $(,)?) => { + $( + impl From<$ty> for Error { + fn from(err: $ty) -> Self { + crate::Error::from(err).into() + } + } + )* + }; +} + +mux_errors!( + moq_net::Error, + moq_net::TimeOverflow, + hang::Error, + crate::codec::aac::Error, + crate::codec::aac::adts::Error, + crate::codec::annexb::Error, + crate::codec::h264::Error, + crate::codec::h265::Error, + crate::codec::legacy::Error, + crate::codec::opus::Error, + crate::container::MissingKeyframe, + crate::container::TimestampRewind, + crate::jitter::Error, +); + +/// [`Error::Malformed`], formatted. +macro_rules! malformed { + ($($arg:tt)*) => { + $crate::container::ts::Error::Malformed(format!($($arg)*)) + }; +} + +/// [`Error::Unsupported`], formatted. +macro_rules! unsupported { + ($($arg:tt)*) => { + $crate::container::ts::Error::Unsupported(format!($($arg)*)) + }; +} + +/// Return [`Error::Malformed`], formatted. +macro_rules! bail { + ($($arg:tt)*) => { + return Err($crate::container::ts::error::malformed!($($arg)*).into()) + }; +} + +/// Return [`Error::Malformed`], formatted, unless `cond` holds. +macro_rules! ensure { + ($cond:expr, $($arg:tt)*) => { + if !$cond { + $crate::container::ts::error::bail!($($arg)*); + } + }; +} + +/// Return [`Error::Unsupported`], formatted. +macro_rules! bail_unsupported { + ($($arg:tt)*) => { + return Err($crate::container::ts::error::unsupported!($($arg)*).into()) + }; +} + +/// Return [`Error::Unsupported`], formatted, unless `cond` holds. +macro_rules! ensure_supported { + ($cond:expr, $($arg:tt)*) => { + if !$cond { + $crate::container::ts::error::bail_unsupported!($($arg)*); + } + }; +} + +pub(super) use {bail, bail_unsupported, ensure, ensure_supported, malformed, unsupported}; + +/// A missing value as [`Error::Malformed`]. +pub(super) trait Context { + /// `msg` when the value is missing. + fn context(self, msg: &str) -> Result; +} + +impl Context for Option { + fn context(self, msg: &str) -> Result { + self.ok_or_else(|| Error::Malformed(msg.to_string())) + } +} diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index a377c5cc1cc..7e99802604f 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -26,7 +26,6 @@ use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use std::task::Poll; use std::time::Duration; -use anyhow::Context; use bytes::Bytes; use hang::catalog::{AudioCodec, AudioConfig, Container, VideoCodec, VideoConfig}; use mpeg2ts::es::StreamId; @@ -42,6 +41,7 @@ use moq_net::Timestamp; use rustc_hash::FxHashMap; use super::catalog::Catalog as _; +use super::error::{Context, Error, Result, bail_unsupported, ensure, ensure_supported, malformed, unsupported}; use crate::catalog::Stream; use crate::catalog::hang::Catalog; use crate::codec::annexb::InBandCodec; @@ -460,12 +460,7 @@ impl Track { /// The decode clock runs here, as frames are read, because the deadline is on decode time: /// reordered video arrives in decode order with PTS 0, 120, 40, 80, so a PTS deadline would /// strand a B-frame behind its reference or send it first. - fn queue( - &mut self, - name: &str, - pending: Pending, - jitter: &mut jitter::Buffer, - ) -> anyhow::Result<()> { + fn queue(&mut self, name: &str, pending: Pending, jitter: &mut jitter::Buffer) -> Result<()> { if pending.restart != self.restart { // A marker declared a break in the timeline, or it stepped back unmarked // ([`Self::steps_back`]), so the decode clock restarts too, and the last keyframe, @@ -792,7 +787,7 @@ impl Track { decode: Timestamp, delay: Duration, frame: &Frame, - ) -> anyhow::Result<(Timestamp, Option)> { + ) -> Result<(Timestamp, Option)> { let lead = delay.saturating_sub(self.ahead(delay)); let key = Duration::try_from(decode)? .checked_add(lead) @@ -820,12 +815,7 @@ impl Track { /// silence: the jitter buffer drops them, where their own arrival would queue them behind a /// schedule that has already gone past them. Nothing settles the frames still held when the /// track ends, so they keep their own arrival and go out late if they must. - fn release( - &mut self, - name: &str, - jitter: &mut jitter::Buffer, - flush: bool, - ) -> anyhow::Result<()> { + fn release(&mut self, name: &str, jitter: &mut jitter::Buffer, flush: bool) -> Result<()> { if let Some(mut clock) = self.declared.take() { let released = (|| { while let Some((item, dts, pts)) = clock.pop(flush)? { @@ -835,7 +825,8 @@ impl Track { // A tie the clock moved presents on the tick it moved to: the least microsecond // that `to_ticks` reads back as that tick. if to_ticks(pending.frame.timestamp) != pts { - let micros = u64::try_from((u128::from(pts) * 100).div_ceil(9))?; + let micros = + u64::try_from((u128::from(pts) * 100).div_ceil(9)).map_err(|_| moq_net::TimeOverflow)?; pending.frame.timestamp = Timestamp::from_micros(micros)?; } if self.read == Read::Live { @@ -843,7 +834,7 @@ impl Track { } self.push(name, pending, Some(dts), description, jitter)?; } - anyhow::Ok(()) + Ok::<_, Error>(()) })(); self.declared = Some(clock); released?; @@ -880,7 +871,7 @@ impl Track { dts: Option, description: Option, jitter: &mut jitter::Buffer, - ) -> anyhow::Result<()> { + ) -> Result<()> { let Pending { frame, restart, @@ -1196,7 +1187,7 @@ impl Psi { /// The tables as they stand, listing the network entry when `network`. Each table that /// differs from the last laid goes out at the version after that one's own, `first` (the PMT's /// and the PAT's) before any has: the PAT changes only with its network entry or PMT PID. - fn current(&self, network: bool, first: (VersionNumber, VersionNumber)) -> anyhow::Result { + fn current(&self, network: bool, first: (VersionNumber, VersionNumber)) -> Result { let next = |mut version: VersionNumber| { version.increment(); version @@ -1248,7 +1239,7 @@ fn encode_pat( pmt_pid: u16, network: bool, version: VersionNumber, -) -> anyhow::Result { +) -> Result { let network = network.then(|| ProgramAssociation { program_num: 0, program_map_pid: Pid::new(NIT_PID).expect("a valid PID"), @@ -1921,9 +1912,7 @@ where "the jitter buffer released an older generation after a newer one" ); if ready.track.program != self.program.id { - return Poll::Ready(Err( - anyhow::anyhow!("a queued frame outlived its publisher program").into() - )); + return Poll::Ready(Err(malformed!("a queued frame outlived its publisher program").into())); } let name = ready.track.name.clone(); let kind = self.program.tracks.get(&name).context("missing track")?.kind.clone(); @@ -2087,7 +2076,7 @@ where /// The trailing SI frame for end of stream. Ordinary tables repeat once; clock /// values keep their stamped placement and never re-assert an already emitted time. - fn write_si_tail(&mut self) -> anyhow::Result> { + fn write_si_tail(&mut self) -> Result> { if self.program.psi.is_none() { return Ok(None); } @@ -2416,7 +2405,7 @@ where Ok(()) } - fn update_catalog(&mut self, mut catalog: Catalog) -> anyhow::Result<()> { + fn update_catalog(&mut self, mut catalog: Catalog) -> Result<()> { // The MPEG-TS section lives in the extension. The trait only exposes // `mpegts_mut`, and this snapshot is owned, so clone it out (`()` yields the // empty default: no verbatim streams, no preserved PIDs/descriptors). @@ -2727,7 +2716,7 @@ where /// stale, silent on its PID. The new subscription's playhead jumped from wherever the /// old one stopped, so its first frame counts as a skip: late frames drop, and a leap /// ahead opens a new generation. - fn resubscribe(&mut self, catalog: &Catalog, mpegts: &catalog::Mpegts) -> anyhow::Result<()> { + fn resubscribe(&mut self, catalog: &Catalog, mpegts: &catalog::Mpegts) -> Result<()> { let budget = self.budget(); for (name, track) in self.program.tracks.iter_mut() { if track.read != Read::Stale { @@ -2785,7 +2774,7 @@ where /// its configuration. Video parameter sets ride with each frame and may change. The new /// source continues the old one ([`ExportSource::continuing`]), and the frames the old one /// left held for their DTS go out first. - fn rebind(&mut self, catalog: &Catalog) -> anyhow::Result<()> { + fn rebind(&mut self, catalog: &Catalog) -> Result<()> { let budget = self.budget(); for (name, track) in self.program.tracks.iter_mut().filter(|(_, track)| track.listed) { let (source, kind) = if let Some(config) = catalog.video.renditions.get(name) { @@ -2820,7 +2809,7 @@ where (Kind::Aac { config, .. }, Kind::Aac { config: old, .. }) => config == old, (kind, old) => kind == old, }; - anyhow::ensure!( + ensure_supported!( same, "TS track layout changed after PAT/PMT was emitted: '{name}' moved to a track announced differently" ); @@ -3251,7 +3240,7 @@ where /// gives a stream or SI, the clock's own PID, or no PID at all is refused, as is a catalog /// that gives the PID to a stream while the move waits. A catalog that records no PID keeps /// the PMT where it is. - fn move_pmt(&mut self, mpegts: &catalog::Mpegts) -> anyhow::Result<()> { + fn move_pmt(&mut self, mpegts: &catalog::Mpegts) -> Result<()> { let Some(pid) = self.program.program.as_ref().map(|s| s.pmt_pid).filter(|&pid| pid != 0) else { // The catalog no longer asks for a move, so drop one that is still waiting. if let Some(psi) = self.program.psi.as_mut() { @@ -3267,7 +3256,7 @@ where return Ok(()); } let psi = &*psi; - anyhow::ensure!(pid < 0x1fff, "TS export cannot move the PMT to PID {pid:#06x}"); + ensure_supported!(pid < 0x1fff, "TS export cannot move the PMT to PID {pid:#06x}"); let taken = psi.own_clock() == Some(pid) || mpegts.si.contains_key(&pid) || self @@ -3275,7 +3264,7 @@ where .tracks .values() .any(|track| track.cataloged && track.pid == pid); - anyhow::ensure!( + ensure_supported!( !taken, "TS export cannot move the PMT from PID {:#06x} to {pid:#06x}, which another stream rides", psi.pmt_pid @@ -3287,7 +3276,7 @@ where /// Move the PMT to the PID the catalog moved it to once no copy of the tables is on its way, /// so each copy goes out whole on the PID its PAT names, and no stream is left on the PID, /// and lead the next frame with them. Every copy not yet begun takes the move. - fn follow_pmt(&mut self) -> anyhow::Result<()> { + fn follow_pmt(&mut self) -> Result<()> { let Some(psi) = self.program.psi.as_mut() else { return Ok(()); }; @@ -3309,7 +3298,7 @@ where } /// Build the PAT/PMT once every track's PID and codec is known. - fn build_psi(&mut self) -> anyhow::Result<()> { + fn build_psi(&mut self) -> Result<()> { for track in self.program.tracks.values_mut() { track.listed = true; } @@ -3359,7 +3348,7 @@ where /// The PID to carry the clock: the first video track's, else the first audio track's, else /// one used by nothing else, since sparse data cannot carry the clock's cadence. - fn pick_clock(&self) -> anyhow::Result { + fn pick_clock(&self) -> Result { let mut tracks: Vec<&Track> = self.program.tracks.values().filter(|t| t.listed).collect(); tracks.sort_by_key(|t| t.pid); tracks @@ -3389,7 +3378,7 @@ where } /// The PMT listing every listed track in PID order, with the clock on `pcr_pid`. - fn program_map(&self, pcr_pid: u16) -> anyhow::Result { + fn program_map(&self, pcr_pid: u16) -> Result { let mut tracks: Vec<&Track> = self.program.tracks.values().filter(|t| t.listed).collect(); tracks.sort_by_key(|t| t.pid); @@ -3407,7 +3396,7 @@ where descriptors, }) }) - .collect::>>()?; + .collect::>>()?; // Re-emit the captured program-level descriptors. With none (a non-TS source), // derive the SCTE-35 'CUEI' registration when a 0x86 verbatim stream is present. @@ -3444,7 +3433,7 @@ where /// Rebuild the PMT from the listed tracks, leading the next frame muxed when it changed. The /// clock stays on its PID unless the stream carrying it retired. - fn revise(&mut self) -> anyhow::Result<()> { + fn revise(&mut self) -> Result<()> { let Some(psi) = self.program.psi.as_ref() else { return Ok(()); }; @@ -3472,7 +3461,7 @@ where /// List `name` in the PMT ahead of its first frame: `false` while it waits for its PID, /// which a stream the catalog dropped still holds. That stream is no longer read, so only /// what it already holds drains ahead of this one ([`Self::settle`]). - fn list(&mut self, name: &str) -> anyhow::Result { + fn list(&mut self, name: &str) -> Result { let track = self.program.tracks.get(name).context("missing track")?; if track.listed { return Ok(true); @@ -3502,7 +3491,7 @@ where /// Retire each track the catalog dropped once nothing of it is left to go out, and list /// the tracks that waited for its PID ([`Self::list`]). - fn settle(&mut self) -> anyhow::Result<()> { + fn settle(&mut self) -> Result<()> { if self.program.psi.is_none() { return Ok(()); } @@ -3577,7 +3566,7 @@ where } /// Hand the program tables to the schedule ahead of the next unit, if they are due. - fn lead_tables(&mut self) -> anyhow::Result<()> { + fn lead_tables(&mut self) -> Result<()> { if !std::mem::take(&mut self.program.psi_due) { return Ok(()); } @@ -3604,7 +3593,7 @@ where /// begin lays. A NIT acquired or lost after the tables went out revises the PAT's network /// entry, at the PAT's next version. The first copy after a switch to another /// instance flags the break on each table PID that owes one ([`Self::breaks`]). - fn restate(&mut self) -> anyhow::Result<()> { + fn restate(&mut self) -> Result<()> { let network = self.carries_nit(); let psi = self.program.psi.as_ref().context("PSI not built")?; let tables = psi.current(network, (self.program.version, self.program.pat_version))?; @@ -3615,7 +3604,7 @@ where .into_iter() .filter(|&pid| self.owes_break(pid)) .collect(); - let encode = |flag: &dyn Fn(u16) -> bool| -> anyhow::Result> { + let encode = |flag: &dyn Fn(u16) -> bool| -> Result> { let mut packets = Vec::new(); section_packets(&mut packets, Pid::PAT, &tables.pat, flag(Pid::PAT))?; section_packets(&mut packets, pmt_pid, §ion, flag(pmt_pid))?; @@ -3637,7 +3626,7 @@ where /// Packetize one media frame and queue it on the [`Schedule`], re-emitting PAT/PMT /// before video keyframes (and periodically) so receivers can tune in mid-stream. - fn mux(&mut self, name: &str, queued: Queued) -> anyhow::Result<()> { + fn mux(&mut self, name: &str, queued: Queued) -> Result<()> { let Queued { frame, decode, @@ -3753,17 +3742,12 @@ where /// The bytes ahead of the first end a frame an earlier PES began; they leave with it, /// which only overstates what the buffer holds. A PES without a PTS holds nothing there. /// The frame's `charge` counts once the unit goes out, unless it was dropped as malformed. - fn place( - &mut self, - name: &str, - (frame, charge): (&Frame, schedule::Charge), - tables: Vec, - ) -> anyhow::Result<()> { + fn place(&mut self, name: &str, (frame, charge): (&Frame, schedule::Charge), tables: Vec) -> Result<()> { let track = self.program.tracks.get(name).context("missing track")?; let pes = match track.kind { Kind::PesPacket { stream_type } => Some( PesPacket::parse(&frame.payload, stream_type) - .with_context(|| format!("MPEG-TS track '{name}' carries a malformed PES packet"))?, + .map_err(|err| malformed!("MPEG-TS track '{name}' carries a malformed PES packet: {err}"))?, ), Kind::Verbatim { .. } => None, _ => unreachable!("a placed track"), @@ -3808,7 +3792,8 @@ where out, frame.keyframe, frames, - ) + )?; + Ok(()) } /// Mux an audio frame into `name`'s PES, sending the PES on first if the frame cannot join @@ -3832,7 +3817,7 @@ where (psi, tables): (bool, Vec), (payload, charge): (Vec, schedule::Charge), (samples, rate, start): (u64, u32, Option), - ) -> anyhow::Result<()> { + ) -> Result<()> { let budget = self.budget().as_nanos(); let track = self.program.tracks.get(name).context("missing track")?; let (size, fit) = track.fit(payload.len()); @@ -3874,7 +3859,7 @@ where } /// Send `name`'s packed frames on as one PES. - fn flush(&mut self, name: &str) -> anyhow::Result<()> { + fn flush(&mut self, name: &str) -> Result<()> { let Some(pack) = self.program.tracks.get_mut(name).and_then(|track| track.pack.take()) else { return Ok(()); }; @@ -3896,7 +3881,7 @@ where /// Send on, in decode order, the packed frames of each track for which `done` holds, given /// the track and the slot its first frame is due in. - fn flush_packs(&mut self, done: impl Fn(&Track, u128) -> bool) -> anyhow::Result<()> { + fn flush_packs(&mut self, done: impl Fn(&Track, u128) -> bool) -> Result<()> { let mut due: Vec<(Timestamp, String)> = self .program .tracks @@ -3925,7 +3910,7 @@ where out: Vec, keyframe: bool, frames: Vec<(u128, usize)>, - ) -> anyhow::Result<()> { + ) -> Result<()> { let mut tables = std::mem::take(&mut self.program.tables); let out = match tables.is_empty() { true => out, @@ -3944,7 +3929,7 @@ where && let Some(needs) = track.timing.buffer_delay() { let slots = schedule::slot(needs.as_nanos()); - anyhow::ensure!( + ensure_supported!( slots <= schedule::slot(self.program.delay.as_nanos()), "MPEG-TS video on PID {pid} declares a {needs:.3?} decoder buffer delay (its HRD's CPB over its bit rate), past what a {:?} delay sends it ahead; raise the delay to {:?}", self.program.delay, @@ -4013,7 +3998,7 @@ where /// Lay out every grid slot settled before a frame keyed `key` in the jitter buffer goes /// out, so frames and slots follow each other in the same order however often the /// exporter is polled. - fn lay_before(&mut self, key: u128) -> anyhow::Result<()> { + fn lay_before(&mut self, key: u128) -> Result<()> { while let Some((known, _)) = self.slot_settled().filter(|&(_, settled)| settled < key) { self.lay(Some(known))?; } @@ -4057,7 +4042,7 @@ where } /// Lay out every grid slot whose time has come, and wake when the next one is due. - fn lay_due(&mut self, waiter: &kio::Waiter) -> anyhow::Result<()> { + fn lay_due(&mut self, waiter: &kio::Waiter) -> Result<()> { while let Some((known, at)) = self.slot_due() { if kio::clock::now() < at { let timer = self @@ -4093,7 +4078,7 @@ where /// groomer can only place the clock samples it receives. So the PCR follows its own /// grid instead: slots on the media timeline, shared by every exporter of the /// broadcast, like [`due`]. - fn lay(&mut self, known: Option) -> anyhow::Result<()> { + fn lay(&mut self, known: Option) -> Result<()> { if self.program.psi.is_none() { return Ok(()); } @@ -4177,20 +4162,20 @@ where } /// Packetize a PES payload into 188-byte TS packets. - fn write_pes(&mut self, out: &mut Vec, unit: &PesUnit, payload: &[u8]) -> anyhow::Result<()> { + fn write_pes(&mut self, out: &mut Vec, unit: &PesUnit, payload: &[u8]) -> Result<()> { let flag = self.breaks(unit.pid); pes_packets(out, unit, payload, flag) } /// Packetize `pes`, a whole PES packet, on `pid` unchanged ([`whole_pes_packets`]). - fn write_pes_packet(&mut self, out: &mut Vec, pid: u16, pes: &[u8]) -> anyhow::Result<()> { + fn write_pes_packet(&mut self, out: &mut Vec, pid: u16, pes: &[u8]) -> Result<()> { let flag = self.breaks(pid); whole_pes_packets(out, pid, pes, flag) } /// Packetize a complete PSI or private section verbatim ([`section_packets`]), returning /// whether it did rather than drop the section as malformed. - fn write_section(&mut self, out: &mut Vec, pid: u16, section: &[u8]) -> anyhow::Result { + fn write_section(&mut self, out: &mut Vec, pid: u16, section: &[u8]) -> Result { // The verbatim track is public; a non-importer producer could publish a frame // that isn't a complete section. Drop it (with a warning) rather than emit a // malformed section a downstream demuxer would choke on. One bad section must @@ -4206,13 +4191,13 @@ where } /// Packetize a PES payload into 188-byte TS packets, flagging a break in the first when `flag`. -fn pes_packets(out: &mut Vec, unit: &PesUnit, payload: &[u8], flag: bool) -> anyhow::Result<()> { +fn pes_packets(out: &mut Vec, unit: &PesUnit, payload: &[u8], flag: bool) -> Result<()> { let pts = to_ts_timestamp(unit.timestamp)?; // A reordered video frame carries DTS alongside PTS; else PTS-only. The decode clock // is continuous ticks, so wrap into the 33-bit wire field here, like the PTS. let dts = unit .dts - .map(|t| TsTimestamp::new(t & TS_TIMESTAMP_MASK).map_err(anyhow::Error::msg)) + .map(|t| TsTimestamp::new(t & TS_TIMESTAMP_MASK).map_err(|err| malformed!("{err}"))) .transpose()?; let stream_id = match unit.stream_id { Some(id) => StreamId::new(id), @@ -4261,10 +4246,10 @@ fn pes_packets(out: &mut Vec, unit: &PesUnit, payload: &[u8], flag: bool) -> TsPayload::PesStart(Pes { header: header.clone(), pes_packet_len, - data: TsBytes::new(chunk).map_err(anyhow::Error::msg)?, + data: TsBytes::new(chunk).map_err(|err| malformed!("{err}"))?, }) } else { - TsPayload::PesContinuation(TsBytes::new(chunk).map_err(anyhow::Error::msg)?) + TsPayload::PesContinuation(TsBytes::new(chunk).map_err(|err| malformed!("{err}"))?) }; write_packet(out, unit.pid, adaptation, ts_payload)?; @@ -4282,8 +4267,8 @@ fn pes_packets(out: &mut Vec, unit: &PesUnit, payload: &[u8], flag: bool) -> /// it, flagging a break in the first packet when `flag` and stuffing the last packet's /// adaptation field. The `mpeg2ts` writer would rebuild the PES header instead, so the packets /// are laid out by hand. -fn whole_pes_packets(out: &mut Vec, pid: u16, pes: &[u8], flag: bool) -> anyhow::Result<()> { - anyhow::ensure!(pid < 0x1FFF, "PID out of range: {pid}"); +fn whole_pes_packets(out: &mut Vec, pid: u16, pes: &[u8], flag: bool) -> Result<()> { + ensure!(pid < 0x1FFF, "PID out of range: {pid}"); let mut offset = 0; while offset < pes.len() { let start = offset == 0; @@ -4314,7 +4299,7 @@ fn whole_pes_packets(out: &mut Vec, pid: u16, pes: &[u8], flag: bool) -> any /// when `flag`. The first packet carries the pointer_field plus the section start as a /// `Section` payload (sets the unit-start bit so the receiver finds the pointer_field); /// continuations are `Raw`. The section bytes are opaque, so this round-trips byte-for-byte. -fn section_packets(out: &mut Vec, pid: u16, section: &[u8], flag: bool) -> anyhow::Result<()> { +fn section_packets(out: &mut Vec, pid: u16, section: &[u8], flag: bool) -> Result<()> { let mut offset = 0; let mut first = true; loop { @@ -4328,13 +4313,13 @@ fn section_packets(out: &mut Vec, pid: u16, section: &[u8], flag: bool) -> a offset = take; TsPayload::Section(Section { pointer_field: 0, - data: TsBytes::new(chunk).map_err(anyhow::Error::msg)?, + data: TsBytes::new(chunk).map_err(|err| malformed!("{err}"))?, }) } else { let take = TsBytes::MAX_SIZE.min(section.len() - offset); let chunk = §ion[offset..offset + take]; offset += take; - TsPayload::Raw(TsBytes::new(chunk).map_err(anyhow::Error::msg)?) + TsPayload::Raw(TsBytes::new(chunk).map_err(|err| malformed!("{err}"))?) }; write_packet(out, pid, flag, payload)?; @@ -4353,7 +4338,7 @@ fn write_packet( pid: u16, adaptation_field: Option, payload: TsPayload, -) -> anyhow::Result<()> { +) -> Result<()> { let continuity_counter = ContinuityCounter::default(); let packet = TsPacket { @@ -4369,7 +4354,7 @@ fn write_packet( }; let mut writer = TsPacketWriter::new(out); - writer.write_ts_packet(&packet).map_err(anyhow::Error::msg)?; + writer.write_ts_packet(&packet).map_err(|err| malformed!("{err}"))?; Ok(()) } @@ -4386,8 +4371,8 @@ impl Export> { /// /// There is no payload, so the field's stuffing fills the packet; its continuity counter /// is numbered when it goes out ([`Export::number`]). -fn pcr_packet(pid: u16, pcr: u64) -> anyhow::Result> { - anyhow::ensure!(pid <= 0x1FFF, "PID out of range: {pid}"); +fn pcr_packet(pid: u16, pcr: u64) -> Result> { + ensure!(pid <= 0x1FFF, "PID out of range: {pid}"); let mut p = Vec::with_capacity(TsPacket::SIZE); p.push(0x47); p.push((pid >> 8) as u8); @@ -4425,13 +4410,13 @@ impl PesPacket { /// the start code, a `PES_packet_length` that matches, and a complete optional header /// where the `stream_id` has one (ISO 13818-1 2.4.3.6). Only video may leave the /// length zero, unbounded, as 2.4.3.7 permits. - fn parse(bytes: &[u8], stream_type: u8) -> anyhow::Result { + fn parse(bytes: &[u8], stream_type: u8) -> Result { // The importer refuses with this same check, so a PES it published never fails here. let pes = psi::PesStart::parse(bytes)?; // The clock fields it refuses ride through unchanged. pes.whole(stream_type)?; if let Some(len) = pes.data_len { - anyhow::ensure!( + ensure!( len == pes.data.len(), "PES_packet_length declares {len} payload bytes where the frame holds {}", pes.data.len() @@ -4548,14 +4533,16 @@ fn slot(timestamp: Timestamp, interval: Duration) -> u128 { } /// A repetition slot's boundary as a media timestamp. -fn slot_stamp(index: u128) -> anyhow::Result { +fn slot_stamp(index: u128) -> Result { stamp(index * PCR_INTERVAL.as_micros() * 1_000) } /// A nanosecond position on the media timeline as a microsecond [`Timestamp`], the /// scale the exporter stamps its own output in. -fn stamp(nanos: u128) -> anyhow::Result { - let micros = (nanos / 1_000).try_into().context("media timeline out of range")?; +fn stamp(nanos: u128) -> Result { + let micros = (nanos / 1_000) + .try_into() + .map_err(|_| malformed!("media timeline out of range"))?; Ok(Timestamp::from_micros(micros)?) } @@ -4606,12 +4593,12 @@ fn nanos(timestamp: Timestamp) -> u128 { timestamp.as_nanos().unwrap_or_default() } -fn to_ts_timestamp(timestamp: Timestamp) -> anyhow::Result { +fn to_ts_timestamp(timestamp: Timestamp) -> Result { // Continuous 90 kHz ticks, wrapped into the 33-bit field. - TsTimestamp::new(to_ticks(timestamp) & TS_TIMESTAMP_MASK).map_err(anyhow::Error::msg) + TsTimestamp::new(to_ticks(timestamp) & TS_TIMESTAMP_MASK).map_err(|err| malformed!("{err}")) } -fn video_kind(config: &VideoConfig, name: &str) -> anyhow::Result { +fn video_kind(config: &VideoConfig, name: &str) -> Result { ensure_raw(&config.container, "video", name)?; // Both in-band (avc3/hev1) and out-of-band (avc1/hvc1) are accepted: // ExportSource normalizes both to length-prefixed NALU + avcC/hvcC, and the @@ -4619,7 +4606,7 @@ fn video_kind(config: &VideoConfig, name: &str) -> anyhow::Result { match &config.codec { VideoCodec::H264(_) => Ok(Kind::Video(StreamType::H264)), VideoCodec::H265(_) => Ok(Kind::Video(StreamType::H265)), - other => anyhow::bail!("TS export does not support video codec {other:?} (track '{name}')"), + other => bail_unsupported!("TS export does not support video codec {other:?} (track '{name}')"), } } @@ -4886,7 +4873,7 @@ fn ac3_sync_prefix(bytes: &[u8]) -> bool { /// Build the elementary-stream payload for a frame of `kind`, repeating an AAC program config /// element when `pce`. Video needs the resolved avcC/hvcC to rewrite length-prefixed NALs as /// Annex-B. Section-framed verbatim streams carry no PES payload, so they have none. -fn es_payload(kind: &Kind, description: Option<&Bytes>, frame: &Frame, pce: bool) -> anyhow::Result>> { +fn es_payload(kind: &Kind, description: Option<&Bytes>, frame: &Frame, pce: bool) -> Result>> { Ok(match kind { Kind::Video(stream_type) => Some(video_es_payload(*stream_type, description, frame)?), Kind::Aac { config, .. } => { @@ -5009,12 +4996,12 @@ fn radl(stream_type: StreamType, description: Option<&Bytes>, frame: &Frame) -> /// length-prefixed NALs to start-code-delimited NALs, injecting the parameter /// sets (SPS/PPS, plus VPS for H.265) from the avcC/hvcC after a leading AUD on /// keyframes so a receiver can tune in mid-stream. -fn video_es_payload(stream_type: StreamType, description: Option<&Bytes>, frame: &Frame) -> anyhow::Result> { +fn video_es_payload(stream_type: StreamType, description: Option<&Bytes>, frame: &Frame) -> Result> { let description = description.context("video codec config (avcC/hvcC) not resolved")?; let (codec, (length_size, params)) = match stream_type { StreamType::H264 => (InBandCodec::H264, crate::codec::h264::avcc_params(description)?), StreamType::H265 => (InBandCodec::H265, crate::codec::h265::hvcc_params(description)?), - other => anyhow::bail!("unsupported TS video stream type {other:?}"), + other => bail_unsupported!("unsupported TS video stream type {other:?}"), }; let mut out = Vec::with_capacity(frame.payload.len() + 64); @@ -5036,7 +5023,7 @@ fn video_es_payload(stream_type: StreamType, description: Option<&Bytes>, frame: Ok(out) } -fn audio_kind(config: &AudioConfig, name: &str) -> anyhow::Result { +fn audio_kind(config: &AudioConfig, name: &str) -> Result { ensure_raw(&config.container, "audio", name)?; match &config.codec { AudioCodec::AAC(codec) => { @@ -5064,7 +5051,7 @@ fn audio_kind(config: &AudioConfig, name: &str) -> anyhow::Result { }), AudioCodec::Ac3 => Ok(Kind::Ac3), AudioCodec::Ec3 => Ok(Kind::Eac3), - other => anyhow::bail!("TS export does not support audio codec {other:?} (track '{name}')"), + other => bail_unsupported!("TS export does not support audio codec {other:?} (track '{name}')"), } } @@ -5098,9 +5085,9 @@ fn registration(format: &'static [u8; 4]) -> catalog::Descriptor { /// Family 0, and family 1 with the Vorbis mapping, use that channel count. A missing /// head is mono or stereo only. Every other layout is refused: the descriptor has no /// code for it, and a clamped count would name a layout the packets do not have. -fn opus_channel_code(config: &AudioConfig, name: &str) -> anyhow::Result { +fn opus_channel_code(config: &AudioConfig, name: &str) -> Result { let Some(description) = config.description.as_deref() else { - anyhow::ensure!( + ensure_supported!( matches!(config.channel_count, 1 | 2), "TS export cannot label Opus track '{name}' with {} channels and no OpusHead", config.channel_count @@ -5110,8 +5097,8 @@ fn opus_channel_code(config: &AudioConfig, name: &str) -> anyhow::Result { let mut buf = description; let head = opus::Config::parse(&mut buf) - .map_err(|err| anyhow::anyhow!("TS export cannot read the OpusHead on track '{name}': {err}"))?; - anyhow::ensure!( + .map_err(|err| malformed!("TS export cannot read the OpusHead on track '{name}': {err}"))?; + ensure!( head.channel_count == config.channel_count, "Opus head has {} channels but the catalog declares {} (track '{name}')", head.channel_count, @@ -5121,20 +5108,20 @@ fn opus_channel_code(config: &AudioConfig, name: &str) -> anyhow::Result { if let Some(mapping) = &head.mapping { let channels = mapping.table().len() as u8; let vorbis = opus::Mapping::vorbis(channels).ok(); - anyhow::ensure!( + ensure_supported!( vorbis.as_ref() == Some(mapping), "TS export cannot label Opus track '{name}': channel mapping family {} is not the Vorbis layout", mapping.family() ); } - let code = u8::try_from(head.channel_count).with_context(|| { - format!( + let code = u8::try_from(head.channel_count).map_err(|_| { + unsupported!( "TS export cannot label Opus track '{name}' with {} channels", head.channel_count ) })?; - anyhow::ensure!( + ensure_supported!( (1..=8).contains(&code), "TS export cannot label Opus track '{name}' with {code} channels" ); @@ -5165,9 +5152,9 @@ fn opus_es_payload(packet: &[u8]) -> Vec { } /// Refuse an SI PID that would share a packet stream with PAT, PMT, null, or an ES. -fn reject_colliding_si_pids(mpegts: &catalog::Mpegts, pmt_pid: u16, es_pids: &[u16]) -> anyhow::Result<()> { +fn reject_colliding_si_pids(mpegts: &catalog::Mpegts, pmt_pid: u16, es_pids: &[u16]) -> Result<()> { for pid in mpegts.si.keys() { - anyhow::ensure!( + ensure!( (1..0x1FFF).contains(pid) && *pid != pmt_pid && !es_pids.contains(pid), "mpegts.si PID {pid} collides with PAT, PMT, null, or an elementary stream" ); @@ -5177,13 +5164,13 @@ fn reject_colliding_si_pids(mpegts: &catalog::Mpegts, pmt_pid: u16, es_pids: &[u /// Refuse an `mpegts.tracks` entry that names both carriages, or carries a track the /// catalog also describes as a media rendition: either leaves two ways to write one PID. -fn reject_ambiguous_tracks(catalog: &Catalog, mpegts: &catalog::Mpegts) -> anyhow::Result<()> { +fn reject_ambiguous_tracks(catalog: &Catalog, mpegts: &catalog::Mpegts) -> Result<()> { for (name, track) in &mpegts.tracks { - anyhow::ensure!( + ensure!( track.verbatim.is_none() || track.pes_packet.is_none(), "mpegts track '{name}' names both verbatim and pesPacket carriage" ); - anyhow::ensure!( + ensure!( (track.verbatim.is_none() && track.pes_packet.is_none()) || !(catalog.video.renditions.contains_key(name) || catalog.audio.renditions.contains_key(name)), "mpegts track '{name}' is carried undecoded but also described as a media rendition" @@ -5208,12 +5195,12 @@ fn is_complete_section(section: &[u8]) -> bool { section.len() >= 3 && section.len() == 3 + ((((section[1] & 0x0f) as usize) << 8) | section[2] as usize) } -fn ensure_raw(container: &Container, kind: &str, name: &str) -> anyhow::Result<()> { +fn ensure_raw(container: &Container, kind: &str, name: &str) -> Result<()> { match container { // TS carries raw codec payloads, like the Legacy varint and LOC formats. Container::Legacy | Container::Loc => Ok(()), - Container::Cmaf { .. } => anyhow::bail!("TS export does not support CMAF {kind} track '{name}'"), - Container::Unknown(unknown) => anyhow::bail!( + Container::Cmaf { .. } => bail_unsupported!("TS export does not support CMAF {kind} track '{name}'"), + Container::Unknown(unknown) => bail_unsupported!( "TS export does not support container '{}' on {kind} track '{name}'", unknown.kind() ), diff --git a/rs/moq-mux/src/container/ts/export_timing_test.rs b/rs/moq-mux/src/container/ts/export_timing_test.rs index 0fb4d4522af..bbb00e6678a 100644 --- a/rs/moq-mux/src/container/ts/export_timing_test.rs +++ b/rs/moq-mux/src/container/ts/export_timing_test.rs @@ -476,7 +476,7 @@ impl Leg { self.busy = None; self.woken.0.store(false, Ordering::Relaxed); while let std::task::Poll::Ready(frame) = self.export.poll_next(&self.waiter) { - match frame.expect("exporter error") { + match frame.unwrap_or_else(|err| panic!("exporter error: {err}")) { Some(frame) => { let bits = frame.payload.len() as u64 * 8; self.out.push((now, frame)); diff --git a/rs/moq-mux/src/container/ts/follower.rs b/rs/moq-mux/src/container/ts/follower.rs index 286c0026c2d..30bf2c1a9f8 100644 --- a/rs/moq-mux/src/container/ts/follower.rs +++ b/rs/moq-mux/src/container/ts/follower.rs @@ -6,6 +6,7 @@ use std::time::Duration; use kio::clock::Instant; +use super::error::malformed; use super::{Export, catalog}; use crate::container::Frame; @@ -374,9 +375,7 @@ impl Follower { match missing { Missing::Ended { end, deadline } => State::Settling { end, deadline }, Missing::Switch => State::Settling { - end: Err( - anyhow::anyhow!("replacement broadcast `{}` ended before serving its catalog", self.path).into(), - ), + end: Err(malformed!("replacement broadcast `{}` ended before serving its catalog", self.path).into()), deadline: kio::clock::now().saturating_add(self.follow), }, } diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 2bc3ded962a..4d16f972cfd 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -19,13 +19,13 @@ use std::collections::{BTreeMap, HashMap, HashSet, VecDeque}; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; -use anyhow::Context; use mpeg2ts::es::StreamType; use mpeg2ts::ts::{Pid, TsPacket}; use super::adts; use super::catalog; use super::demux::{Demux, Packet}; +use super::error::{Context, Error, Result, bail, bail_unsupported, ensure, malformed}; use super::health::{Continuation, Continuity, Health}; use super::mux_rate::PCR_WRAP; use super::passthrough::{PCR_HZ, PCR_OFFSET, Sample, locate}; @@ -99,7 +99,7 @@ mod video; /// `mpegts.muxRate` as an update once a two-second PCR window measures a constant rate, which /// a variable-rate source never does. An export that pads waits for that update. A restarted /// import's catalog carries its predecessor's measurement of the same multiplex -/// ([`Restart`](super::Restart)), which the measurement then refines as usual. +/// ([`Republish`](super::Republish)), which the measurement then refines as usual. /// /// The selected container applies only to decoded media renditions. Verbatim tracks in the /// `mpegts` catalog section continue to use the legacy Hang container. @@ -313,7 +313,7 @@ impl Import { /// Import only the program numbered `program` in the PAT, ignoring the rest of the multiplex. /// /// Without this, a PAT that lists more than one program fails the import with - /// [`MultipleProgramsError`] rather than merging them onto one clock. A PAT that does not + /// [`MultiplePrograms`](Error::MultiplePrograms) rather than merging them onto one clock. A PAT that does not /// list `program` fails it too. The SI describes only this program's service: other /// services' EIT actual is dropped and the SDT actual lists this service alone. pub fn with_program(mut self, program: u16) -> Self { @@ -402,28 +402,41 @@ impl Import { /// now completes. The buffer is fully consumed; a trailing partial packet /// (< 188 bytes) is retained for the next call. /// - /// A step of the source's clock onto another timeline ends the instance with - /// [`Restart`](super::Restart): the program clock and every timed stream stepping together - /// without `discontinuity_indicator`, forward by more than 10 s or back, or the streams - /// stepping back after a flagged break. Until the streams have spoken, which a cadenced one - /// does within 0.7 s, the input after a suspected step is held rather than published, up to - /// 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. + /// A step of the source's clock onto another timeline ends the instance, failing with + /// [`Restarted`](Error::Restarted): the program clock and every timed stream stepping + /// together without `discontinuity_indicator`, forward by more than 10 s or back, or the + /// streams stepping back after a flagged break. Until the streams have spoken, which a + /// cadenced one does within 0.7 s, the input after a suspected step is held rather than + /// published, up to half a group's bytes. A PAT naming another `transport_stream_id` or + /// program number restarts too, at that PAT. Every later call fails the same way; + /// [`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. + /// A PAT listing several programs fails with [`MultiplePrograms`](Error::MultiplePrograms) + /// unless [`with_program`](Self::with_program) picked one. Input after + /// [`finish`](Self::finish) is refused with [`Finished`](crate::Error::Finished), or with the + /// cause of a finish that failed. /// - /// [`Republish`](super::Republish) carries on past a restart instead, on a path of an origin. - pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { + /// [`Republish`](super::Republish) carries on past each restart into a new instance, on a + /// path of an origin. + pub fn decode(&mut self, data: &[u8]) -> Result<()> { match self.decode_or_restart(data)? { - Some(restart) => Err(restart.into()), None => Ok(()), + Some(restart) => Err(Error::Restarted(restart.step)), } } - /// [`decode`](Self::decode), returning a restart rather than failing with it. - pub(super) fn decode_or_restart(&mut self, data: &[u8]) -> anyhow::Result> { + /// No input follows: read what a step still being judged held, returning a restart it + /// reaches, as [`decode_or_restart`](Self::decode_or_restart) does. + pub(super) fn settle(&mut self) -> Result> { + match self.steps.settle() { + true => self.decode_or_restart(&[]), + false => Ok(None), + } + } + + /// [`decode`](Self::decode), returning a restart as a value with the input from the step on, + /// for [`Republish`](super::Republish) to read into a new instance. + pub(super) fn decode_or_restart(&mut self, data: &[u8]) -> Result> { self.input.check()?; let mut demux = std::mem::take(&mut self.demux); let result = self.decode_with(&mut demux, data); @@ -431,7 +444,7 @@ impl Import { result } - fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> anyhow::Result> { + fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> Result> { demux.push(data); // Route one whole packet at a time, so a PMT is parsed (and any PID it declares // registered) before the packets that follow it in the same chunk route. @@ -492,14 +505,14 @@ impl Import { /// damage log. Batching per decode call (plus the store's own host-clock debounce) /// coalesces a junction's burst of sub-table commits into few groups instead of one per /// commit. - pub(super) fn flush_batch(&mut self) -> anyhow::Result<()> { + pub(super) fn flush_batch(&mut self) -> Result<()> { self.flush_partial()?; self.damage_log.flush(kio::clock::now()); self.si.flush(self.last_pts.unwrap_or(Timestamp::ZERO), false) } /// Parse available decoded units before their arrival clock advances. - fn flush_partial(&mut self) -> anyhow::Result<()> { + fn flush_partial(&mut self) -> Result<()> { let mut partial = std::mem::take(&mut self.partial); for pid in partial.drain(..) { if !self.streams.get(&pid).is_some_and(Stream::salvages_partial_pes) { @@ -566,7 +579,7 @@ impl Import { /// Route one packet the parser framed. Packets on PIDs this program does not /// [`claim`](Self::claims) may be skipped, except the PAT's and the SI PIDs' whole sections. - pub(super) fn packet(&mut self, packet: &Packet) -> anyhow::Result<()> { + pub(super) fn packet(&mut self, packet: &Packet) -> Result<()> { let Packet { pkt, pid, @@ -604,7 +617,7 @@ impl Import { if matches!(continuation, Some(Continuation::Broken)) { self.salvage(pid)?; } - return self.damage(pid, &anyhow::anyhow!("malformed TS adaptation field")); + return self.damage(pid, &malformed!("malformed TS adaptation field")); } // Read the clock's own flag before routing, so the break lands between the media // either side of it. The PCR PID can be a dedicated one nothing below routes, and @@ -689,7 +702,7 @@ impl Import { // Flagged corrupt, so the packet joins the partial rather than opening a // new PES out of bytes the demodulator already disowned. Continuation::Corrupt => { - self.damage(pid, &anyhow::anyhow!("transport error indicator"))?; + self.damage(pid, &malformed!("transport error indicator"))?; return Ok(()); } Continuation::Broken => { @@ -724,7 +737,7 @@ impl Import { } /// Feed one packet on the PMT PID to its reassembler, applying every good PMT section. - fn pmt_packet(&mut self, pid: Pid, packet: &Packet) -> anyhow::Result<()> { + fn pmt_packet(&mut self, pid: Pid, packet: &Packet) -> Result<()> { let mut sections = Vec::new(); if let Some((_, reassembler)) = &mut self.pmt { reassembler.push(packet.pkt, &mut sections); @@ -748,7 +761,7 @@ impl Import { /// Start a clock a PMT just named from its latest PCR, if one arrived since the program /// last had none, or note from when one may start it. - fn seed_clock(&mut self, packet: &Packet) -> anyhow::Result<()> { + fn seed_clock(&mut self, packet: &Packet) -> Result<()> { match self.pcr_pid { Some(pid) => { if let Some(since) = self.unclocked_since.take() @@ -773,7 +786,7 @@ impl Import { tracing::warn!(pid = pid.as_u16(), "dropped a PSI section with a bad CRC"); } - fn handle_pmt(&mut self, pmt: Pmt) -> anyhow::Result<()> { + fn handle_pmt(&mut self, pmt: Pmt) -> Result<()> { // PMTs of several programs may share one PID; only the chosen one maps streams. if self.program.is_some_and(|program| program != pmt.program_number) { return Ok(()); @@ -900,20 +913,20 @@ impl Import { /// Route one packet on an elementary stream PID, at input offset `offset`, into its PES /// reassembly. - fn pes_packet(&mut self, pid: Pid, pkt: &[u8; TsPacket::SIZE], offset: u64) -> anyhow::Result<()> { + fn pes_packet(&mut self, pid: Pid, pkt: &[u8; TsPacket::SIZE], offset: u64) -> Result<()> { let payload = match payload(pkt) { Payload::None => return Ok(()), Payload::Bytes(payload) => payload, // No payload can be found in it, so like a corrupt packet it breaks the PES. Payload::Malformed => { - return self.damage(pid, &anyhow::anyhow!("malformed TS adaptation field")); + return self.damage(pid, &malformed!("malformed TS adaptation field")); } }; if pkt[1] & 0x40 != 0 { // The new start ends the preceding PES even while its own header is incomplete. self.flush(pid)?; if self.headers.remove(&pid).is_some() { - self.damage(pid, &anyhow::anyhow!("PES header truncated at next start"))?; + self.damage(pid, &malformed!("PES header truncated at next start"))?; } // The payload ends the packet, so this is where its start code sits in the input. let at = offset + (TsPacket::SIZE - payload.len()) as u64; @@ -938,7 +951,7 @@ impl Import { } /// Continue a PES header split across packets, placed by `span` from its start code. - fn pes_header(&mut self, pid: Pid, span: Option, header: Vec) -> anyhow::Result<()> { + fn pes_header(&mut self, pid: Pid, span: Option, header: Vec) -> Result<()> { let len = match PesStart::header_len(&header) { Ok(len) => len, Err(err) => return self.damage(pid, &err), @@ -953,7 +966,7 @@ impl Import { } } - fn ensure_stream(&mut self, pid: Pid, stream_type: u8, descriptors: &[catalog::Descriptor]) -> anyhow::Result<()> { + fn ensure_stream(&mut self, pid: Pid, stream_type: u8, descriptors: &[catalog::Descriptor]) -> Result<()> { let temporal_poc = if stream_type == StreamType::H264 as u8 { video::temporal_poc(descriptors)? } else { @@ -1138,7 +1151,7 @@ impl Import { /// Register a section-framed verbatim PID (SCTE-35 or other private sections): /// intercepted (see [`Self::decode`]) with a verbatim track when the catalog /// carries the `mpegts` section, dropped as `Ignored` when it can't. - fn ensure_section(&mut self, pid: Pid, stream_type: u8, descriptors: &[catalog::Descriptor]) -> anyhow::Result<()> { + fn ensure_section(&mut self, pid: Pid, stream_type: u8, descriptors: &[catalog::Descriptor]) -> Result<()> { if self.supports_mpegts && self.pcr_pid.is_none() { if self.unclocked.get(&pid) != Some(&stream_type) { self.refuse(pid, "the program has no PCR to place it by")?; @@ -1210,9 +1223,9 @@ impl Import { /// ends every route: the PES it began goes out, what waits on the clock is placed from the /// PCRs so far or counted unplaced (refusing the import, as end of input does, if no media /// timestamp anchored it), and its track finishes and leaves the catalog. - fn end_route(&mut self, pid: Pid) -> anyhow::Result<()> { + fn end_route(&mut self, pid: Pid) -> Result<()> { if self.headers.remove(&pid).is_some() { - self.damage(pid, &anyhow::anyhow!("PES header truncated by a PMT revision"))?; + self.damage(pid, &malformed!("PES header truncated by a PMT revision"))?; } self.flush(pid)?; // Released as if its timebase ended here, so a unit no later PCR will bracket for it is @@ -1224,7 +1237,7 @@ impl Import { if let Some(section) = self.sections.get_mut(&pid.as_u16()) { section.settle(&self.placement); (units, unplaced) = section.release(&self.timebase, &self.pts, generation)?; - anyhow::ensure!( + ensure!( section.held.is_empty(), "TS sections reached a PMT revision without a media timestamp" ); @@ -1243,7 +1256,7 @@ impl Import { let (published, dropped) = stream.release(&self.timebase, &self.pts, generation)?; units += published; unplaced += dropped; - anyhow::ensure!( + ensure!( stream.held.is_empty(), "TS private PES reached a PMT revision without a media timestamp" ); @@ -1283,7 +1296,7 @@ impl Import { } /// Open the PES whose header `pes` begins where `span` says, if any PCR came before it. - fn handle_pes_start(&mut self, pid: Pid, pes: PesStart, span: Option) -> anyhow::Result<()> { + fn handle_pes_start(&mut self, pid: Pid, pes: PesStart, span: Option) -> Result<()> { // Finish the preceding PES before accepting another header. if self.pending.contains_key(&pid) { self.flush(pid)?; @@ -1304,7 +1317,7 @@ impl Import { return self.refuse(pid, &why); } if pes.scrambled() { - return self.damage(pid, &anyhow::anyhow!("scrambled PES is not supported")); + return self.damage(pid, &malformed!("scrambled PES is not supported")); } let stream = &self.streams[&pid]; @@ -1392,7 +1405,7 @@ impl Import { Ok(()) } - fn handle_pes_continuation(&mut self, pid: Pid, data: &[u8]) -> anyhow::Result<()> { + fn handle_pes_continuation(&mut self, pid: Pid, data: &[u8]) -> Result<()> { let Some(pending) = self.pending.get_mut(&pid) else { return Ok(()); }; @@ -1423,7 +1436,7 @@ impl Import { } /// Refuse one damaged unit without changing any other PID or the program clock. - fn damage(&mut self, pid: Pid, err: &anyhow::Error) -> anyhow::Result<()> { + fn damage(&mut self, pid: Pid, err: &Error) -> Result<()> { // Complete AUs ahead of the damaged packet remain valid, including those // in an unbounded PES. Its final NAL/AU is never flushed at a loss boundary. if let Some(pending) = self.pending.remove(&pid) @@ -1433,8 +1446,8 @@ impl Import { self.video_damage(pid); let units = match result { Ok(units) => units, - Err(err) if err.is::() => video::published(&err), - Err(err) => return Err(err), + Err(Fault::Damaged { published, .. }) => published, + Err(Fault::Fatal(err)) => return Err(err), }; self.published |= units > 0; self.liveness.delivered(pid.as_u16(), units); @@ -1452,7 +1465,7 @@ impl Import { Ok(()) } - fn flush(&mut self, pid: Pid) -> anyhow::Result<()> { + fn flush(&mut self, pid: Pid) -> Result<()> { let Some(pending) = self.pending.remove(&pid) else { return Ok(()); }; @@ -1460,34 +1473,28 @@ impl Import { self.write_pending(pid, pending) } - fn write_pending(&mut self, pid: Pid, pending: Pending) -> anyhow::Result<()> { + fn write_pending(&mut self, pid: Pid, pending: Pending) -> Result<()> { let batched = self.has_video; let Some(stream) = self.streams.get_mut(&pid) else { return Ok(()); }; let truncated = pending.data_len.is_some_and(|len| pending.data.len() < len); if truncated && !stream.salvages_partial_pes() { - return self.damage( - pid, - &anyhow::anyhow!("PES payload truncated before its declared length"), - ); + return self.damage(pid, &malformed!("PES payload truncated before its declared length")); } let result = stream.write(pending, batched); self.video_damage(pid); let (units, damage) = match result { Ok(units) => (units, None), - Err(err) if err.is::() => (video::published(&err), Some(err)), - Err(err) => return Err(err), + Err(Fault::Damaged { cause, published }) => (published, Some(cause)), + Err(Fault::Fatal(err)) => return Err(err), }; if let Some(err) = damage { self.damage(pid, &err)?; } else if truncated { if matches!(self.streams.get(&pid), Some(Stream::H264 { .. } | Stream::H265 { .. })) { - self.damage( - pid, - &anyhow::anyhow!("PES payload truncated before its declared length"), - )?; + self.damage(pid, &malformed!("PES payload truncated before its declared length"))?; } else { self.streams.get_mut(&pid).expect("existing route").lost()?; } @@ -1505,7 +1512,7 @@ impl Import { /// A PCR at input offset `offset`, its packet's, on the program's clock PID: it places every /// whole PES and section begun since the last, and releases those complete. - fn place_pcr(&mut self, raw: u64, offset: u64) -> anyhow::Result<()> { + fn place_pcr(&mut self, raw: u64, offset: u64) -> Result<()> { if self.placement.rewinds(raw) { // The clock stepped back without saying so: what it placed so far is placed by the // old timebase, and the new one starts here. @@ -1515,7 +1522,7 @@ impl Import { // PCR and PTS share one era. Advance it from PCR while no media can do so, // including across a wrap before a later PMT adds a media stream. self.pts.unwrap(raw / 300); - let ticks = u64::try_from(self.pts.pcr(raw))?; + let ticks = u64::try_from(self.pts.pcr(raw)).map_err(|_| moq_net::TimeOverflow)?; self.timebase.anchor(Timestamp::from_scale(ticks, PCR_HZ)?)?; self.initial_reservation = None; } @@ -1540,7 +1547,7 @@ impl Import { /// End the placement timebase: extrapolate every PES or section begun past its last PCR from its /// last two, and publish what that places. - fn settle_placement(&mut self) -> anyhow::Result<()> { + fn settle_placement(&mut self) -> Result<()> { let spans = self.pending.values_mut().filter_map(|pending| pending.span.as_mut()); for span in spans.chain(self.headers.values_mut().filter_map(|(span, _)| span.as_mut())) { span.settle(&self.placement); @@ -1558,7 +1565,7 @@ impl Import { } /// Publish every held PES or section that is placed, once the timebase has its offset. - fn release_placed(&mut self) -> anyhow::Result<()> { + fn release_placed(&mut self) -> Result<()> { for (pid, stream) in &mut self.streams { let Stream::PesPacket(stream) = stream else { continue; @@ -1583,7 +1590,7 @@ impl Import { /// Refuse the undecoded stream on `pid` rather than carry it altered: its track ends with an /// error and leaves the catalog, and the PID is dropped from here on. - fn refuse(&mut self, pid: Pid, why: &str) -> anyhow::Result<()> { + fn refuse(&mut self, pid: Pid, why: &str) -> Result<()> { self.pending.remove(&pid); self.headers.remove(&pid); if let Some(section) = self.retire_section(pid) { @@ -1607,7 +1614,7 @@ impl Import { /// A TS-scrambled packet on the carried `pid`: refused, and the unit it interrupts lost with /// it. The route stays, so the stream resumes once its packets are clear again. - fn scrambled(&mut self, pid: Pid) -> anyhow::Result<()> { + fn scrambled(&mut self, pid: Pid) -> Result<()> { self.pending.remove(&pid); self.headers.remove(&pid); if let Some(stream) = self.streams.get_mut(&pid) { @@ -1630,7 +1637,7 @@ impl Import { /// The packet chain on `pid` was cut: salvage the truncated PES, and treat the bytes /// after it as [lost](Stream::lost). - fn broken(&mut self, pid: Pid) -> anyhow::Result<()> { + fn broken(&mut self, pid: Pid) -> Result<()> { self.salvage(pid)?; if let Some(stream) = self.streams.get_mut(&pid) { stream.lost()?; @@ -1640,9 +1647,9 @@ impl Import { /// Publish the truncated PES on `pid` only where its bytes stand on their own, and drop /// whatever is left mid-unit. - fn salvage(&mut self, pid: Pid) -> anyhow::Result<()> { + fn salvage(&mut self, pid: Pid) -> Result<()> { if self.headers.remove(&pid).is_some() { - self.damage(pid, &anyhow::anyhow!("PES header truncated by packet loss"))?; + self.damage(pid, &malformed!("PES header truncated by packet loss"))?; } if self.streams.get(&pid).is_some_and(Stream::salvages_partial_pes) { self.flush(pid) @@ -1666,7 +1673,7 @@ impl Import { /// or a gap with no flag at all) loses bytes without moving the clock, and the 33-bit /// PCR/PTS rollover sets nothing and only moves the next PTS into the next era; neither is /// a program break and neither reaches here. - fn timebase_break(&mut self) -> anyhow::Result<()> { + fn timebase_break(&mut self) -> Result<()> { // The bytes still accumulating belong to the old clock, and their continuation is // stamped on the new one, so cut every PES here rather than splicing across. This // runs before the marker so a salvaged tail lands in the closing group, which the @@ -1680,11 +1687,11 @@ impl Import { stream.end(); } self.settle_placement()?; - anyhow::ensure!( + ensure!( self.sections.values().all(|section| section.held.is_empty()), "TS sections reached a clock break without a media timestamp" ); - anyhow::ensure!( + ensure!( self.streams.values().all(Stream::placed), "TS private PES reached a clock break without a media timestamp" ); @@ -1706,7 +1713,7 @@ impl Import { /// The program clock changed, so the measured rate no longer holds. A leading flag starts this /// instance's clock and cannot invalidate a window not begun. - fn reset_mux_rate(&mut self) -> anyhow::Result<()> { + fn reset_mux_rate(&mut self) -> Result<()> { if !self.mux_rate.started() { return Ok(()); } @@ -1719,7 +1726,7 @@ impl Import { /// Copy the measured multiplex rate into the `mpegts` section, publishing the /// catalog. Called only when the measurement changed, so a steady source republishes /// only when a window sets a new high, ever more rarely. - fn record_mux_rate(&mut self) -> anyhow::Result<()> { + fn record_mux_rate(&mut self) -> Result<()> { let rate = self.mux_rate.published(); if let Some(mpegts) = self.catalog.modify()?.ext.mpegts_mut() { mpegts.mux_rate = rate; @@ -1730,7 +1737,7 @@ impl Import { /// Record a decoded media stream's PID and ES descriptors into `mpegts.tracks`, /// once per track. No-op without the `mpegts` section, before the track exists, /// or for verbatim streams (which self-register). - fn record_media_track(&mut self, pid: Pid) -> anyhow::Result<()> { + fn record_media_track(&mut self, pid: Pid) -> Result<()> { if !self.supports_mpegts { return Ok(()); } @@ -1765,7 +1772,7 @@ impl Import { /// selected program that disappears ends a selected one; media already published stays. A /// PAT naming the program under another identity never reaches a [`decode`](Self::decode), /// which restarts at it, so only an import reading one program of several refuses it. - pub(super) fn handle_pat(&mut self, pat: &psi::Pat) -> anyhow::Result<()> { + pub(super) fn handle_pat(&mut self, pat: &psi::Pat) -> Result<()> { let Some((entry, alone)) = select(self.program, pat)? else { return Ok(()); }; @@ -1773,7 +1780,7 @@ impl Import { if let Some((transport_stream_id, program_number)) = self.identity && (transport_stream_id, program_number) != identity { - anyhow::bail!( + bail_unsupported!( "the PAT changed the program's identity from transport_stream_id {transport_stream_id} program {program_number} to transport_stream_id {} program {}, which the carried service information does not describe", identity.0, identity.1 @@ -1805,7 +1812,7 @@ impl Import { /// Capture the transport/service identity (TSID, service number, PMT PID) from the /// PAT into the catalog service record. No-op without `mpegts` support. - fn record_program_identity(&mut self, transport_stream_id: u16, entry: &psi::Association) -> anyhow::Result<()> { + fn record_program_identity(&mut self, transport_stream_id: u16, entry: &psi::Association) -> Result<()> { if !self.supports_mpegts { return Ok(()); } @@ -1819,7 +1826,7 @@ impl Import { } /// Close the current group on every track and reopen at `sequence`. - pub fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + pub fn seek(&mut self, sequence: u64) -> Result<()> { for stream in self.streams.values_mut() { stream.seek(sequence)?; } @@ -1829,7 +1836,7 @@ impl Import { Ok(()) } - fn finish_stream(&mut self, pid: Pid) -> anyhow::Result { + fn finish_stream(&mut self, pid: Pid) -> Result { let Some(stream) = self.streams.get_mut(&pid) else { return Ok(0); }; @@ -1837,23 +1844,30 @@ impl Import { self.video_damage(pid); match result { Ok(units) => Ok(units), - Err(err) if err.is::() => { - let units = video::published(&err); - self.damage(pid, &err)?; + Err(Fault::Damaged { + cause, + published: units, + }) => { + self.damage(pid, &cause)?; self.streams .get_mut(&pid) .expect("damage preserves the route") - .finish()?; + .finish() + .map_err(Fault::fatal)?; Ok(units) } - Err(err) => Err(err), + Err(Fault::Fatal(err)) => Err(err), } } /// Flush any buffered PES, finish every track, and return the [`Finished`] import. /// - /// A finish that fails aborts every track with the cause, so it can't be retried. - pub fn finish(mut self) -> anyhow::Result> { + /// Input held while a step was judged is read first, failing with + /// [`Restarted`](Error::Restarted) if it reaches one, since this import would drop the + /// broadcast after it. A step [`decode`](Self::decode) already reported ends the instance + /// there instead. A finish that fails aborts every track with the cause, so it can't be + /// retried. + pub fn finish(mut self) -> Result> { self.finish_in_place()?; Ok(Finished { import: self }) } @@ -1861,7 +1875,7 @@ impl Import { /// [`finish`](Self::finish) for an owner that keeps the importer, as /// [`Programs`](super::Programs) keeps each program's. A finish that fails returns the cause to /// every later call; one that succeeded does nothing when called again. - pub(super) fn finish_in_place(&mut self) -> anyhow::Result<()> { + pub(super) fn finish_in_place(&mut self) -> Result<()> { match &self.input { crate::container::Input::Open => {} crate::container::Input::Finished => return Ok(()), @@ -1872,22 +1886,22 @@ impl Import { 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:#}"))), + Err(err) => self.fail(err.clone().into()), } 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.steps.settle() { - self.decode(&[])?; + fn end(&mut self) -> Result<()> { + // A step still being judged never completed: what it held routes as read. A step that + // routing reaches fails the finish rather than drop the broadcast after it; `Republish` + // settles first, so it republishes that broadcast instead. + if let Some(restart) = self.settle()? { + return Err(Error::Restarted(restart.step)); } self.demux.finish(); for pid in self.headers.keys().copied().collect::>() { - self.damage(pid, &anyhow::anyhow!("PES header truncated at end of input"))?; + self.damage(pid, &malformed!("PES header truncated at end of input"))?; } let pids: Vec = self.pending.keys().copied().collect(); for pid in pids { @@ -1897,11 +1911,11 @@ impl Import { stream.end(); } self.settle_placement()?; - anyhow::ensure!( + ensure!( self.sections.values().all(|section| section.held.is_empty()), "TS sections reached end of input without a media timestamp" ); - anyhow::ensure!( + ensure!( self.streams.values().all(Stream::placed), "TS private PES reached end of input without a media timestamp" ); @@ -2060,46 +2074,31 @@ impl Drop for Import { } } -/// A PAT listed more than one program, and the [`Import`] was not told which one to take. -/// -/// Importing them all onto one broadcast would put unrelated clocks on one timeline, so the -/// import stops instead. Pick one with [`Import::with_program`], or import each as its own -/// broadcast with [`Programs`](super::Programs). -#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] -#[non_exhaustive] -#[error("transport stream carries {} programs ({})", .programs.len(), list_programs(.programs))] -pub struct MultipleProgramsError { - /// The program numbers the PAT lists, in PAT order. - pub programs: Vec, -} - /// The entry `pat` lists for `program`, or for its only program when none is selected, and /// whether it is the only one. `None` while an unselected PAT lists no program. -fn select(program: Option, pat: &psi::Pat) -> anyhow::Result> { +fn select(program: Option, pat: &psi::Pat) -> Result> { // program_number 0 is the network PID association, not a program. let programs: Vec<_> = pat.programs.iter().filter(|entry| entry.program_number != 0).collect(); let numbers = || programs.iter().map(|entry| entry.program_number).collect::>(); let entry = match program { Some(program) => match programs.iter().find(|entry| entry.program_number == program) { Some(entry) => entry, - None => anyhow::bail!( - "transport stream has no program {program}; its PAT lists {}", - list_programs(&numbers()) - ), + None => { + return Err(Error::MissingProgram { + program, + programs: numbers(), + }); + } }, None => match programs.as_slice() { [] => return Ok(None), [entry] => entry, - _ => return Err(MultipleProgramsError { programs: numbers() }.into()), + _ => return Err(Error::MultiplePrograms { programs: numbers() }), }, }; Ok(Some((**entry, programs.len() == 1))) } -fn list_programs(programs: &[u16]) -> String { - programs.iter().map(u16::to_string).collect::>().join(", ") -} - /// What a PMT says of one elementary stream. A route lasts as long as its PID's listing does. #[derive(Clone, Debug, PartialEq, Eq)] struct Listing { @@ -2144,7 +2143,7 @@ impl Retained { } } - fn grow(&mut self, bytes: usize, units: usize) -> anyhow::Result<()> { + fn grow(&mut self, bytes: usize, units: usize) -> Result<()> { let reserve = |used: &AtomicUsize, add: usize, max: usize| { if add == 0 { return true; @@ -2154,22 +2153,20 @@ impl Retained { }) .is_ok() }; - anyhow::ensure!( + ensure!( reserve(&self.staging.bytes, bytes, STAGING_BYTES_MAX), "TS staging exceeds {STAGING_BYTES_MAX} bytes while waiting for a PES boundary or timestamp" ); if !reserve(&self.staging.units, units, STAGING_UNITS_MAX) { self.staging.bytes.fetch_sub(bytes, Ordering::Relaxed); - anyhow::bail!( - "TS staging exceeds {STAGING_UNITS_MAX} buffered units while waiting for a boundary or timestamp" - ); + bail!("TS staging exceeds {STAGING_UNITS_MAX} buffered units while waiting for a boundary or timestamp"); } self.bytes += bytes; self.units += units; Ok(()) } - fn resize(&mut self, bytes: usize, units: usize) -> anyhow::Result<()> { + fn resize(&mut self, bytes: usize, units: usize) -> Result<()> { self.grow(bytes.saturating_sub(self.bytes), units.saturating_sub(self.units))?; self.staging.bytes.fetch_sub(self.bytes - bytes, Ordering::Relaxed); self.staging.units.fetch_sub(self.units - units, Ordering::Relaxed); @@ -2245,7 +2242,7 @@ fn register_verbatim( broadcast: &mut moq_net::broadcast::Producer, catalog: &mut crate::catalog::Producer, entry: catalog::Track, -) -> anyhow::Result> { +) -> Result> { // Verbatim payloads ride the legacy container, which normalizes the per-frame // timestamp to microseconds on the wire (see `hang::container::Frame::encode`), // so the track declares that timescale to match. @@ -2270,7 +2267,7 @@ fn register_verbatim( let Some(mpegts) = guard.ext.mpegts_mut() else { // supports_mpegts was true when sampled at construction; None here means the // catalog dropped the section since. - anyhow::bail!("catalog extension no longer carries an mpegts section"); + bail!("catalog extension no longer carries an mpegts section"); }; mpegts.tracks.insert(name, entry); drop(guard); @@ -2325,7 +2322,7 @@ impl SectionStream { pid: u16, stream_type: u8, descriptors: Vec, - ) -> anyhow::Result { + ) -> Result { let entry = catalog::Track { pid, descriptors, @@ -2348,7 +2345,7 @@ impl SectionStream { /// Reassemble sections, retaining their first byte's placement across PCRs and fragments. /// Returns how many began before the first PCR and cannot be placed. - fn packet(&mut self, pkt: &[u8], at: u64, placement: &Placement) -> anyhow::Result { + fn packet(&mut self, pkt: &[u8], at: u64, placement: &Placement) -> Result { let mut completed = Vec::new(); self.reassembler.push_at(pkt, Some((at, placement)), &mut completed); let mut unplaced = 0; @@ -2396,7 +2393,7 @@ impl SectionStream { timebase: &crate::catalog::Timebase, pts: &PtsUnwrap, generation: u64, - ) -> anyhow::Result<(u64, u64)> { + ) -> Result<(u64, u64)> { let Some(offset) = timebase.offset() else { return Ok((0, 0)); }; @@ -2424,7 +2421,7 @@ impl SectionStream { Ok((published, unplaced)) } - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { // The half-reassembled section is stamped with the clock that just ended. self.reassembler = SectionReassembler::default(); if published { @@ -2433,12 +2430,12 @@ impl SectionStream { Ok(()) } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; Ok(()) } - fn finish(&mut self) -> anyhow::Result<()> { + fn finish(&mut self) -> Result<()> { self.track.end_with(None)?; Ok(()) } @@ -2549,7 +2546,7 @@ impl PesPacketStream { pid: u16, stream_type: u8, descriptors: Vec, - ) -> anyhow::Result { + ) -> Result { let entry = catalog::Track { pid, descriptors, @@ -2639,7 +2636,7 @@ impl PesPacketStream { timebase: &crate::catalog::Timebase, pts: &PtsUnwrap, generation: u64, - ) -> anyhow::Result<(u64, u64)> { + ) -> Result<(u64, u64)> { let Some(offset) = timebase.offset() else { return Ok((0, 0)); }; @@ -2678,19 +2675,19 @@ impl PesPacketStream { Ok((published, unplaced)) } - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { if published { self.track.discontinuity()?; } Ok(()) } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { self.track.seek(sequence)?; Ok(()) } - fn finish(&mut self) -> anyhow::Result<()> { + fn finish(&mut self) -> Result<()> { self.track.end_with(None)?; Ok(()) } @@ -3226,12 +3223,49 @@ impl SectionReassembler { } } -/// A parse failure confined to a unit, distinguished from publishing and clock failures. -#[derive(Debug, thiserror::Error)] -#[error("{0}")] -struct Damaged(anyhow::Error); +/// Why a stream stopped publishing what it was handed. +#[derive(Debug)] +enum Fault { + /// A parse failure confined to a unit, which the import drops and carries on past, after + /// `published` units of the same call went out. + Damaged { cause: Error, published: u64 }, + /// A publishing, clock, or stream failure, which ends the import. + Fatal(Error), +} + +impl Fault { + /// The failure this fault reports where nothing recovers from damage. + fn fatal(self) -> Error { + match self { + Self::Damaged { cause, .. } | Self::Fatal(cause) => cause, + } + } + + /// A damaged unit, with nothing published before it. + fn damaged(cause: impl Into) -> Self { + Self::Damaged { + cause: cause.into(), + published: 0, + } + } + + /// This fault after `published` units of the same call went out, so damage still counts them. + fn after(self, published: u64) -> Self { + match self { + Self::Damaged { cause, .. } => Self::Damaged { cause, published }, + fatal => fatal, + } + } +} + +impl> From for Fault { + fn from(err: E) -> Self { + Self::Fatal(err.into()) + } +} -fn unit_error(err: crate::Error) -> anyhow::Error { +/// `err` as damage to the unit it came from, when it is a parse failure confined to that unit. +fn unit_error(err: crate::Error) -> Fault { let damaged = matches!( &err, crate::Error::Annexb(_) @@ -3252,11 +3286,7 @@ fn unit_error(err: crate::Error) -> anyhow::Error { | h265::Error::MissingSps ) ); - if damaged { - Damaged(err.into()).into() - } else { - err.into() - } + if damaged { Fault::damaged(err) } else { err.into() } } /// Whether `stream_type` is MPEG-1 or MPEG-2 video, which is carried whole but still paces the @@ -3317,7 +3347,7 @@ enum Stream { impl Stream { /// Route one reassembled PES, returning how many access units it published. - fn write(&mut self, pending: Pending, batched: bool) -> anyhow::Result { + fn write(&mut self, pending: Pending, batched: bool) -> Result { match self { Stream::H264 { split, @@ -3405,7 +3435,7 @@ impl Stream { /// where its content stops. Every picture until the next keyframe may reference what was /// lost, so the producer refuses them until a keyframe opens the next group. Left open, /// the group would close a GOP later at that keyframe, with an end past its content. - fn lost(&mut self) -> anyhow::Result<()> { + fn lost(&mut self) -> Result<()> { self.desync(); match self { Stream::H264 { import, .. } => import.cut(None)?, @@ -3424,7 +3454,7 @@ impl Stream { } /// The program clock restarted: mark the break on this track. - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { match self { Stream::H264 { import, .. } => { if published { @@ -3446,7 +3476,7 @@ impl Stream { } } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { match self { Stream::H264 { split, @@ -3477,7 +3507,7 @@ impl Stream { } /// Finish the track, returning the access units a drained tail published. - fn finish(&mut self) -> anyhow::Result { + fn finish(&mut self) -> Result { match self { // Only the self-describing audio holds a frame back for a successor to confirm. Stream::Aac(stream) => return stream.finish(), @@ -3861,7 +3891,7 @@ struct AacStream { } impl AacStream { - fn write(&mut self, pending: Pending, batched: bool) -> anyhow::Result { + fn write(&mut self, pending: Pending, batched: bool) -> Result { // The frames a PES completes share one group, closed once the PES is spent, even by an // error. The transport sends a track's newest group first, so a group per frame would // deliver a multi-frame PES newest first, its oldest frame waiting behind the rest. @@ -3877,7 +3907,7 @@ impl AacStream { /// Publish the frames `pending` completes into one group, leaving `open` at where its last frame /// stops while it is open. A frame without a PTS is stamped by the producer, which checks only /// where groups start, so it keeps a group of its own, as every frame once did. - fn split(&mut self, pending: Pending, batched: bool, open: &mut Option) -> anyhow::Result { + fn split(&mut self, pending: Pending, batched: bool, open: &mut Option) -> Result { let pes_base = on_clock(pending.pts, pending.offset)?; // Prepend the partial frame left by the previous PES, if any. @@ -3929,7 +3959,7 @@ impl AacStream { // publishing one without confirming it (which only end of stream does) substitutes // audio rather than leaving a gap. Sampled here, before the publish clears it. let unvouched = !confirm && (self.resync.unconfirmed() || in_tail); - let parsed: Result<_, Option> = match adts::Header::parse(&data[offset..]) { + let parsed: Result<_, Option> = match adts::Header::parse(&data[offset..]) { Ok(header) => { let end = offset + header.frame_len; if end > data.len() { @@ -3974,9 +4004,10 @@ impl AacStream { if self.resync.exhausted() { let context = format!("AAC stream never regained sync after {} bytes", self.resync.discarded); return Err(match err { - Some(err) => err.context(context), - None => anyhow::Error::msg(context), - }); + Some(err) => malformed!("{context}: {err}"), + None => Error::Malformed(context), + } + .into()); } // Take the PES PTS only where the tail is actually abandoned: restoring a fallback // keeps the candidate, so it keeps the timestamp it was found with. @@ -4039,12 +4070,13 @@ impl AacStream { match aac::in_band_config(header.object_type, header.sample_rate, 0, &mut rest) { Ok(asc) if asc == self.asc => block = rest, Ok(_) => { - return Err( - Damaged(anyhow::anyhow!("AAC program config element changed mid-stream")).into(), - ); + return Err(Fault::damaged(malformed!( + "AAC program config element changed mid-stream" + )) + .after(published)); } Err(aac::Error::ProgramConfigMissing) => {} - Err(err) => return Err(unit_error(err.into())), + Err(err) => return Err(unit_error(err.into()).after(published)), } } import @@ -4065,7 +4097,7 @@ impl AacStream { // joined later may never see one. Stop withholding the catalog for this // track; it joins the catalog if an element arrives. self.reserved.take(); - unit_error(err.into()) + unit_error(err.into()).after(published) })?; let mut config = aac::config(&asc)?; config.container = self.container.clone(); @@ -4127,7 +4159,7 @@ impl AacStream { Ok(published) } - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { self.desync(); if published && let Some(import) = &mut self.import { import.discontinuity()?; @@ -4135,7 +4167,7 @@ impl AacStream { Ok(()) } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { // A seek is a discontinuity like any other. self.desync(); if let Some(import) = &mut self.import { @@ -4154,7 +4186,7 @@ impl AacStream { self.resync.desynced(); } - fn finish(&mut self) -> anyhow::Result { + fn finish(&mut self) -> Result { // Drain a frame held only for want of a successor to confirm it: at end of stream // that successor is never coming. Only once this stream has published a frame, // though. Before that nothing has vouched for any boundary, so accepting one here @@ -4201,10 +4233,10 @@ struct OpusStream { } impl OpusStream { - fn write(&mut self, pending: Pending) -> anyhow::Result { + fn write(&mut self, pending: Pending) -> Result { let base = on_clock(pending.pts, pending.offset)?; - let packets = opus_packets(&pending.data).map_err(Damaged)?; + let packets = opus_packets(&pending.data).map_err(Fault::damaged)?; let mut published = 0; // 48 kHz samples elapsed since this PES's PTS, advancing each packet after the first. let mut elapsed: u64 = 0; @@ -4232,18 +4264,18 @@ impl OpusStream { Ok(published) } - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { if published { self.import.discontinuity()?; } Ok(()) } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { Ok(self.import.seek(sequence)?) } - fn finish(&mut self) -> anyhow::Result<()> { + fn finish(&mut self) -> Result<()> { Ok(self.import.finish()?) } @@ -4276,7 +4308,7 @@ fn registration_format(descriptors: &[catalog::Descriptor]) -> Option<[u8; 4]> { /// descriptor (channel count, mapping family, then the stream counts and table, /// bit-packed). A reserved code, or an explicit layout that does not parse, refuses this /// stream. A stream with no extension descriptor stays stereo. -fn opus_config(descriptors: &[catalog::Descriptor]) -> anyhow::Result { +fn opus_config(descriptors: &[catalog::Descriptor]) -> Result { let Some(data) = descriptors .iter() .find(|d| d.tag == 0x7f && d.data.first() == Some(&0x80)) @@ -4285,7 +4317,7 @@ fn opus_config(descriptors: &[catalog::Descriptor]) -> anyhow::Result anyhow::Result Ok(opus::Config::new(48_000, u32::from(code))), 3..=8 => vorbis(code), 0x80 => { - anyhow::ensure!(data.len() == 2, "trailing bytes after Opus channel_config_code 0x80"); + ensure!(data.len() == 2, "trailing bytes after Opus channel_config_code 0x80"); mapped(2, 255, 2, 0, &[0, 1]) } 0x81 => explicit_layout(&data[2..]), 0x82..=0x88 => { - anyhow::ensure!( + ensure!( data.len() == 2, "trailing bytes after Opus channel_config_code 0x{code:02x}" ); @@ -4306,19 +4338,19 @@ fn opus_config(descriptors: &[catalog::Descriptor]) -> anyhow::Result anyhow::bail!("reserved Opus channel_config_code 0x{code:02x}"), + _ => bail!("reserved Opus channel_config_code 0x{code:02x}"), } } /// Family 1 Vorbis mapping for a plain channel count of 3..=8. -fn vorbis(channels: u8) -> anyhow::Result { +fn vorbis(channels: u8) -> Result { let mut config = opus::Config::new(48_000, u32::from(channels)); config.mapping = Some(opus::Mapping::vorbis(channels)?); Ok(config) } /// A mapping table checked by [`opus::Mapping::new`]. -fn mapped(channels: u32, family: u8, streams: u8, coupled: u8, table: &[u8]) -> anyhow::Result { +fn mapped(channels: u32, family: u8, streams: u8, coupled: u8, table: &[u8]) -> Result { let mut config = opus::Config::new(48_000, channels); config.mapping = Some(opus::Mapping::new(opus::mapping::Config { family, @@ -4340,18 +4372,18 @@ fn mapped(channels: u32, family: u8, streams: u8, coupled: u8, table: &[u8]) -> /// whenever `n` is a power of two. Those descriptors fail the trailing or pad check and /// are refused. Retrying with gstreamer's widths could misread a descriptor written to /// the draft, so there is no fallback. -fn explicit_layout(data: &[u8]) -> anyhow::Result { +fn explicit_layout(data: &[u8]) -> Result { use bitstream_io::BitRead; let (&channel_count, rest) = data.split_first().context("truncated Opus channel configuration")?; let (&family, rest) = rest.split_first().context("truncated Opus channel configuration")?; - anyhow::ensure!(channel_count > 0, "Opus channel_count is zero"); + ensure!(channel_count > 0, "Opus channel_count is zero"); if family == 0 { - anyhow::ensure!( + ensure!( rest.is_empty(), "trailing bytes after a family 0 Opus channel configuration" ); - anyhow::ensure!( + ensure!( (1..=2).contains(&channel_count), "channel mapping family 0 does not allow {channel_count} channels" ); @@ -4360,17 +4392,17 @@ fn explicit_layout(data: &[u8]) -> anyhow::Result { let mut bits = bitstream_io::BitReader::endian(rest, bitstream_io::BigEndian); let stream_count = read_bits(&mut bits, ceil_log2(u32::from(channel_count)))? + 1; - anyhow::ensure!( + ensure!( stream_count <= u32::from(channel_count), "Opus stream_count {stream_count} exceeds channel_count {channel_count}" ); let coupled = read_bits(&mut bits, ceil_log2(stream_count + 1))?; - anyhow::ensure!( + ensure!( coupled <= stream_count, "Opus coupled_stream_count {coupled} exceeds stream_count {stream_count}" ); let decoded = stream_count + coupled; - anyhow::ensure!( + ensure!( decoded <= 255, "Opus channel configuration has {decoded} coded channels" ); @@ -4383,9 +4415,9 @@ fn explicit_layout(data: &[u8]) -> anyhow::Result { } while !bits.byte_aligned() { let reserved = read_bits(&mut bits, 1)?; - anyhow::ensure!(reserved == 0, "nonzero reserved bits in Opus channel configuration"); + ensure!(reserved == 0, "nonzero reserved bits in Opus channel configuration"); } - anyhow::ensure!( + ensure!( bits.into_reader().is_empty(), "trailing bytes in Opus channel configuration" ); @@ -4407,12 +4439,13 @@ fn ceil_log2(n: u32) -> u8 { /// Read an `n`-bit field, MSB-first, from the bit-packed tail of an explicit Opus channel /// configuration. A zero-width field reads 0. -fn read_bits(bits: &mut impl bitstream_io::BitRead, n: u8) -> anyhow::Result { - bits.read_var(n.into()).context("truncated Opus channel configuration") +fn read_bits(bits: &mut impl bitstream_io::BitRead, n: u8) -> Result { + bits.read_var(n.into()) + .map_err(|_| malformed!("truncated Opus channel configuration")) } /// Validate the complete PES before exposing any of its declared Opus units. -fn opus_packets(mut data: &[u8]) -> anyhow::Result> { +fn opus_packets(mut data: &[u8]) -> Result> { let mut packets = Vec::new(); while !data.is_empty() { let (header_len, size) = parse_opus_control_header(data)?; @@ -4427,10 +4460,10 @@ fn opus_packets(mut data: &[u8]) -> anyhow::Result> { } /// Parse one Opus-in-TS access-unit control header, returning `(header_len, payload_size)`. -fn parse_opus_control_header(data: &[u8]) -> anyhow::Result<(usize, usize)> { - anyhow::ensure!(data.len() >= 2, "Opus control header truncated"); +fn parse_opus_control_header(data: &[u8]) -> Result<(usize, usize)> { + ensure!(data.len() >= 2, "Opus control header truncated"); // 11-bit 0x3FF sync: byte 0 == 0x7F and the top 3 bits of byte 1 == 0b111. - anyhow::ensure!( + ensure!( data[0] == 0x7f && (data[1] & 0xe0) == 0xe0, "invalid Opus control header sync (0x{:02x}{:02x})", data[0], @@ -4462,7 +4495,7 @@ fn parse_opus_control_header(data: &[u8]) -> anyhow::Result<(usize, usize)> { let len = *data.get(pos).context("Opus control extension truncated")? as usize; pos += 1 + len; } - anyhow::ensure!(pos <= data.len(), "Opus control header exceeds payload"); + ensure!(pos <= data.len(), "Opus control header exceeds payload"); Ok((pos, size)) } @@ -4500,7 +4533,7 @@ struct LegacyStream { } impl LegacyStream { - fn write(&mut self, pending: Pending) -> anyhow::Result { + fn write(&mut self, pending: Pending) -> Result { // One group per PES, closed even by an error: see `AacStream::write`. let mut open = None; let result = self.split(pending, &mut open); @@ -4514,7 +4547,7 @@ impl LegacyStream { /// Publish the frames `pending` completes into one group, leaving `open` at where its last frame /// stops while it is open. A frame without a PTS is stamped by the producer, which checks only /// where groups start, so it keeps a group of its own, as every frame once did. - fn split(&mut self, pending: Pending, open: &mut Option) -> anyhow::Result { + fn split(&mut self, pending: Pending, open: &mut Option) -> Result { let mut published = 0; let pes_base = on_clock(pending.pts, pending.offset)?; @@ -4618,9 +4651,10 @@ impl LegacyStream { self.descriptor.track_suffix, self.resync.discarded ); return Err(match err { - Some(err) => anyhow::Error::new(err).context(context), - None => anyhow::Error::msg(context), - }); + Some(err) => malformed!("{context}: {err}"), + None => Error::Malformed(context), + } + .into()); } // The tail's frame boundary is what we just lost, so its PTS no longer // describes anything. Re-anchor on the PES, whose PTS covers the first @@ -4733,7 +4767,7 @@ impl LegacyStream { Ok(published) } - fn discontinuity(&mut self, published: bool) -> anyhow::Result<()> { + fn discontinuity(&mut self, published: bool) -> Result<()> { self.desync(); if published && let Some(import) = &mut self.import { import.discontinuity()?; @@ -4741,7 +4775,7 @@ impl LegacyStream { Ok(()) } - fn seek(&mut self, sequence: u64) -> anyhow::Result<()> { + fn seek(&mut self, sequence: u64) -> Result<()> { // A seek is a discontinuity like any other. self.desync(); if let Some(import) = &mut self.import { @@ -4759,7 +4793,7 @@ impl LegacyStream { self.resync.desynced(); } - fn finish(&mut self) -> anyhow::Result { + fn finish(&mut self) -> Result { // Drain a frame held only for want of a successor to confirm it: at end of stream // that successor is never coming. Only once this stream has published a frame, // though. Before that nothing has vouched for any boundary, so accepting one here @@ -4803,7 +4837,7 @@ impl LegacyStream { /// Swallow a [`MissingKeyframe`](crate::container::MissingKeyframe) from a video /// decode: a TS capture can join mid-GOP, so the deltas before the first keyframe /// have no group to anchor and are simply dropped rather than aborting the demux. -fn skip_missing_keyframe(result: crate::Result<()>) -> anyhow::Result { +fn skip_missing_keyframe(result: crate::Result<()>) -> Result { match result { Ok(()) => Ok(true), Err(crate::Error::MissingKeyframe(_)) => Ok(false), @@ -4831,7 +4865,7 @@ fn forward(last: &mut Option, pts: Option) -> crate::Resul /// /// Per frame rather than `index * constant`: E-AC-3 varies the samples per frame, and a /// resync means the frames in a PES are no longer a contiguous run to index into. -fn advance_pts(pts: Option, samples: u64, sample_rate: u32) -> anyhow::Result> { +fn advance_pts(pts: Option, samples: u64, sample_rate: u32) -> Result> { let Some(pts) = pts else { return Ok(None); }; @@ -4883,7 +4917,7 @@ impl PtsUnwrap { /// A 90 kHz PTS already in its era, as a [`Timestamp`] on the catalog clock: shifted by the /// timebase's `offset`. Returns `None` when the PES carried no PTS. -fn on_clock(pts: Option, offset: Offset) -> anyhow::Result> { +fn on_clock(pts: Option, offset: Offset) -> Result> { let Some(pts) = pts else { return Ok(None); }; @@ -4997,7 +5031,7 @@ pub(super) mod test { let mut import = super::Import::new(broadcast, catalog.reserve()); let pid = Pid::new(0x61).unwrap(); let peer = Pid::new(0x62).unwrap(); - let error = anyhow::anyhow!("broken PES"); + let error = super::Error::Malformed("broken PES".into()); for _ in 0..3 { import.damage(pid, &error).unwrap(); } @@ -5056,7 +5090,7 @@ pub(super) mod test { let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut import = super::Import::new(broadcast, catalog.reserve()); let pid = Pid::new(0x61).unwrap(); - let error = anyhow::anyhow!("broken PES"); + let error = super::Error::Malformed("broken PES".into()); for _ in 0..3 { import.damage(pid, &error).unwrap(); } @@ -5088,7 +5122,9 @@ pub(super) mod test { legacy.resync.recover(&[0; 10], 0, &codec); legacy.resync.published(false); import.streams.insert(pid, stream); - import.damage(pid, &anyhow::anyhow!("malformed PES")).unwrap(); + import + .damage(pid, &super::Error::Malformed("malformed PES".into())) + .unwrap(); let before = import.stats(); assert_eq!(before.streams[&pid.as_u16()].damaged, 1); @@ -6265,10 +6301,10 @@ pub(super) mod test { let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut import = super::Import::new(broadcast, catalog.reserve()); - let err = import - .decode(&bytes::BytesMut::from(&looped[..])) - .expect_err("a loop wrap is a restart"); - let restart = err.downcast_ref::().expect("a restart"); + let restart = import + .decode_or_restart(&bytes::BytesMut::from(&looped[..])) + .unwrap() + .expect("a loop wrap is a restart"); assert!( matches!(restart.step, crate::container::ts::Step::Rewind { flagged: false, .. }), "{restart:?}" @@ -7174,7 +7210,7 @@ pub(super) mod test { join.extend_from_slice(&mp2_frame(0xEE)); let err = import .decode(audio_pes_packet(MP2_PID, 1, 89_910, &join).as_slice()) - .and_then(|()| import.finish_in_place()) + .and_then(|_| import.finish_in_place()) .expect_err("a PES PTS below the live edge is a restart"); assert!(is_rewind(&err), "{err:?}"); } @@ -7582,21 +7618,25 @@ pub(super) mod test { out: synth_pmt(&[(StreamType::AdtsAac, AAC_PID)], false), ..Default::default() }; + // The third PES publishes a frame before its changed element, which still counts. for (pts, unit) in [ (90_000, [with.as_slice(), &bare]), (180_000, [changed.as_slice(), &bare]), + (270_000, [bare.as_slice(), &changed]), ] { let cc = mux.cc(AAC_PID); mux.out.extend(audio_pes_packet(AAC_PID, cc, pts, &unit.concat())); } let cc = mux.cc(AAC_PID); - mux.out.extend(audio_pes_packet(AAC_PID, cc, 270_000, &bare)); + mux.out.extend(audio_pes_packet(AAC_PID, cc, 360_000, &bare)); import.decode(&mux.out).unwrap(); import.finish_in_place().unwrap(); - assert_eq!(import.stats().streams[&AAC_PID].damaged, 1); + let stream = &import.stats().streams[&AAC_PID]; + assert_eq!(stream.damaged, 2); let frames = read_audio_frames(&consumer, &catalog).await; - assert_eq!(frames.len(), 3, "the changed unit is dropped, the rest publish"); + assert_eq!(frames.len(), 4, "the changed units are dropped, the rest publish"); + assert_eq!(stream.units, 4, "every published frame counts as delivered"); } // Resync is bounded: a PID carrying something other than the codec its PMT declares is a @@ -7632,7 +7672,7 @@ pub(super) mod test { .find_map(Result::err) .expect("an unparseable stream must still fail"); assert!( - err.to_string().contains("never regained sync"), + matches!(&err, super::Error::Malformed(msg) if msg.contains("never regained sync")), "gave up with the wrong error: {err}" ); } @@ -7717,7 +7757,7 @@ pub(super) mod test { .find_map(Result::err) .expect("an unparseable stream must still fail"); assert!( - err.to_string().contains("never regained sync"), + matches!(&err, super::Error::Malformed(msg) if msg.contains("never regained sync")), "gave up with the wrong error: {err}" ); } @@ -8203,11 +8243,10 @@ pub(super) mod test { let mut import = super::Import::new(broadcast, catalog.reserve()); let err = import.decode(&two_programs()).unwrap_err(); - assert_eq!( - err.downcast_ref::(), - Some(&super::MultipleProgramsError { programs: vec![1, 2] }), - "{err:#}" - ); + let super::Error::MultiplePrograms { programs } = err else { + panic!("a multiplex refusal: {err}"); + }; + assert_eq!(programs, [1, 2]); assert!(catalog.snapshot().audio.renditions.is_empty(), "nothing was published"); } @@ -8222,7 +8261,7 @@ pub(super) mod test { import.decode(&mp2_pes(0x0061, 0, 90_000, [0xAA, 0xBB])).unwrap(); let err = import.decode(&two_programs()).unwrap_err(); - assert!(err.downcast_ref::().is_some(), "{err:#}"); + assert!(matches!(err, super::Error::MultiplePrograms { .. }), "{err}"); assert_eq!( catalog.snapshot().audio.renditions.len(), 1, @@ -8263,8 +8302,12 @@ pub(super) mod test { let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut import = super::Import::new(broadcast, catalog.reserve()).with_program(3); - let err = import.decode(&two_programs()).unwrap_err().to_string(); - assert!(err.contains("no program 3") && err.contains("1, 2"), "{err}"); + let err = import.decode(&two_programs()).unwrap_err(); + let super::Error::MissingProgram { program, programs } = &err else { + panic!("a missing program: {err}"); + }; + assert_eq!((*program, programs.as_slice()), (3, &[1, 2][..])); + assert!(err.to_string().contains("no program 3; its PAT lists 1, 2"), "{err}"); } /// `section` on `pid` as the packets a mux would send, counting from `*cc`: the first @@ -8408,8 +8451,10 @@ pub(super) mod test { // Unselected, the whole PAT is what refuses the input. let (_, mut import) = psi_import(); let err = import.decode(&fifty_programs()).unwrap_err(); - let err = err.downcast_ref::().unwrap(); - assert_eq!(err.programs, (1..=50).collect::>()); + let super::Error::MultiplePrograms { programs } = err else { + panic!("a multiplex refusal: {err}"); + }; + assert_eq!(programs, (1..=50).collect::>()); } /// A corrupt PAT and a corrupt PMT between good repetitions are each dropped and counted @@ -8655,7 +8700,10 @@ pub(super) mod test { import.decode(&packet(true, 0, 0, &CUE)).unwrap(); if clock == "none" { let err = import.finish_in_place().unwrap_err(); - assert!(err.to_string().contains("without a media timestamp"), "{clock}: {err}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{clock}: {err}" + ); } else { import.finish_in_place().unwrap(); assert!(import.sections[&0x21].held.is_empty()); @@ -8696,16 +8744,25 @@ pub(super) mod test { let mut track = consumer.track(&name).unwrap().subscribe(None).await.unwrap(); let err = import.finish_in_place().unwrap_err(); - assert!(err.to_string().contains("without a media timestamp"), "{err}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.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_in_place().unwrap_err(); - assert!(retried.to_string().contains("without a media timestamp"), "{retried}"); + assert!( + matches!(&retried, super::Error::Malformed(msg) if msg.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}"); + assert!( + matches!(&refused, super::Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{refused}" + ); } #[test] @@ -8731,7 +8788,7 @@ pub(super) mod test { import.decode(&pcr_packet(PCR_PID, 27_000_000)).unwrap(); import.decode(&packet(true, 0, 0, &CUE)).unwrap(); let err = import.decode(&clock_break_packet(PCR_PID)).unwrap_err(); - assert!(err.to_string().contains("clock break without a media timestamp")); + assert!(matches!(&err, super::Error::Malformed(msg) if msg.contains("clock break without a media timestamp"))); assert_eq!(import.sections[&0x21].held.len(), 1); assert!(!import.published); } @@ -8828,10 +8885,10 @@ pub(super) mod test { after.extend(mp2_pes(PCR_PID, 1, 90_000, [0xCC, 0xDD])); import.decode(&after[188..]).unwrap(); after.extend(mp2_pes(PEER_PID, 1, 90_000, [0xCC, 0xDD])); - let err = import - .decode(&after[2 * 188..]) - .expect_err("a restart below the live edge is a new broadcast"); - let restart = err.downcast_ref::().expect("a restart"); + let restart = import + .decode_or_restart(&after[2 * 188..]) + .unwrap() + .expect("a restart below the live edge is a new broadcast"); assert_eq!( restart.step, crate::container::ts::Step::Rewind { @@ -9184,15 +9241,12 @@ pub(super) mod test { import.staging.bytes.load(super::Ordering::Relaxed), super::STAGING_BYTES_MAX ); - assert!( - import - .handle_pes_continuation(first, &[1]) - .unwrap_err() - .to_string() - .contains("staging exceeds") - ); + assert!(matches!( + import.handle_pes_continuation(first, &[1]), + Err(super::Error::Malformed(msg)) if msg.contains("staging exceeds") + )); assert_eq!(import.pending[&first].data.len(), super::STAGING_BYTES_MAX / 2); - import.damage(first, &anyhow::anyhow!("loss")).unwrap(); + import.damage(first, &super::Error::Malformed("loss".into())).unwrap(); assert_eq!( import.staging.bytes.load(super::Ordering::Relaxed), super::STAGING_BYTES_MAX / 2 @@ -9467,11 +9521,32 @@ pub(super) mod test { /// returned to keep its renditions in the catalog. fn import_all( data: &[u8], - ) -> anyhow::Result<( + ) -> super::Result<( moq_net::broadcast::Consumer, crate::catalog::Producer, super::Import, )> { + let (consumer, catalog, mut import) = import_new(); + import.decode(data)?; + import.finish_in_place()?; + Ok((consumer, catalog, import)) + } + + /// Import `data`, which must restart. + fn import_restart(data: &[u8]) -> crate::container::ts::Restart { + let (_consumer, _catalog, mut import) = import_new(); + import + .decode_or_restart(data) + .unwrap() + .expect("the import carried on past a restart") + } + + /// An importer onto a fresh broadcast with an `mpegts` catalog. + fn import_new() -> ( + moq_net::broadcast::Consumer, + crate::catalog::Producer, + super::Import, + ) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let consumer = broadcast.consume(); let catalog = crate::catalog::Producer::new( @@ -9479,23 +9554,16 @@ pub(super) mod test { crate::catalog::Config::default().with_catalog(crate::catalog::hang::Catalog::::default()), ) .unwrap(); - let mut import = super::Import::new(broadcast, catalog.reserve()); - import.decode(data)?; - import.finish_in_place()?; - Ok((consumer, catalog, import)) + let import = super::Import::new(broadcast, catalog.reserve()); + (consumer, catalog, import) } use crate::container::ts::catalog::Ext; /// Whether `err` is a refused timestamp rewind: the source restarted, which is a new /// broadcast rather than a continuation. - fn is_rewind(err: &anyhow::Error) -> bool { - err.chain().any(|cause| { - matches!( - cause.downcast_ref::(), - Some(crate::Error::TimestampRewind(_)) - ) - }) + fn is_rewind(err: &super::Error) -> bool { + matches!(err, super::Error::Mux(err) if matches!(**err, crate::Error::TimestampRewind(_))) } /// Import `data`, which must end in a refused rewind. @@ -9560,8 +9628,10 @@ pub(super) mod test { Ok(_) => panic!("a second AU cannot reuse the first AU's PES PTS"), Err(err) => err, }; - assert!(!err.is::(), "timing refusal must end the import"); - assert!(err.to_string().contains("timestamp"), "{err:#}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("timestamp")), + "timing refusal must end the import: {err}" + ); } /// Repacketize actual encoder-produced POCs, retaining an independent PTS @@ -9647,8 +9717,10 @@ pub(super) mod test { Ok(_) => panic!("SPS fixed frame rate alone cannot timestamp a later AU"), Err(err) => err, }; - assert!(!err.is::()); - assert!(err.to_string().contains("proportional"), "{err:#}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("proportional")), + "{err}" + ); continue; } let (consumer, catalog, _import) = @@ -10231,13 +10303,14 @@ pub(super) mod test { } let err = import.decode(&data).expect_err("closed producer must end ingest"); assert!( - matches!(err.downcast_ref::(), Some(crate::Error::Moq(_))), + matches!(&err, super::Error::Mux(err) if matches!(**err, crate::Error::Moq(_))), "{err:?}" ); assert!(import.damaged.is_empty(), "publishing failure is not local damage"); - assert!( - !super::unit_error(crate::Error::H264(crate::codec::h264::Error::MissingTimestamp)).is::() - ); + assert!(matches!( + super::unit_error(crate::Error::H264(crate::codec::h264::Error::MissingTimestamp)), + super::Fault::Fatal(_) + )); } /// A break closes the video group where its content stops, not a GOP later at the next @@ -10362,10 +10435,7 @@ pub(super) mod test { let at = mux.out.len(); mux.out.extend_from_slice(&clock_break_packet(VIDEO)); mux.gops(VIDEO, 90_000, 2); - let Err(err) = import_all(&mux.out) else { - panic!("the import carried on past a rewind"); - }; - let restart = err.downcast_ref::().expect("a restart"); + let restart = import_restart(&mux.out); assert!( matches!(restart.step, crate::container::ts::Step::Rewind { flagged: true, .. }), "{restart:?}" @@ -11628,7 +11698,10 @@ pub(super) mod test { let err = import .handle_pat(&pat(transport_stream_id, program_number)) .expect_err("another identity is refused"); - assert!(err.to_string().contains("identity"), "{err:#}"); + assert!( + matches!(&err, super::Error::Unsupported(msg) if msg.contains("identity")), + "{err}" + ); } } @@ -11726,7 +11799,10 @@ pub(super) mod test { import.decode(&bytes).unwrap(); let err = import.decode(&synth_pmt(&streams[..1], true)).unwrap_err(); - assert!(err.to_string().contains("without a media timestamp"), "{err:#}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{err:#}" + ); assert!(import.sections.contains_key(&0x21), "the route is left for `abort`"); } diff --git a/rs/moq-mux/src/container/ts/mod.rs b/rs/moq-mux/src/container/ts/mod.rs index d589f6edd73..217cdec4ce1 100644 --- a/rs/moq-mux/src/container/ts/mod.rs +++ b/rs/moq-mux/src/container/ts/mod.rs @@ -26,6 +26,7 @@ use crate::codec::aac::adts; mod damage; mod demux; +mod error; mod export; mod follower; mod health; @@ -47,6 +48,7 @@ mod unclaimed; mod catalog; pub use catalog::{Catalog, Descriptor, Ext, Framing, Mpegts, PesPacket, Program, SiEntry, Track, Verbatim}; +pub use error::Error; pub use export::Export; pub use follower::Follower; pub use import::*; @@ -54,7 +56,8 @@ pub use passthrough::Passthrough; pub use programs::Programs; pub use release::Release; pub use republish::{Republish, Republished}; -pub use step::{Restart, Step}; +use step::Restart; +pub use step::Step; pub mod stats; diff --git a/rs/moq-mux/src/container/ts/passthrough_test.rs b/rs/moq-mux/src/container/ts/passthrough_test.rs index 8db2efa3f05..ffe15ab644d 100644 --- a/rs/moq-mux/src/container/ts/passthrough_test.rs +++ b/rs/moq-mux/src/container/ts/passthrough_test.rs @@ -64,7 +64,7 @@ impl Imported { } /// Import `data` in chunks of `chunk` bytes and read back every object. -async fn import(data: &[u8], chunk: usize, pcr_pid: Option) -> anyhow::Result { +async fn import(data: &[u8], chunk: usize, pcr_pid: Option) -> crate::Result { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default())?; import_onto(broadcast, catalog, data, chunk, pcr_pid).await @@ -77,7 +77,7 @@ async fn import_onto( data: &[u8], chunk: usize, pcr_pid: Option, -) -> anyhow::Result { +) -> crate::Result { let consumer = broadcast.consume(); let mut passthrough = Passthrough::new(broadcast.clone(), catalog.reserve())?; if let Some(pid) = pcr_pid { @@ -99,7 +99,10 @@ async fn import_onto( let mut objects = Vec::new(); let mut markers = Vec::new(); let mut starts = Vec::new(); - while let Some(mut group) = kio::clock::timeout(Duration::from_secs(1), track.recv_group()).await?? { + while let Some(mut group) = kio::clock::timeout(Duration::from_secs(1), track.recv_group()) + .await + .expect("the track stalled")? + { starts.push(group.start); while let Some(frame) = group.read_frame().await? { if frame.payload.is_empty() { @@ -668,9 +671,8 @@ async fn a_flagged_step_back_is_a_restart() { Ok(_) => panic!("a restart was carried on one timeline"), Err(err) => err, }; - let restart = err.downcast_ref::(); assert!( - matches!(restart, Some(crate::Error::PcrRestart { pid: 0x100, to, .. }) if *to == 2_000 * MS), + matches!(&err, crate::Error::PcrRestart { pid: 0x100, to, .. } if *to == 2_000 * MS), "{err:?}" ); } @@ -702,9 +704,8 @@ async fn an_unflagged_rewind_is_refused() { Ok(_) => panic!("a rewind was accepted"), Err(err) => err, }; - let rewind = err.downcast_ref::(); assert!( - matches!(rewind, Some(crate::Error::PcrRewind { pid: 0x100, to, .. }) if *to == 9_000 * MS), + matches!(&err, crate::Error::PcrRewind { pid: 0x100, to, .. } if *to == 9_000 * MS), "{err:?}" ); } @@ -784,13 +785,7 @@ async fn a_clock_that_stops_is_refused_once_a_group_would_overflow() { Ok(_) => panic!("an untimed run was accepted"), Err(err) => err, }; - assert!( - matches!( - err.downcast_ref::(), - Some(crate::Error::PcrGap { pid: 0x100, .. }) - ), - "{err:?}" - ); + assert!(matches!(err, crate::Error::PcrGap { pid: 0x100, .. }), "{err:?}"); } #[tokio::test(start_paused = true)] @@ -827,10 +822,7 @@ async fn a_192_or_204_byte_source_is_refused() { Ok(_) => panic!("{size}-byte packets were accepted"), Err(err) => err, }; - assert!( - matches!(err.downcast_ref::(), Some(crate::Error::TsPacketSize(got)) if *got == size), - "{err:?}" - ); + assert!(matches!(err, crate::Error::TsPacketSize(got) if got == size), "{err:?}"); } } diff --git a/rs/moq-mux/src/container/ts/pes_packet_test.rs b/rs/moq-mux/src/container/ts/pes_packet_test.rs index 34e14a70014..194bbc326e6 100644 --- a/rs/moq-mux/src/container/ts/pes_packet_test.rs +++ b/rs/moq-mux/src/container/ts/pes_packet_test.rs @@ -179,7 +179,7 @@ struct Imported { /// Import `data`, onto a catalog clock already fixed when `captured` (so the timestamps shift), /// or one the first PTS places (so they stay verbatim). -fn import(data: &[u8], captured: bool) -> anyhow::Result { +fn import(data: &[u8], captured: bool) -> Result { let mut import = start(captured); import.import.decode(data)?; import.import.finish_in_place()?; @@ -627,7 +627,10 @@ fn a_program_with_missing_media_timestamps_is_refused() { feed.pcr(1_000 * MS + i * 40 * MS).data(klv(i as u8, 8)); } let err = import(&feed.bytes(), false).err().expect("the import is refused"); - assert!(err.to_string().contains("without a media timestamp"), "{err:#}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{err:#}" + ); } /// No media PTS is needed when the PMT declares only data: PCR carries the era too. @@ -989,7 +992,10 @@ async fn a_private_stream_dropped_before_the_media_clock_refuses_the_import() { .import .decode(&feed.bytes()[head.len()..]) .expect_err("the held PES has no media timestamp to be placed by"); - assert!(err.to_string().contains("without a media timestamp"), "{err:#}"); + assert!( + matches!(&err, super::Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{err:#}" + ); imported.import.abort(moq_net::Error::Unsupported); let (_, end) = held.await.unwrap(); assert!(end.as_ref().is_err_and(|err| err.contains("unsupported")), "{end:?}"); diff --git a/rs/moq-mux/src/container/ts/programs.rs b/rs/moq-mux/src/container/ts/programs.rs index fb745857ec8..276cc6a0943 100644 --- a/rs/moq-mux/src/container/ts/programs.rs +++ b/rs/moq-mux/src/container/ts/programs.rs @@ -2,10 +2,10 @@ use std::collections::{BTreeMap, HashMap, HashSet}; -use anyhow::Context; use mpeg2ts::ts::Pid; use super::demux::{Demux, Packet}; +use super::error::{Error, Result, ensure_supported}; use super::health::{Checks, Health, PmtPacket}; use super::import::SectionReassembler; use super::unclaimed::Unclaimed; @@ -193,8 +193,8 @@ struct Program { impl Program { /// Announce the broadcast once its catalog has gone out. - fn announce_if_listed(&mut self) -> anyhow::Result { - super::republish::listed(&mut self.pending, &self.route).with_context(|| format!("program {}", self.number)) + fn announce_if_listed(&mut self) -> Result { + super::republish::listed(&mut self.pending, &self.route) } } @@ -365,14 +365,14 @@ impl Programs { } /// Demux a chunk of the multiplex. A trailing partial packet is retained for the next call. - pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { + pub fn decode(&mut self, data: &[u8]) -> Result<()> { let mut demux = std::mem::take(&mut self.demux); let result = self.decode_with(&mut demux, data); self.demux = demux; result } - fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> anyhow::Result<()> { + fn decode_with(&mut self, demux: &mut Demux, data: &[u8]) -> Result<()> { demux.push(data); let mut read = None; while let Some(packet) = demux.next() { @@ -465,13 +465,13 @@ impl Programs { /// /// Fails on a PAT naming another transport stream while a program runs, as each program's /// importer would, and when a retired program cannot finish. - fn follow(&mut self, pat: &psi::Pat) -> anyhow::Result<()> { + fn follow(&mut self, pat: &psi::Pat) -> Result<()> { // The PAT repeats several times a second, mostly unchanged. if self.pat.as_ref() == Some(pat) { return Ok(()); } if let Some(running) = self.pat.as_ref().filter(|_| !self.programs.is_empty()) { - anyhow::ensure!( + ensure_supported!( running.transport_stream_id == pat.transport_stream_id, "the PAT changed transport_stream_id from {} to {} while its programs run, which their carried service information does not describe", running.transport_stream_id, @@ -594,7 +594,7 @@ impl Programs { /// The programs and the PIDs they read are rebuilt once, so a PAT costs what the programs /// claim however many it drops. Fails when a program cannot finish, as an import does at /// the end of its input. - fn retire(&mut self, listed: &HashMap) -> anyhow::Result<()> { + fn retire(&mut self, listed: &HashMap) -> Result<()> { let leaving = |program: &Program| !listed.contains_key(&program.number); if !self.programs.iter().any(leaving) { return Ok(()); @@ -715,7 +715,7 @@ impl Programs { /// A started program reads this packet itself, from the reassembler as it stood before it, /// so its own PMT path applies the PMT and starts its clock from the PCR already seen. Of /// several revisions in the packet, the last decides. - fn describe(&mut self, packet: &Packet) -> anyhow::Result<()> { + fn describe(&mut self, packet: &Packet) -> Result<()> { let Some(pmt) = self.pmts.get_mut(&packet.pid) else { return Ok(()); }; @@ -787,15 +787,12 @@ impl Programs { /// Create the broadcast of program `number`, its importer seeded with the PAT and carrying /// on the PMT `sections` began on `pmt_pid`. - fn start(&mut self, number: u16, pmt_pid: u16, sections: SectionReassembler) -> anyhow::Result<()> { + fn start(&mut self, number: u16, pmt_pid: u16, sections: SectionReassembler) -> Result<()> { let pat = self.pat.as_ref().expect("a program waits on its PAT"); let name = program_broadcast(self.name.as_str(), number); // Each program is its own publisher instance for this run, minted by its route, so a // restart replaces it rather than resuming into it. - let route = self - .origin - .route(&name, moq_net::origin::RouteOptions::hidden()) - .with_context(|| format!("failed to create broadcast {name}"))?; + let route = self.origin.route(&name, moq_net::origin::RouteOptions::hidden())?; // TS carries undecoded elementary streams verbatim, so each catalog carries the // `mpegts` extension, and owns its broadcast's catalog tracks. let config = self @@ -946,27 +943,29 @@ impl Programs { /// 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, so it can't be /// retried. - pub fn finish(mut self) -> anyhow::Result { + pub fn finish(mut self) -> Result { if let Err(err) = self.end() { - // 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:#}"))); + self.fail(err.clone().into()); return Err(err); } Ok(Finished { programs: self }) } /// Finish every program, stopping at the first failure. - fn end(&mut self) -> anyhow::Result<()> { + fn end(&mut self) -> Result<()> { self.demux.finish(); - anyhow::ensure!(self.pat.is_some(), "input ended before its PAT listed a program"); + if self.pat.is_none() { + return Err(Error::Empty("its PAT listed a program")); + } let mut announced = self.announced; for program in &mut self.programs { program.import.finish_in_place()?; announced |= program.announce_if_listed()?; program.catalog.finish()?; } - anyhow::ensure!(announced, "input ended before any program declared a track"); + if !announced { + return Err(Error::Empty("any program declared a track")); + } Ok(()) } @@ -1818,7 +1817,10 @@ mod test { steps.step(&mut programs, &[(1, 0x100)]); steps.mux.pat_in(2, &[(2, 0x200)]); let err = programs.decode(&std::mem::take(&mut steps.mux.out)).unwrap_err(); - assert!(format!("{err:#}").contains("transport_stream_id"), "{err:#}"); + assert!( + matches!(&err, Error::Unsupported(msg) if msg.contains("transport_stream_id")), + "{err}" + ); assert_eq!(programs.programs.len(), 1, "a refused PAT retires nothing"); } @@ -1849,7 +1851,10 @@ mod test { mux.pat(&[(1, 0x100)]); let err = programs.decode(&std::mem::take(&mut mux.out)).unwrap_err(); - assert!(format!("{err:#}").contains("without a media timestamp"), "{err:#}"); + assert!( + matches!(&err, Error::Malformed(msg) if msg.contains("without a media timestamp")), + "{err}" + ); programs.abort(moq_net::Error::Transport("decode failed".into())); let err = loop { match catalog.next().await { @@ -2316,7 +2321,7 @@ mod test { null.resize(188, 0xff); programs.decode(&null).unwrap(); let Err(err) = programs.finish() else { panic!("no PAT") }; - assert!(err.to_string().contains("PAT"), "{err:#}"); + assert!(matches!(err, Error::Empty("its PAT listed a program")), "{err}"); } /// PAT and PMT metadata can name elementary streams before their codec config is known. EOF @@ -2336,7 +2341,7 @@ mod test { let Err(err) = programs.end() else { panic!("the PMTs have no configured media") }; - assert!(err.to_string().contains("declared a track"), "{err:#}"); + assert!(matches!(err, Error::Empty("any program declared a track")), "{err}"); assert!( programs.programs.iter().all(|program| !program.route.is_announced()), "no empty program is announced" diff --git a/rs/moq-mux/src/container/ts/psi.rs b/rs/moq-mux/src/container/ts/psi.rs index adba67404bd..bcbcfca2dcc 100644 --- a/rs/moq-mux/src/container/ts/psi.rs +++ b/rs/moq-mux/src/container/ts/psi.rs @@ -8,10 +8,10 @@ use std::collections::hash_map::Entry; use std::collections::{BTreeMap, HashMap}; -use anyhow::Context; use mpeg2ts::ts::Pid; use super::catalog::Descriptor; +use super::error::{Context, Result, ensure, ensure_supported, unsupported}; /// The checksum closing every long-form section. pub(super) const CRC: crc::Crc = crc::Crc::::new(&crc::CRC_32_MPEG_2); @@ -26,7 +26,7 @@ pub(super) fn crc_ok(section: &[u8]) -> bool { } /// Encode the exporter's single-program PAT independently of TS packet boundaries. -pub(super) fn encode_pat(pat: &mpeg2ts::ts::payload::Pat) -> anyhow::Result { +pub(super) fn encode_pat(pat: &mpeg2ts::ts::payload::Pat) -> Result { let mut section = vec![PAT_TABLE_ID, 0xb0, 0]; section.extend_from_slice(&pat.transport_stream_id.to_be_bytes()); section.extend_from_slice(&[0xc1 | (pat.version_number.as_u8() << 1), 0, 0]); @@ -38,7 +38,7 @@ pub(super) fn encode_pat(pat: &mpeg2ts::ts::payload::Pat) -> anyhow::Result anyhow::Result { +pub(super) fn encode_pmt(pmt: &Pmt, version: mpeg2ts::ts::VersionNumber) -> Result { let mut section = vec![PMT_TABLE_ID, 0xb0, 0]; section.extend_from_slice(&pmt.program_number.to_be_bytes()); section.extend_from_slice(&[0xc1 | (version.as_u8() << 1), 0, 0]); @@ -52,24 +52,25 @@ pub(super) fn encode_pmt(pmt: &Pmt, version: mpeg2ts::ts::VersionNumber) -> anyh finish_section(section) } -fn encode_descriptors(out: &mut Vec, descriptors: &[Descriptor]) -> anyhow::Result<()> { +fn encode_descriptors(out: &mut Vec, descriptors: &[Descriptor]) -> Result<()> { let length = descriptors .iter() .map(|descriptor| 2 + descriptor.data.len()) .sum::(); - anyhow::ensure!(length <= 0x0fff, "PSI descriptor loop exceeds 4095 bytes"); + ensure_supported!(length <= 0x0fff, "PSI descriptor loop exceeds 4095 bytes"); out.extend_from_slice(&(0xf000 | length as u16).to_be_bytes()); for descriptor in descriptors { - let length = u8::try_from(descriptor.data.len()).context("PSI descriptor exceeds 255 bytes")?; + let length = + u8::try_from(descriptor.data.len()).map_err(|_| unsupported!("PSI descriptor exceeds 255 bytes"))?; out.extend_from_slice(&[descriptor.tag, length]); out.extend_from_slice(&descriptor.data); } Ok(()) } -fn finish_section(mut section: Vec) -> anyhow::Result { +fn finish_section(mut section: Vec) -> Result { let length = section.len() - 3 + 4; - anyhow::ensure!(length <= 1021, "PAT/PMT section exceeds 1021 bytes"); + ensure_supported!(length <= 1021, "PAT/PMT section exceeds 1021 bytes"); section[1] |= (length >> 8) as u8; section[2] = length as u8; let crc = CRC.checksum(§ion); @@ -286,9 +287,9 @@ pub(super) struct PesStart<'a> { impl<'a> PesStart<'a> { /// The next header boundary needed before parsing, capped by the 8-bit optional length. - pub fn header_len(payload: &[u8]) -> anyhow::Result { + pub fn header_len(payload: &[u8]) -> Result { let prefix = payload.len().min(3); - anyhow::ensure!( + ensure!( payload[..prefix] == [0, 0, 1][..prefix], "missing PES start code prefix" ); @@ -301,11 +302,11 @@ impl<'a> PesStart<'a> { if payload.len() < 9 { return Ok(9); } - anyhow::ensure!(payload[6] & 0xc0 == 0x80, "unexpected PES header marker bits"); - anyhow::ensure!(payload[7] & 0xc0 != 0x40, "PES DTS without a PTS"); + ensure!(payload[6] & 0xc0 == 0x80, "unexpected PES header marker bits"); + ensure!(payload[7] & 0xc0 != 0x40, "PES DTS without a PTS"); let len = usize::from(payload[8]); let packet_len = usize::from(u16::from_be_bytes([payload[4], payload[5]])); - anyhow::ensure!( + ensure!( packet_len == 0 || packet_len >= 3 + len, "PES packet length shorter than its header" ); @@ -318,9 +319,9 @@ impl<'a> PesStart<'a> { /// PES CRC, extensions, stuffing) are skipped by `PES_header_data_length`. Incomplete headers /// must be assembled before parsing; malformed headers are refused. A scrambled one parses, /// for its stream to refuse: [`Self::scrambled`]. - pub fn parse(payload: &'a [u8]) -> anyhow::Result { + pub fn parse(payload: &'a [u8]) -> Result { let header_len = Self::header_len(payload)?; - anyhow::ensure!(payload.len() >= header_len, "PES header truncated"); + ensure!(payload.len() >= header_len, "PES header truncated"); let stream_id = payload[3]; let packet_len = usize::from(u16::from_be_bytes([payload[4], payload[5]])); @@ -376,10 +377,10 @@ impl PesStart<'_> { /// the clock field it carries that only the PTS and DTS of are moved onto the catalog clock: /// ESCR, a `pack_header_field`, or a TREF (ISO 13818-1 2.4.3.7). Refuses an unbounded PES /// that is not video, and a flagged optional field that runs past `PES_header_data_length`. - pub fn whole(&self, stream_type: u8) -> anyhow::Result> { + pub fn whole(&self, stream_type: u8) -> Result> { let stream_id = self.stream_id; let video = matches!(stream_id, 0xe0..=0xef) || (stream_id == 0xfd && is_video_stream_type(stream_type)); - anyhow::ensure!( + ensure!( self.header[4..6] != [0, 0] || video, "PES_packet_length 0 on stream_id {stream_id:#04x}, which only a video stream may leave unbounded" ); @@ -427,7 +428,7 @@ impl PesStart<'_> { at += 1 + len; } } - anyhow::ensure!( + ensure!( at <= fields.len(), "PES optional fields need {at} bytes where PES_header_data_length gives {}", fields.len() @@ -453,10 +454,10 @@ pub(super) fn is_video_stream_type(stream_type: u8) -> bool { } /// A 33-bit PTS or DTS behind its 4-bit prefix and three marker bits. -fn timestamp(bytes: Option<&[u8]>, prefix: u8) -> anyhow::Result { +fn timestamp(bytes: Option<&[u8]>, prefix: u8) -> Result { let bytes = bytes.context("PES header truncated")?; - anyhow::ensure!(bytes[0] >> 4 == prefix, "unexpected PES timestamp prefix"); - anyhow::ensure!( + ensure!(bytes[0] >> 4 == prefix, "unexpected PES timestamp prefix"); + ensure!( bytes[0] & 1 == 1 && bytes[2] & 1 == 1 && bytes[4] & 1 == 1, "unexpected PES timestamp marker bit" ); diff --git a/rs/moq-mux/src/container/ts/release.rs b/rs/moq-mux/src/container/ts/release.rs index 4db7d842c3b..20398b29828 100644 --- a/rs/moq-mux/src/container/ts/release.rs +++ b/rs/moq-mux/src/container/ts/release.rs @@ -5,6 +5,8 @@ use std::time::Duration; use kio::clock::Instant; +use super::error::unsupported; + #[cfg(feature = "async")] use crate::catalog::CatalogFormat; use crate::catalog::Stream; @@ -49,9 +51,7 @@ impl Release { #[cfg(feature = "async")] pub async fn new(source: crate::Source, catalog_format: CatalogFormat, delay: Duration) -> crate::Result { if catalog_format == CatalogFormat::Msf { - return Err( - anyhow::anyhow!("the MSF catalog has no `m2ts` passthrough track; read the hang catalog").into(), - ); + return Err(unsupported!("the MSF catalog has no `m2ts` passthrough track; read the hang catalog").into()); } let mut subscribing = crate::catalog::Subscribing::new(&source.broadcast(), catalog_format); let catalog = kio::wait(|waiter| subscribing.poll_ok(waiter)).await?; @@ -189,7 +189,7 @@ impl Release { fn update_catalog(&mut self, catalog: &crate::catalog::hang::Catalog<()>) -> crate::Result<()> { let Some(m2ts) = &catalog.m2ts else { if self.track.is_none() && !(catalog.video.renditions.is_empty() && catalog.audio.renditions.is_empty()) { - return Err(anyhow::anyhow!( + return Err(unsupported!( "the broadcast is demultiplexed, with no `m2ts` passthrough track to release" ) .into()); @@ -211,7 +211,7 @@ impl Release { self.subscribed = kio::clock::now(); } Some((name, _)) if *name != m2ts.track => { - return Err(anyhow::anyhow!("the `m2ts` track moved from `{name}` to `{}`", m2ts.track).into()); + return Err(unsupported!("the `m2ts` track moved from `{name}` to `{}`", m2ts.track).into()); } Some(_) => {} } diff --git a/rs/moq-mux/src/container/ts/republish.rs b/rs/moq-mux/src/container/ts/republish.rs index 9bbdb13d73a..bbcaa29f9d3 100644 --- a/rs/moq-mux/src/container/ts/republish.rs +++ b/rs/moq-mux/src/container/ts/republish.rs @@ -1,8 +1,8 @@ //! One program of a multiplex, republished on its path whenever the source changes. -use anyhow::Context; use bytes::{Bytes, BytesMut}; +use super::error::{Error, Result}; use super::{Ext, Import, Step, stats}; use crate::catalog; @@ -49,11 +49,8 @@ struct Target { impl Target { /// Open an instance with `options`, its catalog recording `mux_rate` when the multiplex was /// measured already. - fn open(&self, options: moq_net::origin::RouteOptions, mux_rate: Option) -> anyhow::Result { - let route = self - .origin - .route(self.name.as_str(), options) - .with_context(|| format!("failed to create broadcast {}", self.name))?; + fn open(&self, options: moq_net::origin::RouteOptions, mux_rate: Option) -> Result { + let route = self.origin.route(self.name.as_str(), options)?; // TS carries undecoded elementary streams verbatim, so the catalog carries the `mpegts` // extension. let mut initial = catalog::hang::Catalog::::default(); @@ -86,7 +83,7 @@ struct Instance { impl Instance { /// Announce the broadcast once its catalog has gone out, returning whether it has. - fn announce_if_listed(&mut self) -> anyhow::Result { + fn announce_if_listed(&mut self) -> Result { listed(&mut self.pending, &self.route) } @@ -96,7 +93,7 @@ impl Instance { /// The `last` instance announces if its drain released the first catalog, and fails when the /// input declared no track. One a step ended is withdrawn as it drops, so it does neither: a /// source can step before its first catalog goes out, such as within the mux rate's window. - fn finish(mut self, last: bool) -> anyhow::Result { + fn finish(mut self, last: bool) -> Result { let import = match self.import.finish() { Ok(import) => import, Err(err) => { @@ -107,9 +104,9 @@ impl Instance { // The drain at end of input can release the first catalog. Without one the input declared // no track, and announcing would only advertise an empty broadcast. let declared = match last { - true => listed(&mut self.pending, &self.route).and_then(|declared| { - anyhow::ensure!(declared, "input ended before it declared a track"); - Ok(()) + true => listed(&mut self.pending, &self.route).and_then(|declared| match declared { + true => Ok(()), + false => Err(Error::Empty("it declared a track")), }), false => Ok(()), }; @@ -140,7 +137,7 @@ impl Republish { name: impl Into, instance: moq_net::Instance, config: catalog::Config, - ) -> anyhow::Result { + ) -> Result { let target = Target { origin, name: name.into(), @@ -187,7 +184,7 @@ impl Republish { /// the start of the next call. Call again, with no data if there is none, until it returns /// `None`. A failure to finish the instance before the step returns that failure, with the /// next instance current. - pub fn decode(&mut self, data: &[u8]) -> anyhow::Result> { + pub fn decode(&mut self, data: &[u8]) -> Result> { if !self.held.is_empty() { let held = std::mem::take(&mut self.held); if let Some(republished) = self.feed(&held)? { @@ -203,20 +200,35 @@ impl Republish { } /// Demux `data` into the current instance, or republish at the step it reaches. - fn feed(&mut self, data: &[u8]) -> anyhow::Result> { + fn feed(&mut self, data: &[u8]) -> Result> { let Some(restart) = self.instance.import.decode_or_restart(data)? else { self.instance.announce_if_listed()?; return Ok(None); }; + self.republish(restart).map(Some) + } + + /// No input follows: read what the current instance held while judging a step, and + /// republish at a step it reaches. + fn settle(&mut self) -> Result> { + match self.instance.import.settle()? { + Some(restart) => self.republish(restart).map(Some), + None => Ok(None), + } + } + + /// Finish the current instance before `restart`'s step and open the next, holding the input + /// from the step on for it. + fn republish(&mut self, restart: super::Restart) -> Result { let mux_rate = self.instance.catalog.snapshot().ext.mpegts.mux_rate; let next = self.target.open(moq_net::origin::RouteOptions::hidden(), mux_rate)?; self.held = restart.remainder; // The finished instance ends as it drops, withdrawing its announcement. let finished = std::mem::replace(&mut self.instance, next).finish(false)?; - Ok(Some(Republished { + Ok(Republished { step: restart.step, stats: finished.stats(), - })) + }) } /// What every elementary stream of the current instance has delivered; see [`Import::stats`]. @@ -227,12 +239,17 @@ impl Republish { /// Finish the current instance's media tracks, then its catalog while it still lists them, /// and return the [`Finished`] broadcast. /// - /// Input still held after a [`Republished`] is read first, republishing again at any step in - /// it. Fails when the input ended before the instance declared a track, so nothing was - /// announced. A finish that fails aborts every track with the cause. - pub fn finish(mut self) -> anyhow::Result { + /// Input still held after a [`Republished`], or while a step was judged, is read first, + /// republishing again at any step in it. Fails when the input ended before the instance + /// declared a track, so nothing was announced. A finish that fails aborts every track with + /// the cause. + pub fn finish(mut self) -> Result { loop { - match self.decode(&[]) { + let next = match self.decode(&[]) { + Ok(None) => self.settle(), + next => next, + }; + match next { Ok(Some(republished)) => { tracing::warn!(step = %republished.step, "the source changed at end of input; republishing") } @@ -254,10 +271,7 @@ impl Republish { } /// Announce `route` once the catalog track `pending` has a group, returning whether it has. -pub(super) fn listed( - pending: &mut Option, - route: &moq_net::origin::Route, -) -> anyhow::Result { +pub(super) fn listed(pending: &mut Option, route: &moq_net::origin::Route) -> Result { let Some(listed) = pending else { return Ok(true); }; @@ -267,7 +281,7 @@ pub(super) fn listed( ) { return Ok(false); } - route.announce().context("failed to announce broadcast")?; + route.announce()?; *pending = None; Ok(true) } @@ -300,7 +314,7 @@ impl Finished { #[cfg(test)] mod tests { - use super::super::step_test::{SWITCH, shift}; + use super::super::step_test::{SWITCH, flagged_switch, shift}; use super::*; /// 5 s of H.264 and AAC at a constant 400 kb/s, so the first instance measures a rate. @@ -399,6 +413,29 @@ mod tests { assert!(delivered(&finished.stats())); } + /// A switch the input ends while judging is found by `finish`, which republishes the + /// broadcast after it rather than drop it. + #[test] + fn a_switch_found_at_the_end_republishes() { + let origin = crate::source::produce_origin(); + let mut republish = Republish::new( + origin, + moq_net::Path::from_static("demo"), + moq_net::Instance::random(), + catalog::Config::default(), + ) + .unwrap(); + let first = republish.instance(); + let decoded = republish.decode(&flagged_switch()).unwrap(); + assert!(decoded.is_none(), "the switch is still judged: {decoded:?}"); + let finished = republish.finish().unwrap(); + assert_ne!(finished.route.instance(), first, "the finish republished"); + assert!( + delivered(&finished.stats()), + "the next instance read the input after the step" + ); + } + /// Held input that fails to decode at the finish aborts the instance reading it, so its /// readers get the cause rather than a clean end. #[tokio::test(start_paused = true)] @@ -432,10 +469,7 @@ mod tests { .await .unwrap(); let err = republish.finish().err().expect("two programs"); - assert!( - err.downcast_ref::().is_some(), - "{err:#}" - ); + assert!(matches!(err, Error::MultiplePrograms { .. }), "{err}"); let ended = kio::clock::timeout(std::time::Duration::from_secs(1), async { loop { if let Err(err) = catalog.recv_group().await.transpose()? { @@ -461,7 +495,7 @@ mod tests { .unwrap(); let route = republish.instance.route.clone(); let err = republish.finish().err().expect("nothing was declared"); - assert!(err.to_string().contains("declared"), "{err:#}"); + assert!(matches!(err, Error::Empty("it declared a track")), "{err}"); assert!(!route.is_announced()); } } diff --git a/rs/moq-mux/src/container/ts/schedule.rs b/rs/moq-mux/src/container/ts/schedule.rs index 1c97af19c70..e0de8935b11 100644 --- a/rs/moq-mux/src/container/ts/schedule.rs +++ b/rs/moq-mux/src/container/ts/schedule.rs @@ -4,12 +4,50 @@ use std::cmp::Reverse; use std::collections::{BinaryHeap, VecDeque}; use std::time::Duration; -use anyhow::Context; use mpeg2ts::ts::TsPacket; use rustc_hash::FxHashMap; use super::export::PCR_INTERVAL; +/// Why the schedule cannot lay a unit onto the grid: the multiplex needs more than the export +/// allows it. +#[derive(Clone, Debug, thiserror::Error)] +pub(super) enum Error { + /// A frame of a placed PES does not fit its decoder buffer. + #[error("MPEG-TS PES on PID {pid} holds a {largest}-byte frame, exceeding its {size}-byte decoder buffer")] + Frame { pid: u16, largest: usize, size: usize }, + /// An access unit does not fit its decoder buffer. + #[error( + "MPEG-TS access unit on PID {pid} has {elementary} payload bytes ({bytes} bytes with PES headers), exceeding its {size}-byte decoder buffer" + )] + Unit { + pid: u16, + elementary: usize, + bytes: usize, + size: usize, + }, + /// A unit missed its decode deadline at `rate`, which `delay` would have sent it ahead of. + #[error( + "MPEG-TS output missed a decode deadline on PID {pid} at {rate} b/s (estimated needed rate {needed} b/s); raise the delay to {delay:?}, which sends it as far ahead as its decoder buffer holds, or the multiplex rate" + )] + Late { + pid: u16, + rate: u64, + needed: u128, + delay: Duration, + }, + /// A unit missed its decode deadline at `rate`, sent as far ahead as its decoder buffer holds. + #[error( + "MPEG-TS output missed a decode deadline on PID {pid} at {rate} b/s (estimated needed rate {needed} b/s), sent as far ahead as its decoder buffer holds; raise the multiplex rate" + )] + Rate { pid: u16, rate: u64, needed: u128 }, + /// Program tables were led before the PMT PID and clock they go out with were set. + #[error("program tables led before they were set")] + Unset, +} + +type Result = std::result::Result; + /// Grid slots per second, so a multiplex rate in bits per second is also the per-slot /// allowance in [`PACKET`] units. const SLOTS_PER_SECOND: u64 = (Duration::from_secs(1).as_nanos() / PCR_INTERVAL.as_nanos()) as u64; @@ -849,7 +887,7 @@ impl Schedule { /// Queue an access unit of one frame. #[cfg(test)] - pub fn push(&mut self, pid: u16, timing: Timing, packets: Vec, keyframe: bool) -> anyhow::Result<()> { + pub fn push(&mut self, pid: u16, timing: Timing, packets: Vec, keyframe: bool) -> Result<()> { self.push_frames(pid, timing, packets, keyframe, Vec::new()) } @@ -864,7 +902,7 @@ impl Schedule { packets: Vec, keyframe: bool, frames: Vec<(u128, usize)>, - ) -> anyhow::Result<()> { + ) -> Result<()> { self.queue(pid, timing, None, packets, keyframe, frames) } @@ -883,7 +921,7 @@ impl Schedule { packets: Vec, keyframe: bool, frames: Vec<(u128, usize)>, - ) -> anyhow::Result<()> { + ) -> Result<()> { let timing = Timing { by: decode.max(place), decode, @@ -899,11 +937,11 @@ impl Schedule { packets: Vec, keyframe: bool, mut frames: Vec<(u128, usize)>, - ) -> anyhow::Result<()> { + ) -> Result<()> { let charges = std::mem::take(&mut self.charges); if let Some(tables) = self.lead.take() { - let pmt = self.pmt.context("program tables without a PMT PID")?; - let clock = self.psi.as_ref().context("program tables never set")?.clock; + let pmt = self.pmt.ok_or(Error::Unset)?; + let clock = self.psi.as_ref().ok_or(Error::Unset)?.clock; // Later tables wait too, so each version follows the one before it. let mut holds = tables.added; holds.extend([pid, pmt]); @@ -948,10 +986,9 @@ impl Schedule { // A placed PES's frames leave the buffer one by one as it streams in, so only each // has to fit; any other unit arrives whole by its first frame's decode time. if let Some(largest) = frames.iter().map(|&(_, bytes)| bytes).max().filter(|_| place.is_some()) { - anyhow::ensure!( - largest <= size, - "MPEG-TS PES on PID {pid} holds a {largest}-byte frame, exceeding its {size}-byte decoder buffer" - ); + if largest > size { + return Err(Error::Frame { pid, largest, size }); + } } else if bytes > size { // Only the error names the elementary size, so the PES headers are parsed here. let headers: usize = on_pid() @@ -961,9 +998,12 @@ impl Schedule { .map(|body| 9 + usize::from(body[8])) .sum(); let elementary = bytes.saturating_sub(headers); - anyhow::bail!( - "MPEG-TS access unit on PID {pid} has {elementary} payload bytes ({bytes} bytes with PES headers), exceeding its {size}-byte decoder buffer" - ); + return Err(Error::Unit { + pid, + elementary, + bytes, + size, + }); } } // The deadline its last frame would have on its own. @@ -1148,7 +1188,7 @@ impl Schedule { /// `known` is the first slot a unit still to be pushed could go out in ([`Self::release`]), /// which settles every slot before it; `None` means nothing more is coming, so every /// queued unit goes out and the clock runs on to the last decode time. - pub fn next(&mut self, known: Option) -> anyhow::Result> { + pub fn next(&mut self, known: Option) -> Result> { if known.is_none() && self.is_empty() { return Ok(None); } @@ -1256,7 +1296,7 @@ impl Schedule { /// Plan the packets and null positions together, against the actual transport clock. /// Decoder removal and the delivery deadline stay exact even between PCR grid points. - fn padded(&mut self, index: u128, rate: u64) -> anyhow::Result { + fn padded(&mut self, index: u128, rate: u64) -> Result { let first = packets_before(index, rate); let count = (packets_before(index + 1, rate) - first).max(1) as usize; let time = |position: usize| { @@ -1613,7 +1653,7 @@ impl Schedule { /// The error for a unit on `pid` that misses its decode deadline at `rate`, naming what /// would carry it: the delay at which it goes as far ahead as its decoder buffer holds, /// where the window cut it short, else a faster multiplex. - fn missed(&self, pid: u16, index: u128, rate: u64) -> anyhow::Error { + fn missed(&self, pid: u16, index: u128, rate: u64) -> Error { // Every slot since the last with a spare null position went out full, so the packets // released by now and still unsent built up over that stretch alone. Units not yet // released are the delay's lookahead, not a deficit. @@ -1632,13 +1672,13 @@ impl Schedule { .map(|buffer| slot(buffer.reach.as_nanos())) .filter(|&reach| reach > self.window); match reach { - Some(reach) => anyhow::anyhow!( - "MPEG-TS output missed a decode deadline on PID {pid} at {rate} b/s (estimated needed rate {needed} b/s); raise the delay to {:?}, which sends it as far ahead as its decoder buffer holds, or the multiplex rate", - PCR_INTERVAL * u32::try_from(reach).unwrap_or(u32::MAX) - ), - None => anyhow::anyhow!( - "MPEG-TS output missed a decode deadline on PID {pid} at {rate} b/s (estimated needed rate {needed} b/s), sent as far ahead as its decoder buffer holds; raise the multiplex rate" - ), + Some(reach) => Error::Late { + pid, + rate, + needed, + delay: PCR_INTERVAL * u32::try_from(reach).unwrap_or(u32::MAX), + }, + None => Error::Rate { pid, rate, needed }, } } diff --git a/rs/moq-mux/src/container/ts/si.rs b/rs/moq-mux/src/container/ts/si.rs index 22f6e61d653..70390fdfdb4 100644 --- a/rs/moq-mux/src/container/ts/si.rs +++ b/rs/moq-mux/src/container/ts/si.rs @@ -36,6 +36,7 @@ use bytes::Bytes; use moq_net::Timestamp; use super::catalog; +use super::error::{Result, bail}; use super::mux_rate::{MAX_INTERVAL, PCR_WRAP}; use super::passthrough::PCR_HZ; use super::psi::{self, CRC}; @@ -421,7 +422,7 @@ impl Entry { (pid, table_id): (u16, u8), pts: Timestamp, now: Instant, - ) -> anyhow::Result<()> { + ) -> Result<()> { let frames: Vec> = self .active .values() @@ -647,7 +648,7 @@ impl Capture { /// `pts` is the media clock, used to stamp the frames; the cut debounce runs on /// the host clock ([`DEBOUNCE`]), and a first snapshot is never delayed. `force` /// overrides the debounce, for end of stream. - pub fn flush(&mut self, pts: Timestamp, force: bool) -> anyhow::Result<()> { + pub fn flush(&mut self, pts: Timestamp, force: bool) -> Result<()> { let now = kio::clock::now(); // The max age this catalog mints media with, which its SI shares. let max_age = self.catalog.track_info(hang::catalog::PRIORITY.catalog).max_age; @@ -668,7 +669,7 @@ impl Capture { if self.entries.values().any(|e| e.advertised != e.populated) { let mut guard = self.catalog.modify()?; let Some(mpegts) = guard.ext.mpegts_mut() else { - anyhow::bail!("catalog extension no longer carries an mpegts section"); + bail!("catalog extension no longer carries an mpegts section"); }; for ((pid, table_id), entry) in self.entries.iter_mut() { // Either flag implies the track exists. @@ -695,7 +696,7 @@ impl Capture { } /// Flush everything and finish every track. - pub fn finish(&mut self, pts: Timestamp) -> anyhow::Result<()> { + pub fn finish(&mut self, pts: Timestamp) -> Result<()> { self.flush(pts, true)?; // Each entry keeps its own clone, whose appends the end refuses. for track in self.entries.values().filter_map(|entry| entry.track.as_ref()) { diff --git a/rs/moq-mux/src/container/ts/step.rs b/rs/moq-mux/src/container/ts/step.rs index 8fa7ef6466a..edb6236a5a4 100644 --- a/rs/moq-mux/src/container/ts/step.rs +++ b/rs/moq-mux/src/container/ts/step.rs @@ -25,6 +25,7 @@ use bytes::Bytes; use mpeg2ts::ts::{Pid, TsPacket}; use super::demux::Demux; +use super::error::Result; use super::health::{Continuation, Continuity, Delta, PTS_LIMIT}; use super::import::{Framer, Payload, STAGING_BYTES_MAX, adaptation_valid, discontinuity_indicator, payload, pcr}; use super::mux_rate::PCR_WRAP; @@ -39,12 +40,10 @@ const PTS_WRAP: u64 = 1 << 33; /// The source's clock stepped onto another timeline, or its PAT named another program, so the /// [`Import`](super::Import) ended its instance there. /// -/// What follows is another broadcast. [`Republish`](super::Republish) carries on into a new -/// instance on the same path. -#[derive(Clone, Debug, thiserror::Error)] -#[non_exhaustive] -#[error("the source {step}; what follows is a new broadcast")] -pub struct Restart { +/// Only [`Republish`](super::Republish) sees it, and reads [`remainder`](Self::remainder) into a +/// new instance on the same path; a raw import fails with [`Error::Restarted`](super::Error::Restarted). +#[derive(Clone, Debug)] +pub(super) struct Restart { /// How the source changed. pub step: Step, /// The input from the step on, which this import did not read. diff --git a/rs/moq-mux/src/container/ts/step_test.rs b/rs/moq-mux/src/container/ts/step_test.rs index 800c78d48fe..4c244660880 100644 --- a/rs/moq-mux/src/container/ts/step_test.rs +++ b/rs/moq-mux/src/container/ts/step_test.rs @@ -116,30 +116,91 @@ fn replay(gap: i64, by: impl Fn(Clock) -> i64) -> (Vec, usize) { (data, CLIP.len()) } -/// The importer, its catalog kept alive, fed `data` in `chunk`-byte pieces until it fails. -/// Returns the failure, if any, and the input it did not take: a restart's remainder followed -/// by the pieces never fed. -fn feed(data: &[u8], chunk: usize) -> (Option, Vec) { +/// The importer, its catalog kept alive, fed `data` in `chunk`-byte pieces until it restarts or +/// fails. A restart comes with the input the importer did not take: its remainder followed by +/// the pieces never fed. +fn feed(data: &[u8], chunk: usize) -> Result)>, super::Error> { + let (_catalog, mut import) = open(); + for (index, piece) in data.chunks(chunk).enumerate() { + if let Some(restart) = import.decode_or_restart(piece)? { + let mut rest = restart.remainder.to_vec(); + rest.extend(data.chunks(chunk).skip(index + 1).flatten()); + // The instance before the step ends cleanly. + let _finished = import.finish().unwrap(); + return Ok(Some((restart, rest))); + } + } + let _finished = import.finish()?; + Ok(None) +} + +/// An importer onto a fresh broadcast, with the catalog it must outlive. +fn open() -> (crate::catalog::Producer, Import) { let mut broadcast = moq_net::broadcast::Info::new().produce(); let catalog = crate::catalog::Producer::new( &mut broadcast, crate::catalog::Config::default().with_catalog(crate::catalog::hang::Catalog::::default()), ) .unwrap(); - let mut import = Import::new(broadcast, catalog.reserve()); - for (index, piece) in data.chunks(chunk).enumerate() { - if let Err(err) = import.decode(piece) { - let Some(restart) = err.downcast_ref::() else { - return (Some(err), Vec::new()); - }; - let mut rest = restart.remainder.to_vec(); - rest.extend(data.chunks(chunk).skip(index + 1).flatten()); - // The instance before the step ends cleanly. - let _finished = import.finish().unwrap(); - return (Some(err), rest); + let import = Import::new(broadcast, catalog.reserve()); + (catalog, import) +} + +/// Edit the one PSI section `pkt` starts behind its pointer_field, then mend its CRC. +fn mend(pkt: &mut [u8; 188], edit: impl FnOnce(&mut [u8])) { + assert_eq!(pkt[1] & 0x40, 0x40, "a section starts here"); + let payload = if pkt[3] & 0x20 != 0 { 5 + usize::from(pkt[4]) } else { 4 }; + let section = payload + 1 + usize::from(pkt[payload]); + let len = 3 + (usize::from(pkt[section + 1] & 0x0f) << 8 | usize::from(pkt[section + 2])); + edit(&mut pkt[section..section + len - 4]); + let crc = super::psi::CRC.checksum(&pkt[section..section + len - 4]); + pkt[section + len - 4..section + len].copy_from_slice(&crc.to_be_bytes()); +} + +/// The clip with its PAT naming `transport_stream_id` 7. +fn retagged(ts: &[u8]) -> Vec { + let mut out = ts.to_vec(); + for pkt in out.as_chunks_mut::<188>().0.iter_mut().filter(|pkt| pid(&pkt[..]) == 0) { + mend(pkt, |section| section[3..5].copy_from_slice(&7u16.to_be_bytes())); + } + out +} + +/// The clip, then a `discontinuity_indicator` on its clock PID, then the clip again as another +/// source: its PAT names `transport_stream_id` 7 and its streams and clock sit on other PIDs. +/// The old PIDs never speak again, so the flagged break stays judged until the input ends. +pub(super) fn flagged_switch() -> Vec { + const PCR_PID: u16 = 0x100; + const PMT_PID: u16 = 0x1000; + let moved = |pid: u16| if matches!(pid, 0x100 | 0x101) { pid + 0x100 } else { pid }; + let mut second = retagged(CLIP); + for pkt in second.as_chunks_mut::<188>().0 { + let pid = moved(pid(&pkt[..])); + pkt[1] = (pkt[1] & 0xe0) | (pid >> 8) as u8; + pkt[2] = pid as u8; + if pid == PMT_PID && pkt[1] & 0x40 != 0 { + mend(pkt, |section| { + let write = |section: &mut [u8], at: usize| { + let pid = moved(u16::from_be_bytes([section[at], section[at + 1]]) & 0x1fff); + section[at] = (section[at] & 0xe0) | (pid >> 8) as u8; + section[at + 1] = pid as u8; + }; + write(section, 8); + let mut at = 12 + (usize::from(section[10] & 0x0f) << 8 | usize::from(section[11])); + while at < section.len() { + write(section, at + 1); + at += 5 + (usize::from(section[at + 3] & 0x0f) << 8 | usize::from(section[at + 4])); + } + }); } } - (import.finish().err(), Vec::new()) + let mut data = CLIP.to_vec(); + // An adaptation field alone, flagging the break. + let mut flagged = [0xff; 188]; + flagged[..6].copy_from_slice(&[0x47, (PCR_PID >> 8) as u8, PCR_PID as u8, 0x20, 183, 0x80]); + data.extend_from_slice(&flagged); + data.extend_from_slice(&second); + data } /// The input from the first packet of `pass` that carries a timestamp. @@ -160,9 +221,7 @@ fn an_unflagged_switch_restarts_either_way() { for (gap, expected) in [(-SWITCH, "rewind"), (SWITCH, "leap")] { let (data, second) = replay(gap, |_| 0); for chunk in [data.len(), 188 * 7, 1_000] { - let (err, rest) = feed(&data, chunk); - let err = err.expect("the switch ends the instance"); - let restart = err.downcast_ref::().expect("a restart"); + let (restart, rest) = feed(&data, chunk).unwrap().expect("the switch ends the instance"); let by = match (restart.step, expected) { (Step::Rewind { by, flagged: false }, "rewind") | (Step::Leap { by }, "leap") => by, (step, _) => panic!("{expected}: {step:?}"), @@ -176,30 +235,48 @@ fn an_unflagged_switch_restarts_either_way() { assert!(rest.len() >= first_timed(&data[second..]).len(), "{expected} {chunk}"); // What follows imports as a broadcast of its own. - let (err, _) = feed(&rest, chunk); - assert!(err.is_none(), "{expected}: {err:?}"); + let fed = feed(&rest, chunk); + assert!(matches!(fed, Ok(None)), "{expected}: {fed:?}"); } } } +/// A raw import has no next instance to carry on into, so a switch fails `decode`, and every +/// later call, while `finish` still ends the instance cleanly before the step. +#[test] +fn a_switch_fails_a_raw_import() { + let (data, _) = replay(SWITCH, |_| 0); + let (_catalog, mut import) = open(); + let err = import.decode(&data).unwrap_err(); + assert!(matches!(err, super::Error::Restarted(Step::Leap { .. })), "{err:?}"); + let again = import.decode(&[]).unwrap_err(); + assert!(matches!(again, super::Error::Restarted(Step::Leap { .. })), "{again:?}"); + let _finished = import.finish().unwrap(); +} + +/// A switch the input ends while judging is found by `finish`, which fails as `decode` would +/// have rather than drop the broadcast after the step. +#[test] +fn a_switch_found_at_the_end_fails_a_raw_finish() { + let (_catalog, mut import) = open(); + import.decode(&flagged_switch()).unwrap(); + let err = import.finish().err().expect("the switch fails the finish"); + let identity = Step::Identity { + transport_stream_id: 7, + program_number: 1, + }; + assert!( + matches!(&err, super::Error::Restarted(step) if *step == identity), + "{err:?}" + ); +} + /// A source switch whose PAT names another `transport_stream_id` before its first clock /// reading restarts at that PAT, where the old program's identity check would otherwise fail /// the whole import. The new instance reads the PAT itself, however the input is split. #[test] fn another_identity_restarts_at_its_pat() { - let mut second = shift(CLIP, |_| -SWITCH); - for pkt in second.as_chunks_mut::<188>().0 { - if pid(pkt) != 0 { - continue; - } - // A single-section PAT behind a zero pointer_field: retag its transport_stream_id. - assert_eq!(pkt[1] & 0x40, 0x40); - let section = 5 + usize::from(pkt[4]); - let len = 3 + (usize::from(pkt[section + 1] & 0x0f) << 8 | usize::from(pkt[section + 2])); - pkt[section + 3..section + 5].copy_from_slice(&7u16.to_be_bytes()); - let crc = super::psi::CRC.checksum(&pkt[section..section + len - 4]); - pkt[section + len - 4..section + len].copy_from_slice(&crc.to_be_bytes()); - } + let second = retagged(&shift(CLIP, |_| -SWITCH)); let pat = second .as_chunks::<188>() .0 @@ -214,11 +291,9 @@ fn another_identity_restarts_at_its_pat() { let mut data = CLIP.to_vec(); data.extend_from_slice(&second); for chunk in [data.len(), 188 * 7, 1_000] { - let (err, rest) = feed(&data, chunk); - let err = err.expect("the switch ends the instance"); - let restart = err - .downcast_ref::() - .unwrap_or_else(|| panic!("{chunk}: {err:#}")); + let (restart, rest) = feed(&data, chunk) + .unwrap() + .unwrap_or_else(|| panic!("{chunk}: the switch ends the instance")); assert_eq!( restart.step, Step::Identity { @@ -228,8 +303,8 @@ fn another_identity_restarts_at_its_pat() { "{chunk}" ); assert_eq!(rest, second[pat..], "{chunk}: the new instance starts at its PAT"); - let (err, _) = feed(&rest, chunk); - assert!(err.is_none(), "{chunk}: {err:?}"); + let fed = feed(&rest, chunk); + assert!(matches!(fed, Ok(None)), "{chunk}: {fed:?}"); } } @@ -238,15 +313,15 @@ fn another_identity_restarts_at_its_pat() { #[test] fn a_step_under_the_bound_carries_on() { let (data, _) = replay(9 * HZ, |_| 0); - let (err, _) = feed(&data, 188 * 7); - assert!(err.is_none(), "{err:?}"); + let fed = feed(&data, 188 * 7); + assert!(matches!(fed, Ok(None)), "{fed:?}"); // Two seconds of the clip lost in the middle. let packets = CLIP.len() / 188; let mut lossy = CLIP[..packets / 3 * 188].to_vec(); lossy.extend_from_slice(&CLIP[packets * 2 / 3 * 188..]); - let (err, _) = feed(&lossy, 188 * 7); - assert!(err.is_none(), "{err:?}"); + let fed = feed(&lossy, 188 * 7); + assert!(matches!(fed, Ok(None)), "{fed:?}"); } /// A stream the new source does not carry is waited for only as long as a cadenced stream may @@ -266,9 +341,7 @@ fn a_stream_the_new_source_lacks_is_not_waited_for() { data.truncate(second); data.extend(&without); - let (err, rest) = feed(&data, 188 * 7); - let err = err.expect("the switch ends the instance"); - let restart = err.downcast_ref::().expect("a restart"); + let (restart, rest) = feed(&data, 188 * 7).unwrap().expect("the switch ends the instance"); assert!( matches!(restart.step, Step::Rewind { flagged: false, .. }), "{restart:?}" @@ -281,8 +354,8 @@ fn a_stream_the_new_source_lacks_is_not_waited_for() { #[test] fn a_clock_stepping_alone_is_no_switch() { let (data, _) = replay(HZ, |clock| if clock == Clock::Pcr { -SWITCH } else { 0 }); - let (err, _) = feed(&data, 188 * 7); - assert!(err.as_ref().is_none_or(|err| !err.is::()), "{err:?}"); + let fed = feed(&data, 188 * 7); + assert!(matches!(fed, Ok(None)), "{fed:?}"); // The audio carries on a second after the first pass while the rest rewinds. let audio = audio_pid(CLIP); @@ -290,8 +363,8 @@ fn a_clock_stepping_alone_is_no_switch() { -SWITCH, |clock| if clock == Clock::Pes(audio) { SWITCH + HZ } else { 0 }, ); - let (err, _) = feed(&data, 188 * 7); - assert!(err.as_ref().is_none_or(|err| !err.is::()), "{err:?}"); + let fed = feed(&data, 188 * 7); + assert!(matches!(fed, Ok(None)), "{fed:?}"); } /// The PID of the clip's audio: the PES that are not video. diff --git a/rs/moq-mux/src/container/ts/video.rs b/rs/moq-mux/src/container/ts/video.rs index 7fdce207e84..093a14293ec 100644 --- a/rs/moq-mux/src/container/ts/video.rs +++ b/rs/moq-mux/src/container/ts/video.rs @@ -8,8 +8,9 @@ use std::collections::VecDeque; use bytes::{Buf, Bytes, BytesMut}; use moq_net::Timestamp; -use super::{Pending, Retained, on_clock, reorder_delay, unit_error}; +use super::{Fault, Pending, Retained, on_clock, reorder_delay, unit_error}; use crate::codec::annexb::{START_CODE, after_start_code, find_start_code}; +use crate::container::ts::error::{Error, Result, ensure, ensure_supported, malformed}; #[derive(Clone, Copy)] pub(super) enum Codec { @@ -27,7 +28,7 @@ impl Codec { /// Whether this NAL begins a picture or its leading non-VCL data /// (H.264 7.4.1.2.3, H.265 7.4.2.4.4), and whether it is a slice. - fn boundary(self, nal: &[u8]) -> anyhow::Result<(bool, bool)> { + fn boundary(self, nal: &[u8]) -> Result<(bool, bool), Fault> { match self { Self::Avc => { let header = *nal @@ -98,7 +99,7 @@ mod timing; pub(super) struct Assembly { pub clock: timing::Clock, - damage: Option<(u64, anyhow::Error)>, + damage: Option<(u64, Error)>, codec: Codec, tail: BytesMut, searched: usize, @@ -129,7 +130,7 @@ impl Assembly { } } - pub fn push(&mut self, mut pending: Pending) -> anyhow::Result<()> { + pub fn push(&mut self, mut pending: Pending) -> Result<()> { let pts = on_clock(pending.pts, pending.offset)?; let reorder = reorder_delay(pending.pts, pending.dts); if pending.data.is_empty() { @@ -160,7 +161,7 @@ impl Assembly { self.account() } - fn account(&mut self) -> anyhow::Result<()> { + fn account(&mut self) -> Result<()> { if let Some(retained) = &mut self.retained { retained.resize( self.tail.len() + self.current.len(), @@ -171,7 +172,7 @@ impl Assembly { } /// Return one AU at a time so later parameter sets cannot precede its validation. - pub fn next(&mut self, eof: bool) -> anyhow::Result> { + pub fn next(&mut self, eof: bool) -> Result, Fault> { loop { if self.tail.is_empty() { return if eof { self.emit() } else { Ok(None) }; @@ -288,7 +289,7 @@ impl Assembly { } } - fn emit(&mut self) -> anyhow::Result> { + fn emit(&mut self) -> Result, Fault> { if !self.has_slice { return Ok(None); } @@ -310,7 +311,7 @@ impl Assembly { })) } - pub fn take_damage(&mut self) -> Option<(u64, anyhow::Error)> { + pub fn take_damage(&mut self) -> Option<(u64, Error)> { self.damage.take() } @@ -337,22 +338,22 @@ impl Assembly { /// One proportional AVC clock must be declared explicitly by the TS descriptor; /// an SPS fixed frame rate alone says nothing about numeric POC distance. -pub(super) fn temporal_poc(descriptors: &[super::catalog::Descriptor]) -> anyhow::Result { +pub(super) fn temporal_poc(descriptors: &[super::catalog::Descriptor]) -> Result { let mut proportional = false; for descriptor in descriptors.iter().filter(|d| d.tag == 0x2a) { let data = &descriptor.data; let first = *data .first() - .ok_or_else(|| anyhow::anyhow!("truncated AVC timing descriptor"))?; + .ok_or_else(|| malformed!("truncated AVC timing descriptor"))?; let len = if first & 1 == 0 { 2 } else { let clock = *data .get(1) - .ok_or_else(|| anyhow::anyhow!("truncated AVC timing descriptor"))?; + .ok_or_else(|| malformed!("truncated AVC timing descriptor"))?; if clock & 0x80 != 0 { 7 } else { 15 } }; - anyhow::ensure!(data.len() == len, "invalid AVC timing descriptor length"); + ensure!(data.len() == len, "invalid AVC timing descriptor length"); proportional |= data[len - 1] & 0xc0 == 0xc0; } Ok(proportional) @@ -367,7 +368,7 @@ macro_rules! publish { import: &mut crate::codec::$codec::Import, assembly: &mut Assembly, eof: bool, - ) -> anyhow::Result { + ) -> Result { let mut published = 0; let result = (|| { while let Some(unit) = assembly.next(eof)? { @@ -378,7 +379,7 @@ macro_rules! publish { let provisional = unit.pts.unwrap_or(Timestamp::ZERO); let mut frames = split.decode(&unit.data, provisional).map_err(unit_error)?; frames.extend(split.flush(provisional).map_err(unit_error)?); - anyhow::ensure!(frames.len() == 1, "unsupported TS video access-unit framing"); + ensure_supported!(frames.len() == 1, "unsupported TS video access-unit framing"); for frame in &frames { if frame.keyframe { crate::codec::$codec::config(&frame.payload) @@ -395,14 +396,14 @@ macro_rules! publish { frame.timestamp = pts; published += u64::from(super::skip_missing_keyframe(import.decode([frame]))?); } - anyhow::Ok(published) + Ok::<_, Fault>(published) })(); match result { Ok(units) => { published += units; assembly.clock.commit(); } - Err(err) if err.is::() => { + Err(Fault::Damaged { cause, .. }) => { // This AU has a known end. Keep the next AU's bytes, // which may already be here in the same or following PES. if let Some(params) = params { @@ -412,7 +413,7 @@ macro_rules! publish { assembly.clock.reset(); import.cut(None)?; let count = assembly.damage.take().map_or(1, |(count, _)| count + 1); - assembly.damage = Some((count, err)); + assembly.damage = Some((count, cause)); continue; } Err(err) => return Err(err), @@ -424,12 +425,12 @@ macro_rules! publish { assembly.clock.reset(); } } - anyhow::Ok(()) + Ok::<_, Fault>(()) })(); match result { Ok(()) => Ok(published), - Err(err) if err.is::() => Err(err.context(Published(published))), - Err(err) => Err(err), + Err(Fault::Damaged { cause, .. }) => Err(Fault::Damaged { cause, published }), + Err(Fault::Fatal(err)) => Err(Fault::Fatal(err)), } } }; @@ -437,14 +438,6 @@ macro_rules! publish { publish!(avc, h264); publish!(hevc, h265); -#[derive(Debug, thiserror::Error)] -#[error("damaged TS video after {0} published access units")] -struct Published(u64); - -pub(super) fn published(err: &anyhow::Error) -> u64 { - err.downcast_ref::().map_or(0, |published| published.0) -} - #[cfg(test)] mod tests { use super::*; diff --git a/rs/moq-mux/src/container/ts/video_timing.rs b/rs/moq-mux/src/container/ts/video_timing.rs index 22e9ae86def..72e17061931 100644 --- a/rs/moq-mux/src/container/ts/video_timing.rs +++ b/rs/moq-mux/src/container/ts/video_timing.rs @@ -18,6 +18,7 @@ use h264_reader::rbsp::{BitRead, BitReader, BitReaderError}; use moq_net::Timestamp; use super::Unit; +use crate::container::ts::error::{Result, malformed}; #[derive(Clone, Copy, PartialEq, Eq)] struct Rate { @@ -62,20 +63,14 @@ impl Clock { } } - fn stamp( - &mut self, - explicit: Option, - poc: i64, - rate: Option, - reset: bool, - ) -> anyhow::Result { + fn stamp(&mut self, explicit: Option, poc: i64, rate: Option, reset: bool) -> Result { if reset { self.anchor = None; } let Some(rate) = rate else { self.anchor = None; return explicit.ok_or_else(|| { - anyhow::anyhow!("TS video access unit has no timestamp or signalled proportional POC clock") + malformed!("TS video access unit has no timestamp or signalled proportional POC clock") }); }; if let Some(pts) = explicit { @@ -85,17 +80,17 @@ impl Clock { let anchor = self .anchor .filter(|anchor| anchor.rate == rate) - .ok_or_else(|| anyhow::anyhow!("TS video access unit has no timestamp anchor for its POC clock"))?; + .ok_or_else(|| malformed!("TS video access unit has no timestamp anchor for its POC clock"))?; // Always measure from the explicit anchor, retaining fractional ticks rather // than accumulating the rounding error of a frame-by-frame increment. let scale = anchor .pts .scale() - .ok_or_else(|| anyhow::anyhow!("TS video timestamp anchor has no timescale"))?; + .ok_or_else(|| malformed!("TS video timestamp anchor has no timescale"))?; let scale = i128::from(scale.as_u64()); let denominator = scale .checked_mul(rate.denominator.into()) - .ok_or_else(|| anyhow::anyhow!("TS video timestamp overflow"))?; + .ok_or_else(|| malformed!("TS video timestamp overflow"))?; let delta = (i128::from(poc) - i128::from(anchor.poc)) .checked_mul(i128::from(rate.numerator)) .and_then(|n| n.checked_mul(scale)); @@ -104,11 +99,11 @@ impl Clock { .and_then(|n| n.checked_add(delta?)) .and_then(|n| n.checked_mul(1_000_000)) .and_then(|n| u64::try_from(n / denominator).ok()) - .ok_or_else(|| anyhow::anyhow!("TS video timestamp outside the media clock"))?; + .ok_or_else(|| malformed!("TS video timestamp outside the media clock"))?; Ok(Timestamp::from_micros(micros)?) } - pub fn avc<'a>(&mut self, unit: &Unit, params: impl Iterator) -> anyhow::Result { + pub fn avc<'a>(&mut self, unit: &Unit, params: impl Iterator) -> Result { let parsed = (|| { let mut ctx = self.avc_ctx.clone().unwrap_or_default(); let bootstrap: Vec<_> = if self.avc_ctx.is_none() { @@ -184,7 +179,7 @@ impl Clock { self.avc_update = Some(None); return unit .pts - .ok_or_else(|| anyhow::anyhow!("TS video timestamp cannot be derived: {err}")); + .ok_or_else(|| malformed!("TS video timestamp cannot be derived: {err}")); } }; let pts = self.stamp(unit.pts, poc, rate, reset)?; @@ -196,7 +191,7 @@ impl Clock { Ok(pts) } - pub fn hevc<'a>(&mut self, unit: &Unit, params: impl Iterator) -> anyhow::Result { + pub fn hevc<'a>(&mut self, unit: &Unit, params: impl Iterator) -> Result { let parsed = (|| { let mut ctx = self.hevc_ctx.clone().unwrap_or_default(); let bootstrap: Vec<_> = if self.hevc_ctx.is_none() { @@ -259,7 +254,7 @@ impl Clock { self.reset(); self.hevc_update = Some(None); unit.pts - .ok_or_else(|| anyhow::anyhow!("TS video timestamp cannot be derived: {err}")) + .ok_or_else(|| malformed!("TS video timestamp cannot be derived: {err}")) } } } diff --git a/rs/moq-mux/src/error.rs b/rs/moq-mux/src/error.rs index 0d93dc43c5d..0b7337c53cb 100644 --- a/rs/moq-mux/src/error.rs +++ b/rs/moq-mux/src/error.rs @@ -48,11 +48,15 @@ pub enum Error { #[error("cmaf: {0}")] Cmaf(#[from] crate::container::fmp4::Error), + /// Error importing or exporting MPEG-TS. + #[error("ts: {0}")] + Ts(crate::container::ts::Error), + /// Error parsing or building MKV / WebM streams. #[error("mkv: {0}")] Mkv(#[from] crate::container::mkv::Error), - /// An FLV export error. + /// Error importing or exporting FLV. #[error("flv: {0}")] Flv(#[from] crate::container::flv::Error), @@ -150,6 +154,23 @@ pub enum Error { wanted: &'static str, }, + /// Video does not fit the reorder depth it declares. + #[error("{0}")] + Reorder(#[from] crate::container::ReorderError), + + /// An export's source clock runs further off the output's than the output may follow. + #[error("the source's clock runs {ppm:+.1} ppm off ours, past the {limit:.0} ppm the output's may follow")] + Drift { + /// How far off the source's clock runs, in parts per million. + ppm: f64, + /// The most the output's clock follows, in parts per million. + limit: f64, + }, + + /// A publisher an export handed off from queued media after its handoff deadline. + #[error("a retired publisher queued after its handoff deadline")] + Retired, + /// A non-keyframe frame was received before any keyframe opened a group. /// A track joining mid-stream should skip frames until the first keyframe. #[error("{0}")] @@ -176,11 +197,6 @@ pub enum Error { composition_time_ms: i32, }, - /// Error from a muxer/demuxer that reports via `anyhow` (currently the MPEG-TS demuxer). - /// Boxed in an `Arc` so the enum stays `Clone` (`anyhow::Error` is not). - #[error("{0}")] - Other(std::sync::Arc), - /// A timestamp cannot be mapped between the broadcast clock and the wall: PTS zero would land /// before the moq epoch (2020) or after now, the host's wall clock was unset so the clock has no /// mapping, or a catalog root `clock` would change or clear the producer's fixed clock. @@ -321,9 +337,28 @@ impl Error { } } -impl From for Error { - fn from(err: anyhow::Error) -> Self { - Error::Other(std::sync::Arc::new(err)) +impl From for Error { + fn from(err: crate::jitter::Error) -> Self { + match err { + crate::jitter::Error::Drift { ppm, limit } => Self::Drift { ppm, limit }, + crate::jitter::Error::Retired => Self::Retired, + } + } +} + +impl From for Error { + fn from(err: crate::codec::aac::adts::Error) -> Self { + Self::Aac(err.into()) + } +} + +impl From for Error { + /// A TS error wrapping one of these unwraps to it, so neither nests in the other. + fn from(err: crate::container::ts::Error) -> Self { + match err { + crate::container::ts::Error::Mux(err) => *err, + err => Self::Ts(err), + } } } diff --git a/rs/moq-mux/src/import/container.rs b/rs/moq-mux/src/import/container.rs index e6a988dc1fd..3bcc382c9dd 100644 --- a/rs/moq-mux/src/import/container.rs +++ b/rs/moq-mux/src/import/container.rs @@ -50,7 +50,7 @@ impl ContainerImpl { match self { ContainerImpl::Fmp4(decoder) => decoder.decode(data), ContainerImpl::Mkv(decoder) => decoder.decode(data), - ContainerImpl::Ts(decoder) => decoder.decode(data).map_err(Into::into), + ContainerImpl::Ts(decoder) => Ok(decoder.decode(data)?), ContainerImpl::Flv(decoder) => decoder.decode(data), } } diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index 049ced2980d..52c1185b805 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -10,6 +10,17 @@ use std::time::Duration; use kio::clock::{Instant, Sleep}; use moq_net::Timestamp; +/// Why the buffer refuses a frame. +#[derive(Clone, Debug, thiserror::Error)] +pub(crate) enum Error { + /// The source's clock runs further off ours than the output's may follow. + #[error("the source's clock runs {ppm:+.1} ppm off ours, past the {limit:.0} ppm the output's may follow")] + Drift { ppm: f64, limit: f64 }, + /// A publisher handed off from queued a frame after its handoff deadline. + #[error("a retired publisher queued after its handoff deadline")] + Retired, +} + /// What each track may hold before its reader is held back, as the container consumer's max /// buffer: the most one group may cache. pub(crate) const MAX_BYTES: usize = 32 * 1024 * 1024; @@ -125,12 +136,11 @@ impl Buffer { } } - pub fn push(&mut self, key: K, arrival: Arrival) -> anyhow::Result { - if let Some(Handoff::Committed(committed)) = self.handoff.as_deref() { - anyhow::ensure!( - committed.keys.contains(&key), - "a retired publisher queued after its handoff deadline" - ); + pub fn push(&mut self, key: K, arrival: Arrival) -> Result { + if let Some(Handoff::Committed(committed)) = self.handoff.as_deref() + && !committed.keys.contains(&key) + { + return Err(Error::Retired); } self.writing(&key).push(key, arrival) } @@ -856,7 +866,7 @@ impl Engine { /// Queue a frame, or drop it if it cannot make its deadline. Fails once the source's clock /// runs further off ours than the clock may follow. - pub fn push(&mut self, key: K, arrival: Arrival) -> anyhow::Result { + pub fn push(&mut self, key: K, arrival: Arrival) -> Result { let held = self.acquire.as_ref().and_then(|acquire| acquire.tracks.get(&key)); // A restart behind what the track already holds cannot decode in order on the clock the // acquisition would anchor, so the acquisition ends at it and the frame opens the next. @@ -1164,7 +1174,7 @@ impl Engine { /// Account for the slack a frame arrived with, and every [`STEER`] of our time step the /// clock's drift toward the source's rate plus a pull back to the delay, as far as /// [`MAX_SLEW`] allows. - fn steer(&mut self, key: &K, deadline: Option, arrival: &Arrival) -> anyhow::Result<()> { + fn steer(&mut self, key: &K, deadline: Option, arrival: &Arrival) -> Result<(), Error> { let (Some(clock), Some(deadline)) = (self.clock, deadline) else { return Ok(()); }; @@ -1294,11 +1304,10 @@ impl Engine { self.out_of_tolerance += 1; // The clock can never catch up, so fail once the rate has cost half the delay. if lost * slow.signum() < -delay / 2.0 { - anyhow::bail!( - "the source's clock runs {:+.1} ppm off ours, past the {:.0} ppm the output's may follow", - rate * 1e6, - MAX_DRIFT * 1e6 - ); + return Err(Error::Drift { + ppm: rate * 1e6, + limit: MAX_DRIFT * 1e6, + }); } } Ok(()) @@ -2789,7 +2798,7 @@ mod tests { /// What a run against a drifting source saw. struct Run { /// The error the run ended on, if it failed. - err: Option, + err: Option, /// The largest gap between a frame's slack and the delay, in seconds, from `settled` /// hours on. worst: f64, @@ -2979,7 +2988,7 @@ mod tests { /// What [`feed`] saw. struct Fed { /// The decode time a push failed at, in seconds, and its error. - err: Option<(f64, anyhow::Error)>, + err: Option<(f64, Error)>, dropped: u64, /// What the last push did. last: Option, @@ -3147,7 +3156,7 @@ mod tests { /// What [`unmarked`] saw. struct Stepped { - err: Option, + err: Option, dropped: u64, /// Each frame's generation, in the order they went out. out: Vec, diff --git a/rs/moq-srt/src/ts.rs b/rs/moq-srt/src/ts.rs index 58bdf15a126..b1f4455e1fe 100644 --- a/rs/moq-srt/src/ts.rs +++ b/rs/moq-srt/src/ts.rs @@ -18,7 +18,7 @@ use crate::Result; /// Which programs of a multi-program MPEG-TS an ingest publishes. /// /// Without one, an ingest whose PAT lists more than one program fails with -/// [`ts::MultipleProgramsError`] rather than merging unrelated clocks onto one broadcast. +/// [`ts::Error::MultiplePrograms`] rather than merging unrelated clocks onto one broadcast. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Program { /// Only the program with this PAT program number, on the ingest's path. @@ -603,11 +603,10 @@ mod tests { let origin = produce_origin(); let mut publisher = Publisher::new(&origin, "ingest", Default::default(), None).unwrap(); let err = publisher.feed(two_programs()).unwrap_err(); - let crate::Error::Mux(moq_mux::Error::Other(inner)) = &err else { - panic!("a demux error: {err}"); + let crate::Error::Mux(moq_mux::Error::Ts(ts::Error::MultiplePrograms { programs })) = &err else { + panic!("a multiplex refusal: {err}"); }; - let refused = inner.downcast_ref::(); - assert_eq!(refused.map(|refused| refused.programs.as_slice()), Some(&[1, 2][..])); + assert_eq!(programs, &[1, 2]); } /// A caller reconnecting under the same stream id while its stale connection is