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
use std::collections::HashMap;
use std::sync::Arc;
use std::time::SystemTime;

use tokio::time::Duration;

mod interceptor;

pub use self::interceptor::StatsInterceptor;

pub fn make_stats_interceptor(id: &str) -> Arc<StatsInterceptor> {
    Arc::new(StatsInterceptor::new(id.to_owned()))
}

/// Types related to inbound RTP streams.
mod inbound {
    use std::time::SystemTime;

    use tokio::time::{Duration, Instant};

    use super::{RTCPStats, RTPStats};

    #[derive(Debug, Clone)]
    /// Stats collected for an inbound RTP stream.
    /// Contains both stats relating to the inbound stream and remote stats for the corresponding
    /// outbound stream at the remote end.
    pub(super) struct StreamStats {
        /// Received RTP stats.
        pub(super) rtp_stats: RTPStats,
        /// Common RTCP stats derived from inbound and outbound RTCP packets.
        pub(super) rtcp_stats: RTCPStats,

        /// The last time any stats where update, used for garbage collection to remove obsolete stats.
        last_update: Instant,

        /// The number of packets sent as reported in the latest SR from the remote.
        remote_packets_sent: u32,

        /// The number of bytes sent as reported in the latest SR from the remote.
        remote_bytes_sent: u32,

        /// The total number of sender reports sent by the remote and received.
        remote_reports_sent: u64,

        /// The last remote round trip time measurement in ms. [`None`] if no round trip time has
        /// been derived yet, or if it wasn't possible to derive it.
        remote_round_trip_time: Option<f64>,

        /// The cumulative total round trip times reported in ms.
        remote_total_round_trip_time: f64,

        /// The total number of measurements of the remote round trip time.
        remote_round_trip_time_measurements: u64,
    }

    impl Default for StreamStats {
        fn default() -> Self {
            Self {
                rtp_stats: RTPStats::default(),
                rtcp_stats: RTCPStats::default(),
                last_update: Instant::now(),
                remote_packets_sent: 0,
                remote_bytes_sent: 0,
                remote_reports_sent: 0,
                remote_round_trip_time: None,
                remote_total_round_trip_time: 0.0,
                remote_round_trip_time_measurements: 0,
            }
        }
    }

    impl StreamStats {
        pub(super) fn snapshot(&self) -> StatsSnapshot {
            self.into()
        }

        pub(super) fn mark_updated(&mut self) {
            self.last_update = Instant::now();
        }

        pub(super) fn duration_since_last_update(&self) -> Duration {
            self.last_update.elapsed()
        }

        pub(super) fn record_sender_report(&mut self, packets_sent: u32, bytes_sent: u32) {
            self.remote_reports_sent += 1;
            self.remote_packets_sent = packets_sent;
            self.remote_bytes_sent = bytes_sent;
        }

        pub(super) fn record_remote_round_trip_time(&mut self, round_trip_time: Option<f64>) {
            // Store the latest measurement, even if it's None.
            self.remote_round_trip_time = round_trip_time;

            if let Some(rtt) = round_trip_time {
                // Only if we have a valid measurement do we update the totals
                self.remote_total_round_trip_time += rtt;
                self.remote_round_trip_time_measurements += 1;
            }
        }
    }

    /// A point in time snapshot of the stream stats for an inbound RTP stream.
    ///
    /// Created by [`StreamStats::snapshot`].
    #[derive(Debug)]
    pub struct StatsSnapshot {
        /// Received RTP stats.
        rtp_stats: RTPStats,
        /// Common RTCP stats derived from inbound and outbound RTCP packets.
        rtcp_stats: RTCPStats,

        /// The number of packets sent as reported in the latest SR from the remote.
        remote_packets_sent: u32,

        /// The number of bytes sent as reported in the latest SR from the remote.
        remote_bytes_sent: u32,

        /// The total number of sender reports sent by the remote and received.
        remote_reports_sent: u64,

        /// The last remote round trip time measurement in ms. [`None`] if no round trip time has
        /// been derived yet, or if it wasn't possible to derive it.
        remote_round_trip_time: Option<f64>,

        /// The cumulative total round trip times reported in ms.
        remote_total_round_trip_time: f64,

        /// The total number of measurements of the remote round trip time.
        remote_round_trip_time_measurements: u64,
    }

    impl StatsSnapshot {
        pub fn packets_received(&self) -> u64 {
            self.rtp_stats.packets
        }

        pub fn payload_bytes_received(&self) -> u64 {
            self.rtp_stats.payload_bytes
        }

        pub fn header_bytes_received(&self) -> u64 {
            self.rtp_stats.header_bytes
        }

        pub fn last_packet_received_timestamp(&self) -> Option<SystemTime> {
            self.rtp_stats.last_packet_timestamp
        }

        pub fn nacks_sent(&self) -> u64 {
            self.rtcp_stats.nack_count
        }

        pub fn firs_sent(&self) -> u64 {
            self.rtcp_stats.fir_count
        }

        pub fn plis_sent(&self) -> u64 {
            self.rtcp_stats.pli_count
        }
        pub fn remote_packets_sent(&self) -> u32 {
            self.remote_packets_sent
        }

        pub fn remote_bytes_sent(&self) -> u32 {
            self.remote_bytes_sent
        }

        pub fn remote_reports_sent(&self) -> u64 {
            self.remote_reports_sent
        }

        pub fn remote_round_trip_time(&self) -> Option<f64> {
            self.remote_round_trip_time
        }

        pub fn remote_total_round_trip_time(&self) -> f64 {
            self.remote_total_round_trip_time
        }

        pub fn remote_round_trip_time_measurements(&self) -> u64 {
            self.remote_round_trip_time_measurements
        }
    }

    impl From<&StreamStats> for StatsSnapshot {
        fn from(stream_stats: &StreamStats) -> Self {
            Self {
                rtp_stats: stream_stats.rtp_stats.clone(),
                rtcp_stats: stream_stats.rtcp_stats.clone(),
                remote_packets_sent: stream_stats.remote_packets_sent,
                remote_bytes_sent: stream_stats.remote_bytes_sent,
                remote_reports_sent: stream_stats.remote_reports_sent,
                remote_round_trip_time: stream_stats.remote_round_trip_time,
                remote_total_round_trip_time: stream_stats.remote_total_round_trip_time,
                remote_round_trip_time_measurements: stream_stats
                    .remote_round_trip_time_measurements,
            }
        }
    }
}

/// Types related to outbound RTP streams.
mod outbound {
    use std::time::SystemTime;

    use tokio::time::{Duration, Instant};

    use super::{RTCPStats, RTPStats};

    #[derive(Debug, Clone)]
    /// Stats collected for an outbound RTP stream.
    /// Contains both stats relating to the outbound stream and remote stats for the corresponding
    /// inbound stream.
    pub(super) struct StreamStats {
        /// Sent RTP stats.
        pub(super) rtp_stats: RTPStats,
        /// Common RTCP stats derived from inbound and outbound RTCP packets.
        pub(super) rtcp_stats: RTCPStats,

        /// The last time any stats where update, used for garbage collection to remove obsolete stats.
        last_update: Instant,

        /// The first value of extended seq num that was sent in an SR for this SSRC. [`None`] before
        /// the first SR is sent.
        ///
        /// Used to calculate packet statistic for remote stats.
        initial_outbound_ext_seq_num: Option<u32>,

        /// The number of inbound packets received by the remote side for this stream.
        remote_packets_received: u64,

        /// The number of lost packets reported by the remote for this tream.
        remote_total_lost: u32,

        /// The estimated remote jitter for this stream in timestamp units.
        remote_jitter: u32,

        /// The last remote round trip time measurement in ms. [`None`] if no round trip time has
        /// been derived yet, or if it wasn't possible to derive it.
        remote_round_trip_time: Option<f64>,

        /// The cumulative total round trip times reported in ms.
        remote_total_round_trip_time: f64,

        /// The total number of measurements of the remote round trip time.
        remote_round_trip_time_measurements: u64,

        /// The latest fraction lost value from RR.
        remote_fraction_lost: Option<u8>,
    }

    impl Default for StreamStats {
        fn default() -> Self {
            Self {
                rtp_stats: RTPStats::default(),
                rtcp_stats: RTCPStats::default(),
                last_update: Instant::now(),
                initial_outbound_ext_seq_num: None,
                remote_packets_received: 0,
                remote_total_lost: 0,
                remote_jitter: 0,
                remote_round_trip_time: None,
                remote_total_round_trip_time: 0.0,
                remote_round_trip_time_measurements: 0,
                remote_fraction_lost: None,
            }
        }
    }

    impl StreamStats {
        pub(super) fn snapshot(&self) -> StatsSnapshot {
            self.into()
        }

        pub(super) fn mark_updated(&mut self) {
            self.last_update = Instant::now();
        }

        pub(super) fn duration_since_last_update(&self) -> Duration {
            self.last_update.elapsed()
        }

        pub(super) fn update_remote_inbound_packets_received(
            &mut self,
            rr_ext_seq_num: u32,
            rr_total_lost: u32,
        ) {
            if let Some(initial_ext_seq_num) = self.initial_outbound_ext_seq_num {
                // Total number of RTP packets received for this SSRC.
                // At the receiving endpoint, this is calculated as defined in [RFC3550] section 6.4.1.
                // At the sending endpoint the packetsReceived is estimated by subtracting the
                // Cumulative Number of Packets Lost from the Extended Highest Sequence Number Received,
                // both reported in the RTCP Receiver Report, and then subtracting the
                // initial Extended Sequence Number that was sent to this SSRC in a RTCP Sender Report and then adding one,
                // to mirror what is discussed in Appendix A.3 in [RFC3550], but for the sender side.
                // If no RTCP Receiver Report has been received yet, then return 0.
                self.remote_packets_received =
                    (rr_ext_seq_num as u64) - (rr_total_lost as u64) - (initial_ext_seq_num as u64)
                        + 1;
            }
        }

        #[inline(always)]
        pub(super) fn record_sr_ext_seq_num(&mut self, seq_num: u32) {
            // Only record the initial value
            if self.initial_outbound_ext_seq_num.is_none() {
                self.initial_outbound_ext_seq_num = Some(seq_num);
            }
        }

        pub(super) fn record_remote_round_trip_time(&mut self, round_trip_time: Option<f64>) {
            // Store the latest measurement, even if it's None.
            self.remote_round_trip_time = round_trip_time;

            if let Some(rtt) = round_trip_time {
                // Only if we have a valid measurement do we update the totals
                self.remote_total_round_trip_time += rtt;
                self.remote_round_trip_time_measurements += 1;
            }
        }

        pub(super) fn update_remote_fraction_lost(&mut self, fraction_lost: u8) {
            self.remote_fraction_lost = Some(fraction_lost);
        }

        pub(super) fn update_remote_jitter(&mut self, jitter: u32) {
            self.remote_jitter = jitter;
        }

        pub(super) fn update_remote_total_lost(&mut self, lost: u32) {
            self.remote_total_lost = lost;
        }
    }

    /// A point in time snapshot of the stream stats for an outbound RTP stream.
    ///
    /// Created by [`StreamStats::snapshot`].
    #[derive(Debug)]
    pub struct StatsSnapshot {
        /// Sent RTP stats.
        rtp_stats: RTPStats,
        /// Common RTCP stats derived from inbound and outbound RTCP packets.
        rtcp_stats: RTCPStats,

        /// The number of inbound packets received by the remote side for this stream.
        remote_packets_received: u64,

        /// The number of lost packets reported by the remote for this tream.
        remote_total_lost: u32,

        /// The estimated remote jitter for this stream in timestamp units.
        remote_jitter: u32,

        /// The most recent remote round trip time in milliseconds.
        remote_round_trip_time: Option<f64>,

        /// The cumulative total round trip times reported in ms.
        remote_total_round_trip_time: f64,

        /// The total number of measurements of the remote round trip time.
        remote_round_trip_time_measurements: u64,

        /// The fraction of packets lost reported for this stream.
        /// Calculated as defined in [RFC3550](https://www.rfc-editor.org/rfc/rfc3550) section 6.4.1 and Appendix A.3.
        remote_fraction_lost: Option<f64>,
    }

    impl StatsSnapshot {
        pub fn packets_sent(&self) -> u64 {
            self.rtp_stats.packets
        }

        pub fn payload_bytes_sent(&self) -> u64 {
            self.rtp_stats.payload_bytes
        }

        pub fn header_bytes_sent(&self) -> u64 {
            self.rtp_stats.header_bytes
        }

        pub fn last_packet_sent_timestamp(&self) -> Option<SystemTime> {
            self.rtp_stats.last_packet_timestamp
        }

        pub fn nacks_received(&self) -> u64 {
            self.rtcp_stats.nack_count
        }

        pub fn firs_received(&self) -> u64 {
            self.rtcp_stats.fir_count
        }

        pub fn plis_received(&self) -> u64 {
            self.rtcp_stats.pli_count
        }

        /// Packets received on the remote side.
        pub fn remote_packets_received(&self) -> u64 {
            self.remote_packets_received
        }

        /// The number of lost packets reported by the remote for this tream.
        pub fn remote_total_lost(&self) -> u32 {
            self.remote_total_lost
        }

        /// The estimated remote jitter for this stream in timestamp units.
        pub fn remote_jitter(&self) -> u32 {
            self.remote_jitter
        }

        /// The latest RTT in ms if enough data is available to measure it.
        pub fn remote_round_trip_time(&self) -> Option<f64> {
            self.remote_round_trip_time
        }

        /// Total RTT in ms.
        pub fn remote_total_round_trip_time(&self) -> f64 {
            self.remote_total_round_trip_time
        }

        /// The number of RTT measurements so far.
        pub fn remote_round_trip_time_measurements(&self) -> u64 {
            self.remote_round_trip_time_measurements
        }

        /// The latest fraction lost value from the remote or None if it hasn't been reported yet.
        pub fn remote_fraction_lost(&self) -> Option<f64> {
            self.remote_fraction_lost
        }
    }

    impl From<&StreamStats> for StatsSnapshot {
        fn from(stream_stats: &StreamStats) -> Self {
            Self {
                rtp_stats: stream_stats.rtp_stats.clone(),
                rtcp_stats: stream_stats.rtcp_stats.clone(),
                remote_packets_received: stream_stats.remote_packets_received,
                remote_total_lost: stream_stats.remote_total_lost,
                remote_jitter: stream_stats.remote_jitter,
                remote_round_trip_time: stream_stats.remote_round_trip_time,
                remote_total_round_trip_time: stream_stats.remote_total_round_trip_time,
                remote_round_trip_time_measurements: stream_stats
                    .remote_round_trip_time_measurements,
                remote_fraction_lost: stream_stats
                    .remote_fraction_lost
                    .map(|fraction| (fraction as f64) / (u8::MAX as f64)),
            }
        }
    }
}

#[derive(Default, Debug)]
struct StatsContainer {
    inbound_stats: HashMap<u32, inbound::StreamStats>,
    outbound_stats: HashMap<u32, outbound::StreamStats>,
}

impl StatsContainer {
    fn get_or_create_inbound_stream_stats(&mut self, ssrc: u32) -> &mut inbound::StreamStats {
        self.inbound_stats.entry(ssrc).or_default()
    }

    fn get_or_create_outbound_stream_stats(&mut self, ssrc: u32) -> &mut outbound::StreamStats {
        self.outbound_stats.entry(ssrc).or_default()
    }

    fn get_inbound_stats(&self, ssrc: u32) -> Option<&inbound::StreamStats> {
        self.inbound_stats.get(&ssrc)
    }

    fn get_outbound_stats(&self, ssrc: u32) -> Option<&outbound::StreamStats> {
        self.outbound_stats.get(&ssrc)
    }

    fn remove_stale_entries(&mut self) {
        const MAX_AGE: Duration = Duration::from_secs(60);

        self.inbound_stats
            .retain(|_, s| s.duration_since_last_update() < MAX_AGE);
        self.outbound_stats
            .retain(|_, s| s.duration_since_last_update() < MAX_AGE);
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
/// Records stats about a given RTP stream.
pub struct RTPStats {
    /// Packets sent or received
    packets: u64,

    /// Payload bytes sent or received
    payload_bytes: u64,

    /// Header bytes sent or received
    header_bytes: u64,

    /// A wall clock timestamp for when the last packet was sent or received encoded as milliseconds since
    /// [`SystemTime::UNIX_EPOCH`].
    last_packet_timestamp: Option<SystemTime>,
}

impl RTPStats {
    fn update(&mut self, header_bytes: u64, payload_bytes: u64, packets: u64, now: SystemTime) {
        self.header_bytes += header_bytes;
        self.payload_bytes += payload_bytes;
        self.packets += packets;
        self.last_packet_timestamp = Some(now);
    }

    pub fn header_bytes(&self) -> u64 {
        self.header_bytes
    }

    pub fn payload_bytes(&self) -> u64 {
        self.payload_bytes
    }

    pub fn packets(&self) -> u64 {
        self.packets
    }

    pub fn last_packet_timestamp(&self) -> Option<SystemTime> {
        self.last_packet_timestamp
    }
}

#[derive(Debug, Default, Clone)]
pub struct RTCPStats {
    /// The number of FIRs sent or received
    fir_count: u64,

    /// The number of PLIs sent or received
    pli_count: u64,

    /// The number of NACKs sent or received
    nack_count: u64,
}

impl RTCPStats {
    #[allow(clippy::too_many_arguments)]
    fn update(&mut self, fir_count: Option<u64>, pli_count: Option<u64>, nack_count: Option<u64>) {
        if let Some(fir_count) = fir_count {
            self.fir_count += fir_count;
        }

        if let Some(pli_count) = pli_count {
            self.pli_count += pli_count;
        }

        if let Some(nack_count) = nack_count {
            self.nack_count += nack_count;
        }
    }

    pub fn fir_count(&self) -> u64 {
        self.fir_count
    }

    pub fn pli_count(&self) -> u64 {
        self.pli_count
    }

    pub fn nack_count(&self) -> u64 {
        self.nack_count
    }
}

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

    #[test]
    fn test_rtp_stats() {
        let mut stats: RTPStats = Default::default();
        assert_eq!(
            (stats.header_bytes(), stats.payload_bytes(), stats.packets()),
            (0, 0, 0),
        );

        stats.update(24, 960, 1, SystemTime::now());

        assert_eq!(
            (stats.header_bytes(), stats.payload_bytes(), stats.packets()),
            (24, 960, 1),
        );
    }

    #[test]
    fn test_rtcp_stats() {
        let mut stats: RTCPStats = Default::default();
        assert_eq!(
            (stats.fir_count(), stats.pli_count(), stats.nack_count()),
            (0, 0, 0),
        );

        stats.update(Some(1), Some(2), Some(3));

        assert_eq!(
            (stats.fir_count(), stats.pli_count(), stats.nack_count()),
            (1, 2, 3),
        );
    }

    #[test]
    fn test_rtp_stats_send_sync() {
        fn test_send_sync<T: Send + Sync>() {}
        test_send_sync::<RTPStats>();
    }

    #[test]
    fn test_rtcp_stats_send_sync() {
        fn test_send_sync<T: Send + Sync>() {}
        test_send_sync::<RTCPStats>();
    }
}