]>
Commit | Line | Data |
---|---|---|
1 | #[cfg(feature="demuxer_gdv")] | |
2 | mod gdv; | |
3 | #[cfg(feature="demuxer_avi")] | |
4 | mod avi; | |
5 | ||
6 | use std::rc::Rc; | |
7 | use frame::*; | |
8 | use io::byteio::*; | |
9 | ||
10 | #[derive(Debug,Clone,Copy,PartialEq)] | |
11 | #[allow(dead_code)] | |
12 | pub enum DemuxerError { | |
13 | EOF, | |
14 | NoSuchInput, | |
15 | InvalidData, | |
16 | IOError, | |
17 | NotImplemented, | |
18 | MemoryError, | |
19 | } | |
20 | ||
21 | type DemuxerResult<T> = Result<T, DemuxerError>; | |
22 | ||
23 | pub trait DemuxCore<'a> { | |
24 | fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()>; | |
25 | fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>; | |
26 | fn seek(&mut self, time: u64) -> DemuxerResult<()>; | |
27 | } | |
28 | ||
29 | pub trait NAPacketReader { | |
30 | fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>; | |
31 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>; | |
32 | } | |
33 | ||
34 | impl<'a> NAPacketReader for ByteReader<'a> { | |
35 | fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> { | |
36 | let mut buf: Vec<u8> = Vec::with_capacity(size); | |
37 | if buf.capacity() < size { return Err(DemuxerError::MemoryError); } | |
38 | buf.resize(size, 0); | |
39 | let res = self.read_buf(buf.as_mut_slice()); | |
40 | if let Err(_) = res { return Err(DemuxerError::IOError); } | |
41 | let pkt = NAPacket::new(str, ts, kf, buf); | |
42 | Ok(pkt) | |
43 | } | |
44 | fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> { | |
45 | let mut refbuf = pkt.get_buffer(); | |
46 | let mut buf = Rc::make_mut(&mut refbuf); | |
47 | let res = self.read_buf(buf.as_mut_slice()); | |
48 | if let Err(_) = res { return Err(DemuxerError::IOError); } | |
49 | Ok(()) | |
50 | } | |
51 | } | |
52 | ||
53 | pub struct StreamManager { | |
54 | streams: Vec<Rc<NAStream>>, | |
55 | ignored: Vec<bool>, | |
56 | no_ign: bool, | |
57 | } | |
58 | ||
59 | impl StreamManager { | |
60 | pub fn new() -> Self { | |
61 | StreamManager { | |
62 | streams: Vec::new(), | |
63 | ignored: Vec::new(), | |
64 | no_ign: true, | |
65 | } | |
66 | } | |
67 | pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) } | |
68 | ||
69 | pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> { | |
70 | let stream_num = self.streams.len(); | |
71 | let mut str = stream.clone(); | |
72 | str.set_num(stream_num); | |
73 | self.streams.push(Rc::new(str)); | |
74 | self.ignored.push(false); | |
75 | Some(stream_num) | |
76 | } | |
77 | pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { | |
78 | if idx < self.streams.len() { | |
79 | Some(self.streams[idx].clone()) | |
80 | } else { | |
81 | None | |
82 | } | |
83 | } | |
84 | pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> { | |
85 | for i in 0..self.streams.len() { | |
86 | if self.streams[i].get_id() == id { | |
87 | return Some(self.streams[i].clone()); | |
88 | } | |
89 | } | |
90 | None | |
91 | } | |
92 | pub fn get_num_streams(&self) -> usize { self.streams.len() } | |
93 | pub fn is_ignored(&self, idx: usize) -> bool { | |
94 | if self.no_ign { | |
95 | true | |
96 | } else if idx < self.ignored.len() { | |
97 | self.ignored[idx] | |
98 | } else { | |
99 | false | |
100 | } | |
101 | } | |
102 | pub fn set_ignored(&mut self, idx: usize) { | |
103 | if idx < self.ignored.len() { | |
104 | self.ignored[idx] = true; | |
105 | self.no_ign = false; | |
106 | } | |
107 | } | |
108 | pub fn set_unignored(&mut self, idx: usize) { | |
109 | if idx < self.ignored.len() { | |
110 | self.ignored[idx] = false; | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
115 | pub struct StreamIter<'a> { | |
116 | streams: &'a Vec<Rc<NAStream>>, | |
117 | pos: usize, | |
118 | } | |
119 | ||
120 | impl<'a> StreamIter<'a> { | |
121 | pub fn new(streams: &'a Vec<Rc<NAStream>>) -> Self { | |
122 | StreamIter { streams: streams, pos: 0 } | |
123 | } | |
124 | } | |
125 | ||
126 | impl<'a> Iterator for StreamIter<'a> { | |
127 | type Item = Rc<NAStream>; | |
128 | ||
129 | fn next(&mut self) -> Option<Self::Item> { | |
130 | if self.pos >= self.streams.len() { return None; } | |
131 | let ret = self.streams[self.pos].clone(); | |
132 | self.pos += 1; | |
133 | Some(ret) | |
134 | } | |
135 | } | |
136 | ||
137 | pub struct Demuxer<'a> { | |
138 | dmx: Box<DemuxCore<'a> + 'a>, | |
139 | streams: StreamManager, | |
140 | } | |
141 | ||
142 | impl<'a> Demuxer<'a> { | |
143 | fn new(dmx: Box<DemuxCore<'a> + 'a>, str: StreamManager) -> Self { | |
144 | Demuxer { | |
145 | dmx: dmx, | |
146 | streams: str, | |
147 | } | |
148 | } | |
149 | pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> { | |
150 | self.streams.get_stream(idx) | |
151 | } | |
152 | pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> { | |
153 | self.streams.get_stream_by_id(id) | |
154 | } | |
155 | pub fn get_num_streams(&self) -> usize { | |
156 | self.streams.get_num_streams() | |
157 | } | |
158 | pub fn get_streams(&self) -> StreamIter { | |
159 | self.streams.iter() | |
160 | } | |
161 | pub fn is_ignored_stream(&self, idx: usize) -> bool { | |
162 | self.streams.is_ignored(idx) | |
163 | } | |
164 | pub fn set_ignored_stream(&mut self, idx: usize) { | |
165 | self.streams.set_ignored(idx) | |
166 | } | |
167 | pub fn set_unignored_stream(&mut self, idx: usize) { | |
168 | self.streams.set_unignored(idx) | |
169 | } | |
170 | ||
171 | pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> { | |
172 | loop { | |
173 | let res = self.dmx.get_frame(&mut self.streams); | |
174 | if self.streams.no_ign || res.is_err() { return res; } | |
175 | let res = res.unwrap(); | |
176 | let idx = res.get_stream().get_num(); | |
177 | if !self.is_ignored_stream(idx) { | |
178 | return Ok(res); | |
179 | } | |
180 | } | |
181 | } | |
182 | pub fn seek(&mut self, time: u64) -> DemuxerResult<()> { | |
183 | self.dmx.seek(time) | |
184 | } | |
185 | } | |
186 | ||
187 | impl From<ByteIOError> for DemuxerError { | |
188 | fn from(_: ByteIOError) -> Self { DemuxerError::IOError } | |
189 | } | |
190 | ||
191 | ///The structure used to create demuxers. | |
192 | pub trait DemuxerCreator { | |
193 | /// Create new demuxer instance that will use `ByteReader` source as an input. | |
194 | fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a>; | |
195 | /// Get the name of current demuxer creator. | |
196 | fn get_name(&self) -> &'static str; | |
197 | } | |
198 | ||
199 | const DEMUXERS: &[&'static DemuxerCreator] = &[ | |
200 | #[cfg(feature="demuxer_avi")] | |
201 | &avi::AVIDemuxerCreator {}, | |
202 | #[cfg(feature="demuxer_gdv")] | |
203 | &gdv::GDVDemuxerCreator {}, | |
204 | ]; | |
205 | ||
206 | pub fn find_demuxer(name: &str) -> Option<&DemuxerCreator> { | |
207 | for &dmx in DEMUXERS { | |
208 | if dmx.get_name() == name { | |
209 | return Some(dmx); | |
210 | } | |
211 | } | |
212 | None | |
213 | } | |
214 | ||
215 | pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> { | |
216 | let mut dmx = dmxcr.new_demuxer(br); | |
217 | let mut str = StreamManager::new(); | |
218 | dmx.open(&mut str)?; | |
219 | Ok(Demuxer::new(dmx, str)) | |
220 | } |