| 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 | SeekError, |
| 15 | NotPossible, |
| 16 | } |
| 17 | |
| 18 | pub type DemuxerResult<T> = Result<T, DemuxerError>; |
| 19 | |
| 20 | pub trait DemuxCore<'a> { |
| 21 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; |
| 22 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; |
| 23 | fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()>; |
| 24 | } |
| 25 | |
| 26 | pub trait NAPacketReader { |
| 27 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
| 28 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
| 29 | } |
| 30 | |
| 31 | impl<'a> NAPacketReader for ByteReader<'a> { |
| 32 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
| 33 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
| 34 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } |
| 35 | buf.resize(size, 0); |
| 36 | self.read_buf(buf.as_mut_slice())?; |
| 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 | self.read_buf(buf.as_mut_slice())?; |
| 44 | Ok(()) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[derive(Default)] |
| 49 | pub struct StreamManager { |
| 50 | streams: Vec<NAStreamRef>, |
| 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(str.into_ref()); |
| 70 | self.ignored.push(false); |
| 71 | Some(stream_num) |
| 72 | } |
| 73 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
| 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<NAStreamRef> { |
| 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 [NAStreamRef], |
| 121 | pos: usize, |
| 122 | } |
| 123 | |
| 124 | impl<'a> StreamIter<'a> { |
| 125 | pub fn new(streams: &'a [NAStreamRef]) -> Self { |
| 126 | StreamIter { streams, pos: 0 } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl<'a> Iterator for StreamIter<'a> { |
| 131 | type Item = NAStreamRef; |
| 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 | #[derive(Clone,Copy,PartialEq)] |
| 142 | pub enum SeekIndexMode { |
| 143 | None, |
| 144 | Present, |
| 145 | Automatic, |
| 146 | } |
| 147 | |
| 148 | impl Default for SeekIndexMode { |
| 149 | fn default() -> Self { SeekIndexMode::None } |
| 150 | } |
| 151 | |
| 152 | #[derive(Clone,Copy,Default)] |
| 153 | pub struct SeekEntry { |
| 154 | pub pts: u64, |
| 155 | pub pos: u64, |
| 156 | } |
| 157 | |
| 158 | #[derive(Clone)] |
| 159 | pub struct StreamSeekInfo { |
| 160 | pub id: u32, |
| 161 | pub tb_num: u32, |
| 162 | pub tb_den: u32, |
| 163 | pub filled: bool, |
| 164 | pub entries: Vec<SeekEntry>, |
| 165 | } |
| 166 | |
| 167 | impl StreamSeekInfo { |
| 168 | pub fn new(id: u32, tb_num: u32, tb_den: u32) -> Self { |
| 169 | Self { |
| 170 | id, tb_num, tb_den, |
| 171 | filled: false, |
| 172 | entries: Vec::new(), |
| 173 | } |
| 174 | } |
| 175 | pub fn add_entry(&mut self, entry: SeekEntry) { |
| 176 | self.entries.push(entry); |
| 177 | } |
| 178 | pub fn find_pos(&self, pts: u64) -> Option<u64> { |
| 179 | if !self.entries.is_empty() { |
| 180 | // todo something faster like binary search |
| 181 | let mut cand = 0; |
| 182 | for (idx, entry) in self.entries.iter().enumerate() { |
| 183 | if entry.pts <= pts { |
| 184 | cand = idx; |
| 185 | } else { |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | Some(self.entries[cand].pos) |
| 190 | } else { |
| 191 | None |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #[derive(Clone,Copy,Default)] |
| 197 | pub struct SeekIndexResult { |
| 198 | pub pts: u64, |
| 199 | pub pos: u64, |
| 200 | pub str_id: u32, |
| 201 | } |
| 202 | |
| 203 | #[derive(Default)] |
| 204 | pub struct SeekIndex { |
| 205 | pub seek_info: Vec<StreamSeekInfo>, |
| 206 | pub mode: SeekIndexMode, |
| 207 | } |
| 208 | |
| 209 | impl SeekIndex { |
| 210 | pub fn new() -> Self { Self::default() } |
| 211 | pub fn add_stream(&mut self, id: u32, tb_num: u32, tb_den: u32) { |
| 212 | if self.stream_id_to_index(id).is_none() { |
| 213 | self.seek_info.push(StreamSeekInfo::new(id, tb_num, tb_den)); |
| 214 | } |
| 215 | } |
| 216 | pub fn stream_id_to_index(&self, id: u32) -> Option<usize> { |
| 217 | for (idx, str) in self.seek_info.iter().enumerate() { |
| 218 | if str.id == id { |
| 219 | return Some(idx); |
| 220 | } |
| 221 | } |
| 222 | None |
| 223 | } |
| 224 | pub fn find_pos(&self, time: u64) -> Option<SeekIndexResult> { |
| 225 | let mut cand = None; |
| 226 | for str in self.seek_info.iter() { |
| 227 | if !str.filled { continue; } |
| 228 | let pts = NATimeInfo::time_to_ts(time, 1000, str.tb_num, str.tb_den); |
| 229 | let pos = str.find_pos(pts); |
| 230 | if pos.is_none() { continue; } |
| 231 | let pos = pos.unwrap(); |
| 232 | if cand.is_none() { |
| 233 | cand = Some(SeekIndexResult { pts, pos, str_id: str.id }); |
| 234 | } else if let Some(entry) = cand { |
| 235 | if pos < entry.pos { |
| 236 | cand = Some(SeekIndexResult { pts, pos, str_id: str.id }); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | cand |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | pub struct Demuxer<'a> { |
| 245 | dmx: Box<dyn DemuxCore<'a> + 'a>, |
| 246 | streams: StreamManager, |
| 247 | seek_idx: SeekIndex, |
| 248 | } |
| 249 | |
| 250 | impl<'a> Demuxer<'a> { |
| 251 | fn new(dmx: Box<dyn DemuxCore<'a> + 'a>, str: StreamManager, seek_idx: SeekIndex) -> Self { |
| 252 | Demuxer { |
| 253 | dmx, |
| 254 | streams: str, |
| 255 | seek_idx, |
| 256 | } |
| 257 | } |
| 258 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
| 259 | self.streams.get_stream(idx) |
| 260 | } |
| 261 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
| 262 | self.streams.get_stream_by_id(id) |
| 263 | } |
| 264 | pub fn get_num_streams(&self) -> usize { |
| 265 | self.streams.get_num_streams() |
| 266 | } |
| 267 | pub fn get_streams(&self) -> StreamIter { |
| 268 | self.streams.iter() |
| 269 | } |
| 270 | pub fn is_ignored_stream(&self, idx: usize) -> bool { |
| 271 | self.streams.is_ignored(idx) |
| 272 | } |
| 273 | pub fn set_ignored_stream(&mut self, idx: usize) { |
| 274 | self.streams.set_ignored(idx) |
| 275 | } |
| 276 | pub fn set_unignored_stream(&mut self, idx: usize) { |
| 277 | self.streams.set_unignored(idx) |
| 278 | } |
| 279 | |
| 280 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { |
| 281 | loop { |
| 282 | let res = self.dmx.get_frame(&mut self.streams); |
| 283 | if self.streams.no_ign || res.is_err() { return res; } |
| 284 | let res = res.unwrap(); |
| 285 | let idx = res.get_stream().get_num(); |
| 286 | if !self.is_ignored_stream(idx) { |
| 287 | return Ok(res); |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | pub fn seek(&mut self, time: u64) -> DemuxerResult<()> { |
| 292 | self.dmx.seek(time, &self.seek_idx) |
| 293 | } |
| 294 | pub fn get_seek_index(&self) -> &SeekIndex { |
| 295 | &self.seek_idx |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | impl From<ByteIOError> for DemuxerError { |
| 300 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } |
| 301 | } |
| 302 | |
| 303 | ///The structure used to create demuxers. |
| 304 | pub trait DemuxerCreator { |
| 305 | /// Create new demuxer instance that will use `ByteReader` source as an input. |
| 306 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a>; |
| 307 | /// Get the name of current demuxer creator. |
| 308 | fn get_name(&self) -> &'static str; |
| 309 | } |
| 310 | |
| 311 | pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { |
| 312 | let mut dmx = dmxcr.new_demuxer(br); |
| 313 | let mut str = StreamManager::new(); |
| 314 | let mut seek_idx = SeekIndex::new(); |
| 315 | dmx.open(&mut str, &mut seek_idx)?; |
| 316 | Ok(Demuxer::new(dmx, str, seek_idx)) |
| 317 | } |
| 318 | |
| 319 | #[derive(Default)] |
| 320 | pub struct RegisteredDemuxers { |
| 321 | dmxs: Vec<&'static DemuxerCreator>, |
| 322 | } |
| 323 | |
| 324 | impl RegisteredDemuxers { |
| 325 | pub fn new() -> Self { |
| 326 | Self { dmxs: Vec::new() } |
| 327 | } |
| 328 | pub fn add_demuxer(&mut self, dmx: &'static DemuxerCreator) { |
| 329 | self.dmxs.push(dmx); |
| 330 | } |
| 331 | pub fn find_demuxer(&self, name: &str) -> Option<&DemuxerCreator> { |
| 332 | for &dmx in self.dmxs.iter() { |
| 333 | if dmx.get_name() == name { |
| 334 | return Some(dmx); |
| 335 | } |
| 336 | } |
| 337 | None |
| 338 | } |
| 339 | } |