nandi/jolt-nativepublic Fork 0
8cf1b3347ef6fa83297b6866dfd47d2728d4e339
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

error.rs · 354 lines · 12.8 KBRust Blame HistoryRaw
Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4/// The requested host, although supported on this platform, is unavailable.
5#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
6pub struct HostUnavailable;
7
8impl Display for HostUnavailable {
9 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
10 f.write_str("the requested host is unavailable")
11 }
12}
13
14impl Error for HostUnavailable {}
15
16/// Some error has occurred that is specific to the backend from which it was produced.
17///
18/// This error is often used as a catch-all in cases where:
19///
20/// - It is unclear exactly what error might be produced by the backend API.
21/// - It does not make sense to add a variant to the enclosing error type.
22/// - No error was expected to occur at all, but we return an error to avoid the possibility of a
23/// `panic!` caused by some unforeseen or unknown reason.
24///
25/// **Note:** If you notice a `BackendSpecificError` that you believe could be better handled in a
26/// cross-platform manner, please create an issue at <https://github.com/RustAudio/cpal/issues>
27/// with details about your use case, the backend you're using, and the error message. Or submit
28/// a pull request with a patch that adds the necessary error variant to the appropriate error enum.
29#[derive(Clone, Debug, PartialEq, Eq, Hash)]
30pub struct BackendSpecificError {
31 pub description: String,
32}
33
34impl Display for BackendSpecificError {
35 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36 write!(
37 f,
38 "A backend-specific error has occurred: {}",
39 self.description
40 )
41 }
42}
43
44impl Error for BackendSpecificError {}
45
46/// An error that might occur while attempting to enumerate the available devices on a system.
47#[derive(Clone, Debug, PartialEq, Eq, Hash)]
48#[non_exhaustive]
49pub enum DevicesError {
50 /// See the [`BackendSpecificError`] docs for more information about this error variant.
51 BackendSpecific { err: BackendSpecificError },
52}
53
54impl Display for DevicesError {
55 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56 match self {
57 Self::BackendSpecific { err } => err.fmt(f),
58 }
59 }
60}
61
62impl Error for DevicesError {}
63
64impl From<BackendSpecificError> for DevicesError {
65 fn from(err: BackendSpecificError) -> Self {
66 Self::BackendSpecific { err }
67 }
68}
69
70/// An error that may occur while attempting to retrieve a device ID.
71#[derive(Clone, Debug, Eq, PartialEq)]
72#[non_exhaustive]
73pub enum DeviceIdError {
74 /// See the [`BackendSpecificError`] docs for more information about this error variant.
75 BackendSpecific {
76 err: BackendSpecificError,
77 },
78 UnsupportedPlatform,
79}
80
81impl Display for DeviceIdError {
82 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
83 match self {
84 Self::BackendSpecific { err } => err.fmt(f),
85 Self::UnsupportedPlatform => f.write_str("Device IDs are unsupported for this OS"),
86 }
87 }
88}
89
90impl Error for DeviceIdError {}
91
92impl From<BackendSpecificError> for DeviceIdError {
93 fn from(err: BackendSpecificError) -> Self {
94 Self::BackendSpecific { err }
95 }
96}
97
98/// An error that may occur while attempting to retrieve a device name.
99#[derive(Clone, Debug, PartialEq, Eq, Hash)]
100#[non_exhaustive]
101pub enum DeviceNameError {
102 /// See the [`BackendSpecificError`] docs for more information about this error variant.
103 BackendSpecific { err: BackendSpecificError },
104}
105
106impl Display for DeviceNameError {
107 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
108 match self {
109 Self::BackendSpecific { err } => err.fmt(f),
110 }
111 }
112}
113
114impl Error for DeviceNameError {}
115
116impl From<BackendSpecificError> for DeviceNameError {
117 fn from(err: BackendSpecificError) -> Self {
118 Self::BackendSpecific { err }
119 }
120}
121
122/// Error that can happen when enumerating the list of supported formats.
123#[derive(Clone, Debug, PartialEq, Eq, Hash)]
124#[non_exhaustive]
125pub enum SupportedStreamConfigsError {
126 /// The device no longer exists. This can happen if the device is disconnected while the
127 /// program is running.
128 DeviceNotAvailable,
129 /// The device is temporarily busy. This can happen when another application or stream
130 /// is using the device. Retrying may succeed.
131 DeviceBusy,
132 /// We called something the C-Layer did not understand
133 InvalidArgument,
134 /// See the [`BackendSpecificError`] docs for more information about this error variant.
135 BackendSpecific { err: BackendSpecificError },
136}
137
138impl Display for SupportedStreamConfigsError {
139 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
140 match self {
141 Self::BackendSpecific { err } => err.fmt(f),
142 Self::DeviceNotAvailable => f.write_str("The requested device is no longer available. For example, it has been unplugged."),
143 Self::DeviceBusy => f.write_str("The requested device is temporarily busy. Another application or stream may be using it."),
144 Self::InvalidArgument => f.write_str("Invalid argument passed to the backend. For example, this happens when trying to read capture capabilities when the device does not support it.")
145 }
146 }
147}
148
149impl Error for SupportedStreamConfigsError {}
150
151impl From<BackendSpecificError> for SupportedStreamConfigsError {
152 fn from(err: BackendSpecificError) -> Self {
153 Self::BackendSpecific { err }
154 }
155}
156
157/// May occur when attempting to request the default input or output stream format from a [`Device`](crate::Device).
158#[derive(Clone, Debug, PartialEq, Eq, Hash)]
159#[non_exhaustive]
160pub enum DefaultStreamConfigError {
161 /// The device no longer exists. This can happen if the device is disconnected while the
162 /// program is running.
163 DeviceNotAvailable,
164 /// The device is temporarily busy. This can happen when another application or stream
165 /// is using the device. Retrying after a short delay may succeed.
166 DeviceBusy,
167 /// Returned if e.g. the default input format was requested on an output-only audio device.
168 StreamTypeNotSupported,
169 /// See the [`BackendSpecificError`] docs for more information about this error variant.
170 BackendSpecific { err: BackendSpecificError },
171}
172
173impl Display for DefaultStreamConfigError {
174 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
175 match self {
176 Self::BackendSpecific { err } => err.fmt(f),
177 Self::DeviceNotAvailable => f.write_str(
178 "The requested device is no longer available. For example, it has been unplugged.",
179 ),
180 Self::DeviceBusy => f.write_str(
181 "The requested device is temporarily busy. Another application or stream may be using it.",
182 ),
183 Self::StreamTypeNotSupported => {
184 f.write_str("The requested stream type is not supported by the device.")
185 }
186 }
187 }
188}
189
190impl Error for DefaultStreamConfigError {}
191
192impl From<BackendSpecificError> for DefaultStreamConfigError {
193 fn from(err: BackendSpecificError) -> Self {
194 Self::BackendSpecific { err }
195 }
196}
197/// Error that can happen when creating a [`Stream`](crate::Stream).
198#[derive(Clone, Debug, PartialEq, Eq, Hash)]
199#[non_exhaustive]
200pub enum BuildStreamError {
201 /// The device no longer exists. This can happen if the device is disconnected while the
202 /// program is running.
203 DeviceNotAvailable,
204 /// The device is temporarily busy. This can happen when another application or stream
205 /// is using the device. Retrying may succeed.
206 DeviceBusy,
207 /// The specified stream configuration is not supported.
208 StreamConfigNotSupported,
209 /// We called something the C-Layer did not understand
210 ///
211 /// On ALSA device functions called with a feature they do not support will yield this. E.g.
212 /// Trying to use capture capabilities on an output only format yields this.
213 InvalidArgument,
214 /// Occurs if adding a new Stream ID would cause an integer overflow.
215 StreamIdOverflow,
216 /// See the [`BackendSpecificError`] docs for more information about this error variant.
217 BackendSpecific { err: BackendSpecificError },
218}
219
220impl Display for BuildStreamError {
221 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
222 match self {
223 Self::BackendSpecific { err } => err.fmt(f),
224 Self::DeviceNotAvailable => f.write_str(
225 "The requested device is no longer available. For example, it has been unplugged.",
226 ),
227 Self::DeviceBusy => f.write_str(
228 "The requested device is temporarily busy. Another application or stream may be using it.",
229 ),
230 Self::StreamConfigNotSupported => {
231 f.write_str("The requested stream configuration is not supported by the device.")
232 }
233 Self::InvalidArgument => f.write_str(
234 "The requested device does not support this capability (invalid argument)",
235 ),
236 Self::StreamIdOverflow => f.write_str("Adding a new stream ID would cause an overflow"),
237 }
238 }
239}
240
241impl Error for BuildStreamError {}
242
243impl From<BackendSpecificError> for BuildStreamError {
244 fn from(err: BackendSpecificError) -> Self {
245 Self::BackendSpecific { err }
246 }
247}
248
249/// Errors that might occur when calling [`Stream::play()`](crate::traits::StreamTrait::play).
250///
251/// As of writing this, only macOS may immediately return an error while calling this method. This
252/// is because both the alsa and wasapi backends only enqueue these commands and do not process
253/// them immediately.
254#[derive(Clone, Debug, PartialEq, Eq, Hash)]
255#[non_exhaustive]
256pub enum PlayStreamError {
257 /// The device associated with the stream is no longer available.
258 DeviceNotAvailable,
259 /// See the [`BackendSpecificError`] docs for more information about this error variant.
260 BackendSpecific { err: BackendSpecificError },
261}
262
263impl Display for PlayStreamError {
264 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
265 match self {
266 Self::BackendSpecific { err } => err.fmt(f),
267 Self::DeviceNotAvailable => {
268 f.write_str("the device associated with the stream is no longer available")
269 }
270 }
271 }
272}
273
274impl Error for PlayStreamError {}
275
276impl From<BackendSpecificError> for PlayStreamError {
277 fn from(err: BackendSpecificError) -> Self {
278 Self::BackendSpecific { err }
279 }
280}
281
282/// Errors that might occur when calling [`Stream::pause()`](crate::traits::StreamTrait::pause).
283///
284/// As of writing this, only macOS may immediately return an error while calling this method. This
285/// is because both the alsa and wasapi backends only enqueue these commands and do not process
286/// them immediately.
287#[derive(Clone, Debug, PartialEq, Eq, Hash)]
288#[non_exhaustive]
289pub enum PauseStreamError {
290 /// The device associated with the stream is no longer available.
291 DeviceNotAvailable,
292 /// See the [`BackendSpecificError`] docs for more information about this error variant.
293 BackendSpecific { err: BackendSpecificError },
294}
295
296impl Display for PauseStreamError {
297 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
298 match self {
299 Self::BackendSpecific { err } => err.fmt(f),
300 Self::DeviceNotAvailable => {
301 f.write_str("the device associated with the stream is no longer available")
302 }
303 }
304 }
305}
306
307impl Error for PauseStreamError {}
308
309impl From<BackendSpecificError> for PauseStreamError {
310 fn from(err: BackendSpecificError) -> Self {
311 Self::BackendSpecific { err }
312 }
313}
314
315/// Errors that might occur while a stream is running.
316#[derive(Clone, Debug, PartialEq, Eq, Hash)]
317#[non_exhaustive]
318pub enum StreamError {
319 /// The device no longer exists. This can happen if the device is disconnected while the
320 /// program is running.
321 DeviceNotAvailable,
322
323 /// The stream configuration is no longer valid and must be rebuilt.
324 StreamInvalidated,
325
326 /// Buffer underrun or overrun occurred, causing a potential audio glitch.
327 BufferUnderrun,
328
329 /// See the [`BackendSpecificError`] docs for more information about this error variant.
330 BackendSpecific { err: BackendSpecificError },
331}
332
333impl Display for StreamError {
334 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
335 match self {
336 Self::BackendSpecific { err } => err.fmt(f),
337 Self::StreamInvalidated => {
338 f.write_str("The stream configuration is no longer valid and must be rebuilt.")
339 }
340 Self::BufferUnderrun => f.write_str("Buffer underrun/overrun occurred."),
341 Self::DeviceNotAvailable => f.write_str(
342 "The requested device is no longer available. For example, it has been unplugged.",
343 ),
344 }
345 }
346}
347
348impl Error for StreamError {}
349
350impl From<BackendSpecificError> for StreamError {
351 fn from(err: BackendSpecificError) -> Self {
352 Self::BackendSpecific { err }
353 }
354}