Commit | Line | Data |
---|---|---|
4e8b4f31 KS |
1 | pub use crate::frame::*; |
2 | pub use crate::io::byteio::*; | |
5869fd63 | 3 | |
b1be9318 | 4 | #[derive(Debug,Clone,Copy,PartialEq)] |
5869fd63 KS |
5 | #[allow(dead_code)] |
6 | pub enum DemuxerError { | |
7 | EOF, | |
8 | NoSuchInput, | |
9 | InvalidData, | |
10 | IOError, | |
11 | NotImplemented, | |
12 | MemoryError, | |
fe07b469 | 13 | TryAgain, |
33b5a8f0 KS |
14 | SeekError, |
15 | NotPossible, | |
5869fd63 KS |
16 | } |
17 | ||
5641dccf | 18 | pub type DemuxerResult<T> = Result<T, DemuxerError>; |
5869fd63 | 19 | |
bcfeae48 | 20 | pub trait DemuxCore<'a> { |
33b5a8f0 | 21 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; |
bcfeae48 | 22 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; |
33b5a8f0 | 23 | fn seek(&mut self, time: u64, seek_idx: &SeekIndex) -> DemuxerResult<()>; |
5869fd63 KS |
24 | } |
25 | ||
8869d452 | 26 | pub trait NAPacketReader { |
70910ac3 | 27 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
5869fd63 KS |
28 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
29 | } | |
30 | ||
8869d452 | 31 | impl<'a> NAPacketReader for ByteReader<'a> { |
70910ac3 | 32 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
5869fd63 KS |
33 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
34 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } | |
35 | buf.resize(size, 0); | |
e243ceb4 | 36 | self.read_buf(buf.as_mut_slice())?; |
e189501e | 37 | let pkt = NAPacket::new(str, ts, kf, buf); |
5869fd63 KS |
38 | Ok(pkt) |
39 | } | |
40 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { | |
41 | let mut refbuf = pkt.get_buffer(); | |
1a967e6b | 42 | let buf = refbuf.as_mut().unwrap(); |
e243ceb4 | 43 | self.read_buf(buf.as_mut_slice())?; |
5869fd63 KS |
44 | Ok(()) |
45 | } | |
46 | } | |
47 | ||
e243ceb4 | 48 | #[derive(Default)] |
bcfeae48 | 49 | pub struct StreamManager { |
70910ac3 | 50 | streams: Vec<NAStreamRef>, |
bcfeae48 KS |
51 | ignored: Vec<bool>, |
52 | no_ign: bool, | |
20ef4353 KS |
53 | } |
54 | ||
bcfeae48 KS |
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 | ||
20ef4353 KS |
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); | |
70910ac3 | 69 | self.streams.push(str.into_ref()); |
bcfeae48 | 70 | self.ignored.push(false); |
20ef4353 KS |
71 | Some(stream_num) |
72 | } | |
70910ac3 | 73 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
20ef4353 KS |
74 | if idx < self.streams.len() { |
75 | Some(self.streams[idx].clone()) | |
76 | } else { | |
77 | None | |
78 | } | |
79 | } | |
70910ac3 | 80 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
20ef4353 KS |
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 | } | |
66116504 | 88 | pub fn get_num_streams(&self) -> usize { self.streams.len() } |
bcfeae48 KS |
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 | } | |
ce52b3b5 KS |
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 | } | |
bcfeae48 KS |
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> { | |
e243ceb4 | 120 | streams: &'a [NAStreamRef], |
bcfeae48 KS |
121 | pos: usize, |
122 | } | |
123 | ||
124 | impl<'a> StreamIter<'a> { | |
e243ceb4 KS |
125 | pub fn new(streams: &'a [NAStreamRef]) -> Self { |
126 | StreamIter { streams, pos: 0 } | |
bcfeae48 KS |
127 | } |
128 | } | |
129 | ||
130 | impl<'a> Iterator for StreamIter<'a> { | |
70910ac3 | 131 | type Item = NAStreamRef; |
bcfeae48 KS |
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 | ||
33b5a8f0 KS |
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 | ||
bcfeae48 | 244 | pub struct Demuxer<'a> { |
6011e201 | 245 | dmx: Box<dyn DemuxCore<'a> + 'a>, |
bcfeae48 | 246 | streams: StreamManager, |
33b5a8f0 | 247 | seek_idx: SeekIndex, |
bcfeae48 KS |
248 | } |
249 | ||
250 | impl<'a> Demuxer<'a> { | |
33b5a8f0 | 251 | fn new(dmx: Box<dyn DemuxCore<'a> + 'a>, str: StreamManager, seek_idx: SeekIndex) -> Self { |
bcfeae48 | 252 | Demuxer { |
e243ceb4 | 253 | dmx, |
bcfeae48 | 254 | streams: str, |
33b5a8f0 | 255 | seek_idx, |
bcfeae48 KS |
256 | } |
257 | } | |
70910ac3 | 258 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
bcfeae48 KS |
259 | self.streams.get_stream(idx) |
260 | } | |
70910ac3 | 261 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
bcfeae48 KS |
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<()> { | |
33b5a8f0 KS |
292 | self.dmx.seek(time, &self.seek_idx) |
293 | } | |
294 | pub fn get_seek_index(&self) -> &SeekIndex { | |
295 | &self.seek_idx | |
bcfeae48 | 296 | } |
5869fd63 KS |
297 | } |
298 | ||
299 | impl From<ByteIOError> for DemuxerError { | |
300 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } | |
301 | } | |
302 | ||
3f7c7cfd | 303 | ///The structure used to create demuxers. |
eb71d98f | 304 | pub trait DemuxerCreator { |
3f7c7cfd | 305 | /// Create new demuxer instance that will use `ByteReader` source as an input. |
6011e201 | 306 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a>; |
3f7c7cfd | 307 | /// Get the name of current demuxer creator. |
eb71d98f KS |
308 | fn get_name(&self) -> &'static str; |
309 | } | |
310 | ||
bcfeae48 KS |
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(); | |
33b5a8f0 KS |
314 | let mut seek_idx = SeekIndex::new(); |
315 | dmx.open(&mut str, &mut seek_idx)?; | |
316 | Ok(Demuxer::new(dmx, str, seek_idx)) | |
bcfeae48 | 317 | } |
5641dccf | 318 | |
e243ceb4 | 319 | #[derive(Default)] |
5641dccf KS |
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 | } | |
1a967e6b | 339 | } |