rtci-interceptor 0.1.0

Sans-I/O RTP/RTCP interceptor pipeline for WebRTC: RTCP reports, NACK, TWCC, congestion control, pacing, FEC
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
use std::time::Instant;

/// RFC 3550 Appendix A.1: a forward jump of up to this many sequence numbers is a gap (loss), a
/// larger one is a source that restarted (or a wild packet).
const MAX_DROPOUT: u16 = 3000;
/// RFC 3550 Appendix A.1: a packet up to this many sequence numbers behind the highest seen is
/// late or a duplicate, one further behind is a source that restarted (or a wild packet).
const MAX_MISORDER: u16 = 100;
/// 2^16, the modulus of RTP sequence numbers.
const RTP_SEQ_MOD: u32 = 1 << 16;

/// The largest and smallest cumulative number of packets lost a report can carry: the field is
/// a signed 24-bit integer (RFC 3550 section 6.4.1), and Appendix A.3 clamps to it.
const MAX_CUMULATIVE_LOST: i64 = 0x7F_FFFF;
const MIN_CUMULATIVE_LOST: i64 = -0x80_0000;

/// The reception statistics of one remote SSRC, kept exactly as RFC 3550 Appendix A.1 (sequence
/// numbers), A.3 (expected, lost, fraction lost) and A.8 (interarrival jitter) describe.
///
/// Appendix A.1's probation (`MIN_SEQUENTIAL`) is not used: a stream is bound from the
/// negotiated description and its packets are SRTP authenticated, so the first packet of a
/// source is taken as its first.
pub(crate) struct ReceiverStream {
    ssrc: u32,
    receiver_ssrc: u32,
    clock_rate: f64,
    /// The clock rate of each other payload type the source may switch to (RFC 7160).
    payload_clock_rates: Vec<(u8, f64)>,
    /// The clock rate of the previous packet's payload type.
    last_clock_rate: f64,

    /// Whether a packet has been received (the sequence state below is initialised).
    started: bool,
    /// The highest sequence number seen (`max_seq`).
    max_seq: u16,
    /// The count of sequence number cycles, shifted: a multiple of 2^16 (`cycles`).
    cycles: u32,
    /// The first sequence number seen (`base_seq`).
    base_seq: u32,
    /// The last "bad" sequence number plus one (`bad_seq`); 2^16 + 1 when there is none.
    bad_seq: u32,
    /// Packets received, late ones and duplicates included (`received`).
    received: u32,
    /// `expected` at the previous report (`expected_prior`).
    expected_prior: u32,
    /// `received` at the previous report (`received_prior`).
    received_prior: u32,

    /// The relative transit time of the previous packet, in timestamp units (`transit`).
    last_rtp_time_rtp: u32,
    last_rtp_time_time: Option<Instant>,
    jitter: f64,
    last_sender_report: u32,
    last_sender_report_time: Option<Instant>,
}

impl ReceiverStream {
    pub(crate) fn new(ssrc: u32, clock_rate: u32) -> Self {
        Self {
            ssrc,
            receiver_ssrc: rand::random::<u32>(),
            clock_rate: clock_rate as f64,
            payload_clock_rates: vec![],
            last_clock_rate: clock_rate as f64,

            started: false,
            max_seq: 0,
            cycles: 0,
            base_seq: 0,
            bad_seq: RTP_SEQ_MOD + 1,
            received: 0,
            expected_prior: 0,
            received_prior: 0,

            last_rtp_time_rtp: 0,
            last_rtp_time_time: None,
            jitter: 0.0,
            last_sender_report: 0,
            last_sender_report_time: None,
        }
    }

    /// The SSRC this stream reports on.
    pub(crate) fn ssrc(&self) -> u32 {
        self.ssrc
    }

    /// The SSRC this stream's receiver reports (and XR blocks) are sent from.
    pub(crate) fn receiver_ssrc(&self) -> u32 {
        self.receiver_ssrc
    }

    /// The clock rate of the codec the stream is received with.
    pub(crate) fn set_clock_rate(&mut self, clock_rate: u32) {
        self.clock_rate = clock_rate as f64;
    }

    /// The clock rates of the payload types the source may switch to (RFC 7160): a packet of
    /// one of them is timed with its own rate.
    pub(crate) fn set_payload_clock_rates(&mut self, rates: &[(u8, u32)]) {
        self.payload_clock_rates = rates
            .iter()
            .filter(|(_, rate)| *rate > 0)
            .map(|(pt, rate)| (*pt, f64::from(*rate)))
            .collect();
    }

    fn clock_rate_of(&self, payload_type: u8) -> f64 {
        self.payload_clock_rates
            .iter()
            .find(|(pt, _)| *pt == payload_type)
            .map_or(self.clock_rate, |(_, rate)| *rate)
    }

    /// Whether any packet of the stream has been received.
    pub(crate) fn has_received(&self) -> bool {
        self.started
    }

    /// When the stream's last packet arrived.
    pub(crate) fn last_received(&self) -> Option<Instant> {
        self.last_rtp_time_time
    }

    /// RFC 3550 Appendix A.1 `init_seq`.
    fn init_seq(&mut self, seq: u16) {
        self.base_seq = u32::from(seq);
        self.max_seq = seq;
        self.bad_seq = RTP_SEQ_MOD + 1;
        self.cycles = 0;
        self.received = 0;
        self.received_prior = 0;
        self.expected_prior = 0;
    }

    /// RFC 3550 Appendix A.1 `update_seq`, without the probation: whether the packet belongs
    /// to the sequence and was counted.
    fn update_seq(&mut self, seq: u16) -> bool {
        if !self.started {
            self.started = true;
            self.init_seq(seq);
            self.received = 1;
            return true;
        }

        let udelta = seq.wrapping_sub(self.max_seq);
        if udelta < MAX_DROPOUT {
            // In order, with a permissible gap.
            if seq < self.max_seq {
                // The sequence number wrapped: count another 64K cycle.
                self.cycles = self.cycles.wrapping_add(RTP_SEQ_MOD);
            }
            self.max_seq = seq;
        } else if u32::from(udelta) <= RTP_SEQ_MOD - u32::from(MAX_MISORDER) {
            // The sequence number made a very large jump.
            if u32::from(seq) == self.bad_seq {
                // Two sequential packets: the other side restarted without telling us, so
                // re-sync as if this were the first packet.
                self.init_seq(seq);
            } else {
                self.bad_seq = (u32::from(seq) + 1) & (RTP_SEQ_MOD - 1);
                return false;
            }
        } else {
            // A duplicate or a reordered packet. One older than the first packet received
            // was overtaken by it on the way: it extends the sequence back, so `base_seq` is
            // the lowest sequence number received and the packet is expected as well as
            // received. Counting it only as received would leave the cumulative number lost
            // at -1 for the rest of the session on a path that lost nothing (RFC 3550 6.4.1:
            // expected is "the highest sequence number received less the initial one", the
            // initial one being the start of the sequence, whatever order it arrived in).
            let extended = if seq <= self.max_seq {
                self.cycles.wrapping_add(u32::from(seq))
            } else {
                // Before the most recent wrap.
                self.cycles
                    .wrapping_sub(RTP_SEQ_MOD)
                    .wrapping_add(u32::from(seq))
            };
            let before_base = self.base_seq.wrapping_sub(extended);
            if before_base != 0 && before_base < 1 << 31 {
                self.base_seq = extended;
            }
        }
        self.received = self.received.wrapping_add(1);
        true
    }

    pub(crate) fn process_rtp(&mut self, now: Instant, pkt: &rtp::packet::Packet) {
        if !self.update_seq(pkt.header.sequence_number) {
            return;
        }

        // RFC 3550 Appendix A.8, for every packet of the sequence, in arrival order:
        // D = (Rj - Ri) - (Sj - Si), J += (|D| - J) / 16. The timestamp difference is taken
        // signed, so a wrap of the 32-bit timestamp is a small step, not a jump of 2^32.
        // RFC 7160 section 4.3: across a change of clock rate the arrival times are taken in
        // the clock of the earlier packet, so a path without jitter shows none.
        let clock_rate = self.clock_rate_of(pkt.header.payload_type);
        if let Some(last_rtp_time_time) = self.last_rtp_time_time {
            let d = now.duration_since(last_rtp_time_time).as_secs_f64() * self.last_clock_rate
                - pkt.header.timestamp.wrapping_sub(self.last_rtp_time_rtp) as i32 as f64;
            self.jitter += (d.abs() - self.jitter) / 16.0;
        }

        self.last_clock_rate = clock_rate;
        self.last_rtp_time_rtp = pkt.header.timestamp;
        self.last_rtp_time_time = Some(now);
    }

    pub(crate) fn process_sender_report(
        &mut self,
        now: Instant,
        sr: &rtcp::sender_report::SenderReport,
    ) {
        self.last_sender_report = (sr.ntp_time >> 16) as u32;
        self.last_sender_report_time = Some(now);
    }

    /// The reception report block for this stream (RFC 3550 section 6.4.1, Appendix A.3),
    /// closing the reporting interval: the next block's fraction lost counts from here.
    /// The fraction lost (in 1/256) a report made now would carry, without making one: the
    /// interval's state is left as it is.
    pub(crate) fn pending_fraction_lost(&self) -> u8 {
        if !self.started {
            return 0;
        }
        let extended_max = self.cycles.wrapping_add(u32::from(self.max_seq));
        let expected = extended_max.wrapping_sub(self.base_seq).wrapping_add(1);
        let expected_interval = expected.wrapping_sub(self.expected_prior);
        let received_interval = self.received.wrapping_sub(self.received_prior);
        let lost_interval = i64::from(expected_interval) - i64::from(received_interval);
        if expected_interval == 0 || lost_interval <= 0 {
            0
        } else {
            ((lost_interval << 8) / i64::from(expected_interval)).min(255) as u8
        }
    }

    pub(crate) fn generate_reception_report(
        &mut self,
        now: Instant,
    ) -> rtcp::reception_report::ReceptionReport {
        // Appendix A.3: the extended highest sequence number, the packets expected, and the
        // cumulative number lost as expected less received. Late packets were counted when
        // they arrived, so they are not lost; duplicates were counted too, so the number can
        // be negative. It is clamped to the 24 bits it is carried in.
        let extended_max = self.cycles.wrapping_add(u32::from(self.max_seq));
        let expected = if self.started {
            extended_max.wrapping_sub(self.base_seq).wrapping_add(1)
        } else {
            0
        };
        let lost = (i64::from(expected) - i64::from(self.received))
            .clamp(MIN_CUMULATIVE_LOST, MAX_CUMULATIVE_LOST);

        // The fraction lost over the interval since the previous report; zero when duplicates
        // make the interval's loss negative.
        let expected_interval = expected.wrapping_sub(self.expected_prior);
        self.expected_prior = expected;
        let received_interval = self.received.wrapping_sub(self.received_prior);
        self.received_prior = self.received;
        let lost_interval = i64::from(expected_interval) - i64::from(received_interval);
        let fraction_lost = if expected_interval == 0 || lost_interval <= 0 {
            0
        } else {
            ((lost_interval << 8) / i64::from(expected_interval)).min(255) as u8
        };

        // DLSR: the delay since the last sender report, in 1/65536 s; zero when there is none.
        let delay = match self.last_sender_report_time {
            Some(sr_time) => (now.duration_since(sr_time).as_secs_f64() * 65536.0) as u64 as u32,
            None => 0,
        };

        let mut report = rtcp::reception_report::ReceptionReport {
            ssrc: self.ssrc,
            last_sequence_number: extended_max,
            last_sender_report: self.last_sender_report,
            fraction_lost,
            total_lost: 0,
            delay,
            jitter: self.jitter as u32,
        };
        report.set_cumulative_lost(lost as i32);
        report
    }

    pub(crate) fn generate_report(
        &mut self,
        now: Instant,
    ) -> rtcp::receiver_report::ReceiverReport {
        rtcp::receiver_report::ReceiverReport {
            ssrc: self.receiver_ssrc,
            reports: vec![self.generate_reception_report(now)],
            ..Default::default()
        }
    }
}

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

    fn make_rtp_packet(seq: u16, timestamp: u32) -> rtp::packet::Packet {
        rtp::packet::Packet {
            header: rtp::header::Header {
                sequence_number: seq,
                timestamp,
                ..Default::default()
            },
            ..Default::default()
        }
    }

    #[test]
    fn test_receiver_stream_before_any_packet() {
        // Port of pion's TestReceiverInterceptor/before any packet
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        assert_eq!(rr.reports[0].last_sequence_number, 0);
        assert_eq!(rr.reports[0].last_sender_report, 0);
        assert_eq!(rr.reports[0].fraction_lost, 0);
        assert_eq!(rr.reports[0].total_lost, 0);
        assert_eq!(rr.reports[0].delay, 0);
        assert_eq!(rr.reports[0].jitter, 0);
    }

    #[test]
    fn test_receiver_stream_after_rtp_packets() {
        // Port of pion's TestReceiverInterceptor/after RTP packets
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        for i in 0..10u16 {
            let pkt = make_rtp_packet(i, 0);
            stream.process_rtp(now, &pkt);
        }

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        assert_eq!(rr.reports[0].last_sequence_number, 9);
        assert_eq!(rr.reports[0].last_sender_report, 0);
        assert_eq!(rr.reports[0].fraction_lost, 0);
        assert_eq!(rr.reports[0].total_lost, 0);
        assert_eq!(rr.reports[0].delay, 0);
        assert_eq!(rr.reports[0].jitter, 0);
    }

    /// RFC 3550 6.4.1: a stream whose first packet was overtaken by the second loses nothing,
    /// so once every packet is in, the cumulative number lost is 0 (it stayed at -1: the late
    /// first packet was counted as received but not as expected). Also across the sequence
    /// number wrap, where the overtaken packet is from the previous cycle.
    #[test]
    fn a_first_packet_overtaken_by_the_next_is_not_counted_as_a_duplicate() {
        for first in [100u16, 65535] {
            let mut stream = ReceiverStream::new(123456, 48000);
            let now = Instant::now();
            let order = [1u16, 0, 2, 4, 3, 5];
            for i in order {
                stream.process_rtp(now, &make_rtp_packet(first.wrapping_add(i), 0));
            }
            let rr = stream.generate_report(now);
            assert_eq!(rr.reports[0].total_lost, 0, "first {first}");
            assert_eq!(rr.reports[0].fraction_lost, 0, "first {first}");
            // The cycle count starts at the first packet received (sequence number 0 when
            // 65535 was overtaken).
            let highest = u32::from(first.wrapping_add(5));
            assert_eq!(rr.reports[0].last_sequence_number, highest, "first {first}");
        }
    }

    /// A reordered packet inside the sequence leaves its start where it is: a loss before the
    /// late packet still counts.
    #[test]
    fn a_reordered_packet_after_the_first_does_not_move_the_start() {
        let mut stream = ReceiverStream::new(123456, 48000);
        let now = Instant::now();
        for i in [10u16, 12, 11, 14] {
            stream.process_rtp(now, &make_rtp_packet(i, 0));
        }
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].total_lost, 1, "13 is missing");
    }

    #[test]
    fn test_receiver_stream_jitter_ts_wrap() {
        // steady 20 ms at 48 khz across u32::MAX: was ~2^32 / 16
        let mut stream = ReceiverStream::new(123456, 48000);
        let now = Instant::now();
        for i in 0..4u16 {
            let ts = (u32::MAX - 1919).wrapping_add(960 * i as u32);
            stream.process_rtp(
                now + Duration::from_millis(20 * i as u64),
                &make_rtp_packet(i, ts),
            );
        }
        assert_eq!(stream.generate_report(now).reports[0].jitter, 0);
    }

    #[test]
    fn test_receiver_stream_jitter_delay_across_ts_wrap() {
        // 100 ms late right after the wrap: d = 4800, jitter = 4800 / 16
        let mut stream = ReceiverStream::new(123456, 48000);
        let now = Instant::now();
        stream.process_rtp(now, &make_rtp_packet(1, u32::MAX - 959));
        stream.process_rtp(now + Duration::from_millis(120), &make_rtp_packet(2, 0));
        assert_eq!(stream.generate_report(now).reports[0].jitter, 300);
    }

    #[test]
    fn test_receiver_stream_overflow() {
        // Port of pion's TestReceiverInterceptor/overflow
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Receive packet at 0xffff
        stream.process_rtp(now, &make_rtp_packet(0xffff, 0));

        // Wrap to 0x00
        stream.process_rtp(now, &make_rtp_packet(0x00, 0));

        // Out-of-order packet 0xfffe: it does not move the highest sequence number
        stream.process_rtp(now, &make_rtp_packet(0xfffe, 0));

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        // Extended sequence number should show 1 cycle (1 << 16)
        assert_eq!(rr.reports[0].last_sequence_number, 1 << 16);
        assert_eq!(rr.reports[0].fraction_lost, 0);
        // RFC 3550 6.4.1: 0xfffe was sent before the first packet received and overtaken by
        // it: the sequence starts there, three packets expected and three received. (Counted
        // as a duplicate, it left the cumulative loss at -1 on a path that lost nothing.)
        assert_eq!(rr.reports[0].cumulative_lost(), 0);
    }

    #[test]
    fn test_receiver_stream_packet_loss() {
        // Port of pion's TestReceiverInterceptor/packet loss
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Receive packet 1
        stream.process_rtp(now, &make_rtp_packet(0x01, 0));

        // Skip packet 2, receive packet 3
        stream.process_rtp(now, &make_rtp_packet(0x03, 0));

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        assert_eq!(rr.reports[0].last_sequence_number, 0x03);
        // fraction_lost = 256 * 1 / 3 = 85
        assert_eq!(rr.reports[0].fraction_lost, (256u32 / 3) as u8);
        assert_eq!(rr.reports[0].total_lost, 1);
    }

    #[test]
    fn test_receiver_stream_overflow_and_packet_loss() {
        // Port of pion's TestReceiverInterceptor/overflow and packet loss
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Receive packet 0xffff
        stream.process_rtp(now, &make_rtp_packet(0xffff, 0));

        // Skip 0x00, receive 0x01
        stream.process_rtp(now, &make_rtp_packet(0x01, 0));

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        // Extended sequence number: 1 cycle + 0x01
        assert_eq!(rr.reports[0].last_sequence_number, (1 << 16) | 0x01);
        // fraction_lost = 256 * 1 / 3 = 85
        assert_eq!(rr.reports[0].fraction_lost, (256u32 / 3) as u8);
        assert_eq!(rr.reports[0].total_lost, 1);
    }

    #[test]
    fn test_receiver_stream_reordered_packets() {
        // Port of pion's TestReceiverInterceptor/reordered packets
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Receive packets in order: 1, 3, 2, 4
        for seq in [0x01u16, 0x03, 0x02, 0x04] {
            stream.process_rtp(now, &make_rtp_packet(seq, 0));
        }

        let rr = stream.generate_report(now);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        assert_eq!(rr.reports[0].last_sequence_number, 0x04);
        // No loss because packet 2 arrived (just out of order)
        assert_eq!(rr.reports[0].fraction_lost, 0);
        assert_eq!(rr.reports[0].total_lost, 0);
    }

    /// RFC 4733 section 2.5.2.2: the jitter is computed over every packet of the SSRC, the
    /// packets of a telephone event that repeat its timestamp included, each in its own clock
    /// (`telephone-event/8000` on a 48 kHz Opus stream, RFC 7160 section 4.3).
    #[test]
    fn telephone_event_packets_with_a_repeated_timestamp_count_in_the_jitter() {
        let mut stream = ReceiverStream::new(1, 48_000);
        stream.set_payload_clock_rates(&[(101, 8_000)]);
        let base = Instant::now();
        let packet = |seq: u16, payload_type: u8, timestamp: u32| rtp::packet::Packet {
            header: rtp::header::Header {
                sequence_number: seq,
                payload_type,
                timestamp,
                ..Default::default()
            },
            ..Default::default()
        };
        // Opus every 20 ms on a steady path: no jitter.
        for n in 0..5u16 {
            let at = base + Duration::from_millis(20 * u64::from(n));
            stream.process_rtp(at, &packet(n, 111, 960 * u32::from(n)));
        }
        // The event starts 20 ms after the last Opus packet and is repeated 50 ms later with
        // the same timestamp: D = 0.05 s * 8000 = 400, J = 400 / 16.
        stream.process_rtp(base + Duration::from_millis(100), &packet(5, 101, 4_800));
        assert_eq!(0, stream.generate_report(base).reports[0].jitter);
        stream.process_rtp(base + Duration::from_millis(150), &packet(6, 101, 4_800));
        assert_eq!(25, stream.generate_report(base).reports[0].jitter);
    }

    #[test]
    fn test_receiver_stream_jitter() {
        // Port of pion's TestReceiverInterceptor/jitter
        let mut stream = ReceiverStream::new(123456, 90000);
        let base_time = Instant::now();

        // First packet
        stream.process_rtp(base_time, &make_rtp_packet(0x01, 42378934));

        // Second packet arrives 1 second later, but RTP timestamp only advances by 60000
        // (should be 90000 for 1 second at 90kHz clock rate)
        // D = |arrival_diff * clock_rate - rtp_diff| = |1 * 90000 - 60000| = 30000
        let later_time = base_time + Duration::from_secs(1);
        stream.process_rtp(later_time, &make_rtp_packet(0x02, 42378934 + 60000));

        let rr = stream.generate_report(later_time);

        assert_eq!(rr.reports.len(), 1);
        assert_eq!(rr.reports[0].ssrc, 123456);
        assert_eq!(rr.reports[0].last_sequence_number, 0x02);
        // jitter = D / 16 = 30000 / 16 = 1875
        assert_eq!(rr.reports[0].jitter, 30000 / 16);
    }

    #[test]
    fn test_receiver_stream_delay() {
        // Port of pion's TestReceiverInterceptor/delay
        let mut stream = ReceiverStream::new(123456, 90000);
        let base_time = Instant::now();

        // Receive a sender report
        let sr = rtcp::sender_report::SenderReport {
            ssrc: 123456,
            ntp_time: 0x1234_5678_0000_0000, // Some NTP time
            rtp_time: 987654321,
            packet_count: 0,
            octet_count: 0,
            ..Default::default()
        };
        stream.process_sender_report(base_time, &sr);

        // Generate receiver report 1 second later
        let later_time = base_time + Duration::from_secs(1);
        let rr = stream.generate_report(later_time);

        assert_eq!(rr.reports.len(), 1);
        // DLSR in 1/65536 seconds units: 1 second = 65536
        assert_eq!(rr.reports[0].delay, 65536);
        // LSR is middle 32 bits of NTP time
        assert_eq!(rr.reports[0].last_sender_report, 0x5678_0000);
    }

    #[test]
    fn test_receiver_stream_delay_before_sender_report() {
        // Test that delay is 0 before receiving any sender report
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Receive some RTP packets
        for i in 0..5u16 {
            stream.process_rtp(now, &make_rtp_packet(i, 0));
        }

        // Generate report without having received any SR
        let rr = stream.generate_report(now);

        assert_eq!(rr.reports[0].delay, 0);
        assert_eq!(rr.reports[0].last_sender_report, 0);
    }

    #[test]
    fn test_receiver_stream_cumulative_loss() {
        // Test that total_lost accumulates across reports
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // First batch: receive 1, skip 2, receive 3
        stream.process_rtp(now, &make_rtp_packet(1, 0));
        stream.process_rtp(now, &make_rtp_packet(3, 0));

        let rr1 = stream.generate_report(now);
        assert_eq!(rr1.reports[0].total_lost, 1);

        // Second batch: receive 4, skip 5, receive 6
        stream.process_rtp(now, &make_rtp_packet(4, 0));
        stream.process_rtp(now, &make_rtp_packet(6, 0));

        let rr2 = stream.generate_report(now);
        // Total lost should now be 2 (1 from first batch + 1 from second)
        assert_eq!(rr2.reports[0].total_lost, 2);
    }

    #[test]
    fn test_receiver_stream_24bit_loss_clamping() {
        // The cumulative loss is clamped to the signed 24 bits it is carried in
        // (RFC 3550 section 6.4.1, Appendix A.3), at both ends.
        let now = Instant::now();

        let mut stream = ReceiverStream::new(123456, 90000);
        stream.process_rtp(now, &make_rtp_packet(1, 0));
        // 2^24 sequence numbers later, having received almost nothing in between.
        stream.cycles = 256 << 16;
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].total_lost, 0x7F_FFFF);
        assert_eq!(rr.reports[0].cumulative_lost(), 0x7F_FFFF);

        let mut stream = ReceiverStream::new(123456, 90000);
        stream.process_rtp(now, &make_rtp_packet(1, 0));
        // Far more duplicates than packets expected.
        stream.received = 0x0100_0000;
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].cumulative_lost(), -0x80_0000);
    }

    /// RFC 3550 6.4.1: "packets that arrive late are not counted as lost".
    #[test]
    fn a_late_packet_is_no_longer_lost_in_the_next_report() {
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        stream.process_rtp(now, &make_rtp_packet(1, 0));
        stream.process_rtp(now, &make_rtp_packet(3, 0));
        let first = stream.generate_report(now);
        assert_eq!(first.reports[0].cumulative_lost(), 1);
        assert_eq!(first.reports[0].fraction_lost, (256u32 / 3) as u8);

        // Packet 2 arrives after the report that counted it lost.
        stream.process_rtp(now, &make_rtp_packet(2, 0));
        let second = stream.generate_report(now);
        assert_eq!(second.reports[0].cumulative_lost(), 0);
        // Nothing new was expected in the interval and one packet arrived: no loss.
        assert_eq!(second.reports[0].fraction_lost, 0);
    }

    /// RFC 3550 6.4.1: "the loss may be negative if there are duplicates".
    #[test]
    fn duplicates_make_the_cumulative_loss_negative() {
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        for seq in [1u16, 2, 3, 3, 3] {
            stream.process_rtp(now, &make_rtp_packet(seq, 0));
        }
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].last_sequence_number, 3);
        assert_eq!(rr.reports[0].cumulative_lost(), -2);
        // Appendix A.3: a negative interval loss reports a fraction of zero.
        assert_eq!(rr.reports[0].fraction_lost, 0);
        // On the wire: the 24-bit two's complement.
        assert_eq!(rr.reports[0].total_lost, 0xFF_FFFE);
    }

    /// Appendix A.3: the fraction is the loss of the interval over what the interval expected,
    /// as an 8-bit fixed point number.
    #[test]
    fn fraction_lost_covers_the_interval_since_the_previous_report() {
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        // Interval one: 1..=10 with nothing lost.
        for seq in 1..=10u16 {
            stream.process_rtp(now, &make_rtp_packet(seq, 0));
        }
        assert_eq!(stream.generate_report(now).reports[0].fraction_lost, 0);

        // Interval two: 11..=20 expected, five of them received.
        for seq in [11u16, 12, 13, 14, 20] {
            stream.process_rtp(now, &make_rtp_packet(seq, 0));
        }
        let rr = stream.generate_report(now);
        // 5 lost of 10 expected: 5 * 256 / 10 = 128.
        assert_eq!(rr.reports[0].fraction_lost, 128);
        assert_eq!(rr.reports[0].cumulative_lost(), 5);
        assert_eq!(rr.reports[0].last_sequence_number, 20);
    }

    /// Appendix A.1: one packet far outside the window is not part of the sequence; two in a
    /// row are a source that restarted, and the statistics start again from there.
    #[test]
    fn a_large_jump_is_ignored_once_and_a_restart_when_it_repeats() {
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        for seq in 100..=109u16 {
            stream.process_rtp(now, &make_rtp_packet(seq, 0));
        }
        // A wild packet: more than MAX_DROPOUT ahead.
        stream.process_rtp(now, &make_rtp_packet(40_000, 0));
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].last_sequence_number, 109);
        assert_eq!(rr.reports[0].cumulative_lost(), 0);

        // The packet after it, in sequence with it: the source restarted.
        stream.process_rtp(now, &make_rtp_packet(40_001, 0));
        stream.process_rtp(now, &make_rtp_packet(40_002, 0));
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].last_sequence_number, 40_002);
        assert_eq!(rr.reports[0].cumulative_lost(), 0);
        assert_eq!(rr.reports[0].fraction_lost, 0);
    }

    /// Appendix A.1: a gap of up to MAX_DROPOUT is loss, not a restart.
    #[test]
    fn a_gap_below_the_dropout_limit_is_counted_as_loss() {
        let mut stream = ReceiverStream::new(123456, 90000);
        let now = Instant::now();

        stream.process_rtp(now, &make_rtp_packet(10, 0));
        stream.process_rtp(now, &make_rtp_packet(10 + 2999, 0));
        let rr = stream.generate_report(now);
        assert_eq!(rr.reports[0].last_sequence_number, 3009);
        assert_eq!(rr.reports[0].cumulative_lost(), 2998);
    }
}