Refactor ALSA handle manipulation into methods on DeviceHandles.

This commit is contained in:
Alex Moon 2020-12-07 16:14:21 -07:00
parent fb30d291ce
commit b89df31797

View file

@ -170,6 +170,41 @@ struct DeviceHandles {
capture: 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 {
alsa::Direction::Playback => &mut self.playback,
alsa::Direction::Capture => &mut self.capture,
}
}
/// Get a mutable reference to the `alsa::PCM` handle for a specific `stream_type`.
/// If the handle is not yet opened, it will be opened and stored in `self`.
fn get_mut(
&mut self,
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())
}
}
}
/// 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)?),
}
}
}
pub struct Device { pub struct Device {
name: String, name: String,
handles: Mutex<DeviceHandles>, handles: Mutex<DeviceHandles>,
@ -182,19 +217,11 @@ impl Device {
sample_format: SampleFormat, sample_format: SampleFormat,
stream_type: alsa::Direction, stream_type: alsa::Direction,
) -> Result<StreamInner, BuildStreamError> { ) -> Result<StreamInner, BuildStreamError> {
let name = &self.name; let handle_result = self
.handles
let device_handle = { .lock()
let mut guard = self.handles.lock(); .take(&self.name, stream_type)
match stream_type { .map_err(|e| (e, e.errno()));
alsa::Direction::Playback => guard.playback.take(),
alsa::Direction::Capture => guard.capture.take(),
}
};
let handle_result = Ok(device_handle).transpose().unwrap_or_else(|| {
alsa::pcm::PCM::new(name, stream_type, true).map_err(|e| (e, e.errno()))
});
let handle = match handle_result { let handle = match handle_result {
Err((_, Some(nix::errno::Errno::EBUSY))) => { Err((_, Some(nix::errno::Errno::EBUSY))) => {
@ -256,24 +283,10 @@ impl Device {
&self, &self,
stream_t: alsa::Direction, stream_t: alsa::Direction,
) -> Result<VecIntoIter<SupportedStreamConfigRange>, SupportedStreamConfigsError> { ) -> Result<VecIntoIter<SupportedStreamConfigRange>, SupportedStreamConfigsError> {
let name = &self.name;
let mut guard = self.handles.lock(); let mut guard = self.handles.lock();
let handle_result = guard
let device_handle = match stream_t { .get_mut(&self.name, stream_t)
alsa::Direction::Playback => &mut guard.playback, .map_err(|e| (e, e.errno()));
alsa::Direction::Capture => &mut guard.capture,
};
let handle_result = match device_handle {
Some(handle) => Ok(handle),
None => alsa::pcm::PCM::new(name, stream_t, true)
.map(|handle| {
*device_handle = Some(handle);
device_handle.as_mut().unwrap()
})
.map_err(|e| (e, e.errno())),
};
let handle = match handle_result { let handle = match handle_result {
Err((_, Some(nix::errno::Errno::ENOENT))) Err((_, Some(nix::errno::Errno::ENOENT)))