revamp stream handling in demuxers
[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 }
17
18 impl fmt::Display for StreamType {
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 match *self {
21 StreamType::Video => write!(f, "Video"),
22 StreamType::Audio => write!(f, "Audio"),
23 StreamType::Subtitles => write!(f, "Subtitles"),
24 StreamType::Data => write!(f, "Data"),
25 }
26 }
27 }
28
29
30 #[allow(dead_code)]
31 #[derive(Clone)]
32 pub struct NAStream {
33 media_type: StreamType,
34 id: u32,
35 num: usize,
36 info: Rc<NACodecInfo>,
37 }
38
39 impl NAStream {
40 pub fn new(mt: StreamType, id: u32, info: NACodecInfo) -> Self {
41 NAStream { media_type: mt, id: id, num: 0, info: Rc::new(info) }
42 }
43 pub fn get_id(&self) -> u32 { self.id }
44 pub fn get_num(&self) -> usize { self.num }
45 pub fn set_num(&mut self, num: usize) { self.num = num; }
46 pub fn get_info(&self) -> Rc<NACodecInfo> { self.info.clone() }
47 }
48
49 impl fmt::Display for NAStream {
50 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51 write!(f, "({}#{})", self.media_type, self.id)
52 }
53 }
54
55 #[allow(dead_code)]
56 pub struct NAPacket {
57 stream: Rc<NAStream>,
58 pts: Option<u64>,
59 dts: Option<u64>,
60 duration: Option<u64>,
61 buffer: Rc<Vec<u8>>,
62 keyframe: bool,
63 // options: HashMap<String, NAValue<'a>>,
64 }
65
66 impl NAPacket {
67 pub fn new(str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, vec: Vec<u8>) -> Self {
68 // let mut vec: Vec<u8> = Vec::new();
69 // vec.resize(size, 0);
70 NAPacket { stream: str, pts: pts, dts: dts, duration: dur, keyframe: kf, buffer: Rc::new(vec) }
71 }
72 pub fn get_stream(&self) -> Rc<NAStream> { self.stream.clone() }
73 pub fn get_pts(&self) -> Option<u64> { self.pts }
74 pub fn get_buffer(&self) -> Rc<Vec<u8>> { self.buffer.clone() }
75 }
76
77 impl fmt::Display for NAPacket {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 let mut foo = format!("[pkt for {} size {}", self.stream, self.buffer.len());
80 if let Some(pts) = self.pts { foo = format!("{} pts {}", foo, pts); }
81 if let Some(dts) = self.dts { foo = format!("{} dts {}", foo, dts); }
82 if let Some(dur) = self.duration { foo = format!("{} duration {}", foo, dur); }
83 if self.keyframe { foo = format!("{} kf", foo); }
84 foo = foo + "]";
85 write!(f, "{}", foo)
86 }
87 }
88
89 #[derive(Debug)]
90 #[allow(dead_code)]
91 pub enum DemuxerError {
92 EOF,
93 NoSuchInput,
94 InvalidData,
95 IOError,
96 NotImplemented,
97 MemoryError,
98 }
99
100 type DemuxerResult<T> = Result<T, DemuxerError>;
101
102 pub trait Demux<'a> {
103 fn open(&mut self) -> DemuxerResult<()>;
104 fn get_frame(&mut self) -> DemuxerResult<NAPacket>;
105 fn seek(&mut self, time: u64) -> DemuxerResult<()>;
106 }
107
108 pub trait NAPacketReader {
109 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, keyframe: bool, size: usize) -> DemuxerResult<NAPacket>;
110 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()>;
111 }
112
113 impl<'a> NAPacketReader for ByteReader<'a> {
114 fn read_packet(&mut self, str: Rc<NAStream>, pts: Option<u64>, dts: Option<u64>, dur: Option<u64>, kf: bool, size: usize) -> DemuxerResult<NAPacket> {
115 let mut buf: Vec<u8> = Vec::with_capacity(size);
116 if buf.capacity() < size { return Err(DemuxerError::MemoryError); }
117 buf.resize(size, 0);
118 let res = self.read_buf(buf.as_mut_slice());
119 if let Err(_) = res { return Err(DemuxerError::IOError); }
120 if res.unwrap() < buf.len() { return Err(DemuxerError::IOError); }
121 let pkt = NAPacket::new(str, pts, dts, dur, kf, buf);
122 Ok(pkt)
123 }
124 fn fill_packet(&mut self, pkt: &mut NAPacket) -> DemuxerResult<()> {
125 let mut refbuf = pkt.get_buffer();
126 let mut buf = Rc::make_mut(&mut refbuf);
127 let res = self.read_buf(buf.as_mut_slice());
128 if let Err(_) = res { return Err(DemuxerError::IOError); }
129 if res.unwrap() < buf.len() { 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 //}