| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | use std::error::Error; |
| 2 | use std::fmt::{Display, Formatter}; |
| 3 | |
| 4 | /// The requested host, although supported on this platform, is unavailable. |
| 5 | #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
| 6 | pub struct HostUnavailable; |
| 7 | |
| 8 | impl 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 | |
| 14 | impl 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)] |
| 30 | pub struct BackendSpecificError { |
| 31 | pub description: String, |
| 32 | } |
| 33 | |
| 34 | impl 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 | |
| 44 | impl 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] |
| 49 | pub enum DevicesError { |
| 50 | /// See the [`BackendSpecificError`] docs for more information about this error variant. |
| 51 | BackendSpecific { err: BackendSpecificError }, |
| 52 | } |
| 53 | |
| 54 | impl 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 | |
| 62 | impl Error for DevicesError {} |
| 63 | |
| 64 | impl 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] |
| 73 | pub enum DeviceIdError { |
| 74 | /// See the [`BackendSpecificError`] docs for more information about this error variant. |
| 75 | BackendSpecific { |
| 76 | err: BackendSpecificError, |
| 77 | }, |
| 78 | UnsupportedPlatform, |
| 79 | } |
| 80 | |
| 81 | impl 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 | |
| 90 | impl Error for DeviceIdError {} |
| 91 | |
| 92 | impl 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] |
| 101 | pub enum DeviceNameError { |
| 102 | /// See the [`BackendSpecificError`] docs for more information about this error variant. |
| 103 | BackendSpecific { err: BackendSpecificError }, |
| 104 | } |
| 105 | |
| 106 | impl 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 | |
| 114 | impl Error for DeviceNameError {} |
| 115 | |
| 116 | impl 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] |
| 125 | pub 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 | |
| 138 | impl 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 | |
| 149 | impl Error for SupportedStreamConfigsError {} |
| 150 | |
| 151 | impl 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] |
| 160 | pub 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 | |
| 173 | impl 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 | |
| 190 | impl Error for DefaultStreamConfigError {} |
| 191 | |
| 192 | impl 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] |
| 200 | pub 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 | |
| 220 | impl 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 | |
| 241 | impl Error for BuildStreamError {} |
| 242 | |
| 243 | impl 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] |
| 256 | pub 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 | |
| 263 | impl 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 | |
| 274 | impl Error for PlayStreamError {} |
| 275 | |
| 276 | impl 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] |
| 289 | pub 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 | |
| 296 | impl 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 | |
| 307 | impl Error for PauseStreamError {} |
| 308 | |
| 309 | impl 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] |
| 318 | pub 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 | |
| 333 | impl 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 | |
| 348 | impl Error for StreamError {} |
| 349 | |
| 350 | impl From<BackendSpecificError> for StreamError { |
| 351 | fn from(err: BackendSpecificError) -> Self { |
| 352 | Self::BackendSpecific { err } |
| 353 | } |
| 354 | } |