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
//! Sender Report Interceptor - Filters hop-by-hop RTCP feedback.

use super::sender_stream::SenderStream;
use crate::Interceptor;
use crate::stream_info::StreamInfo;
use crate::{AttributedPacket, Packet, TaggedPacket};
use rtcp::header::PacketType;
use sansio::Protocol;
use shared::TransportContext;
use shared::error::Error;
use shared::time::SystemInstant;
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Builder for the SenderReportInterceptor.
///
/// # Example
///
/// ```
/// use rtci_interceptor::{Slot, Registry, SenderReportBuilder};
/// use std::time::Duration;
///
/// // With default interval (1 second)
/// let chain = Registry::new()
///     .with(Slot::SenderReport, SenderReportBuilder::new().build())
///     .build();
///
/// // With custom interval
/// let chain = Registry::new()
///     .with(Slot::SenderReport, SenderReportBuilder::new().with_interval(Duration::from_millis(500)).build())
///     .build();
///
/// // With use_latest_packet enabled
/// let chain = Registry::new()
///     .with(Slot::SenderReport, SenderReportBuilder::new().with_use_latest_packet().build())
///     .build();
/// ```
pub struct SenderReportBuilder {
    /// Interval between sender reports.
    interval: Duration,
    /// Whether to always use the latest packet, even if out-of-order.
    use_latest_packet: bool,
    /// The wall-clock anchor NTP times derive from (`None`: the process-wide one).
    wall_clock: Option<SystemInstant>,
}

impl Default for SenderReportBuilder {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(1),
            use_latest_packet: false,
            wall_clock: None,
        }
    }
}

impl SenderReportBuilder {
    /// Create a new builder with default settings.
    ///
    /// Default interval is 1 second.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a custom interval between sender reports.
    ///
    /// # Example
    ///
    /// ```
    /// use rtci_interceptor::{Registry, SenderReportBuilder, Slot};
    /// use std::time::Duration;
    ///
    /// let registry = Registry::new().with(
    ///     Slot::SenderReport,
    ///     SenderReportBuilder::new()
    ///         .with_interval(Duration::from_millis(500))
    ///         .build(),
    /// );
    /// ```
    pub fn with_interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Enable always using the latest packet for timestamp tracking,
    /// even if it appears to be out-of-order based on sequence numbers.
    ///
    /// By default (disabled), only in-order packets update the RTP↔NTP
    /// timestamp correlation. This prevents out-of-order packets from
    /// corrupting the timestamp mapping.
    ///
    /// Enable this option when:
    /// - Packets are guaranteed to arrive in order
    /// - The application reorders packets before the interceptor
    /// - You want the sender report to always reflect the most recent packet
    ///
    /// # Example
    ///
    /// ```
    /// use rtci_interceptor::{Slot, Registry, SenderReportBuilder};
    ///
    /// let registry =
    ///     Registry::new().with(Slot::SenderReport, SenderReportBuilder::new().with_use_latest_packet().build());
    /// ```
    pub fn with_use_latest_packet(mut self) -> Self {
        self.use_latest_packet = true;
        self
    }

    /// The wall-clock anchor the sender reports' NTP times derive from. Default: the
    /// process-wide [`SystemInstant::shared`]. Give one per peer connection for reproducible
    /// virtual-time runs: two runs in one process otherwise map the same instants to NTP times
    /// as far apart as the real time between them.
    pub fn with_wall_clock(mut self, anchor: SystemInstant) -> Self {
        self.wall_clock = Some(anchor);
        self
    }

    /// Create a builder function for use with Registry.
    ///
    /// This returns a closure that can be passed to `Registry::with()`.
    ///
    /// # Example
    ///
    /// ```
    /// use rtci_interceptor::{Slot, Registry, SenderReportBuilder};
    ///
    /// let registry = Registry::new()
    ///     .with(Slot::SenderReport, SenderReportBuilder::new().build());
    /// ```
    pub fn build(self) -> SenderReportInterceptor {
        let mut interceptor = SenderReportInterceptor::new(self.interval, self.use_latest_packet);
        interceptor.wall_clock = self.wall_clock;
        interceptor
    }
}

/// The most receiver reference times held for answering at once.
const MAX_REFERENCE_TIMES: usize = 64;

/// A stream unbound since the chain was last polled.
struct Departing {
    ssrc: u32,
    stream: SenderStream,
    /// Its final sender report and BYE, if it ever sent.
    goodbye: Option<TaggedPacket>,
}

/// Interceptor that filters hop-by-hop RTCP reports.
///
/// This interceptor filters out RTCP Receiver Reports and Transport-Specific
/// Feedback, which are hop-by-hop reports that should not be forwarded
/// end-to-end.
///
/// # Type Parameters
///
/// - `P`: The inner protocol being wrapped
///
/// # Example
///
/// ```
/// use rtci_interceptor::{Slot, Registry, SenderReportBuilder};
///
/// let chain = Registry::new()
///     .with(Slot::SenderReport, SenderReportBuilder::new().build())
///     .build();
/// ```
pub struct SenderReportInterceptor {
    interval: Duration,
    next_timeout: Option<Instant>,

    /// Whether to always use the latest packet, even if out-of-order.
    use_latest_packet: bool,

    /// [`SenderReportBuilder::with_wall_clock`].
    wall_clock: Option<SystemInstant>,

    streams: HashMap<u32, SenderStream>,

    /// The latest instant handed in, for stamping the goodbye of a stream unbound without one.
    last_now: Option<Instant>,

    /// The receiver reference times (RFC 3611 section 4.4) received since the last sender
    /// reports went out, by the SSRC they came from: the compact NTP timestamp and when it
    /// arrived. Answered with a DLRR block (section 4.5) in the next sender report's compound
    /// packet, as libwebrtc does whether or not anything was negotiated.
    reference_times: HashMap<u32, (u32, Instant)>,

    /// Streams unbound since the chain was last polled, with the goodbye each says. A stream
    /// bound again with the same SSRC before then (a renegotiation re-binding it with a new
    /// codec) takes its state back and says no goodbye: it goes on.
    departing: Vec<Departing>,

    read_queue: VecDeque<TaggedPacket>,
    write_queue: VecDeque<TaggedPacket>,
}

impl SenderReportInterceptor {
    /// Create a new SenderReportInterceptor.
    fn new(interval: Duration, use_latest_packet: bool) -> Self {
        Self {
            interval,
            next_timeout: None,

            use_latest_packet,

            wall_clock: None,

            streams: HashMap::new(),

            last_now: None,

            departing: Vec::new(),

            reference_times: HashMap::new(),

            read_queue: VecDeque::new(),
            write_queue: VecDeque::new(),
        }
    }

    /// Queues the goodbye of every stream that was unbound and not bound again.
    fn flush_departing(&mut self) {
        for departing in self.departing.drain(..) {
            if let Some(goodbye) = departing.goodbye {
                self.write_queue.push_back(goodbye);
            }
        }
    }

    /// Check if an RTCP packet type should be filtered.
    ///
    /// Returns `true` for hop-by-hop report types that should not be forwarded:
    /// - Receiver Report (201)
    /// - Transport-Specific Feedback (205)
    fn should_filter(packet_type: PacketType) -> bool {
        packet_type == PacketType::ReceiverReport
            || (packet_type == PacketType::TransportSpecificFeedback)
    }
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for SenderReportInterceptor {
    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::Rtcp(rtcp_packets) = &msg.message.packet {
            for packet in rtcp_packets {
                let Some(xr) = packet
                    .as_any()
                    .downcast_ref::<rtcp::extended_report::ExtendedReport>()
                else {
                    continue;
                };
                for block in &xr.reports {
                    if let Some(rrtr) = block
                        .as_any()
                        .downcast_ref::<rtcp::extended_report::ReceiverReferenceTimeReportBlock>(
                    ) {
                        // A remote that sends from many SSRCs is held to a bounded table.
                        if self.reference_times.len() < MAX_REFERENCE_TIMES
                            || self.reference_times.contains_key(&xr.sender_ssrc)
                        {
                            self.reference_times.insert(
                                xr.sender_ssrc,
                                ((rrtr.ntp_timestamp >> 16) as u32, msg.now),
                            );
                        }
                    }
                }
            }
        }
        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.flush_departing();
        self.last_now = Some(self.last_now.map_or(msg.now, |last| last.max(msg.now)));
        if let Packet::Rtp(rtp_packet) = &msg.message.packet
            && let Some(stream) = self.streams.get_mut(&rtp_packet.header.ssrc)
        {
            stream.process_rtp(msg.now, rtp_packet);

            // Arm the report timer from the first packet's instant (see nack::generator).
            if self.next_timeout.is_none() {
                self.next_timeout = Some(msg.now + self.interval);
            }
        }

        self.write_queue.push_back(msg);

        Ok(())
    }

    fn poll_write(&mut self) -> Option<TaggedPacket> {
        self.flush_departing();
        // First drain generated RTCP reports
        if let Some(pkt) = self.write_queue.pop_front() {
            return Some(pkt);
        }
        None
    }

    fn handle_timeout(&mut self, now: Instant) -> Result<(), Error> {
        self.flush_departing();
        self.last_now = Some(self.last_now.map_or(now, |last| last.max(now)));
        if let Some(next_timeout) = self.next_timeout
            && now >= next_timeout
        {
            self.next_timeout = Some(now + self.interval);

            for stream in self.streams.values_mut() {
                // RFC 3550 6.4: only a stream that sent during the last two reporting intervals
                // issues a sender report; a silent one reports nothing from here.
                if let Some(sr) = stream.generate_regular_report(now) {
                    let mut compound: Vec<Box<dyn rtcp::Packet>> = vec![Box::new(sr)];
                    // The DLRR block answers every reference time received since the last
                    // reports, once, from the first stream that reports (RFC 3611 section 4.5).
                    if !self.reference_times.is_empty() {
                        let reports = self
                            .reference_times
                            .drain()
                            .map(
                                |(ssrc, (last_rr, arrived))| rtcp::extended_report::DLRRReport {
                                    ssrc,
                                    last_rr,
                                    dlrr: (now.saturating_duration_since(arrived).as_secs_f64()
                                        * 65536.0) as u32,
                                },
                            )
                            .collect();
                        compound.push(Box::new(rtcp::extended_report::ExtendedReport {
                            sender_ssrc: stream.ssrc(),
                            reports: vec![Box::new(rtcp::extended_report::DLRRReportBlock {
                                reports,
                            })],
                        }));
                    }
                    self.write_queue.push_back(TaggedPacket {
                        now,
                        transport: TransportContext::default(),
                        message: AttributedPacket::new(Packet::Rtcp(compound)),
                    });
                }
            }
        }
        Ok(())
    }

    fn poll_timeout(&mut self) -> Option<Instant> {
        self.next_timeout
    }
}

impl Interceptor for SenderReportInterceptor {
    /// A stream unbound a moment ago (since the chain was last polled) and bound again with the
    /// same SSRC is the same stream re-bound by a renegotiation: it keeps its packet and octet
    /// counts and RTP / NTP timing, takes the new clock rate, and its goodbye is withdrawn.
    fn bind_local_stream(&mut self, info: &StreamInfo) {
        let stream = match self.departing.iter().position(|d| d.ssrc == info.ssrc) {
            Some(i) => {
                let mut stream = self.departing.remove(i).stream;
                stream.set_clock_rate(info.clock_rate);
                stream
            }
            None => SenderStream::new(info.ssrc, info.clock_rate, self.use_latest_packet)
                .with_wall_clock(self.wall_clock),
        };
        self.streams.insert(info.ssrc, stream);
    }

    /// A stream that sent and is now unbound (its sender stopped) says goodbye: a final sender
    /// report and an RTCP BYE for its SSRC and its repair flows, in one compound packet
    /// (RFC 3550 6.6: a BYE follows a report in a compound packet). The goodbye goes out when
    /// the chain is next polled, unless the stream is bound again first.
    fn unbind_local_stream(&mut self, info: &StreamInfo) {
        let Some(mut stream) = self.streams.remove(&info.ssrc) else {
            return;
        };
        let goodbye = match self.last_now {
            Some(now) if stream.has_sent() => {
                // RFC 3550 6.4: a sender report only from a stream that sent lately; one that
                // fell silent leads its goodbye with an empty receiver report instead.
                let report: Option<Box<dyn rtcp::Packet>> = if stream.is_sender() {
                    stream
                        .generate_report(now)
                        .map(|report| Box::new(report) as Box<dyn rtcp::Packet>)
                } else {
                    Some(Box::new(rtcp::receiver_report::ReceiverReport {
                        ssrc: info.ssrc,
                        ..Default::default()
                    }))
                };
                report.map(|report| {
                    let sources = std::iter::once(info.ssrc)
                        .chain(info.ssrc_rtx)
                        .chain(info.ssrc_fec)
                        .collect();
                    TaggedPacket {
                        now,
                        transport: TransportContext::default(),
                        message: AttributedPacket::new(Packet::Rtcp(vec![
                            report,
                            Box::new(rtcp::goodbye::Goodbye {
                                sources,
                                reason: Default::default(),
                            }),
                        ])),
                    }
                })
            }
            _ => None,
        };
        self.departing.retain(|d| d.ssrc != info.ssrc);
        self.departing.push(Departing {
            ssrc: info.ssrc,
            stream,
            goodbye,
        });
    }

    fn bind_remote_stream(&mut self, _info: &StreamInfo) {}

    fn unbind_remote_stream(&mut self, _info: &StreamInfo) {}
}