more boilerplate for encoder spawning

This commit is contained in:
Muaz Ahmad 2023-10-12 14:45:24 +05:00
parent 89d9100c7e
commit 1023bfdee5
7 changed files with 80 additions and 9 deletions

View file

@ -3,14 +3,14 @@ use crate::util;
pub fn prepend_v(data: Vec<u8>, metadata: &util::Metadata) -> Vec<u8> {
match metadata.video.codec {
Some(util::VideoCodec::H264) => preprocess_h264(data),
None => panic!("not possible for codec to be None by this point")
_ => panic!("not possible for codec to be anything else by this point")
}
}
pub fn prepend_a(data: Vec<u8>, metadata: &util::Metadata) -> Vec<u8> {
match metadata.audio.codec {
Some(util::AudioCodec::AAC) => preprocess_aac(data, metadata),
None => panic!("not possible for codec to be None by this point")
_ => panic!("not possible for codec to be anything else by this point")
}
}

View file

@ -3,11 +3,20 @@ use std::sync::{Arc, mpsc};
use crate::util;
use crate::encode::codecs::Encoder;
use crate::encode::codecs;
pub fn spawn(v_target: util::VideoCodec, a_target: util::AudioCodec) -> Result<(impl Encoder, impl Encoder), Box<dyn Error>> {
todo!();
pub fn spawn(metadata: Arc<util::Metadata>, v_target: util::VideoCodec, a_target: util::AudioCodec) -> Result<(impl Encoder, impl Encoder), Box<dyn Error>> {
let v = match v_target {
util::VideoCodec::VP9 => codecs::video::new_vp9(metadata.clone())?,
_ => return Err(Box::new(util::EncoderError::CodecNotSupported))
};
let a = match a_target {
util::AudioCodec::OPUS => codecs::audio::new_opus(metadata)?,
_ => return Err(Box::new(util::EncoderError::CodecNotSupported))
};
Ok((v, a))
}
pub fn handle_encoder(encoder: impl Encoder, c_in: mpsc::Receiver<util::RawMedia>, c_out: mpsc::Sender<util::NALUPacket>, metadata: Arc<util::Metadata>, c_err: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
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>>) {
todo!();
}

View file

@ -0,0 +1,17 @@
use std::error::Error;
use std::process::{Child, Command, Stdio, ChildStdin, ChildStdout};
use std::sync::Arc;
use crate::util;
use crate::encode::codecs::Encoder;
pub struct OpusEncoder {
cmd: Child,
}
impl Encoder for OpusEncoder {}
pub fn new_opus(metadata: Arc<util::Metadata>) -> Result<OpusEncoder, Box<dyn Error>> {
todo!();
}

View file

@ -1,2 +1,5 @@
pub mod video;
pub mod audio;
pub trait Encoder {
}

View file

@ -0,0 +1,16 @@
use std::error::Error;
use std::process::{Child, Command, Stdio, ChildStdin, ChildStdout};
use std::sync::Arc;
use crate::util;
use crate::encode::codecs::Encoder;
pub struct VP9Encoder {
cmd: Child,
}
impl Encoder for VP9Encoder {}
pub fn new_vp9(metadata: Arc<util::Metadata>) -> Result<VP9Encoder, Box<dyn Error>> {
todo!();
}

View file

@ -25,13 +25,12 @@ pub fn spawn(
let (a_in, a_out) = mpsc::channel();
let (err_in_v, err_out) = mpsc::channel();
let err_in_a = err_in_v.clone();
let (v_encoder, a_encoder) = cmds::spawn(v_target, a_target)?;
let metadata_cp = metadata.clone();
let (v_encoder, a_encoder) = cmds::spawn(metadata, v_target, a_target)?;
thread::spawn(move || {
cmds::handle_encoder(v_encoder, v, v_in, metadata_cp, err_in_v);
cmds::handle_encoder(v_encoder, v, v_in, err_in_v);
});
thread::spawn(move || {
cmds::handle_encoder(a_encoder, a, a_in, metadata, err_in_a);
cmds::handle_encoder(a_encoder, a, a_in, err_in_a);
});
return Ok((v_out, a_out, err_out));
}

View file

@ -159,6 +159,33 @@ impl fmt::Display for DecoderError {
}
}
pub enum EncoderError {
CodecNotSupported,
BrokenInPipe,
BrokenOutPipe,
EOF,
}
impl Error for EncoderError {}
#[allow(unreachable_patterns)]
impl fmt::Debug for EncoderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
EncoderError::CodecNotSupported => write!(f, "Codec not recognized or not allowed for encoding"),
EncoderError::BrokenInPipe => write!(f, "Encoder has invalid piping, stdin closed"),
EncoderError::BrokenInPipe => write!(f, "Encoder has invalid piping, stdout closed"),
EncoderError::EOF => write!(f, "Encoder output closed, expected EOF"),
_ => write!(f, "Error not described yet")
}
}
}
impl fmt::Display for EncoderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
// funcs
pub fn thread_freeze(err_sender: mpsc::Sender<Box<dyn Error + Send + Sync>>, err: Box<dyn Error + Send + Sync>) {