rtci 0.2.0

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
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
//! RFC 8083 section 4.5 at the peer connection: a flow the RTP circuit breaker ceased or
//! restarted is announced for the sender and encoding it belongs to, and a ceased encoding
//! takes no share of the congestion controller's target.

use super::*;
use crate::peer_connection::RTCPeerConnectionBuilder;
use crate::rtp_transceiver::rtp_sender::{
    RTCRtpCodingParameters, RTCRtpEncodingParameters, RTCRtpRtxParameters, RtpCodecKind,
};
use crate::rtp_transceiver::{RTCRtpSenderId, RTCRtpTransceiverDirection, RTCRtpTransceiverInit};
use interceptor::{AttributedPacket, CircuitBreakerNotice, CircuitBreakerReason, StreamInfo};
use sansio::Protocol;
use shared::TransportContext;
use std::time::Duration;

/// Hands out on `poll_read` what was queued: the chain application-ward of the breaker.
#[derive(Default)]
struct ReadChain {
    reads: VecDeque<TaggedPacket>,
}

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

    fn handle_read(&mut self, _: TaggedPacket) -> Result<()> {
        Ok(())
    }
    fn poll_read(&mut self) -> Option<TaggedPacket> {
        self.reads.pop_front()
    }
    fn handle_write(&mut self, _: TaggedPacket) -> Result<()> {
        Ok(())
    }
    fn poll_write(&mut self) -> Option<TaggedPacket> {
        None
    }
    fn handle_event(&mut self, _: ()) -> Result<()> {
        Ok(())
    }
    fn poll_event(&mut self) -> Option<()> {
        None
    }
    fn handle_timeout(&mut self, _: Instant) -> Result<()> {
        Ok(())
    }
    fn poll_timeout(&mut self) -> Option<Instant> {
        None
    }
    fn close(&mut self) -> Result<()> {
        Ok(())
    }
}

impl Interceptor for ReadChain {
    fn bind_local_stream(&mut self, _: &StreamInfo) {}
    fn unbind_local_stream(&mut self, _: &StreamInfo) {}
    fn bind_remote_stream(&mut self, _: &StreamInfo) {}
    fn unbind_remote_stream(&mut self, _: &StreamInfo) {}
}

/// What `OnCircuitBreaker` carried.
#[derive(Debug, PartialEq)]
struct Told {
    sender: RTCRtpSenderId,
    rid: Option<String>,
    ssrc: u32,
    ceased: Option<CircuitBreakerReason>,
    restart_after: Duration,
}

/// Hands `notice` to the connection's interceptor handler as the breaker's chain would, and
/// returns the events the application is given for it.
fn announce(
    pc: &mut crate::peer_connection::RTCPeerConnection,
    notice: CircuitBreakerNotice,
) -> Vec<Told> {
    hand(
        pc,
        AttributedPacket::new(Packet::Rtcp(vec![]))
            .with(Attribute::Custom(std::sync::Arc::new(notice))),
    )
}

/// Hands `message` to the connection's interceptor handler as its chain would on the read
/// leg, and returns the `OnCircuitBreaker` events the application is given for it.
fn hand(
    pc: &mut crate::peer_connection::RTCPeerConnection,
    message: AttributedPacket,
) -> Vec<Told> {
    let mut chain = ReadChain::default();
    chain.reads.push_back(TaggedPacket {
        now: Instant::now(),
        transport: TransportContext::default(),
        message,
    });
    let media_engine = MediaEngine::default();
    let context = &mut pc.pipeline_context;
    context
        .interceptor_handler_context
        .is_dtls_handshake_complete = true;
    {
        let mut handler = InterceptorHandler::new(
            &mut context.interceptor_handler_context,
            &mut pc.rtp_transceivers,
            &media_engine,
            &mut chain,
            &mut context.stats,
            None,
        );
        while handler.poll_read().is_some() {}
    }
    context
        .interceptor_handler_context
        .event_outs
        .drain(..)
        .filter_map(|tagged| match tagged.event {
            RTCEventInternal::RTCPeerConnectionEvent(
                RTCPeerConnectionEvent::OnCircuitBreaker {
                    sender,
                    rid,
                    ssrc,
                    ceased,
                    restart_after,
                },
            ) => Some(Told {
                sender,
                rid,
                ssrc,
                ceased,
                restart_after,
            }),
            _ => None,
        })
        .collect()
}

const HIGH: u32 = 0x1001;
const LOW: u32 = 0x1002;
const HIGH_RTX: u32 = 0x2001;
const LOW_RTX: u32 = 0x2002;
const VOICE: u32 = 0x3001;

fn encoding(rid: &str, ssrc: u32, rtx: Option<u32>) -> RTCRtpEncodingParameters {
    RTCRtpEncodingParameters {
        active: true,
        rtp_coding_parameters: RTCRtpCodingParameters {
            rid: rid.to_owned(),
            ssrc: Some(ssrc),
            rtx: rtx.map(|ssrc| RTCRtpRtxParameters { ssrc }),
            ..Default::default()
        },
        ..Default::default()
    }
}

/// A connection that sends a simulcast video (rids `h` and `l`, each with an RTX flow) and a
/// voice stream, with a target of 900 kbit/s to divide.
fn connection() -> (
    crate::peer_connection::RTCPeerConnection,
    RTCRtpSenderId,
    RTCRtpSenderId,
) {
    let mut media_engine = MediaEngine::default();
    media_engine.register_default_codecs().unwrap();
    let mut pc = RTCPeerConnectionBuilder::new()
        .with_media_engine(media_engine)
        .build(Instant::now())
        .unwrap();
    let mut add = |kind: RtpCodecKind, send_encodings: Vec<RTCRtpEncodingParameters>| {
        RTCRtpSenderId::from(
            pc.add_transceiver_from_kind(
                kind,
                Some(RTCRtpTransceiverInit {
                    direction: RTCRtpTransceiverDirection::Sendonly,
                    streams: vec![],
                    send_encodings,
                }),
            )
            .unwrap(),
        )
    };
    let video = add(
        RtpCodecKind::Video,
        vec![
            encoding("h", HIGH, Some(HIGH_RTX)),
            encoding("l", LOW, Some(LOW_RTX)),
        ],
    );
    let voice = add(RtpCodecKind::Audio, vec![encoding("", VOICE, None)]);
    pc.pipeline_context
        .interceptor_handler_context
        .transport_targets
        .insert(0, 900_000.0);
    pc.reallocate_target_bitrate();
    (pc, video, voice)
}

fn shares(
    pc: &mut crate::peer_connection::RTCPeerConnection,
    id: RTCRtpSenderId,
) -> Vec<(u32, f64)> {
    pc.rtp_sender(id).unwrap().target_bitrates().to_vec()
}

fn notice(
    ssrc: u32,
    ceased: Option<CircuitBreakerReason>,
    restart_after: Duration,
) -> CircuitBreakerNotice {
    CircuitBreakerNotice {
        ssrc,
        ceased,
        restart_after,
    }
}

/// `OnCircuitBreaker` names the sender, the RID of the encoding and the flow, whether the
/// flow is the encoding's media SSRC or its RTX SSRC; an encoding without a RID has none; a
/// restart is told with `ceased: None`; a flow no sender sends is not announced.
#[test]
fn a_ceased_flow_is_announced_for_its_sender_and_the_rid_of_its_encoding() {
    let (mut pc, video, voice) = connection();
    let hold = Duration::from_millis(125);

    assert_eq!(
        vec![Told {
            sender: video,
            rid: Some("l".to_owned()),
            ssrc: LOW_RTX,
            ceased: Some(CircuitBreakerReason::Congestion),
            restart_after: Duration::from_secs(3),
        }],
        announce(
            &mut pc,
            notice(
                LOW_RTX,
                Some(CircuitBreakerReason::Congestion),
                Duration::from_secs(3)
            )
        )
    );
    assert_eq!(
        vec![Told {
            sender: video,
            rid: Some("h".to_owned()),
            ssrc: HIGH,
            ceased: Some(CircuitBreakerReason::MediaTimeout),
            restart_after: hold,
        }],
        announce(
            &mut pc,
            notice(HIGH, Some(CircuitBreakerReason::MediaTimeout), hold)
        )
    );
    assert_eq!(
        vec![Told {
            sender: voice,
            rid: None,
            ssrc: VOICE,
            ceased: Some(CircuitBreakerReason::MediaTimeout),
            restart_after: hold,
        }],
        announce(
            &mut pc,
            notice(VOICE, Some(CircuitBreakerReason::MediaTimeout), hold)
        )
    );
    assert_eq!(
        vec![Told {
            sender: voice,
            rid: None,
            ssrc: VOICE,
            ceased: None,
            restart_after: Duration::ZERO,
        }],
        announce(&mut pc, notice(VOICE, None, Duration::ZERO))
    );
    assert!(
        announce(
            &mut pc,
            notice(0x7777, Some(CircuitBreakerReason::RtcpTimeout), hold)
        )
        .is_empty()
    );
}

/// A ceased encoding sends nothing, so it takes no share of the target: what it had goes to
/// the encodings that still send, and it has its share again when it is restarted. A ceased
/// RTX flow leaves its encoding's share alone (the media still leaves).
#[test]
fn a_ceased_encoding_takes_no_share_of_the_target_until_it_is_restarted() {
    let (mut pc, video, voice) = connection();
    let hold = Duration::from_millis(125);
    let timeout = Some(CircuitBreakerReason::MediaTimeout);
    assert_eq!(
        vec![(HIGH, 300_000.0), (LOW, 300_000.0)],
        shares(&mut pc, video)
    );
    assert_eq!(vec![(VOICE, 300_000.0)], shares(&mut pc, voice));

    announce(&mut pc, notice(LOW_RTX, timeout, hold));
    assert_eq!(
        vec![(HIGH, 300_000.0), (LOW, 300_000.0)],
        shares(&mut pc, video),
        "the RTX flow is not the encoding"
    );

    announce(&mut pc, notice(LOW, timeout, hold));
    assert_eq!(vec![(HIGH, 450_000.0)], shares(&mut pc, video));
    assert_eq!(vec![(VOICE, 450_000.0)], shares(&mut pc, voice));

    announce(&mut pc, notice(VOICE, timeout, hold));
    assert_eq!(vec![(HIGH, 900_000.0)], shares(&mut pc, video));
    assert!(shares(&mut pc, voice).is_empty());

    // A later target is divided the same way.
    pc.pipeline_context
        .interceptor_handler_context
        .transport_targets
        .insert(0, 600_000.0);
    pc.reallocate_target_bitrate();
    assert_eq!(vec![(HIGH, 600_000.0)], shares(&mut pc, video));

    announce(&mut pc, notice(LOW, None, Duration::ZERO));
    assert_eq!(
        vec![(HIGH, 300_000.0), (LOW, 300_000.0)],
        shares(&mut pc, video)
    );
    announce(&mut pc, notice(VOICE, None, Duration::ZERO));
    assert_eq!(vec![(VOICE, 200_000.0)], shares(&mut pc, voice));
}

/// While the pacer has the source paused the streams' target is zero and the congestion
/// controller's estimate rides along (`PacerPause`): `availableOutgoingBitrate` stays the
/// estimate. A flow that ceases or restarts during the pause has the (zero) target divided
/// again, and the estimate is still what the congestion controller said: dividing is not a
/// new estimate.
#[test]
fn a_flow_ceasing_while_the_source_is_paused_leaves_the_estimate_alone() {
    let (mut pc, _video, _voice) = connection();
    let estimate = |pc: &crate::peer_connection::RTCPeerConnection| {
        pc.pipeline_context
            .stats
            .outgoing_estimates
            .get(&0)
            .copied()
    };
    // The pacer's pause as the chain hands it over: a target of zero, the estimate with it.
    hand(
        &mut pc,
        AttributedPacket::new(Packet::Rtcp(vec![]))
            .with(Attribute::TargetBitrateChanged {
                bits_per_second: 0.0,
            })
            .with(Attribute::Custom(std::sync::Arc::new(
                interceptor::PacerPause {
                    target_bits_per_second: 250_000.0,
                },
            ))),
    );
    assert_eq!(
        Some(&0.0),
        pc.pipeline_context
            .interceptor_handler_context
            .transport_targets
            .get(&0),
        "the streams are paused"
    );
    assert_eq!(Some(250_000.0), estimate(&pc));

    let told = announce(
        &mut pc,
        notice(
            VOICE,
            Some(CircuitBreakerReason::MediaTimeout),
            Duration::from_millis(125),
        ),
    );
    assert_eq!(1, told.len());
    assert_eq!(Some(250_000.0), estimate(&pc), "a flow ceased in the pause");

    let told = announce(&mut pc, notice(VOICE, None, Duration::ZERO));
    assert_eq!(1, told.len());
    assert_eq!(
        Some(250_000.0),
        estimate(&pc),
        "a flow restarted in the pause"
    );
}

/// The same when the application changes an encoding's parameters during a pause
/// (`RTCRtpSender::set_parameters` divides the target again): the paused target of zero is
/// not written as the estimate, and `availableOutgoingBitrate` does not lose it.
#[test]
fn dividing_the_target_again_while_the_source_is_paused_leaves_the_estimate_alone() {
    let (mut pc, _video, _voice) = connection();
    hand(
        &mut pc,
        AttributedPacket::new(Packet::Rtcp(vec![]))
            .with(Attribute::TargetBitrateChanged {
                bits_per_second: 0.0,
            })
            .with(Attribute::Custom(std::sync::Arc::new(
                interceptor::PacerPause {
                    target_bits_per_second: 250_000.0,
                },
            ))),
    );
    pc.reallocate_target_bitrate();
    assert_eq!(
        Some(250_000.0),
        pc.pipeline_context
            .stats
            .outgoing_estimates
            .get(&0)
            .copied()
    );
}