2016-03-14 02:16:59 +00:00
|
|
|
use super::{Open, Sink};
|
|
|
|
use std::io;
|
|
|
|
|
2018-03-20 15:32:43 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
use alsa::{Direction, ValueOr};
|
|
|
|
use alsa::pcm::{PCM, HwParams, Format, Access};
|
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
pub struct AlsaSink(Option<PCM>, String);
|
|
|
|
|
|
|
|
impl Open for AlsaSink {
|
2018-03-20 15:32:43 +00:00
|
|
|
fn open(device: Option<String>) -> AlsaSink {
|
2017-01-06 14:21:44 +00:00
|
|
|
info!("Using alsa sink");
|
2016-03-14 02:16:59 +00:00
|
|
|
|
2017-01-18 18:41:22 +00:00
|
|
|
let name = device.unwrap_or("default".to_string());
|
2016-03-14 02:16:59 +00:00
|
|
|
|
|
|
|
AlsaSink(None, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for AlsaSink {
|
|
|
|
fn start(&mut self) -> io::Result<()> {
|
2018-03-20 15:32:43 +00:00
|
|
|
if self.0.is_some() {
|
|
|
|
} else {
|
|
|
|
let pcm = PCM::open(&*CString::new(self.1.to_owned().into_bytes()).unwrap(),
|
|
|
|
Direction::Playback,
|
|
|
|
false).unwrap();
|
|
|
|
{
|
|
|
|
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
|
|
|
|
let hwp = HwParams::any(&pcm).unwrap();
|
|
|
|
hwp.set_channels(2).unwrap();
|
|
|
|
hwp.set_rate(44100, ValueOr::Nearest).unwrap();
|
|
|
|
hwp.set_format(Format::s16()).unwrap();
|
|
|
|
hwp.set_access(Access::RWInterleaved).unwrap();
|
|
|
|
pcm.hw_params(&hwp).unwrap();
|
2018-01-26 00:42:24 +00:00
|
|
|
}
|
2018-03-20 15:32:43 +00:00
|
|
|
|
|
|
|
self.0 = Some(pcm);
|
2016-03-14 02:16:59 +00:00
|
|
|
}
|
2018-03-20 15:32:43 +00:00
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
|
|
|
self.0 = None;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
2018-03-20 15:32:43 +00:00
|
|
|
let pcm = self.0.as_mut().unwrap();
|
|
|
|
let io = pcm.io_i16().unwrap();
|
|
|
|
|
|
|
|
match io.writei(&data) {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => pcm.recover(err.code(), false).unwrap(),
|
|
|
|
}
|
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|