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::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<Device> {
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<Device> {
pub fn default_output_device() -> Option<Device> {
Some(Device {
name: "default".to_owned(),
handles: Mutex::new(super::DeviceHandles {
playback: None,
capture: None,
}),
handles: Mutex::new(Default::default()),
})
}

View file

@ -165,17 +165,45 @@ impl Drop for TriggerReceiver {
}
}
#[derive(Default)]
struct DeviceHandles {
playback: Option<alsa::PCM>,
capture: Option<alsa::PCM>,
}
impl DeviceHandles {
fn handle_mut(&mut self, stream_type: alsa::Direction) -> &mut Option<alsa::PCM> {
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<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::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<alsa::PCM, alsa::Error> {
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())
}
}