librespot/src/audio_backend/pulseaudio.rs

79 lines
2.2 KiB
Rust
Raw Normal View History

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;
2017-01-14 21:22:33 +00:00
pub struct PulseAudioSink {
s : *mut pa_simple,
ss : pa_sample_spec,
name : CString,
desc : CString
}
2016-03-20 19:16:32 +00:00
impl Open for PulseAudioSink {
2017-01-18 18:41:22 +00:00
fn open(device: Option<String>) -> PulseAudioSink {
2016-07-06 09:54:46 +00:00
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();
2017-01-14 21:22:33 +00:00
PulseAudioSink {
s: null_mut(),
ss: ss,
name: name,
desc: description
}
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() {
self.s = 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.
null_mut(), // Ignore error code.
)
};
assert!(self.s != null_mut());
}
2016-03-20 19:16:32 +00:00
Ok(())
}
2016-08-01 19:20:17 +00:00
fn stop(&mut self) -> io::Result<()> {
2017-01-14 21:22:33 +00:00
unsafe {
pa_simple_free(self.s);
}
self.s = null_mut();
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;
2017-01-14 21:22:33 +00:00
pa_simple_write(self.s, ptr, bytes, null_mut());
2016-03-20 19:16:32 +00:00
};
Ok(())
}
}