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
//! Emits RFC 8888 congestion control feedback for the streams being received.

use super::recorder::CcFeedbackRecorder;
use crate::Interceptor;
use crate::cc::feedback_cadence::FeedbackCadence;
use crate::stream_info::StreamInfo;
use crate::twcc::flow_ssrcs;
use crate::{Attribute, AttributedPacket, Packet, TaggedPacket};
use rtcp::transport_feedbacks::cc_feedback_report::Ecn;
use sansio::Protocol;
use shared::error::Error;
use shared::marshal::MarshalSize;
use shared::time::SystemInstant;
use shared::{EcnCodepoint, TransportContext};
use std::collections::HashSet;
use std::collections::VecDeque;
use std::time::{Duration, Instant};

/// How often feedback is sent when no interval is configured.
pub const DEFAULT_INTERVAL: Duration = Duration::from_millis(100);

/// Byte budget for one report, chosen to sit inside a conservative path MTU.
pub const DEFAULT_MAX_REPORT_SIZE: usize = 1200;

/// The RFC 8888 marking for the codepoint a datagram arrived with (`None` is Not-ECT).
fn ecn_of(codepoint: Option<EcnCodepoint>) -> Ecn {
    match codepoint {
        None => Ecn::NotEct,
        Some(EcnCodepoint::Ect0) => Ecn::Ect0,
        Some(EcnCodepoint::Ect1) => Ecn::Ect1,
        Some(EcnCodepoint::Ce) => Ecn::Ce,
    }
}

/// Builder for [`Rfc8888Interceptor`].
///
/// # Example
///
/// ```
/// use rtci_interceptor::{Slot, Registry, Rfc8888Builder};
/// use std::time::Duration;
///
/// let chain = Registry::new()
///     .with(Slot::Rfc8888, Rfc8888Builder::new().with_interval(Duration::from_millis(50)).build())
///     .build();
/// ```
pub struct Rfc8888Builder {
    interval: Duration,
    adaptive_interval: bool,
    feedback_share: f64,
    max_adaptive_interval: Duration,
    max_report_size: usize,
    sender_ssrc: Option<u32>,
    /// The wall-clock anchor report timestamps derive from (`None`: the process-wide one).
    wall_clock: Option<SystemInstant>,
}

impl Default for Rfc8888Builder {
    fn default() -> Self {
        Self {
            interval: DEFAULT_INTERVAL,
            adaptive_interval: true,
            feedback_share: crate::cc::feedback_cadence::FEEDBACK_SHARE,
            max_adaptive_interval: crate::cc::feedback_cadence::MAX_ADAPTIVE_INTERVAL,
            max_report_size: DEFAULT_MAX_REPORT_SIZE,
            sender_ssrc: None,
            wall_clock: None,
        }
    }
}

/// Whether a stream negotiated RFC 8888 feedback: `a=rtcp-fb:<pt> ack ccfb` (RFC 8888
/// section 6).
pub(crate) fn stream_supports_ccfb(info: &StreamInfo) -> bool {
    info.rtcp_feedback
        .iter()
        .any(|feedback| feedback.typ == "ack" && feedback.parameter == "ccfb")
}

/// An SSRC for a participant that sends no media: random, and never 0 (RFC 3550 section 8).
pub(crate) fn random_ssrc() -> u32 {
    loop {
        let ssrc: u32 = rand::random();
        if ssrc != 0 {
            return ssrc;
        }
    }
}

impl Rfc8888Builder {
    /// Create a builder with the default interval and report size.
    pub fn new() -> Self {
        Self::default()
    }

    /// How often feedback is sent.
    ///
    /// Congestion control wants this often — the estimate is only as fresh as the feedback
    /// driving it — traded against the RTCP bandwidth it costs.
    pub fn with_interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Whether the interval stretches to what the RTCP bandwidth holds one report in. On by
    /// default.
    ///
    /// "RTCP bandwidth and transmission rules put some upper limits on how frequently the
    /// RTCP feedback messages can be sent" (RFC 8888 section 4), and the RTCP session applies
    /// them: 5% of the session bandwidth unless `b=RS` / `b=RR` say otherwise (RFC 3550
    /// section 6.2). Reports built more often than that queue in the session and reach the
    /// sender late or not at all. With this on, the interval is the configured one where the
    /// bandwidth allows it and longer where it does not, up to half a second: one report for
    /// every RTCP packet the session may send, each covering everything since the last.
    /// `false` keeps the interval fixed.
    pub fn with_adaptive_interval(mut self, adaptive_interval: bool) -> Self {
        self.adaptive_interval = adaptive_interval;
        self
    }

    /// The share of the media received that the feedback may take while the interval is
    /// adaptive: the interval is the time in which this share holds one feedback packet (with
    /// its lower layers, and the minimal compound packet around it without reduced-size
    /// RTCP). Default 0.05, RFC 3550 section 6.2's 5% for all of RTCP. A smaller share leaves
    /// room in the 5% for the regular reports (the `Performance` behaviour profile: 0.025).
    pub fn with_feedback_share(mut self, share: f64) -> Self {
        self.feedback_share = share;
        self
    }

    /// The longest the adaptive interval stretches to while media flows. Default 250 ms.
    /// draft-holmer-rmcat-transport-wide-cc-extensions-01 section 3: "in cases of low uplink
    /// bandwidth it is acceptable to send them less frequently, e.g., for instance once per
    /// RTT"; RFC 8888 section 4 leaves the interval to the application ("Applications need to
    /// negotiate an appropriate congestion control feedback interval ... based on ... the
    /// acceptable feedback overhead"). The `Performance` behaviour profile takes 500 ms.
    pub fn with_max_adaptive_interval(mut self, max_interval: Duration) -> Self {
        self.max_adaptive_interval = max_interval;
        self
    }

    /// Byte budget for one report.
    ///
    /// A report larger than the path MTU is fragmented or dropped, and feedback that does not
    /// arrive is worth nothing, so feedback that exceeds the budget is split into several
    /// reports (RFC 8888 section 3.1).
    pub fn with_max_report_size(mut self, max_report_size: usize) -> Self {
        self.max_report_size = max_report_size;
        self
    }

    /// The SSRC these reports are sent from.
    ///
    /// Left unset, the interceptor uses the SSRC of the first local stream bound to it (the
    /// RTCP packet sender is then the endpoint's own media source), and while there is none, a
    /// random non-zero SSRC chosen when it is built, as a receive-only RTP participant has
    /// (RFC 3550 section 8). It is never 0.
    pub fn with_sender_ssrc(mut self, sender_ssrc: u32) -> Self {
        self.sender_ssrc = Some(sender_ssrc);
        self
    }

    /// The wall-clock anchor the reports' timestamps derive from (RFC 8888 section 3.1: the
    /// clock of the RTCP sender reports' NTP timestamps, so give the RTCP session the same).
    /// 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
    }

    /// Build the interceptor.
    pub fn build(self) -> Rfc8888Interceptor {
        let mut interceptor = Rfc8888Interceptor::new(
            self.interval,
            self.adaptive_interval,
            self.max_report_size,
            self.sender_ssrc,
        );
        interceptor.cadence = FeedbackCadence::new(self.interval, self.adaptive_interval)
            .with_limits(self.feedback_share, self.max_adaptive_interval);
        interceptor.epoch = self.wall_clock;
        interceptor
    }
}

/// Reports when each packet of each bound remote stream arrived ([RFC 8888]).
///
/// # Differences from upstream
///
/// `pion/interceptor` injects a `SenderTicker` and a `SenderNow` so its tests can control time.
/// Sans-I/O needs neither: every instant arrives as a parameter, so the tests drive the clock by
/// passing one. Both are dropped.
///
/// Upstream's `Recorder` also never sets its sender SSRC, so every report it builds claims to come
/// from SSRC 0. Here it is configured.
///
/// [RFC 8888]: https://www.rfc-editor.org/rfc/rfc8888
pub struct Rfc8888Interceptor {
    /// When a report is built: the configured interval, stretched to the RTCP bandwidth.
    cadence: FeedbackCadence,
    max_report_size: usize,
    /// The SSRC set on the builder, if one was.
    configured_ssrc: Option<u32>,
    /// The SSRC reports are sent from when none was configured and no local stream is bound.
    receiver_ssrc: u32,
    /// Local streams in the order they were bound: the first is the RTCP packet sender.
    local_ssrcs: Vec<u32>,
    recorder: CcFeedbackRecorder,
    /// Remote streams being reported on.
    streams: HashSet<u32>,
    next_timeout: Option<Instant>,
    /// Wall-clock reference, captured from the first instant handed over, so a monotonic `Instant`
    /// can be turned into the NTP timestamp a report carries.
    epoch: Option<SystemInstant>,
    write_queue: VecDeque<TaggedPacket>,
    /// Inbound packets ready for the next interceptor.
    read_queue: VecDeque<TaggedPacket>,
}

impl Rfc8888Interceptor {
    fn new(
        interval: Duration,
        adaptive_interval: bool,
        max_report_size: usize,
        sender_ssrc: Option<u32>,
    ) -> Self {
        Self {
            read_queue: VecDeque::new(),
            cadence: FeedbackCadence::new(interval, adaptive_interval),
            max_report_size,
            configured_ssrc: sender_ssrc,
            receiver_ssrc: random_ssrc(),
            local_ssrcs: Vec::new(),
            recorder: CcFeedbackRecorder::new(),
            streams: HashSet::new(),
            next_timeout: None,
            epoch: None,
            write_queue: VecDeque::new(),
        }
    }

    /// The SSRC the next report is sent from (RFC 8888 section 3.1: the SSRC of the RTCP packet
    /// sender).
    pub fn sender_ssrc(&self) -> u32 {
        self.configured_ssrc
            .or_else(|| self.local_ssrcs.first().copied())
            .unwrap_or(self.receiver_ssrc)
    }

    /// The middle 32 bits of the NTP timestamp for `now`, which is what a report carries.
    fn report_timestamp(&mut self, now: Instant) -> u32 {
        let epoch = self.epoch.get_or_insert_with(|| SystemInstant::shared(now));
        (epoch.ntp(now) >> 16) as u32
    }

    /// Arm the interval from the first instant this interceptor is given.
    fn arm(&mut self, now: Instant) {
        if self.next_timeout.is_none() && !self.streams.is_empty() && !self.cadence.is_off() {
            self.next_timeout = Some(now + self.cadence.interval(now));
        }
    }
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for Rfc8888Interceptor {
    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.cadence.on_packet(&msg);
        if let Packet::Rtp(rtp_packet) = &msg.message.packet {
            // An RTX packet reaches the chain rewritten into the original it carries; what
            // arrived on the wire was a packet of the retransmission stream, and that is what
            // the report describes (RFC 8888 section 3.1: a block "for each RTP stream", and
            // section 6: feedback for every payload type in use, retransmission included). Its
            // SSRC and sequence number are the ones the sender's history knows the RTX packet
            // by; reporting it as the original would tell the sender a lost packet arrived.
            let (ssrc, sequence_number) = msg
                .message
                .attributes
                .iter()
                .find_map(|attribute| match attribute {
                    Attribute::Retransmitted {
                        ssrc,
                        sequence_number,
                    } => Some((*ssrc, *sequence_number)),
                    _ => None,
                })
                .unwrap_or((rtp_packet.header.ssrc, rtp_packet.header.sequence_number));
            if self.streams.contains(&ssrc) {
                // ECN lives in the IP header, which a sans-I/O interceptor never sees: the
                // application's socket layer reads it and hands it over in the transport
                // context. A datagram without one is reported Not-ECT, which is what an
                // endpoint without ECN support reports anyway.
                self.recorder
                    .add_packet(msg.now, ssrc, sequence_number, ecn_of(msg.transport.ecn));
                self.arm(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> {
        // The media this endpoint sends is part of the session bandwidth, and what the RTCP
        // session is told about its bandwidth passes here on its way to it.
        self.cadence.on_write(&msg);
        self.write_queue.push_back(msg);
        Ok(())
    }

    fn poll_write(&mut self) -> Option<TaggedPacket> {
        // Rejoins the belt and passes every interceptor between here and the wire, so a pacer
        // meters it and a send history counts its bytes like any other outgoing packet.
        self.write_queue.pop_front()
    }

    fn handle_timeout(&mut self, now: Instant) -> Result<(), Error> {
        self.arm(now);

        if let Some(next_timeout) = self.next_timeout
            && now >= next_timeout
        {
            self.next_timeout = Some(now + self.cadence.interval(now));

            if !self.recorder.is_empty() {
                let report_timestamp = self.report_timestamp(now);
                let sender_ssrc = self.sender_ssrc();
                for report in self.recorder.build_reports(
                    now,
                    sender_ssrc,
                    report_timestamp,
                    self.max_report_size,
                ) {
                    if report.report_blocks.is_empty() {
                        continue;
                    }
                    self.cadence.on_feedback(report.marshal_size());
                    self.write_queue.push_back(TaggedPacket {
                        now,
                        transport: TransportContext::default(),
                        message: AttributedPacket::new(Packet::Rtcp(vec![Box::new(report)])),
                    });
                }
            }
        }
        Ok(())
    }

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

impl Interceptor for Rfc8888Interceptor {
    /// Only a stream that negotiated `ack ccfb` is reported on: RTCP feedback is sent for the
    /// payload types whose `a=rtcp-fb` line names it and for no other (RFC 4585 section 4.2,
    /// RFC 8888 section 6). A peer that declined `ack ccfb` gets no report.
    ///
    /// The stream's retransmission and FEC SSRCs are reported with it: the feedback covers
    /// every packet of the session's negotiated payload types, repair included (section 6).
    fn bind_remote_stream(&mut self, info: &StreamInfo) {
        if stream_supports_ccfb(info) {
            self.streams.extend(flow_ssrcs(info));
        }
    }

    fn unbind_remote_stream(&mut self, info: &StreamInfo) {
        for ssrc in flow_ssrcs(info) {
            self.streams.remove(&ssrc);
            self.recorder.remove_stream(ssrc);
        }
        if self.streams.is_empty() {
            self.next_timeout = None;
        }
    }

    fn bind_local_stream(&mut self, info: &StreamInfo) {
        if !self.local_ssrcs.contains(&info.ssrc) {
            self.local_ssrcs.push(info.ssrc);
        }
    }

    fn unbind_local_stream(&mut self, info: &StreamInfo) {
        self.local_ssrcs.retain(|ssrc| *ssrc != info.ssrc);
    }
}

#[cfg(test)]
mod report_timestamp_test {
    use super::*;
    use crate::report::sender_stream::SenderStream;
    use std::time::Duration;

    /// RFC 8888 section 3.1: the report timestamp is "the middle 32 bits of an NTP format
    /// timestamp ... derived from the same clock used to generate the NTP timestamp field in
    /// RTCP SR packets". A stream that started sending well before the first feedback report
    /// (thirty seconds of a clock the caller drives) and the report agree on the time.
    #[test]
    fn the_report_timestamp_is_on_the_clock_of_the_sender_reports() {
        let start = Instant::now();
        let mut stream = SenderStream::new(0x1234, 90_000, false);
        stream.process_rtp(
            start,
            &rtp::packet::Packet {
                header: rtp::header::Header {
                    ssrc: 0x1234,
                    timestamp: 1,
                    ..Default::default()
                },
                payload: bytes::Bytes::from_static(b"media"),
            },
        );

        let later = start + Duration::from_secs(30);
        let mut feedback = Rfc8888Builder::new().build();
        let rts = feedback.report_timestamp(later);
        let sr = stream.generate_report(later).expect("a sender report");
        assert_eq!((sr.ntp_time >> 16) as u32, rts);
    }
}