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