Switch from vendored alsa-sys to alsa crate

This commit is contained in:
Alex Moon 2020-04-03 12:14:52 -04:00
parent f3e7c46205
commit 62d540d396
7 changed files with 255 additions and 4322 deletions

View file

@ -1,139 +1,67 @@
use super::alsa;
use super::check_errors;
use super::Device;
use std::ffi::CString;
use std::ptr;
use {BackendSpecificError, DevicesError};
/// ALSA implementation for `Devices`.
pub struct Devices {
// we keep the original list so that we can pass it to the free function
global_list: *const *const u8,
// pointer to the next string ; contained within `global_list`
next_str: *const *const u8,
hint_iter: alsa::device_name::HintIter,
}
impl Devices {
pub fn new() -> Result<Self, DevicesError> {
unsafe {
// TODO: check in which situation this can fail.
let card = -1; // -1 means all cards.
let iface = b"pcm\0"; // Interface identification.
let mut hints = ptr::null_mut(); // Array of device name hints.
let res = alsa::snd_device_name_hint(card, iface.as_ptr() as *const _, &mut hints);
if let Err(description) = check_errors(res) {
let err = BackendSpecificError { description };
return Err(err.into());
}
let hints = hints as *const *const u8;
let devices = Devices {
global_list: hints,
next_str: hints,
};
Ok(devices)
}
Ok(Devices {
hint_iter: alsa::device_name::HintIter::new_str(None, "pcm")?,
})
}
}
unsafe impl Send for Devices {}
unsafe impl Sync for Devices {}
impl Drop for Devices {
#[inline]
fn drop(&mut self) {
unsafe {
alsa::snd_device_name_free_hint(self.global_list as *mut _);
}
}
}
impl Iterator for Devices {
type Item = Device;
fn next(&mut self) -> Option<Device> {
loop {
unsafe {
if (*self.next_str).is_null() {
return None;
}
match self.hint_iter.next() {
None => return None,
Some(hint) => {
let name = hint.name;
let name = {
let n_ptr = alsa::snd_device_name_get_hint(
*self.next_str as *const _,
b"NAME\0".as_ptr() as *const _,
);
if !n_ptr.is_null() {
let bytes = CString::from_raw(n_ptr).into_bytes();
let string = String::from_utf8(bytes).unwrap();
Some(string)
} else {
None
}
};
let io = hint.direction;
let io = {
let n_ptr = alsa::snd_device_name_get_hint(
*self.next_str as *const _,
b"IOID\0".as_ptr() as *const _,
);
if !n_ptr.is_null() {
let bytes = CString::from_raw(n_ptr).into_bytes();
let string = String::from_utf8(bytes).unwrap();
Some(string)
} else {
None
}
};
self.next_str = self.next_str.offset(1);
if let Some(io) = io {
if io != "Output" {
continue;
}
}
let name = match name {
Some(name) => {
// Ignoring the `null` device.
if name == "null" {
if let Some(io) = io {
if io != alsa::Direction::Playback {
continue;
}
name
}
_ => continue,
};
// trying to open the PCM device to see if it can be opened
let name_zeroed = CString::new(&name[..]).unwrap();
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.
let mut playback_handle = ptr::null_mut();
let has_available_output = alsa::snd_pcm_open(
&mut playback_handle,
name_zeroed.as_ptr() as *const _,
alsa::SND_PCM_STREAM_PLAYBACK,
alsa::SND_PCM_NONBLOCK,
) == 0;
if has_available_output {
alsa::snd_pcm_close(playback_handle);
}
// See if the device has an available output stream.
let has_available_output = {
let playback_handle = alsa::pcm::PCM::new(&name, alsa::Direction::Playback, true);
playback_handle.is_ok()
};
// See if the device has an available input stream.
let mut capture_handle = ptr::null_mut();
let has_available_input = alsa::snd_pcm_open(
&mut capture_handle,
name_zeroed.as_ptr() as *const _,
alsa::SND_PCM_STREAM_CAPTURE,
alsa::SND_PCM_NONBLOCK,
) == 0;
if has_available_input {
alsa::snd_pcm_close(capture_handle);
}
// See if the device has an available input stream.
let has_available_input = {
let capture_handle = alsa::pcm::PCM::new(&name, alsa::Direction::Capture, true);
capture_handle.is_ok()
};
if has_available_output || has_available_input {
return Some(Device(name));
if has_available_output || has_available_input {
return Some(Device(name));
}
}
}
}
@ -149,3 +77,10 @@ pub fn default_input_device() -> Option<Device> {
pub fn default_output_device() -> Option<Device> {
Some(Device("default".to_owned()))
}
impl From<alsa::Error> for DevicesError {
fn from(err: alsa::Error) -> Self {
let err: BackendSpecificError = err.into();
err.into()
}
}