Merge pull request #107 from joerg-krause/fix/close-stream-on-stop

close stream on stop
This commit is contained in:
Paul Lietar 2016-08-27 01:25:48 -07:00 committed by GitHub
commit 253dfb7c88
4 changed files with 27 additions and 24 deletions

View file

@ -5,9 +5,9 @@ pub trait Open {
} }
pub trait Sink { pub trait Sink {
fn start(&self) -> io::Result<()>; fn start(&mut self) -> io::Result<()>;
fn stop(&self) -> io::Result<()>; fn stop(&mut self) -> io::Result<()>;
fn write(&self, data: &[i16]) -> io::Result<()>; fn write(&mut self, data: &[i16]) -> io::Result<()>;
} }
/* /*

View file

@ -3,9 +3,10 @@ use std::io;
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
use portaudio; use portaudio;
use portaudio::stream::*;
use portaudio::device::{DeviceIndex, DeviceInfo, get_default_output_index}; use portaudio::device::{DeviceIndex, DeviceInfo, get_default_output_index};
pub struct PortAudioSink<'a>(portaudio::stream::Stream<'a, i16, i16>); pub struct PortAudioSink<'a>(Option<portaudio::stream::Stream<'a, i16, i16>>, StreamParameters<i16>);
fn output_devices() -> Box<Iterator<Item=(DeviceIndex, DeviceInfo)>> { fn output_devices() -> Box<Iterator<Item=(DeviceIndex, DeviceInfo)>> {
let count = portaudio::device::get_count().unwrap(); let count = portaudio::device::get_count().unwrap();
@ -39,7 +40,6 @@ fn find_output(device: &str) -> Option<DeviceIndex> {
impl <'a> Open for PortAudioSink<'a> { impl <'a> Open for PortAudioSink<'a> {
fn open(device: Option<&str>) -> PortAudioSink<'a> { fn open(device: Option<&str>) -> PortAudioSink<'a> {
use portaudio::stream::*;
debug!("Using PortAudio sink"); debug!("Using PortAudio sink");
@ -67,29 +67,32 @@ impl <'a> Open for PortAudioSink<'a> {
data: 0i16, data: 0i16,
}; };
let stream = Stream::open( PortAudioSink(None, params)
None, Some(params),
44100.0,
FRAMES_PER_BUFFER_UNSPECIFIED,
StreamFlags::empty(),
None
).unwrap();
PortAudioSink(stream)
} }
} }
impl <'a> Sink for PortAudioSink<'a> { impl <'a> Sink for PortAudioSink<'a> {
fn start(&self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
self.0.start().unwrap(); if self.0.is_none() {
self.0 = Some(Stream::open(
None, Some(self.1),
44100.0,
FRAMES_PER_BUFFER_UNSPECIFIED,
StreamFlags::empty(),
None
).unwrap());;
}
self.0.as_mut().unwrap().start().unwrap();
Ok(()) Ok(())
} }
fn stop(&self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
self.0.stop().unwrap(); self.0.as_mut().unwrap().stop().unwrap();
self.0 = None;
Ok(()) Ok(())
} }
fn write(&self, data: &[i16]) -> io::Result<()> { fn write(&mut self, data: &[i16]) -> io::Result<()> {
match self.0.write(&data) { match self.0.as_mut().unwrap().write(&data) {
Ok(_) => (), Ok(_) => (),
Err(portaudio::PaError::OutputUnderflowed) => Err(portaudio::PaError::OutputUnderflowed) =>
error!("PortAudio write underflow"), error!("PortAudio write underflow"),

View file

@ -43,15 +43,15 @@ impl Open for PulseAudioSink {
} }
impl Sink for PulseAudioSink { impl Sink for PulseAudioSink {
fn start(&self) -> io::Result<()> { fn start(&mut self) -> io::Result<()> {
Ok(()) Ok(())
} }
fn stop(&self) -> io::Result<()> { fn stop(&mut self) -> io::Result<()> {
Ok(()) Ok(())
} }
fn write(&self, data: &[i16]) -> io::Result<()> { fn write(&mut self, data: &[i16]) -> io::Result<()> {
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;

View file

@ -213,7 +213,7 @@ fn load_track(session: &Session, track_id: SpotifyId) -> Option<vorbis::Decoder<
} }
impl PlayerInternal { impl PlayerInternal {
fn run(self, sink: Box<Sink>) { fn run(self, mut sink: Box<Sink>) {
let mut decoder = None; let mut decoder = None;
loop { loop {