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

View file

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