2021-03-12 22:05:38 +00:00
|
|
|
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};
|
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
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 {
|
2021-03-12 22:05:38 +00:00
|
|
|
info!("Using pipe sink with format: {:?}", format);
|
2016-12-31 12:24:18 +00:00
|
|
|
|
2021-03-12 22:05:38 +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
|
|
|
|
2021-04-10 08:27:24 +00:00
|
|
|
Self { output, format }
|
2016-12-31 12:24:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for StdoutSink {
|
2021-03-12 22:05:38 +00:00
|
|
|
start_stop_noop!();
|
|
|
|
sink_as_bytes!();
|
|
|
|
}
|
2016-12-31 12:24:18 +00:00
|
|
|
|
2021-03-12 22:05:38 +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(())
|
|
|
|
}
|
|
|
|
}
|