]>
Commit | Line | Data |
---|---|---|
ab683361 | 1 | //! Demuxer definitions. |
4e8b4f31 KS |
2 | pub use crate::frame::*; |
3 | pub use crate::io::byteio::*; | |
787b8d03 | 4 | pub use crate::options::*; |
5869fd63 | 5 | |
ab683361 | 6 | /// A list specifying general demuxing errors. |
b1be9318 | 7 | #[derive(Debug,Clone,Copy,PartialEq)] |
5869fd63 KS |
8 | #[allow(dead_code)] |
9 | pub enum DemuxerError { | |
ab683361 | 10 | /// Reader got to end of stream. |
5869fd63 | 11 | EOF, |
ab683361 | 12 | /// Demuxer encountered empty container. |
5869fd63 | 13 | NoSuchInput, |
ab683361 | 14 | /// Demuxer encountered invalid input data. |
5869fd63 | 15 | InvalidData, |
ab683361 | 16 | /// Data reading error. |
5869fd63 | 17 | IOError, |
ab683361 | 18 | /// Feature is not implemented. |
5869fd63 | 19 | NotImplemented, |
ab683361 | 20 | /// Allocation failed. |
5869fd63 | 21 | MemoryError, |
ab683361 | 22 | /// The operation should be repeated. |
fe07b469 | 23 | TryAgain, |
ab683361 | 24 | /// Seeking failed. |
33b5a8f0 | 25 | SeekError, |
ab683361 | 26 | /// Operation cannot succeed in principle (e.g. seeking in a format not supporting seeking). |
33b5a8f0 | 27 | NotPossible, |
5869fd63 KS |
28 | } |
29 | ||
ab683361 | 30 | /// A specialised `Result` type for demuxing operations. |
5641dccf | 31 | pub type DemuxerResult<T> = Result<T, DemuxerError>; |
5869fd63 | 32 | |
ab683361 | 33 | /// A trait for demuxing operations. |
787b8d03 | 34 | pub trait DemuxCore<'a>: NAOptionHandler { |
ab683361 | 35 | /// Opens the input stream, reads required headers and prepares everything for packet demuxing. |
33b5a8f0 | 36 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; |
ab683361 | 37 | /// Demuxes a packet. |
bcfeae48 | 38 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; |
ab683361 | 39 | /// Seeks to the requested time. |
24d99894 | 40 | fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()>; |
a480a0de KS |
41 | /// Returns container duration in milliseconds (zero if not available). |
42 | fn get_duration(&self) -> u64; | |
5869fd63 KS |
43 | } |
44 | ||
ab683361 | 45 | /// An auxiliary trait to make bytestream reader read packet data. |
8869d452 | 46 | pub trait NAPacketReader { |
ab683361 | 47 | /// Reads input and constructs a packet containing it. |
70910ac3 | 48 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; |
ab683361 | 49 | /// Reads input into already existing packet. |
5869fd63 KS |
50 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; |
51 | } | |
52 | ||
8869d452 | 53 | impl<'a> NAPacketReader for ByteReader<'a> { |
70910ac3 | 54 | fn read_packet(&mut self, str: NAStreamRef, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { |
5869fd63 KS |
55 | let mut buf: Vec<u8> = Vec::with_capacity(size); |
56 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } | |
57 | buf.resize(size, 0); | |
e243ceb4 | 58 | self.read_buf(buf.as_mut_slice())?; |
e189501e | 59 | let pkt = NAPacket::new(str, ts, kf, buf); |
5869fd63 KS |
60 | Ok(pkt) |
61 | } | |
62 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { | |
63 | let mut refbuf = pkt.get_buffer(); | |
1a967e6b | 64 | let buf = refbuf.as_mut().unwrap(); |
e243ceb4 | 65 | self.read_buf(buf.as_mut_slice())?; |
5869fd63 KS |
66 | Ok(()) |
67 | } | |
68 | } | |
69 | ||
ab683361 | 70 | /// An auxiliary structure for operations with individual streams inside the container. |
e243ceb4 | 71 | #[derive(Default)] |
bcfeae48 | 72 | pub struct StreamManager { |
70910ac3 | 73 | streams: Vec<NAStreamRef>, |
bcfeae48 KS |
74 | ignored: Vec<bool>, |
75 | no_ign: bool, | |
20ef4353 KS |
76 | } |
77 | ||
bcfeae48 | 78 | impl StreamManager { |
ab683361 | 79 | /// Constructs a new instance of `StreamManager`. |
bcfeae48 KS |
80 | pub fn new() -> Self { |
81 | StreamManager { | |
82 | streams: Vec::new(), | |
83 | ignored: Vec::new(), | |
84 | no_ign: true, | |
85 | } | |
86 | } | |
ab683361 | 87 | /// Returns stream iterator. |
bcfeae48 KS |
88 | pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) } |
89 | ||
ab683361 | 90 | /// Adds a new stream. |
20ef4353 KS |
91 | pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> { |
92 | let stream_num = self.streams.len(); | |
817e4872 KS |
93 | let mut stream = stream; |
94 | stream.set_num(stream_num); | |
95 | self.streams.push(stream.into_ref()); | |
bcfeae48 | 96 | self.ignored.push(false); |
20ef4353 KS |
97 | Some(stream_num) |
98 | } | |
b3247252 KS |
99 | /// Adds a new stream from reference-counted object. |
100 | pub fn add_stream_ref(&mut self, stream: NAStreamRef) -> Option<usize> { | |
101 | let stream_num = self.streams.len(); | |
102 | self.streams.push(stream); | |
103 | self.ignored.push(false); | |
104 | Some(stream_num) | |
105 | } | |
ab683361 | 106 | /// Returns stream with the requested index. |
70910ac3 | 107 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
20ef4353 KS |
108 | if idx < self.streams.len() { |
109 | Some(self.streams[idx].clone()) | |
110 | } else { | |
111 | None | |
112 | } | |
113 | } | |
ab683361 | 114 | /// Returns stream with the requested stream ID. |
70910ac3 | 115 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
20ef4353 KS |
116 | for i in 0..self.streams.len() { |
117 | if self.streams[i].get_id() == id { | |
118 | return Some(self.streams[i].clone()); | |
119 | } | |
120 | } | |
121 | None | |
122 | } | |
ab683361 | 123 | /// Returns the number of known streams. |
66116504 | 124 | pub fn get_num_streams(&self) -> usize { self.streams.len() } |
ab683361 | 125 | /// Reports whether the stream is marked as ignored. |
bcfeae48 KS |
126 | pub fn is_ignored(&self, idx: usize) -> bool { |
127 | if self.no_ign { | |
128 | true | |
129 | } else if idx < self.ignored.len() { | |
130 | self.ignored[idx] | |
131 | } else { | |
132 | false | |
133 | } | |
134 | } | |
ab683361 | 135 | /// Reports whether the stream with certain ID is marked as ignored. |
ce52b3b5 KS |
136 | pub fn is_ignored_id(&self, id: u32) -> bool { |
137 | for i in 0..self.streams.len() { | |
138 | if self.streams[i].get_id() == id { | |
139 | return self.ignored[i]; | |
140 | } | |
141 | } | |
142 | false | |
143 | } | |
ab683361 | 144 | /// Marks requested stream as ignored. |
bcfeae48 KS |
145 | pub fn set_ignored(&mut self, idx: usize) { |
146 | if idx < self.ignored.len() { | |
147 | self.ignored[idx] = true; | |
148 | self.no_ign = false; | |
149 | } | |
150 | } | |
ab683361 | 151 | /// Clears the ignored mark for the requested stream. |
bcfeae48 KS |
152 | pub fn set_unignored(&mut self, idx: usize) { |
153 | if idx < self.ignored.len() { | |
154 | self.ignored[idx] = false; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
ab683361 | 159 | /// Stream iterator. |
bcfeae48 | 160 | pub struct StreamIter<'a> { |
e243ceb4 | 161 | streams: &'a [NAStreamRef], |
bcfeae48 KS |
162 | pos: usize, |
163 | } | |
164 | ||
165 | impl<'a> StreamIter<'a> { | |
ab683361 | 166 | /// Constructs a new instance of `StreamIter`. |
e243ceb4 KS |
167 | pub fn new(streams: &'a [NAStreamRef]) -> Self { |
168 | StreamIter { streams, pos: 0 } | |
bcfeae48 KS |
169 | } |
170 | } | |
171 | ||
172 | impl<'a> Iterator for StreamIter<'a> { | |
70910ac3 | 173 | type Item = NAStreamRef; |
bcfeae48 KS |
174 | |
175 | fn next(&mut self) -> Option<Self::Item> { | |
176 | if self.pos >= self.streams.len() { return None; } | |
177 | let ret = self.streams[self.pos].clone(); | |
178 | self.pos += 1; | |
179 | Some(ret) | |
180 | } | |
181 | } | |
182 | ||
ab683361 | 183 | /// Seeking modes. |
33b5a8f0 KS |
184 | #[derive(Clone,Copy,PartialEq)] |
185 | pub enum SeekIndexMode { | |
ab683361 | 186 | /// No seeking index present. |
33b5a8f0 | 187 | None, |
ab683361 | 188 | /// Seeking index is present. |
33b5a8f0 | 189 | Present, |
ab683361 | 190 | /// Seeking index should be constructed by the demuxer if possible. |
33b5a8f0 KS |
191 | Automatic, |
192 | } | |
193 | ||
194 | impl Default for SeekIndexMode { | |
195 | fn default() -> Self { SeekIndexMode::None } | |
196 | } | |
197 | ||
ab683361 | 198 | /// A structure holding seeking information. |
33b5a8f0 KS |
199 | #[derive(Clone,Copy,Default)] |
200 | pub struct SeekEntry { | |
ab683361 KS |
201 | /// Time in milliseconds. |
202 | pub time: u64, | |
203 | /// PTS | |
33b5a8f0 | 204 | pub pts: u64, |
ab683361 | 205 | /// Position in file. |
33b5a8f0 KS |
206 | pub pos: u64, |
207 | } | |
208 | ||
ab683361 | 209 | /// Seeking information for individual streams. |
33b5a8f0 KS |
210 | #[derive(Clone)] |
211 | pub struct StreamSeekInfo { | |
ab683361 | 212 | /// Stream ID. |
33b5a8f0 | 213 | pub id: u32, |
ab683361 | 214 | /// Index is present. |
33b5a8f0 | 215 | pub filled: bool, |
ab683361 | 216 | /// Packet seeking information. |
33b5a8f0 KS |
217 | pub entries: Vec<SeekEntry>, |
218 | } | |
219 | ||
220 | impl StreamSeekInfo { | |
ab683361 | 221 | /// Constructs a new `StreamSeekInfo` instance. |
4cfb5dd4 | 222 | pub fn new(id: u32) -> Self { |
33b5a8f0 | 223 | Self { |
4cfb5dd4 | 224 | id, |
33b5a8f0 KS |
225 | filled: false, |
226 | entries: Vec::new(), | |
227 | } | |
228 | } | |
ab683361 | 229 | /// Adds new seeking point. |
33b5a8f0 KS |
230 | pub fn add_entry(&mut self, entry: SeekEntry) { |
231 | self.entries.push(entry); | |
232 | } | |
ab683361 | 233 | /// Searches for an appropriate seek position before requested time. |
24d99894 KS |
234 | pub fn find_pos(&self, time: NATimePoint) -> Option<SeekEntry> { |
235 | if time == NATimePoint::None { | |
236 | return None; | |
237 | } | |
33b5a8f0 KS |
238 | if !self.entries.is_empty() { |
239 | // todo something faster like binary search | |
4cfb5dd4 KS |
240 | let mut cand = None; |
241 | for entry in self.entries.iter() { | |
24d99894 KS |
242 | match time { |
243 | NATimePoint::Milliseconds(ms) => { | |
244 | if entry.time <= ms { | |
245 | cand = Some(*entry); | |
246 | } else { | |
247 | break; | |
248 | } | |
249 | }, | |
250 | NATimePoint::PTS(pts) => { | |
251 | if entry.pts <= pts { | |
252 | cand = Some(*entry); | |
253 | } else { | |
254 | break; | |
255 | } | |
256 | }, | |
257 | NATimePoint::None => unreachable!(), | |
258 | }; | |
33b5a8f0 | 259 | } |
4cfb5dd4 | 260 | cand |
33b5a8f0 KS |
261 | } else { |
262 | None | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
ab683361 | 267 | /// Structure for holding seeking point search results. |
33b5a8f0 KS |
268 | #[derive(Clone,Copy,Default)] |
269 | pub struct SeekIndexResult { | |
ab683361 | 270 | /// Packet PTS. |
33b5a8f0 | 271 | pub pts: u64, |
ab683361 | 272 | /// Position in file. |
33b5a8f0 | 273 | pub pos: u64, |
ab683361 | 274 | /// Stream ID. |
33b5a8f0 KS |
275 | pub str_id: u32, |
276 | } | |
277 | ||
ab683361 | 278 | /// Seek information for the whole container. |
33b5a8f0 KS |
279 | #[derive(Default)] |
280 | pub struct SeekIndex { | |
ab683361 | 281 | /// Seek information for individual streams. |
33b5a8f0 | 282 | pub seek_info: Vec<StreamSeekInfo>, |
ab683361 | 283 | /// Seeking index mode. |
33b5a8f0 | 284 | pub mode: SeekIndexMode, |
ab683361 | 285 | /// Ignore index flag. |
9da33f04 | 286 | pub skip_index: bool, |
33b5a8f0 KS |
287 | } |
288 | ||
289 | impl SeekIndex { | |
ab683361 | 290 | /// Constructs a new `SeekIndex` instance. |
33b5a8f0 | 291 | pub fn new() -> Self { Self::default() } |
6492988d KS |
292 | pub fn add_stream(&mut self, id: u32) -> usize { |
293 | let ret = self.stream_id_to_index(id); | |
b7c882c1 KS |
294 | if let Some(res) = ret { |
295 | res | |
296 | } else { | |
4cfb5dd4 | 297 | self.seek_info.push(StreamSeekInfo::new(id)); |
6492988d | 298 | self.seek_info.len() - 1 |
33b5a8f0 KS |
299 | } |
300 | } | |
ab683361 | 301 | /// Adds a new stream to the index. |
33b5a8f0 KS |
302 | pub fn stream_id_to_index(&self, id: u32) -> Option<usize> { |
303 | for (idx, str) in self.seek_info.iter().enumerate() { | |
304 | if str.id == id { | |
305 | return Some(idx); | |
306 | } | |
307 | } | |
308 | None | |
309 | } | |
ab683361 | 310 | /// Returns stream reference for provided stream ID. |
4cfb5dd4 | 311 | pub fn get_stream_index(&mut self, id: u32) -> Option<&mut StreamSeekInfo> { |
817e4872 KS |
312 | for stream in self.seek_info.iter_mut() { |
313 | if stream.id == id { | |
314 | return Some(stream); | |
4cfb5dd4 KS |
315 | } |
316 | } | |
317 | None | |
318 | } | |
ab683361 | 319 | /// Adds seeking information to the index. |
6492988d KS |
320 | pub fn add_entry(&mut self, id: u32, entry: SeekEntry) { |
321 | let mut idx = self.stream_id_to_index(id); | |
322 | if idx.is_none() { | |
323 | idx = Some(self.add_stream(id)); | |
324 | } | |
325 | self.seek_info[idx.unwrap()].add_entry(entry); | |
326 | self.seek_info[idx.unwrap()].filled = true; | |
327 | } | |
ab683361 | 328 | /// Searches for a seek position before requested time. |
24d99894 | 329 | pub fn find_pos(&self, time: NATimePoint) -> Option<SeekIndexResult> { |
33b5a8f0 | 330 | let mut cand = None; |
817e4872 KS |
331 | for stream in self.seek_info.iter() { |
332 | if !stream.filled { continue; } | |
333 | let res = stream.find_pos(time); | |
4cfb5dd4 KS |
334 | if res.is_none() { continue; } |
335 | let res = res.unwrap(); | |
33b5a8f0 | 336 | if cand.is_none() { |
817e4872 | 337 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: stream.id }); |
33b5a8f0 | 338 | } else if let Some(entry) = cand { |
4cfb5dd4 | 339 | if res.pos < entry.pos { |
817e4872 | 340 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: stream.id }); |
33b5a8f0 KS |
341 | } |
342 | } | |
343 | } | |
344 | cand | |
345 | } | |
346 | } | |
347 | ||
ab683361 | 348 | /// Demuxer structure with auxiliary data. |
bcfeae48 | 349 | pub struct Demuxer<'a> { |
6011e201 | 350 | dmx: Box<dyn DemuxCore<'a> + 'a>, |
bcfeae48 | 351 | streams: StreamManager, |
33b5a8f0 | 352 | seek_idx: SeekIndex, |
bcfeae48 KS |
353 | } |
354 | ||
355 | impl<'a> Demuxer<'a> { | |
ab683361 | 356 | /// Constructs a new `Demuxer` instance. |
33b5a8f0 | 357 | fn new(dmx: Box<dyn DemuxCore<'a> + 'a>, str: StreamManager, seek_idx: SeekIndex) -> Self { |
bcfeae48 | 358 | Demuxer { |
e243ceb4 | 359 | dmx, |
bcfeae48 | 360 | streams: str, |
33b5a8f0 | 361 | seek_idx, |
bcfeae48 KS |
362 | } |
363 | } | |
ab683361 | 364 | /// Returns a stream reference by its number. |
70910ac3 | 365 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
bcfeae48 KS |
366 | self.streams.get_stream(idx) |
367 | } | |
ab683361 | 368 | /// Returns a stream reference by its ID. |
70910ac3 | 369 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
bcfeae48 KS |
370 | self.streams.get_stream_by_id(id) |
371 | } | |
ab683361 | 372 | /// Reports the total number of streams. |
bcfeae48 KS |
373 | pub fn get_num_streams(&self) -> usize { |
374 | self.streams.get_num_streams() | |
375 | } | |
cacc0c44 KS |
376 | /// Returns a reference to the internal stream manager. |
377 | pub fn get_stream_manager(&self) -> &StreamManager { | |
378 | &self.streams | |
379 | } | |
ab683361 | 380 | /// Returns an iterator over streams. |
bcfeae48 KS |
381 | pub fn get_streams(&self) -> StreamIter { |
382 | self.streams.iter() | |
383 | } | |
ab683361 | 384 | /// Returns 'ignored' marker for requested stream. |
bcfeae48 KS |
385 | pub fn is_ignored_stream(&self, idx: usize) -> bool { |
386 | self.streams.is_ignored(idx) | |
387 | } | |
ab683361 | 388 | /// Sets 'ignored' marker for requested stream. |
bcfeae48 KS |
389 | pub fn set_ignored_stream(&mut self, idx: usize) { |
390 | self.streams.set_ignored(idx) | |
391 | } | |
ab683361 | 392 | /// Clears 'ignored' marker for requested stream. |
bcfeae48 KS |
393 | pub fn set_unignored_stream(&mut self, idx: usize) { |
394 | self.streams.set_unignored(idx) | |
395 | } | |
396 | ||
ab683361 | 397 | /// Demuxes a new packet from the container. |
bcfeae48 KS |
398 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { |
399 | loop { | |
400 | let res = self.dmx.get_frame(&mut self.streams); | |
401 | if self.streams.no_ign || res.is_err() { return res; } | |
402 | let res = res.unwrap(); | |
403 | let idx = res.get_stream().get_num(); | |
404 | if !self.is_ignored_stream(idx) { | |
405 | return Ok(res); | |
406 | } | |
407 | } | |
408 | } | |
24d99894 KS |
409 | /// Seeks to the requested time if possible. |
410 | pub fn seek(&mut self, time: NATimePoint) -> DemuxerResult<()> { | |
9da33f04 KS |
411 | if self.seek_idx.skip_index { |
412 | return Err(DemuxerError::NotPossible); | |
413 | } | |
33b5a8f0 KS |
414 | self.dmx.seek(time, &self.seek_idx) |
415 | } | |
ab683361 | 416 | /// Returns internal seek index. |
33b5a8f0 KS |
417 | pub fn get_seek_index(&self) -> &SeekIndex { |
418 | &self.seek_idx | |
bcfeae48 | 419 | } |
a480a0de KS |
420 | /// Returns media duration reported by container or its streams. |
421 | /// | |
422 | /// Duration is in milliseconds and set to zero when it is not available. | |
423 | pub fn get_duration(&self) -> u64 { | |
424 | let duration = self.dmx.get_duration(); | |
425 | if duration != 0 { | |
426 | return duration; | |
427 | } | |
428 | let mut duration = 0; | |
429 | for stream in self.streams.iter() { | |
430 | if stream.duration > 0 { | |
431 | let dur = NATimeInfo::ts_to_time(stream.duration, 1000, stream.tb_num, stream.tb_den); | |
432 | if duration < dur { | |
433 | duration = dur; | |
434 | } | |
435 | } | |
436 | } | |
437 | duration | |
438 | } | |
5869fd63 KS |
439 | } |
440 | ||
787b8d03 KS |
441 | impl<'a> NAOptionHandler for Demuxer<'a> { |
442 | fn get_supported_options(&self) -> &[NAOptionDefinition] { | |
443 | self.dmx.get_supported_options() | |
444 | } | |
445 | fn set_options(&mut self, options: &[NAOption]) { | |
446 | self.dmx.set_options(options); | |
447 | } | |
448 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
449 | self.dmx.query_option_value(name) | |
450 | } | |
451 | } | |
452 | ||
5869fd63 KS |
453 | impl From<ByteIOError> for DemuxerError { |
454 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } | |
455 | } | |
456 | ||
ab683361 | 457 | /// The trait for creating demuxers. |
eb71d98f | 458 | pub trait DemuxerCreator { |
ab683361 | 459 | /// Creates new demuxer instance that will use `ByteReader` source as an input. |
6011e201 | 460 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a>; |
ab683361 | 461 | /// Returns the name of current demuxer creator (equal to the container name it can demux). |
eb71d98f KS |
462 | fn get_name(&self) -> &'static str; |
463 | } | |
464 | ||
ab683361 | 465 | /// Creates demuxer for a provided bytestream. |
ac818eac | 466 | pub fn create_demuxer<'a>(dmxcr: &dyn DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { |
bcfeae48 | 467 | let mut dmx = dmxcr.new_demuxer(br); |
817e4872 | 468 | let mut strmgr = StreamManager::new(); |
33b5a8f0 | 469 | let mut seek_idx = SeekIndex::new(); |
817e4872 KS |
470 | dmx.open(&mut strmgr, &mut seek_idx)?; |
471 | Ok(Demuxer::new(dmx, strmgr, seek_idx)) | |
bcfeae48 | 472 | } |
5641dccf | 473 | |
534a5b60 KS |
474 | /// Creates demuxer for a provided bytestream with options applied right after its creation. |
475 | pub fn create_demuxer_with_options<'a>(dmxcr: &dyn DemuxerCreator, br: &'a mut ByteReader<'a>, opts: &[NAOption]) -> DemuxerResult<Demuxer<'a>> { | |
476 | let mut dmx = dmxcr.new_demuxer(br); | |
477 | dmx.set_options(opts); | |
478 | let mut strmgr = StreamManager::new(); | |
479 | let mut seek_idx = SeekIndex::new(); | |
480 | dmx.open(&mut strmgr, &mut seek_idx)?; | |
481 | Ok(Demuxer::new(dmx, strmgr, seek_idx)) | |
482 | } | |
483 | ||
ab683361 | 484 | /// List of registered demuxers. |
e243ceb4 | 485 | #[derive(Default)] |
5641dccf | 486 | pub struct RegisteredDemuxers { |
ac818eac | 487 | dmxs: Vec<&'static dyn DemuxerCreator>, |
5641dccf KS |
488 | } |
489 | ||
490 | impl RegisteredDemuxers { | |
ab683361 | 491 | /// Constructs a new `RegisteredDemuxers` instance. |
5641dccf KS |
492 | pub fn new() -> Self { |
493 | Self { dmxs: Vec::new() } | |
494 | } | |
ab683361 | 495 | /// Registers a new demuxer. |
ac818eac | 496 | pub fn add_demuxer(&mut self, dmx: &'static dyn DemuxerCreator) { |
5641dccf KS |
497 | self.dmxs.push(dmx); |
498 | } | |
ab683361 | 499 | /// Searches for a demuxer that supports requested container format. |
ac818eac | 500 | pub fn find_demuxer(&self, name: &str) -> Option<&dyn DemuxerCreator> { |
5641dccf KS |
501 | for &dmx in self.dmxs.iter() { |
502 | if dmx.get_name() == name { | |
503 | return Some(dmx); | |
504 | } | |
505 | } | |
506 | None | |
507 | } | |
cec53b88 | 508 | /// Provides an iterator over currently registered demuxers. |
ac818eac | 509 | pub fn iter(&self) -> std::slice::Iter<&dyn DemuxerCreator> { |
cec53b88 KS |
510 | self.dmxs.iter() |
511 | } | |
1a967e6b | 512 | } |
1ebf572d KS |
513 | |
514 | /// A trait for raw data demuxing operations. | |
515 | pub trait RawDemuxCore<'a>: NAOptionHandler { | |
516 | /// Opens the input stream, reads required headers and prepares everything for packet demuxing. | |
517 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; | |
518 | /// Reads a piece of raw data. | |
519 | fn get_data(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NARawData>; | |
520 | /// Seeks to the requested time. | |
521 | fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()>; | |
522 | /// Returns container duration in milliseconds (zero if not available). | |
523 | fn get_duration(&self) -> u64; | |
524 | } | |
525 | ||
526 | /// Demuxer structure with auxiliary data. | |
527 | pub struct RawDemuxer<'a> { | |
528 | dmx: Box<dyn RawDemuxCore<'a> + 'a>, | |
529 | streams: StreamManager, | |
530 | seek_idx: SeekIndex, | |
531 | } | |
532 | ||
533 | impl<'a> RawDemuxer<'a> { | |
534 | /// Constructs a new `Demuxer` instance. | |
535 | fn new(dmx: Box<dyn RawDemuxCore<'a> + 'a>, strmgr: StreamManager, seek_idx: SeekIndex) -> Self { | |
536 | Self { | |
537 | dmx, | |
538 | streams: strmgr, | |
539 | seek_idx, | |
540 | } | |
541 | } | |
542 | /// Returns a stream reference by its number. | |
543 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { | |
544 | self.streams.get_stream(idx) | |
545 | } | |
546 | /// Returns a stream reference by its ID. | |
547 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { | |
548 | self.streams.get_stream_by_id(id) | |
549 | } | |
550 | /// Reports the total number of streams. | |
551 | pub fn get_num_streams(&self) -> usize { | |
552 | self.streams.get_num_streams() | |
553 | } | |
554 | /// Returns a reference to the internal stream manager. | |
555 | pub fn get_stream_manager(&self) -> &StreamManager { | |
556 | &self.streams | |
557 | } | |
558 | /// Returns an iterator over streams. | |
559 | pub fn get_streams(&self) -> StreamIter { | |
560 | self.streams.iter() | |
561 | } | |
562 | /// Returns 'ignored' marker for requested stream. | |
563 | pub fn is_ignored_stream(&self, idx: usize) -> bool { | |
564 | self.streams.is_ignored(idx) | |
565 | } | |
566 | /// Sets 'ignored' marker for requested stream. | |
567 | pub fn set_ignored_stream(&mut self, idx: usize) { | |
568 | self.streams.set_ignored(idx) | |
569 | } | |
570 | /// Clears 'ignored' marker for requested stream. | |
571 | pub fn set_unignored_stream(&mut self, idx: usize) { | |
572 | self.streams.set_unignored(idx) | |
573 | } | |
574 | ||
575 | /// Demuxes a new piece of data from the container. | |
576 | pub fn get_data(&mut self) -> DemuxerResult<NARawData> { | |
577 | loop { | |
578 | let res = self.dmx.get_data(&mut self.streams); | |
579 | if self.streams.no_ign || res.is_err() { return res; } | |
580 | let res = res.unwrap(); | |
581 | let idx = res.get_stream().get_num(); | |
582 | if !self.is_ignored_stream(idx) { | |
583 | return Ok(res); | |
584 | } | |
585 | } | |
586 | } | |
587 | /// Seeks to the requested time if possible. | |
588 | pub fn seek(&mut self, time: NATimePoint) -> DemuxerResult<()> { | |
589 | if self.seek_idx.skip_index { | |
590 | return Err(DemuxerError::NotPossible); | |
591 | } | |
592 | self.dmx.seek(time, &self.seek_idx) | |
593 | } | |
594 | /// Returns internal seek index. | |
595 | pub fn get_seek_index(&self) -> &SeekIndex { | |
596 | &self.seek_idx | |
597 | } | |
598 | /// Returns media duration reported by container or its streams. | |
599 | /// | |
600 | /// Duration is in milliseconds and set to zero when it is not available. | |
601 | pub fn get_duration(&self) -> u64 { | |
602 | let duration = self.dmx.get_duration(); | |
603 | if duration != 0 { | |
604 | return duration; | |
605 | } | |
606 | let mut duration = 0; | |
607 | for stream in self.streams.iter() { | |
608 | if stream.duration > 0 { | |
609 | let dur = NATimeInfo::ts_to_time(stream.duration, 1000, stream.tb_num, stream.tb_den); | |
610 | if duration < dur { | |
611 | duration = dur; | |
612 | } | |
613 | } | |
614 | } | |
615 | duration | |
616 | } | |
617 | } | |
618 | ||
619 | impl<'a> NAOptionHandler for RawDemuxer<'a> { | |
620 | fn get_supported_options(&self) -> &[NAOptionDefinition] { | |
621 | self.dmx.get_supported_options() | |
622 | } | |
623 | fn set_options(&mut self, options: &[NAOption]) { | |
624 | self.dmx.set_options(options); | |
625 | } | |
626 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
627 | self.dmx.query_option_value(name) | |
628 | } | |
629 | } | |
630 | ||
631 | /// The trait for creating raw data demuxers. | |
632 | pub trait RawDemuxerCreator { | |
633 | /// Creates new raw demuxer instance that will use `ByteReader` source as an input. | |
634 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn RawDemuxCore<'a> + 'a>; | |
635 | /// Tries to check whether the input can be demuxed with the demuxer. | |
636 | fn check_format(&self, br: &mut ByteReader) -> bool; | |
637 | /// Returns the name of current raw data demuxer creator (equal to the container name it can demux). | |
638 | fn get_name(&self) -> &'static str; | |
639 | } | |
640 | ||
641 | /// Creates raw data demuxer for a provided bytestream. | |
642 | pub fn create_raw_demuxer<'a>(dmxcr: &dyn RawDemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<RawDemuxer<'a>> { | |
643 | let mut dmx = dmxcr.new_demuxer(br); | |
817e4872 | 644 | let mut strmgr = StreamManager::new(); |
1ebf572d | 645 | let mut seek_idx = SeekIndex::new(); |
534a5b60 KS |
646 | dmx.open(&mut strmgr, &mut seek_idx)?; |
647 | Ok(RawDemuxer::new(dmx, strmgr, seek_idx)) | |
648 | } | |
649 | ||
650 | /// Creates raw data demuxer for a provided bytestream with options applied right after its creation. | |
651 | pub fn create_raw_demuxer_with_options<'a>(dmxcr: &dyn RawDemuxerCreator, br: &'a mut ByteReader<'a>, opts: &[NAOption]) -> DemuxerResult<RawDemuxer<'a>> { | |
652 | let mut dmx = dmxcr.new_demuxer(br); | |
653 | dmx.set_options(opts); | |
654 | let mut strmgr = StreamManager::new(); | |
655 | let mut seek_idx = SeekIndex::new(); | |
817e4872 KS |
656 | dmx.open(&mut strmgr, &mut seek_idx)?; |
657 | Ok(RawDemuxer::new(dmx, strmgr, seek_idx)) | |
1ebf572d KS |
658 | } |
659 | ||
660 | /// List of registered demuxers. | |
661 | #[derive(Default)] | |
662 | pub struct RegisteredRawDemuxers { | |
663 | dmxs: Vec<&'static dyn RawDemuxerCreator>, | |
664 | } | |
665 | ||
666 | impl RegisteredRawDemuxers { | |
667 | /// Constructs a new `RegisteredDemuxers` instance. | |
668 | pub fn new() -> Self { | |
669 | Self { dmxs: Vec::new() } | |
670 | } | |
671 | /// Registers a new demuxer. | |
672 | pub fn add_demuxer(&mut self, dmx: &'static dyn RawDemuxerCreator) { | |
673 | self.dmxs.push(dmx); | |
674 | } | |
675 | /// Searches for a demuxer that supports requested container format. | |
676 | pub fn find_demuxer(&self, name: &str) -> Option<&dyn RawDemuxerCreator> { | |
677 | for &dmx in self.dmxs.iter() { | |
678 | if dmx.get_name() == name { | |
679 | return Some(dmx); | |
680 | } | |
681 | } | |
682 | None | |
683 | } | |
684 | /// Provides an iterator over currently registered demuxers. | |
685 | pub fn iter(&self) -> std::slice::Iter<&dyn RawDemuxerCreator> { | |
686 | self.dmxs.iter() | |
687 | } | |
688 | } |