diff --git a/src/host/alsa/enumerate.rs b/src/host/alsa/enumerate.rs index 00e2bb9..32f1894 100644 --- a/src/host/alsa/enumerate.rs +++ b/src/host/alsa/enumerate.rs @@ -1,6 +1,6 @@ use super::alsa; use super::parking_lot::Mutex; -use super::Device; +use super::{Device, DeviceHandles}; use {BackendSpecificError, DevicesError}; /// ALSA implementation for `Devices`. @@ -27,37 +27,17 @@ impl Iterator for Devices { match self.hint_iter.next() { None => return None, Some(hint) => { - let name = hint.name; - - let io = hint.direction; - - if let Some(io) = io { - if io != alsa::Direction::Playback { - continue; - } - } - - let name = match name { - Some(name) => { - // Ignoring the `null` device. - if name == "null" { - continue; - } - name - } - _ => continue, + let name = match hint.name { + None => continue, + // Ignoring the `null` device. + Some(name) if name == "null" => continue, + Some(name) => name, }; - // See if the device has an available output stream. - let playback = alsa::pcm::PCM::new(&name, alsa::Direction::Playback, true).ok(); - - // See if the device has an available input stream. - let capture = alsa::pcm::PCM::new(&name, alsa::Direction::Capture, true).ok(); - - if playback.is_some() || capture.is_some() { + if let Ok(handles) = DeviceHandles::open(&name) { return Some(Device { name, - handles: Mutex::new(super::DeviceHandles { playback, capture }), + handles: Mutex::new(handles), }); } } @@ -70,10 +50,7 @@ impl Iterator for Devices { pub fn default_input_device() -> Option { Some(Device { name: "default".to_owned(), - handles: Mutex::new(super::DeviceHandles { - playback: None, - capture: None, - }), + handles: Mutex::new(Default::default()), }) } @@ -81,10 +58,7 @@ pub fn default_input_device() -> Option { pub fn default_output_device() -> Option { Some(Device { name: "default".to_owned(), - handles: Mutex::new(super::DeviceHandles { - playback: None, - capture: None, - }), + handles: Mutex::new(Default::default()), }) } diff --git a/src/host/alsa/mod.rs b/src/host/alsa/mod.rs index cee4723..d306519 100644 --- a/src/host/alsa/mod.rs +++ b/src/host/alsa/mod.rs @@ -165,17 +165,45 @@ impl Drop for TriggerReceiver { } } +#[derive(Default)] struct DeviceHandles { playback: Option, capture: Option, } impl DeviceHandles { - fn handle_mut(&mut self, stream_type: alsa::Direction) -> &mut Option { - match stream_type { + /// Create `DeviceHandles` for `name` and try to open a handle for both + /// directions. Returns `Ok` if either direction is opened successfully. + fn open(name: &str) -> Result { + let mut handles = Self::default(); + let playback_err = handles.try_open(name, alsa::Direction::Playback).err(); + let capture_err = handles.try_open(name, alsa::Direction::Capture).err(); + if let Some(err) = capture_err.and(playback_err) { + Err(err) + } else { + Ok(handles) + } + } + + /// Get a mutable reference to the `Option` for a specific `stream_type`. + /// If the `Option` is `None`, the `alsa::PCM` will be opened and placed in + /// the `Option` before returning. If `handle_mut()` returns `Ok` the contained + /// `Option` is guaranteed to be `Some(..)`. + fn try_open( + &mut self, + name: &str, + stream_type: alsa::Direction, + ) -> Result<&mut Option, alsa::Error> { + let handle = match stream_type { alsa::Direction::Playback => &mut self.playback, alsa::Direction::Capture => &mut self.capture, + }; + + if handle.is_none() { + *handle = Some(alsa::pcm::PCM::new(name, stream_type, true)?); } + + Ok(handle) } /// Get a mutable reference to the `alsa::PCM` handle for a specific `stream_type`. @@ -185,23 +213,13 @@ impl DeviceHandles { name: &str, stream_type: alsa::Direction, ) -> Result<&mut alsa::PCM, alsa::Error> { - let pcm = self.handle_mut(stream_type); - match pcm { - Some(pcm) => Ok(pcm), - None => { - *pcm = Some(alsa::pcm::PCM::new(name, stream_type, true)?); - Ok(pcm.as_mut().unwrap()) - } - } + Ok(self.try_open(name, stream_type)?.as_mut().unwrap()) } /// Take ownership of the `alsa::PCM` handle for a specific `stream_type`. /// If the handle is not yet opened, it will be opened and returned. fn take(&mut self, name: &str, stream_type: alsa::Direction) -> Result { - match self.handle_mut(stream_type).take() { - Some(pcm) => Ok(pcm), - None => Ok(alsa::pcm::PCM::new(name, stream_type, true)?), - } + Ok(self.try_open(name, stream_type)?.take().unwrap()) } }