From: Kostya Shishkov Date: Fri, 19 May 2017 12:50:54 +0000 (+0200) Subject: make an interface for working with demuxers X-Git-Url: https://git.nihav.org/?a=commitdiff_plain;h=eb71d98ffafe7cc00bab4c3b7c9c97f813eca6c4;p=nihav.git make an interface for working with demuxers --- diff --git a/src/codecs/indeo2.rs b/src/codecs/indeo2.rs index 6d02a65..1df483f 100644 --- a/src/codecs/indeo2.rs +++ b/src/codecs/indeo2.rs @@ -360,17 +360,18 @@ impl NADecoder for Indeo2Decoder { #[cfg(test)] mod test { use super::*; - use demuxers::avi::*; + use demuxers::*; use io::byteio::*; use std::fs::File; use std::io::prelude::*; #[test] fn test_indeo2() { + let avi_dmx = find_demuxer("avi").unwrap(); let mut file = File::open("assets/laser05.avi").unwrap(); let mut fr = FileReader::new_read(&mut file); let mut br = ByteReader::new(&mut fr); - let mut dmx = AVIDemuxer::new(&mut br); + let mut dmx = avi_dmx.new_demuxer(&mut br); dmx.open().unwrap(); let mut dec = Indeo2Decoder::new(); diff --git a/src/demuxers/avi.rs b/src/demuxers/avi.rs index 034ef70..8737fbf 100644 --- a/src/demuxers/avi.rs +++ b/src/demuxers/avi.rs @@ -38,7 +38,7 @@ impl StreamState { } #[allow(dead_code)] -pub struct AVIDemuxer<'a> { +struct AVIDemuxer<'a> { opened: bool, src: &'a mut ByteReader<'a>, cur_frame: Vec, @@ -106,7 +106,7 @@ impl<'a> Demux<'a> for AVIDemuxer<'a> { } impl<'a> AVIDemuxer<'a> { - pub fn new(io: &'a mut ByteReader<'a>) -> Self { + fn new(io: &'a mut ByteReader<'a>) -> Self { AVIDemuxer { cur_frame: Vec::new(), num_streams: 0, @@ -358,6 +358,15 @@ fn parse_junk(dmx: &mut AVIDemuxer, size: usize) -> DemuxerResult { Ok(size) } +pub struct AVIDemuxerCreator { } + +impl DemuxerCreator for AVIDemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a> { + Box::new(AVIDemuxer::new(br)) + } + fn get_name(&self) -> &'static str { "avi" } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/demuxers/gdv.rs b/src/demuxers/gdv.rs index 09c2620..897550f 100644 --- a/src/demuxers/gdv.rs +++ b/src/demuxers/gdv.rs @@ -10,7 +10,7 @@ enum GDVState { } #[allow(dead_code)] -pub struct GremlinVideoDemuxer<'a> { +struct GremlinVideoDemuxer<'a> { opened: bool, src: &'a mut ByteReader<'a>, frames: u16, @@ -90,7 +90,7 @@ impl<'a> Demux<'a> for GremlinVideoDemuxer<'a> { } }*/ impl<'a> GremlinVideoDemuxer<'a> { - pub fn new(io: &'a mut ByteReader<'a>) -> Self { + fn new(io: &'a mut ByteReader<'a>) -> Self { GremlinVideoDemuxer { cur_frame: 0, frames: 0, @@ -126,6 +126,15 @@ pktdta: Vec::new(), } } +pub struct GDVDemuxerCreator { } + +impl DemuxerCreator for GDVDemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a> { + Box::new(GremlinVideoDemuxer::new(br)) + } + fn get_name(&self) -> &'static str { "gdv" } +} + #[cfg(test)] mod test { use super::*; diff --git a/src/demuxers/mod.rs b/src/demuxers/mod.rs index 6499517..707984b 100644 --- a/src/demuxers/mod.rs +++ b/src/demuxers/mod.rs @@ -193,3 +193,24 @@ impl FrameFromPacket for NAFrame { self.set_duration(pkt.duration); } } + +pub trait DemuxerCreator { + fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box + 'a>; + fn get_name(&self) -> &'static str; +} + +const DEMUXERS: &[&'static DemuxerCreator] = &[ +#[cfg(feature="demuxer_avi")] + &avi::AVIDemuxerCreator {}, +#[cfg(feature="demuxer_gdv")] + &gdv::GDVDemuxerCreator {}, +]; + +pub fn find_demuxer(name: &str) -> Option<&DemuxerCreator> { + for i in 0..DEMUXERS.len() { + if DEMUXERS[i].get_name() == name { + return Some(DEMUXERS[i]); + } + } + None +}