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