Skip to main content

moq_mux/container/ts/
error.rs

1/// Why a TS import or export stopped.
2///
3/// A packet lost or flagged by the transport error indicator is not one: the importer counts
4/// the unit it breaks in its [`stats`](super::stats) and carries on. An intact packet, header,
5/// or unit that does not parse is one: [`Error::Malformed`].
6#[derive(Clone, Debug, thiserror::Error)]
7#[non_exhaustive]
8pub enum Error {
9	/// A PAT listed more than one program, and the import was not told which one to take.
10	///
11	/// Importing them all onto one broadcast would put unrelated clocks on one timeline, so the
12	/// import stops instead. Pick one with [`Import::with_program`](super::Import::with_program), or
13	/// import each as its own broadcast with [`Programs`](super::Programs).
14	#[error("transport stream carries {} programs ({})", .programs.len(), list(.programs))]
15	MultiplePrograms {
16		/// The program numbers the PAT lists, in PAT order.
17		programs: Vec<u16>,
18	},
19
20	/// The PAT does not list the program the import was told to take.
21	#[error("transport stream has no program {program}; its PAT lists {}", list(.programs))]
22	MissingProgram {
23		/// The selected program number.
24		program: u16,
25		/// The program numbers the PAT lists, in PAT order.
26		programs: Vec<u16>,
27	},
28
29	/// The source's clock stepped onto another timeline, or its PAT named another program, so
30	/// what follows is another broadcast. [`Republish`](super::Republish) carries on into a new
31	/// identity instead.
32	#[error("the source {0}; what follows is a new broadcast")]
33	Restarted(super::Step),
34
35	/// The input ended before it declared anything to publish.
36	#[error("input ended before {0}")]
37	Empty(&'static str),
38
39	/// The input breaks the MPEG-TS or a codec's rules past what the import or export recovers
40	/// from: a TS byte stream for an import, a broadcast's catalog and frames for an export.
41	#[error("{0}")]
42	Malformed(String),
43
44	/// The input is well-formed but needs what this import or export does not carry, such as a
45	/// codec, a layout change, or more buffer or rate than its configuration allows.
46	#[error("{0}")]
47	Unsupported(String),
48
49	/// Parsing the media or publishing it failed.
50	#[error(transparent)]
51	Mux(Box<crate::Error>),
52}
53
54/// A result whose failure is a TS [`Error`].
55pub(super) type Result<T, E = Error> = std::result::Result<T, E>;
56
57fn list(programs: &[u16]) -> String {
58	programs.iter().map(u16::to_string).collect::<Vec<_>>().join(", ")
59}
60
61impl From<crate::Error> for Error {
62	/// A TS error that crossed into [`crate::Error`] comes back out as itself.
63	fn from(err: crate::Error) -> Self {
64		match err {
65			crate::Error::Ts(err) => err,
66			err => Self::Mux(Box::new(err)),
67		}
68	}
69}
70
71impl From<super::schedule::Error> for Error {
72	fn from(err: super::schedule::Error) -> Self {
73		match err {
74			super::schedule::Error::Unset => Self::Malformed(err.to_string()),
75			err => Self::Unsupported(err.to_string()),
76		}
77	}
78}
79
80// Flattened to its message so `mpeg2ts` stays out of this crate's public API. The export meets it
81// only on a value out of a field's range.
82impl From<mpeg2ts::Error> for Error {
83	fn from(err: mpeg2ts::Error) -> Self {
84		Self::Malformed(crate::error::message(err))
85	}
86}
87
88/// Every error a TS function's `?` meets on its way to [`Error::Mux`].
89macro_rules! mux_errors {
90	($($ty:ty),* $(,)?) => {
91		$(
92			impl From<$ty> for Error {
93				fn from(err: $ty) -> Self {
94					crate::Error::from(err).into()
95				}
96			}
97		)*
98	};
99}
100
101mux_errors!(
102	moq_net::Error,
103	moq_net::TimeOverflow,
104	hang::Error,
105	crate::codec::aac::Error,
106	crate::codec::aac::adts::Error,
107	crate::codec::annexb::Error,
108	crate::codec::h264::Error,
109	crate::codec::h265::Error,
110	crate::codec::legacy::Error,
111	crate::codec::opus::Error,
112	crate::container::MissingKeyframe,
113	crate::container::TimestampRewind,
114	crate::jitter::Error,
115);
116
117/// [`Error::Malformed`], formatted.
118macro_rules! malformed {
119	($($arg:tt)*) => {
120		$crate::container::ts::Error::Malformed(format!($($arg)*))
121	};
122}
123
124/// [`Error::Unsupported`], formatted.
125macro_rules! unsupported {
126	($($arg:tt)*) => {
127		$crate::container::ts::Error::Unsupported(format!($($arg)*))
128	};
129}
130
131/// Return [`Error::Malformed`], formatted.
132macro_rules! bail {
133	($($arg:tt)*) => {
134		return Err($crate::container::ts::error::malformed!($($arg)*).into())
135	};
136}
137
138/// Return [`Error::Malformed`], formatted, unless `cond` holds.
139macro_rules! ensure {
140	($cond:expr, $($arg:tt)*) => {
141		if !$cond {
142			$crate::container::ts::error::bail!($($arg)*);
143		}
144	};
145}
146
147/// Return [`Error::Unsupported`], formatted.
148macro_rules! bail_unsupported {
149	($($arg:tt)*) => {
150		return Err($crate::container::ts::error::unsupported!($($arg)*).into())
151	};
152}
153
154/// Return [`Error::Unsupported`], formatted, unless `cond` holds.
155macro_rules! ensure_supported {
156	($cond:expr, $($arg:tt)*) => {
157		if !$cond {
158			$crate::container::ts::error::bail_unsupported!($($arg)*);
159		}
160	};
161}
162
163pub(super) use {bail, bail_unsupported, ensure, ensure_supported, malformed, unsupported};
164
165/// A missing value as [`Error::Malformed`].
166pub(super) trait Context<T> {
167	/// `msg` when the value is missing.
168	fn context(self, msg: &str) -> Result<T>;
169}
170
171impl<T> Context<T> for Option<T> {
172	fn context(self, msg: &str) -> Result<T> {
173		self.ok_or_else(|| Error::Malformed(msg.to_string()))
174	}
175}