rtci 0.2.1

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
use crate::rtp_transceiver::RTCRtpTransceiverId;
use crate::rtp_transceiver::SSRC;
use crate::rtp_transceiver::rtp_sender::RtpCodecKind;
use crate::statistics::accumulator::{AudioReceiverStatsUpdate, CcfbTally, DecoderStatsUpdate};
use crate::statistics::stats::rtp_stream::RTCRtpStreamStats;
use crate::statistics::stats::rtp_stream::received::RTCReceivedRtpStreamStats;
use crate::statistics::stats::rtp_stream::received::inbound::RTCInboundRtpStreamStats;
use crate::statistics::stats::rtp_stream::sent::RTCSentRtpStreamStats;
use crate::statistics::stats::rtp_stream::sent::remote_outbound::RTCRemoteOutboundRtpStreamStats;
use crate::statistics::stats::{RTCStats, RTCStatsType};
use rtcp::transport_feedbacks::cc_feedback_report::CcFeedbackReportBlock;
use shared::EcnCodepoint;
use shared::time::SystemInstant;
use std::collections::VecDeque;
use std::time::{Duration, Instant};

/// How many of our latest RTCP XR receiver reference times (RFC 3611 section 4.4) a stream
/// remembers, for the DLRR block that echoes one.
const REFERENCE_TIMES_REMEMBERED: usize = 16;

/// The suffix that tells a stream object from an earlier, deleted one that had the same SSRC:
/// `generation` is how many RTP stream objects the connection had deleted when this one was
/// created, so it differs from that of any deleted object with the SSRC (webrtc-stats 4.3: the
/// id of a deleted object is never reused; 8.1: a recycled SSRC is a new object with a new id).
pub(crate) fn generation_suffix(generation: u64) -> String {
    if generation == 0 {
        String::new()
    } else {
        format!("_{generation}")
    }
}

/// The stats id of the inbound-rtp object for `ssrc`, before the report seals it.
pub(crate) fn inbound_stats_id(kind: RtpCodecKind, ssrc: SSRC, generation: u64) -> String {
    format!(
        "RTCInboundRTPStream_{kind}_{ssrc}{}",
        generation_suffix(generation)
    )
}

/// The stats id of the remote-outbound-rtp object for `ssrc`, before the report seals it.
pub(crate) fn remote_outbound_stats_id(kind: RtpCodecKind, ssrc: SSRC, generation: u64) -> String {
    format!(
        "RTCRemoteOutboundRTPStream_{kind}_{ssrc}{}",
        generation_suffix(generation)
    )
}

/// The wall-clock time an RTCP sender report's 64-bit NTP timestamp names, as a duration since
/// the Unix epoch; `None` for a timestamp before 1970 (a sender that does not have wall-clock
/// time puts something else there, RFC 3550 section 6.4.1).
pub(crate) fn ntp_to_unix(ntp_time: u64) -> Option<Duration> {
    /// Seconds between the NTP epoch (1900) and the Unix epoch (1970).
    const NTP_TO_UNIX_SECONDS: u64 = 0x83AA_7E80;
    let seconds = (ntp_time >> 32).checked_sub(NTP_TO_UNIX_SECONDS)?;
    let nanos = ((ntp_time & 0xFFFF_FFFF) * 1_000_000_000) >> 32;
    Some(Duration::new(seconds, nanos as u32))
}

/// An RTCP interarrival jitter (RFC 3550 section 6.4.1, in RTP timestamp units) in seconds, or
/// `None` while the clock rate it is counted in is not known.
pub(crate) fn jitter_seconds(jitter_rtp_units: Option<u32>, clock_rate: u32) -> Option<f64> {
    match (jitter_rtp_units, clock_rate) {
        (Some(units), rate) if rate > 0 => Some(f64::from(units) / f64::from(rate)),
        _ => None,
    }
}

/// Whether the members of one media kind apply to a stream.
pub(crate) struct OnlyFor(pub(crate) bool);

impl OnlyFor {
    /// `value` if the stream is of that kind, `None` (the member does not exist) otherwise.
    pub(crate) fn the<T>(&self, value: Option<T>) -> Option<T> {
        value.filter(|_| self.0)
    }
}

/// Accumulated statistics for an inbound RTP stream.
///
/// This struct tracks packet counters, RTCP feedback and FEC/RTX counters for an incoming RTP
/// stream, plus whatever the application supplied about decoding and playout.
#[derive(Debug, Default)]
pub struct InboundRtpStreamAccumulator {
    // Base identification
    /// The SSRC identifier for this stream.
    pub ssrc: SSRC,
    /// The media kind (audio/video).
    pub kind: RtpCodecKind,
    /// Reference to the transport stats.
    pub transport_id: String,
    /// Reference to the codec stats.
    pub codec_id: String,
    /// The codec's clock rate, which the RTCP jitter is counted in (0 until the codec is known).
    pub clock_rate: u32,
    /// The track identifier from the MediaStreamTrack.
    pub track_identifier: String,
    /// The media stream identification tag from SDP.
    pub mid: String,
    /// The transceiver ID that owns this stream (for filtering).
    pub transceiver_id: RTCRtpTransceiverId,
    /// Tells this object's id from that of a deleted one with the same SSRC
    /// ([`generation_suffix`]).
    pub generation: u64,

    // Packet counters
    /// Total RTP packets received for the SSRC, with those of its RTX and FEC streams
    /// (webrtc-stats 7.1: they are not objects of their own and count here).
    pub packets_received: u64,
    /// Total payload bytes received (excluding headers and padding).
    pub bytes_received: u64,
    /// Total RTP header and padding bytes received; an RTX packet's original sequence number
    /// (RFC 4588 section 4) is header too.
    pub header_bytes_received: u64,
    /// Cumulative packets lost (negative when duplicates outnumber losses), from the latest
    /// reception report this side sent about the stream.
    pub packets_lost: i64,
    /// Interarrival jitter in RTP timestamp units, from the latest reception report this side
    /// sent about the stream.
    pub jitter_rtp_units: Option<u32>,
    /// Timestamp of the last RTP packet received.
    pub last_packet_received_timestamp: Option<Instant>,

    // ECN (RFC 3168) and RFC 8888 congestion control feedback
    /// The application's socket layer reports each datagram's ECN codepoint, so the ECN
    /// counts below are measured (`SettingEngineBuilder::with_ecn`).
    pub ecn: bool,
    /// RTP packets received with ECT(1).
    pub packets_received_with_ect1: u64,
    /// RTP packets received with CE.
    pub packets_received_with_ce: u64,
    /// The stream's codec negotiated RFC 8888 feedback (`a=rtcp-fb:<pt> ack ccfb`).
    pub ccfb_negotiated: bool,
    /// What the RFC 8888 reports this side sent say about the stream.
    pub ccfb_sent: Option<Box<CcfbTally>>,

    // RTCP feedback sent
    /// Number of NACKs sent for this stream.
    pub nack_count: u32,
    /// Number of FIRs sent for this stream.
    pub fir_count: u32,
    /// Number of PLIs sent for this stream.
    pub pli_count: u32,

    // FEC stats
    /// FEC packets received.
    pub fec_packets_received: u64,
    /// FEC bytes received.
    pub fec_bytes_received: u64,
    /// What the chain's ULPFEC decoder last reported for the stream: the FEC that came in its
    /// RED packets, and what it rebuilt and discarded. `None` while no decoder has.
    pub ulpfec: Option<interceptor::UlpfecReceiveStats>,
    /// Media packets of the stream rebuilt by FEC (any decoder) that left the chain.
    pub packets_recovered_by_fec: u64,

    // Retransmission
    /// Retransmitted packets received (RTX).
    pub retransmitted_packets_received: u64,
    /// Retransmitted bytes received.
    pub retransmitted_bytes_received: u64,
    /// RTX SSRC if available.
    pub rtx_ssrc: Option<u32>,
    /// FEC SSRC if available.
    pub fec_ssrc: Option<u32>,
    /// A packet of the stream carried the negotiated corruption-detection header extension
    /// (W3C webrtc-stats 8.5): the corruption members are present from then on.
    pub corruption_detection_received: bool,

    // Remote sender info (from RTCP SR)
    /// Packets sent by the remote sender (from SR).
    pub remote_packets_sent: u64,
    /// Bytes sent by the remote sender (from SR).
    pub remote_bytes_sent: u64,
    /// When the latest remote sender report arrived: the remote-outbound object's `timestamp`.
    pub remote_report_received_at: Option<Instant>,
    /// The NTP timestamp the latest remote sender report carried, the remote clock's reading:
    /// the remote-outbound object's `remoteTimestamp`.
    pub remote_ntp_time: Option<u64>,
    /// The RTP timestamp the latest remote sender report carried, the same instant as
    /// `remote_ntp_time` on the stream's media clock (RFC 3550 section 6.4.1).
    pub remote_rtp_time: Option<u32>,
    /// Number of RTCP SR reports received.
    pub reports_received: u64,
    /// RFC 6051 section 3.3: the latest in-band NTP-format timestamp (`ntp-64` or `ntp-56`)
    /// the stream carried, the RTP timestamp of the packet that carried it, and when that
    /// packet arrived: a mapping of the stream's RTP clock to the sender's NTP clock taken "in
    /// exactly the same way as if an additional RTCP SR packet had been received". It is not
    /// a report: the remote-outbound members stay those of the SRs.
    pub in_band_ntp: Option<(u64, u32, Instant)>,
    /// The clock rates the stream's packets have run at, on one SSRC (RFC 7160 section 3.2):
    /// where the rate changed, and the rate each mapping above was taken at.
    pub media_clock: MediaClock,

    // Round trip time from RTCP XR (RFC 3611 sections 4.4, 4.5)
    /// The SSRC our receiver reports about this stream are sent from, which our XR receiver
    /// reference time blocks carry and the remote sender's DLRR sub-blocks name.
    pub receiver_ssrc: Option<u32>,
    /// Our latest receiver reference times for the stream: the compact NTP timestamp each
    /// carried and when it was sent.
    pub reference_times_sent: VecDeque<(u32, Instant)>,
    /// The latest round trip time measured from a DLRR block, in seconds.
    pub remote_round_trip_time: Option<f64>,
    /// Sum of the round trip times measured from DLRR blocks, in seconds.
    pub remote_total_round_trip_time: f64,
    /// Number of round trip times measured from DLRR blocks.
    pub remote_rtt_measurements: u64,
    /// When the latest of them was measured: a sender report arriving at that instant came
    /// with the block.
    pub remote_rtt_measured_at: Option<Instant>,

    // Application-provided stats
    /// Frame assembly, decoder and renderer statistics provided by the application.
    pub decoder_stats: DecoderStatsUpdate,
    /// Jitter buffer, concealment and audio statistics provided by the application.
    pub audio_receiver_stats: AudioReceiverStatsUpdate,
    /// The id of the audio playout stats object this stream plays out through, once the
    /// application has supplied one.
    pub playout_id: Option<String>,
}

impl InboundRtpStreamAccumulator {
    /// Called when an RTP packet is received: `header_bytes` is its header and padding,
    /// `payload_bytes` its payload.
    pub fn on_rtp_received(&mut self, header_bytes: usize, payload_bytes: usize, now: Instant) {
        self.packets_received += 1;
        self.header_bytes_received += header_bytes as u64;
        self.bytes_received += payload_bytes as u64;
        self.last_packet_received_timestamp = Some(now);
    }

    /// Counts header-side bytes of a packet that [`on_rtp_received`](Self::on_rtp_received)
    /// counts without them: the original sequence number and the padding of an RTX packet,
    /// both gone once it is de-encapsulated.
    pub fn on_header_overhead_received(&mut self, bytes: usize) {
        self.header_bytes_received += bytes as u64;
    }

    /// Called when this side sends a reception report about the stream (in an RTCP RR or SR):
    /// the cumulative loss (already sign-extended, RFC 3550 section 6.4.1) and the interarrival
    /// jitter in RTP timestamp units.
    pub fn on_rtcp_rr_generated(&mut self, packets_lost: i64, jitter_rtp_units: u32) {
        self.packets_lost = packets_lost;
        self.jitter_rtp_units = Some(jitter_rtp_units);
    }

    /// Called when a NACK is sent.
    pub fn on_nack_sent(&mut self) {
        self.nack_count += 1;
    }

    /// Called when a FIR is sent.
    pub fn on_fir_sent(&mut self) {
        self.fir_count += 1;
    }

    /// Called when a PLI is sent.
    pub fn on_pli_sent(&mut self) {
        self.pli_count += 1;
    }

    /// Called when RTCP Sender Report is received from remote: its sender's packet and octet
    /// counts, the NTP timestamp it carries (the remote clock) and when it arrived (ours).
    pub fn on_rtcp_sr_received(
        &mut self,
        packets_sent: u64,
        bytes_sent: u64,
        ntp_time: u64,
        now: Instant,
    ) {
        self.remote_packets_sent = packets_sent;
        self.remote_bytes_sent = bytes_sent;
        self.remote_ntp_time = Some(ntp_time);
        self.remote_report_received_at = Some(now);
        self.reports_received += 1;
        // webrtc-stats: `roundTripTime` does not exist "if the latest SR does not contain the
        // DLRR report block". The block comes after the sender report in the compound packet
        // and sets it again; the total and the count of measurements stay. A block that
        // arrived at this very instant is this report's too, in whichever RTCP packet or
        // datagram of the sender's burst it travelled.
        if self.remote_rtt_measured_at != Some(now) {
            self.remote_round_trip_time = None;
        }
    }

    /// The packet and octet counts of a sender report whose timestamps are not taken (RFC
    /// 7164 section 5.1: it was generated near a leap second).
    pub fn on_rtcp_sr_counts(&mut self, packets_sent: u64, bytes_sent: u64) {
        self.remote_packets_sent = packets_sent;
        self.remote_bytes_sent = bytes_sent;
        self.reports_received += 1;
    }

    /// The RTP timestamp of the sender report `on_rtcp_sr_received` took.
    pub fn on_rtcp_sr_rtp_time(&mut self, rtp_time: u32) {
        self.remote_rtp_time = Some(rtp_time);
        // The sender extrapolated it at the rate of the format it sends now: the rate of the
        // last packet received.
        self.media_clock.report = self.media_clock.anchor();
    }

    /// An original packet of the stream with RTP timestamp `timestamp` arrived, of a format
    /// whose clock rate is `clock_rate`.
    pub fn on_media_clock(&mut self, timestamp: u32, clock_rate: u32) {
        self.media_clock.on_packet(timestamp, clock_rate);
    }

    /// An in-band NTP timestamp (`in_band_ntp`) was just taken from the packet last given to
    /// [`Self::on_media_clock`].
    pub fn on_in_band_ntp_clock(&mut self) {
        self.media_clock.in_band = self.media_clock.anchor();
    }

    /// webrtc-extensions 15 "remote capture timestamp": when a frame of this stream was
    /// captured, on this side's clock, by the latest sender report (RFC 3550 section 6.4.1).
    /// `capture_ntp` is the capture time on the sender's NTP clock in seconds when the frame
    /// said it (abs-capture-time with the sender's offset), else the frame's RTP timestamp is
    /// placed by the report's NTP / RTP pair at `clock_rate`. The sender's clock is brought to
    /// this one by the report's arrival less half the round trip time (webrtc-extensions 8:
    /// `senderReceiverTimeOffset`), 0 while no round trip is known. `None` before a report.
    pub fn remote_capture_instant(
        &self,
        rtp_timestamp: u32,
        clock_rate: u32,
        capture_ntp: Option<f64>,
    ) -> Option<Instant> {
        // The newer of the latest sender report and the latest in-band timestamp (RFC 6051
        // section 3.3), which stands in for a report until one arrives.
        let report = match (self.remote_report_received_at, self.in_band_ntp) {
            (Some(at), Some((_, _, in_band_at))) if in_band_at <= at => {
                Some((at, self.remote_ntp_time?, self.remote_rtp_time))
            }
            (_, Some((ntp, rtp, at))) => Some((at, ntp, Some(rtp))),
            (Some(at), None) => Some((at, self.remote_ntp_time?, self.remote_rtp_time)),
            (None, None) => None,
        };
        let (received_at, report_ntp, report_rtp) = report?;
        let in_band = match (self.remote_report_received_at, self.in_band_ntp) {
            (Some(at), Some((_, _, in_band_at))) => in_band_at > at,
            (None, Some(_)) => true,
            _ => false,
        };
        let anchor = if in_band {
            self.media_clock.in_band
        } else {
            self.media_clock.report
        };
        let report_ntp = report_ntp as f64 / 4_294_967_296.0;
        let capture_ntp = match capture_ntp {
            Some(ntp) => ntp,
            None => {
                if clock_rate == 0 {
                    return None;
                }
                let report_rtp = report_rtp?;
                report_ntp
                    + self
                        .media_clock
                        .seconds_since(anchor, report_rtp, rtp_timestamp, clock_rate)
            }
        };
        let one_way = self.remote_round_trip_time.unwrap_or(0.0) / 2.0;
        let offset = capture_ntp - report_ntp - one_way;
        if offset >= 0.0 {
            received_at.checked_add(Duration::from_secs_f64(offset))
        } else {
            received_at.checked_sub(Duration::from_secs_f64(-offset))
        }
    }

    /// Called when this side sends an RTCP XR receiver reference time block (RFC 3611 section
    /// 4.4) for the stream: remembers when, so the DLRR sub-block that echoes it gives a round
    /// trip time.
    pub fn on_reference_time_sent(&mut self, compact_ntp: u32, now: Instant) {
        if self.reference_times_sent.len() == REFERENCE_TIMES_REMEMBERED {
            self.reference_times_sent.pop_front();
        }
        self.reference_times_sent.push_back((compact_ntp, now));
    }

    /// Called when a DLRR sub-block (RFC 3611 section 4.5) answers one of our receiver
    /// reference times: the round trip time is `A - LRR - DLRR`, where `A - LRR` is the time
    /// since we sent the reference time the LRR field echoes. A sub-block with LRR 0 (no
    /// reference time received yet) or naming one we no longer remember gives none.
    pub fn on_dlrr_received(&mut self, last_rr: u32, dlrr: u32, now: Instant) {
        // webrtc-stats: no round trip time "if the last RR timestamp in the DLRR report block
        // is zero, or if the delay since last RR value in the DLRR report block is zero".
        if last_rr == 0 || dlrr == 0 {
            return;
        }
        let Some(&(_, sent_at)) = self
            .reference_times_sent
            .iter()
            .rev()
            .find(|(lrr, _)| *lrr == last_rr)
        else {
            return;
        };
        let Some(since_sent) = now.checked_duration_since(sent_at) else {
            return;
        };
        let delay = Duration::from_secs_f64(f64::from(dlrr) / 65536.0);
        // DLRR is truncated to 1/65536 s: on a near-zero path it can exceed the elapsed time by
        // a fraction of a tick, which is a round trip of 0.
        let rtt = since_sent.saturating_sub(delay).as_secs_f64();
        self.remote_round_trip_time = Some(rtt);
        self.remote_rtt_measured_at = Some(now);
        self.remote_total_round_trip_time += rtt;
        self.remote_rtt_measurements += 1;
    }

    /// Counts one complete frame received (`framesReceived`).
    pub fn on_frame_received(&mut self) {
        let frames = self.decoder_stats.frames_received.unwrap_or(0);
        self.decoder_stats.frames_received = Some(frames.saturating_add(1));
    }

    /// Counts one frame dropped before decoding (`framesDropped`).
    pub fn on_frame_dropped(&mut self) {
        let frames = self.decoder_stats.frames_dropped.unwrap_or(0);
        self.decoder_stats.frames_dropped = Some(frames.saturating_add(1));
    }

    /// Counts the ECN codepoint an RTP packet of the stream arrived with.
    pub fn on_ecn_received(&mut self, ecn: Option<EcnCodepoint>) {
        match ecn {
            Some(EcnCodepoint::Ect1) => self.packets_received_with_ect1 += 1,
            Some(EcnCodepoint::Ce) => self.packets_received_with_ce += 1,
            _ => {}
        }
    }

    /// Counts what an RFC 8888 report block this side sent says about the stream: the packets
    /// it reported lost (`packetsReportedAsLost`) and, of those, the ones a later report said
    /// arrived (`packetsReportedAsLostButRecovered`).
    pub fn on_ccfb_block_sent(&mut self, block: &CcFeedbackReportBlock) {
        self.ccfb_sent
            .get_or_insert_with(|| Box::new(CcfbTally::new()))
            .on_report_block(block, false);
    }

    /// Whether the RFC 8888 members exist: webrtc-stats has them only where `ccfb` is
    /// negotiated.
    fn reports_ccfb(&self) -> bool {
        self.ccfb_negotiated || self.ccfb_sent.is_some()
    }

    /// Called when a retransmission is received: `bytes` is the payload of the packet it
    /// repeats (an RTX packet's payload without the original sequence number).
    pub fn on_rtx_received(&mut self, bytes: usize) {
        self.retransmitted_packets_received += 1;
        self.retransmitted_bytes_received += bytes as u64;
    }

    /// Called when a packet of the stream's FEC SSRC is received: it counts as a packet of the
    /// stream (`packetsReceived`, `bytesReceived`, `headerBytesReceived`) and as FEC, which
    /// webrtc-stats defines as a subset of those.
    pub fn on_fec_received(&mut self, header_bytes: usize, payload_bytes: usize, now: Instant) {
        self.on_rtp_received(header_bytes, payload_bytes, now);
        self.fec_packets_received += 1;
        self.fec_bytes_received += payload_bytes as u64;
    }

    /// Called when a packet of the stream's RTX SSRC that repeats nothing is received (padding
    /// only, a bandwidth probe: RFC 4588 section 4): a packet of the stream, all of it header
    /// and padding, and not a retransmission.
    pub fn on_rtx_padding_received(&mut self, bytes: usize, now: Instant) {
        self.on_rtp_received(bytes, 0, now);
    }

    /// The stats id of this object, before the report seals it.
    pub fn stats_id(&self) -> String {
        inbound_stats_id(self.kind, self.ssrc, self.generation)
    }

    /// The stats id of the remote-outbound object, before the report seals it.
    fn remote_stats_id(&self) -> String {
        remote_outbound_stats_id(self.kind, self.ssrc, self.generation)
    }

    /// Whether the remote-outbound object exists: webrtc-stats 6.1 creates it when the first
    /// RTCP Sender Report for the SSRC arrives.
    pub fn has_remote(&self) -> bool {
        self.reports_received > 0
    }

    /// Creates a snapshot of the accumulated stats at the given timestamp.
    pub fn snapshot(&self, now: Instant, id: &str) -> RTCInboundRtpStreamStats {
        let decoder = &self.decoder_stats;
        let receiver = &self.audio_receiver_stats;
        // webrtc-stats 4.2: a member that does not apply to the object does not exist. The
        // application may supply any member for any stream; a video-only member is reported
        // for a video stream only, an audio-only one for an audio stream only.
        let video = OnlyFor(self.kind == RtpCodecKind::Video);
        let audio = OnlyFor(self.kind == RtpCodecKind::Audio);
        RTCInboundRtpStreamStats {
            received_rtp_stream_stats: RTCReceivedRtpStreamStats {
                rtp_stream_stats: RTCRtpStreamStats {
                    stats: RTCStats {
                        timestamp: SystemInstant::now(now),
                        typ: RTCStatsType::InboundRTP,
                        id: id.to_string(),
                    },
                    ssrc: self.ssrc,
                    kind: self.kind,
                    transport_id: self.transport_id.clone(),
                    codec_id: self.codec_id.clone(),
                },
                packets_received: self.packets_received,
                packets_received_with_ect1: self.ecn.then_some(self.packets_received_with_ect1),
                packets_received_with_ce: self.ecn.then_some(self.packets_received_with_ce),
                packets_reported_as_lost: self.reports_ccfb().then(|| {
                    self.ccfb_sent
                        .as_ref()
                        .map_or(0, |tally| tally.reported_as_lost)
                }),
                packets_reported_as_lost_but_recovered: self.reports_ccfb().then(|| {
                    self.ccfb_sent
                        .as_ref()
                        .map_or(0, |tally| tally.reported_as_lost_but_recovered)
                }),
                packets_lost: self.packets_lost,
                jitter: jitter_seconds(self.jitter_rtp_units, self.clock_rate),
            },
            track_identifier: self.track_identifier.clone(),
            mid: self.mid.clone(),
            remote_id: self.has_remote().then(|| self.remote_stats_id()),
            frames_decoded: video.the(decoder.frames_decoded),
            key_frames_decoded: video.the(decoder.key_frames_decoded),
            frames_rendered: video.the(decoder.frames_rendered),
            frames_dropped: video.the(decoder.frames_dropped),
            frame_width: video.the(decoder.frame_width),
            frame_height: video.the(decoder.frame_height),
            frames_per_second: video.the(decoder.frames_per_second),
            qp_sum: video.the(decoder.qp_sum),
            total_decode_time: video.the(decoder.total_decode_time),
            total_inter_frame_delay: video.the(decoder.total_inter_frame_delay),
            total_squared_inter_frame_delay: video.the(decoder.total_squared_inter_frame_delay),
            pause_count: video.the(decoder.pause_count),
            total_pauses_duration: video.the(decoder.total_pauses_duration),
            freeze_count: video.the(decoder.freeze_count),
            total_freezes_duration: video.the(decoder.total_freezes_duration),
            last_packet_received_timestamp: self
                .last_packet_received_timestamp
                .map(SystemInstant::now),
            header_bytes_received: self.header_bytes_received,
            packets_discarded: decoder.packets_discarded,
            // FEC in the stream's RED packets is FEC received for this SSRC as well; its
            // octets are a subset of `bytesReceived` (they arrived in the RED packets).
            fec_bytes_received: self.fec_bytes_received
                + self.ulpfec.map_or(0, |ulpfec| ulpfec.fec_bytes_in_red)
                + receiver.in_band_fec_bytes_received.unwrap_or(0),
            fec_packets_received: self.fec_packets_received
                + self.ulpfec.map_or(0, |ulpfec| ulpfec.fec_packets_in_red)
                + receiver.in_band_fec_packets_received.unwrap_or(0),
            // The engine's decoder discards; the application's (in-band FEC) on top. Absent
            // while neither has anything to say.
            fec_packets_discarded: match (self.ulpfec, receiver.fec_packets_discarded) {
                (None, supplied) => supplied,
                (Some(ulpfec), supplied) => {
                    Some(ulpfec.decoder.discarded() + supplied.unwrap_or(0))
                }
            },
            bytes_received: self.bytes_received,
            nack_count: self.nack_count,
            // FIR and PLI ask for a video key frame: the members exist for video only.
            fir_count: video.the(Some(self.fir_count)),
            pli_count: video.the(Some(self.pli_count)),
            total_processing_delay: decoder.total_processing_delay,
            // The sender's clock, as the application read it: not an instant of ours.
            estimated_playout_timestamp: receiver
                .estimated_playout_timestamp
                .map(|sender_clock| SystemInstant::at(now, sender_clock)),
            // The jitter buffer members apply to either kind (webrtc-stats defines them for
            // audio and video alike): a video
            // pipeline supplies them with its decoder update, an audio one with its receiver
            // update; the decoder update's value wins when both did.
            jitter_buffer_delay: decoder.jitter_buffer_delay.or(receiver.jitter_buffer_delay),
            jitter_buffer_target_delay: decoder
                .jitter_buffer_target_delay
                .or(receiver.jitter_buffer_target_delay),
            jitter_buffer_emitted_count: decoder
                .jitter_buffer_emitted_count
                .or(receiver.jitter_buffer_emitted_count),
            jitter_buffer_minimum_delay: decoder
                .jitter_buffer_minimum_delay
                .or(receiver.jitter_buffer_minimum_delay),
            total_samples_received: audio.the(receiver.total_samples_received),
            concealed_samples: audio.the(receiver.concealed_samples),
            silent_concealed_samples: audio.the(receiver.silent_concealed_samples),
            concealment_events: audio.the(receiver.concealment_events),
            inserted_samples_for_deceleration: audio
                .the(receiver.inserted_samples_for_deceleration),
            removed_samples_for_acceleration: audio.the(receiver.removed_samples_for_acceleration),
            audio_level: audio.the(receiver.audio_level),
            total_audio_energy: audio.the(receiver.total_audio_energy),
            total_samples_duration: audio.the(receiver.total_samples_duration),
            frames_received: video.the(decoder.frames_received),
            decoder_implementation: video.the(decoder.decoder_implementation.clone()),
            playout_id: audio.the(self.playout_id.clone()),
            power_efficient_decoder: video.the(decoder.power_efficient_decoder),
            frames_assembled_from_multiple_packets: video
                .the(decoder.frames_assembled_from_multiple_packets),
            total_assembly_time: video.the(decoder.total_assembly_time),
            // webrtc-stats: these do not exist where RTX is not negotiated.
            retransmitted_packets_received: self
                .rtx_ssrc
                .map(|_| self.retransmitted_packets_received),
            retransmitted_bytes_received: self.rtx_ssrc.map(|_| self.retransmitted_bytes_received),
            rtx_ssrc: self.rtx_ssrc,
            fec_ssrc: self.fec_ssrc,
            // Present once the application measured, or once the corruption-detection
            // extension arrived (zero until the application's decoder measures).
            total_corruption_probability: video.the(
                decoder
                    .total_corruption_probability
                    .or(self.corruption_detection_received.then_some(0.0)),
            ),
            total_squared_corruption_probability: video.the(
                decoder
                    .total_squared_corruption_probability
                    .or(self.corruption_detection_received.then_some(0.0)),
            ),
            corruption_measurements: video.the(
                decoder
                    .corruption_measurements
                    .or(self.corruption_detection_received.then_some(0)),
            ),
        }
    }

    /// Creates a snapshot of remote outbound stats from RTCP SR data.
    ///
    /// Only meaningful once [`has_remote`](Self::has_remote) is true; the report leaves the
    /// object out before that.
    ///
    /// Its `timestamp` is when the latest sender report arrived (webrtc 8.4: for statistics
    /// that came from a remote source, the time the information reached the local endpoint),
    /// its `remoteTimestamp` the NTP time that report carries.
    pub fn snapshot_remote(&self, now: Instant) -> RTCRemoteOutboundRtpStreamStats {
        let received_at = self.remote_report_received_at.unwrap_or(now);
        RTCRemoteOutboundRtpStreamStats {
            sent_rtp_stream_stats: RTCSentRtpStreamStats {
                rtp_stream_stats: RTCRtpStreamStats {
                    stats: RTCStats {
                        timestamp: SystemInstant::now(received_at),
                        typ: RTCStatsType::RemoteOutboundRTP,
                        id: self.remote_stats_id(),
                    },
                    ssrc: self.ssrc,
                    kind: self.kind,
                    transport_id: self.transport_id.clone(),
                    codec_id: self.codec_id.clone(),
                },
                packets_sent: self.remote_packets_sent,
                bytes_sent: self.remote_bytes_sent,
            },
            local_id: self.stats_id(),
            remote_timestamp: self
                .remote_ntp_time
                .and_then(ntp_to_unix)
                .map(|unix| SystemInstant::at(received_at, unix)),
            reports_sent: self.reports_received,
            round_trip_time: self.remote_round_trip_time,
            total_round_trip_time: self.remote_total_round_trip_time,
            round_trip_time_measurements: self.remote_rtt_measurements,
        }
    }
}

/// How many clock rate changes of a stream are remembered.
const CLOCK_CHANGES_KEPT: usize = 32;

/// The clock rates one SSRC's packets ran at (RFC 7160 section 3.2: one SSRC, several clock
/// rates), so an RTP timestamp is placed against a mapping taken at another rate.
///
/// RFC 7160 section 4.2: where the rate changes, the new rate's timestamps go on from the
/// timestamp the old rate had reached at that instant ("start_offset += (capture_time -
/// capture_start) * previous_clock_rate"). The first packet of a new rate is therefore placed
/// by the old rate from the mapping before it, and every later one by its own rate from there:
/// a sender report taken while Opus (48 kHz) was sent places the PCMU (8 kHz) packets after
/// a switch, and the Opus ones after the switch back, instead of reading 8 kHz ticks as 48 kHz
/// ones.
#[derive(Debug, Default, Clone)]
pub struct MediaClock {
    /// The clock rate of the last packet.
    rate: Option<u32>,
    /// How many changes there have been, and the latest ones: the change's number, the
    /// timestamp of the first packet at the new rate, the old rate and the new one.
    changes: u64,
    recent: std::collections::VecDeque<(u64, u32, u32, u32)>,
    /// Where the clock stood when the latest sender report and the latest in-band timestamp
    /// were taken: the rate, and how many changes had happened.
    report: Option<(u32, u64)>,
    in_band: Option<(u32, u64)>,
}

impl MediaClock {
    fn on_packet(&mut self, timestamp: u32, clock_rate: u32) {
        if clock_rate == 0 {
            return;
        }
        if let Some(rate) = self.rate
            && rate != clock_rate
        {
            self.changes += 1;
            self.recent
                .push_back((self.changes, timestamp, rate, clock_rate));
            while self.recent.len() > CLOCK_CHANGES_KEPT {
                self.recent.pop_front();
            }
        }
        self.rate = Some(clock_rate);
    }

    fn anchor(&self) -> Option<(u32, u64)> {
        self.rate.map(|rate| (rate, self.changes))
    }

    /// Seconds from the mapping's RTP timestamp `from` (taken with the clock at `anchor`) to
    /// `to`, a timestamp of a packet at `clock_rate`: across the changes since the mapping,
    /// each stretch at its own rate.
    fn seconds_since(
        &self,
        anchor: Option<(u32, u64)>,
        from: u32,
        to: u32,
        clock_rate: u32,
    ) -> f64 {
        let ticks = |a: u32, b: u32| f64::from(b.wrapping_sub(a) as i32);
        let Some((anchor_rate, anchor_changes)) = anchor else {
            return ticks(from, to) / f64::from(clock_rate);
        };
        let after: Vec<&(u64, u32, u32, u32)> = self
            .recent
            .iter()
            .filter(|(number, ..)| *number > anchor_changes)
            .collect();
        // The stretch `to` is in: the last change to its rate it does not precede.
        let stretch = after
            .iter()
            .rposition(|(_, start, _, rate)| *rate == clock_rate && ticks(*start, to) >= 0.0);
        let Some(stretch) = stretch else {
            // In the mapping's own stretch (or one no longer remembered).
            let rate = if anchor_rate == clock_rate || after.is_empty() {
                clock_rate
            } else {
                anchor_rate
            };
            return ticks(from, to) / f64::from(rate);
        };
        if self.changes - anchor_changes > after.len() as u64 {
            // Changes were forgotten since the mapping: the best left is the packet's rate.
            return ticks(from, to) / f64::from(clock_rate);
        }
        let mut seconds = 0.0;
        let (mut at, mut rate) = (from, anchor_rate);
        for (_, start, _, new_rate) in &after[..=stretch] {
            seconds += ticks(at, *start) / f64::from(rate);
            at = *start;
            rate = *new_rate;
        }
        seconds + ticks(at, to) / f64::from(clock_rate)
    }
}

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

    /// RFC 7160 4.2 timestamps on one SSRC: a sender report at RTP 100000 (Opus, 48 kHz), a
    /// change to PCMU (8 kHz) 1 s later (timestamp 148000 by the old rate), a change back to
    /// Opus 2 s after that (148000 + 16000). A PCMU frame 0.5 s into its stretch was captured
    /// 1.5 s after the report, and an Opus frame 0.25 s into the last stretch 3.25 s after it:
    /// each stretch at its own rate.
    #[test]
    fn a_timestamp_is_placed_across_clock_rate_changes_since_the_report() {
        let mut stream = InboundRtpStreamAccumulator::default();
        let at = Instant::now();
        stream.on_media_clock(99_040, 48_000);
        let ntp = 1000u64 << 32;
        stream.on_rtcp_sr_received(1, 1, ntp, at);
        stream.on_rtcp_sr_rtp_time(100_000);
        stream.on_media_clock(148_000, 8_000);
        stream.on_media_clock(152_000, 8_000);
        stream.on_media_clock(164_000, 48_000);
        let seconds =
            |instant: Option<Instant>| instant.expect("placed").duration_since(at).as_secs_f64();
        let pcmu = seconds(stream.remote_capture_instant(152_000, 8_000, None));
        assert!((pcmu - 1.5).abs() < 1e-6, "{pcmu}");
        let opus = seconds(stream.remote_capture_instant(164_000 + 12_000, 48_000, None));
        assert!((opus - 3.25).abs() < 1e-6, "{opus}");
        // Before any change the old reading holds.
        let before = seconds(stream.remote_capture_instant(124_000, 48_000, None));
        assert!((before - 0.5).abs() < 1e-6, "{before}");
    }

    #[test]
    fn a_dlrr_answering_a_reference_time_gives_a_round_trip_time() {
        let mut acc = InboundRtpStreamAccumulator::default();
        let t0 = Instant::now();
        acc.on_dlrr_received(0x1234_0000, 0, t0 + Duration::from_millis(50));
        assert_eq!(acc.remote_rtt_measurements, 0, "nothing was sent yet");

        acc.on_reference_time_sent(0x1234_0000, t0);
        // Echoed 50 ms after it was sent, held 10 ms by the remote: 40 ms on the wire.
        acc.on_dlrr_received(
            0x1234_0000,
            (0.010 * 65536.0) as u32,
            t0 + Duration::from_millis(50),
        );
        assert_eq!(acc.remote_rtt_measurements, 1);
        let rtt = acc.remote_round_trip_time.expect("a round trip time");
        assert!((rtt - 0.040).abs() < 0.001, "{rtt}");
        acc.on_dlrr_received(0, 0, t0 + Duration::from_millis(60));
        acc.on_dlrr_received(0x9999_0000, 0, t0 + Duration::from_millis(60));
        assert_eq!(
            acc.remote_rtt_measurements, 1,
            "LRR 0 or unknown gives none"
        );

        let remote = acc.snapshot_remote(t0);
        assert_eq!(remote.round_trip_time, Some(rtt));
        assert_eq!(remote.round_trip_time_measurements, 1);
        assert!((remote.total_round_trip_time - rtt).abs() < 1e-9);
    }

    #[test]
    fn test_default() {
        let acc = InboundRtpStreamAccumulator::default();
        assert_eq!(acc.ssrc, 0);
        assert_eq!(acc.packets_received, 0);
        assert_eq!(acc.bytes_received, 0);
        assert_eq!(acc.header_bytes_received, 0);
        assert_eq!(acc.packets_lost, 0);
        assert_eq!(acc.jitter_rtp_units, None);
        assert_eq!(acc.nack_count, 0);
        assert_eq!(acc.fir_count, 0);
        assert_eq!(acc.pli_count, 0);
        assert_eq!(acc.decoder_stats.frames_received, None);
        assert_eq!(acc.decoder_stats.frames_dropped, None);
        assert_eq!(acc.fec_packets_received, 0);
        assert_eq!(acc.retransmitted_packets_received, 0);
    }

    #[test]
    fn test_on_rtp_received() {
        let mut acc = InboundRtpStreamAccumulator::default();
        let now = Instant::now();

        acc.on_rtp_received(12, 1188, now);
        assert_eq!(acc.packets_received, 1);
        assert_eq!(acc.header_bytes_received, 12);
        assert_eq!(acc.bytes_received, 1188);
        assert_eq!(acc.last_packet_received_timestamp, Some(now));

        let later = now + std::time::Duration::from_millis(20);
        acc.on_rtp_received(12, 1000, later);
        assert_eq!(acc.packets_received, 2);
        assert_eq!(acc.header_bytes_received, 24);
        assert_eq!(acc.bytes_received, 2188);
        assert_eq!(acc.last_packet_received_timestamp, Some(later));
    }

    #[test]
    fn test_on_rtcp_rr_generated() {
        let mut acc = InboundRtpStreamAccumulator::default();

        acc.on_rtcp_rr_generated(10, 450);
        assert_eq!(acc.packets_lost, 10);
        assert_eq!(acc.jitter_rtp_units, Some(450));

        // Update with new values
        acc.on_rtcp_rr_generated(-2, 720);
        assert_eq!(acc.packets_lost, -2);
        assert_eq!(acc.jitter_rtp_units, Some(720));
    }

    #[test]
    fn test_rtcp_feedback_counters() {
        let mut acc = InboundRtpStreamAccumulator::default();

        acc.on_nack_sent();
        acc.on_nack_sent();
        assert_eq!(acc.nack_count, 2);

        acc.on_fir_sent();
        assert_eq!(acc.fir_count, 1);

        acc.on_pli_sent();
        acc.on_pli_sent();
        acc.on_pli_sent();
        assert_eq!(acc.pli_count, 3);
    }

    #[test]
    fn test_on_rtcp_sr_received() {
        let mut acc = InboundRtpStreamAccumulator::default();
        let now = Instant::now();
        assert!(!acc.has_remote());

        acc.on_rtcp_sr_received(1000, 1_000_000, 0, now);
        assert_eq!(acc.remote_packets_sent, 1000);
        assert_eq!(acc.remote_bytes_sent, 1_000_000);
        assert_eq!(acc.remote_report_received_at, Some(now));
        assert_eq!(acc.reports_received, 1);
        assert!(acc.has_remote());

        let later = now + std::time::Duration::from_secs(5);
        acc.on_rtcp_sr_received(2000, 2_000_000, 0, later);
        assert_eq!(acc.remote_packets_sent, 2000);
        assert_eq!(acc.remote_bytes_sent, 2_000_000);
        assert_eq!(acc.remote_report_received_at, Some(later));
        assert_eq!(acc.reports_received, 2);
    }

    #[test]
    fn test_frame_tracking() {
        let mut acc = InboundRtpStreamAccumulator::default();

        acc.on_frame_received();
        acc.on_frame_received();
        acc.on_frame_received();
        assert_eq!(acc.decoder_stats.frames_received, Some(3));

        acc.on_frame_dropped();
        assert_eq!(acc.decoder_stats.frames_dropped, Some(1));
    }

    #[test]
    fn test_rtx_and_fec_received() {
        let mut acc = InboundRtpStreamAccumulator::default();

        acc.on_rtx_received(500);
        acc.on_rtx_received(600);
        assert_eq!(acc.retransmitted_packets_received, 2);
        assert_eq!(acc.retransmitted_bytes_received, 1100);

        let now = Instant::now();
        acc.on_fec_received(12, 200, now);
        acc.on_fec_received(12, 300, now);
        acc.on_fec_received(12, 250, now);
        assert_eq!(acc.fec_packets_received, 3);
        assert_eq!(acc.fec_bytes_received, 750);
        // FEC packets are packets of the stream: `fecBytesReceived` is a subset of
        // `bytesReceived`.
        assert_eq!(acc.packets_received, 3);
        assert_eq!(acc.bytes_received, 750);
        assert_eq!(acc.header_bytes_received, 36);
    }

    #[test]
    fn test_snapshot() {
        let now = Instant::now();
        let mut acc = InboundRtpStreamAccumulator {
            ssrc: 11111111,
            kind: RtpCodecKind::Audio,
            transport_id: "RTCTransport_0".to_string(),
            codec_id: "RTCCodec_audio_111".to_string(),
            clock_rate: 48000,
            track_identifier: "audio-track".to_string(),
            mid: "1".to_string(),
            ..Default::default()
        };

        acc.on_rtp_received(12, 160, now);
        acc.on_rtp_received(12, 160, now);
        acc.on_rtcp_rr_generated(0, 48);

        let stats = acc.snapshot(now, "RTCInboundRTPStream_audio_11111111");

        assert_eq!(
            stats.received_rtp_stream_stats.rtp_stream_stats.stats.id,
            "RTCInboundRTPStream_audio_11111111"
        );
        assert_eq!(
            stats.received_rtp_stream_stats.rtp_stream_stats.stats.typ,
            RTCStatsType::InboundRTP
        );
        assert_eq!(
            stats.received_rtp_stream_stats.rtp_stream_stats.ssrc,
            11111111
        );
        assert_eq!(stats.received_rtp_stream_stats.packets_received, 2);
        assert_eq!(stats.bytes_received, 320);
        assert_eq!(stats.header_bytes_received, 24);
        // 48 RTP units at 48 kHz is 1 ms.
        assert_eq!(stats.received_rtp_stream_stats.jitter, Some(0.001));
        assert_eq!(stats.track_identifier, "audio-track");
        assert_eq!(stats.mid, "1");
        // No sender report yet: no remote-outbound object to name.
        assert_eq!(stats.remote_id, None);
    }

    #[test]
    fn test_snapshot_remote() {
        let now = Instant::now();
        let mut acc = InboundRtpStreamAccumulator {
            ssrc: 22222222,
            kind: RtpCodecKind::Video,
            transport_id: "RTCTransport_0".to_string(),
            codec_id: "RTCCodec_video_96".to_string(),
            ..Default::default()
        };

        acc.on_rtcp_sr_received(500, 600000, 0, now);
        acc.on_rtcp_sr_received(1000, 1200000, 0, now);

        let remote_stats = acc.snapshot_remote(now);

        assert_eq!(
            remote_stats
                .sent_rtp_stream_stats
                .rtp_stream_stats
                .stats
                .typ,
            RTCStatsType::RemoteOutboundRTP
        );
        assert_eq!(remote_stats.sent_rtp_stream_stats.packets_sent, 1000);
        assert_eq!(remote_stats.sent_rtp_stream_stats.bytes_sent, 1200000);
        assert_eq!(remote_stats.reports_sent, 2);
        assert_eq!(remote_stats.round_trip_time, None);
        assert_eq!(remote_stats.local_id, "RTCInboundRTPStream_video_22222222");
        assert_eq!(
            acc.snapshot(now, "x").remote_id.as_deref(),
            Some("RTCRemoteOutboundRTPStream_video_22222222")
        );
    }

    #[test]
    fn test_snapshot_json_serialization() {
        let now = Instant::now();
        let mut acc = InboundRtpStreamAccumulator {
            ssrc: 33333333,
            kind: RtpCodecKind::Video,
            ..Default::default()
        };

        acc.on_rtp_received(12, 1200, now);
        acc.on_frame_received();
        acc.on_nack_sent();

        let stats = acc.snapshot(now, "RTCInboundRTPStream_video_33333333");

        let json = serde_json::to_string(&stats).expect("should serialize");
        assert!(json.contains("\"ssrc\":33333333"));
        assert!(json.contains("\"packetsReceived\":1"));
        assert!(json.contains("\"bytesReceived\":1200"));
        assert!(json.contains("\"framesReceived\":1"));
        assert!(json.contains("\"nackCount\":1"));
        assert!(json.contains("\"type\":\"inbound-rtp\""));
        // What needs a decoder was never supplied, so it is absent rather than 0.
        assert!(!json.contains("framesDecoded"));
        assert!(!json.contains("jitterBufferDelay"));
        assert!(!json.contains("remoteId"));
    }
}