print more info for streams
[nihav.git] / src / demuxers / mod.rs
1 pub mod gdv;
2
3 use std::fmt;
4 use std::rc::Rc;
5 use frame::*;
6 //use std::collections::HashMap;
7 use io::byteio::*;
8
9 #[derive(Debug,Clone,Copy)]
10 #[allow(dead_code)]
11 pub enum StreamType {
12 Video,
13 Audio,
14 Subtitles,
15 Data,
16 None,
17 }
18
19 impl fmt::Display for StreamType {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 match *self {
22 StreamType::Video => write!(f, "Video"),
23 StreamType::Audio => write!(f, "Audio"),
24 StreamType::Subtitles => write!(f, "Subtitles"),
25 StreamType::Data => write!(f, "Data"),
26 StreamType::None => write!(f, "-"),
27 }
28 }
29 }
30
31
32 #[allow(dead_code)]
33 #[derive(Clone)]
34 pub struct NAStream {
35 media_type: StreamType,
36 id: u32,
37 num: usize,
38 info: Rc<NACodecInfo>,
39 }
40
41 impl NAStream {
42 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
43 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
44 }
45 pub fn get_id(&self) -> u32 { self.id }
46 pub fn get_num(&self) -> usize { self.num }
47 pub fn set_num(&mut self, num: usize) { self.num = num; }
48 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
49 }
50
51 impl fmt::Display for NAStream {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 write!(f, "({}#{} - {})", self.media_type, self.id, self.info.get_properties())
54 }
55 }
56
57 #[allow(dead_code)]
58 pub struct NAPacket {
59 stream: Rc<NAStream>,
60 pts: Option<u64>,
61 dts: Option<u64>,
62 duration: Option<u64>,
63 buffer: Rc<Vec<u8>>,
64 keyframe: bool,
65 // options: HashMap<String, NAValue<'a>>,
66 }
67
68 impl NAPacket {
69 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
70 // let mut vec: Vec<u8> = Vec::new();
71 // vec.resize(size, 0);
72 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
73 }
74 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
75 pub fn get_pts(&self) -> Option<u64> { self.pts }
76 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
77 }
78
79 impl fmt::Display for NAPacket {
80 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
82 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
83 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
84 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
85 if self.keyframe { foo = format!("{} kf", foo); }
86 foo = foo + "]";
87 write!(f, "{}", foo)
88 }
89 }
90
91 #[derive(Debug)]
92 #[allow(dead_code)]
93 pub enum DemuxerError {
94 EOF,
95 NoSuchInput,
96 InvalidData,
97 IOError,
98 NotImplemented,
99 MemoryError,
100 }
101
102 type DemuxerResult<T> = Result<T, DemuxerError>;
103
104 pub trait Demux<'a> {
105 fn open(&mut self) -> DemuxerResult<()>;
106 fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
107 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
108 }
109
110 pub trait NAPacketReader {
111 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
112 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
113 }
114
115 impl<'a> NAPacketReader for ByteReader<'a> {
116 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
117 let mut buf: Vec<u8> = Vec::with_capacity(size);
118 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
119 buf.resize(size, 0);
120 let res = self.read_buf(buf.as_mut_slice());
121 if let Err(_) = res { return Err(DemuxerError::IOError); }
122 let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
123 Ok(pkt)
124 }
125 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
126 let mut refbuf = pkt.get_buffer();
127 let mut buf = Rc::make_mut(&mut refbuf);
128 let res = self.read_buf(buf.as_mut_slice());
129 if let Err(_) = res { return Err(DemuxerError::IOError); }
130 Ok(())
131 }
132 }
133
134 pub struct Demuxer {
135 streams: Vec<Rc<NAStream>>,
136 }
137
138 impl Demuxer {
139 pub fn new() -> Self { Demuxer { streams: Vec::new() } }
140 pub fn add_stream(&mut self, stream: NAStream) -> Option<usize> {
141 let stream_num = self.streams.len();
142 let mut str = stream.clone();
143 str.set_num(stream_num);
144 self.streams.push(Rc::new(str));
145 Some(stream_num)
146 }
147 pub fn get_stream(&self, idx: usize) -> Option<Rc<NAStream>> {
148 if idx < self.streams.len() {
149 Some(self.streams[idx].clone())
150 } else {
151 None
152 }
153 }
154 pub fn get_stream_by_id(&self, id: u32) -> Option<Rc<NAStream>> {
155 for i in 0..self.streams.len() {
156 if self.streams[i].get_id() == id {
157 return Some(self.streams[i].clone());
158 }
159 }
160 None
161 }
162 }
163
164 impl From<ByteIOError> for DemuxerError {
165 fn from(_: ByteIOError) -> Self { DemuxerError::IOError }
166 }
167
168 //impl NADemuxerBuilder {
169 // #[allow(unused_variables)]
170 // pub fn create_demuxer(name: &str, url: &str) -> DemuxerResult<Box<NADemuxer<'static>>> {
171 // unimplemented!()
172 // }
173 //}