From 6d83be0221ecc5be2e81e28a99ea2f26c493505d Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Thu, 12 Oct 2023 16:05:57 +0500 Subject: [PATCH] encoder outputs to file successful --- src/encode/cmds.rs | 8 ++++++-- src/encode/codecs/audio.rs | 14 +++++++++++--- src/encode/codecs/mod.rs | 37 +++++++++++++++++++++++++++++++++++++ src/encode/codecs/video.rs | 10 +++++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/encode/cmds.rs b/src/encode/cmds.rs index af5d973..56a98a9 100644 --- a/src/encode/cmds.rs +++ b/src/encode/cmds.rs @@ -1,5 +1,6 @@ use std::error::Error; use std::sync::{Arc, mpsc}; +use std::thread; use crate::util; use crate::encode::codecs::Encoder; @@ -17,6 +18,9 @@ pub fn spawn(metadata: Arc, v_target: util::VideoCodec, a_target Ok((v, a)) } -pub fn handle_encoder(encoder: impl Encoder, c_in: mpsc::Receiver, c_out: mpsc::Sender, c_err: mpsc::Sender>) { - todo!(); +pub fn handle_encoder(mut encoder: impl Encoder, c_in: mpsc::Receiver, c_out: mpsc::Sender, c_err: mpsc::Sender>) { + let stdin = encoder.stdin(); + thread::spawn(move || { + codecs::read_raw_loop(stdin, c_in, c_err); + }); } diff --git a/src/encode/codecs/audio.rs b/src/encode/codecs/audio.rs index 6b4293a..4ccf566 100644 --- a/src/encode/codecs/audio.rs +++ b/src/encode/codecs/audio.rs @@ -10,7 +10,15 @@ pub struct OpusEncoder { cmd: Child, } -impl Encoder for OpusEncoder {} +impl Encoder for OpusEncoder { + fn stdin(&mut self) -> ChildStdin { + self.cmd.stdin.take().unwrap() + } + + fn stdout(&mut self) -> ChildStdout { + self.cmd.stdout.take().unwrap() + } +} pub fn new_opus(metadata: Arc) -> Result> { let f = File::create("dump.opus")?; @@ -18,12 +26,12 @@ pub fn new_opus(metadata: Arc) -> Result ChildStdin; + + fn stdout(&mut self) -> ChildStdout; +} + +fn write_raw(cmd_in: &mut ChildStdin, media: util::RawMedia) -> Result<(), Box> { + cmd_in.write_all(media.sample.as_slice())?; + Ok(()) +} + +fn read_raw(cmd_out: &mut ChildStdout) -> Result> { + todo!(); +} + +pub fn read_raw_loop(mut cmd_in: ChildStdin, c_in: mpsc::Receiver, c_err: mpsc::Sender>) { + loop { + let media = match c_in.recv() { + Ok(x) => x, + Err(err) => {drop(cmd_in); thread::park(); return} + }; + match write_raw(&mut cmd_in, media) { + Ok(_) => (), + Err(err) => {util::thread_freeze(c_err, err); return} + } + } +} + +pub fn write_nalu_loop(mut cmd_out: ChildStdout, c_out: mpsc::Sender, c_err: mpsc::Sender>) { + todo!(); } diff --git a/src/encode/codecs/video.rs b/src/encode/codecs/video.rs index c9fcd7a..0b38ee0 100644 --- a/src/encode/codecs/video.rs +++ b/src/encode/codecs/video.rs @@ -10,7 +10,15 @@ pub struct VP9Encoder { cmd: Child, } -impl Encoder for VP9Encoder {} +impl Encoder for VP9Encoder { + fn stdin(&mut self) -> ChildStdin { + self.cmd.stdin.take().unwrap() + } + + fn stdout(&mut self) -> ChildStdout { + self.cmd.stdout.take().unwrap() + } +} pub fn new_vp9(metadata: Arc) -> Result> { let f = File::create("dump.av1")?;