]> git.nihav.org Git - nihav.git/blobdiff - src/demuxers/mod.rs
move NAStream/NAPacket definitions into module frame
[nihav.git] / src / demuxers / mod.rs
index 14aac9380761bff34c4ff1fdb6161fca95f3b373..d4d70a2f82e66673ed8e253c19660b9a473d4500 100644 (file)
@@ -1,91 +1,12 @@
+#[cfg(feature="demuxer_gdv")]
 pub mod gdv;
+#[cfg(feature="demuxer_avi")]
+pub mod avi;
 
-use std::fmt;
 use std::rc::Rc;
 use frame::*;
-//use std::collections::HashMap;
 use io::byteio::*;
 
-#[derive(Debug,Clone,Copy)]
-#[allow(dead_code)]
-pub enum StreamType {
-    Video,
-    Audio,
-    Subtitles,
-    Data,
-}
-
-impl fmt::Display for StreamType {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        match *self {
-            StreamType::Video => write!(f, "Video"),
-            StreamType::Audio => write!(f, "Audio"),
-            StreamType::Subtitles => write!(f, "Subtitles"),
-            StreamType::Data => write!(f, "Data"),
-        }
-    }
-}
-
-
-#[allow(dead_code)]
-#[derive(Clone)]
-pub struct NAStream {
-    media_type:     StreamType,
-    id:             u32,
-    num:            usize,
-    info:           Rc<NACodecInfo>,
-}
-
-impl NAStream {
-    pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
-        NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
-    }
-    pub fn get_id(&self) -> u32 { self.id }
-    pub fn get_num(&self) -> usize { self.num }
-    pub fn set_num(&mut self, num: usize) { self.num = num; }
-    pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
-}
-
-impl fmt::Display for NAStream {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "({}#{})", self.media_type, self.id)
-    }
-}
-
-#[allow(dead_code)]
-pub struct NAPacket {
-    stream:         Rc<NAStream>,
-    pts:            Option<u64>,
-    dts:            Option<u64>,
-    duration:       Option<u64>,
-    buffer:         Rc<Vec<u8>>,
-    keyframe:       bool,
-//    options:        HashMap<String, NAValue<'a>>,
-}
-
-impl NAPacket {
-    pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
-//        let mut vec: Vec<u8> = Vec::new();
-//        vec.resize(size, 0);
-        NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
-    }
-    pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
-    pub fn get_pts(&self) -> Option<u64> { self.pts }
-    pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
-}
-
-impl fmt::Display for NAPacket {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
-        if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
-        if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
-        if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
-        if self.keyframe { foo = format!("{} kf", foo); }
-        foo = foo + "]";
-        write!(f, "{}", foo)
-    }
-}
-
 #[derive(Debug)]
 #[allow(dead_code)]
 pub enum DemuxerError {
@@ -101,6 +22,8 @@ type DemuxerResult<T> = Result<T, DemuxerError>;
 
 pub trait Demux<'a> {
     fn open(&mut self) -> DemuxerResult<()>;
+    fn get_num_streams(&self) -> usize;
+    fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>>;
     fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
     fn seek(&mut self, time: u64) -> DemuxerResult<()>;
 }
@@ -117,7 +40,6 @@ impl<'a> NAPacketReader for ByteReader<'a> {
         buf.resize(size, 0);
         let res = self.read_buf(buf.as_mut_slice());
         if let Err(_) = res { return Err(DemuxerError::IOError); }
-        if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); }
         let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
         Ok(pkt)
     }
@@ -126,12 +48,11 @@ impl<'a> NAPacketReader for ByteReader<'a> {
         let mut buf = Rc::make_mut(&mut refbuf);
         let res = self.read_buf(buf.as_mut_slice());
         if let Err(_) = res { return Err(DemuxerError::IOError); }
-        if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); }
         Ok(())
     }
 }
 
-pub struct Demuxer {
+struct Demuxer {
     streams: Vec<Rc<NAStream>>,
 }
 
@@ -151,6 +72,7 @@ impl Demuxer {
             None
         }
     }
+    #[allow(dead_code)]
     pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
         for i in 0..self.streams.len() {
             if self.streams[i].get_id() == id {
@@ -159,15 +81,33 @@ impl Demuxer {
         }
         None
     }
+    pub fn get_num_streams(&self) -> usize { self.streams.len() }
 }
 
 impl From<ByteIOError> for DemuxerError {
     fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
 }
 
-//impl NADemuxerBuilder {
-//    #[allow(unused_variables)]
-//    pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult<Box<NADemuxer<'static>>> {
-//        unimplemented!()
-//    }
-//}
+///The structure used to create demuxers.
+pub trait DemuxerCreator {
+    /// Create new demuxer instance that will use `ByteReader` source as an input.
+    fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<Demux<'a> + 'a>;
+    /// Get the name of current demuxer creator.
+    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 &dmx in DEMUXERS {
+        if dmx.get_name() == name {
+            return Some(dmx);
+        }
+    }
+    None
+}