Skip to content

Commit a732681

Browse files
authored
Merge pull request #802 from JasonLG1979/fix_pipe_backend
Better errors in pipe backend
2 parents 40e5679 + 586e864 commit a732681

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4343
- [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected
4444
- [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness
4545
- [playback] `alsa`: revert buffer size to ~500 ms
46-
- [playback] `alsa`: better error handling
46+
- [playback] `alsa`, `pipe`: better error handling
4747

4848
## [0.2.0] - 2021-05-04
4949

playback/src/audio_backend/pipe.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,57 @@ use std::fs::OpenOptions;
66
use std::io::{self, Write};
77

88
pub struct StdoutSink {
9-
output: Box<dyn Write>,
9+
output: Option<Box<dyn Write>>,
10+
path: Option<String>,
1011
format: AudioFormat,
1112
}
1213

1314
impl Open for StdoutSink {
1415
fn open(path: Option<String>, format: AudioFormat) -> Self {
1516
info!("Using pipe sink with format: {:?}", format);
16-
17-
let output: Box<dyn Write> = match path {
18-
Some(path) => Box::new(OpenOptions::new().write(true).open(path).unwrap()),
19-
_ => Box::new(io::stdout()),
20-
};
21-
22-
Self { output, format }
17+
Self {
18+
output: None,
19+
path,
20+
format,
21+
}
2322
}
2423
}
2524

2625
impl Sink for StdoutSink {
26+
fn start(&mut self) -> io::Result<()> {
27+
if self.output.is_none() {
28+
let output: Box<dyn Write> = match self.path.as_deref() {
29+
Some(path) => {
30+
let open_op = OpenOptions::new()
31+
.write(true)
32+
.open(path)
33+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
34+
Box::new(open_op)
35+
}
36+
None => Box::new(io::stdout()),
37+
};
38+
39+
self.output = Some(output);
40+
}
41+
42+
Ok(())
43+
}
44+
2745
sink_as_bytes!();
2846
}
2947

3048
impl SinkAsBytes for StdoutSink {
3149
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
32-
self.output.write_all(data)?;
33-
self.output.flush()?;
50+
match self.output.as_deref_mut() {
51+
Some(output) => {
52+
output.write_all(data)?;
53+
output.flush()?;
54+
}
55+
None => {
56+
return Err(io::Error::new(io::ErrorKind::Other, "Output is None"));
57+
}
58+
}
59+
3460
Ok(())
3561
}
3662
}

0 commit comments

Comments
 (0)