| 1 | use std::rc::Rc; |
| 2 | pub use crate::frame::*; |
| 3 | pub use crate::io::byteio::*; |
| 4 | |
| 5 | #[derive(Debug,Clone,Copy,PartialEq)] |
| 6 | #[allow(dead_code)] |
| 7 | pub enum DemuxerError { |
| 8 | EOF, |
| 9 | NoSuchInput, |
| 10 | InvalidData, |
| 11 | IOError, |
| 12 | NotImplemented, |
| 13 | MemoryError, |
| 14 | TryAgain, |
| 15 | } |
| 16 | |
| 17 | pub type DemuxerResult<T> = Result<T, DemuxerError>; |
| 18 | |
| 19 | pub trait DemuxCore<'a> { |
| 20 | fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()>; |
| 21 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; |
| 22 | fn seek(&mut self, time: u64) -> DemuxerResult<()>; |
| 23 | } |
| 24 | |
| 25 | pub trait NAPacketReader { |
| 26 | fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
| 27 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
| 28 | } |
| 29 | |
| 30 | impl<'a> NAPacketReader for ByteReader<'a> { |
| 31 | fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
| 32 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
| 33 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } |
| 34 | buf.resize(size, 0); |
| 35 | let res = self.read_buf(buf.as_mut_slice()); |
| 36 | if let Err(_) = res { return Err(DemuxerError::IOError); } |
| 37 | let pkt = NAPacket::new(str, ts, kf, buf); |
| 38 | Ok(pkt) |
| 39 | } |
| 40 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { |
| 41 | let mut refbuf = pkt.get_buffer(); |
| 42 | let buf = refbuf.as_mut().unwrap(); |
| 43 | let res = self.read_buf(buf.as_mut_slice()); |
| 44 | if let Err(_) = res { return Err(DemuxerError::IOError); } |
| 45 | Ok(()) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub struct StreamManager { |
| 50 | streams: Vec<Rc<NAStream>>, |
| 51 | ignored: Vec<bool>, |
| 52 | no_ign: bool, |
| 53 | } |
| 54 | |
| 55 | impl StreamManager { |
| 56 | pub fn new() -> Self { |
| 57 | StreamManager { |
| 58 | streams: Vec::new(), |
| 59 | ignored: Vec::new(), |
| 60 | no_ign: true, |
| 61 | } |
| 62 | } |
| 63 | pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) } |
| 64 | |
| 65 | pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> { |
| 66 | let stream_num = self.streams.len(); |
| 67 | let mut str = stream.clone(); |
| 68 | str.set_num(stream_num); |
| 69 | self.streams.push(Rc::new(str)); |
| 70 | self.ignored.push(false); |
| 71 | Some(stream_num) |
| 72 | } |
| 73 | pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { |
| 74 | if idx < self.streams.len() { |
| 75 | Some(self.streams[idx].clone()) |
| 76 | } else { |
| 77 | None |
| 78 | } |
| 79 | } |
| 80 | pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> { |
| 81 | for i in 0..self.streams.len() { |
| 82 | if self.streams[i].get_id() == id { |
| 83 | return Some(self.streams[i].clone()); |
| 84 | } |
| 85 | } |
| 86 | None |
| 87 | } |
| 88 | pub fn get_num_streams(&self) -> usize { self.streams.len() } |
| 89 | pub fn is_ignored(&self, idx: usize) -> bool { |
| 90 | if self.no_ign { |
| 91 | true |
| 92 | } else if idx < self.ignored.len() { |
| 93 | self.ignored[idx] |
| 94 | } else { |
| 95 | false |
| 96 | } |
| 97 | } |
| 98 | pub fn is_ignored_id(&self, id: u32) -> bool { |
| 99 | for i in 0..self.streams.len() { |
| 100 | if self.streams[i].get_id() == id { |
| 101 | return self.ignored[i]; |
| 102 | } |
| 103 | } |
| 104 | false |
| 105 | } |
| 106 | pub fn set_ignored(&mut self, idx: usize) { |
| 107 | if idx < self.ignored.len() { |
| 108 | self.ignored[idx] = true; |
| 109 | self.no_ign = false; |
| 110 | } |
| 111 | } |
| 112 | pub fn set_unignored(&mut self, idx: usize) { |
| 113 | if idx < self.ignored.len() { |
| 114 | self.ignored[idx] = false; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | pub struct StreamIter<'a> { |
| 120 | streams: &'a Vec<Rc<NAStream>>, |
| 121 | pos: usize, |
| 122 | } |
| 123 | |
| 124 | impl<'a> StreamIter<'a> { |
| 125 | pub fn new(streams: &'a Vec<Rc<NAStream>>) -> Self { |
| 126 | StreamIter { streams: streams, pos: 0 } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl<'a> Iterator for StreamIter<'a> { |
| 131 | type Item = Rc<NAStream>; |
| 132 | |
| 133 | fn next(&mut self) -> Option<Self::Item> { |
| 134 | if self.pos >= self.streams.len() { return None; } |
| 135 | let ret = self.streams[self.pos].clone(); |
| 136 | self.pos += 1; |
| 137 | Some(ret) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub struct Demuxer<'a> { |
| 142 | dmx: Box<DemuxCore<'a> + 'a>, |
| 143 | streams: StreamManager, |
| 144 | } |
| 145 | |
| 146 | impl<'a> Demuxer<'a> { |
| 147 | fn new(dmx: Box<DemuxCore<'a> + 'a>, str: StreamManager) -> Self { |
| 148 | Demuxer { |
| 149 | dmx: dmx, |
| 150 | streams: str, |
| 151 | } |
| 152 | } |
| 153 | pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { |
| 154 | self.streams.get_stream(idx) |
| 155 | } |
| 156 | pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> { |
| 157 | self.streams.get_stream_by_id(id) |
| 158 | } |
| 159 | pub fn get_num_streams(&self) -> usize { |
| 160 | self.streams.get_num_streams() |
| 161 | } |
| 162 | pub fn get_streams(&self) -> StreamIter { |
| 163 | self.streams.iter() |
| 164 | } |
| 165 | pub fn is_ignored_stream(&self, idx: usize) -> bool { |
| 166 | self.streams.is_ignored(idx) |
| 167 | } |
| 168 | pub fn set_ignored_stream(&mut self, idx: usize) { |
| 169 | self.streams.set_ignored(idx) |
| 170 | } |
| 171 | pub fn set_unignored_stream(&mut self, idx: usize) { |
| 172 | self.streams.set_unignored(idx) |
| 173 | } |
| 174 | |
| 175 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { |
| 176 | loop { |
| 177 | let res = self.dmx.get_frame(&mut self.streams); |
| 178 | if self.streams.no_ign || res.is_err() { return res; } |
| 179 | let res = res.unwrap(); |
| 180 | let idx = res.get_stream().get_num(); |
| 181 | if !self.is_ignored_stream(idx) { |
| 182 | return Ok(res); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | pub fn seek(&mut self, time: u64) -> DemuxerResult<()> { |
| 187 | self.dmx.seek(time) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | impl From<ByteIOError> for DemuxerError { |
| 192 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } |
| 193 | } |
| 194 | |
| 195 | ///The structure used to create demuxers. |
| 196 | pub trait DemuxerCreator { |
| 197 | /// Create new demuxer instance that will use `ByteReader` source as an input. |
| 198 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a>; |
| 199 | /// Get the name of current demuxer creator. |
| 200 | fn get_name(&self) -> &'static str; |
| 201 | } |
| 202 | |
| 203 | pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { |
| 204 | let mut dmx = dmxcr.new_demuxer(br); |
| 205 | let mut str = StreamManager::new(); |
| 206 | dmx.open(&mut str)?; |
| 207 | Ok(Demuxer::new(dmx, str)) |
| 208 | } |
| 209 | |
| 210 | pub struct RegisteredDemuxers { |
| 211 | dmxs: Vec<&'static DemuxerCreator>, |
| 212 | } |
| 213 | |
| 214 | impl RegisteredDemuxers { |
| 215 | pub fn new() -> Self { |
| 216 | Self { dmxs: Vec::new() } |
| 217 | } |
| 218 | pub fn add_demuxer(&mut self, dmx: &'static DemuxerCreator) { |
| 219 | self.dmxs.push(dmx); |
| 220 | } |
| 221 | pub fn find_demuxer(&self, name: &str) -> Option<&DemuxerCreator> { |
| 222 | for &dmx in self.dmxs.iter() { |
| 223 | if dmx.get_name() == name { |
| 224 | return Some(dmx); |
| 225 | } |
| 226 | } |
| 227 | None |
| 228 | } |
| 229 | } |