mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Stop pulseaudio sink when not in use
This commit is contained in:
parent
9e495d6fd7
commit
4e02ef5a6c
1 changed files with 32 additions and 21 deletions
|
@ -5,7 +5,12 @@ use std::ptr::{null, null_mut};
|
|||
use std::mem::{transmute};
|
||||
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 {
|
||||
fn open(device: Option<&str>) -> PulseAudioSink {
|
||||
|
@ -24,30 +29,40 @@ impl Open for PulseAudioSink {
|
|||
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)
|
||||
PulseAudioSink {
|
||||
s: null_mut(),
|
||||
ss: ss,
|
||||
name: name,
|
||||
desc: description
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sink for PulseAudioSink {
|
||||
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(())
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> io::Result<()> {
|
||||
unsafe {
|
||||
pa_simple_free(self.s);
|
||||
}
|
||||
self.s = null_mut();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -55,13 +70,9 @@ impl Sink for PulseAudioSink {
|
|||
unsafe {
|
||||
let ptr = transmute(data.as_ptr());
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue