mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-06 03:50:06 +02:00
Switch to latest alsa-rs
crate
This commit is contained in:
parent
a67048c3d7
commit
08cfb1516d
3 changed files with 56 additions and 20 deletions
|
@ -1,17 +1,34 @@
|
|||
use super::{Open, Sink};
|
||||
use alsa::{Direction, Error, ValueOr};
|
||||
use alsa::device_name::HintIter;
|
||||
use std::ffi::{CStr, CString};
|
||||
use alsa::pcm::{Access, Format, HwParams, PCM};
|
||||
use std::io;
|
||||
use std::process::exit;
|
||||
|
||||
use std::ffi::CString;
|
||||
use alsa::{Direction, ValueOr};
|
||||
use alsa::pcm::{PCM, HwParams, Format, Access};
|
||||
|
||||
pub struct AlsaSink(Option<PCM>, String);
|
||||
|
||||
fn list_outputs() {
|
||||
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
|
||||
println!("{} devices:", t);
|
||||
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
|
||||
for a in i { println!(" {:?}", a) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Open for AlsaSink {
|
||||
fn open(device: Option<String>) -> AlsaSink {
|
||||
info!("Using alsa sink");
|
||||
|
||||
let name = device.unwrap_or("default".to_string());
|
||||
let name = match device.as_ref().map(AsRef::as_ref) {
|
||||
Some("?") => {
|
||||
list_outputs();
|
||||
exit(0)
|
||||
}
|
||||
Some(device) => device,
|
||||
None => "default",
|
||||
}.to_string();
|
||||
|
||||
AlsaSink(None, name)
|
||||
}
|
||||
|
@ -19,11 +36,8 @@ impl Open for AlsaSink {
|
|||
|
||||
impl Sink for AlsaSink {
|
||||
fn start(&mut self) -> io::Result<()> {
|
||||
if self.0.is_some() {
|
||||
} else {
|
||||
let pcm = PCM::open(&*CString::new(self.1.to_owned().into_bytes()).unwrap(),
|
||||
Direction::Playback,
|
||||
false).unwrap();
|
||||
if self.0.is_none() {
|
||||
let pcm = PCM::new(&*self.1, Direction::Playback, false).unwrap();
|
||||
{
|
||||
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
|
||||
let hwp = HwParams::any(&pcm).unwrap();
|
||||
|
@ -32,7 +46,9 @@ impl Sink for AlsaSink {
|
|||
hwp.set_format(Format::s16()).unwrap();
|
||||
hwp.set_access(Access::RWInterleaved).unwrap();
|
||||
pcm.hw_params(&hwp).unwrap();
|
||||
}
|
||||
println!("PCM status: {:?}, {:?}", pcm.state(), pcm.hw_params_current().unwrap())
|
||||
}
|
||||
PCM::prepare(&pcm).unwrap();
|
||||
|
||||
self.0 = Some(pcm);
|
||||
}
|
||||
|
@ -41,6 +57,10 @@ impl Sink for AlsaSink {
|
|||
}
|
||||
|
||||
fn stop(&mut self) -> io::Result<()> {
|
||||
{
|
||||
let pcm = self.0.as_mut().unwrap();
|
||||
pcm.drain().unwrap();
|
||||
}
|
||||
self.0 = None;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -51,7 +71,8 @@ impl Sink for AlsaSink {
|
|||
|
||||
match io.writei(&data) {
|
||||
Ok(_) => (),
|
||||
Err(err) => pcm.recover(err.code(), false).unwrap(),
|
||||
Err(err) => pcm.try_recover(err, false).unwrap(),
|
||||
// Err(err) => println!("{:?}",err),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue