1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fmt;
use descriptor;
use demultiplex::DemuxError;
use StreamType;
pub struct PmtSection<'buf> {
data: &'buf[u8],
}
impl<'buf> fmt::Debug for PmtSection<'buf> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_struct("PmtSection")
.field("pcr_pid", &self.pcr_pid())
.field("descriptors", &DescriptorsDebug(self))
.field("streams", &StreamsDebug(self))
.finish()
}
}
struct StreamsDebug<'buf>(&'buf PmtSection<'buf>);
impl<'buf> fmt::Debug for StreamsDebug<'buf> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_list().entries(self.0.streams()).finish()
}
}
struct DescriptorsDebug<'buf>(&'buf PmtSection<'buf>);
impl<'buf> fmt::Debug for DescriptorsDebug<'buf> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_list().entries(self.0.descriptors::<descriptor::CoreDescriptors>()).finish()
}
}
impl<'buf> PmtSection<'buf> {
pub fn from_bytes(data: &'buf[u8]) -> Result<PmtSection<'buf>, DemuxError> {
if data.len() < Self::HEADER_SIZE {
Err(DemuxError::NotEnoughData { field: "program_map_section", expected: Self::HEADER_SIZE, actual: data.len() })
} else {
Ok(PmtSection {
data,
})
}
}
const HEADER_SIZE: usize = 4;
pub fn reserved1(&self) -> u8 {
self.data[0] >> 5
}
pub fn pcr_pid(&self) -> u16 {
u16::from(self.data[0] & 0b0001_1111) << 8 | u16::from(self.data[1])
}
pub fn reserved2(&self) -> u8 {
self.data[2] >> 4
}
pub fn program_info_length(&self) -> u16 {
u16::from(self.data[2] & 0b0000_1111) << 8 | u16::from(self.data[3])
}
pub fn descriptors<Desc: descriptor::Descriptor<'buf> + 'buf>(&self) -> impl Iterator<Item=Result<Desc, descriptor::DescriptorError>> + 'buf {
let descriptor_end = Self::HEADER_SIZE + self.program_info_length() as usize;
let descriptor_data = &self.data[Self::HEADER_SIZE..descriptor_end];
descriptor::DescriptorIter::new(descriptor_data)
}
pub fn streams(&self) -> impl Iterator<Item=StreamInfo<'buf>> {
let descriptor_end = Self::HEADER_SIZE + self.program_info_length() as usize;
if descriptor_end > self.data.len() {
panic!("program_info_length={} extends beyond end of PMT section (section_length={})", self.program_info_length(), self.data.len());
}
StreamInfoIter::new(&self.data[descriptor_end..])
}
}
struct StreamInfoIter<'buf> {
buf: &'buf[u8],
}
impl<'buf> StreamInfoIter<'buf> {
fn new(buf: &'buf[u8]) -> StreamInfoIter<'buf> {
StreamInfoIter { buf }
}
}
impl<'buf> Iterator for StreamInfoIter<'buf> {
type Item = StreamInfo<'buf>;
fn next(&mut self) -> Option<Self::Item> {
if self.buf.is_empty() {
return None;
}
if let Some((stream_info, info_len)) = StreamInfo::from_bytes(self.buf) {
self.buf = &self.buf[info_len..];
Some(stream_info)
} else {
None
}
}
}
pub struct StreamInfo<'buf> {
data: &'buf[u8],
}
impl<'buf> StreamInfo<'buf> {
const HEADER_SIZE: usize = 5;
fn from_bytes(data: &'buf[u8]) -> Option<(StreamInfo<'buf>, usize)> {
if data.len() < Self::HEADER_SIZE {
println!("only {} bytes remaining for stream info, at least {} required {:?}", data.len(), Self::HEADER_SIZE, data);
return None;
}
let result = StreamInfo {
data,
};
let descriptor_end = Self::HEADER_SIZE + result.es_info_length() as usize;
if descriptor_end > data.len() {
print!("PMT section of size {} is not large enough to contain es_info_length of {}", data.len(), result.es_info_length());
return None;
}
Some((result, descriptor_end))
}
pub fn stream_type(&self) -> StreamType {
self.data[0].into()
}
pub fn reserved1(&self) -> u8 {
self.data[1] >> 5
}
pub fn elementary_pid(&self) -> u16 {
u16::from(self.data[1] & 0b0001_1111) << 8 | u16::from(self.data[2])
}
pub fn reserved2(&self) -> u8 {
self.data[3] >> 4
}
pub fn es_info_length(&self) -> u16 {
u16::from(self.data[3] & 0b0000_1111) << 8 | u16::from(self.data[4])
}
pub fn descriptors<Desc: descriptor::Descriptor<'buf> + 'buf>(&self) -> impl Iterator<Item=Result<Desc, descriptor::DescriptorError>> + 'buf {
let descriptor_end = Self::HEADER_SIZE + self.es_info_length() as usize;
descriptor::DescriptorIter::new(&self.data[Self::HEADER_SIZE..descriptor_end])
}
}
impl<'buf> fmt::Debug for StreamInfo<'buf> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_struct("StreamInfo")
.field("stream_type", &self.stream_type())
.field("elementry_pid", &self.elementary_pid())
.field("descriptors", &StreamInfoDescriptorsDebug(self))
.finish()
}
}
struct StreamInfoDescriptorsDebug<'buf>(&'buf StreamInfo<'buf>);
impl<'buf> fmt::Debug for StreamInfoDescriptorsDebug<'buf> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.debug_list().entries(self.0.descriptors::<descriptor::CoreDescriptors>()).finish()
}
}