rtci_rtcp/extended_report/
ssr.rs1use super::*;
2
3const SSR_REPORT_BLOCK_LENGTH: u16 = 4 + 2 * 2 + 4 * 6 + 4;
4
5#[derive(Debug, Default, PartialEq, Eq, Clone)]
32pub struct StatisticsSummaryReportBlock {
33 pub loss_reports: bool,
36 pub duplicate_reports: bool,
38 pub jitter_reports: bool,
40 pub ttl_or_hop_limit: TTLorHopLimitType,
42
43 pub ssrc: u32,
46 pub begin_seq: u16,
48 pub end_seq: u16,
50 pub lost_packets: u32,
52 pub dup_packets: u32,
54 pub min_jitter: u32,
56 pub max_jitter: u32,
58 pub mean_jitter: u32,
60 pub dev_jitter: u32,
62 pub min_ttl_or_hl: u8,
64 pub max_ttl_or_hl: u8,
66 pub mean_ttl_or_hl: u8,
68 pub dev_ttl_or_hl: u8,
70}
71
72impl fmt::Display for StatisticsSummaryReportBlock {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 write!(f, "{self:?}")
75 }
76}
77
78#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
81pub enum TTLorHopLimitType {
82 #[default]
83 Missing = 0,
85 IPv4 = 1,
87 IPv6 = 2,
89}
90
91impl From<u8> for TTLorHopLimitType {
92 fn from(v: u8) -> Self {
93 match v {
94 1 => TTLorHopLimitType::IPv4,
95 2 => TTLorHopLimitType::IPv6,
96 _ => TTLorHopLimitType::Missing,
97 }
98 }
99}
100
101impl fmt::Display for TTLorHopLimitType {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 let s = match *self {
104 TTLorHopLimitType::Missing => "[ToH Missing]",
105 TTLorHopLimitType::IPv4 => "[ToH = IPv4]",
106 TTLorHopLimitType::IPv6 => "[ToH = IPv6]",
107 };
108 write!(f, "{s}")
109 }
110}
111
112impl StatisticsSummaryReportBlock {
113 pub fn reported(&self) -> Self {
118 let mut block = self.clone();
119 if !block.loss_reports {
120 block.lost_packets = 0;
121 }
122 if !block.duplicate_reports {
123 block.dup_packets = 0;
124 }
125 if !block.jitter_reports {
126 block.min_jitter = 0;
127 block.max_jitter = 0;
128 block.mean_jitter = 0;
129 block.dev_jitter = 0;
130 }
131 if block.ttl_or_hop_limit == TTLorHopLimitType::Missing {
132 block.min_ttl_or_hl = 0;
133 block.max_ttl_or_hl = 0;
134 block.mean_ttl_or_hl = 0;
135 block.dev_ttl_or_hl = 0;
136 }
137 block
138 }
139
140 pub fn xr_header(&self) -> XRHeader {
142 let mut type_specific = 0x00;
143 if self.loss_reports {
144 type_specific |= 0x80;
145 }
146 if self.duplicate_reports {
147 type_specific |= 0x40;
148 }
149 if self.jitter_reports {
150 type_specific |= 0x20;
151 }
152 type_specific |= (self.ttl_or_hop_limit as u8 & 0x03) << 3;
153
154 XRHeader {
155 block_type: BlockType::StatisticsSummary,
156 type_specific,
157 block_length: (self.raw_size() / 4 - 1) as u16,
158 }
159 }
160}
161
162impl Packet for StatisticsSummaryReportBlock {
163 fn header(&self) -> Header {
164 Header::default()
165 }
166
167 fn destination_ssrc(&self) -> Vec<u32> {
169 vec![self.ssrc]
170 }
171
172 fn raw_size(&self) -> usize {
173 XR_HEADER_LENGTH + SSR_REPORT_BLOCK_LENGTH as usize
174 }
175
176 fn as_any(&self) -> &dyn Any {
177 self
178 }
179 fn equal(&self, other: &dyn Packet) -> bool {
180 other
181 .as_any()
182 .downcast_ref::<StatisticsSummaryReportBlock>()
183 == Some(self)
184 }
185 fn cloned(&self) -> Box<dyn Packet> {
186 Box::new(self.clone())
187 }
188}
189
190impl MarshalSize for StatisticsSummaryReportBlock {
191 fn marshal_size(&self) -> usize {
192 self.raw_size()
193 }
194}
195
196impl Marshal for StatisticsSummaryReportBlock {
197 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
199 if buf.remaining_mut() < self.marshal_size() {
200 return Err(Error::BufferTooShort);
201 }
202
203 let h = self.xr_header();
204 let n = h.marshal_to(buf)?;
205 buf = &mut buf[n..];
206
207 let reported = self.reported();
210
211 buf.put_u32(reported.ssrc);
212 buf.put_u16(reported.begin_seq);
213 buf.put_u16(reported.end_seq);
214 buf.put_u32(reported.lost_packets);
215 buf.put_u32(reported.dup_packets);
216 buf.put_u32(reported.min_jitter);
217 buf.put_u32(reported.max_jitter);
218 buf.put_u32(reported.mean_jitter);
219 buf.put_u32(reported.dev_jitter);
220 buf.put_u8(reported.min_ttl_or_hl);
221 buf.put_u8(reported.max_ttl_or_hl);
222 buf.put_u8(reported.mean_ttl_or_hl);
223 buf.put_u8(reported.dev_ttl_or_hl);
224
225 Ok(self.marshal_size())
226 }
227}
228
229impl Unmarshal for StatisticsSummaryReportBlock {
230 fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
232 where
233 Self: Sized,
234 B: Buf,
235 {
236 if raw_packet.remaining() < XR_HEADER_LENGTH {
237 return Err(Error::PacketTooShort);
238 }
239
240 let xr_header = XRHeader::unmarshal(raw_packet)?;
241 if xr_header.block_type != BlockType::StatisticsSummary {
242 return Err(Error::WrongType);
243 }
244 let block_length = match xr_header.block_length.checked_mul(4) {
245 Some(length) => length,
246 None => return Err(Error::InvalidBlockSize),
247 };
248 if block_length != SSR_REPORT_BLOCK_LENGTH || raw_packet.remaining() < block_length as usize
249 {
250 return Err(Error::PacketTooShort);
251 }
252
253 let loss_reports = xr_header.type_specific & 0x80 != 0;
254 let duplicate_reports = xr_header.type_specific & 0x40 != 0;
255 let jitter_reports = xr_header.type_specific & 0x20 != 0;
256 let ttl_or_hop_limit: TTLorHopLimitType = ((xr_header.type_specific & 0x18) >> 3).into();
257
258 let ssrc = raw_packet.get_u32();
259 let begin_seq = raw_packet.get_u16();
260 let end_seq = raw_packet.get_u16();
261 let lost_packets = raw_packet.get_u32();
262 let dup_packets = raw_packet.get_u32();
263 let min_jitter = raw_packet.get_u32();
264 let max_jitter = raw_packet.get_u32();
265 let mean_jitter = raw_packet.get_u32();
266 let dev_jitter = raw_packet.get_u32();
267 let min_ttl_or_hl = raw_packet.get_u8();
268 let max_ttl_or_hl = raw_packet.get_u8();
269 let mean_ttl_or_hl = raw_packet.get_u8();
270 let dev_ttl_or_hl = raw_packet.get_u8();
271
272 Ok(StatisticsSummaryReportBlock {
275 loss_reports,
276 duplicate_reports,
277 jitter_reports,
278 ttl_or_hop_limit,
279
280 ssrc,
281 begin_seq,
282 end_seq,
283 lost_packets,
284 dup_packets,
285 min_jitter,
286 max_jitter,
287 mean_jitter,
288 dev_jitter,
289 min_ttl_or_hl,
290 max_ttl_or_hl,
291 mean_ttl_or_hl,
292 dev_ttl_or_hl,
293 }
294 .reported())
295 }
296}