diff --git a/src/decode/codecs/audio.rs b/src/decode/codecs/audio.rs index 375f1a3..c7d8471 100644 --- a/src/decode/codecs/audio.rs +++ b/src/decode/codecs/audio.rs @@ -2,14 +2,13 @@ use std::error::Error; use std::process::{Child, Command, Stdio}; use std::sync::{Arc, Mutex}; use std::io::{Read, Write}; -use std::fs::File; use crate::decode::codecs::Decoder; use crate::util; pub struct AACDecoder { cmd: Child, - metadata: Arc, + bytes_per_sample_chunk: usize } impl Decoder for Arc> { @@ -27,14 +26,21 @@ impl Decoder for Arc> { Ok(()) } fn read_raw(&self) -> Result> { - 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) -> Result> { - let f = File::create("out.pcm")?; let cmd = Command::new("faad") .args(["-f", "2", "-w", "-q", "-"]) - .stdin(Stdio::piped()).stdout(f).spawn()?; - return Ok(AACDecoder {cmd: cmd, metadata: metadata}) + .stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; + 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}) } diff --git a/src/decode/codecs/video.rs b/src/decode/codecs/video.rs index 70ea6d3..5720cc5 100644 --- a/src/decode/codecs/video.rs +++ b/src/decode/codecs/video.rs @@ -9,7 +9,7 @@ use crate::util; pub struct H264Decoder { cmd: Child, - metadata: Arc, + bytes_per_sample: usize } impl Decoder for Arc> { @@ -28,7 +28,14 @@ impl Decoder for Arc> { Ok(()) } fn read_raw(&self) -> Result> { - 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) -> Result fmt::Result { match self { 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") } }