Stop pulseaudio sink when not in use

This commit is contained in:
Mitch Bigelow 2017-01-14 16:22:33 -05:00
parent 9e495d6fd7
commit 4e02ef5a6c

View file

@ -5,7 +5,12 @@ use std::ptr::{null, null_mut};
use std::mem::{transmute}; use std::mem::{transmute};
use std::ffi::CString; use std::ffi::CString;
pub struct PulseAudioSink(*mut pa_simple); pub struct PulseAudioSink {
s : *mut pa_simple,
ss : pa_sample_spec,
name : CString,
desc : CString
}
impl Open for PulseAudioSink { impl Open for PulseAudioSink {
fn open(device: Option<&str>) -> PulseAudioSink { fn open(device: Option<&str>) -> PulseAudioSink {
@ -24,30 +29,40 @@ impl Open for PulseAudioSink {
let name = CString::new("librespot").unwrap(); let name = CString::new("librespot").unwrap();
let description = CString::new("A spoty client library").unwrap(); let description = CString::new("A spoty client library").unwrap();
let s = unsafe { PulseAudioSink {
pa_simple_new(null(), // Use the default server. s: null_mut(),
name.as_ptr(), // Our application's name. ss: ss,
PA_STREAM_PLAYBACK, name: name,
null(), // Use the default device. desc: description
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 { impl Sink for PulseAudioSink {
fn start(&mut self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
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());
}
Ok(()) Ok(())
} }
fn stop(&mut self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
unsafe {
pa_simple_free(self.s);
}
self.s = null_mut();
Ok(()) Ok(())
} }
@ -55,13 +70,9 @@ impl Sink for PulseAudioSink {
unsafe { unsafe {
let ptr = transmute(data.as_ptr()); let ptr = transmute(data.as_ptr());
let bytes = data.len() as usize * 2; let bytes = data.len() as usize * 2;
pa_simple_write(self.0, ptr, bytes, null_mut()); pa_simple_write(self.s, ptr, bytes, null_mut());
}; };
Ok(()) Ok(())
} }
} }