Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop pulseaudio sink when not in use #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/audio_backend/pulseaudio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,44 +29,50 @@ 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(())
}

fn write(&mut self, data: &[i16]) -> io::Result<()> {
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(())
}
}