mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-06 12:00:10 +02:00
Format according to rustfmt
This commit is contained in:
parent
c3745a958a
commit
237ef1e4f9
22 changed files with 502 additions and 360 deletions
|
@ -1,11 +1,11 @@
|
|||
use super::{Open, Sink};
|
||||
use alsa::{Access, Format, Mode, Stream, PCM};
|
||||
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 {
|
||||
fn open(device: Option<String>) -> AlsaSink {
|
||||
info!("Using alsa sink");
|
||||
|
||||
let name = device.unwrap_or("default".to_string());
|
||||
|
@ -17,14 +17,22 @@ impl Open for AlsaSink {
|
|||
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) {
|
||||
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"));
|
||||
error!("Alsa error PCM open {}", e);
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Alsa error: PCM open failed",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
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};
|
||||
use jack::prelude::{client_options, AsyncClient, AudioOutPort, AudioOutSpec, Client, JackControl, Port,
|
||||
ProcessHandler, ProcessScope};
|
||||
use std::io;
|
||||
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
|
||||
|
||||
pub struct JackSink {
|
||||
send: SyncSender<i16>,
|
||||
active_client: AsyncClient<(),JackData>,
|
||||
active_client: AsyncClient<(), JackData>,
|
||||
}
|
||||
|
||||
pub struct JackData {
|
||||
|
@ -43,15 +44,26 @@ impl Open for JackSink {
|
|||
|
||||
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();
|
||||
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 (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 }
|
||||
}
|
||||
JackSink {
|
||||
send: tx,
|
||||
active_client: active_client,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sink for JackSink {
|
||||
|
|
|
@ -37,9 +37,7 @@ use self::jackaudio::JackSink;
|
|||
mod pipe;
|
||||
use self::pipe::StdoutSink;
|
||||
|
||||
pub const BACKENDS : &'static [
|
||||
(&'static str, fn(Option<String>) -> Box<Sink>)
|
||||
] = &[
|
||||
pub const BACKENDS: &'static [(&'static str, fn(Option<String>) -> Box<Sink>)] = &[
|
||||
#[cfg(feature = "alsa-backend")]
|
||||
("alsa", mk_sink::<AlsaSink>),
|
||||
#[cfg(feature = "portaudio-backend")]
|
||||
|
@ -53,8 +51,16 @@ pub const BACKENDS : &'static [
|
|||
|
||||
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)
|
||||
BACKENDS
|
||||
.iter()
|
||||
.find(|backend| name == backend.0)
|
||||
.map(|backend| backend.1)
|
||||
} else {
|
||||
Some(BACKENDS.first().expect("No backends were enabled at build time").1)
|
||||
Some(
|
||||
BACKENDS
|
||||
.first()
|
||||
.expect("No backends were enabled at build time")
|
||||
.1,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,10 @@ impl Sink for StdoutSink {
|
|||
|
||||
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>())
|
||||
slice::from_raw_parts(
|
||||
data.as_ptr() as *const u8,
|
||||
data.len() * mem::size_of::<i16>(),
|
||||
)
|
||||
};
|
||||
|
||||
self.0.write_all(data)?;
|
||||
|
@ -37,4 +40,3 @@ impl Sink for StdoutSink {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
use super::{Open, Sink};
|
||||
use portaudio_rs;
|
||||
use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo};
|
||||
use portaudio_rs::stream::*;
|
||||
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>);
|
||||
pub struct PortAudioSink<'a>(
|
||||
Option<portaudio_rs::stream::Stream<'a, i16, i16>>,
|
||||
StreamParameters<i16>,
|
||||
);
|
||||
|
||||
fn output_devices() -> Box<Iterator<Item=(DeviceIndex, DeviceInfo)>> {
|
||||
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
|
||||
});
|
||||
.filter_map(|idx| portaudio_rs::device::get_info(idx).map(|info| (idx, info)))
|
||||
.filter(|&(_, ref info)| info.max_output_channels > 0);
|
||||
|
||||
Box::new(devices)
|
||||
}
|
||||
|
@ -38,9 +38,8 @@ fn find_output(device: &str) -> Option<DeviceIndex> {
|
|||
.map(|(idx, _)| idx)
|
||||
}
|
||||
|
||||
impl <'a> Open for PortAudioSink<'a> {
|
||||
impl<'a> Open for PortAudioSink<'a> {
|
||||
fn open(device: Option<String>) -> PortAudioSink<'a> {
|
||||
|
||||
debug!("Using PortAudio sink");
|
||||
|
||||
portaudio_rs::initialize().unwrap();
|
||||
|
@ -71,16 +70,19 @@ impl <'a> Open for PortAudioSink<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <'a> Sink for PortAudioSink<'a> {
|
||||
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 = Some(
|
||||
Stream::open(
|
||||
None,
|
||||
Some(self.1),
|
||||
44100.0,
|
||||
FRAMES_PER_BUFFER_UNSPECIFIED,
|
||||
StreamFlags::empty(),
|
||||
None,
|
||||
).unwrap(),
|
||||
);;
|
||||
}
|
||||
|
||||
self.0.as_mut().unwrap().start().unwrap();
|
||||
|
@ -94,8 +96,7 @@ impl <'a> Sink for PortAudioSink<'a> {
|
|||
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(portaudio_rs::PaError::OutputUnderflowed) => error!("PortAudio write underflow"),
|
||||
Err(e) => panic!("PA Error {}", e),
|
||||
};
|
||||
|
||||
|
@ -103,7 +104,7 @@ impl <'a> Sink for PortAudioSink<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl <'a> Drop for PortAudioSink<'a> {
|
||||
impl<'a> Drop for PortAudioSink<'a> {
|
||||
fn drop(&mut self) {
|
||||
portaudio_rs::terminate().unwrap();
|
||||
}
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
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;
|
||||
use libpulse_sys::*;
|
||||
use std::ffi::CStr;
|
||||
use std::ffi::CString;
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::ptr::{null, null_mut};
|
||||
|
||||
pub struct PulseAudioSink {
|
||||
s : *mut pa_simple,
|
||||
ss : pa_sample_spec,
|
||||
name : CString,
|
||||
desc : CString
|
||||
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
|
||||
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,
|
||||
|
@ -23,7 +24,7 @@ fn call_pulseaudio<T, F, FailCheck>(f: F, fail_check: FailCheck, kind: io::Error
|
|||
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();
|
||||
let errstr = err_cstr.to_string_lossy().into_owned();
|
||||
Err(io::Error::new(kind, errstr))
|
||||
} else {
|
||||
Ok(ret)
|
||||
|
@ -48,7 +49,7 @@ impl Drop for PulseAudioSink {
|
|||
}
|
||||
|
||||
impl Open for PulseAudioSink {
|
||||
fn open(device: Option<String>) -> PulseAudioSink {
|
||||
fn open(device: Option<String>) -> PulseAudioSink {
|
||||
debug!("Using PulseAudio sink");
|
||||
|
||||
if device.is_some() {
|
||||
|
@ -58,9 +59,9 @@ impl Open for PulseAudioSink {
|
|||
let ss = pa_sample_spec {
|
||||
format: PA_SAMPLE_S16LE,
|
||||
channels: 2, // stereo
|
||||
rate: 44100
|
||||
rate: 44100,
|
||||
};
|
||||
|
||||
|
||||
let name = CString::new("librespot").unwrap();
|
||||
let description = CString::new("Spotify endpoint").unwrap();
|
||||
|
||||
|
@ -68,7 +69,7 @@ impl Open for PulseAudioSink {
|
|||
s: null_mut(),
|
||||
ss: ss,
|
||||
name: name,
|
||||
desc: description
|
||||
desc: description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,18 +79,21 @@ impl Sink for PulseAudioSink {
|
|||
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)
|
||||
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)?;
|
||||
io::ErrorKind::ConnectionRefused,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -101,17 +105,18 @@ impl Sink for PulseAudioSink {
|
|||
|
||||
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 {
|
||||
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)
|
||||
},
|
||||
|err| unsafe { pa_simple_write(self.s, ptr, len, err) },
|
||||
|ret| ret < 0,
|
||||
io::ErrorKind::BrokenPipe)?;
|
||||
io::ErrorKind::BrokenPipe,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue