From 0bece0d867094d50e1252be7a4842085df6aaa10 Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Fri, 18 Jun 2021 00:32:04 -0500 Subject: [PATCH 1/3] Fix pipe backend * Move all code that can fail to `start` where errors can be returned to prevent a panic! * Replace unwrap --- playback/src/audio_backend/pipe.rs | 46 +++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/playback/src/audio_backend/pipe.rs b/playback/src/audio_backend/pipe.rs index 926219f..65e905c 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(()) } } From 336e714dba4bb45f1e0d26fb0966efed8edca968 Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Fri, 18 Jun 2021 15:30:22 -0500 Subject: [PATCH 2/3] Fix clippy warning --- playback/src/audio_backend/pipe.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playback/src/audio_backend/pipe.rs b/playback/src/audio_backend/pipe.rs index 65e905c..5604038 100644 --- a/playback/src/audio_backend/pipe.rs +++ b/playback/src/audio_backend/pipe.rs @@ -16,7 +16,7 @@ impl Open for StdoutSink { info!("Using pipe sink with format: {:?}", format); Self { output: None, - path: path, + path, format, } } From 586e864427162a056c58e49e114a8b78e3bc216d Mon Sep 17 00:00:00 2001 From: JasonLG1979 Date: Fri, 18 Jun 2021 17:33:49 -0500 Subject: [PATCH 3/3] Update change log --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ca30a..ceb6354 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected - [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness - [playback] `alsa`: revert buffer size to ~500 ms -- [playback] `alsa`: better error handling +- [playback] `alsa`, `pipe`: better error handling ## [0.2.0] - 2021-05-04