Further refactoring of DeviceHandles.

Also fixes rustaudio/cpal#515.
This commit is contained in:
Alex Moon 2020-12-09 08:33:18 -07:00
parent b89df31797
commit 9c4e240a7c
2 changed files with 42 additions and 50 deletions

View file

@ -1,6 +1,6 @@
use super::alsa; use super::alsa;
use super::parking_lot::Mutex; use super::parking_lot::Mutex;
use super::Device; use super::{Device, DeviceHandles};
use {BackendSpecificError, DevicesError}; use {BackendSpecificError, DevicesError};
/// ALSA implementation for `Devices`. /// ALSA implementation for `Devices`.
@ -27,37 +27,17 @@ impl Iterator for Devices {
match self.hint_iter.next() { match self.hint_iter.next() {
None => return None, None => return None,
Some(hint) => { Some(hint) => {
let name = hint.name; let name = match hint.name {
None => continue,
let io = hint.direction; // Ignoring the `null` device.
Some(name) if name == "null" => continue,
if let Some(io) = io { Some(name) => name,
if io != alsa::Direction::Playback {
continue;
}
}
let name = match name {
Some(name) => {
// Ignoring the `null` device.
if name == "null" {
continue;
}
name
}
_ => continue,
}; };
// See if the device has an available output stream. if let Ok(handles) = DeviceHandles::open(&name) {
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() {
return Some(Device { return Some(Device {
name, 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<Device> { pub fn default_input_device() -> Option<Device> {
Some(Device { Some(Device {
name: "default".to_owned(), name: "default".to_owned(),
handles: Mutex::new(super::DeviceHandles { handles: Mutex::new(Default::default()),
playback: None,
capture: None,
}),
}) })
} }
@ -81,10 +58,7 @@ pub fn default_input_device() -> Option<Device> {
pub fn default_output_device() -> Option<Device> { pub fn default_output_device() -> Option<Device> {
Some(Device { Some(Device {
name: "default".to_owned(), name: "default".to_owned(),
handles: Mutex::new(super::DeviceHandles { handles: Mutex::new(Default::default()),
playback: None,
capture: None,
}),
}) })
} }

View file

@ -165,17 +165,45 @@ impl Drop for TriggerReceiver {
} }
} }
#[derive(Default)]
struct DeviceHandles { struct DeviceHandles {
playback: Option<alsa::PCM>, playback: Option<alsa::PCM>,
capture: Option<alsa::PCM>, capture: Option<alsa::PCM>,
} }
impl DeviceHandles { impl DeviceHandles {
fn handle_mut(&mut self, stream_type: alsa::Direction) -> &mut Option<alsa::PCM> { /// Create `DeviceHandles` for `name` and try to open a handle for both
match stream_type { /// directions. Returns `Ok` if either direction is opened successfully.
fn open(name: &str) -> Result<Self, alsa::Error> {
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::PCM>, alsa::Error> {
let handle = match stream_type {
alsa::Direction::Playback => &mut self.playback, alsa::Direction::Playback => &mut self.playback,
alsa::Direction::Capture => &mut self.capture, 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`. /// Get a mutable reference to the `alsa::PCM` handle for a specific `stream_type`.
@ -185,23 +213,13 @@ impl DeviceHandles {
name: &str, name: &str,
stream_type: alsa::Direction, stream_type: alsa::Direction,
) -> Result<&mut alsa::PCM, alsa::Error> { ) -> Result<&mut alsa::PCM, alsa::Error> {
let pcm = self.handle_mut(stream_type); Ok(self.try_open(name, stream_type)?.as_mut().unwrap())
match pcm {
Some(pcm) => Ok(pcm),
None => {
*pcm = Some(alsa::pcm::PCM::new(name, stream_type, true)?);
Ok(pcm.as_mut().unwrap())
}
}
} }
/// Take ownership of the `alsa::PCM` handle for a specific `stream_type`. /// 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. /// If the handle is not yet opened, it will be opened and returned.
fn take(&mut self, name: &str, stream_type: alsa::Direction) -> Result<alsa::PCM, alsa::Error> { fn take(&mut self, name: &str, stream_type: alsa::Direction) -> Result<alsa::PCM, alsa::Error> {
match self.handle_mut(stream_type).take() { Ok(self.try_open(name, stream_type)?.take().unwrap())
Some(pcm) => Ok(pcm),
None => Ok(alsa::pcm::PCM::new(name, stream_type, true)?),
}
} }
} }