diff --git a/src/alsa/enumerate.rs b/src/alsa/enumerate.rs index 6e590ac..5ceaba6 100644 --- a/src/alsa/enumerate.rs +++ b/src/alsa/enumerate.rs @@ -5,6 +5,8 @@ use super::Endpoint; use std::ffi::CStr; use std::mem; +use libc; + /// ALSA implementation for `EndpointsIterator`. pub struct EndpointsIterator { // we keep the original list so that we can pass it to the free function @@ -54,19 +56,44 @@ impl Iterator for EndpointsIterator { return None; } - let name = alsa::snd_device_name_get_hint(*self.next_str as *const _, - b"NAME".as_ptr() as *const _); + 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 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); - if name.is_null() { - continue; + if let Some(io) = io { + if io != "Output" { + continue; + } } - let name = CStr::from_ptr(name).to_bytes().to_vec(); - let name = String::from_utf8(name).unwrap(); - - if name != "null" { - return Some(Endpoint(name)); + if let Some(name) = name { + if name != "null" { + return Some(Endpoint(name)); + } } } } diff --git a/src/alsa/mod.rs b/src/alsa/mod.rs index 7dc6cc8..f8a041f 100644 --- a/src/alsa/mod.rs +++ b/src/alsa/mod.rs @@ -27,7 +27,8 @@ impl Endpoint { { unsafe { 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_NONBLOCK)).unwrap(); @@ -89,8 +90,8 @@ impl Endpoint { let samples_rates = if min_rate == max_rate { vec![min_rate] - } 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() + /*} 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()*/ // TODO: code is correct but returns lots of stuff } else { const RATES: [libc::c_uint; 13] = [ 5512, @@ -115,11 +116,11 @@ impl Endpoint { } } - if rates.len() == 0 { + /*if rates.len() == 0 { (min_rate .. max_rate + 1).collect() - } else { - rates - } + } else {*/ + rates // TODO: code is correct but returns lots of stuff + //} }; let mut min_channels = mem::uninitialized(); diff --git a/src/lib.rs b/src/lib.rs index cf50a6f..c2d42c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ calling `append_data` repeatedly if you don't want the audio to stop playing. */ #[macro_use] extern crate lazy_static; +extern crate libc; pub use samples_formats::{SampleFormat, Sample};