Fix bug and filter out devices that are not "Output"

This commit is contained in:
Pierre Krieger 2015-09-22 15:04:32 +02:00
parent debb6c957c
commit 73906885f9
2 changed files with 37 additions and 9 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

@ -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};