2016-03-20 19:16:32 +00:00
|
|
|
use super::{Open, Sink};
|
|
|
|
use std::io;
|
|
|
|
use libpulse_sys::*;
|
|
|
|
use std::ptr::{null, null_mut};
|
|
|
|
use std::mem::{transmute};
|
|
|
|
use std::ffi::CString;
|
|
|
|
|
|
|
|
pub struct PulseAudioSink(*mut pa_simple);
|
|
|
|
|
|
|
|
impl Open for PulseAudioSink {
|
2016-07-06 09:54:46 +00:00
|
|
|
fn open(device: Option<&str>) -> PulseAudioSink {
|
|
|
|
debug!("Using PulseAudio sink");
|
|
|
|
|
|
|
|
if device.is_some() {
|
|
|
|
panic!("pulseaudio sink does not support specifying a device name");
|
|
|
|
}
|
2016-03-20 19:16:32 +00:00
|
|
|
|
|
|
|
let ss = pa_sample_spec {
|
|
|
|
format: PA_SAMPLE_S16LE,
|
|
|
|
channels: 2, // stereo
|
|
|
|
rate: 44100
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = CString::new("librespot").unwrap();
|
|
|
|
let description = CString::new("A spoty client library").unwrap();
|
|
|
|
|
|
|
|
let s = unsafe {
|
|
|
|
pa_simple_new(null(), // Use the default server.
|
|
|
|
name.as_ptr(), // Our application's name.
|
|
|
|
PA_STREAM_PLAYBACK,
|
|
|
|
null(), // Use the default device.
|
|
|
|
description.as_ptr(), // Description of our stream.
|
|
|
|
&ss, // Our sample format.
|
|
|
|
null(), // Use default channel map
|
|
|
|
null(), // Use default buffering attributes.
|
|
|
|
null_mut(), // Ignore error code.
|
|
|
|
)
|
|
|
|
};
|
|
|
|
assert!(s != null_mut());
|
|
|
|
|
|
|
|
PulseAudioSink(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for PulseAudioSink {
|
2016-08-01 19:20:17 +00:00
|
|
|
fn start(&mut self) -> io::Result<()> {
|
2016-03-20 19:16:32 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-08-01 19:20:17 +00:00
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
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<()> {
|
2016-03-20 19:16:32 +00:00
|
|
|
unsafe {
|
|
|
|
let ptr = transmute(data.as_ptr());
|
2016-03-21 09:13:28 +00:00
|
|
|
let bytes = data.len() as usize * 2;
|
2016-03-20 19:16:32 +00:00
|
|
|
pa_simple_write(self.0, ptr, bytes, null_mut());
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|