Merge pull request #74 from tomaka/alsa-devices

Alsa devices enumeration
This commit is contained in:
tomaka 2015-09-22 15:20:51 +02:00
commit 8ddbbf2c1e
3 changed files with 45 additions and 16 deletions

View File

@ -5,6 +5,8 @@ use super::Endpoint;
use std::ffi::CStr; use std::ffi::CStr;
use std::mem; use std::mem;
use libc;
/// ALSA implementation for `EndpointsIterator`. /// ALSA implementation for `EndpointsIterator`.
pub struct EndpointsIterator { pub struct EndpointsIterator {
// we keep the original list so that we can pass it to the free function // we keep the original list so that we can pass it to the free function
@ -54,17 +56,41 @@ impl Iterator for EndpointsIterator {
return None; return None;
} }
let name = alsa::snd_device_name_get_hint(*self.next_str as *const _, let name = {
b"NAME".as_ptr() as *const _); 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 n = CStr::from_ptr(n_ptr).to_bytes().to_vec();
let n = String::from_utf8(n).unwrap();
libc::free(n_ptr as *mut _);
Some(n)
} else {
None
}
};
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 n = CStr::from_ptr(n_ptr).to_bytes().to_vec();
let n = String::from_utf8(n).unwrap();
libc::free(n_ptr as *mut _);
Some(n)
} else {
None
}
};
self.next_str = self.next_str.offset(1); self.next_str = self.next_str.offset(1);
if name.is_null() { if let Some(io) = io {
if io != "Output" {
continue; continue;
} }
}
let name = CStr::from_ptr(name).to_bytes().to_vec(); if let Some(name) = name {
let name = String::from_utf8(name).unwrap();
if name != "null" { if name != "null" {
return Some(Endpoint(name)); return Some(Endpoint(name));
} }
@ -72,6 +98,7 @@ impl Iterator for EndpointsIterator {
} }
} }
} }
}
#[inline] #[inline]
pub fn get_default_endpoint() -> Option<Endpoint> { pub fn get_default_endpoint() -> Option<Endpoint> {

View File

@ -27,7 +27,8 @@ impl Endpoint {
{ {
unsafe { unsafe {
let mut playback_handle = mem::uninitialized(); let mut playback_handle = mem::uninitialized();
check_errors(alsa::snd_pcm_open(&mut playback_handle, b"hw\0".as_ptr() as *const _, let device_name = ffi::CString::new(self.0.clone()).unwrap();
check_errors(alsa::snd_pcm_open(&mut playback_handle, device_name.as_ptr() as *const _,
alsa::SND_PCM_STREAM_PLAYBACK, alsa::SND_PCM_STREAM_PLAYBACK,
alsa::SND_PCM_NONBLOCK)).unwrap(); alsa::SND_PCM_NONBLOCK)).unwrap();
@ -89,8 +90,8 @@ impl Endpoint {
let samples_rates = if min_rate == max_rate { let samples_rates = if min_rate == max_rate {
vec![min_rate] vec![min_rate]
} else if alsa::snd_pcm_hw_params_test_rate(playback_handle, hw_params.0, min_rate + 1, 0) == 0 { /*} else if alsa::snd_pcm_hw_params_test_rate(playback_handle, hw_params.0, min_rate + 1, 0) == 0 {
(min_rate .. max_rate + 1).collect() (min_rate .. max_rate + 1).collect()*/ // TODO: code is correct but returns lots of stuff
} else { } else {
const RATES: [libc::c_uint; 13] = [ const RATES: [libc::c_uint; 13] = [
5512, 5512,
@ -115,11 +116,11 @@ impl Endpoint {
} }
} }
if rates.len() == 0 { /*if rates.len() == 0 {
(min_rate .. max_rate + 1).collect() (min_rate .. max_rate + 1).collect()
} else { } else {*/
rates rates // TODO: code is correct but returns lots of stuff
} //}
}; };
let mut min_channels = mem::uninitialized(); let mut min_channels = mem::uninitialized();

View File

@ -39,6 +39,7 @@ calling `append_data` repeatedly if you don't want the audio to stop playing.
*/ */
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate libc;
pub use samples_formats::{SampleFormat, Sample}; pub use samples_formats::{SampleFormat, Sample};