output to pipe instead to link with channels through trait

This commit is contained in:
Muaz Ahmad 2023-10-11 14:33:25 +05:00
parent 3b71f9babd
commit 78e0e5c7cc
3 changed files with 27 additions and 11 deletions

View file

@ -2,14 +2,13 @@ use std::error::Error;
use std::process::{Child, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::fs::File;
use crate::decode::codecs::Decoder; use crate::decode::codecs::Decoder;
use crate::util; use crate::util;
pub struct AACDecoder { pub struct AACDecoder {
cmd: Child, cmd: Child,
metadata: Arc<util::Metadata>, bytes_per_sample_chunk: usize
} }
impl Decoder for Arc<Mutex<AACDecoder>> { impl Decoder for Arc<Mutex<AACDecoder>> {
@ -27,14 +26,21 @@ impl Decoder for Arc<Mutex<AACDecoder>> {
Ok(()) Ok(())
} }
fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> { fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> {
todo!(); let mut self_ptr = self.lock().unwrap();
let mut pcm_buff = vec![0u8; self_ptr.bytes_per_sample_chunk];
let mut pipe = match &mut self_ptr.cmd.stdout {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::BrokenOutPipe))}
};
pipe.read_exact(pcm_buff.as_mut_slice())?;
return Ok(util::RawMedia {media_type: util::RawMediaType::PCM16BE, sample: pcm_buff});
} }
} }
pub fn new_aac(metadata: Arc<util::Metadata>) -> Result<AACDecoder, Box<dyn Error>> { pub fn new_aac(metadata: Arc<util::Metadata>) -> Result<AACDecoder, Box<dyn Error>> {
let f = File::create("out.pcm")?;
let cmd = Command::new("faad") let cmd = Command::new("faad")
.args(["-f", "2", "-w", "-q", "-"]) .args(["-f", "2", "-w", "-q", "-"])
.stdin(Stdio::piped()).stdout(f).spawn()?; .stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
return Ok(AACDecoder {cmd: cmd, metadata: metadata}) let bytes_per_sample_chunk = 1024 * 2 * metadata.audio.channels as usize;
return Ok(AACDecoder {cmd: cmd, bytes_per_sample_chunk: bytes_per_sample_chunk})
} }

View file

@ -9,7 +9,7 @@ use crate::util;
pub struct H264Decoder { pub struct H264Decoder {
cmd: Child, cmd: Child,
metadata: Arc<util::Metadata>, bytes_per_sample: usize
} }
impl Decoder for Arc<Mutex<H264Decoder>> { impl Decoder for Arc<Mutex<H264Decoder>> {
@ -28,7 +28,14 @@ impl Decoder for Arc<Mutex<H264Decoder>> {
Ok(()) Ok(())
} }
fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> { fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> {
todo!(); let mut self_ptr = self.lock().unwrap();
let mut yuv_buff = vec![0u8; self_ptr.bytes_per_sample];
let mut pipe = match &mut self_ptr.cmd.stdout {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::BrokenOutPipe))}
};
pipe.read_exact(yuv_buff.as_mut_slice());
return Ok(util::RawMedia {media_type: util::RawMediaType::YUV420P, sample: yuv_buff});
} }
} }
@ -37,5 +44,6 @@ pub fn new_h264(metadata: Arc<util::Metadata>) -> Result<H264Decoder, Box<dyn Er
let cmd = Command::new("ffmpeg") let cmd = Command::new("ffmpeg")
.args(["-y", "-hide_banner", "-loglevel", "error" , "-i", "-", "-c:v", "rawvideo", "-pix_fmt", "yuv420p", "-f", "rawvideo", "-"]) .args(["-y", "-hide_banner", "-loglevel", "error" , "-i", "-", "-c:v", "rawvideo", "-pix_fmt", "yuv420p", "-f", "rawvideo", "-"])
.stdin(Stdio::piped()).stdout(f).spawn()?; .stdin(Stdio::piped()).stdout(f).spawn()?;
return Ok(H264Decoder {cmd: cmd, metadata: metadata}); let bytes_per_sample = metadata.video.width * metadata.video.height * 3 / 2;
return Ok(H264Decoder {cmd: cmd, bytes_per_sample: bytes_per_sample as usize});
} }

View file

@ -61,7 +61,7 @@ pub struct NALUPacket {
pub enum RawMediaType { pub enum RawMediaType {
YUV420P, YUV420P,
PCM16LE, PCM16BE,
} }
pub struct RawMedia { pub struct RawMedia {
@ -131,6 +131,7 @@ impl fmt::Display for DemuxerError {
pub enum DecoderError { pub enum DecoderError {
CodecNotImplemented, CodecNotImplemented,
BrokenInPipe, BrokenInPipe,
BrokenOutPipe,
} }
impl Error for DecoderError {} impl Error for DecoderError {}
@ -140,7 +141,8 @@ impl fmt::Debug for DecoderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
DecoderError::CodecNotImplemented => write!(f, "Codec not recognized"), DecoderError::CodecNotImplemented => write!(f, "Codec not recognized"),
DecoderError::BrokenInPipe => write!(f, "Decoder has invalid piping"), DecoderError::BrokenInPipe => write!(f, "Decoder has invalid piping, stdin closed"),
DecoderError::BrokenInPipe => write!(f, "Decoder has invalid piping, stdout closed"),
_ => write!(f, "Error not described yet") _ => write!(f, "Error not described yet")
} }
} }