oximedia-net 0.1.8

Network streaming for OxiMedia
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! RTCP (RTP Control Protocol) implementation.
//!
//! This module implements RTCP packet handling for WebRTC media control.

#![allow(dead_code)]
#![allow(clippy::too_many_arguments)]

use crate::error::{NetError, NetResult};
use bytes::{Buf, BufMut, Bytes, BytesMut};

/// RTCP packet type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PacketType {
    /// Sender Report.
    SR = 200,
    /// Receiver Report.
    RR = 201,
    /// Source Description.
    SDES = 202,
    /// Goodbye.
    BYE = 203,
    /// Application-defined.
    APP = 204,
    /// Transport layer feedback.
    RTPFB = 205,
    /// Payload-specific feedback.
    PSFB = 206,
}

impl PacketType {
    /// Parses from byte value.
    #[must_use]
    pub const fn from_u8(value: u8) -> Option<Self> {
        match value {
            200 => Some(Self::SR),
            201 => Some(Self::RR),
            202 => Some(Self::SDES),
            203 => Some(Self::BYE),
            204 => Some(Self::APP),
            205 => Some(Self::RTPFB),
            206 => Some(Self::PSFB),
            _ => None,
        }
    }
}

/// RTCP Sender Report.
#[derive(Debug, Clone)]
pub struct SenderReport {
    /// SSRC of sender.
    pub ssrc: u32,
    /// NTP timestamp (most significant word).
    pub ntp_timestamp_msw: u32,
    /// NTP timestamp (least significant word).
    pub ntp_timestamp_lsw: u32,
    /// RTP timestamp.
    pub rtp_timestamp: u32,
    /// Sender packet count.
    pub sender_packet_count: u32,
    /// Sender octet count.
    pub sender_octet_count: u32,
    /// Report blocks.
    pub reports: Vec<ReportBlock>,
}

impl SenderReport {
    /// Creates a new sender report.
    #[must_use]
    pub const fn new(ssrc: u32) -> Self {
        Self {
            ssrc,
            ntp_timestamp_msw: 0,
            ntp_timestamp_lsw: 0,
            rtp_timestamp: 0,
            sender_packet_count: 0,
            sender_octet_count: 0,
            reports: Vec::new(),
        }
    }

    /// Sets NTP timestamp.
    #[must_use]
    pub const fn with_ntp_timestamp(mut self, msw: u32, lsw: u32) -> Self {
        self.ntp_timestamp_msw = msw;
        self.ntp_timestamp_lsw = lsw;
        self
    }

    /// Sets RTP timestamp.
    #[must_use]
    pub const fn with_rtp_timestamp(mut self, timestamp: u32) -> Self {
        self.rtp_timestamp = timestamp;
        self
    }

    /// Sets packet count.
    #[must_use]
    pub const fn with_packet_count(mut self, count: u32) -> Self {
        self.sender_packet_count = count;
        self
    }

    /// Sets octet count.
    #[must_use]
    pub const fn with_octet_count(mut self, count: u32) -> Self {
        self.sender_octet_count = count;
        self
    }

    /// Adds a report block.
    #[must_use]
    pub fn with_report(mut self, report: ReportBlock) -> Self {
        self.reports.push(report);
        self
    }

    /// Encodes the sender report.
    #[must_use]
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::new();

        // RTCP header
        let rc = self.reports.len().min(31) as u8;
        buf.put_u8((2 << 6) | rc); // V=2, P=0, RC
        buf.put_u8(PacketType::SR as u8);

        // Length in 32-bit words - 1
        let length = 6 + self.reports.len() * 6;
        buf.put_u16(length as u16);

        // SR fields
        buf.put_u32(self.ssrc);
        buf.put_u32(self.ntp_timestamp_msw);
        buf.put_u32(self.ntp_timestamp_lsw);
        buf.put_u32(self.rtp_timestamp);
        buf.put_u32(self.sender_packet_count);
        buf.put_u32(self.sender_octet_count);

        // Report blocks
        for report in &self.reports {
            buf.put(report.encode());
        }

        buf.freeze()
    }

    /// Parses a sender report.
    pub fn parse(data: &[u8]) -> NetResult<Self> {
        if data.len() < 28 {
            return Err(NetError::parse(0, "SR packet too short"));
        }

        let mut cursor = Bytes::copy_from_slice(data);

        let byte0 = cursor.get_u8();
        let rc = byte0 & 0x1F;
        let _pt = cursor.get_u8();
        let _length = cursor.get_u16();

        let ssrc = cursor.get_u32();
        let ntp_timestamp_msw = cursor.get_u32();
        let ntp_timestamp_lsw = cursor.get_u32();
        let rtp_timestamp = cursor.get_u32();
        let sender_packet_count = cursor.get_u32();
        let sender_octet_count = cursor.get_u32();

        let mut reports = Vec::new();
        for _ in 0..rc {
            if cursor.remaining() >= 24 {
                reports.push(ReportBlock::parse(&mut cursor)?);
            }
        }

        Ok(Self {
            ssrc,
            ntp_timestamp_msw,
            ntp_timestamp_lsw,
            rtp_timestamp,
            sender_packet_count,
            sender_octet_count,
            reports,
        })
    }
}

/// RTCP Receiver Report.
#[derive(Debug, Clone)]
pub struct ReceiverReport {
    /// SSRC of receiver.
    pub ssrc: u32,
    /// Report blocks.
    pub reports: Vec<ReportBlock>,
}

impl ReceiverReport {
    /// Creates a new receiver report.
    #[must_use]
    pub const fn new(ssrc: u32) -> Self {
        Self {
            ssrc,
            reports: Vec::new(),
        }
    }

    /// Adds a report block.
    #[must_use]
    pub fn with_report(mut self, report: ReportBlock) -> Self {
        self.reports.push(report);
        self
    }

    /// Encodes the receiver report.
    #[must_use]
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::new();

        // RTCP header
        let rc = self.reports.len().min(31) as u8;
        buf.put_u8((2 << 6) | rc); // V=2, P=0, RC
        buf.put_u8(PacketType::RR as u8);

        // Length in 32-bit words - 1
        let length = 1 + self.reports.len() * 6;
        buf.put_u16(length as u16);

        // RR fields
        buf.put_u32(self.ssrc);

        // Report blocks
        for report in &self.reports {
            buf.put(report.encode());
        }

        buf.freeze()
    }

    /// Parses a receiver report.
    pub fn parse(data: &[u8]) -> NetResult<Self> {
        if data.len() < 8 {
            return Err(NetError::parse(0, "RR packet too short"));
        }

        let mut cursor = Bytes::copy_from_slice(data);

        let byte0 = cursor.get_u8();
        let rc = byte0 & 0x1F;
        let _pt = cursor.get_u8();
        let _length = cursor.get_u16();

        let ssrc = cursor.get_u32();

        let mut reports = Vec::new();
        for _ in 0..rc {
            if cursor.remaining() >= 24 {
                reports.push(ReportBlock::parse(&mut cursor)?);
            }
        }

        Ok(Self { ssrc, reports })
    }
}

/// RTCP Report Block.
#[derive(Debug, Clone)]
pub struct ReportBlock {
    /// SSRC of source.
    pub ssrc: u32,
    /// Fraction lost.
    pub fraction_lost: u8,
    /// Cumulative packets lost.
    pub packets_lost: i32,
    /// Extended highest sequence number.
    pub extended_highest_seq: u32,
    /// Interarrival jitter.
    pub jitter: u32,
    /// Last SR timestamp.
    pub last_sr: u32,
    /// Delay since last SR.
    pub delay_since_last_sr: u32,
}

impl ReportBlock {
    /// Creates a new report block.
    #[must_use]
    pub const fn new(ssrc: u32) -> Self {
        Self {
            ssrc,
            fraction_lost: 0,
            packets_lost: 0,
            extended_highest_seq: 0,
            jitter: 0,
            last_sr: 0,
            delay_since_last_sr: 0,
        }
    }

    /// Encodes the report block.
    #[must_use]
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::new();

        buf.put_u32(self.ssrc);
        buf.put_u8(self.fraction_lost);

        // Packets lost (24-bit signed)
        let packets_lost = self.packets_lost & 0x00FF_FFFF;
        buf.put_u8((packets_lost >> 16) as u8);
        buf.put_u8((packets_lost >> 8) as u8);
        buf.put_u8(packets_lost as u8);

        buf.put_u32(self.extended_highest_seq);
        buf.put_u32(self.jitter);
        buf.put_u32(self.last_sr);
        buf.put_u32(self.delay_since_last_sr);

        buf.freeze()
    }

    /// Parses a report block.
    pub fn parse(cursor: &mut Bytes) -> NetResult<Self> {
        if cursor.remaining() < 24 {
            return Err(NetError::parse(0, "Report block too short"));
        }

        let ssrc = cursor.get_u32();
        let fraction_lost = cursor.get_u8();

        // Packets lost (24-bit signed)
        let lost_hi = cursor.get_u8();
        let lost_mid = cursor.get_u8();
        let lost_lo = cursor.get_u8();
        let packets_lost =
            ((i32::from(lost_hi) << 16) | (i32::from(lost_mid) << 8) | i32::from(lost_lo)) as i32;

        let extended_highest_seq = cursor.get_u32();
        let jitter = cursor.get_u32();
        let last_sr = cursor.get_u32();
        let delay_since_last_sr = cursor.get_u32();

        Ok(Self {
            ssrc,
            fraction_lost,
            packets_lost,
            extended_highest_seq,
            jitter,
            last_sr,
            delay_since_last_sr,
        })
    }
}

/// RTCP packet.
#[derive(Debug, Clone)]
pub enum Packet {
    /// Sender report.
    SR(SenderReport),
    /// Receiver report.
    RR(ReceiverReport),
}

impl Packet {
    /// Encodes the packet.
    #[must_use]
    pub fn encode(&self) -> Bytes {
        match self {
            Self::SR(sr) => sr.encode(),
            Self::RR(rr) => rr.encode(),
        }
    }

    /// Parses a packet.
    pub fn parse(data: &[u8]) -> NetResult<Self> {
        if data.len() < 4 {
            return Err(NetError::parse(0, "RTCP packet too short"));
        }

        let packet_type = data[1];

        match PacketType::from_u8(packet_type) {
            Some(PacketType::SR) => Ok(Self::SR(SenderReport::parse(data)?)),
            Some(PacketType::RR) => Ok(Self::RR(ReceiverReport::parse(data)?)),
            _ => Err(NetError::parse(0, "Unsupported RTCP packet type")),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_packet_type() {
        assert_eq!(PacketType::from_u8(200), Some(PacketType::SR));
        assert_eq!(PacketType::from_u8(201), Some(PacketType::RR));
    }

    #[test]
    fn test_sender_report() {
        let sr = SenderReport::new(12345)
            .with_ntp_timestamp(100, 200)
            .with_rtp_timestamp(48000)
            .with_packet_count(1000)
            .with_octet_count(50000);

        assert_eq!(sr.ssrc, 12345);
        assert_eq!(sr.ntp_timestamp_msw, 100);
        assert_eq!(sr.rtp_timestamp, 48000);
    }

    #[test]
    fn test_sender_report_encode_decode() {
        let sr = SenderReport::new(12345)
            .with_ntp_timestamp(100, 200)
            .with_rtp_timestamp(48000)
            .with_packet_count(1000)
            .with_octet_count(50000);

        let encoded = sr.encode();
        let decoded = SenderReport::parse(&encoded).expect("should succeed in test");

        assert_eq!(decoded.ssrc, sr.ssrc);
        assert_eq!(decoded.ntp_timestamp_msw, sr.ntp_timestamp_msw);
        assert_eq!(decoded.rtp_timestamp, sr.rtp_timestamp);
    }

    #[test]
    fn test_receiver_report() {
        let rr = ReceiverReport::new(12345);
        assert_eq!(rr.ssrc, 12345);
        assert!(rr.reports.is_empty());
    }

    #[test]
    fn test_receiver_report_encode_decode() {
        let rr = ReceiverReport::new(12345);
        let encoded = rr.encode();
        let decoded = ReceiverReport::parse(&encoded).expect("should succeed in test");

        assert_eq!(decoded.ssrc, rr.ssrc);
        assert_eq!(decoded.reports.len(), 0);
    }

    #[test]
    fn test_report_block() {
        let block = ReportBlock::new(54321);
        let encoded = block.encode();
        assert_eq!(encoded.len(), 24);
    }

    #[test]
    fn test_packet_parse_sr() {
        let sr = SenderReport::new(12345).with_packet_count(100);
        let encoded = sr.encode();

        let packet = Packet::parse(&encoded).expect("should succeed in test");
        match packet {
            Packet::SR(parsed_sr) => {
                assert_eq!(parsed_sr.ssrc, 12345);
            }
            _ => panic!("Expected SR packet"),
        }
    }
}