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