rtc 0.9.0

Sans-I/O WebRTC implementation in Rust
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
use crate::rtp_transceiver::RTCRtpTransceiverId;
use crate::rtp_transceiver::SSRC;
use crate::rtp_transceiver::rtp_sender::RtpCodecKind;
use crate::statistics::accumulator::EncoderStatsUpdate;
use crate::statistics::stats::rtp_stream::RTCRtpStreamStats;
use crate::statistics::stats::rtp_stream::received::RTCReceivedRtpStreamStats;
use crate::statistics::stats::rtp_stream::received::remote_inbound::RTCRemoteInboundRtpStreamStats;
use crate::statistics::stats::rtp_stream::sent::RTCSentRtpStreamStats;
use crate::statistics::stats::rtp_stream::sent::outbound::RTCOutboundRtpStreamStats;
use crate::statistics::stats::{RTCQualityLimitationReason, RTCStats, RTCStatsType};
use std::collections::HashMap;
use std::time::Instant;

/// Accumulated statistics for an outbound RTP stream.
///
/// This struct tracks packet counters, RTCP feedback received,
/// and video frame metrics for an outgoing RTP stream.
#[derive(Debug, Default)]
pub struct OutboundRtpStreamAccumulator {
    // 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 media stream identification tag from SDP.
    pub mid: String,
    /// The RTP stream ID (RID) for simulcast.
    pub rid: String,
    /// The encoding index in simulcast.
    pub encoding_index: u32,
    /// Reference to the media source stats.
    pub media_source_id: String,
    /// The transceiver ID that owns this stream (for filtering).
    pub transceiver_id: RTCRtpTransceiverId,

    // Packet counters
    /// Total RTP packets sent.
    pub packets_sent: u64,
    /// Total payload bytes sent.
    pub bytes_sent: u64,
    /// Total RTP header bytes sent.
    pub header_bytes_sent: u64,
    /// Timestamp of the last RTP packet sent.
    pub last_packet_sent_timestamp: Option<Instant>,

    // Retransmission
    /// Retransmitted packets sent (RTX).
    pub retransmitted_packets_sent: u64,
    /// Retransmitted bytes sent.
    pub retransmitted_bytes_sent: u64,
    /// RTX SSRC if available.
    pub rtx_ssrc: Option<u32>,

    // Frame tracking (RTP-level)
    /// Frames sent.
    pub frames_sent: u32,
    /// Huge frames sent (larger than average).
    pub huge_frames_sent: u32,
    /// Current frame rate.
    pub frames_per_second: f64,

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

    // Timing
    /// Total packet send delay in seconds.
    pub total_packet_send_delay: f64,

    // State
    /// Whether the stream is actively sending.
    pub active: bool,

    // Quality limitation (from BWE/interceptor)
    /// Current quality limitation reason.
    pub quality_limitation_reason: RTCQualityLimitationReason,
    /// Number of resolution changes due to quality limitations.
    pub quality_limitation_resolution_changes: u32,
    /// Target bitrate from bandwidth estimation.
    pub target_bitrate: f64,

    // Remote receiver info (from RTCP RR)
    /// Packets received by the remote receiver (from RR).
    pub remote_packets_received: u64,
    /// Packets lost at the remote receiver (from RR).
    pub remote_packets_lost: u64,
    /// Jitter at the remote receiver (from RR).
    pub remote_jitter: f64,
    /// Fraction lost at the remote receiver (from RR).
    pub remote_fraction_lost: f64,
    /// Round trip time calculated from RR.
    pub remote_round_trip_time: f64,
    /// Number of RTT measurements.
    pub rtt_measurements: u64,

    // Application-provided stats (encoder)
    /// Encoder statistics provided by the application.
    pub encoder_stats: Option<EncoderStatsUpdate>,
}

impl OutboundRtpStreamAccumulator {
    /// Called when an RTP packet is sent.
    pub fn on_rtp_sent(&mut self, header_bytes: usize, payload_bytes: usize, now: Instant) {
        self.packets_sent += 1;
        self.header_bytes_sent += header_bytes as u64;
        self.bytes_sent += payload_bytes as u64;
        self.last_packet_sent_timestamp = Some(now);
    }

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

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

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

    /// Called when an RTX packet is sent.
    pub fn on_rtx_sent(&mut self, bytes: usize) {
        self.retransmitted_packets_sent += 1;
        self.retransmitted_bytes_sent += bytes as u64;
    }

    /// Called when RTCP Receiver Report is received from remote.
    pub fn on_rtcp_rr_received(
        &mut self,
        packets_received: u64,
        packets_lost: u64,
        jitter: f64,
        fraction_lost: f64,
        rtt: f64,
    ) {
        self.remote_packets_received = packets_received;
        self.remote_packets_lost = packets_lost;
        self.remote_jitter = jitter;
        self.remote_fraction_lost = fraction_lost;
        self.remote_round_trip_time = rtt;
        self.rtt_measurements += 1;
    }

    /// Called when a video frame is sent (marker bit set).
    pub fn on_frame_sent(&mut self, is_huge: bool) {
        self.frames_sent += 1;
        if is_huge {
            self.huge_frames_sent += 1;
        }
    }

    /// Creates a snapshot of the accumulated stats at the given timestamp.
    pub fn snapshot(&self, now: Instant, id: &str) -> RTCOutboundRtpStreamStats {
        RTCOutboundRtpStreamStats {
            sent_rtp_stream_stats: RTCSentRtpStreamStats {
                rtp_stream_stats: RTCRtpStreamStats {
                    stats: RTCStats {
                        timestamp: now,
                        typ: RTCStatsType::OutboundRTP,
                        id: id.to_string(),
                    },
                    ssrc: self.ssrc,
                    kind: self.kind,
                    transport_id: self.transport_id.clone(),
                    codec_id: self.codec_id.clone(),
                },
                packets_sent: self.packets_sent,
                bytes_sent: self.bytes_sent,
            },
            mid: self.mid.clone(),
            media_source_id: self.media_source_id.clone(),
            remote_id: format!("RTCRemoteInboundRTPStream_{}_{}", self.kind, self.ssrc),
            rid: self.rid.clone(),
            encoding_index: self.encoding_index,
            header_bytes_sent: self.header_bytes_sent,
            retransmitted_packets_sent: self.retransmitted_packets_sent,
            retransmitted_bytes_sent: self.retransmitted_bytes_sent,
            rtx_ssrc: self.rtx_ssrc.unwrap_or(0),
            target_bitrate: self.target_bitrate,
            total_encoded_bytes_target: 0,
            frame_width: self
                .encoder_stats
                .as_ref()
                .map(|s| s.frame_width)
                .unwrap_or(0),
            frame_height: self
                .encoder_stats
                .as_ref()
                .map(|s| s.frame_height)
                .unwrap_or(0),
            frames_per_second: self.frames_per_second,
            frames_sent: self.frames_sent,
            huge_frames_sent: self.huge_frames_sent,
            frames_encoded: self
                .encoder_stats
                .as_ref()
                .map(|s| s.frames_encoded)
                .unwrap_or(0),
            key_frames_encoded: self
                .encoder_stats
                .as_ref()
                .map(|s| s.key_frames_encoded)
                .unwrap_or(0),
            qp_sum: self.encoder_stats.as_ref().map(|s| s.qp_sum).unwrap_or(0),
            psnr_sum: HashMap::new(),
            psnr_measurements: 0,
            total_encode_time: self
                .encoder_stats
                .as_ref()
                .map(|s| s.total_encode_time)
                .unwrap_or(0.0),
            total_packet_send_delay: self.total_packet_send_delay,
            quality_limitation_reason: self.quality_limitation_reason,
            quality_limitation_durations: HashMap::new(),
            quality_limitation_resolution_changes: self.quality_limitation_resolution_changes,
            nack_count: self.nack_count,
            fir_count: self.fir_count,
            pli_count: self.pli_count,
            encoder_implementation: self
                .encoder_stats
                .as_ref()
                .map(|s| s.encoder_implementation.clone())
                .unwrap_or_default(),
            power_efficient_encoder: self
                .encoder_stats
                .as_ref()
                .map(|s| s.power_efficient_encoder)
                .unwrap_or(false),
            active: self.active,
            scalability_mode: self
                .encoder_stats
                .as_ref()
                .map(|s| s.scalability_mode.clone())
                .unwrap_or_default(),
            packets_sent_with_ect1: 0,
        }
    }

    /// Creates a snapshot of remote inbound stats from RTCP RR data.
    pub fn snapshot_remote(&self, now: Instant) -> RTCRemoteInboundRtpStreamStats {
        RTCRemoteInboundRtpStreamStats {
            received_rtp_stream_stats: RTCReceivedRtpStreamStats {
                rtp_stream_stats: RTCRtpStreamStats {
                    stats: RTCStats {
                        timestamp: now,
                        typ: RTCStatsType::RemoteInboundRTP,
                        id: format!("RTCRemoteInboundRTPStream_{}_{}", self.kind, self.ssrc),
                    },
                    ssrc: self.ssrc,
                    kind: self.kind,
                    transport_id: self.transport_id.clone(),
                    codec_id: self.codec_id.clone(),
                },
                packets_received: self.remote_packets_received,
                packets_received_with_ect1: 0,
                packets_received_with_ce: 0,
                packets_reported_as_lost: self.remote_packets_lost,
                packets_reported_as_lost_but_recovered: 0,
                packets_lost: self.remote_packets_lost as i64,
                jitter: self.remote_jitter,
            },
            local_id: format!("RTCOutboundRTPStream_{}_{}", self.kind, self.ssrc),
            round_trip_time: self.remote_round_trip_time,
            total_round_trip_time: self.remote_round_trip_time * self.rtt_measurements as f64,
            fraction_lost: self.remote_fraction_lost,
            round_trip_time_measurements: self.rtt_measurements,
            packets_with_bleached_ect1_marking: 0,
        }
    }
}

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

    #[test]
    fn test_default() {
        let acc = OutboundRtpStreamAccumulator::default();
        assert_eq!(acc.ssrc, 0);
        assert_eq!(acc.packets_sent, 0);
        assert_eq!(acc.bytes_sent, 0);
        assert_eq!(acc.header_bytes_sent, 0);
        assert_eq!(acc.retransmitted_packets_sent, 0);
        assert_eq!(acc.retransmitted_bytes_sent, 0);
        assert_eq!(acc.frames_sent, 0);
        assert_eq!(acc.huge_frames_sent, 0);
        assert_eq!(acc.nack_count, 0);
        assert_eq!(acc.fir_count, 0);
        assert_eq!(acc.pli_count, 0);
        assert!(!acc.active);
    }

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

        acc.on_rtp_sent(12, 1188, now);
        assert_eq!(acc.packets_sent, 1);
        assert_eq!(acc.header_bytes_sent, 12);
        assert_eq!(acc.bytes_sent, 1188);
        assert_eq!(acc.last_packet_sent_timestamp, Some(now));

        let later = now + std::time::Duration::from_millis(20);
        acc.on_rtp_sent(12, 1000, later);
        assert_eq!(acc.packets_sent, 2);
        assert_eq!(acc.header_bytes_sent, 24);
        assert_eq!(acc.bytes_sent, 2188);
        assert_eq!(acc.last_packet_sent_timestamp, Some(later));
    }

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

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

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

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

    #[test]
    fn test_on_rtx_sent() {
        let mut acc = OutboundRtpStreamAccumulator::default();

        acc.on_rtx_sent(500);
        acc.on_rtx_sent(600);
        assert_eq!(acc.retransmitted_packets_sent, 2);
        assert_eq!(acc.retransmitted_bytes_sent, 1100);
    }

    #[test]
    fn test_on_rtcp_rr_received() {
        let mut acc = OutboundRtpStreamAccumulator::default();

        acc.on_rtcp_rr_received(100, 5, 0.003, 0.05, 0.025);
        assert_eq!(acc.remote_packets_received, 100);
        assert_eq!(acc.remote_packets_lost, 5);
        assert_eq!(acc.remote_jitter, 0.003);
        assert_eq!(acc.remote_fraction_lost, 0.05);
        assert_eq!(acc.remote_round_trip_time, 0.025);
        assert_eq!(acc.rtt_measurements, 1);

        // Second RR with updated values
        acc.on_rtcp_rr_received(200, 8, 0.004, 0.04, 0.030);
        assert_eq!(acc.remote_packets_received, 200);
        assert_eq!(acc.remote_packets_lost, 8);
        assert_eq!(acc.remote_jitter, 0.004);
        assert_eq!(acc.remote_fraction_lost, 0.04);
        assert_eq!(acc.remote_round_trip_time, 0.030);
        assert_eq!(acc.rtt_measurements, 2);
    }

    #[test]
    fn test_on_frame_sent() {
        let mut acc = OutboundRtpStreamAccumulator::default();

        acc.on_frame_sent(false);
        acc.on_frame_sent(false);
        acc.on_frame_sent(true); // huge frame
        acc.on_frame_sent(false);
        acc.on_frame_sent(true); // huge frame

        assert_eq!(acc.frames_sent, 5);
        assert_eq!(acc.huge_frames_sent, 2);
    }

    #[test]
    fn test_full_outbound_stream_flow() {
        let mut acc = OutboundRtpStreamAccumulator {
            ssrc: 87654321,
            kind: RtpCodecKind::Video,
            transport_id: "RTCTransport_0".to_string(),
            codec_id: "RTCCodec_video_96".to_string(),
            mid: "0".to_string(),
            rid: "h".to_string(),
            rtx_ssrc: Some(87654322),
            active: true,
            ..Default::default()
        };

        let now = Instant::now();

        // Send RTP packets
        for i in 0..100 {
            acc.on_rtp_sent(12, 1200, now + std::time::Duration::from_millis(i * 33));
        }

        // Send frames
        for _ in 0..30 {
            acc.on_frame_sent(false);
        }
        acc.on_frame_sent(true); // One huge frame

        // Receive NACK and resend
        acc.on_nack_received();
        acc.on_rtx_sent(1200);

        // Receive RR from remote
        acc.on_rtcp_rr_received(95, 5, 0.002, 0.05, 0.020);

        assert_eq!(acc.packets_sent, 100);
        assert_eq!(acc.bytes_sent, 120000);
        assert_eq!(acc.frames_sent, 31);
        assert_eq!(acc.huge_frames_sent, 1);
        assert_eq!(acc.nack_count, 1);
        assert_eq!(acc.retransmitted_packets_sent, 1);
        assert_eq!(acc.retransmitted_bytes_sent, 1200);
        assert_eq!(acc.remote_packets_received, 95);
        assert_eq!(acc.remote_packets_lost, 5);
        assert_eq!(acc.rtt_measurements, 1);
    }

    #[test]
    fn test_snapshot() {
        let now = Instant::now();
        let mut acc = OutboundRtpStreamAccumulator {
            ssrc: 44444444,
            kind: RtpCodecKind::Audio,
            transport_id: "RTCTransport_0".to_string(),
            codec_id: "RTCCodec_audio_111".to_string(),
            mid: "1".to_string(),
            active: true,
            ..Default::default()
        };

        acc.on_rtp_sent(12, 160, now);
        acc.on_rtp_sent(12, 160, now);

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

        assert_eq!(
            stats.sent_rtp_stream_stats.rtp_stream_stats.stats.id,
            "RTCOutboundRTPStream_audio_44444444"
        );
        assert_eq!(
            stats.sent_rtp_stream_stats.rtp_stream_stats.stats.typ,
            RTCStatsType::OutboundRTP
        );
        assert_eq!(stats.sent_rtp_stream_stats.rtp_stream_stats.ssrc, 44444444);
        assert_eq!(
            stats.sent_rtp_stream_stats.rtp_stream_stats.kind,
            RtpCodecKind::Audio
        );
        assert_eq!(stats.sent_rtp_stream_stats.packets_sent, 2);
        assert_eq!(stats.sent_rtp_stream_stats.bytes_sent, 320);
        assert_eq!(stats.header_bytes_sent, 24);
        assert_eq!(stats.mid, "1");
        assert!(stats.active);
    }

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

        acc.on_rtcp_rr_received(500, 10, 0.005, 0.02, 0.035);
        acc.on_rtcp_rr_received(1000, 20, 0.004, 0.02, 0.030);

        let remote_stats = acc.snapshot_remote(now);

        assert_eq!(
            remote_stats
                .received_rtp_stream_stats
                .rtp_stream_stats
                .stats
                .typ,
            RTCStatsType::RemoteInboundRTP
        );
        assert_eq!(
            remote_stats.received_rtp_stream_stats.rtp_stream_stats.ssrc,
            55555555
        );
        assert_eq!(
            remote_stats.received_rtp_stream_stats.packets_received,
            1000
        );
        assert_eq!(remote_stats.received_rtp_stream_stats.packets_lost, 20);
        assert_eq!(remote_stats.received_rtp_stream_stats.jitter, 0.004);
        assert_eq!(remote_stats.round_trip_time, 0.030);
        assert_eq!(remote_stats.round_trip_time_measurements, 2);
        assert_eq!(remote_stats.fraction_lost, 0.02);
        assert!(remote_stats.local_id.contains("RTCOutboundRTPStream"));
    }

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

        acc.on_rtp_sent(12, 1200, now);
        acc.on_frame_sent(false);
        acc.on_nack_received();

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

        let json = serde_json::to_string(&stats).expect("should serialize");
        assert!(json.contains("\"ssrc\":66666666"));
        assert!(json.contains("\"packetsSent\":1"));
        assert!(json.contains("\"bytesSent\":1200"));
        assert!(json.contains("\"framesSent\":1"));
        assert!(json.contains("\"nackCount\":1"));
        assert!(json.contains("\"active\":true"));
        assert!(json.contains("\"type\":\"outbound-rtp\""));
    }

    #[test]
    fn test_quality_limitation_tracking() {
        let mut acc = OutboundRtpStreamAccumulator::default();

        acc.quality_limitation_reason = RTCQualityLimitationReason::Bandwidth;
        acc.quality_limitation_resolution_changes = 3;
        acc.target_bitrate = 1_500_000.0;

        let now = Instant::now();
        let stats = acc.snapshot(now, "test");

        assert_eq!(
            stats.quality_limitation_reason,
            RTCQualityLimitationReason::Bandwidth
        );
        assert_eq!(stats.quality_limitation_resolution_changes, 3);
        assert_eq!(stats.target_bitrate, 1_500_000.0);
    }
}