encoder outputs to file successful

This commit is contained in:
Muaz Ahmad 2023-10-12 16:05:57 +05:00
parent 763683d9b3
commit 6d83be0221
4 changed files with 63 additions and 6 deletions

View file

@ -1,5 +1,6 @@
use std::error::Error; use std::error::Error;
use std::sync::{Arc, mpsc}; use std::sync::{Arc, mpsc};
use std::thread;
use crate::util; use crate::util;
use crate::encode::codecs::Encoder; use crate::encode::codecs::Encoder;
@ -17,6 +18,9 @@ pub fn spawn(metadata: Arc<util::Metadata>, v_target: util::VideoCodec, a_target
Ok((v, a)) Ok((v, a))
} }
pub fn handle_encoder(encoder: impl Encoder, c_in: mpsc::Receiver<util::RawMedia>, c_out: mpsc::Sender<util::NALUPacket>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) { pub fn handle_encoder(mut encoder: impl Encoder, c_in: mpsc::Receiver<util::RawMedia>, c_out: mpsc::Sender<util::NALUPacket>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
todo!(); let stdin = encoder.stdin();
thread::spawn(move || {
codecs::read_raw_loop(stdin, c_in, c_err);
});
} }

View file

@ -10,7 +10,15 @@ pub struct OpusEncoder {
cmd: Child, 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<util::Metadata>) -> Result<OpusEncoder, Box<dyn Error>> { pub fn new_opus(metadata: Arc<util::Metadata>) -> Result<OpusEncoder, Box<dyn Error>> {
let f = File::create("dump.opus")?; let f = File::create("dump.opus")?;
@ -18,12 +26,12 @@ pub fn new_opus(metadata: Arc<util::Metadata>) -> Result<OpusEncoder, Box<dyn Er
let samplerate = metadata.audio.samplerate.to_string(); let samplerate = metadata.audio.samplerate.to_string();
let cmd = Command::new("opusenc") let cmd = Command::new("opusenc")
.args([ .args([
"--quiet",
"--raw", "--raw",
"--raw-bits", "16", "--raw-bits", "16",
"--raw-rate", samplerate.as_str(), "--raw-rate", samplerate.as_str(),
"--raw-chan", channels.as_str(), "--raw-chan", channels.as_str(),
"--raw-endianness", "1" "--raw-endianness", "1",
"-", "-"
]).stdin(Stdio::piped()).stdout(f).spawn()?; ]).stdin(Stdio::piped()).stdout(f).spawn()?;
return Ok(OpusEncoder {cmd: cmd}); return Ok(OpusEncoder {cmd: cmd});
} }

View file

@ -1,5 +1,42 @@
pub mod video; pub mod video;
pub mod audio; pub mod audio;
use std::error::Error;
use std::process::{ChildStdin, ChildStdout};
use std::sync::mpsc;
use std::io::{Read, Write};
use std::thread;
use crate::util;
pub trait Encoder { pub trait Encoder {
fn stdin(&mut self) -> ChildStdin;
fn stdout(&mut self) -> ChildStdout;
}
fn write_raw(cmd_in: &mut ChildStdin, media: util::RawMedia) -> Result<(), Box<dyn Error + Send + Sync>> {
cmd_in.write_all(media.sample.as_slice())?;
Ok(())
}
fn read_raw(cmd_out: &mut ChildStdout) -> Result<util::NALUPacket, Box<dyn Error>> {
todo!();
}
pub fn read_raw_loop(mut cmd_in: ChildStdin, c_in: mpsc::Receiver<util::RawMedia>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
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<util::NALUPacket>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
todo!();
} }

View file

@ -10,7 +10,15 @@ pub struct VP9Encoder {
cmd: Child, 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<util::Metadata>) -> Result<VP9Encoder, Box<dyn Error>> { pub fn new_vp9(metadata: Arc<util::Metadata>) -> Result<VP9Encoder, Box<dyn Error>> {
let f = File::create("dump.av1")?; let f = File::create("dump.av1")?;