rtc 0.21.0-rc.2

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
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
//! FlexFEC-03 end to end: the encoder protects, the path loses, the decoder rebuilds.
//!
//! The two halves of [`play-from-disk-fec`](../examples/play-from-disk-fec) and
//! [`save-to-disk-fec`](../examples/save-to-disk-fec) in one process, with the assertion those
//! examples can only gesture at: **every media packet the sender discarded arrives at the
//! receiver, and arrives marked as rebuilt.**
//!
//! # Why sequence numbers rather than counts
//!
//! A recovered packet is re-injected where the decoder finishes rebuilding it, not where it would
//! have arrived, so the inbound stream is not in sequence order. Counting is therefore not enough —
//! "we dropped 10 and received 200" holds just as well if FEC rebuilt the wrong ten. Both sides
//! report the sequence numbers they acted on and the test compares the two *sets*, which is order
//! independent and says exactly which packets were involved.
//!
//! # Why the chain is this small
//!
//! Neither side calls `register_default_interceptors`. With the NACK pair in the chain the receiver
//! would ask for the missing packets and the sender would retransmit them, so the packets would
//! come back and the test would pass with the FEC decoder doing nothing at all. Retransmission is a
//! perfectly good recovery mechanism; it is just not the one under test.

use anyhow::Result;
use bytes::BytesMut;
use rtc::interceptor::{
    Attribute, FlexFec03ReceiveBuilder, FlexFec03SendBuilder, Interceptor, Packet, Registry, Slot,
    StreamInfo, TaggedPacket,
};
use rtc::media_stream::MediaStreamTrack;
use rtc::peer_connection::RTCPeerConnectionBuilder;
use rtc::peer_connection::configuration::RTCConfigurationBuilder;
use rtc::peer_connection::configuration::media_engine::{
    MIME_TYPE_FLEX_FEC03, MIME_TYPE_VP8, MediaEngine,
};
use rtc::peer_connection::configuration::setting_engine::SettingEngineBuilder;
use rtc::peer_connection::event::RTCPeerConnectionEvent;
use rtc::peer_connection::message::{RTCMessage, TaggedRTCMessage};
use rtc::peer_connection::state::{RTCIceConnectionState, RTCPeerConnectionState};
use rtc::peer_connection::transport::RTCDtlsRole;
use rtc::peer_connection::transport::{CandidateConfig, CandidateHostConfig, RTCIceCandidate};
use rtc::rtp;
use rtc::rtp_transceiver::rtp_sender::{
    RTCRtpCodec, RTCRtpCodecParameters, RTCRtpCodingParameters, RTCRtpEncodingParameters,
    RTCRtpFecParameters, RtpCodecKind,
};
use rtc::rtp_transceiver::{RTCRtpTransceiverDirection, RTCRtpTransceiverInit, SSRC};
use rtc::sansio::Protocol;
use rtc::shared::error::Error;
use rtc::shared::{TaggedBytesMut, TransportContext, TransportProtocol};
use std::collections::{BTreeSet, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::net::UdpSocket;
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};

mod common;

const VP8_PT: u8 = 96;
const FLEX_FEC_PT: u8 = 49;

const MEDIA_SSRC: SSRC = 0x1111_1111;
const FEC_SSRC: SSRC = 0x2222_2222;

/// One repair packet per ten media packets recovers a single loss anywhere in the block.
const NUM_MEDIA_PACKETS: u32 = 10;
const NUM_FEC_PACKETS: u32 = 1;

/// Drop one media packet in twenty.
///
/// Chosen against the repair rate above, not for realism: at most one loss lands in any block of
/// ten, so every loss is recoverable and the test can demand *all* of them back. At one in five —
/// what `play-from-disk-fec` defaults to — most blocks lose two and the correct expectation would
/// be a fraction, which is a much weaker thing to assert.
const DROP_ONE_IN: u64 = 20;

/// Enough to exercise twenty FEC blocks, so a systematic failure cannot hide behind one lucky one.
const MEDIA_PACKETS_TO_SEND: u16 = 200;

/// The drop filter stands in for the network: below every built-in slot, so the packet is gone only
/// after everything on the sending side has already accounted for it.
const DROP_FILTER_SLOT: usize = 500;

/// The recorder sits immediately application-ward of `Slot::FecDecoder` (6_000) — the earliest
/// point at which [`Attribute::RecoveredByFec`] exists to be read.
const RECOVERY_RECORDER_SLOT: usize = 6_500;

const IDLE_TIMEOUT: Duration = Duration::from_secs(30);

/// Discards one media packet in `drop_one_in` and reports the sequence number it discarded.
///
/// Repair packets are exempt: dropping those would be testing something else. They are identified
/// by the FEC SSRC the stream negotiated, learned at bind.
struct DropFilter {
    fec_ssrcs: Vec<SSRC>,
    media_packets: u64,
    dropped_tx: UnboundedSender<u16>,
    read_queue: VecDeque<TaggedPacket>,
    write_queue: VecDeque<TaggedPacket>,
}

impl DropFilter {
    fn new(dropped_tx: UnboundedSender<u16>) -> Self {
        Self {
            fec_ssrcs: Vec::new(),
            media_packets: 0,
            dropped_tx,
            read_queue: VecDeque::new(),
            write_queue: VecDeque::new(),
        }
    }
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for DropFilter {
    type Rout = TaggedPacket;
    type Wout = TaggedPacket;
    type Eout = ();
    type Error = Error;
    type Time = Instant;

    fn handle_read(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        self.read_queue.push_back(msg);
        Ok(())
    }

    fn poll_read(&mut self) -> Option<Self::Rout> {
        self.read_queue.pop_front()
    }

    fn handle_write(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        let Packet::Rtp(ref rtp_packet) = msg.message.packet else {
            self.write_queue.push_back(msg);
            return Ok(());
        };

        if self.fec_ssrcs.contains(&rtp_packet.header.ssrc) {
            self.write_queue.push_back(msg);
            return Ok(());
        }

        self.media_packets += 1;
        if self.media_packets.is_multiple_of(DROP_ONE_IN) {
            let _ = self.dropped_tx.send(rtp_packet.header.sequence_number);
            // Not queued: nothing below this point ever sees it, exactly as if the path had lost it.
            return Ok(());
        }

        self.write_queue.push_back(msg);
        Ok(())
    }

    fn poll_write(&mut self) -> Option<Self::Wout> {
        self.write_queue.pop_front()
    }

    fn handle_timeout(&mut self, _now: Instant) -> Result<(), Self::Error> {
        Ok(())
    }

    fn poll_timeout(&mut self) -> Option<Self::Time> {
        None
    }
}

impl Interceptor for DropFilter {
    fn bind_local_stream(&mut self, info: &StreamInfo) {
        if let Some(ssrc_fec) = info.ssrc_fec {
            self.fec_ssrcs.push(ssrc_fec);
        }
    }
    fn unbind_local_stream(&mut self, info: &StreamInfo) {
        if let Some(ssrc_fec) = info.ssrc_fec {
            self.fec_ssrcs.retain(|ssrc| *ssrc != ssrc_fec);
        }
    }
    fn bind_remote_stream(&mut self, _info: &StreamInfo) {}
    fn unbind_remote_stream(&mut self, _info: &StreamInfo) {}
}

/// Reports every inbound media packet's sequence number, and whether the decoder rebuilt it.
struct RecoveryRecorder {
    fec_ssrcs: Vec<SSRC>,
    arrivals_tx: UnboundedSender<(u16, bool)>,
    read_queue: VecDeque<TaggedPacket>,
    write_queue: VecDeque<TaggedPacket>,
}

impl RecoveryRecorder {
    fn new(arrivals_tx: UnboundedSender<(u16, bool)>) -> Self {
        Self {
            fec_ssrcs: Vec::new(),
            arrivals_tx,
            read_queue: VecDeque::new(),
            write_queue: VecDeque::new(),
        }
    }
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for RecoveryRecorder {
    type Rout = TaggedPacket;
    type Wout = TaggedPacket;
    type Eout = ();
    type Error = Error;
    type Time = Instant;

    fn handle_read(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        if let Packet::Rtp(ref rtp_packet) = msg.message.packet
            && !self.fec_ssrcs.contains(&rtp_packet.header.ssrc)
        {
            let _ = self.arrivals_tx.send((
                rtp_packet.header.sequence_number,
                msg.message.has(&Attribute::RecoveredByFec),
            ));
        }
        self.read_queue.push_back(msg);
        Ok(())
    }

    fn poll_read(&mut self) -> Option<Self::Rout> {
        self.read_queue.pop_front()
    }

    fn handle_write(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        self.write_queue.push_back(msg);
        Ok(())
    }

    fn poll_write(&mut self) -> Option<Self::Wout> {
        self.write_queue.pop_front()
    }

    fn handle_timeout(&mut self, _now: Instant) -> Result<(), Self::Error> {
        Ok(())
    }

    fn poll_timeout(&mut self) -> Option<Self::Time> {
        None
    }
}

impl Interceptor for RecoveryRecorder {
    fn bind_remote_stream(&mut self, info: &StreamInfo) {
        if let Some(ssrc_fec) = info.ssrc_fec {
            self.fec_ssrcs.push(ssrc_fec);
        }
    }
    fn unbind_remote_stream(&mut self, info: &StreamInfo) {
        if let Some(ssrc_fec) = info.ssrc_fec {
            self.fec_ssrcs.retain(|ssrc| *ssrc != ssrc_fec);
        }
    }
    fn bind_local_stream(&mut self, _info: &StreamInfo) {}
    fn unbind_local_stream(&mut self, _info: &StreamInfo) {}
}

fn video_codec() -> RTCRtpCodecParameters {
    RTCRtpCodecParameters {
        rtp_codec: RTCRtpCodec {
            mime_type: MIME_TYPE_VP8.to_owned(),
            clock_rate: 90000,
            channels: 0,
            sdp_fmtp_line: "".to_owned(),
            rtcp_feedback: vec![],
        },
        payload_type: VP8_PT,
    }
}

fn fec_codec() -> RTCRtpCodecParameters {
    RTCRtpCodecParameters {
        rtp_codec: RTCRtpCodec {
            mime_type: MIME_TYPE_FLEX_FEC03.to_owned(),
            clock_rate: 90000,
            channels: 0,
            sdp_fmtp_line: "repair-window=10000000".to_owned(),
            rtcp_feedback: vec![],
        },
        payload_type: FLEX_FEC_PT,
    }
}

fn media_engine() -> Result<MediaEngine> {
    let mut media_engine = MediaEngine::default();
    media_engine.register_codec(video_codec(), RtpCodecKind::Video)?;
    media_engine.register_codec(fec_codec(), RtpCodecKind::Video)?;
    Ok(media_engine)
}

#[tokio::test]
async fn flexfec03_recovers_every_dropped_packet() -> Result<()> {
    common::install_crypto_provider();
    env_logger::builder()
        .filter_level(log::LevelFilter::Info)
        .is_test(true)
        .try_init()
        .ok();

    let (dropped_tx, mut dropped_rx) = unbounded_channel::<u16>();
    let (arrivals_tx, mut arrivals_rx) = unbounded_channel::<(u16, bool)>();

    // ---- receiver (answerer) ----
    let answerer_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await?);
    let answerer_local_addr = answerer_socket.local_addr()?;

    let mut answerer_media_engine = media_engine()?;
    let answerer_registry = Registry::new()
        .with(Slot::FecDecoder, FlexFec03ReceiveBuilder::new().build())
        .with(
            Slot::from(RECOVERY_RECORDER_SLOT),
            RecoveryRecorder::new(arrivals_tx),
        );

    let mut answerer_pc = RTCPeerConnectionBuilder::new()
        .with_configuration(RTCConfigurationBuilder::new().build())
        .with_setting_engine(
            SettingEngineBuilder::new()
                .with_answering_dtls_role(RTCDtlsRole::Server)
                .build(),
        )
        .with_media_engine(std::mem::take(&mut answerer_media_engine))
        .with_interceptor_registry(answerer_registry)
        .build(Instant::now())?;

    answerer_pc.add_transceiver_from_kind(
        RtpCodecKind::Video,
        Some(RTCRtpTransceiverInit {
            direction: RTCRtpTransceiverDirection::Recvonly,
            ..Default::default()
        }),
    )?;
    answerer_pc.add_local_candidate(
        RTCIceCandidate::from(&host_candidate(&answerer_local_addr)?).to_json()?,
    )?;

    // ---- sender (offerer) ----
    let offerer_socket = Arc::new(UdpSocket::bind("127.0.0.1:0").await?);
    let offerer_local_addr = offerer_socket.local_addr()?;

    let mut offerer_media_engine = media_engine()?;
    let offerer_registry = Registry::new()
        .with(
            Slot::FecEncoder,
            FlexFec03SendBuilder::new()
                .with_num_media_packets(NUM_MEDIA_PACKETS)
                .with_num_fec_packets(NUM_FEC_PACKETS)
                .build(),
        )
        .with(Slot::from(DROP_FILTER_SLOT), DropFilter::new(dropped_tx));

    let mut offerer_pc = RTCPeerConnectionBuilder::new()
        .with_configuration(RTCConfigurationBuilder::new().build())
        .with_setting_engine(
            SettingEngineBuilder::new()
                .with_answering_dtls_role(RTCDtlsRole::Server)
                .build(),
        )
        .with_media_engine(std::mem::take(&mut offerer_media_engine))
        .with_interceptor_registry(offerer_registry)
        .build(Instant::now())?;

    let sender_id = offerer_pc.add_track(MediaStreamTrack::new(
        "stream".to_owned(),
        "video".to_owned(),
        "video".to_owned(),
        RtpCodecKind::Video,
        vec![RTCRtpEncodingParameters {
            rtp_coding_parameters: RTCRtpCodingParameters {
                ssrc: Some(MEDIA_SSRC),
                fec: Some(RTCRtpFecParameters { ssrc: FEC_SSRC }),
                ..Default::default()
            },
            codec: video_codec().rtp_codec,
            ..Default::default()
        }],
    ))?;
    offerer_pc.add_local_candidate(
        RTCIceCandidate::from(&host_candidate(&offerer_local_addr)?).to_json()?,
    )?;

    // ---- negotiate ----
    let offer = offerer_pc.create_offer(None)?;
    assert!(
        offer
            .sdp
            .contains(&format!("a=rtpmap:{FLEX_FEC_PT} flexfec-03/90000")),
        "the offer must carry the repair codec, or nothing below tests FEC:\n{}",
        offer.sdp
    );
    assert!(
        offer
            .sdp
            .contains(&format!("a=ssrc-group:FEC-FR {MEDIA_SSRC} {FEC_SSRC}")),
        "the offer must group the repair flow with the media it repairs:\n{}",
        offer.sdp
    );

    offerer_pc.set_local_description(Instant::now(), offer.clone())?;
    answerer_pc.set_remote_description(Instant::now(), offer)?;
    let answer = answerer_pc.create_answer(None)?;
    assert!(
        answer
            .sdp
            .contains(&format!("a=rtpmap:{FLEX_FEC_PT} flexfec-03/90000")),
        "the answerer must select the repair codec, or its decoder never binds:\n{}",
        answer.sdp
    );
    answerer_pc.set_local_description(Instant::now(), answer.clone())?;
    offerer_pc.set_remote_description(Instant::now(), answer)?;

    // ---- run ----
    let mut offerer_buf = vec![0u8; 2000];
    let mut answerer_buf = vec![0u8; 2000];
    let mut offerer_connected = false;
    let mut answerer_connected = false;
    let mut packets_sent: u16 = 0;
    let mut last_send = Instant::now();
    let mut finished_sending_at: Option<Instant> = None;

    let payload = bytes::Bytes::from(vec![0xAB; 200]);
    let start = Instant::now();
    let deadline = Duration::from_secs(30);

    while start.elapsed() < deadline {
        while let Some(msg) = offerer_pc.poll_write() {
            offerer_socket
                .send_to(&msg.message, msg.transport.peer_addr)
                .await?;
        }
        while let Some(msg) = answerer_pc.poll_write() {
            answerer_socket
                .send_to(&msg.message, msg.transport.peer_addr)
                .await?;
        }

        while let Some(event) = offerer_pc.poll_event() {
            match event {
                RTCPeerConnectionEvent::OnIceConnectionStateChangeEvent(
                    RTCIceConnectionState::Failed,
                ) => return Err(anyhow::anyhow!("offerer ICE failed")),
                RTCPeerConnectionEvent::OnConnectionStateChangeEvent(state) => {
                    if state == RTCPeerConnectionState::Failed {
                        return Err(anyhow::anyhow!("offerer peer connection failed"));
                    }
                    offerer_connected |= state == RTCPeerConnectionState::Connected;
                }
                _ => {}
            }
        }
        while let Some(event) = answerer_pc.poll_event() {
            match event {
                RTCPeerConnectionEvent::OnIceConnectionStateChangeEvent(
                    RTCIceConnectionState::Failed,
                ) => return Err(anyhow::anyhow!("answerer ICE failed")),
                RTCPeerConnectionEvent::OnConnectionStateChangeEvent(state) => {
                    if state == RTCPeerConnectionState::Failed {
                        return Err(anyhow::anyhow!("answerer peer connection failed"));
                    }
                    answerer_connected |= state == RTCPeerConnectionState::Connected;
                }
                _ => {}
            }
        }

        // The application-facing stream is drained but not asserted on: `RTCMessage::RtpPacket`
        // carries no attributes, which is exactly why `RecoveryRecorder` exists.
        while let Some(TaggedRTCMessage { message, .. }) = answerer_pc.poll_read() {
            if let RTCMessage::RtpPacket(_, _) = message {}
        }

        if offerer_connected
            && answerer_connected
            && packets_sent < MEDIA_PACKETS_TO_SEND
            && last_send.elapsed() >= Duration::from_millis(2)
        {
            packets_sent += 1;
            last_send = Instant::now();

            let mut rtp_sender = offerer_pc
                .rtp_sender(sender_id)
                .ok_or(Error::ErrRTPSenderNotExisted)?;
            rtp_sender.write_rtp(
                Instant::now(),
                rtp::packet::Packet {
                    header: rtp::header::Header {
                        version: 2,
                        payload_type: VP8_PT,
                        // The identity under test. One per packet, from 1, so a reported
                        // sequence number names exactly one send.
                        sequence_number: packets_sent,
                        timestamp: u32::from(packets_sent) * 3000,
                        ssrc: MEDIA_SSRC,
                        ..Default::default()
                    },
                    payload: payload.clone(),
                },
            )?;

            if packets_sent == MEDIA_PACKETS_TO_SEND {
                finished_sending_at = Some(Instant::now());
            }
        }

        // Give the tail of the stream time to land: the last block's repair packet is emitted
        // after its tenth media packet, so recovery of a loss in that block necessarily trails
        // the last send.
        if finished_sending_at.is_some_and(|at| at.elapsed() > Duration::from_secs(2)) {
            break;
        }

        let next = offerer_pc
            .poll_timeout()
            .unwrap_or(Instant::now() + IDLE_TIMEOUT)
            .min(
                answerer_pc
                    .poll_timeout()
                    .unwrap_or(Instant::now() + IDLE_TIMEOUT),
            );
        let delay = next
            .checked_duration_since(Instant::now())
            .unwrap_or(Duration::ZERO);
        if delay.is_zero() {
            offerer_pc.handle_timeout(Instant::now())?;
            answerer_pc.handle_timeout(Instant::now())?;
            continue;
        }

        let timer = tokio::time::sleep(delay.min(Duration::from_millis(2)));
        tokio::pin!(timer);

        tokio::select! {
            _ = timer.as_mut() => {
                offerer_pc.handle_timeout(Instant::now())?;
                answerer_pc.handle_timeout(Instant::now())?;
            }
            res = offerer_socket.recv_from(&mut offerer_buf) => {
                let (n, peer_addr) = res?;
                offerer_pc.handle_read(TaggedBytesMut {
                    now: Instant::now(),
                    transport: TransportContext {
                        local_addr: offerer_local_addr,
                        peer_addr,
                        ecn: None,
                        transport_protocol: TransportProtocol::UDP,
                    },
                    message: BytesMut::from(&offerer_buf[..n]),
                })?;
            }
            res = answerer_socket.recv_from(&mut answerer_buf) => {
                let (n, peer_addr) = res?;
                answerer_pc.handle_read(TaggedBytesMut {
                    now: Instant::now(),
                    transport: TransportContext {
                        local_addr: answerer_local_addr,
                        peer_addr,
                        ecn: None,
                        transport_protocol: TransportProtocol::UDP,
                    },
                    message: BytesMut::from(&answerer_buf[..n]),
                })?;
            }
        }
    }

    offerer_pc.close()?;
    answerer_pc.close()?;

    // ---- collect ----
    let mut dropped = BTreeSet::new();
    while let Ok(sequence_number) = dropped_rx.try_recv() {
        dropped.insert(sequence_number);
    }

    let mut arrived = BTreeSet::new();
    let mut recovered = BTreeSet::new();
    while let Ok((sequence_number, was_recovered)) = arrivals_rx.try_recv() {
        if was_recovered {
            recovered.insert(sequence_number);
        } else {
            arrived.insert(sequence_number);
        }
    }

    log::info!(
        "sent {packets_sent}, dropped {}, arrived {}, recovered {}",
        dropped.len(),
        arrived.len(),
        recovered.len()
    );

    assert_eq!(
        MEDIA_PACKETS_TO_SEND, packets_sent,
        "the connection did not stay up long enough to send the whole stream"
    );

    // Guards the rest: with nothing dropped, every assertion below holds vacuously and the test
    // would pass on a build where FEC does nothing whatsoever.
    let expected_drops = usize::from(MEDIA_PACKETS_TO_SEND) / DROP_ONE_IN as usize;
    assert_eq!(
        expected_drops,
        dropped.len(),
        "the drop filter should have discarded one packet in {DROP_ONE_IN}"
    );

    // And nothing else went astray, so the recovery above is the whole story rather than one
    // effect among several.
    let all: BTreeSet<u16> = (1..=MEDIA_PACKETS_TO_SEND).collect();
    let delivered: BTreeSet<u16> = arrived.union(&recovered).copied().collect();
    assert_eq!(
        all,
        delivered,
        "packets neither delivered nor recovered: {:?}",
        all.difference(&delivered).collect::<Vec<_>>()
    );

    // Set equality in both directions: every dropped packet came back rebuilt, and nothing else
    // was rebuilt.
    //
    // The second half is the one with history. Until stream establishment moved into the
    // interceptor handler, sequence number 1 was always rebuilt as well — a duplicate of a packet
    // that had arrived perfectly well — because the packet that resolved the stream's codec had
    // already traversed the chain by the time the decoder was bound to it. See
    // `handler/stream_establishment.rs`.
    assert_eq!(
        dropped,
        recovered,
        "every dropped sequence number must be recovered, and only those:\n\
         dropped:   {dropped:?}\n\
         recovered: {recovered:?}\n\
         never recovered: {:?}\n\
         rebuilt but never lost: {:?}",
        dropped.difference(&recovered).collect::<Vec<_>>(),
        recovered.difference(&dropped).collect::<Vec<_>>()
    );

    // A recovered packet must not also have arrived: that would mean the decoder rebuilt something
    // it did not need to, and the sets above would agree for the wrong reason.
    assert!(
        arrived.is_disjoint(&recovered),
        "a packet was both received and recovered: {:?}",
        arrived.intersection(&recovered).collect::<Vec<_>>()
    );

    Ok(())
}

fn host_candidate(addr: &std::net::SocketAddr) -> Result<rtc::ice::candidate::Candidate> {
    Ok(CandidateHostConfig {
        base_config: CandidateConfig {
            network: "udp".to_owned(),
            address: addr.ip().to_string(),
            port: addr.port(),
            component: 1,
            ..Default::default()
        },
        ..Default::default()
    }
    .new_candidate_host()?)
}