diff --git a/playback/src/audio_backend/pipe.rs b/playback/src/audio_backend/pipe.rs index 926219f9..65e905c3 100644 --- a/playback/src/audio_backend/pipe.rs +++ b/playback/src/audio_backend/pipe.rs @@ -6,31 +6,57 @@ use std::fs::OpenOptions; use std::io::{self, Write}; pub struct StdoutSink { - output: Box, + output: Option>, + path: Option, format: AudioFormat, } impl Open for StdoutSink { fn open(path: Option, format: AudioFormat) -> Self { info!("Using pipe sink with format: {:?}", format); - - let output: Box = match path { - Some(path) => Box::new(OpenOptions::new().write(true).open(path).unwrap()), - _ => Box::new(io::stdout()), - }; - - Self { output, format } + Self { + output: None, + path: path, + format, + } } } impl Sink for StdoutSink { + fn start(&mut self) -> io::Result<()> { + if self.output.is_none() { + let output: Box = match self.path.as_deref() { + Some(path) => { + let open_op = OpenOptions::new() + .write(true) + .open(path) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + Box::new(open_op) + } + None => Box::new(io::stdout()), + }; + + self.output = Some(output); + } + + Ok(()) + } + sink_as_bytes!(); } impl SinkAsBytes for StdoutSink { fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { - self.output.write_all(data)?; - self.output.flush()?; + match self.output.as_deref_mut() { + Some(output) => { + output.write_all(data)?; + output.flush()?; + } + None => { + return Err(io::Error::new(io::ErrorKind::Other, "Output is None")); + } + } + Ok(()) } }