]>
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. |
405cec9e | 48 | fn read_packet(&mut self, strm: 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> { |
405cec9e | 54 | fn read_packet(&mut self, strm: 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())?; |
405cec9e | 59 | let pkt = NAPacket::new(strm, 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. |
e6aaad5c | 184 | #[derive(Clone,Copy,PartialEq,Default)] |
33b5a8f0 | 185 | pub enum SeekIndexMode { |
ab683361 | 186 | /// No seeking index present. |
e6aaad5c | 187 | #[default] |
33b5a8f0 | 188 | None, |
ab683361 | 189 | /// Seeking index is present. |
33b5a8f0 | 190 | Present, |
ab683361 | 191 | /// Seeking index should be constructed by the demuxer if possible. |
33b5a8f0 KS |
192 | Automatic, |
193 | } | |
194 | ||
ab683361 | 195 | /// A structure holding seeking information. |
33b5a8f0 KS |
196 | #[derive(Clone,Copy,Default)] |
197 | pub struct SeekEntry { | |
ab683361 KS |
198 | /// Time in milliseconds. |
199 | pub time: u64, | |
200 | /// PTS | |
33b5a8f0 | 201 | pub pts: u64, |
ab683361 | 202 | /// Position in file. |
33b5a8f0 KS |
203 | pub pos: u64, |
204 | } | |
205 | ||
ab683361 | 206 | /// Seeking information for individual streams. |
33b5a8f0 KS |
207 | #[derive(Clone)] |
208 | pub struct StreamSeekInfo { | |
ab683361 | 209 | /// Stream ID. |
33b5a8f0 | 210 | pub id: u32, |
ab683361 | 211 | /// Index is present. |
33b5a8f0 | 212 | pub filled: bool, |
ab683361 | 213 | /// Packet seeking information. |
33b5a8f0 KS |
214 | pub entries: Vec<SeekEntry>, |
215 | } | |
216 | ||
217 | impl StreamSeekInfo { | |
ab683361 | 218 | /// Constructs a new `StreamSeekInfo` instance. |
4cfb5dd4 | 219 | pub fn new(id: u32) -> Self { |
33b5a8f0 | 220 | Self { |
4cfb5dd4 | 221 | id, |
33b5a8f0 KS |
222 | filled: false, |
223 | entries: Vec::new(), | |
224 | } | |
225 | } | |
ab683361 | 226 | /// Adds new seeking point. |
33b5a8f0 KS |
227 | pub fn add_entry(&mut self, entry: SeekEntry) { |
228 | self.entries.push(entry); | |
229 | } | |
ab683361 | 230 | /// Searches for an appropriate seek position before requested time. |
24d99894 KS |
231 | pub fn find_pos(&self, time: NATimePoint) -> Option<SeekEntry> { |
232 | if time == NATimePoint::None { | |
233 | return None; | |
234 | } | |
33b5a8f0 KS |
235 | if !self.entries.is_empty() { |
236 | // todo something faster like binary search | |
4cfb5dd4 KS |
237 | let mut cand = None; |
238 | for entry in self.entries.iter() { | |
24d99894 KS |
239 | match time { |
240 | NATimePoint::Milliseconds(ms) => { | |
241 | if entry.time <= ms { | |
242 | cand = Some(*entry); | |
243 | } else { | |
244 | break; | |
245 | } | |
246 | }, | |
247 | NATimePoint::PTS(pts) => { | |
248 | if entry.pts <= pts { | |
249 | cand = Some(*entry); | |
250 | } else { | |
251 | break; | |
252 | } | |
253 | }, | |
254 | NATimePoint::None => unreachable!(), | |
255 | }; | |
33b5a8f0 | 256 | } |
4cfb5dd4 | 257 | cand |
33b5a8f0 KS |
258 | } else { |
259 | None | |
260 | } | |
261 | } | |
262 | } | |
263 | ||
ab683361 | 264 | /// Structure for holding seeking point search results. |
33b5a8f0 KS |
265 | #[derive(Clone,Copy,Default)] |
266 | pub struct SeekIndexResult { | |
ab683361 | 267 | /// Packet PTS. |
33b5a8f0 | 268 | pub pts: u64, |
ab683361 | 269 | /// Position in file. |
33b5a8f0 | 270 | pub pos: u64, |
ab683361 | 271 | /// Stream ID. |
33b5a8f0 KS |
272 | pub str_id: u32, |
273 | } | |
274 | ||
ab683361 | 275 | /// Seek information for the whole container. |
33b5a8f0 KS |
276 | #[derive(Default)] |
277 | pub struct SeekIndex { | |
ab683361 | 278 | /// Seek information for individual streams. |
33b5a8f0 | 279 | pub seek_info: Vec<StreamSeekInfo>, |
ab683361 | 280 | /// Seeking index mode. |
33b5a8f0 | 281 | pub mode: SeekIndexMode, |
ab683361 | 282 | /// Ignore index flag. |
9da33f04 | 283 | pub skip_index: bool, |
33b5a8f0 KS |
284 | } |
285 | ||
286 | impl SeekIndex { | |
ab683361 | 287 | /// Constructs a new `SeekIndex` instance. |
33b5a8f0 | 288 | pub fn new() -> Self { Self::default() } |
6492988d KS |
289 | pub fn add_stream(&mut self, id: u32) -> usize { |
290 | let ret = self.stream_id_to_index(id); | |
b7c882c1 KS |
291 | if let Some(res) = ret { |
292 | res | |
293 | } else { | |
4cfb5dd4 | 294 | self.seek_info.push(StreamSeekInfo::new(id)); |
6492988d | 295 | self.seek_info.len() - 1 |
33b5a8f0 KS |
296 | } |
297 | } | |
ab683361 | 298 | /// Adds a new stream to the index. |
33b5a8f0 | 299 | pub fn stream_id_to_index(&self, id: u32) -> Option<usize> { |
405cec9e KS |
300 | for (idx, strm) in self.seek_info.iter().enumerate() { |
301 | if strm.id == id { | |
33b5a8f0 KS |
302 | return Some(idx); |
303 | } | |
304 | } | |
305 | None | |
306 | } | |
ab683361 | 307 | /// Returns stream reference for provided stream ID. |
4cfb5dd4 | 308 | pub fn get_stream_index(&mut self, id: u32) -> Option<&mut StreamSeekInfo> { |
e6aaad5c | 309 | self.seek_info.iter_mut().find(|stream| stream.id == id) |
4cfb5dd4 | 310 | } |
ab683361 | 311 | /// Adds seeking information to the index. |
6492988d KS |
312 | pub fn add_entry(&mut self, id: u32, entry: SeekEntry) { |
313 | let mut idx = self.stream_id_to_index(id); | |
314 | if idx.is_none() { | |
315 | idx = Some(self.add_stream(id)); | |
316 | } | |
317 | self.seek_info[idx.unwrap()].add_entry(entry); | |
318 | self.seek_info[idx.unwrap()].filled = true; | |
319 | } | |
ab683361 | 320 | /// Searches for a seek position before requested time. |
24d99894 | 321 | pub fn find_pos(&self, time: NATimePoint) -> Option<SeekIndexResult> { |
33b5a8f0 | 322 | let mut cand = None; |
817e4872 KS |
323 | for stream in self.seek_info.iter() { |
324 | if !stream.filled { continue; } | |
325 | let res = stream.find_pos(time); | |
4cfb5dd4 KS |
326 | if res.is_none() { continue; } |
327 | let res = res.unwrap(); | |
33b5a8f0 | 328 | if cand.is_none() { |
817e4872 | 329 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: stream.id }); |
33b5a8f0 | 330 | } else if let Some(entry) = cand { |
4cfb5dd4 | 331 | if res.pos < entry.pos { |
817e4872 | 332 | cand = Some(SeekIndexResult { pts: res.pts, pos: res.pos, str_id: stream.id }); |
33b5a8f0 KS |
333 | } |
334 | } | |
335 | } | |
336 | cand | |
337 | } | |
338 | } | |
339 | ||
ab683361 | 340 | /// Demuxer structure with auxiliary data. |
bcfeae48 | 341 | pub struct Demuxer<'a> { |
6011e201 | 342 | dmx: Box<dyn DemuxCore<'a> + 'a>, |
bcfeae48 | 343 | streams: StreamManager, |
33b5a8f0 | 344 | seek_idx: SeekIndex, |
bcfeae48 KS |
345 | } |
346 | ||
347 | impl<'a> Demuxer<'a> { | |
ab683361 | 348 | /// Constructs a new `Demuxer` instance. |
405cec9e | 349 | fn new(dmx: Box<dyn DemuxCore<'a> + 'a>, strmgr: StreamManager, seek_idx: SeekIndex) -> Self { |
bcfeae48 | 350 | Demuxer { |
e243ceb4 | 351 | dmx, |
405cec9e | 352 | streams: strmgr, |
33b5a8f0 | 353 | seek_idx, |
bcfeae48 KS |
354 | } |
355 | } | |
ab683361 | 356 | /// Returns a stream reference by its number. |
70910ac3 | 357 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { |
bcfeae48 KS |
358 | self.streams.get_stream(idx) |
359 | } | |
ab683361 | 360 | /// Returns a stream reference by its ID. |
70910ac3 | 361 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { |
bcfeae48 KS |
362 | self.streams.get_stream_by_id(id) |
363 | } | |
ab683361 | 364 | /// Reports the total number of streams. |
bcfeae48 KS |
365 | pub fn get_num_streams(&self) -> usize { |
366 | self.streams.get_num_streams() | |
367 | } | |
cacc0c44 KS |
368 | /// Returns a reference to the internal stream manager. |
369 | pub fn get_stream_manager(&self) -> &StreamManager { | |
370 | &self.streams | |
371 | } | |
ab683361 | 372 | /// Returns an iterator over streams. |
bcfeae48 KS |
373 | pub fn get_streams(&self) -> StreamIter { |
374 | self.streams.iter() | |
375 | } | |
ab683361 | 376 | /// Returns 'ignored' marker for requested stream. |
bcfeae48 KS |
377 | pub fn is_ignored_stream(&self, idx: usize) -> bool { |
378 | self.streams.is_ignored(idx) | |
379 | } | |
ab683361 | 380 | /// Sets 'ignored' marker for requested stream. |
bcfeae48 KS |
381 | pub fn set_ignored_stream(&mut self, idx: usize) { |
382 | self.streams.set_ignored(idx) | |
383 | } | |
ab683361 | 384 | /// Clears 'ignored' marker for requested stream. |
bcfeae48 KS |
385 | pub fn set_unignored_stream(&mut self, idx: usize) { |
386 | self.streams.set_unignored(idx) | |
387 | } | |
388 | ||
ab683361 | 389 | /// Demuxes a new packet from the container. |
bcfeae48 KS |
390 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { |
391 | loop { | |
392 | let res = self.dmx.get_frame(&mut self.streams); | |
393 | if self.streams.no_ign || res.is_err() { return res; } | |
394 | let res = res.unwrap(); | |
395 | let idx = res.get_stream().get_num(); | |
396 | if !self.is_ignored_stream(idx) { | |
397 | return Ok(res); | |
398 | } | |
399 | } | |
400 | } | |
24d99894 KS |
401 | /// Seeks to the requested time if possible. |
402 | pub fn seek(&mut self, time: NATimePoint) -> DemuxerResult<()> { | |
9da33f04 KS |
403 | if self.seek_idx.skip_index { |
404 | return Err(DemuxerError::NotPossible); | |
405 | } | |
33b5a8f0 KS |
406 | self.dmx.seek(time, &self.seek_idx) |
407 | } | |
ab683361 | 408 | /// Returns internal seek index. |
33b5a8f0 KS |
409 | pub fn get_seek_index(&self) -> &SeekIndex { |
410 | &self.seek_idx | |
bcfeae48 | 411 | } |
a480a0de KS |
412 | /// Returns media duration reported by container or its streams. |
413 | /// | |
414 | /// Duration is in milliseconds and set to zero when it is not available. | |
415 | pub fn get_duration(&self) -> u64 { | |
416 | let duration = self.dmx.get_duration(); | |
417 | if duration != 0 { | |
418 | return duration; | |
419 | } | |
420 | let mut duration = 0; | |
421 | for stream in self.streams.iter() { | |
422 | if stream.duration > 0 { | |
423 | let dur = NATimeInfo::ts_to_time(stream.duration, 1000, stream.tb_num, stream.tb_den); | |
424 | if duration < dur { | |
425 | duration = dur; | |
426 | } | |
427 | } | |
428 | } | |
429 | duration | |
430 | } | |
5869fd63 KS |
431 | } |
432 | ||
787b8d03 KS |
433 | impl<'a> NAOptionHandler for Demuxer<'a> { |
434 | fn get_supported_options(&self) -> &[NAOptionDefinition] { | |
435 | self.dmx.get_supported_options() | |
436 | } | |
437 | fn set_options(&mut self, options: &[NAOption]) { | |
438 | self.dmx.set_options(options); | |
439 | } | |
440 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
441 | self.dmx.query_option_value(name) | |
442 | } | |
443 | } | |
444 | ||
5869fd63 KS |
445 | impl From<ByteIOError> for DemuxerError { |
446 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } | |
447 | } | |
448 | ||
ab683361 | 449 | /// The trait for creating demuxers. |
eb71d98f | 450 | pub trait DemuxerCreator { |
ab683361 | 451 | /// Creates new demuxer instance that will use `ByteReader` source as an input. |
6011e201 | 452 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn DemuxCore<'a> + 'a>; |
ab683361 | 453 | /// Returns the name of current demuxer creator (equal to the container name it can demux). |
eb71d98f KS |
454 | fn get_name(&self) -> &'static str; |
455 | } | |
456 | ||
ab683361 | 457 | /// Creates demuxer for a provided bytestream. |
ac818eac | 458 | pub fn create_demuxer<'a>(dmxcr: &dyn DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { |
bcfeae48 | 459 | let mut dmx = dmxcr.new_demuxer(br); |
817e4872 | 460 | let mut strmgr = StreamManager::new(); |
33b5a8f0 | 461 | let mut seek_idx = SeekIndex::new(); |
817e4872 KS |
462 | dmx.open(&mut strmgr, &mut seek_idx)?; |
463 | Ok(Demuxer::new(dmx, strmgr, seek_idx)) | |
bcfeae48 | 464 | } |
5641dccf | 465 | |
534a5b60 KS |
466 | /// Creates demuxer for a provided bytestream with options applied right after its creation. |
467 | pub fn create_demuxer_with_options<'a>(dmxcr: &dyn DemuxerCreator, br: &'a mut ByteReader<'a>, opts: &[NAOption]) -> DemuxerResult<Demuxer<'a>> { | |
468 | let mut dmx = dmxcr.new_demuxer(br); | |
469 | dmx.set_options(opts); | |
470 | let mut strmgr = StreamManager::new(); | |
471 | let mut seek_idx = SeekIndex::new(); | |
472 | dmx.open(&mut strmgr, &mut seek_idx)?; | |
473 | Ok(Demuxer::new(dmx, strmgr, seek_idx)) | |
474 | } | |
475 | ||
ab683361 | 476 | /// List of registered demuxers. |
e243ceb4 | 477 | #[derive(Default)] |
5641dccf | 478 | pub struct RegisteredDemuxers { |
ac818eac | 479 | dmxs: Vec<&'static dyn DemuxerCreator>, |
5641dccf KS |
480 | } |
481 | ||
482 | impl RegisteredDemuxers { | |
ab683361 | 483 | /// Constructs a new `RegisteredDemuxers` instance. |
5641dccf KS |
484 | pub fn new() -> Self { |
485 | Self { dmxs: Vec::new() } | |
486 | } | |
ab683361 | 487 | /// Registers a new demuxer. |
ac818eac | 488 | pub fn add_demuxer(&mut self, dmx: &'static dyn DemuxerCreator) { |
5641dccf KS |
489 | self.dmxs.push(dmx); |
490 | } | |
ab683361 | 491 | /// Searches for a demuxer that supports requested container format. |
ac818eac | 492 | pub fn find_demuxer(&self, name: &str) -> Option<&dyn DemuxerCreator> { |
e6aaad5c | 493 | self.dmxs.iter().find(|&&dmx| dmx.get_name() == name).copied() |
5641dccf | 494 | } |
cec53b88 | 495 | /// Provides an iterator over currently registered demuxers. |
ac818eac | 496 | pub fn iter(&self) -> std::slice::Iter<&dyn DemuxerCreator> { |
cec53b88 KS |
497 | self.dmxs.iter() |
498 | } | |
1a967e6b | 499 | } |
1ebf572d KS |
500 | |
501 | /// A trait for raw data demuxing operations. | |
502 | pub trait RawDemuxCore<'a>: NAOptionHandler { | |
503 | /// Opens the input stream, reads required headers and prepares everything for packet demuxing. | |
504 | fn open(&mut self, strmgr: &mut StreamManager, seek_idx: &mut SeekIndex) -> DemuxerResult<()>; | |
505 | /// Reads a piece of raw data. | |
506 | fn get_data(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NARawData>; | |
507 | /// Seeks to the requested time. | |
508 | fn seek(&mut self, time: NATimePoint, seek_idx: &SeekIndex) -> DemuxerResult<()>; | |
509 | /// Returns container duration in milliseconds (zero if not available). | |
510 | fn get_duration(&self) -> u64; | |
511 | } | |
512 | ||
513 | /// Demuxer structure with auxiliary data. | |
514 | pub struct RawDemuxer<'a> { | |
515 | dmx: Box<dyn RawDemuxCore<'a> + 'a>, | |
516 | streams: StreamManager, | |
517 | seek_idx: SeekIndex, | |
518 | } | |
519 | ||
520 | impl<'a> RawDemuxer<'a> { | |
521 | /// Constructs a new `Demuxer` instance. | |
522 | fn new(dmx: Box<dyn RawDemuxCore<'a> + 'a>, strmgr: StreamManager, seek_idx: SeekIndex) -> Self { | |
523 | Self { | |
524 | dmx, | |
525 | streams: strmgr, | |
526 | seek_idx, | |
527 | } | |
528 | } | |
529 | /// Returns a stream reference by its number. | |
530 | pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> { | |
531 | self.streams.get_stream(idx) | |
532 | } | |
533 | /// Returns a stream reference by its ID. | |
534 | pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> { | |
535 | self.streams.get_stream_by_id(id) | |
536 | } | |
537 | /// Reports the total number of streams. | |
538 | pub fn get_num_streams(&self) -> usize { | |
539 | self.streams.get_num_streams() | |
540 | } | |
541 | /// Returns a reference to the internal stream manager. | |
542 | pub fn get_stream_manager(&self) -> &StreamManager { | |
543 | &self.streams | |
544 | } | |
545 | /// Returns an iterator over streams. | |
546 | pub fn get_streams(&self) -> StreamIter { | |
547 | self.streams.iter() | |
548 | } | |
549 | /// Returns 'ignored' marker for requested stream. | |
550 | pub fn is_ignored_stream(&self, idx: usize) -> bool { | |
551 | self.streams.is_ignored(idx) | |
552 | } | |
553 | /// Sets 'ignored' marker for requested stream. | |
554 | pub fn set_ignored_stream(&mut self, idx: usize) { | |
555 | self.streams.set_ignored(idx) | |
556 | } | |
557 | /// Clears 'ignored' marker for requested stream. | |
558 | pub fn set_unignored_stream(&mut self, idx: usize) { | |
559 | self.streams.set_unignored(idx) | |
560 | } | |
561 | ||
562 | /// Demuxes a new piece of data from the container. | |
563 | pub fn get_data(&mut self) -> DemuxerResult<NARawData> { | |
564 | loop { | |
565 | let res = self.dmx.get_data(&mut self.streams); | |
566 | if self.streams.no_ign || res.is_err() { return res; } | |
567 | let res = res.unwrap(); | |
568 | let idx = res.get_stream().get_num(); | |
569 | if !self.is_ignored_stream(idx) { | |
570 | return Ok(res); | |
571 | } | |
572 | } | |
573 | } | |
574 | /// Seeks to the requested time if possible. | |
575 | pub fn seek(&mut self, time: NATimePoint) -> DemuxerResult<()> { | |
576 | if self.seek_idx.skip_index { | |
577 | return Err(DemuxerError::NotPossible); | |
578 | } | |
579 | self.dmx.seek(time, &self.seek_idx) | |
580 | } | |
581 | /// Returns internal seek index. | |
582 | pub fn get_seek_index(&self) -> &SeekIndex { | |
583 | &self.seek_idx | |
584 | } | |
585 | /// Returns media duration reported by container or its streams. | |
586 | /// | |
587 | /// Duration is in milliseconds and set to zero when it is not available. | |
588 | pub fn get_duration(&self) -> u64 { | |
589 | let duration = self.dmx.get_duration(); | |
590 | if duration != 0 { | |
591 | return duration; | |
592 | } | |
593 | let mut duration = 0; | |
594 | for stream in self.streams.iter() { | |
595 | if stream.duration > 0 { | |
596 | let dur = NATimeInfo::ts_to_time(stream.duration, 1000, stream.tb_num, stream.tb_den); | |
597 | if duration < dur { | |
598 | duration = dur; | |
599 | } | |
600 | } | |
601 | } | |
602 | duration | |
603 | } | |
604 | } | |
605 | ||
606 | impl<'a> NAOptionHandler for RawDemuxer<'a> { | |
607 | fn get_supported_options(&self) -> &[NAOptionDefinition] { | |
608 | self.dmx.get_supported_options() | |
609 | } | |
610 | fn set_options(&mut self, options: &[NAOption]) { | |
611 | self.dmx.set_options(options); | |
612 | } | |
613 | fn query_option_value(&self, name: &str) -> Option<NAValue> { | |
614 | self.dmx.query_option_value(name) | |
615 | } | |
616 | } | |
617 | ||
618 | /// The trait for creating raw data demuxers. | |
619 | pub trait RawDemuxerCreator { | |
620 | /// Creates new raw demuxer instance that will use `ByteReader` source as an input. | |
621 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<dyn RawDemuxCore<'a> + 'a>; | |
622 | /// Tries to check whether the input can be demuxed with the demuxer. | |
623 | fn check_format(&self, br: &mut ByteReader) -> bool; | |
624 | /// Returns the name of current raw data demuxer creator (equal to the container name it can demux). | |
625 | fn get_name(&self) -> &'static str; | |
626 | } | |
627 | ||
628 | /// Creates raw data demuxer for a provided bytestream. | |
629 | pub fn create_raw_demuxer<'a>(dmxcr: &dyn RawDemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<RawDemuxer<'a>> { | |
630 | let mut dmx = dmxcr.new_demuxer(br); | |
817e4872 | 631 | let mut strmgr = StreamManager::new(); |
1ebf572d | 632 | let mut seek_idx = SeekIndex::new(); |
534a5b60 KS |
633 | dmx.open(&mut strmgr, &mut seek_idx)?; |
634 | Ok(RawDemuxer::new(dmx, strmgr, seek_idx)) | |
635 | } | |
636 | ||
637 | /// Creates raw data demuxer for a provided bytestream with options applied right after its creation. | |
638 | pub fn create_raw_demuxer_with_options<'a>(dmxcr: &dyn RawDemuxerCreator, br: &'a mut ByteReader<'a>, opts: &[NAOption]) -> DemuxerResult<RawDemuxer<'a>> { | |
639 | let mut dmx = dmxcr.new_demuxer(br); | |
640 | dmx.set_options(opts); | |
641 | let mut strmgr = StreamManager::new(); | |
642 | let mut seek_idx = SeekIndex::new(); | |
817e4872 KS |
643 | dmx.open(&mut strmgr, &mut seek_idx)?; |
644 | Ok(RawDemuxer::new(dmx, strmgr, seek_idx)) | |
1ebf572d KS |
645 | } |
646 | ||
647 | /// List of registered demuxers. | |
648 | #[derive(Default)] | |
649 | pub struct RegisteredRawDemuxers { | |
650 | dmxs: Vec<&'static dyn RawDemuxerCreator>, | |
651 | } | |
652 | ||
653 | impl RegisteredRawDemuxers { | |
654 | /// Constructs a new `RegisteredDemuxers` instance. | |
655 | pub fn new() -> Self { | |
656 | Self { dmxs: Vec::new() } | |
657 | } | |
658 | /// Registers a new demuxer. | |
659 | pub fn add_demuxer(&mut self, dmx: &'static dyn RawDemuxerCreator) { | |
660 | self.dmxs.push(dmx); | |
661 | } | |
662 | /// Searches for a demuxer that supports requested container format. | |
663 | pub fn find_demuxer(&self, name: &str) -> Option<&dyn RawDemuxerCreator> { | |
e6aaad5c | 664 | self.dmxs.iter().find(|&&dmx| dmx.get_name() == name).copied() |
1ebf572d KS |
665 | } |
666 | /// Provides an iterator over currently registered demuxers. | |
667 | pub fn iter(&self) -> std::slice::Iter<&dyn RawDemuxerCreator> { | |
668 | self.dmxs.iter() | |
669 | } | |
670 | } |