cleaner read, eof error creation

This commit is contained in:
Muaz Ahmad 2023-10-16 14:17:04 +05:00
parent 2e63c5e9aa
commit 91104fcd69

View file

@ -3,6 +3,7 @@ use std::process::{Child, Command, Stdio, ChildStdin};
use std::sync::Arc;
use std::fs::File;
use std::io::Read;
use std::io;
use crate::util;
use crate::encode::codecs::Encoder;
@ -17,20 +18,26 @@ impl Encoder for AV1Encoder {
}
fn preprocess_stream(&mut self) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut stdout = self.cmd.stdout.as_mut().unwrap();
let mut buff = [0u8; 8];
self.cmd.stdout.as_mut().unwrap().read_exact(&mut buff)?;
stdout.read_exact(&mut buff)?;
let head_len = u16::from_le_bytes(buff[6..].try_into().unwrap()) as usize;
let mut buff = vec![0u8; head_len - 8];
self.cmd.stdout.as_mut().unwrap().read_exact(buff.as_mut_slice())?;
stdout.read_exact(buff.as_mut_slice())?;
return Ok(());
}
fn read_nalu(&mut self) -> Result<util::NALUPacket, Box<dyn Error + Send + Sync>> {
let mut stdout = self.cmd.stdout.as_mut().unwrap();
let mut buff = [0u8; 11];
self.cmd.stdout.as_mut().unwrap().read_exact(&mut buff)?;
match stdout.read_exact(&mut buff) {
Ok(_) => (),
Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => return Err(Box::new(util::EncoderError::EOF)),
Err(err) => return Err(Box::new(err))
}
let nalu_len = (u32::from_le_bytes(buff[..4].try_into().unwrap()) & 0xffffff) as usize;
let mut data = vec![0u8; nalu_len];
self.cmd.stdout.as_mut().unwrap().read_exact(data.as_mut_slice())?;
stdout.read_exact(data.as_mut_slice())?;
return Ok(util::NALUPacket {packet_type: util::NALUPacketType::Video(util::VideoCodec::AV1), packet_data: data});
}
}