2016-03-20 19:16:32 +00:00
|
|
|
use super::{Open, Sink};
|
2018-02-26 01:50:41 +00:00
|
|
|
use libc;
|
2016-03-20 19:16:32 +00:00
|
|
|
use libpulse_sys::*;
|
2020-11-27 00:06:16 +00:00
|
|
|
use libpulse_simple_sys::*;
|
2017-11-27 19:01:30 +00:00
|
|
|
use std::ffi::CStr;
|
2018-02-26 01:50:41 +00:00
|
|
|
use std::ffi::CString;
|
|
|
|
use std::io;
|
2017-11-27 19:01:30 +00:00
|
|
|
use std::mem;
|
2018-02-26 01:50:41 +00:00
|
|
|
use std::ptr::{null, null_mut};
|
2016-03-20 19:16:32 +00:00
|
|
|
|
2017-01-14 21:22:33 +00:00
|
|
|
pub struct PulseAudioSink {
|
2018-02-26 01:50:41 +00:00
|
|
|
s: *mut pa_simple,
|
|
|
|
ss: pa_sample_spec,
|
|
|
|
name: CString,
|
|
|
|
desc: CString,
|
2020-04-25 11:27:21 +00:00
|
|
|
device: Option<CString>,
|
2017-01-14 21:22:33 +00:00
|
|
|
}
|
2016-03-20 19:16:32 +00:00
|
|
|
|
2019-10-08 09:31:18 +00:00
|
|
|
fn call_pulseaudio<T, F, FailCheck>(
|
|
|
|
f: F,
|
|
|
|
fail_check: FailCheck,
|
|
|
|
kind: io::ErrorKind,
|
|
|
|
) -> io::Result<T>
|
2018-02-26 01:50:41 +00:00
|
|
|
where
|
2017-11-27 19:01:30 +00:00
|
|
|
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)) };
|
2018-02-26 01:50:41 +00:00
|
|
|
let errstr = err_cstr.to_string_lossy().into_owned();
|
2017-11-27 19:01:30 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 19:16:32 +00:00
|
|
|
impl Open for PulseAudioSink {
|
2018-02-26 01:50:41 +00:00
|
|
|
fn open(device: Option<String>) -> PulseAudioSink {
|
2016-07-06 09:54:46 +00:00
|
|
|
debug!("Using PulseAudio sink");
|
|
|
|
|
2016-03-20 19:16:32 +00:00
|
|
|
let ss = pa_sample_spec {
|
|
|
|
format: PA_SAMPLE_S16LE,
|
|
|
|
channels: 2, // stereo
|
2018-02-26 01:50:41 +00:00
|
|
|
rate: 44100,
|
2016-03-20 19:16:32 +00:00
|
|
|
};
|
2018-02-26 01:50:41 +00:00
|
|
|
|
2016-03-20 19:16:32 +00:00
|
|
|
let name = CString::new("librespot").unwrap();
|
2017-11-27 19:01:30 +00:00
|
|
|
let description = CString::new("Spotify endpoint").unwrap();
|
2016-03-20 19:16:32 +00:00
|
|
|
|
2017-01-14 21:22:33 +00:00
|
|
|
PulseAudioSink {
|
|
|
|
s: null_mut(),
|
|
|
|
ss: ss,
|
|
|
|
name: name,
|
2018-02-26 01:50:41 +00:00
|
|
|
desc: description,
|
2020-04-25 11:27:21 +00:00
|
|
|
device: device.and_then(|s| CString::new(s).ok()),
|
2017-01-14 21:22:33 +00:00
|
|
|
}
|
2016-03-20 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for PulseAudioSink {
|
2016-08-01 19:20:17 +00:00
|
|
|
fn start(&mut self) -> io::Result<()> {
|
2017-01-14 21:22:33 +00:00
|
|
|
if self.s == null_mut() {
|
2020-04-25 11:27:21 +00:00
|
|
|
let device = match &self.device {
|
|
|
|
None => null(),
|
|
|
|
Some(device) => device.as_ptr(),
|
|
|
|
};
|
2017-11-27 19:01:30 +00:00
|
|
|
self.s = call_pulseaudio(
|
|
|
|
|err| unsafe {
|
2018-02-26 01:50:41 +00:00
|
|
|
pa_simple_new(
|
|
|
|
null(), // Use the default server.
|
|
|
|
self.name.as_ptr(), // Our application's name.
|
|
|
|
PA_STREAM_PLAYBACK,
|
2020-04-25 11:27:21 +00:00
|
|
|
device,
|
2018-02-26 01:50:41 +00:00
|
|
|
self.desc.as_ptr(), // desc of our stream.
|
|
|
|
&self.ss, // Our sample format.
|
|
|
|
null(), // Use default channel map
|
|
|
|
null(), // Use default buffering attributes.
|
|
|
|
err,
|
|
|
|
)
|
2017-11-27 19:01:30 +00:00
|
|
|
},
|
|
|
|
|ptr| ptr == null_mut(),
|
2018-02-26 01:50:41 +00:00
|
|
|
io::ErrorKind::ConnectionRefused,
|
|
|
|
)?;
|
2017-01-14 21:22:33 +00:00
|
|
|
}
|
2016-03-20 19:16:32 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:20:17 +00:00
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
2017-11-27 19:01:30 +00:00
|
|
|
self.free_connection();
|
2016-03-20 19:16:32 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:20:17 +00:00
|
|
|
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
2017-11-27 19:01:30 +00:00
|
|
|
if self.s == null_mut() {
|
2018-02-26 01:50:41 +00:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::NotConnected,
|
|
|
|
"Not connected to pulseaudio",
|
|
|
|
))
|
|
|
|
} else {
|
2017-11-27 19:01:30 +00:00
|
|
|
let ptr = data.as_ptr() as *const libc::c_void;
|
|
|
|
let len = data.len() as usize * mem::size_of::<i16>();
|
2018-03-20 13:05:50 +00:00
|
|
|
assert!(len > 0);
|
2017-11-27 19:01:30 +00:00
|
|
|
call_pulseaudio(
|
2018-02-26 01:50:41 +00:00
|
|
|
|err| unsafe { pa_simple_write(self.s, ptr, len, err) },
|
2017-11-27 19:01:30 +00:00
|
|
|
|ret| ret < 0,
|
2018-02-26 01:50:41 +00:00
|
|
|
io::ErrorKind::BrokenPipe,
|
|
|
|
)?;
|
2017-11-27 19:01:30 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2016-03-20 19:16:32 +00:00
|
|
|
}
|
|
|
|
}
|