rtci-interceptor 0.1.0

Sans-I/O RTP/RTCP interceptor pipeline for WebRTC: RTCP reports, NACK, TWCC, congestion control, pacing, FEC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! RFC 3611 section 4: the report blocks a receiver writes about each source.

use super::*;
use crate::stream_info::RTCPFeedback;
use bytes::Bytes;
use rtcp::extended_report::VoIPMetricsReportBlock;

const SSRC: u32 = 0x0A0B_0C0D;

fn stream(formats: &[&str]) -> StreamInfo {
    StreamInfo {
        ssrc: SSRC,
        clock_rate: 8_000,
        rtcp_feedback: formats
            .iter()
            .map(|format| RTCPFeedback {
                typ: RTCP_FB_XR.to_owned(),
                parameter: (*format).to_owned(),
            })
            .collect(),
        ..Default::default()
    }
}

fn interceptor(formats: &[&str]) -> XrReportInterceptor {
    let mut xr = XrReportBuilder::new().with_sender_ssrc(1).build();
    xr.bind_remote_stream(&stream(formats));
    xr
}

fn rtp(now: Instant, seq: u16, timestamp: u32) -> TaggedPacket {
    TaggedPacket {
        now,
        transport: TransportContext::default(),
        message: AttributedPacket::new(Packet::Rtp(rtp::Packet {
            header: rtp::header::Header {
                version: 2,
                payload_type: 0,
                sequence_number: seq,
                timestamp,
                ssrc: SSRC,
                ..Default::default()
            },
            payload: Bytes::from_static(&[0; 160]),
        })),
    }
}

fn ms(n: u64) -> Duration {
    Duration::from_millis(n)
}

/// Feeds packets `(seq, arrival offset in ms)` at 160 ticks (20 ms) per sequence number.
fn feed(xr: &mut XrReportInterceptor, base: Instant, packets: &[(u16, u64)], first_seq: u16) {
    for (seq, at) in packets {
        let timestamp = u32::from(seq.wrapping_sub(first_seq)) * 160;
        xr.handle_read(rtp(base + ms(*at), *seq, timestamp))
            .unwrap();
        while xr.poll_read().is_some() {}
    }
}

/// The XR written at `at`, if any.
fn report(xr: &mut XrReportInterceptor, at: Instant) -> Option<ExtendedReport> {
    xr.handle_timeout(at).unwrap();
    let written = xr.poll_write()?;
    match written.message.packet {
        Packet::Rtcp(mut packets) => {
            let packet = packets.remove(0);
            Some(
                packet
                    .as_any()
                    .downcast_ref::<ExtendedReport>()
                    .expect("an XR")
                    .clone(),
            )
        }
        Packet::Rtp(_) => panic!("RTP written"),
    }
}

fn block<T: 'static + Clone>(report: &ExtendedReport) -> Vec<T> {
    report
        .reports
        .iter()
        .filter_map(|b| b.as_any().downcast_ref::<T>().cloned())
        .collect()
}

/// Packets 10 to 19 at 20 ms each, 13 and 15 lost.
fn ten_with_two_lost() -> Vec<(u16, u64)> {
    (10u16..20)
        .filter(|seq| *seq != 13 && *seq != 15)
        .map(|seq| (seq, u64::from(seq - 10) * 20))
        .collect()
}

/// RFC 3611 section 5.1: "participants SHOULD NOT send XR blocks other than the ones
/// indicated by the parameters": a stream that negotiated none gets none.
#[test]
fn a_stream_that_negotiated_no_xr_format_gets_no_block() {
    let base = Instant::now();
    let mut xr = interceptor(&[]);
    feed(&mut xr, base, &ten_with_two_lost(), 10);
    assert!(report(&mut xr, base + ms(1_000)).is_none());
    assert_eq!(None, xr.poll_timeout());
}

/// RFC 3611 section 4.1: one bit per sequence number of `begin_seq..end_seq`, 1 for a packet
/// that arrived; the next block starts where this one ended.
#[test]
fn the_loss_rle_block_marks_each_sequence_number_received_or_not() {
    let base = Instant::now();
    let mut xr = interceptor(&["pkt-loss-rle"]);
    feed(&mut xr, base, &ten_with_two_lost(), 10);
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    assert_eq!(1, report.sender_ssrc);
    let loss = block::<RLEReportBlock>(&report);
    assert_eq!(1, loss.len());
    assert!(loss[0].is_loss_rle);
    assert_eq!(SSRC, loss[0].ssrc);
    assert_eq!((10, 20), (loss[0].begin_seq, loss[0].end_seq));
    let mut expected = vec![true; 10];
    expected[3] = false;
    expected[5] = false;
    assert_eq!(expected, loss[0].flags());

    feed(&mut xr, base, &[(20, 1_010), (21, 1_030)], 10);
    let next = self::report(&mut xr, base + ms(2_000)).expect("an XR");
    let loss = block::<RLEReportBlock>(&next);
    assert_eq!((20, 22), (loss[0].begin_seq, loss[0].end_seq));
}

/// RFC 3611 section 4.1 and Appendix A.1: across the 16-bit wrap the range runs on; a
/// sequence number 32,768 behind is taken as behind, not ahead with a rollover.
#[test]
fn the_reported_range_runs_across_the_sequence_number_wrap() {
    let base = Instant::now();
    let mut xr = interceptor(&["pkt-loss-rle"]);
    feed(
        &mut xr,
        base,
        &[(65_534, 0), (65_535, 20), (1, 60), (2, 80)],
        65_534,
    );
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    let loss = block::<RLEReportBlock>(&report);
    assert_eq!((65_534, 3), (loss[0].begin_seq, loss[0].end_seq));
    assert_eq!(vec![true, true, false, true, true], loss[0].flags());
}

/// RFC 3611 section 4.2: the Duplicate RLE block marks the sequence numbers that arrived more
/// than once.
#[test]
fn duplicates_are_marked_in_the_duplicate_rle_block() {
    let base = Instant::now();
    let mut xr = interceptor(&["pkt-dup-rle"]);
    feed(&mut xr, base, &[(1, 0), (2, 20), (2, 25), (3, 40)], 1);
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    let duplicates = block::<RLEReportBlock>(&report);
    assert!(!duplicates[0].is_loss_rle);
    assert_eq!(vec![false, true, false], duplicates[0].flags());
}

/// RFC 3611 section 4.3: "At least one packet MUST have been received for each sequence
/// number reported upon in this block", so a range with losses takes one block per run of
/// received packets; and of duplicates "any time other than the earliest MUST NOT be
/// reported". Receipt times are in RTP timestamp units, 20 ms apart at 8 kHz.
#[test]
fn receipt_times_cover_only_received_runs_and_the_earliest_copy() {
    let base = Instant::now();
    let mut xr = interceptor(&["pkt-rcpt-times"]);
    let mut packets = ten_with_two_lost();
    // A late copy of 11, 500 ms after the first.
    packets.push((11, 520));
    feed(&mut xr, base, &packets, 10);
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    let blocks = block::<PacketReceiptTimesReportBlock>(&report);
    let ranges: Vec<(u16, u16)> = blocks.iter().map(|b| (b.begin_seq, b.end_seq)).collect();
    assert_eq!(vec![(10, 13), (14, 15), (16, 20)], ranges);
    let first = &blocks[0].receipt_time;
    assert_eq!(3, first.len());
    assert_eq!(
        160,
        first[1].wrapping_sub(first[0]),
        "11 is reported at its first arrival"
    );
    assert_eq!(160, first[2].wrapping_sub(first[1]));
    assert_eq!(
        4 * 160,
        blocks[1].receipt_time[0].wrapping_sub(first[0]),
        "14 arrived 80 ms after 10"
    );
}

/// RFC 3611 section 4.6: the counts of lost and duplicate packets of the range, and the
/// minimum, maximum, mean and deviation of the relative transit time between consecutive
/// packets; the TTL cannot be measured here, so ToH says it is not reported even when asked.
#[test]
fn the_statistics_summary_counts_loss_duplicates_and_transit_jitter() {
    let base = Instant::now();
    let mut xr = interceptor(&["stat-summary=loss,dup,jitt,TTL"]);
    // 1 to 5 every 20 ms, 4 arriving 10 ms late, a copy of 2, and 6 missing before 7.
    feed(
        &mut xr,
        base,
        &[
            (1, 0),
            (2, 20),
            (2, 21),
            (3, 40),
            (4, 70),
            (5, 80),
            (7, 120),
        ],
        1,
    );
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    let summary = block::<StatisticsSummaryReportBlock>(&report);
    let summary = &summary[0];
    assert!(summary.loss_reports && summary.duplicate_reports && summary.jitter_reports);
    assert_eq!(TTLorHopLimitType::Missing, summary.ttl_or_hop_limit);
    assert_eq!((1, 8), (summary.begin_seq, summary.end_seq));
    assert_eq!(1, summary.lost_packets);
    assert_eq!(1, summary.dup_packets);
    // Transit differences in ticks: 0 (2), 0 (3), 80 (4 late), 80 (5 back), 0 (7).
    assert_eq!(0, summary.min_jitter);
    assert_eq!(80, summary.max_jitter);
    assert_eq!(32, summary.mean_jitter);
    assert_eq!(39, summary.dev_jitter);
    assert_eq!(0, summary.max_ttl_or_hl);
}

/// RFC 3611 section 4.6: a field the signalling did not ask for carries no report.
#[test]
fn the_statistics_summary_reports_only_the_fields_asked_for() {
    let base = Instant::now();
    let mut xr = interceptor(&["stat-summary=loss"]);
    feed(&mut xr, base, &ten_with_two_lost(), 10);
    let report = report(&mut xr, base + ms(1_000)).expect("an XR");
    let summary = &block::<StatisticsSummaryReportBlock>(&report)[0];
    assert!(summary.loss_reports);
    assert!(!summary.duplicate_reports && !summary.jitter_reports);
    assert_eq!(2, summary.lost_packets);
    assert_eq!(0, summary.max_jitter);
}

/// RFC 3611 section 4.7: the loss rate since the beginning of reception, the burst and gap
/// metrics, the round trip delay once there is an estimate, the end system delay 0 when
/// unknown, a constant non-zero Gmin and the jitter buffer the application described; the
/// rates and densities are zero before anything was lost.
#[test]
fn voip_metrics_are_populated_from_the_reception() {
    let base = Instant::now();
    let mut xr = XrReportBuilder::new()
        .with_formats(["voip-metrics"])
        .with_jitter_buffer(JitterBufferMetrics {
            adaptive: Some(true),
            rate: 4,
            nominal_ms: 40,
            maximum_ms: 120,
            absolute_maximum_ms: 200,
        })
        .build();
    xr.bind_remote_stream(&stream(&[]));
    let clean: Vec<(u16, u64)> = (0u16..50).map(|seq| (seq, u64::from(seq) * 20)).collect();
    feed(&mut xr, base, &clean, 0);
    let first = report(&mut xr, base + ms(1_000)).expect("an XR");
    let first = &block::<VoIPMetricsReportBlock>(&first)[0];
    assert_eq!(0, first.loss_rate);
    assert_eq!(0, first.discard_rate);
    assert_eq!((0, 0), (first.burst_density, first.gap_density));
    assert_eq!(0, first.burst_duration);
    assert_eq!(1_000, first.gap_duration, "50 packets of 20 ms");
    assert_eq!(0, first.round_trip_delay, "no estimate yet");
    assert_eq!(0, first.end_system_delay);
    assert_eq!(16, first.gmin);
    assert_eq!(0b0011_0100, first.rx_config, "adaptive, rate 4");
    assert_eq!(
        (40, 120, 200),
        (first.jb_nominal, first.jb_maximum, first.jb_abs_max)
    );
    assert_eq!(127, first.mos_lq, "not available");

    // The RTCP session measures a round trip time.
    xr.handle_read(TaggedPacket {
        now: base + ms(1_100),
        transport: TransportContext::default(),
        message: AttributedPacket::new(Packet::Rtcp(vec![])).with(crate::Attribute::Custom(
            std::sync::Arc::new(RtcpSessionEvent::RoundTripTime {
                ssrc: 9,
                rtt: ms(87),
            }),
        )),
    })
    .unwrap();
    while xr.poll_read().is_some() {}
    // 50 to 99 with a burst: 60, 61 and 63 lost.
    let lossy: Vec<(u16, u64)> = (50u16..100)
        .filter(|seq| ![60, 61, 63].contains(seq))
        .map(|seq| (seq, u64::from(seq) * 20))
        .collect();
    feed(&mut xr, base, &lossy, 0);
    let second = report(&mut xr, base + ms(2_000)).expect("an XR");
    let second = &block::<VoIPMetricsReportBlock>(&second)[0];
    assert_eq!(87, second.round_trip_delay);
    assert_eq!(16, second.gmin, "Gmin is constant across the session");
    // 3 lost of 100 expected since the beginning: 7.68, integer part 7.
    assert_eq!(7, second.loss_rate);
    // One burst, 60 to 63: 3 of 4 lost.
    assert_eq!(192, second.burst_density);
    assert_eq!(80, second.burst_duration);
    assert_eq!(0, second.gap_density);
    assert_eq!(960, second.gap_duration, "96 packets in two gaps");
}

/// RFC 3611 section 5.1: `pkt-loss-rle=<size>` is the largest size the block should have;
/// thinning brings it under (section 4.1).
#[test]
fn a_signalled_size_thins_the_block_until_it_fits() {
    let base = Instant::now();
    let mut xr = interceptor(&["pkt-loss-rle=16"]);
    // Every odd sequence number lost: at full resolution a bit vector each 15 packets.
    let packets: Vec<(u16, u64)> = (0u16..100)
        .filter(|seq| seq % 2 == 0)
        .map(|seq| (seq, u64::from(seq) * 20))
        .collect();
    feed(&mut xr, base, &packets, 0);
    let report = report(&mut xr, base + ms(3_000)).expect("an XR");
    let loss = &block::<RLEReportBlock>(&report)[0];
    assert!(loss.marshal_size() <= 16, "{loss:?}");
    assert_eq!(1, loss.t);
    assert!(
        loss.flags().iter().all(|f| *f),
        "the even numbers all arrived"
    );
}

/// RFC 3611 section 4.3: receipt times are in the stream's RTP timestamp clock and "the first
/// receipt time value SHOULD also be chosen at random". Receivers fed the same packets at the
/// same instants report first receipt times that differ, and keep 160 ticks (20 ms at 8 kHz)
/// between consecutive packets.
#[test]
fn the_first_receipt_time_is_random() {
    let base = Instant::now();
    let firsts: Vec<u32> = (0..64)
        .map(|_| {
            let mut xr = interceptor(&["pkt-rcpt-times"]);
            feed(&mut xr, base, &[(10, 0), (11, 20)], 10);
            let report = report(&mut xr, base + ms(1_000)).expect("an XR");
            let blocks = block::<PacketReceiptTimesReportBlock>(&report);
            let times = &blocks[0].receipt_time;
            assert_eq!(160, times[1].wrapping_sub(times[0]));
            times[0]
        })
        .collect();
    let distinct: std::collections::HashSet<u32> = firsts.iter().copied().collect();
    assert_eq!(distinct.len(), firsts.len(), "{firsts:?}");
}

/// RFC 3611 section 4.7.4: "signal level ... measured only for packets containing speech
/// energy" and "noise level ... measured during periods of silence", in dBm0, 127 when not
/// available. From the RFC 6464 audio level the stream carries: 30 packets with voice at 20
/// -dBov and 20 without it at 70 -dBov give a signal level of -17 dBm0 and a noise level of
/// -67; a stream without the extension reports both as 127. RERL and the call quality
/// figures (section 4.7.5) are the application's: 127.
#[test]
fn voip_metrics_carry_the_signal_and_noise_levels_of_the_audio_level_extension() {
    use rtp::extension::audio_level_extension::AudioLevelExtension;
    use shared::marshal::Marshal;
    let base = Instant::now();
    let levels = |with_extension: bool| {
        let mut xr = XrReportBuilder::new()
            .with_formats(["voip-metrics"])
            .build();
        let mut info = stream(&[]);
        if with_extension {
            info.rtp_header_extensions = vec![crate::RTPHeaderExtension {
                uri: rtp::extension::AUDIO_LEVEL_URI.to_owned(),
                id: 3,
            }];
        }
        xr.bind_remote_stream(&info);
        for seq in 0u16..50 {
            let mut packet = rtp(base + ms(u64::from(seq) * 20), seq, u32::from(seq) * 160);
            if let Packet::Rtp(rtp_packet) = &mut packet.message.packet {
                let voice = seq < 30;
                let level = AudioLevelExtension {
                    level: if voice { 20 } else { 70 },
                    voice,
                };
                rtp_packet
                    .header
                    .set_extension(3, level.marshal().unwrap().freeze())
                    .unwrap();
            }
            xr.handle_read(packet).unwrap();
            while xr.poll_read().is_some() {}
        }
        let report = report(&mut xr, base + ms(1_000)).expect("an XR");
        let block = &block::<VoIPMetricsReportBlock>(&report)[0];
        (
            block.signal_level as i8,
            block.noise_level as i8,
            block.rerl,
            block.rfactor,
        )
    };
    assert_eq!((-17, -67, 127, 127), levels(true));
    assert_eq!((127, 127, 127, 127), levels(false));
}