rtci-interceptor 0.1.0

Sans-I/O RTP/RTCP interceptor pipeline for WebRTC: RTCP reports, NACK, TWCC, congestion control, pacing, FEC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! RTCP transmission timing through the session interceptor: RFC 3550 6.2 and 6.3 (interval,
//! minimum, initial delay, timer reconsideration, reverse reconsideration, BYE back-off,
//! RTCP bandwidth) and the regular-packet rules of RFC 4585 3.5.3, in virtual time with fixed
//! random numbers.

mod rtcp_session_common;

use rtci_interceptor::{Interceptor, RtcpBandwidth, RtcpProfile, RtcpSessionControl};
use rtcp::goodbye::Goodbye;
use rtcp::receiver_report::ReceiverReport;
use rtcp_session_common::*;
use sansio::Protocol;
use std::time::{Duration, Instant};

/// `e - 3/2`, the compensation of RFC 3550 6.3.1.
const COMPENSATION: f64 = std::f64::consts::E - 1.5;

fn offsets(sent: &[Sent], base: Instant) -> Vec<f64> {
    sent.iter()
        .map(|sent| sent.at.duration_since(base).as_secs_f64())
        .collect()
}

fn near(actual: f64, expected: f64) -> bool {
    (actual - expected).abs() < 0.002
}

fn remote_receiver_report(ssrc: u32) -> Vec<Box<dyn rtcp::Packet>> {
    vec![Box::new(ReceiverReport {
        ssrc,
        ..Default::default()
    })]
}

/// RFC 3550 6.2 under RTP/AVP: a five second minimum, halved for the first packet, and the
/// interval divided by 1.21828 (the random factor is fixed at 1.0 here).
#[test]
fn avp_reports_keep_the_five_second_minimum_and_half_of_it_at_first() {
    let mut chain = chain(
        builder()
            .with_profile(RtcpProfile::Avp)
            .with_bandwidth(RtcpBandwidth {
                session: Some(64_000),
                ..Default::default()
            }),
    );
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    let sent = run(&mut chain, base + secs(11.0));
    let at = offsets(&sent, base);
    assert_eq!(3, at.len(), "{at:?}");
    assert!(near(at[0], 2.5 / COMPENSATION), "{at:?}");
    assert!(near(at[1] - at[0], 5.0 / COMPENSATION), "{at:?}");
    assert!(near(at[2] - at[1], 5.0 / COMPENSATION), "{at:?}");
}

/// RFC 3550 6.2: the reduced minimum is 360 divided by the session bandwidth in kbit/s: half
/// a second at 720 kbit/s.
#[test]
fn avp_reports_can_use_the_reduced_minimum() {
    let mut chain = chain(
        builder()
            .with_profile(RtcpProfile::Avp)
            .with_reduced_minimum(true)
            .with_bandwidth(RtcpBandwidth {
                session: Some(720_000),
                ..Default::default()
            }),
    );
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    let sent = run(&mut chain, base + secs(1.5));
    let at = offsets(&sent, base);
    assert!(at.len() >= 3, "{at:?}");
    assert!(near(at[0], 0.25 / COMPENSATION), "{at:?}");
    assert!(near(at[1] - at[0], 0.5 / COMPENSATION), "{at:?}");
}

/// RFC 3550 6.2, 6.3.3 and 6.3.6: the interval scales with the number of members heard, and
/// a timer that expires after the membership grew is reconsidered, not fired.
#[test]
fn the_interval_grows_with_the_membership_by_timer_reconsideration() {
    let mut chain = chain(
        builder()
            .with_profile(RtcpProfile::Avp)
            .with_bandwidth(RtcpBandwidth {
                session: Some(8_000),
                ..Default::default()
            }),
    );
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    // Alone, the first report would leave after 2.05 s. Ten more members are heard first.
    for member in 0..10u32 {
        chain
            .handle_read(rtcp(
                base + secs(1.0),
                remote_receiver_report(7000 + member),
            ))
            .unwrap();
    }
    read(&mut chain);

    let sent = run(&mut chain, base + secs(40.0));
    let at = offsets(&sent, base);
    assert!(!at.is_empty());
    // 50 octets/s of RTCP, three quarters of it for 11 receivers with packets of about 50
    // octets: around 15 s, over 1.21828.
    assert!(at[0] > 9.0 && at[0] < 16.0, "{at:?}");
}

/// RFC 3550 6.3.4: when members leave, the time to the next report shrinks with the
/// membership (reverse reconsideration).
#[test]
fn the_next_report_comes_sooner_when_members_say_bye() {
    let mut chain = chain(
        builder()
            .with_profile(RtcpProfile::Avp)
            .with_bandwidth(RtcpBandwidth {
                session: Some(8_000),
                ..Default::default()
            }),
    );
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    for member in 0..10u32 {
        chain
            .handle_read(rtcp(
                base + secs(1.0),
                remote_receiver_report(7000 + member),
            ))
            .unwrap();
    }
    // The first timer expires at 2.05 s and is put off past 9 s (the test above).
    assert!(run(&mut chain, base + secs(3.0)).is_empty());

    chain
        .handle_read(rtcp(
            base + secs(3.0),
            vec![Box::new(Goodbye {
                sources: (0..10u32).map(|member| 7000 + member).collect(),
                reason: Default::default(),
            })],
        ))
        .unwrap();
    read(&mut chain);

    let sent = run(&mut chain, base + secs(40.0));
    let at = offsets(&sent, base);
    assert!(!at.is_empty());
    assert!(at[0] > 3.0 && at[0] < 6.5, "{at:?}");
}

/// RFC 4585 3.5.3: under RTP/AVPF regular reports are suppressed until `T_rr_interval` has
/// passed since the last one (one second by default here, dithered over [0.5, 1.5]).
#[test]
fn avpf_regular_reports_keep_t_rr_interval() {
    let mut chain = chain(builder());
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    let sent = run(&mut chain, base + secs(10.0));
    let at = offsets(&sent, base);
    // The first leaves within the first calculated interval (no minimum between two
    // parties, RFC 4585 3.5.1), the others at least `T_rr_interval` apart.
    assert!(at[0] < 0.5, "{at:?}");
    assert!((8..=11).contains(&at.len()), "{at:?}");
    for pair in at.windows(2) {
        let gap = pair[1] - pair[0];
        assert!((1.0..1.3).contains(&gap), "{at:?}");
    }
    assert!(sent.iter().all(Sent::is_compound));
}

/// RFC 4585 4.2: a negotiated `trr-int` is the minimum interval between regular reports.
#[test]
fn a_negotiated_trr_int_spaces_the_regular_reports() {
    let mut chain = chain(builder());
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    control(
        &mut chain,
        base,
        RtcpSessionControl::TrrInt(Duration::from_secs(5)),
    );

    let sent = run(&mut chain, base + secs(21.0));
    let at = offsets(&sent, base);
    assert!((4..=5).contains(&at.len()), "{at:?}");
    for pair in at.windows(2) {
        assert!(pair[1] - pair[0] >= 5.0, "{at:?}");
    }
}

/// A `trr-int` a later negotiation no longer has stops spacing the reports: the interval the
/// session was built with applies again (RFC 4585 4.2).
#[test]
fn a_cleared_trr_int_gives_back_the_configured_interval() {
    let mut chain = chain(builder());
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    control(
        &mut chain,
        base,
        RtcpSessionControl::TrrInt(Duration::from_secs(5)),
    );
    let sent = run(&mut chain, base + secs(11.0));
    assert!(
        offsets(&sent, base).len() <= 3,
        "{:?}",
        offsets(&sent, base)
    );

    let later = base + secs(11.0);
    control(&mut chain, later, RtcpSessionControl::TrrIntCleared);
    let sent = run(&mut chain, later + secs(10.0));
    // The builder's one-second interval: about ten reports in ten seconds.
    let at = offsets(&sent, later);
    assert!((8..=11).contains(&at.len()), "{at:?}");
}

/// RFC 4585 3.5.3 with `T_rr_interval` zero (its default when `trr-int` is not signalled):
/// every regular packet is sent, at the interval the RTCP bandwidth gives. RFC 3556: `b=RS`
/// and `b=RR` are that bandwidth. 8000 + 24000 bit/s, a lone receiver with 64-octet packets:
/// 64 / 3000 s, over 1.21828.
#[test]
fn rs_and_rr_set_the_rtcp_bandwidth() {
    let mut chain = chain(builder().with_t_rr_interval(Duration::ZERO).with_bandwidth(
        RtcpBandwidth {
            session: None,
            rs: Some(8_000),
            rr: Some(24_000),
        },
    ));
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    let sent = run(&mut chain, base + secs(1.0));
    let at = offsets(&sent, base);
    let expected = 64.0 / 3000.0 / COMPENSATION;
    assert!(at.len() > 40, "{} reports", at.len());
    for pair in at.windows(2) {
        assert!(near(pair[1] - pair[0], expected), "{at:?}");
    }
}

/// RFC 3556: `b=RR:0` turns the receivers' RTCP off.
#[test]
fn rr_zero_silences_a_receiver() {
    let mut chain = chain(builder().with_bandwidth(RtcpBandwidth {
        session: None,
        rs: Some(8_000),
        rr: Some(0),
    }));
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    assert!(run(&mut chain, base + secs(120.0)).is_empty());
}

/// The bandwidth can arrive after the session started, when the description is negotiated.
#[test]
fn the_negotiated_bandwidth_can_be_set_while_running() {
    let mut chain = chain(builder().with_t_rr_interval(Duration::ZERO));
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    control(
        &mut chain,
        base,
        RtcpSessionControl::Bandwidth(RtcpBandwidth {
            session: None,
            rs: Some(8_000),
            rr: Some(24_000),
        }),
    );
    run(&mut chain, base + secs(1.0));

    let sent = run(&mut chain, base + secs(2.0));
    let at = offsets(&sent, base);
    let expected = 64.0 / 3000.0 / COMPENSATION;
    assert!(near(at[1] - at[0], expected), "{at:?}");
}

/// RFC 3550 6.2: a session that is told nothing starts without waiting for a first packet
/// once started, and without being started, with the first packet it sees.
#[test]
fn a_session_that_was_not_started_joins_with_its_first_packet() {
    let mut chain = chain(builder());
    assert!(chain.poll_timeout().is_none());
    let base = Instant::now();
    chain.bind_remote_stream(&video(200));
    chain.handle_read(rtp(base, 200, 1, 0)).unwrap();
    read(&mut chain);

    let sent = run(&mut chain, base + secs(1.0));
    assert!(!sent.is_empty());
    assert_eq!(vec![200], sent[0].reported());
}

/// RFC 3550 6.3.7: with more than fifty members a BYE is not sent at once; it takes its turn
/// like a report (and is sent, a little later).
#[test]
fn a_bye_in_a_large_session_waits_for_its_turn() {
    let mut chain = chain(builder());
    let base = Instant::now();
    chain.bind_local_stream(&video(100));
    control(&mut chain, base, RtcpSessionControl::Start);
    chain.handle_write(rtp(base, 100, 1, 0)).unwrap();
    for member in 0..60u32 {
        chain
            .handle_read(rtcp(base, remote_receiver_report(7000 + member)))
            .unwrap();
    }
    read(&mut chain);
    run(&mut chain, base + secs(5.0));

    chain.unbind_local_stream(&video(100));
    let now = base + secs(5.0);
    chain.handle_timeout(now).unwrap();
    let at_once = written(&mut chain);
    assert!(at_once.iter().all(|sent| sent.goodbye().is_none()));

    let later = run(&mut chain, now + secs(5.0));
    let goodbye = later
        .iter()
        .find_map(|sent| sent.goodbye().map(|goodbye| (sent.at, goodbye)))
        .expect("the BYE is sent after its back-off");
    assert_eq!(vec![100], goodbye.1.sources);
    assert!(goodbye.0 > now);
}

/// RFC 3550 6.2: RTCP gets five percent of the session bandwidth. At 64 kbit/s that is 400
/// octets a second; a lone receiver shares three quarters of it (300), and with 64-octet
/// packets reports every 64 / 300 s, over 1.21828.
#[test]
fn rtcp_gets_five_percent_of_the_session_bandwidth() {
    let mut chain = chain(builder().with_t_rr_interval(Duration::ZERO).with_bandwidth(
        RtcpBandwidth {
            session: Some(64_000),
            ..Default::default()
        },
    ));
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);

    let sent = run(&mut chain, base + secs(2.0));
    let at = offsets(&sent, base);
    let expected = 64.0 / 300.0 / COMPENSATION;
    assert!(near(at[0], expected), "{at:?}");
    for pair in at.windows(2) {
        assert!(near(pair[1] - pair[0], expected), "{at:?}");
    }

    // Another share: ten percent halves the interval.
    let mut chain = self::chain(
        builder()
            .with_t_rr_interval(Duration::ZERO)
            .with_rtcp_fraction(0.10)
            .with_bandwidth(RtcpBandwidth {
                session: Some(64_000),
                ..Default::default()
            }),
    );
    control(&mut chain, base, RtcpSessionControl::Start);
    let sent = run(&mut chain, base + secs(1.0));
    assert!(near(offsets(&sent, base)[0], expected / 2.0));
}

/// RFC 8108 5.3: the reports of several local SSRCs leave in one compound packet per
/// interval, each SSRC keeping its own timer (5.3.2), not one packet per SSRC.
#[test]
fn several_local_ssrcs_report_together_once_per_interval() {
    let mut chain = chain(builder());
    let base = Instant::now();
    for ssrc in [10, 20, 30] {
        chain.bind_local_stream(&video(ssrc));
    }
    control(&mut chain, base, RtcpSessionControl::Start);

    let mut sent = vec![];
    for step in 0..100u32 {
        let now = base + secs(0.05) * step;
        for ssrc in [10, 20, 30] {
            chain
                .handle_write(rtp(now, ssrc, step as u16, step * 4500))
                .unwrap();
        }
        sent.extend(run(&mut chain, now));
    }

    // Five seconds: about one compound packet a second, each with all three sender reports.
    assert!((4..=7).contains(&sent.len()), "{} packets", sent.len());
    for sent in &sent {
        let mut reporting: Vec<u32> = sent
            .get::<rtcp::sender_report::SenderReport>()
            .iter()
            .map(|sr| sr.ssrc)
            .collect();
        reporting.sort_unstable();
        assert_eq!(vec![10, 20, 30], reporting);
        assert_eq!(3, sent.cnames().len());
    }
    for pair in offsets(&sent, base).windows(2) {
        assert!(pair[1] - pair[0] >= 0.9, "{:?}", offsets(&sent, base));
    }
}

/// Regular (no feedback) packets among `sent` after `from`.
fn regular_after(sent: &[Sent], from: Instant) -> usize {
    sent.iter()
        .filter(|sent| sent.at >= from)
        .filter(|sent| {
            !sent.has::<rtcp::transport_feedbacks::transport_layer_cc::TransportLayerCc>()
        })
        .count()
}

/// Twenty seconds of a receive-only session at a 16 kbit/s session bandwidth (100 octets of
/// RTCP a second), writing a 100-octet congestion control feedback packet every 250 ms (400
/// octets a second, more than the whole RTCP bandwidth). Returns the regular packets sent
/// after the first two seconds.
fn regular_reports_beside_congestion_feedback(in_budget: bool) -> usize {
    use rtcp::transport_feedbacks::transport_layer_cc::{
        PacketStatusChunk, RunLengthChunk, StatusChunkTypeTcc, SymbolTypeTcc, TransportLayerCc,
    };
    let mut chain = chain(
        builder()
            .with_t_rr_interval(Duration::ZERO)
            .with_congestion_feedback_in_budget(in_budget)
            .with_bandwidth(RtcpBandwidth {
                session: Some(16_000),
                ..Default::default()
            }),
    );
    let base = Instant::now();
    control(&mut chain, base, RtcpSessionControl::Start);
    let mut sent = vec![];
    for step in 0..80u32 {
        let now = base + secs(0.25) * step;
        sent.extend(run(&mut chain, now));
        let feedback = TransportLayerCc {
            sender_ssrc: 0,
            media_ssrc: 0x1234,
            base_sequence_number: step as u16 * 10,
            packet_status_count: 10,
            reference_time: step,
            fb_pkt_count: step as u8,
            packet_chunks: vec![
                PacketStatusChunk::RunLengthChunk(RunLengthChunk {
                    type_tcc: StatusChunkTypeTcc::RunLengthChunk,
                    packet_status_symbol: SymbolTypeTcc::PacketReceivedSmallDelta,
                    run_length: 10,
                });
                20
            ],
            recv_deltas: vec![],
        };
        chain
            .handle_write(rtcp(now, vec![Box::new(feedback)]))
            .unwrap();
        sent.extend(written(&mut chain));
    }
    sent.extend(run(&mut chain, base + secs(20.0)));
    regular_after(&sent, base + secs(2.0))
}

/// `RtcpSessionBuilder::with_congestion_feedback_in_budget` (the `Performance` behaviour
/// profile): congestion control feedback that leaves at once counts against the RTCP
/// bandwidth, and the regular reports space out (as a `T_rr_interval`, RFC 4585 3.5.3) to
/// what it leaves, here nothing: one every five seconds, the most it stretches to. Without
/// it (the default) the regular reports keep RFC 3550's interval for the whole bandwidth.
#[test]
fn congestion_feedback_in_the_budget_spaces_the_regular_reports() {
    let outside = regular_reports_beside_congestion_feedback(false);
    let inside = regular_reports_beside_congestion_feedback(true);
    assert!(outside >= 15, "default: {outside} regular reports in 18 s");
    assert!(
        (3..=5).contains(&inside),
        "in the budget: {inside} regular reports in 18 s (default {outside})"
    );
}

/// The regular packets of a sender of about 27 kbit/s (thirty 112-octet packets a second), no
/// `b=` line, over seconds 4 to 24.
fn regular_reports_at_low_media(minimum: Option<u64>) -> usize {
    let builder = builder().with_t_rr_interval(Duration::ZERO);
    let builder = match minimum {
        Some(minimum) => builder.with_minimum_session_bandwidth(minimum),
        None => builder,
    };
    let mut chain = chain(builder);
    let base = Instant::now();
    chain.bind_local_stream(&video(10));
    control(&mut chain, base, RtcpSessionControl::Start);
    let mut sent = vec![];
    for step in 0..720u32 {
        let now = base + secs(1.0 / 30.0) * step;
        chain
            .handle_write(rtp(now, 10, step as u16, step * 3000))
            .unwrap();
        sent.extend(run(&mut chain, now));
    }
    regular_after(&sent, base + secs(4.0))
}

/// RFC 3550 6.2 with the media measured: by default the session bandwidth is never taken
/// below 64 kbit/s, so a 27 kbit/s sender reports as a 64 kbit/s one would; with
/// `with_minimum_session_bandwidth(16_000)` (the `Performance` profile) the RTCP bandwidth
/// follows the media, and the sender reports about 27/64 as often.
#[test]
fn the_minimum_session_bandwidth_lets_rtcp_follow_low_media() {
    let floor = regular_reports_at_low_media(None) as f64;
    let follows = regular_reports_at_low_media(Some(16_000)) as f64;
    assert!(floor >= 40.0, "{floor}");
    let ratio = follows / floor;
    assert!(
        (0.3..=0.6).contains(&ratio),
        "{follows} / {floor} = {ratio:.2}"
    );
}