impl with arc mutex, use random file as bytestream output

This commit is contained in:
Muaz Ahmad 2023-10-10 14:39:29 +05:00
parent 44b647615e
commit cd94b24b7b
2 changed files with 32 additions and 36 deletions

View file

@ -1,36 +1,34 @@
use std::error::Error; use std::error::Error;
use std::process::{ChildStdin, ChildStdout, Command, Stdio}; use std::process::{Child, Command, Stdio};
use std::sync::Arc; 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 {
stdin: ChildStdin, cmd: Child,
stdout: ChildStdout,
metadata: Arc<util::Metadata>, metadata: Arc<util::Metadata>,
} }
impl Decoder for AACDecoder { impl Decoder for Arc<Mutex<AACDecoder>> {
fn write_nalu(&mut self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> { fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> {
self.stdin.write_all(nalu.packet_data.as_slice())?; let mut self_ptr = self.lock().unwrap();
let mut pipe = match &self_ptr.cmd.stdin {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::BrokenInPipe))}
};
pipe.write_all(nalu.packet_data.as_slice())?;
Ok(()) Ok(())
} }
fn read_raw(&mut self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> { fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> {
todo!(); todo!();
} }
} }
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 cmd = Command::new("tee").stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; let f = File::create("raw.aac")?;
let stdin = match cmd.stdin { let cmd = Command::new("tee").stdin(Stdio::piped()).stdout(f).spawn()?;
Some(x) => x, return Ok(AACDecoder {cmd: cmd, metadata: metadata})
None => {return Err(Box::new(util::DecoderError::InvalidPipeInit))}
};
let stdout = match cmd.stdout {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::InvalidPipeInit))}
};
return Ok(AACDecoder {stdin: stdin, stdout: stdout, metadata: metadata})
} }

View file

@ -1,36 +1,34 @@
use std::error::Error; use std::error::Error;
use std::process::{ChildStdin, ChildStdout, Stdio, Command}; use std::process::{Child, Stdio, Command};
use std::sync::Arc; 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 H264Decoder { pub struct H264Decoder {
stdin: ChildStdin, cmd: Child,
stdout: ChildStdout,
metadata: Arc<util::Metadata>, metadata: Arc<util::Metadata>,
} }
impl Decoder for H264Decoder { impl Decoder for Arc<Mutex<H264Decoder>> {
fn write_nalu(&mut self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> { fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> {
self.stdin.write_all(nalu.packet_data.as_slice())?; let mut self_ptr = self.lock().unwrap();
let mut pipe = match &self_ptr.cmd.stdin {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::BrokenInPipe))}
};
pipe.write_all(nalu.packet_data.as_slice())?;
Ok(()) Ok(())
} }
fn read_raw(&mut self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> { fn read_raw(&self) -> Result<util::RawMedia, Box<dyn Error + Send + Sync>> {
todo!(); todo!();
} }
} }
pub fn new_h264(metadata: Arc<util::Metadata>) -> Result<H264Decoder, Box<dyn Error>> { pub fn new_h264(metadata: Arc<util::Metadata>) -> Result<H264Decoder, Box<dyn Error>> {
let cmd = Command::new("tee").stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; let f = File::create("raw.h264")?;
let stdin = match cmd.stdin { let cmd = Command::new("tee").stdin(Stdio::piped()).stdout(f).spawn()?;
Some(x) => x, return Ok(H264Decoder {cmd: cmd, metadata: metadata});
None => {return Err(Box::new(util::DecoderError::InvalidPipeInit))}
};
let stdout = match cmd.stdout {
Some(x) => x,
None => {return Err(Box::new(util::DecoderError::InvalidPipeInit))}
};
return Ok(H264Decoder {stdin: stdin, stdout: stdout, metadata: metadata});
} }