moq_mux/error.rs
1/// Renders an error and its `source()` chain into a single message.
2///
3/// Dependency errors are stored as messages so their crates stay out of this crate's public
4/// API. Several of them keep the actionable half in `source()` and nothing but a category in
5/// `Display`, so a plain `to_string()` would drop the only detail worth reporting.
6pub(crate) fn message(err: impl std::error::Error) -> String {
7 use std::fmt::Write;
8
9 let mut out = err.to_string();
10 let mut source = err.source();
11 while let Some(err) = source {
12 let _ = write!(out, ": {err}");
13 source = err.source();
14 }
15 out
16}
17
18/// Errors from moq-mux operations.
19///
20/// Most variants are delegations to underlying layers: [`moq_net::Error`] for
21/// transport / pub-sub failures, [`hang::Error`] for catalog/codec parsing, the
22/// per-format Errors for container shape problems, and the per-codec Errors for
23/// bitstream parsing problems.
24#[derive(Debug, Clone, thiserror::Error)]
25#[non_exhaustive]
26pub enum Error {
27 /// Error from the underlying moq-net transport.
28 #[error("moq: {0}")]
29 Moq(#[from] moq_net::Error),
30
31 /// Error from the hang catalog/codec layer.
32 #[error("hang: {0}")]
33 Hang(#[from] hang::Error),
34
35 /// Error publishing or consuming JSON over a track.
36 #[error("json: {0}")]
37 Json(#[from] moq_json::Error),
38
39 /// A catalog entry declares a track mode this build does not implement.
40 #[error("unsupported track mode: {0}")]
41 UnsupportedMode(String),
42
43 /// A locally published track's catalog entry points at another broadcast.
44 #[error("a locally published track can't reference another broadcast")]
45 ForeignBroadcast,
46
47 /// Error parsing or building CMAF moof+mdat fragments.
48 #[error("cmaf: {0}")]
49 Cmaf(#[from] crate::container::fmp4::Error),
50
51 /// Error importing or exporting MPEG-TS.
52 #[error("ts: {0}")]
53 Ts(crate::container::ts::Error),
54
55 /// Error parsing or building MKV / WebM streams.
56 #[error("mkv: {0}")]
57 Mkv(#[from] crate::container::mkv::Error),
58
59 /// Error importing or exporting FLV.
60 #[error("flv: {0}")]
61 Flv(#[from] crate::container::flv::Error),
62
63 /// Error decoding the MSF catalog.
64 #[error("msf: {0}")]
65 Msf(#[from] crate::catalog::msf::Error),
66
67 /// Error parsing or building LOC frames.
68 #[error("loc: {0}")]
69 Loc(#[from] moq_loc::Error),
70
71 /// Error parsing an Annex B NAL stream.
72 #[error("annexb: {0}")]
73 Annexb(#[from] crate::codec::annexb::Error),
74
75 /// Error parsing AAC.
76 #[error("aac: {0}")]
77 Aac(#[from] crate::codec::aac::Error),
78
79 /// Error parsing Opus.
80 #[error("opus: {0}")]
81 Opus(#[from] crate::codec::opus::Error),
82
83 /// Error parsing FLAC.
84 #[error("flac: {0}")]
85 Flac(#[from] crate::codec::flac::Error),
86
87 /// Error parsing MP3.
88 #[error("mp3: {0}")]
89 Mp3(#[from] crate::codec::mp3::Error),
90
91 /// Error parsing H.264.
92 #[error("h264: {0}")]
93 H264(#[from] crate::codec::h264::Error),
94
95 /// Error parsing H.265.
96 #[error("h265: {0}")]
97 H265(#[from] crate::codec::h265::Error),
98
99 /// Error parsing AV1.
100 #[error("av1: {0}")]
101 Av1(#[from] crate::codec::av1::Error),
102
103 /// Error parsing VP8.
104 #[error("vp8: {0}")]
105 Vp8(#[from] crate::codec::vp8::Error),
106
107 /// Error parsing VP9.
108 #[error("vp9: {0}")]
109 Vp9(#[from] crate::codec::vp9::Error),
110
111 /// Error parsing legacy audio (MP2 / AC-3 / E-AC-3).
112 #[error("legacy: {0}")]
113 Legacy(#[from] crate::codec::legacy::Error),
114
115 /// Timestamp overflow when converting between timescales.
116 #[error("timestamp overflow")]
117 TimestampOverflow(#[from] moq_net::TimeOverflow),
118
119 /// Error decoding or encoding an mp4 atom.
120 #[error("mp4: {0}")]
121 Mp4(#[source] std::sync::Arc<dyn std::error::Error + Send + Sync>),
122
123 /// I/O error.
124 #[error("io: {0}")]
125 Io(std::sync::Arc<std::io::Error>),
126
127 /// Unknown media format.
128 #[error("unknown format: {0}")]
129 UnknownFormat(String),
130
131 /// A video format that a raw byte stream cannot be split into.
132 ///
133 /// Only the self-delimiting codecs (Annex-B H.264/H.265, AV1 OBUs) carry their own frame
134 /// boundaries. The rest need length prefixes or an out-of-band config record, so they can only
135 /// be imported as whole frames via [`Track::video`](crate::import::Track::video).
136 #[error("{0} is not self-describing, so its frame boundaries can't be inferred from a stream")]
137 NotSelfDescribing(String),
138
139 /// A format was handed to a constructor for a different kind of import.
140 ///
141 /// Each entry point takes only the fields its kind can honor, so the format has to match: an
142 /// audio format carries no video hint, and a container has no single rendition to label.
143 #[error("{format} is a {actual} format, not {wanted}")]
144 WrongKind {
145 /// The format string the caller passed.
146 format: String,
147 /// The kind that actually handles it.
148 actual: &'static str,
149 /// The kind the constructor expected.
150 wanted: &'static str,
151 },
152
153 /// Video does not fit the reorder depth it declares.
154 #[error("{0}")]
155 Reorder(#[from] crate::container::ReorderError),
156
157 /// An export's source clock runs further off the output's than the output may follow.
158 #[error("the source's clock runs {ppm:+.1} ppm off ours, past the {limit:.0} ppm the output's may follow")]
159 Drift {
160 /// How far off the source's clock runs, in parts per million.
161 ppm: f64,
162 /// The most the output's clock follows, in parts per million.
163 limit: f64,
164 },
165
166 /// A publisher an export handed off from queued media after its handoff deadline.
167 #[error("a retired publisher queued after its handoff deadline")]
168 Retired,
169
170 /// A non-keyframe frame was received before any keyframe opened a group.
171 /// A track joining mid-stream should skip frames until the first keyframe.
172 #[error("{0}")]
173 MissingKeyframe(#[from] crate::container::MissingKeyframe),
174
175 /// An explicit video endpoint precedes its last frame.
176 #[error("{0}")]
177 InvalidEnd(#[from] crate::container::InvalidEnd),
178
179 /// A group starts before the previous group did: the source restarted.
180 #[error("{0}")]
181 TimestampRewind(#[from] crate::container::TimestampRewind),
182
183 /// A rendition was modified before its initial config was published.
184 #[error("rendition is not published")]
185 NotPublished,
186
187 /// A FLV video frame resolved to a negative presentation timestamp.
188 #[error("negative FLV video presentation timestamp: dts={dts_ms}ms composition_time={composition_time_ms}ms")]
189 NegativeFlvPts {
190 /// The FLV tag decode timestamp in milliseconds.
191 dts_ms: u64,
192 /// The signed FLV composition-time offset in milliseconds.
193 composition_time_ms: i32,
194 },
195
196 /// A timestamp cannot be mapped between the broadcast clock and the wall: PTS zero would land
197 /// before the moq epoch (2020) or after now, the host's wall clock was unset so the clock has no
198 /// mapping, or a catalog root `clock` would change or clear the producer's fixed clock.
199 #[error("timestamp cannot be mapped onto the broadcast clock: {0}")]
200 UnmappableTimestamp(String),
201
202 /// A rendition declared a container `kind` this build does not recognize, so its
203 /// frames cannot be parsed. Such a rendition must be ignored, not guessed at.
204 #[error("unsupported container: {0}")]
205 UnsupportedContainer(String),
206
207 /// A rendition's `broadcast` reference walks above the root, so it names no broadcast.
208 ///
209 /// The root is the consumer's authorized subtree, so such a reference is an attempt to
210 /// name content the consumer cannot reach. It rejects the whole catalog
211 /// ([`catalog::hang::Consumer`](crate::catalog::hang::Consumer)) rather than the one
212 /// rendition, and is also what a direct [`Source::resolve`](crate::Source::resolve) /
213 /// [`Source::subscribe_track`](crate::Source::subscribe_track) reports.
214 #[error("broadcast reference escapes the root: {0}")]
215 EscapingBroadcast(String),
216
217 /// A rendition tried to lower jitter already advertised to subscribers.
218 #[error("catalog jitter cannot decrease for a published rendition")]
219 JitterDecreased,
220
221 /// A rendition tried to lower delay already advertised to subscribers.
222 #[error("catalog delay cannot decrease for a published rendition")]
223 DelayDecreased,
224
225 /// A capture instant is ahead of the broadcast clock's now, or before its epoch.
226 #[error("capture time is outside the broadcast clock")]
227 InvalidCapture,
228
229 /// A transport stream passthrough's catalog extension carries no `mpegts` section to list its
230 /// track in.
231 #[error("a passthrough lists its track in the `mpegts` section, which the catalog extension does not carry")]
232 NoMpegtsSection,
233
234 /// A transport stream passthrough was fed packets other than 188 bytes.
235 ///
236 /// A 192-byte source (BDAV M2TS, a four-byte timestamp before each packet) or a 204-byte one
237 /// (Reed-Solomon parity after each) is refused rather than reframed, since dropping those bytes
238 /// would break the byte identity passthrough exists for.
239 #[error("transport stream uses {0}-byte packets; passthrough carries 188-byte packets only")]
240 TsPacketSize(usize),
241
242 /// The clock PID carried no PCR across more bytes than a group may hold.
243 ///
244 /// Those bytes cannot be timed, and holding them for a PCR that may never come is unbounded.
245 #[error("no PCR on PID {pid} for {bytes} bytes")]
246 PcrGap {
247 /// The clock PID.
248 pid: u16,
249 /// The bytes waiting on a PCR.
250 bytes: u64,
251 },
252
253 /// Another publisher identity replaced a followed broadcast, and stitching was off.
254 #[error("another publisher identity replaced broadcast `{0}`; enable stitch to follow it as a program switch")]
255 Replaced(String),
256 /// A published rendition changed fields that require a new decoder configuration.
257 #[error("rendition `{0}` changed identity; publish it under a new track name")]
258 IdentityChanged(String),
259
260 /// A rendition moved to a track whose media goes back before what the export already read for it.
261 #[error(
262 "rendition {rendition} moved to a track that goes back in time: a group starts at {start:?}, not past {read:?}"
263 )]
264 RenditionRewound {
265 /// The rendition's ID in the catalog.
266 rendition: String,
267 /// Where the new track's group starts.
268 start: std::time::Duration,
269 /// The latest media the export read from the rendition's previous track.
270 read: std::time::Duration,
271 },
272
273 /// An importer was given input after it finished.
274 #[error("input after finish")]
275 Finished,
276
277 /// A raw data track was handed an empty payload, which its zero-byte discontinuity marker
278 /// leaves no way to carry.
279 #[error("a raw data track can't carry an empty payload: a zero-byte object marks a discontinuity")]
280 EmptyRawPayload,
281}
282
283impl Error {
284 /// The error for a rendition whose container this build does not recognize.
285 pub(crate) fn unsupported_container(container: &hang::catalog::UnknownContainer) -> Self {
286 Self::UnsupportedContainer(container.kind().to_string())
287 }
288
289 /// The reason a track this error aborts gives its subscribers. Only a transport error has a
290 /// wire code; anything else reaches them as a generic failure while the cause stays here.
291 pub(crate) fn reason(&self) -> moq_net::Error {
292 match self {
293 Self::Moq(err) => err.clone(),
294 Self::Json(moq_json::Error::Net(err)) => err.clone(),
295 _ => moq_net::StreamError::Internal.into(),
296 }
297 }
298}
299
300impl From<crate::jitter::Error> for Error {
301 fn from(err: crate::jitter::Error) -> Self {
302 match err {
303 crate::jitter::Error::Drift { ppm, limit } => Self::Drift { ppm, limit },
304 crate::jitter::Error::Retired => Self::Retired,
305 }
306 }
307}
308
309impl From<crate::codec::aac::adts::Error> for Error {
310 fn from(err: crate::codec::aac::adts::Error) -> Self {
311 Self::Aac(err.into())
312 }
313}
314
315impl From<crate::container::ts::Error> for Error {
316 /// A TS error wrapping one of these unwraps to it, so neither nests in the other.
317 fn from(err: crate::container::ts::Error) -> Self {
318 match err {
319 crate::container::ts::Error::Mux(err) => *err,
320 err => Self::Ts(err),
321 }
322 }
323}
324
325impl Error {
326 /// An mp4 atom error, kept opaque so an mp4-atom bump is no moq-mux break.
327 pub(crate) fn mp4(err: mp4_atom::Error) -> Self {
328 Error::Mp4(std::sync::Arc::new(err))
329 }
330}
331
332impl From<std::io::Error> for Error {
333 fn from(err: std::io::Error) -> Self {
334 Error::Io(std::sync::Arc::new(err))
335 }
336}
337
338/// A Result type alias for moq-mux operations.
339pub type Result<T> = std::result::Result<T, Error>;