librespot/playback/src/audio_backend/pipe.rs

37 lines
887 B
Rust
Raw Normal View History

use super::{Open, Sink, SinkAsBytes};
use crate::config::AudioFormat;
2021-04-13 08:29:34 +00:00
use crate::decoder::AudioPacket;
2016-12-31 12:24:18 +00:00
use std::fs::OpenOptions;
use std::io::{self, Write};
pub struct StdoutSink {
output: Box<dyn Write>,
format: AudioFormat,
}
2016-12-31 12:24:18 +00:00
impl Open for StdoutSink {
2021-04-05 21:14:02 +00:00
fn open(path: Option<String>, format: AudioFormat) -> Self {
info!("Using pipe sink with format: {:?}", format);
2016-12-31 12:24:18 +00:00
let output: Box<dyn Write> = match path {
Some(path) => Box::new(OpenOptions::new().write(true).open(path).unwrap()),
_ => Box::new(io::stdout()),
};
2016-12-31 12:24:18 +00:00
Self { output, format }
2016-12-31 12:24:18 +00:00
}
}
impl Sink for StdoutSink {
start_stop_noop!();
sink_as_bytes!();
}
2016-12-31 12:24:18 +00:00
impl SinkAsBytes for StdoutSink {
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
self.output.write_all(data)?;
self.output.flush()?;
2016-12-31 12:24:18 +00:00
Ok(())
}
}