moq_ext_sources/error.rs
1//! The error the sources, the player and the publisher share.
2//!
3//! The variants of [`Error`] group failures by what a caller can act on.
4
5use n0_error::{AnyError, stack_error};
6
7/// An error from the media crates.
8///
9/// `moq-ext-sources`, `moq-ext-player` and `moq-ext-publisher` all return this
10/// one type, so an error passes from a source to a broadcast unchanged. A
11/// watched status holds it as `Arc<Error>`, as in
12/// [`SlotState::Failed`](crate::SlotState::Failed).
13#[stack_error(derive, add_meta)]
14pub enum Error {
15 /// A capture or playback device, or a media file, would not open or failed while running.
16 #[error("device failed")]
17 Device {
18 /// What the device reported.
19 #[error(source)]
20 source: AnyError,
21 },
22 /// A configuration cannot be used as given.
23 #[error("invalid configuration: {reason}")]
24 InvalidConfig {
25 /// What is wrong with it.
26 reason: String,
27 },
28 /// No encoder compiled into this build can produce the codec asked for.
29 #[error("no encoder for {codec} is available in this build")]
30 NoEncoder {
31 /// The codec that was asked for.
32 codec: String,
33 },
34 /// No decoder compiled into this build can read the codec.
35 #[error("no decoder for {codec} is available in this build")]
36 NoDecoder {
37 /// The codec the broadcast carries.
38 codec: String,
39 },
40 /// An encoder failed to open or to encode.
41 #[error("encoder failed")]
42 Encoder {
43 /// What the encoder reported.
44 #[error(source)]
45 source: AnyError,
46 },
47 /// A decoder failed to open or to decode.
48 #[error("decoder failed")]
49 Decoder {
50 /// What the decoder reported.
51 #[error(source)]
52 source: AnyError,
53 },
54 /// The broadcast's catalog has no video rendition of that name.
55 #[error("no video rendition named {name}, the broadcast has [{}]", offered.join(", "))]
56 UnknownRendition {
57 /// The name that was asked for.
58 name: String,
59 /// The names the catalog has, largest first.
60 offered: Vec<String>,
61 },
62 /// The catalog could not be read or written.
63 #[error("catalog failed")]
64 Catalog {
65 /// What went wrong with it.
66 #[error(source)]
67 source: AnyError,
68 },
69 /// The broadcast layer refused an operation or reset a track being read.
70 ///
71 /// The error comes from moq-net and looks the same whatever transport
72 /// carries the broadcast. Failures of the transport itself, such as an
73 /// unreachable peer, belong to the transport's own error, `iroh_live_node::Error`
74 /// for iroh.
75 #[error("broadcast failed")]
76 Broadcast {
77 /// What moq-net reported.
78 #[error(source)]
79 source: AnyError,
80 },
81 /// The broadcast, source or player was closed, or the session under it.
82 #[error("closed")]
83 Closed,
84 /// Reading or writing a file or pipe failed.
85 #[error("I/O failed")]
86 Io {
87 /// What the operating system reported.
88 #[error(source, std_err)]
89 source: std::io::Error,
90 },
91}
92
93impl Error {
94 /// Creates an [`Error::InvalidConfig`] with `reason`.
95 #[must_use]
96 pub fn invalid(reason: impl Into<String>) -> Self {
97 n0_error::e!(Self::InvalidConfig {
98 reason: reason.into()
99 })
100 }
101
102 /// Creates an [`Error::Device`] from an upstream error.
103 #[must_use]
104 pub fn device(source: impl std::error::Error + Send + Sync + 'static) -> Self {
105 n0_error::e!(Self::Device {
106 source: AnyError::from_std(source)
107 })
108 }
109
110 /// Creates an [`Error::Device`] from a message.
111 #[must_use]
112 pub fn device_msg(message: impl std::fmt::Display) -> Self {
113 n0_error::e!(Self::Device {
114 source: AnyError::from_display(message)
115 })
116 }
117
118 /// Creates an [`Error::Encoder`] from an upstream error.
119 #[must_use]
120 pub fn encoder(source: impl std::error::Error + Send + Sync + 'static) -> Self {
121 n0_error::e!(Self::Encoder {
122 source: AnyError::from_std(source)
123 })
124 }
125
126 /// Creates an [`Error::Encoder`] from a message.
127 #[must_use]
128 pub fn encoder_msg(message: impl std::fmt::Display) -> Self {
129 n0_error::e!(Self::Encoder {
130 source: AnyError::from_display(message)
131 })
132 }
133
134 /// Creates an [`Error::Decoder`] from an upstream error.
135 #[must_use]
136 pub fn decoder(source: impl std::error::Error + Send + Sync + 'static) -> Self {
137 n0_error::e!(Self::Decoder {
138 source: AnyError::from_std(source)
139 })
140 }
141
142 /// Creates an [`Error::Decoder`] from a message.
143 #[must_use]
144 pub fn decoder_msg(message: impl std::fmt::Display) -> Self {
145 n0_error::e!(Self::Decoder {
146 source: AnyError::from_display(message)
147 })
148 }
149
150 /// Creates an [`Error::Catalog`] from an upstream error.
151 #[must_use]
152 pub fn catalog(source: impl std::error::Error + Send + Sync + 'static) -> Self {
153 n0_error::e!(Self::Catalog {
154 source: AnyError::from_std(source)
155 })
156 }
157
158 /// Creates an [`Error::Catalog`] from a message.
159 #[must_use]
160 pub fn catalog_msg(message: impl std::fmt::Display) -> Self {
161 n0_error::e!(Self::Catalog {
162 source: AnyError::from_display(message)
163 })
164 }
165
166 /// Creates an [`Error::Broadcast`] from an upstream error.
167 #[must_use]
168 pub fn broadcast(source: impl std::error::Error + Send + Sync + 'static) -> Self {
169 n0_error::e!(Self::Broadcast {
170 source: AnyError::from_std(source)
171 })
172 }
173}
174
175impl From<std::io::Error> for Error {
176 fn from(source: std::io::Error) -> Self {
177 n0_error::e!(Self::Io { source })
178 }
179}
180
181/// The source a `FrameSender` feeds has gone.
182///
183/// Returned by [`FrameSender::push`](crate::FrameSender::push), so a producing
184/// loop knows to stop.
185#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Display)]
186#[display("the source has closed")]
187pub struct SourceClosed;
188
189impl std::error::Error for SourceClosed {}