6 //use std::collections::HashMap;
18 impl fmt::Display for StreamType {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 StreamType::Video => write!(f, "Video"),
22 StreamType::Audio => write!(f, "Audio"),
23 StreamType::Subtitles => write!(f, "Subtitles"),
24 StreamType::Data => write!(f, "Data"),
31 pub struct NAStream<'a> {
32 media_type: StreamType,
34 info: Rc<NACodecInfo<'a>>,
37 impl<'a> NAStream<'a> {
38 pub fn new(mt: StreamType, id: u32, info: NACodecInfo<'a>) -> Self {
39 NAStream { media_type: mt, id: id, info: Rc::new(info) }
41 pub fn get_id(&self) -> u32 { self.id }
42 pub fn get_info(&self) -> Rc<NACodecInfo<'a>> { self.info.clone() }
45 impl<'a> fmt::Display for NAStream<'a> {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 write!(f, "({}#{})", self.media_type, self.id)
52 pub struct NAPacket<'a> {
53 stream: Rc<NAStream<'a>>,
56 duration: Option<u64>,
59 // options: HashMap<String, NAValue<'a>>,
62 impl<'a> NAPacket<'a> {
63 pub fn new(str: Rc<NAStream<'a>>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
64 // let mut vec: Vec<u8> = Vec::new();
65 // vec.resize(size, 0);
66 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
68 pub fn get_stream(&self) -> Rc<NAStream<'a>> { self.stream.clone() }
69 pub fn get_pts(&self) -> Option<u64> { self.pts }
70 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
73 impl<'a> fmt::Display for NAPacket<'a> {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
76 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
77 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
78 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
79 if self.keyframe { foo = format!("{} kf", foo); }
87 pub enum DemuxerError {
96 type DemuxerResult<T> = Result<T, DemuxerError>;
98 pub trait NADemuxer<'a> {
99 fn open(&mut self) -> DemuxerResult<()>;
100 fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
101 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
104 pub trait NAPacketReader<'a> {
105 fn read_packet(&mut self, str: Rc<NAStream<'a>>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
106 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
109 impl<'a> NAPacketReader<'a> for ByteReader<'a> {
110 fn read_packet(&mut self, str: Rc<NAStream<'a>>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
111 let mut buf: Vec<u8> = Vec::with_capacity(size);
112 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
114 let res = self.read_buf(buf.as_mut_slice());
115 if let Err(_) = res { return Err(DemuxerError::IOError); }
116 if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); }
117 let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
120 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
121 let mut refbuf = pkt.get_buffer();
122 let mut buf = Rc::make_mut(&mut refbuf);
123 let res = self.read_buf(buf.as_mut_slice());
124 if let Err(_) = res { return Err(DemuxerError::IOError); }
125 if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); }
130 pub struct NADemuxerBuilder {
133 impl From<ByteIOError> for DemuxerError {
134 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
137 impl NADemuxerBuilder {
138 #[allow(unused_variables)]
139 pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult<Box<NADemuxer<'static>>> {