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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! RTCP Packet module
//!
//! This module provides structures for handling RTCP packets as defined in RFC 3550.
//! It includes implementations for different RTCP packet types: SR, RR, SDES, BYE, APP.
//! Extended Reports (XR) are defined in RFC 3611.
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use crate::error::Error;
use crate::{Result, RtpSsrc};
/// RTCP version (same as RTP, always 2)
pub const RTCP_VERSION: u8 = 2;
/// RTCP packet types as defined in RFC 3550
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum RtcpPacketType {
/// Sender Report (SR)
SenderReport = 200,
/// Receiver Report (RR)
ReceiverReport = 201,
/// Source Description (SDES)
SourceDescription = 202,
/// Goodbye (BYE)
Goodbye = 203,
/// Application-Defined (APP)
ApplicationDefined = 204,
/// Extended Reports (XR) as defined in RFC 3611
ExtendedReport = 207,
}
impl TryFrom<u8> for RtcpPacketType {
type Error = Error;
fn try_from(value: u8) -> Result<Self> {
match value {
200 => Ok(RtcpPacketType::SenderReport),
201 => Ok(RtcpPacketType::ReceiverReport),
202 => Ok(RtcpPacketType::SourceDescription),
203 => Ok(RtcpPacketType::Goodbye),
204 => Ok(RtcpPacketType::ApplicationDefined),
207 => Ok(RtcpPacketType::ExtendedReport),
_ => Err(Error::RtcpError(format!("Unknown RTCP packet type: {}", value))),
}
}
}
// Import and re-export types from submodules
mod sender_report;
mod receiver_report;
mod sdes;
mod bye;
mod app;
mod report_block;
mod ntp;
mod xr;
mod compound;
// Re-export all public types
pub use report_block::RtcpReportBlock;
pub use ntp::NtpTimestamp;
pub use sender_report::RtcpSenderReport;
pub use receiver_report::RtcpReceiverReport;
pub use sdes::{RtcpSourceDescription, RtcpSdesChunk, RtcpSdesItem, RtcpSdesItemType};
pub use bye::RtcpGoodbye;
pub use app::RtcpApplicationDefined;
pub use xr::{
RtcpExtendedReport, RtcpXrBlock, RtcpXrBlockType,
ReceiverReferenceTimeBlock, VoipMetricsBlock
};
pub use compound::RtcpCompoundPacket;
/// RTCP packet variants
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RtcpPacket {
/// Sender Report (SR)
SenderReport(RtcpSenderReport),
/// Receiver Report (RR)
ReceiverReport(RtcpReceiverReport),
/// Source Description (SDES)
SourceDescription(RtcpSourceDescription),
/// Goodbye (BYE)
Goodbye(RtcpGoodbye),
/// Application-Defined (APP)
ApplicationDefined(RtcpApplicationDefined),
/// Extended Reports (XR)
ExtendedReport(RtcpExtendedReport),
}
impl RtcpPacket {
/// Parse an RTCP packet from bytes
pub fn parse(data: &[u8]) -> Result<Self> {
let mut buf = Bytes::copy_from_slice(data);
// Parse common header (first 4 bytes)
if buf.remaining() < 4 {
return Err(Error::BufferTooSmall {
required: 4,
available: buf.remaining(),
});
}
let first_byte = buf.get_u8();
// Check version (2 bits)
let version = (first_byte >> 6) & 0x03;
if version != RTCP_VERSION {
return Err(Error::RtcpError(format!("Invalid RTCP version: {}", version)));
}
// Check padding flag (1 bit)
let _padding = ((first_byte >> 5) & 0x01) != 0;
// Get reception report count (5 bits)
let report_count = first_byte & 0x1F;
// Get packet type
let packet_type = RtcpPacketType::try_from(buf.get_u8())?;
// Get length in 32-bit words minus one (convert to bytes)
let length = buf.get_u16() as usize * 4;
if buf.remaining() < length {
return Err(Error::BufferTooSmall {
required: length,
available: buf.remaining(),
});
}
// Parse specific packet type
match packet_type {
RtcpPacketType::SenderReport => {
Ok(RtcpPacket::SenderReport(
sender_report::parse_sender_report(&mut buf, report_count)?
))
}
RtcpPacketType::ReceiverReport => {
Ok(RtcpPacket::ReceiverReport(
receiver_report::parse_receiver_report(&mut buf, report_count)?
))
}
RtcpPacketType::SourceDescription => {
Ok(RtcpPacket::SourceDescription(
RtcpSourceDescription { chunks: Vec::new() }
))
}
RtcpPacketType::Goodbye => {
Ok(RtcpPacket::Goodbye(
bye::parse_bye(&mut buf, report_count)?
))
}
RtcpPacketType::ApplicationDefined => {
Ok(RtcpPacket::ApplicationDefined(
app::parse_app(&mut buf)?
))
}
RtcpPacketType::ExtendedReport => {
Ok(RtcpPacket::ExtendedReport(
xr::parse_xr(&mut buf)?
))
}
}
}
/// Serialize an RTCP packet to bytes
pub fn serialize(&self) -> Result<Bytes> {
let mut buf = BytesMut::new();
match self {
RtcpPacket::SenderReport(sr) => {
// Create a buffer for the SR content
let sr_content = sender_report::serialize_sender_report(sr)?;
let content_size = sr_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | report count (5 bits)
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | (sr.report_blocks.len() as u8 & 0x1F);
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::SenderReport as u8);
// Write length
buf.put_u16(length as u16);
// Write SR content
buf.extend_from_slice(&sr_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
RtcpPacket::ReceiverReport(rr) => {
// Create a buffer for the RR content
let rr_content = receiver_report::serialize_receiver_report(rr)?;
let content_size = rr_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | report count (5 bits)
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | (rr.report_blocks.len() as u8 & 0x1F);
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::ReceiverReport as u8);
// Write length
buf.put_u16(length as u16);
// Write RR content
buf.extend_from_slice(&rr_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
RtcpPacket::SourceDescription(sdes) => {
// Create a buffer for the SDES content
let sdes_content = sdes::serialize_sdes(sdes)?;
let content_size = sdes_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | chunk count (5 bits)
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | (sdes.chunks.len() as u8 & 0x1F);
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::SourceDescription as u8);
// Write length
buf.put_u16(length as u16);
// Write SDES content
buf.extend_from_slice(&sdes_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
RtcpPacket::Goodbye(bye) => {
// Create a buffer for the BYE packet content
let bye_content = bye.serialize()?;
let content_size = bye_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | source count (5 bits)
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | (bye.sources.len() as u8 & 0x1F);
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::Goodbye as u8);
// Write length
buf.put_u16(length as u16);
// Write BYE content
buf.extend_from_slice(&bye_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
RtcpPacket::ApplicationDefined(app) => {
// Create a buffer for the APP packet content
let app_content = app.serialize()?;
let content_size = app_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | subtype (5 bits)
// For APP packets, subtype is always 0 in this implementation
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | 0;
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::ApplicationDefined as u8);
// Write length
buf.put_u16(length as u16);
// Write APP content
buf.extend_from_slice(&app_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
RtcpPacket::ExtendedReport(xr) => {
// Create a buffer for the XR packet content
let xr_content = xr.serialize()?;
let content_size = xr_content.len();
// Calculate length in 32-bit words minus one
let words = (content_size + 4) / 4; // content plus header, in 32-bit words
let length = words - 1; // minus one as per RFC
// Write header
// First byte: version (2 bits) | padding (1 bit) | reserved (5 bits)
let first_byte = (RTCP_VERSION << 6) | (0 << 5) | 0;
buf.put_u8(first_byte);
// Write packet type
buf.put_u8(RtcpPacketType::ExtendedReport as u8);
// Write length
buf.put_u16(length as u16);
// Write XR content
buf.extend_from_slice(&xr_content);
// Pad to 32-bit boundary if needed
let padding_bytes = (4 - (buf.len() % 4)) % 4;
for _ in 0..padding_bytes {
buf.put_u8(0);
}
}
}
Ok(buf.freeze())
}
/// Get the RTCP packet type
pub fn packet_type(&self) -> RtcpPacketType {
match self {
RtcpPacket::SenderReport(_) => RtcpPacketType::SenderReport,
RtcpPacket::ReceiverReport(_) => RtcpPacketType::ReceiverReport,
RtcpPacket::SourceDescription(_) => RtcpPacketType::SourceDescription,
RtcpPacket::Goodbye(_) => RtcpPacketType::Goodbye,
RtcpPacket::ApplicationDefined(_) => RtcpPacketType::ApplicationDefined,
RtcpPacket::ExtendedReport(_) => RtcpPacketType::ExtendedReport,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rtcp_packet_type_conversion() {
assert_eq!(RtcpPacketType::try_from(200).unwrap(), RtcpPacketType::SenderReport);
assert_eq!(RtcpPacketType::try_from(201).unwrap(), RtcpPacketType::ReceiverReport);
assert_eq!(RtcpPacketType::try_from(202).unwrap(), RtcpPacketType::SourceDescription);
assert_eq!(RtcpPacketType::try_from(203).unwrap(), RtcpPacketType::Goodbye);
assert_eq!(RtcpPacketType::try_from(204).unwrap(), RtcpPacketType::ApplicationDefined);
assert_eq!(RtcpPacketType::try_from(207).unwrap(), RtcpPacketType::ExtendedReport);
assert!(RtcpPacketType::try_from(100).is_err());
}
}