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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::{error::Error, header::*, packet::*, util::*};
use util::marshal::{Marshal, MarshalSize, Unmarshal};
use anyhow::Result;
use bytes::{Buf, BufMut};
use std::any::Any;
use std::fmt;
pub(crate) const RECEPTION_REPORT_LENGTH: usize = 24;
pub(crate) const FRACTION_LOST_OFFSET: usize = 4;
pub(crate) const TOTAL_LOST_OFFSET: usize = 5;
pub(crate) const LAST_SEQ_OFFSET: usize = 8;
pub(crate) const JITTER_OFFSET: usize = 12;
pub(crate) const LAST_SR_OFFSET: usize = 16;
pub(crate) const DELAY_OFFSET: usize = 20;
#[derive(Debug, PartialEq, Default, Clone)]
pub struct ReceptionReport {
pub ssrc: u32,
pub fraction_lost: u8,
pub total_lost: u32,
pub last_sequence_number: u32,
pub jitter: u32,
pub last_sender_report: u32,
pub delay: u32,
}
impl fmt::Display for ReceptionReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl Packet for ReceptionReport {
fn header(&self) -> Header {
Header::default()
}
fn destination_ssrc(&self) -> Vec<u32> {
vec![]
}
fn raw_size(&self) -> usize {
RECEPTION_REPORT_LENGTH
}
fn as_any(&self) -> &dyn Any {
self
}
fn equal(&self, other: &dyn Packet) -> bool {
other
.as_any()
.downcast_ref::<ReceptionReport>()
.map_or(false, |a| self == a)
}
fn cloned(&self) -> Box<dyn Packet> {
Box::new(self.clone())
}
}
impl MarshalSize for ReceptionReport {
fn marshal_size(&self) -> usize {
let l = self.raw_size();
l + get_padding_size(l)
}
}
impl Marshal for ReceptionReport {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
if buf.remaining_mut() < self.marshal_size() {
return Err(Error::BufferTooShort.into());
}
buf.put_u32(self.ssrc);
buf.put_u8(self.fraction_lost);
if self.total_lost >= (1 << 25) {
return Err(Error::InvalidTotalLost.into());
}
buf.put_u8(((self.total_lost >> 16) & 0xFF) as u8);
buf.put_u8(((self.total_lost >> 8) & 0xFF) as u8);
buf.put_u8((self.total_lost & 0xFF) as u8);
buf.put_u32(self.last_sequence_number);
buf.put_u32(self.jitter);
buf.put_u32(self.last_sender_report);
buf.put_u32(self.delay);
put_padding(buf, self.raw_size());
Ok(self.marshal_size())
}
}
impl Unmarshal for ReceptionReport {
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
where
Self: Sized,
B: Buf,
{
let raw_packet_len = raw_packet.remaining();
if raw_packet_len < RECEPTION_REPORT_LENGTH {
return Err(Error::PacketTooShort.into());
}
let ssrc = raw_packet.get_u32();
let fraction_lost = raw_packet.get_u8();
let t0 = raw_packet.get_u8();
let t1 = raw_packet.get_u8();
let t2 = raw_packet.get_u8();
let total_lost = (t2 as u32) | (t1 as u32) << 8 | (t0 as u32) << 16;
let last_sequence_number = raw_packet.get_u32();
let jitter = raw_packet.get_u32();
let last_sender_report = raw_packet.get_u32();
let delay = raw_packet.get_u32();
Ok(ReceptionReport {
ssrc,
fraction_lost,
total_lost,
last_sequence_number,
jitter,
last_sender_report,
delay,
})
}
}