5 #[derive(Debug,Clone,Copy,PartialEq)]
7 pub enum DemuxerError {
17 type DemuxerResult<T> = Result<T, DemuxerError>;
19 pub trait DemuxCore<'a> {
20 fn open(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<()>;
21 fn get_frame(&mut self, strmgr: &mut StreamManager) -> DemuxerResult<NAPacket>;
22 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
25 pub trait NAPacketReader {
26 fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
27 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
30 impl<'a> NAPacketReader for ByteReader<'a> {
31 fn read_packet(&mut self, str: Rc<NAStream>, ts: NATimeInfo, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
32 let mut buf: Vec<u8> = Vec::with_capacity(size);
33 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
35 let res = self.read_buf(buf.as_mut_slice());
36 if let Err(_) = res { return Err(DemuxerError::IOError); }
37 let pkt = NAPacket::new(str, ts, kf, buf);
40 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
41 let mut refbuf = pkt.get_buffer();
42 let buf = Rc::make_mut(&mut refbuf);
43 let res = self.read_buf(buf.as_mut_slice());
44 if let Err(_) = res { return Err(DemuxerError::IOError); }
49 pub struct StreamManager {
50 streams: Vec<Rc<NAStream>>,
56 pub fn new() -> Self {
63 pub fn iter(&self) -> StreamIter { StreamIter::new(&self.streams) }
65 pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> {
66 let stream_num = self.streams.len();
67 let mut str = stream.clone();
68 str.set_num(stream_num);
69 self.streams.push(Rc::new(str));
70 self.ignored.push(false);
73 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
74 if idx < self.streams.len() {
75 Some(self.streams[idx].clone())
80 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
81 for i in 0..self.streams.len() {
82 if self.streams[i].get_id() == id {
83 return Some(self.streams[i].clone());
88 pub fn get_num_streams(&self) -> usize { self.streams.len() }
89 pub fn is_ignored(&self, idx: usize) -> bool {
92 } else if idx < self.ignored.len() {
98 pub fn is_ignored_id(&self, id: u32) -> bool {
99 for i in 0..self.streams.len() {
100 if self.streams[i].get_id() == id {
101 return self.ignored[i];
106 pub fn set_ignored(&mut self, idx: usize) {
107 if idx < self.ignored.len() {
108 self.ignored[idx] = true;
112 pub fn set_unignored(&mut self, idx: usize) {
113 if idx < self.ignored.len() {
114 self.ignored[idx] = false;
119 pub struct StreamIter<'a> {
120 streams: &'a Vec<Rc<NAStream>>,
124 impl<'a> StreamIter<'a> {
125 pub fn new(streams: &'a Vec<Rc<NAStream>>) -> Self {
126 StreamIter { streams: streams, pos: 0 }
130 impl<'a> Iterator for StreamIter<'a> {
131 type Item = Rc<NAStream>;
133 fn next(&mut self) -> Option<Self::Item> {
134 if self.pos >= self.streams.len() { return None; }
135 let ret = self.streams[self.pos].clone();
141 pub struct Demuxer<'a> {
142 dmx: Box<DemuxCore<'a> + 'a>,
143 streams: StreamManager,
146 impl<'a> Demuxer<'a> {
147 fn new(dmx: Box<DemuxCore<'a> + 'a>, str: StreamManager) -> Self {
153 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
154 self.streams.get_stream(idx)
156 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
157 self.streams.get_stream_by_id(id)
159 pub fn get_num_streams(&self) -> usize {
160 self.streams.get_num_streams()
162 pub fn get_streams(&self) -> StreamIter {
165 pub fn is_ignored_stream(&self, idx: usize) -> bool {
166 self.streams.is_ignored(idx)
168 pub fn set_ignored_stream(&mut self, idx: usize) {
169 self.streams.set_ignored(idx)
171 pub fn set_unignored_stream(&mut self, idx: usize) {
172 self.streams.set_unignored(idx)
175 pub fn get_frame(&mut self) -> DemuxerResult<NAPacket> {
177 let res = self.dmx.get_frame(&mut self.streams);
178 if self.streams.no_ign || res.is_err() { return res; }
179 let res = res.unwrap();
180 let idx = res.get_stream().get_num();
181 if !self.is_ignored_stream(idx) {
186 pub fn seek(&mut self, time: u64) -> DemuxerResult<()> {
191 impl From<ByteIOError> for DemuxerError {
192 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
195 ///The structure used to create demuxers.
196 pub trait DemuxerCreator {
197 /// Create new demuxer instance that will use `ByteReader` source as an input.
198 fn new_demuxer<'a>(&self, br: &'a mut ByteReader<'a>) -> Box<DemuxCore<'a> + 'a>;
199 /// Get the name of current demuxer creator.
200 fn get_name(&self) -> &'static str;
203 macro_rules! validate {
204 ($a:expr) => { if !$a { return Err(DemuxerError::InvalidData); } };
207 #[cfg(feature="demuxer_gdv")]
209 #[cfg(feature="demuxer_avi")]
211 #[cfg(feature="demuxer_real")]
215 const DEMUXERS: &[&'static DemuxerCreator] = &[
216 #[cfg(feature="demuxer_avi")]
217 &avi::AVIDemuxerCreator {},
218 #[cfg(feature="demuxer_gdv")]
219 &gdv::GDVDemuxerCreator {},
220 #[cfg(feature="demuxer_real")]
221 &realmedia::RealMediaDemuxerCreator {},
222 //#[cfg(feature="demuxer_real")]
223 // &realmedia::RealAudioDemuxerCreator {},
224 //#[cfg(feature="demuxer_real")]
225 // &realmedia::RealIVRDemuxerCreator {},
228 pub fn find_demuxer(name: &str) -> Option<&DemuxerCreator> {
229 for &dmx in DEMUXERS {
230 if dmx.get_name() == name {
237 pub fn create_demuxer<'a>(dmxcr: &DemuxerCreator, br: &'a mut ByteReader<'a>) -> DemuxerResult<Demuxer<'a>> {
238 let mut dmx = dmxcr.new_demuxer(br);
239 let mut str = StreamManager::new();
241 Ok(Demuxer::new(dmx, str))