eof handling for demux -> decode, break channel to signal eof without returning error

This commit is contained in:
Muaz Ahmad 2023-10-11 13:02:00 +05:00
parent 82fc74f22c
commit 3b71f9babd
4 changed files with 28 additions and 2 deletions

View file

@ -13,6 +13,10 @@ pub struct AACDecoder {
} }
impl Decoder for Arc<Mutex<AACDecoder>> { impl Decoder for Arc<Mutex<AACDecoder>> {
fn close_clean(&self) {
let mut self_ptr = self.lock().unwrap();
drop(self_ptr.cmd.stdin.take().unwrap());
}
fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> { fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut self_ptr = self.lock().unwrap(); let mut self_ptr = self.lock().unwrap();
let mut pipe = match &self_ptr.cmd.stdin { let mut pipe = match &self_ptr.cmd.stdin {

View file

@ -3,16 +3,18 @@ pub mod audio;
use std::sync::mpsc; use std::sync::mpsc;
use std::error::Error; use std::error::Error;
use std::thread;
use crate::util; use crate::util;
pub trait Decoder { pub trait Decoder {
fn close_clean(&self);
fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>>; fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>>;
fn write_loop(&self, chan_in: mpsc::Receiver<util::NALUPacket>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>) { fn write_loop(&self, chan_in: mpsc::Receiver<util::NALUPacket>, err_in: mpsc::Sender<Box<dyn Error + Send + Sync>>) {
loop { loop {
let nalu = match chan_in.recv() { let nalu = match chan_in.recv() {
Ok(x) => x, Ok(x) => x,
Err(err) => {util::thread_freeze(err_in, Box::new(err)); return} Err(err) => {self.close_clean(); thread::park(); return}
}; };
match self.write_nalu(nalu) { match self.write_nalu(nalu) {
Ok(_) => (), Ok(_) => (),

View file

@ -13,6 +13,11 @@ pub struct H264Decoder {
} }
impl Decoder for Arc<Mutex<H264Decoder>> { impl Decoder for Arc<Mutex<H264Decoder>> {
fn close_clean(&self) {
let mut self_ptr = self.lock().unwrap();
drop(self_ptr.cmd.stdin.take().unwrap());
}
fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> { fn write_nalu(&self, nalu: util::NALUPacket) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut self_ptr = self.lock().unwrap(); let mut self_ptr = self.lock().unwrap();
let mut pipe = match &self_ptr.cmd.stdin { let mut pipe = match &self_ptr.cmd.stdin {

View file

@ -2,6 +2,7 @@ use std::error::Error;
use std::io; use std::io;
use std::io::Read; use std::io::Read;
use std::sync::{mpsc, Arc}; use std::sync::{mpsc, Arc};
use std::thread;
use crate::util; use crate::util;
use crate::demux::flv; use crate::demux::flv;
@ -13,7 +14,21 @@ pub trait FileReader {
loop { loop {
let nalu = match self.read_nalu(&metadata) { let nalu = match self.read_nalu(&metadata) {
Ok(x) => x, Ok(x) => x,
Err(err) => {util::thread_freeze(err_in, err); return} Err(err) => {
if let Some(e) = err.downcast_ref::<util::DemuxerError>() {
match e {
util::DemuxerError::EOF => {
drop(v_in);
drop(a_in);
thread::park();
return
},
_ => ()
}
};
util::thread_freeze(err_in, err);
return
}
}; };
match nalu.packet_type { match nalu.packet_type {
util::NALUPacketType::Audio => { util::NALUPacketType::Audio => {