| 1 | pub mod gdv; |
| 2 | |
| 3 | use std::fmt; |
| 4 | use std::rc::Rc; |
| 5 | use frame::*; |
| 6 | //use std::collections::HashMap; |
| 7 | use io::byteio::*; |
| 8 | |
| 9 | #[derive(Debug)] |
| 10 | #[allow(dead_code)] |
| 11 | pub enum StreamType { |
| 12 | Video, |
| 13 | Audio, |
| 14 | Subtitles, |
| 15 | Data, |
| 16 | } |
| 17 | |
| 18 | impl fmt::Display for StreamType { |
| 19 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 20 | match *self { |
| 21 | StreamType::Video => write!(f, "Video"), |
| 22 | StreamType::Audio => write!(f, "Audio"), |
| 23 | StreamType::Subtitles => write!(f, "Subtitles"), |
| 24 | StreamType::Data => write!(f, "Data"), |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | |
| 30 | #[allow(dead_code)] |
| 31 | pub struct NAStream { |
| 32 | media_type: StreamType, |
| 33 | id: u32, |
| 34 | info: Rc<NACodecInfo>, |
| 35 | } |
| 36 | |
| 37 | impl NAStream { |
| 38 | pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self { |
| 39 | NAStream { media_type: mt, id: id, info: Rc::new(info) } |
| 40 | } |
| 41 | pub fn get_id(&self) -> u32 { self.id } |
| 42 | pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() } |
| 43 | } |
| 44 | |
| 45 | impl fmt::Display for NAStream { |
| 46 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 47 | write!(f, "({}#{})", self.media_type, self.id) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | #[allow(dead_code)] |
| 52 | pub struct NAPacket { |
| 53 | stream: Rc<NAStream>, |
| 54 | pts: Option<u64>, |
| 55 | dts: Option<u64>, |
| 56 | duration: Option<u64>, |
| 57 | buffer: Rc<Vec<u8>>, |
| 58 | keyframe: bool, |
| 59 | // options: HashMap<String, NAValue<'a>>, |
| 60 | } |
| 61 | |
| 62 | impl NAPacket { |
| 63 | pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self { |
| 64 | // let mut vec: Vec<u8> = Vec::new(); |
| 65 | // vec.resize(size, 0); |
| 66 | NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) } |
| 67 | } |
| 68 | pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() } |
| 69 | pub fn get_pts(&self) -> Option<u64> { self.pts } |
| 70 | pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() } |
| 71 | } |
| 72 | |
| 73 | impl fmt::Display for NAPacket { |
| 74 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 75 | let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len()); |
| 76 | if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); } |
| 77 | if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); } |
| 78 | if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); } |
| 79 | if self.keyframe { foo = format!("{} kf", foo); } |
| 80 | foo = foo + "]"; |
| 81 | write!(f, "{}", foo) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #[derive(Debug)] |
| 86 | #[allow(dead_code)] |
| 87 | pub enum DemuxerError { |
| 88 | EOF, |
| 89 | NoSuchInput, |
| 90 | InvalidData, |
| 91 | IOError, |
| 92 | NotImplemented, |
| 93 | MemoryError, |
| 94 | } |
| 95 | |
| 96 | type DemuxerResult<T> = Result<T, DemuxerError>; |
| 97 | |
| 98 | pub trait NADemuxer<'a> { |
| 99 | fn open(&mut self) -> DemuxerResult<()>; |
| 100 | fn get_frame(&mut self) -> DemuxerResult<NAPacket>; |
| 101 | fn seek(&mut self, time: u64) -> DemuxerResult<()>; |
| 102 | } |
| 103 | |
| 104 | pub trait NAPacketReader { |
| 105 | fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
| 106 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
| 107 | } |
| 108 | |
| 109 | impl<'a> NAPacketReader for ByteReader<'a> { |
| 110 | fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
| 111 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
| 112 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } |
| 113 | buf.resize(size, 0); |
| 114 | let res = self.read_buf(buf.as_mut_slice()); |
| 115 | if let Err(_) = res { return Err(DemuxerError::IOError); } |
| 116 | if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); } |
| 117 | let pkt = NAPacket::new(str, pts, dts, dur, kf, buf); |
| 118 | Ok(pkt) |
| 119 | } |
| 120 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { |
| 121 | let mut refbuf = pkt.get_buffer(); |
| 122 | let mut buf = Rc::make_mut(&mut refbuf); |
| 123 | let res = self.read_buf(buf.as_mut_slice()); |
| 124 | if let Err(_) = res { return Err(DemuxerError::IOError); } |
| 125 | if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); } |
| 126 | Ok(()) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub struct NADemuxerBuilder { |
| 131 | } |
| 132 | |
| 133 | impl From<ByteIOError> for DemuxerError { |
| 134 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } |
| 135 | } |
| 136 | |
| 137 | impl NADemuxerBuilder { |
| 138 | #[allow(unused_variables)] |
| 139 | pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult<Box<NADemuxer<'static>>> { |
| 140 | unimplemented!() |
| 141 | } |
| 142 | } |