1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-06 03:50:06 +02:00

Move audio backends into seperate crate

This commit is contained in:
Sasha Hilton 2018-02-09 02:05:50 +01:00
parent bd59ded224
commit 1fb65354b0
16 changed files with 112 additions and 45 deletions

View file

@ -0,0 +1,43 @@
use super::{Open, Sink};
use std::io;
use alsa::{PCM, Stream, Mode, Format, Access};
pub struct AlsaSink(Option<PCM>, String);
impl Open for AlsaSink {
fn open(device: Option<String>) -> AlsaSink {
info!("Using alsa sink");
let name = device.unwrap_or("default".to_string());
AlsaSink(None, name)
}
}
impl Sink for AlsaSink {
fn start(&mut self) -> io::Result<()> {
if self.0.is_none() {
match PCM::open(&*self.1,
Stream::Playback, Mode::Blocking,
Format::Signed16, Access::Interleaved,
2, 44100) {
Ok(f) => self.0 = Some(f),
Err(e) => {
error!("Alsa error PCM open {}", e);
return Err(io::Error::new(io::ErrorKind::Other, "Alsa error: PCM open failed"));
}
}
}
Ok(())
}
fn stop(&mut self) -> io::Result<()> {
self.0 = None;
Ok(())
}
fn write(&mut self, data: &[i16]) -> io::Result<()> {
self.0.as_mut().unwrap().write_interleaved(&data).unwrap();
Ok(())
}
}

View file

@ -0,0 +1,75 @@
use std::io;
use super::{Open, Sink};
use jack::prelude::{AudioOutPort, AudioOutSpec, Client, JackControl, ProcessScope, AsyncClient, client_options, ProcessHandler, Port };
use std::sync::mpsc::{sync_channel, SyncSender, Receiver};
pub struct JackSink {
send: SyncSender<i16>,
active_client: AsyncClient<(),JackData>,
}
pub struct JackData {
rec: Receiver<i16>,
port_l: Port<AudioOutSpec>,
port_r: Port<AudioOutSpec>,
}
fn pcm_to_f32(sample: i16) -> f32 {
sample as f32 / 32768.0;
}
impl ProcessHandler for JackData {
fn process(&mut self, _: &Client, ps: &ProcessScope) -> JackControl {
// get output port buffers
let mut out_r = AudioOutPort::new(&mut self.port_r, ps);
let mut out_l = AudioOutPort::new(&mut self.port_l, ps);
let buf_r: &mut [f32] = &mut out_r;
let buf_l: &mut [f32] = &mut out_l;
// get queue iterator
let mut queue_iter = self.rec.try_iter();
let buf_size = buf_r.len();
for i in 0..buf_size {
buf_r[i] = pcm_to_f32(queue_iter.next().unwrap_or(0));
buf_l[i] = pcm_to_f32(queue_iter.next().unwrap_or(0));
}
JackControl::Continue
}
}
impl Open for JackSink {
fn open(client_name: Option<String>) -> JackSink {
info!("Using jack sink!");
let client_name = client_name.unwrap_or("librespot".to_string());
let (client, _status) = Client::new(&client_name[..], client_options::NO_START_SERVER).unwrap();
let ch_r = client.register_port("out_0", AudioOutSpec::default()).unwrap();
let ch_l = client.register_port("out_1", AudioOutSpec::default()).unwrap();
// buffer for samples from librespot (~10ms)
let (tx, rx) = sync_channel(2*1024*4);
let jack_data = JackData { rec: rx, port_l: ch_l, port_r: ch_r };
let active_client = AsyncClient::new(client, (), jack_data).unwrap();
JackSink { send: tx, active_client: active_client }
}
}
impl Sink for JackSink {
fn start(&mut self) -> io::Result<()> {
Ok(())
}
fn stop(&mut self) -> io::Result<()> {
Ok(())
}
fn write(&mut self, data: &[i16]) -> io::Result<()> {
for s in data.iter() {
let res = self.send.send(*s);
if res.is_err() {
error!("jackaudio: cannot write to channel");
}
}
Ok(())
}
}

View file

@ -0,0 +1,60 @@
use std::io;
pub trait Open {
fn open(Option<String>) -> Self;
}
pub trait Sink {
fn start(&mut self) -> io::Result<()>;
fn stop(&mut self) -> io::Result<()>;
fn write(&mut self, data: &[i16]) -> io::Result<()>;
}
fn mk_sink<S: Sink + Open + 'static>(device: Option<String>) -> Box<Sink> {
Box::new(S::open(device))
}
#[cfg(feature = "alsa-backend")]
mod alsa;
#[cfg(feature = "alsa-backend")]
use self::alsa::AlsaSink;
#[cfg(feature = "portaudio-backend")]
mod portaudio;
#[cfg(feature = "portaudio-backend")]
use self::portaudio::PortAudioSink;
#[cfg(feature = "pulseaudio-backend")]
mod pulseaudio;
#[cfg(feature = "pulseaudio-backend")]
use self::pulseaudio::PulseAudioSink;
#[cfg(feature = "jackaudio-backend")]
mod jackaudio;
#[cfg(feature = "jackaudio-backend")]
use self::jackaudio::JackSink;
mod pipe;
use self::pipe::StdoutSink;
pub const BACKENDS : &'static [
(&'static str, fn(Option<String>) -> Box<Sink>)
] = &[
#[cfg(feature = "alsa-backend")]
("alsa", mk_sink::<AlsaSink>),
#[cfg(feature = "portaudio-backend")]
("portaudio", mk_sink::<PortAudioSink>),
#[cfg(feature = "pulseaudio-backend")]
("pulseaudio", mk_sink::<PulseAudioSink>),
#[cfg(feature = "jackaudio-backend")]
("jackaudio", mk_sink::<JackSink>),
("pipe", mk_sink::<StdoutSink>),
];
pub fn find(name: Option<String>) -> Option<fn(Option<String>) -> Box<Sink>> {
if let Some(name) = name {
BACKENDS.iter().find(|backend| name == backend.0).map(|backend| backend.1)
} else {
Some(BACKENDS.first().expect("No backends were enabled at build time").1)
}
}

View file

@ -0,0 +1,40 @@
use super::{Open, Sink};
use std::fs::OpenOptions;
use std::io::{self, Write};
use std::mem;
use std::slice;
pub struct StdoutSink(Box<Write>);
impl Open for StdoutSink {
fn open(path: Option<String>) -> StdoutSink {
if let Some(path) = path {
let file = OpenOptions::new().write(true).open(path).unwrap();
StdoutSink(Box::new(file))
} else {
StdoutSink(Box::new(io::stdout()))
}
}
}
impl Sink for StdoutSink {
fn start(&mut self) -> io::Result<()> {
Ok(())
}
fn stop(&mut self) -> io::Result<()> {
Ok(())
}
fn write(&mut self, data: &[i16]) -> io::Result<()> {
let data: &[u8] = unsafe {
slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::<i16>())
};
self.0.write_all(data)?;
self.0.flush()?;
Ok(())
}
}

View file

@ -0,0 +1,110 @@
use super::{Open, Sink};
use std::io;
use std::process::exit;
use std::time::Duration;
use portaudio_rs;
use portaudio_rs::stream::*;
use portaudio_rs::device::{DeviceIndex, DeviceInfo, get_default_output_index};
pub struct PortAudioSink<'a>(Option<portaudio_rs::stream::Stream<'a, i16, i16>>, StreamParameters<i16>);
fn output_devices() -> Box<Iterator<Item=(DeviceIndex, DeviceInfo)>> {
let count = portaudio_rs::device::get_count().unwrap();
let devices = (0..count)
.filter_map(|idx| {
portaudio_rs::device::get_info(idx).map(|info| (idx, info))
}).filter(|&(_, ref info)| {
info.max_output_channels > 0
});
Box::new(devices)
}
fn list_outputs() {
let default = get_default_output_index();
for (idx, info) in output_devices() {
if Some(idx) == default {
println!("- {} (default)", info.name);
} else {
println!("- {}", info.name)
}
}
}
fn find_output(device: &str) -> Option<DeviceIndex> {
output_devices()
.find(|&(_, ref info)| info.name == device)
.map(|(idx, _)| idx)
}
impl <'a> Open for PortAudioSink<'a> {
fn open(device: Option<String>) -> PortAudioSink<'a> {
debug!("Using PortAudio sink");
portaudio_rs::initialize().unwrap();
let device_idx = match device.as_ref().map(AsRef::as_ref) {
Some("?") => {
list_outputs();
exit(0)
}
Some(device) => find_output(device),
None => get_default_output_index(),
}.expect("Could not find device");
let info = portaudio_rs::device::get_info(device_idx);
let latency = match info {
Some(info) => info.default_high_output_latency,
None => Duration::new(0, 0),
};
let params = StreamParameters {
device: device_idx,
channel_count: 2,
suggested_latency: latency,
data: 0i16,
};
PortAudioSink(None, params)
}
}
impl <'a> Sink for PortAudioSink<'a> {
fn start(&mut self) -> io::Result<()> {
if self.0.is_none() {
self.0 = Some(Stream::open(
None, Some(self.1),
44100.0,
FRAMES_PER_BUFFER_UNSPECIFIED,
StreamFlags::empty(),
None
).unwrap());;
}
self.0.as_mut().unwrap().start().unwrap();
Ok(())
}
fn stop(&mut self) -> io::Result<()> {
self.0.as_mut().unwrap().stop().unwrap();
self.0 = None;
Ok(())
}
fn write(&mut self, data: &[i16]) -> io::Result<()> {
match self.0.as_mut().unwrap().write(data) {
Ok(_) => (),
Err(portaudio_rs::PaError::OutputUnderflowed) =>
error!("PortAudio write underflow"),
Err(e) => panic!("PA Error {}", e),
};
Ok(())
}
}
impl <'a> Drop for PortAudioSink<'a> {
fn drop(&mut self) {
portaudio_rs::terminate().unwrap();
}
}

View file

@ -0,0 +1,118 @@
use super::{Open, Sink};
use std::io;
use libpulse_sys::*;
use std::ptr::{null, null_mut};
use std::ffi::CString;
use std::ffi::CStr;
use std::mem;
use libc;
pub struct PulseAudioSink {
s : *mut pa_simple,
ss : pa_sample_spec,
name : CString,
desc : CString
}
fn call_pulseaudio<T, F, FailCheck>(f: F, fail_check: FailCheck, kind: io::ErrorKind) -> io::Result<T> where
T: Copy,
F: Fn(*mut libc::c_int) -> T,
FailCheck: Fn(T) -> bool,
{
let mut error: libc::c_int = 0;
let ret = f(&mut error);
if fail_check(ret) {
let err_cstr = unsafe { CStr::from_ptr(pa_strerror(error)) };
let errstr = err_cstr.to_string_lossy().into_owned();
Err(io::Error::new(kind, errstr))
} else {
Ok(ret)
}
}
impl PulseAudioSink {
fn free_connection(&mut self) {
if self.s != null_mut() {
unsafe {
pa_simple_free(self.s);
}
self.s = null_mut();
}
}
}
impl Drop for PulseAudioSink {
fn drop(&mut self) {
self.free_connection();
}
}
impl Open for PulseAudioSink {
fn open(device: Option<String>) -> PulseAudioSink {
debug!("Using PulseAudio sink");
if device.is_some() {
panic!("pulseaudio sink does not support specifying a device name");
}
let ss = pa_sample_spec {
format: PA_SAMPLE_S16LE,
channels: 2, // stereo
rate: 44100
};
let name = CString::new("librespot").unwrap();
let description = CString::new("Spotify endpoint").unwrap();
PulseAudioSink {
s: null_mut(),
ss: ss,
name: name,
desc: description
}
}
}
impl Sink for PulseAudioSink {
fn start(&mut self) -> io::Result<()> {
if self.s == null_mut() {
self.s = call_pulseaudio(
|err| unsafe {
pa_simple_new(null(), // Use the default server.
self.name.as_ptr(), // Our application's name.
PA_STREAM_PLAYBACK,
null(), // Use the default device.
self.desc.as_ptr(), // desc of our stream.
&self.ss, // Our sample format.
null(), // Use default channel map
null(), // Use default buffering attributes.
err)
},
|ptr| ptr == null_mut(),
io::ErrorKind::ConnectionRefused)?;
}
Ok(())
}
fn stop(&mut self) -> io::Result<()> {
self.free_connection();
Ok(())
}
fn write(&mut self, data: &[i16]) -> io::Result<()> {
if self.s == null_mut() {
Err(io::Error::new(io::ErrorKind::NotConnected, "Not connected to pulseaudio"))
}
else {
let ptr = data.as_ptr() as *const libc::c_void;
let len = data.len() as usize * mem::size_of::<i16>();
call_pulseaudio(
|err| unsafe {
pa_simple_write(self.s, ptr, len, err)
},
|ret| ret < 0,
io::ErrorKind::BrokenPipe)?;
Ok(())
}
}
}