2021-03-12 22:05:38 +00:00
|
|
|
use super::{Open, Sink, SinkAsBytes};
|
2021-01-07 06:42:38 +00:00
|
|
|
use crate::audio::AudioPacket;
|
2021-03-12 22:05:38 +00:00
|
|
|
use crate::config::AudioFormat;
|
2020-01-24 00:35:24 +00:00
|
|
|
use shell_words::split;
|
2021-03-01 02:37:22 +00:00
|
|
|
|
2020-01-24 00:35:24 +00:00
|
|
|
use std::io::{self, Write};
|
|
|
|
use std::process::{Child, Command, Stdio};
|
|
|
|
|
|
|
|
pub struct SubprocessSink {
|
|
|
|
shell_command: String,
|
|
|
|
child: Option<Child>,
|
2021-03-12 22:05:38 +00:00
|
|
|
format: AudioFormat,
|
2020-01-24 00:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Open for SubprocessSink {
|
2021-04-05 21:14:02 +00:00
|
|
|
fn open(shell_command: Option<String>, format: AudioFormat) -> Self {
|
2021-03-12 22:05:38 +00:00
|
|
|
info!("Using subprocess sink with format: {:?}", format);
|
|
|
|
|
2020-01-24 00:35:24 +00:00
|
|
|
if let Some(shell_command) = shell_command {
|
|
|
|
SubprocessSink {
|
2021-03-01 02:37:22 +00:00
|
|
|
shell_command,
|
2020-01-24 00:35:24 +00:00
|
|
|
child: None,
|
2021-04-10 08:27:24 +00:00
|
|
|
format,
|
2020-01-24 00:35:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic!("subprocess sink requires specifying a shell command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for SubprocessSink {
|
|
|
|
fn start(&mut self) -> io::Result<()> {
|
|
|
|
let args = split(&self.shell_command).unwrap();
|
|
|
|
self.child = Some(
|
|
|
|
Command::new(&args[0])
|
|
|
|
.args(&args[1..])
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.spawn()?,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
|
|
|
if let Some(child) = &mut self.child.take() {
|
|
|
|
child.kill()?;
|
|
|
|
child.wait()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-12 22:05:38 +00:00
|
|
|
sink_as_bytes!();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SinkAsBytes for SubprocessSink {
|
|
|
|
fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
|
2020-01-24 00:35:24 +00:00
|
|
|
if let Some(child) = &mut self.child {
|
|
|
|
let child_stdin = child.stdin.as_mut().unwrap();
|
|
|
|
child_stdin.write_all(data)?;
|
2021-03-12 22:05:38 +00:00
|
|
|
child_stdin.flush()?;
|
2020-01-24 00:35:24 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|