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