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
//! RTCP into the RTP stream statistics.
//!
//! The interceptor handler hands every RTCP compound packet that crosses it, in either
//! direction, to [`RTCStatsAccumulator::on_rtcp_received`] / [`RTCStatsAccumulator::on_rtcp_sent`].
//! What each packet means for the stats is decided here, next to the accumulators it updates.
use super::RTCStatsAccumulator;
use rtcp::extended_report::{
DLRRReportBlock, EcnSummaryReportBlock, ExtendedReport, ReceiverReferenceTimeReportBlock,
};
use rtcp::header::{FORMAT_CCFB, PacketType};
use rtcp::payload_feedbacks::full_intra_request::FullIntraRequest;
use rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
use rtcp::receiver_report::ReceiverReport;
use rtcp::reception_report::ReceptionReport;
use rtcp::sender_report::SenderReport;
use rtcp::transport_feedbacks::cc_feedback_report::CcFeedbackReport;
use rtcp::transport_feedbacks::ecn_feedback::EcnFeedback;
use rtcp::transport_feedbacks::transport_layer_nack::TransportLayerNack;
use std::time::Instant;
/// The reception report blocks a packet carries: an RR's, and an SR's too (RFC 3550 section
/// 6.4.1: a sender that also receives puts its report blocks in its SR).
fn reception_reports(packet: &dyn rtcp::Packet) -> &[ReceptionReport] {
if let Some(rr) = packet.as_any().downcast_ref::<ReceiverReport>() {
&rr.reports
} else if let Some(sr) = packet.as_any().downcast_ref::<SenderReport>() {
&sr.reports
} else {
&[]
}
}
/// Whether the packet is RFC 8888 congestion control feedback (PT 205, FMT 11).
fn is_ccfb(packet: &dyn rtcp::Packet) -> bool {
let header = packet.header();
header.packet_type == PacketType::TransportSpecificFeedback && header.count == FORMAT_CCFB
}
impl RTCStatsAccumulator {
/// Counts an incoming RTCP compound packet.
///
/// * A sender report describes the remote sender of one of our inbound streams (the
/// remote-outbound object).
/// * A reception report block, in an RR or an SR, describes how the remote receives one of
/// our outbound streams (the remote-inbound object): loss, jitter, and a round trip time
/// when it echoes one of our sender reports.
/// * NACK, PLI and FIR are feedback about one of our outbound streams.
/// * An XR DLRR sub-block (RFC 3611 section 4.5) answers a receiver reference time we sent
/// for one of our inbound streams: the remote-outbound object's round trip time.
/// * An RFC 8888 report block describes, packet by packet, how the remote received one of
/// our outbound streams: ECN marks, losses and recoveries (the remote-inbound object).
pub(crate) fn on_rtcp_received(&mut self, packets: &[Box<dyn rtcp::Packet>], now: Instant) {
// RFC 6679 section 7.2.3: under the leap of faith, a receiver report that does not
// report on a stream (or reports everything lost) shows no reception of it; the
// stream's validation waits for a second such report in a row. A report that is full
// (31 blocks) may have left the stream for the next one, and says nothing.
let reports: Vec<&dyn rtcp::Packet> = packets
.iter()
.map(|packet| packet.as_ref())
.filter(|packet| {
let any = packet.as_any();
(any.is::<ReceiverReport>() || any.is::<SenderReport>())
&& reception_reports(*packet).len() < 31
})
.collect();
if !reports.is_empty() {
let received: Vec<u32> = reports
.iter()
.flat_map(|packet| reception_reports(*packet))
.filter(|report| report.fraction_lost < 255)
.map(|report| report.ssrc)
.collect();
for (ssrc, stream) in self.outbound_rtp_streams.iter_mut() {
if let Some(validation) = stream.ecn_validation.as_deref_mut() {
if received.contains(ssrc) {
validation.on_reception_reported();
} else {
validation.on_report_without_reception(now);
}
}
}
}
// RFC 6679 section 7.2.1: a reception report about a stream that is initiating ECN,
// in a compound packet with no ECN report about it, ends the initiation when it shows
// more than three probes should have arrived (the receiver does not do ECN feedback).
let ecn_reported: Vec<u32> = packets
.iter()
.flat_map(|packet| {
let feedback = packet
.as_any()
.downcast_ref::<EcnFeedback>()
.map(|f| f.media_ssrc);
let summaries: Vec<u32> = packet
.as_any()
.downcast_ref::<ExtendedReport>()
.map(|xr| {
xr.reports
.iter()
.filter_map(|b| b.as_any().downcast_ref::<EcnSummaryReportBlock>())
.flat_map(|b| b.reports.iter().map(|r| r.ssrc))
.collect()
})
.unwrap_or_default();
feedback.into_iter().chain(summaries)
})
.collect();
for report in packets.iter().flat_map(|p| reception_reports(p.as_ref())) {
if !ecn_reported.contains(&report.ssrc)
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&report.ssrc)
&& !stream.ccfb_negotiated
&& let Some(validation) = stream.ecn_validation.as_deref_mut()
{
validation.on_reception_report_without_ecn(now, report.last_sequence_number);
}
}
// The sender reports first, wherever they stand in the compound packet: a sender
// report ends the round trip time its stream had (webrtc-stats 8.10: none "if the
// latest SR does not contain the DLRR report block"), and the DLRR block that came
// with it, before or after it in the compound, gives the new one below.
for packet in packets {
if let Some(sr) = packet.as_any().downcast_ref::<SenderReport>()
&& let Some(stream) = self.inbound_rtp_streams.get_mut(&sr.ssrc)
{
stream.on_rtcp_sr_received(
u64::from(sr.packet_count),
u64::from(sr.octet_count),
sr.ntp_time,
now,
);
stream.on_rtcp_sr_rtp_time(sr.rtp_time);
}
}
for packet in packets {
let packet = packet.as_ref();
if is_ccfb(packet) {
self.transport.on_ccfb_received();
}
if let Some(ccfb) = packet.as_any().downcast_ref::<CcFeedbackReport>() {
for block in &ccfb.report_blocks {
if let Some(stream) = self.outbound_rtp_streams.get_mut(&block.media_ssrc) {
stream.on_ccfb_block_received(block, now);
}
}
}
for report in reception_reports(packet) {
if let Some(stream) = self.outbound_rtp_streams.get_mut(&report.ssrc) {
stream.on_receiver_report(report, now);
}
}
if let Some(xr) = packet.as_any().downcast_ref::<ExtendedReport>() {
for block in &xr.reports {
let Some(dlrr) = block.as_any().downcast_ref::<DLRRReportBlock>() else {
continue;
};
for sub in &dlrr.reports {
// A sub-block names the SSRC our reference time came from, which is the
// SSRC our receiver reports about the stream are sent from.
if let Some(stream) = self
.inbound_rtp_streams
.values_mut()
.find(|s| s.receiver_ssrc == Some(sub.ssrc))
{
stream.on_dlrr_received(sub.last_rr, sub.dlrr, now);
}
}
}
}
if let Some(nack) = packet.as_any().downcast_ref::<TransportLayerNack>()
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&nack.media_ssrc)
{
stream.on_nack_received();
}
// RFC 6679 sections 5.1 and 5.2: the receiver's ECN counters about one of our
// outbound streams. An XR ECN Summary Report carries no sequence number; the
// reception report about the same SSRC in the compound packet gives it.
if let Some(feedback) = packet.as_any().downcast_ref::<EcnFeedback>()
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&feedback.media_ssrc)
{
stream.on_ecn_counters_received(
feedback.extended_highest_sequence_number,
feedback.counters,
now,
);
}
if let Some(xr) = packet.as_any().downcast_ref::<ExtendedReport>() {
let summaries = xr
.reports
.iter()
.filter_map(|block| block.as_any().downcast_ref::<EcnSummaryReportBlock>())
.flat_map(|block| &block.reports);
for summary in summaries {
let highest = packets
.iter()
.flat_map(|p| reception_reports(p.as_ref()))
.find(|report| report.ssrc == summary.ssrc)
.map(|report| report.last_sequence_number);
if let Some(highest) = highest
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&summary.ssrc)
{
stream.on_ecn_counters_received(highest, summary.counters, now);
}
}
}
if let Some(pli) = packet.as_any().downcast_ref::<PictureLossIndication>()
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&pli.media_ssrc)
{
stream.on_pli_received();
}
if let Some(fir) = packet.as_any().downcast_ref::<FullIntraRequest>() {
for entry in &fir.fir {
if let Some(stream) = self.outbound_rtp_streams.get_mut(&entry.ssrc) {
stream.on_fir_received();
}
}
}
}
}
/// Counts an outgoing RTCP compound packet.
///
/// * A sender report for one of our outbound streams is remembered, so the reception report
/// that echoes it gives a round trip time.
/// * A reception report block, in an RR or an SR, is what we tell the remote sender about
/// one of our inbound streams: its cumulative loss and jitter are the inbound object's.
/// * NACK, PLI and FIR are feedback we send about one of our inbound streams.
/// * An XR receiver reference time block (RFC 3611 section 4.4) is remembered for the
/// inbound stream whose receiver reports come from the same SSRC, so the DLRR that
/// echoes it gives a round trip time.
/// * An RFC 8888 report block we send about one of our inbound streams counts the packets
/// it reports lost, and later recovered.
pub(crate) fn on_rtcp_sent(&mut self, packets: &[Box<dyn rtcp::Packet>], now: Instant) {
for packet in packets {
let packet = packet.as_ref();
if is_ccfb(packet) {
self.transport.on_ccfb_sent();
}
if let Some(ccfb) = packet.as_any().downcast_ref::<CcFeedbackReport>() {
for block in &ccfb.report_blocks {
if let Some(stream) = self.inbound_rtp_streams.get_mut(&block.media_ssrc) {
stream.on_ccfb_block_sent(block);
}
}
}
if let Some(sr) = packet.as_any().downcast_ref::<SenderReport>()
&& let Some(stream) = self.outbound_rtp_streams.get_mut(&sr.ssrc)
{
stream.on_sender_report_sent(sr.ntp_time, now);
}
let reporter_ssrc = packet
.as_any()
.downcast_ref::<ReceiverReport>()
.map(|rr| rr.ssrc)
.or_else(|| {
packet
.as_any()
.downcast_ref::<SenderReport>()
.map(|sr| sr.ssrc)
});
for report in reception_reports(packet) {
if let Some(stream) = self.inbound_rtp_streams.get_mut(&report.ssrc) {
stream.on_rtcp_rr_generated(i64::from(report.cumulative_lost()), report.jitter);
stream.receiver_ssrc = reporter_ssrc;
}
}
if let Some(xr) = packet.as_any().downcast_ref::<ExtendedReport>() {
for block in &xr.reports {
if let Some(rrtr) = block
.as_any()
.downcast_ref::<ReceiverReferenceTimeReportBlock>()
&& let Some(stream) = self
.inbound_rtp_streams
.values_mut()
.find(|s| s.receiver_ssrc == Some(xr.sender_ssrc))
{
stream.on_reference_time_sent((rrtr.ntp_timestamp >> 16) as u32, now);
}
}
}
if let Some(nack) = packet.as_any().downcast_ref::<TransportLayerNack>()
&& let Some(stream) = self.inbound_rtp_streams.get_mut(&nack.media_ssrc)
{
stream.on_nack_sent();
}
if let Some(pli) = packet.as_any().downcast_ref::<PictureLossIndication>()
&& let Some(stream) = self.inbound_rtp_streams.get_mut(&pli.media_ssrc)
{
stream.on_pli_sent();
}
if let Some(fir) = packet.as_any().downcast_ref::<FullIntraRequest>() {
for entry in &fir.fir {
if let Some(stream) = self.inbound_rtp_streams.get_mut(&entry.ssrc) {
stream.on_fir_sent();
}
}
}
}
}
}