Added error handling for unknown ALSA device errors (#1)

Instead of taking the easy way out and killing the whole program by panicking, device enumeration and stream creation will now report the error variant 'Unknown'
This commit is contained in:
Florian Vick 2018-11-06 21:01:03 +01:00 committed by GitHub
parent c2d606176a
commit 5cb45bfd7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -92,7 +92,10 @@ impl Device {
) { ) {
-2 | -2 |
-16 /* determined empirically */ => return Err(FormatsEnumerationError::DeviceNotAvailable), -16 /* determined empirically */ => return Err(FormatsEnumerationError::DeviceNotAvailable),
e => check_errors(e).expect("device not available") -22 => return Err(FormatsEnumerationError::InvalidArgument),
e => if check_errors(e).is_err() {
return Err(FormatsEnumerationError::Unknown)
}
} }
let hw_params = HwParams::alloc(); let hw_params = HwParams::alloc();
@ -271,6 +274,14 @@ impl Device {
Err(FormatsEnumerationError::DeviceNotAvailable) => { Err(FormatsEnumerationError::DeviceNotAvailable) => {
return Err(DefaultFormatError::DeviceNotAvailable); return Err(DefaultFormatError::DeviceNotAvailable);
}, },
Err(FormatsEnumerationError::InvalidArgument) => {
// this happens sometimes when querying for input and output capabilities but
// the device supports only one
return Err(DefaultFormatError::StreamTypeNotSupported);
}
Err(FormatsEnumerationError::Unknown) => {
return Err(DefaultFormatError::DeviceNotAvailable);
}
Ok(fmts) => fmts.collect(), Ok(fmts) => fmts.collect(),
} }
}; };
@ -633,7 +644,10 @@ impl EventLoop {
alsa::SND_PCM_NONBLOCK, alsa::SND_PCM_NONBLOCK,
) { ) {
-16 /* determined empirically */ => return Err(CreationError::DeviceNotAvailable), -16 /* determined empirically */ => return Err(CreationError::DeviceNotAvailable),
e => check_errors(e).expect("Device unavailable") -22 => return Err(CreationError::InvalidArgument),
e => if check_errors(e).is_err() {
return Err(CreationError::Unknown);
}
} }
let hw_params = HwParams::alloc(); let hw_params = HwParams::alloc();
@ -693,7 +707,10 @@ impl EventLoop {
alsa::SND_PCM_NONBLOCK, alsa::SND_PCM_NONBLOCK,
) { ) {
-16 /* determined empirically */ => return Err(CreationError::DeviceNotAvailable), -16 /* determined empirically */ => return Err(CreationError::DeviceNotAvailable),
e => check_errors(e).expect("Device unavailable") -22 => return Err(CreationError::InvalidArgument),
e => if check_errors(e).is_err() {
return Err(CreationError::Unknown);
}
} }
let hw_params = HwParams::alloc(); let hw_params = HwParams::alloc();

View File

@ -289,6 +289,10 @@ pub enum FormatsEnumerationError {
/// The device no longer exists. This can happen if the device is disconnected while the /// The device no longer exists. This can happen if the device is disconnected while the
/// program is running. /// program is running.
DeviceNotAvailable, DeviceNotAvailable,
/// We called something the C-Layer did not understand
InvalidArgument,
/// The C-Layer returned an error we don't know about
Unknown
} }
/// May occur when attempting to request the default input or output stream format from a `Device`. /// May occur when attempting to request the default input or output stream format from a `Device`.
@ -309,6 +313,11 @@ pub enum CreationError {
DeviceNotAvailable, DeviceNotAvailable,
/// The required format is not supported. /// The required format is not supported.
FormatNotSupported, FormatNotSupported,
/// An ALSA device function was called with a feature it does not support
/// (trying to use capture capabilities on an output only format yields this)
InvalidArgument,
/// The C-Layer returned an error we don't know about
Unknown,
} }
/// An iterator yielding all `Device`s currently available to the system. /// An iterator yielding all `Device`s currently available to the system.
@ -714,6 +723,12 @@ impl Error for FormatsEnumerationError {
&FormatsEnumerationError::DeviceNotAvailable => { &FormatsEnumerationError::DeviceNotAvailable => {
"The requested device is no longer available (for example, it has been unplugged)." "The requested device is no longer available (for example, it has been unplugged)."
}, },
&FormatsEnumerationError::InvalidArgument => {
"Invalid argument passed to the Backend (This happens when trying to read for example capture capabilities but the device does not support it -> dmix on Linux)"
},
&FormatsEnumerationError::Unknown => {
"An unknown error in the Backend occured"
},
} }
} }
} }
@ -736,6 +751,14 @@ impl Error for CreationError {
&CreationError::FormatNotSupported => { &CreationError::FormatNotSupported => {
"The requested samples format is not supported by the device." "The requested samples format is not supported by the device."
}, },
&CreationError::InvalidArgument => {
"The requested device does not support this capability (invalid argument)"
}
&CreationError::Unknown => {
"An unknown error in the Backend occured"
},
} }
} }
} }