librespot/playback/src/audio_backend/alsa.rs

125 lines
3.6 KiB
Rust
Raw Normal View History

2016-03-14 02:16:59 +00:00
use super::{Open, Sink};
2018-03-21 21:18:37 +00:00
use alsa::device_name::HintIter;
use alsa::pcm::{Access, Format, HwParams, PCM};
2018-09-01 00:42:50 +00:00
use alsa::{Direction, Error, ValueOr};
use std::env;
use std::ffi::CString;
2016-03-14 02:16:59 +00:00
use std::io;
2018-03-21 21:18:37 +00:00
use std::process::exit;
2016-03-14 02:16:59 +00:00
pub struct AlsaSink(Option<PCM>, String);
2018-03-21 21:18:37 +00:00
fn list_outputs() {
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
2018-09-01 00:42:50 +00:00
println!("{} devices:", t);
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
for a in i {
println!(" {:?}", a)
}
}
}
fn open_device(dev_name: &str) -> Result<(PCM), Box<Error>> {
let pcm = PCM::new(dev_name, Direction::Playback, false)?;
// http://www.linuxjournal.com/article/6735?page=0,1#N0x19ab2890.0x19ba78d8
// latency = period_size * periods / (rate * bytes_per_frame)
// For 16 Bit stereo data, one frame has a length of four bytes.
// 500ms = buffer_size / (44100 * 4)
// buffer_size = 0.5 * 44100 = 22050 frames
{
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
let hwp = HwParams::any(&pcm)?;
hwp.set_access(Access::RWInterleaved)?;
hwp.set_format(Format::s16())?;
hwp.set_rate(44100, ValueOr::Nearest)?;
hwp.set_channels(2)?;
// hwp.set_period_size_near(256, ValueOr::Nearest)?;
hwp.set_buffer_size_near(11025 * 2)?; // ~ 0.25 x 2 s latency
pcm.hw_params(&hwp)?;
}
// Additional software paramters + check
if env::var("LIBRESPOT_DEBUG").is_ok() {
let hwp = pcm.hw_params_current()?;
let swp = pcm.sw_params_current()?;
let (bufsize, periodsize) = (hwp.get_buffer_size()?, hwp.get_period_size()?);
let periods = hwp.get_periods()?;
info!(
"periods: {:?} buffer_size: {:?} period_size {:?}",
periods, bufsize, periodsize
);
// Not required now that buffer size is set properly
// swp.set_start_threshold(bufsize - periodsize)?;
// swp.set_avail_min(periodsize)?;
// pcm.sw_params(&swp).unwrap();
info!(
"Opened audio output {:?} with parameters: {:?}, {:?}",
dev_name, hwp, swp
);
}
Ok(pcm)
2018-03-21 21:18:37 +00:00
}
2016-03-14 02:16:59 +00:00
impl Open for AlsaSink {
2018-03-20 15:32:43 +00:00
fn open(device: Option<String>) -> AlsaSink {
info!("Using alsa sink");
2016-03-14 02:16:59 +00:00
2018-03-21 21:18:37 +00:00
let name = match device.as_ref().map(AsRef::as_ref) {
Some("?") => {
2018-09-01 00:42:50 +00:00
println!("Listing available alsa outputs");
2018-03-21 21:18:37 +00:00
list_outputs();
exit(0)
}
Some(device) => device,
None => "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-21 21:18:37 +00:00
if self.0.is_none() {
2018-09-01 00:42:50 +00:00
let pcm = open_device(&self.1);
match pcm {
Ok(p) => self.0 = Some(p),
Err(e) => {
error!("Alsa error PCM open {}", e);
return Err(io::Error::new(
io::ErrorKind::Other,
"Alsa error: PCM open failed",
));
2018-03-21 21:18:37 +00:00
}
2018-09-01 00:42:50 +00:00
}
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<()> {
2018-03-21 21:18:37 +00:00
{
2018-09-01 00:42:50 +00:00
let pcm = self.0.as_ref().unwrap();
2018-03-21 21:18:37 +00:00
pcm.drain().unwrap();
}
2016-03-14 02:16:59 +00:00
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(_) => (),
2018-03-21 21:18:37 +00:00
Err(err) => pcm.try_recover(err, false).unwrap(),
// Err(err) => println!("{:?}",err),
2018-03-20 15:32:43 +00:00
}
2016-03-14 02:16:59 +00:00
Ok(())
}
}