mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Merge pull request #802 from JasonLG1979/fix_pipe_backend
Better errors in pipe backend
This commit is contained in:
commit
a7326815bd
2 changed files with 37 additions and 11 deletions
|
@ -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] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected
|
||||||
- [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness
|
- [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness
|
||||||
- [playback] `alsa`: revert buffer size to ~500 ms
|
- [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
|
## [0.2.0] - 2021-05-04
|
||||||
|
|
||||||
|
|
|
@ -6,31 +6,57 @@ use std::fs::OpenOptions;
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
pub struct StdoutSink {
|
pub struct StdoutSink {
|
||||||
output: Box<dyn Write>,
|
output: Option<Box<dyn Write>>,
|
||||||
|
path: Option<String>,
|
||||||
format: AudioFormat,
|
format: AudioFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Open for StdoutSink {
|
impl Open for StdoutSink {
|
||||||
fn open(path: Option<String>, format: AudioFormat) -> Self {
|
fn open(path: Option<String>, format: AudioFormat) -> Self {
|
||||||
info!("Using pipe sink with format: {:?}", format);
|
info!("Using pipe sink with format: {:?}", format);
|
||||||
|
Self {
|
||||||
let output: Box<dyn Write> = match path {
|
output: None,
|
||||||
Some(path) => Box::new(OpenOptions::new().write(true).open(path).unwrap()),
|
path,
|
||||||
_ => Box::new(io::stdout()),
|
format,
|
||||||
};
|
}
|
||||||
|
|
||||||
Self { output, format }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sink for StdoutSink {
|
impl Sink for StdoutSink {
|
||||||
|
fn start(&mut self) -> io::Result<()> {
|
||||||
|
if self.output.is_none() {
|
||||||
|
let output: Box<dyn Write> = 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!();
|
sink_as_bytes!();
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SinkAsBytes for StdoutSink {
|
impl SinkAsBytes for StdoutSink {
|
||||||
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
|
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
|
||||||
self.output.write_all(data)?;
|
match self.output.as_deref_mut() {
|
||||||
self.output.flush()?;
|
Some(output) => {
|
||||||
|
output.write_all(data)?;
|
||||||
|
output.flush()?;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
return Err(io::Error::new(io::ErrorKind::Other, "Output is None"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue