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
use crate::packet;
use log::warn;
pub const PAT_PID: packet::Pid = packet::Pid::new(0);
#[derive(Clone, Debug)]
pub enum ProgramDescriptor {
Network {
pid: packet::Pid,
},
Program {
program_number: u16,
pid: packet::Pid,
},
}
impl ProgramDescriptor {
pub fn from_bytes(data: &[u8]) -> ProgramDescriptor {
let program_number = (u16::from(data[0]) << 8) | u16::from(data[1]);
let pid = packet::Pid::new((u16::from(data[2]) & 0b0001_1111) << 8 | u16::from(data[3]));
if program_number == 0 {
ProgramDescriptor::Network { pid }
} else {
ProgramDescriptor::Program {
program_number,
pid,
}
}
}
pub fn pid(&self) -> packet::Pid {
match *self {
ProgramDescriptor::Network { pid } => pid,
ProgramDescriptor::Program { pid, .. } => pid,
}
}
}
#[derive(Clone, Debug)]
pub struct PatSection<'buf> {
data: &'buf [u8],
}
impl<'buf> PatSection<'buf> {
pub fn new(data: &'buf [u8]) -> PatSection<'buf> {
PatSection { data }
}
pub fn programs(&self) -> impl Iterator<Item = ProgramDescriptor> + 'buf {
ProgramIter { buf: self.data }
}
}
struct ProgramIter<'buf> {
buf: &'buf [u8],
}
impl<'buf> Iterator for ProgramIter<'buf> {
type Item = ProgramDescriptor;
fn next(&mut self) -> Option<Self::Item> {
if self.buf.is_empty() {
return None;
}
if self.buf.len() < 4 {
warn!(
"too few bytes remaining for PAT descriptor: {}",
self.buf.len()
);
return None;
}
let (head, tail) = self.buf.split_at(4);
self.buf = tail;
Some(ProgramDescriptor::from_bytes(head))
}
}