Rename `FormatsEnumerationError` to `SupportedFormatsError`

This more tightly associates the error with the device method on which
this might occur.
This commit is contained in:
mitchmindtree 2019-06-20 21:16:39 +02:00
parent 1275db805b
commit 0f27c1e0bb
7 changed files with 39 additions and 39 deletions

View File

@ -30,13 +30,13 @@ impl Drop for Devices {
impl Default for Devices { impl Default for Devices {
fn default() -> Devices { fn default() -> Devices {
unsafe { unsafe {
let mut hints = mem::uninitialized(); // TODO: check in which situation this can fail.
// TODO: check in which situation this can fail let card = -1; // -1 means all cards.
check_errors(alsa::snd_device_name_hint(-1, b"pcm\0".as_ptr() as *const _, &mut hints)) let iface = b"pcm\0"; // Interface identification.
.unwrap(); let mut hints = mem::uninitialized(); // Array of device name hints.
let res = alsa::snd_device_name_hint(card, iface.as_ptr() as *const _, &mut hints);
check_errors(res).unwrap();
let hints = hints as *const *const u8; let hints = hints as *const *const u8;
Devices { Devices {
global_list: hints, global_list: hints,
next_str: hints, next_str: hints,

View File

@ -7,7 +7,7 @@ use ChannelCount;
use CreationError; use CreationError;
use DefaultFormatError; use DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use SupportedFormatsError;
use SampleFormat; use SampleFormat;
use SampleRate; use SampleRate;
use StreamData; use StreamData;
@ -80,7 +80,7 @@ impl Device {
unsafe fn supported_formats( unsafe fn supported_formats(
&self, &self,
stream_t: alsa::snd_pcm_stream_t, stream_t: alsa::snd_pcm_stream_t,
) -> Result<VecIntoIter<SupportedFormat>, FormatsEnumerationError> ) -> Result<VecIntoIter<SupportedFormat>, SupportedFormatsError>
{ {
let mut handle = mem::uninitialized(); let mut handle = mem::uninitialized();
let device_name = ffi::CString::new(&self.0[..]).expect("Unable to get device name"); let device_name = ffi::CString::new(&self.0[..]).expect("Unable to get device name");
@ -92,10 +92,10 @@ impl Device {
alsa::SND_PCM_NONBLOCK, alsa::SND_PCM_NONBLOCK,
) { ) {
-2 | -2 |
-16 /* determined empirically */ => return Err(FormatsEnumerationError::DeviceNotAvailable), -16 /* determined empirically */ => return Err(SupportedFormatsError::DeviceNotAvailable),
-22 => return Err(FormatsEnumerationError::InvalidArgument), -22 => return Err(SupportedFormatsError::InvalidArgument),
e => if check_errors(e).is_err() { e => if check_errors(e).is_err() {
return Err(FormatsEnumerationError::Unknown) return Err(SupportedFormatsError::Unknown)
} }
} }
@ -251,13 +251,13 @@ impl Device {
Ok(output.into_iter()) Ok(output.into_iter())
} }
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
unsafe { unsafe {
self.supported_formats(alsa::SND_PCM_STREAM_CAPTURE) self.supported_formats(alsa::SND_PCM_STREAM_CAPTURE)
} }
} }
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
unsafe { unsafe {
self.supported_formats(alsa::SND_PCM_STREAM_PLAYBACK) self.supported_formats(alsa::SND_PCM_STREAM_PLAYBACK)
} }
@ -272,15 +272,15 @@ impl Device {
{ {
let mut formats: Vec<_> = unsafe { let mut formats: Vec<_> = unsafe {
match self.supported_formats(stream_t) { match self.supported_formats(stream_t) {
Err(FormatsEnumerationError::DeviceNotAvailable) => { Err(SupportedFormatsError::DeviceNotAvailable) => {
return Err(DefaultFormatError::DeviceNotAvailable); return Err(DefaultFormatError::DeviceNotAvailable);
}, },
Err(FormatsEnumerationError::InvalidArgument) => { Err(SupportedFormatsError::InvalidArgument) => {
// this happens sometimes when querying for input and output capabilities but // this happens sometimes when querying for input and output capabilities but
// the device supports only one // the device supports only one
return Err(DefaultFormatError::StreamTypeNotSupported); return Err(DefaultFormatError::StreamTypeNotSupported);
} }
Err(FormatsEnumerationError::Unknown) => { Err(SupportedFormatsError::Unknown) => {
return Err(DefaultFormatError::DeviceNotAvailable); return Err(DefaultFormatError::DeviceNotAvailable);
} }
Ok(fmts) => fmts.collect(), Ok(fmts) => fmts.collect(),

View File

@ -5,7 +5,7 @@ use ChannelCount;
use CreationError; use CreationError;
use DefaultFormatError; use DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use SupportedFormatsError;
use Sample; use Sample;
use SampleFormat; use SampleFormat;
use SampleRate; use SampleRate;
@ -108,7 +108,7 @@ impl Device {
fn supported_formats( fn supported_formats(
&self, &self,
scope: AudioObjectPropertyScope, scope: AudioObjectPropertyScope,
) -> Result<SupportedOutputFormats, FormatsEnumerationError> ) -> Result<SupportedOutputFormats, SupportedFormatsError>
{ {
let mut property_address = AudioObjectPropertyAddress { let mut property_address = AudioObjectPropertyAddress {
mSelector: kAudioDevicePropertyStreamConfiguration, mSelector: kAudioDevicePropertyStreamConfiguration,
@ -212,11 +212,11 @@ impl Device {
} }
} }
pub fn supported_input_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
self.supported_formats(kAudioObjectPropertyScopeInput) self.supported_formats(kAudioObjectPropertyScopeInput)
} }
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
self.supported_formats(kAudioObjectPropertyScopeOutput) self.supported_formats(kAudioObjectPropertyScopeOutput)
} }

View File

@ -11,7 +11,7 @@ use stdweb::web::set_timeout;
use CreationError; use CreationError;
use DefaultFormatError; use DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use SupportedFormatsError;
use StreamData; use StreamData;
use SupportedFormat; use SupportedFormat;
use UnknownTypeOutputBuffer; use UnknownTypeOutputBuffer;
@ -226,12 +226,12 @@ impl Device {
} }
#[inline] #[inline]
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
unimplemented!(); unimplemented!();
} }
#[inline] #[inline]
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
// TODO: right now cpal's API doesn't allow flexibility here // TODO: right now cpal's API doesn't allow flexibility here
// "44100" and "2" (channels) have also been hard-coded in the rest of the code ; if // "44100" and "2" (channels) have also been hard-coded in the rest of the code ; if
// this ever becomes more flexible, don't forget to change that // this ever becomes more flexible, don't forget to change that

View File

@ -281,7 +281,7 @@ pub struct SupportedOutputFormats(cpal_impl::SupportedOutputFormats);
/// Error that can happen when enumerating the list of supported formats. /// Error that can happen when enumerating the list of supported formats.
#[derive(Debug, Fail)] #[derive(Debug, Fail)]
pub enum FormatsEnumerationError { pub enum SupportedFormatsError {
/// 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.
#[fail(display = "The requested device is no longer available. For example, it has been unplugged.")] #[fail(display = "The requested device is no longer available. For example, it has been unplugged.")]
@ -386,7 +386,7 @@ impl Device {
/// ///
/// Can return an error if the device is no longer valid (eg. it has been disconnected). /// Can return an error if the device is no longer valid (eg. it has been disconnected).
#[inline] #[inline]
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
Ok(SupportedInputFormats(self.0.supported_input_formats()?)) Ok(SupportedInputFormats(self.0.supported_input_formats()?))
} }
@ -394,7 +394,7 @@ impl Device {
/// ///
/// Can return an error if the device is no longer valid (eg. it has been disconnected). /// Can return an error if the device is no longer valid (eg. it has been disconnected).
#[inline] #[inline]
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
Ok(SupportedOutputFormats(self.0.supported_output_formats()?)) Ok(SupportedOutputFormats(self.0.supported_output_formats()?))
} }

View File

@ -5,7 +5,7 @@ use std::marker::PhantomData;
use CreationError; use CreationError;
use DefaultFormatError; use DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use SupportedFormatsError;
use StreamData; use StreamData;
use SupportedFormat; use SupportedFormat;
@ -80,12 +80,12 @@ pub struct Device;
impl Device { impl Device {
#[inline] #[inline]
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
unimplemented!() unimplemented!()
} }
#[inline] #[inline]
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
unimplemented!() unimplemented!()
} }

View File

@ -11,7 +11,7 @@ use std::sync::{Arc, Mutex, MutexGuard};
use DefaultFormatError; use DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use SupportedFormatsError;
use SampleFormat; use SampleFormat;
use SampleRate; use SampleRate;
use SupportedFormat; use SupportedFormat;
@ -165,7 +165,7 @@ unsafe fn data_flow_from_immendpoint(endpoint: *const IMMEndpoint) -> EDataFlow
pub unsafe fn is_format_supported( pub unsafe fn is_format_supported(
client: *const IAudioClient, client: *const IAudioClient,
waveformatex_ptr: *const mmreg::WAVEFORMATEX, waveformatex_ptr: *const mmreg::WAVEFORMATEX,
) -> Result<bool, FormatsEnumerationError> ) -> Result<bool, SupportedFormatsError>
{ {
@ -213,7 +213,7 @@ pub unsafe fn is_format_supported(
// has been found, but not an exact match) so we also treat this as unsupported. // has been found, but not an exact match) so we also treat this as unsupported.
match (result, check_result(result)) { match (result, check_result(result)) {
(_, Err(ref e)) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => { (_, Err(ref e)) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
return Err(FormatsEnumerationError::DeviceNotAvailable); return Err(SupportedFormatsError::DeviceNotAvailable);
}, },
(_, Err(_)) => { (_, Err(_)) => {
Ok(false) Ok(false)
@ -386,14 +386,14 @@ impl Device {
// number of channels seems to be supported. Any more or less returns an invalid // number of channels seems to be supported. Any more or less returns an invalid
// parameter error. Thus we just assume that the default number of channels is the only // parameter error. Thus we just assume that the default number of channels is the only
// number supported. // number supported.
fn supported_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { fn supported_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
// initializing COM because we call `CoTaskMemFree` to release the format. // initializing COM because we call `CoTaskMemFree` to release the format.
com::com_initialized(); com::com_initialized();
// Retrieve the `IAudioClient`. // Retrieve the `IAudioClient`.
let lock = match self.ensure_future_audio_client() { let lock = match self.ensure_future_audio_client() {
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) =>
return Err(FormatsEnumerationError::DeviceNotAvailable), return Err(SupportedFormatsError::DeviceNotAvailable),
e => e.unwrap(), e => e.unwrap(),
}; };
let client = lock.unwrap().0; let client = lock.unwrap().0;
@ -403,7 +403,7 @@ impl Device {
let mut default_waveformatex_ptr = WaveFormatExPtr(mem::uninitialized()); let mut default_waveformatex_ptr = WaveFormatExPtr(mem::uninitialized());
match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) { match check_result((*client).GetMixFormat(&mut default_waveformatex_ptr.0)) {
Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => { Err(ref e) if e.raw_os_error() == Some(AUDCLNT_E_DEVICE_INVALIDATED) => {
return Err(FormatsEnumerationError::DeviceNotAvailable); return Err(SupportedFormatsError::DeviceNotAvailable);
}, },
Err(e) => panic!("{:?}", e), Err(e) => panic!("{:?}", e),
Ok(()) => (), Ok(()) => (),
@ -462,7 +462,7 @@ impl Device {
} }
} }
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, FormatsEnumerationError> { pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, SupportedFormatsError> {
if self.data_flow() == eCapture { if self.data_flow() == eCapture {
self.supported_formats() self.supported_formats()
// If it's an output device, assume no input formats. // If it's an output device, assume no input formats.
@ -471,7 +471,7 @@ impl Device {
} }
} }
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, FormatsEnumerationError> { pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, SupportedFormatsError> {
if self.data_flow() == eRender { if self.data_flow() == eRender {
self.supported_formats() self.supported_formats()
// If it's an input device, assume no output formats. // If it's an input device, assume no output formats.