1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/rtp_rtcp/source/rtcp_receiver.h"
12
13 #include <string.h>
14
15 #include <algorithm>
16 #include <limits>
17 #include <map>
18 #include <memory>
19 #include <utility>
20 #include <vector>
21
22 #include "api/video/video_bitrate_allocation.h"
23 #include "api/video/video_bitrate_allocator.h"
24 #include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
26 #include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
27 #include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
28 #include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
29 #include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
30 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
31 #include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
32 #include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
33 #include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
34 #include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
35 #include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
36 #include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
37 #include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
38 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
39 #include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
40 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
41 #include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
42 #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
43 #include "modules/rtp_rtcp/source/time_util.h"
44 #include "modules/rtp_rtcp/source/tmmbr_help.h"
45 #include "rtc_base/checks.h"
46 #include "rtc_base/logging.h"
47 #include "rtc_base/time_utils.h"
48 #include "rtc_base/trace_event.h"
49 #include "system_wrappers/include/ntp_time.h"
50
51 namespace webrtc {
52 namespace {
53
54 using rtcp::CommonHeader;
55 using rtcp::ReportBlock;
56
57 // The number of RTCP time intervals needed to trigger a timeout.
58 const int kRrTimeoutIntervals = 3;
59
60 const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
61
62 const int64_t kMaxWarningLogIntervalMs = 10000;
63 const int64_t kRtcpMinFrameLengthMs = 17;
64
65 // Maximum number of received RRTRs that will be stored.
66 const size_t kMaxNumberOfStoredRrtrs = 300;
67
68 constexpr TimeDelta kDefaultVideoReportInterval = TimeDelta::Seconds(1);
69 constexpr TimeDelta kDefaultAudioReportInterval = TimeDelta::Seconds(5);
70
71 // Returns true if the `timestamp` has exceeded the |interval *
72 // kRrTimeoutIntervals| period and was reset (set to PlusInfinity()). Returns
73 // false if the timer was either already reset or if it has not expired.
ResetTimestampIfExpired(const Timestamp now,Timestamp & timestamp,TimeDelta interval)74 bool ResetTimestampIfExpired(const Timestamp now,
75 Timestamp& timestamp,
76 TimeDelta interval) {
77 if (timestamp.IsInfinite() ||
78 now <= timestamp + interval * kRrTimeoutIntervals) {
79 return false;
80 }
81
82 timestamp = Timestamp::PlusInfinity();
83 return true;
84 }
85
86 } // namespace
87
88 constexpr size_t RTCPReceiver::RegisteredSsrcs::kMediaSsrcIndex;
89 constexpr size_t RTCPReceiver::RegisteredSsrcs::kMaxSsrcs;
90
RegisteredSsrcs(bool disable_sequence_checker,const RtpRtcpInterface::Configuration & config)91 RTCPReceiver::RegisteredSsrcs::RegisteredSsrcs(
92 bool disable_sequence_checker,
93 const RtpRtcpInterface::Configuration& config)
94 : packet_sequence_checker_(disable_sequence_checker) {
95 packet_sequence_checker_.Detach();
96 ssrcs_.push_back(config.local_media_ssrc);
97 if (config.rtx_send_ssrc) {
98 ssrcs_.push_back(*config.rtx_send_ssrc);
99 }
100 if (config.fec_generator) {
101 absl::optional<uint32_t> flexfec_ssrc = config.fec_generator->FecSsrc();
102 if (flexfec_ssrc) {
103 ssrcs_.push_back(*flexfec_ssrc);
104 }
105 }
106 // Ensure that the RegisteredSsrcs can inline the SSRCs.
107 RTC_DCHECK_LE(ssrcs_.size(), RTCPReceiver::RegisteredSsrcs::kMaxSsrcs);
108 }
109
contains(uint32_t ssrc) const110 bool RTCPReceiver::RegisteredSsrcs::contains(uint32_t ssrc) const {
111 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
112 return absl::c_linear_search(ssrcs_, ssrc);
113 }
114
media_ssrc() const115 uint32_t RTCPReceiver::RegisteredSsrcs::media_ssrc() const {
116 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
117 return ssrcs_[kMediaSsrcIndex];
118 }
119
set_media_ssrc(uint32_t ssrc)120 void RTCPReceiver::RegisteredSsrcs::set_media_ssrc(uint32_t ssrc) {
121 RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
122 ssrcs_[kMediaSsrcIndex] = ssrc;
123 }
124
125 struct RTCPReceiver::PacketInformation {
126 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
127
128 uint32_t remote_ssrc = 0;
129 std::vector<uint16_t> nack_sequence_numbers;
130 // TODO(hbos): Remove `report_blocks` in favor of `report_block_datas`.
131 ReportBlockList report_blocks;
132 std::vector<ReportBlockData> report_block_datas;
133 int64_t rtt_ms = 0;
134 uint32_t receiver_estimated_max_bitrate_bps = 0;
135 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
136 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
137 absl::optional<NetworkStateEstimate> network_state_estimate;
138 std::unique_ptr<rtcp::LossNotification> loss_notification;
139 };
140
RTCPReceiver(const RtpRtcpInterface::Configuration & config,ModuleRtpRtcpImpl2 * owner)141 RTCPReceiver::RTCPReceiver(const RtpRtcpInterface::Configuration& config,
142 ModuleRtpRtcpImpl2* owner)
143 : clock_(config.clock),
144 receiver_only_(config.receiver_only),
145 rtp_rtcp_(owner),
146 registered_ssrcs_(false, config),
147 rtcp_bandwidth_observer_(config.bandwidth_callback),
148 rtcp_intra_frame_observer_(config.intra_frame_callback),
149 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
150 network_state_estimate_observer_(config.network_state_estimate_observer),
151 transport_feedback_observer_(config.transport_feedback_callback),
152 bitrate_allocation_observer_(config.bitrate_allocation_observer),
153 report_interval_(config.rtcp_report_interval_ms > 0
154 ? TimeDelta::Millis(config.rtcp_report_interval_ms)
155 : (config.audio ? kDefaultAudioReportInterval
156 : kDefaultVideoReportInterval)),
157 // TODO(bugs.webrtc.org/10774): Remove fallback.
158 remote_ssrc_(0),
159 remote_sender_rtp_time_(0),
160 remote_sender_packet_count_(0),
161 remote_sender_octet_count_(0),
162 remote_sender_reports_count_(0),
163 xr_rrtr_status_(config.non_sender_rtt_measurement),
164 xr_rr_rtt_ms_(0),
165 oldest_tmmbr_info_ms_(0),
166 cname_callback_(config.rtcp_cname_callback),
167 report_block_data_observer_(config.report_block_data_observer),
168 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
169 num_skipped_packets_(0),
170 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
171 RTC_DCHECK(owner);
172 }
173
RTCPReceiver(const RtpRtcpInterface::Configuration & config,ModuleRtpRtcp * owner)174 RTCPReceiver::RTCPReceiver(const RtpRtcpInterface::Configuration& config,
175 ModuleRtpRtcp* owner)
176 : clock_(config.clock),
177 receiver_only_(config.receiver_only),
178 rtp_rtcp_(owner),
179 registered_ssrcs_(true, config),
180 rtcp_bandwidth_observer_(config.bandwidth_callback),
181 rtcp_intra_frame_observer_(config.intra_frame_callback),
182 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
183 network_state_estimate_observer_(config.network_state_estimate_observer),
184 transport_feedback_observer_(config.transport_feedback_callback),
185 bitrate_allocation_observer_(config.bitrate_allocation_observer),
186 report_interval_(config.rtcp_report_interval_ms > 0
187 ? TimeDelta::Millis(config.rtcp_report_interval_ms)
188 : (config.audio ? kDefaultAudioReportInterval
189 : kDefaultVideoReportInterval)),
190 // TODO(bugs.webrtc.org/10774): Remove fallback.
191 remote_ssrc_(0),
192 remote_sender_rtp_time_(0),
193 remote_sender_packet_count_(0),
194 remote_sender_octet_count_(0),
195 remote_sender_reports_count_(0),
196 xr_rrtr_status_(config.non_sender_rtt_measurement),
197 xr_rr_rtt_ms_(0),
198 oldest_tmmbr_info_ms_(0),
199 cname_callback_(config.rtcp_cname_callback),
200 report_block_data_observer_(config.report_block_data_observer),
201 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
202 num_skipped_packets_(0),
203 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
204 RTC_DCHECK(owner);
205 // Dear reader - if you're here because of this log statement and are
206 // wondering what this is about, chances are that you are using an instance
207 // of RTCPReceiver without using the webrtc APIs. This creates a bit of a
208 // problem for WebRTC because this class is a part of an internal
209 // implementation that is constantly changing and being improved.
210 // The intention of this log statement is to give a heads up that changes
211 // are coming and encourage you to use the public APIs or be prepared that
212 // things might break down the line as more changes land. A thing you could
213 // try out for now is to replace the `CustomSequenceChecker` in the header
214 // with a regular `SequenceChecker` and see if that triggers an
215 // error in your code. If it does, chances are you have your own threading
216 // model that is not the same as WebRTC internally has.
217 RTC_LOG(LS_INFO) << "************** !!!DEPRECATION WARNING!! **************";
218 }
219
~RTCPReceiver()220 RTCPReceiver::~RTCPReceiver() {}
221
IncomingPacket(rtc::ArrayView<const uint8_t> packet)222 void RTCPReceiver::IncomingPacket(rtc::ArrayView<const uint8_t> packet) {
223 if (packet.empty()) {
224 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
225 return;
226 }
227
228 PacketInformation packet_information;
229 if (!ParseCompoundPacket(packet, &packet_information))
230 return;
231 TriggerCallbacksFromRtcpPacket(packet_information);
232 }
233
234 // This method is only used by test and legacy code, so we should be able to
235 // remove it soon.
LastReceivedReportBlockMs() const236 int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
237 MutexLock lock(&rtcp_receiver_lock_);
238 return last_received_rb_.IsFinite() ? last_received_rb_.ms() : 0;
239 }
240
SetRemoteSSRC(uint32_t ssrc)241 void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
242 MutexLock lock(&rtcp_receiver_lock_);
243 // New SSRC reset old reports.
244 last_received_sr_ntp_.Reset();
245 remote_ssrc_ = ssrc;
246 }
247
set_local_media_ssrc(uint32_t ssrc)248 void RTCPReceiver::set_local_media_ssrc(uint32_t ssrc) {
249 registered_ssrcs_.set_media_ssrc(ssrc);
250 }
251
local_media_ssrc() const252 uint32_t RTCPReceiver::local_media_ssrc() const {
253 return registered_ssrcs_.media_ssrc();
254 }
255
RemoteSSRC() const256 uint32_t RTCPReceiver::RemoteSSRC() const {
257 MutexLock lock(&rtcp_receiver_lock_);
258 return remote_ssrc_;
259 }
260
AddRtt(TimeDelta rtt)261 void RTCPReceiver::RttStats::AddRtt(TimeDelta rtt) {
262 last_rtt_ = rtt;
263 if (rtt < min_rtt_) {
264 min_rtt_ = rtt;
265 }
266 if (rtt > max_rtt_) {
267 max_rtt_ = rtt;
268 }
269 sum_rtt_ += rtt;
270 ++num_rtts_;
271 }
272
RTT(uint32_t remote_ssrc,int64_t * last_rtt_ms,int64_t * avg_rtt_ms,int64_t * min_rtt_ms,int64_t * max_rtt_ms) const273 int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
274 int64_t* last_rtt_ms,
275 int64_t* avg_rtt_ms,
276 int64_t* min_rtt_ms,
277 int64_t* max_rtt_ms) const {
278 MutexLock lock(&rtcp_receiver_lock_);
279
280 auto it = rtts_.find(remote_ssrc);
281 if (it == rtts_.end()) {
282 return -1;
283 }
284
285 if (last_rtt_ms) {
286 *last_rtt_ms = it->second.last_rtt().ms();
287 }
288
289 if (avg_rtt_ms) {
290 *avg_rtt_ms = it->second.average_rtt().ms();
291 }
292
293 if (min_rtt_ms) {
294 *min_rtt_ms = it->second.min_rtt().ms();
295 }
296
297 if (max_rtt_ms) {
298 *max_rtt_ms = it->second.max_rtt().ms();
299 }
300
301 return 0;
302 }
303
GetNonSenderRTT() const304 RTCPReceiver::NonSenderRttStats RTCPReceiver::GetNonSenderRTT() const {
305 MutexLock lock(&rtcp_receiver_lock_);
306 auto it = non_sender_rtts_.find(remote_ssrc_);
307 if (it == non_sender_rtts_.end()) {
308 return {};
309 }
310 return it->second;
311 }
312
SetNonSenderRttMeasurement(bool enabled)313 void RTCPReceiver::SetNonSenderRttMeasurement(bool enabled) {
314 MutexLock lock(&rtcp_receiver_lock_);
315 xr_rrtr_status_ = enabled;
316 }
317
GetAndResetXrRrRtt(int64_t * rtt_ms)318 bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
319 RTC_DCHECK(rtt_ms);
320 MutexLock lock(&rtcp_receiver_lock_);
321 if (xr_rr_rtt_ms_ == 0) {
322 return false;
323 }
324 *rtt_ms = xr_rr_rtt_ms_;
325 xr_rr_rtt_ms_ = 0;
326 return true;
327 }
328
329 // Called regularly (1/sec) on the worker thread to do rtt calculations.
OnPeriodicRttUpdate(Timestamp newer_than,bool sending)330 absl::optional<TimeDelta> RTCPReceiver::OnPeriodicRttUpdate(
331 Timestamp newer_than,
332 bool sending) {
333 // Running on the worker thread (same as construction thread).
334 absl::optional<TimeDelta> rtt;
335
336 if (sending) {
337 // Check if we've received a report block within the last kRttUpdateInterval
338 // amount of time.
339 MutexLock lock(&rtcp_receiver_lock_);
340 if (last_received_rb_.IsInfinite() || last_received_rb_ > newer_than) {
341 TimeDelta max_rtt = TimeDelta::MinusInfinity();
342 for (const auto& rtt_stats : rtts_) {
343 if (rtt_stats.second.last_rtt() > max_rtt) {
344 max_rtt = rtt_stats.second.last_rtt();
345 }
346 }
347 if (max_rtt.IsFinite()) {
348 rtt = max_rtt;
349 }
350 }
351
352 // Check for expired timers and if so, log and reset.
353 auto now = clock_->CurrentTime();
354 if (RtcpRrTimeoutLocked(now)) {
355 RTC_LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
356 } else if (RtcpRrSequenceNumberTimeoutLocked(now)) {
357 RTC_LOG_F(LS_WARNING) << "Timeout: No increase in RTCP RR extended "
358 "highest sequence number.";
359 }
360 } else {
361 // Report rtt from receiver.
362 int64_t rtt_ms;
363 if (GetAndResetXrRrRtt(&rtt_ms)) {
364 rtt.emplace(TimeDelta::Millis(rtt_ms));
365 }
366 }
367
368 return rtt;
369 }
370
NTP(uint32_t * received_ntp_secs,uint32_t * received_ntp_frac,uint32_t * rtcp_arrival_time_secs,uint32_t * rtcp_arrival_time_frac,uint32_t * rtcp_timestamp,uint32_t * remote_sender_packet_count,uint64_t * remote_sender_octet_count,uint64_t * remote_sender_reports_count) const371 bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
372 uint32_t* received_ntp_frac,
373 uint32_t* rtcp_arrival_time_secs,
374 uint32_t* rtcp_arrival_time_frac,
375 uint32_t* rtcp_timestamp,
376 uint32_t* remote_sender_packet_count,
377 uint64_t* remote_sender_octet_count,
378 uint64_t* remote_sender_reports_count) const {
379 MutexLock lock(&rtcp_receiver_lock_);
380 if (!last_received_sr_ntp_.Valid())
381 return false;
382
383 // NTP from incoming SenderReport.
384 if (received_ntp_secs)
385 *received_ntp_secs = remote_sender_ntp_time_.seconds();
386 if (received_ntp_frac)
387 *received_ntp_frac = remote_sender_ntp_time_.fractions();
388 // Rtp time from incoming SenderReport.
389 if (rtcp_timestamp)
390 *rtcp_timestamp = remote_sender_rtp_time_;
391
392 // Local NTP time when we received a RTCP packet with a send block.
393 if (rtcp_arrival_time_secs)
394 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
395 if (rtcp_arrival_time_frac)
396 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
397
398 // Counters.
399 if (remote_sender_packet_count)
400 *remote_sender_packet_count = remote_sender_packet_count_;
401 if (remote_sender_octet_count)
402 *remote_sender_octet_count = remote_sender_octet_count_;
403 if (remote_sender_reports_count)
404 *remote_sender_reports_count = remote_sender_reports_count_;
405
406 return true;
407 }
408
409 std::vector<rtcp::ReceiveTimeInfo>
ConsumeReceivedXrReferenceTimeInfo()410 RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
411 MutexLock lock(&rtcp_receiver_lock_);
412
413 const size_t last_xr_rtis_size = std::min(
414 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
415 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
416 last_xr_rtis.reserve(last_xr_rtis_size);
417
418 const uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
419
420 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
421 RrtrInformation& rrtr = received_rrtrs_.front();
422 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
423 now_ntp - rrtr.local_receive_mid_ntp_time);
424 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
425 received_rrtrs_.pop_front();
426 }
427
428 return last_xr_rtis;
429 }
430
GetLatestReportBlockData() const431 std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
432 std::vector<ReportBlockData> result;
433 MutexLock lock(&rtcp_receiver_lock_);
434 for (const auto& report : received_report_blocks_) {
435 result.push_back(report.second);
436 }
437 return result;
438 }
439
ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet,PacketInformation * packet_information)440 bool RTCPReceiver::ParseCompoundPacket(rtc::ArrayView<const uint8_t> packet,
441 PacketInformation* packet_information) {
442 MutexLock lock(&rtcp_receiver_lock_);
443
444 CommonHeader rtcp_block;
445 // If a sender report is received but no DLRR, we need to reset the
446 // roundTripTime stat according to the standard, see
447 // https://www.w3.org/TR/webrtc-stats/#dom-rtcremoteoutboundrtpstreamstats-roundtriptime
448 struct RtcpReceivedBlock {
449 bool sender_report = false;
450 bool dlrr = false;
451 };
452 // For each remote SSRC we store if we've received a sender report or a DLRR
453 // block.
454 flat_map<uint32_t, RtcpReceivedBlock> received_blocks;
455 for (const uint8_t* next_block = packet.begin(); next_block != packet.end();
456 next_block = rtcp_block.NextPacket()) {
457 ptrdiff_t remaining_blocks_size = packet.end() - next_block;
458 RTC_DCHECK_GT(remaining_blocks_size, 0);
459 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
460 if (next_block == packet.begin()) {
461 // Failed to parse 1st header, nothing was extracted from this packet.
462 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
463 return false;
464 }
465 ++num_skipped_packets_;
466 break;
467 }
468
469 switch (rtcp_block.type()) {
470 case rtcp::SenderReport::kPacketType:
471 HandleSenderReport(rtcp_block, packet_information);
472 received_blocks[packet_information->remote_ssrc].sender_report = true;
473 break;
474 case rtcp::ReceiverReport::kPacketType:
475 HandleReceiverReport(rtcp_block, packet_information);
476 break;
477 case rtcp::Sdes::kPacketType:
478 HandleSdes(rtcp_block, packet_information);
479 break;
480 case rtcp::ExtendedReports::kPacketType: {
481 bool contains_dlrr = false;
482 uint32_t ssrc = 0;
483 HandleXr(rtcp_block, packet_information, contains_dlrr, ssrc);
484 if (contains_dlrr) {
485 received_blocks[ssrc].dlrr = true;
486 }
487 break;
488 }
489 case rtcp::Bye::kPacketType:
490 HandleBye(rtcp_block);
491 break;
492 case rtcp::App::kPacketType:
493 HandleApp(rtcp_block, packet_information);
494 break;
495 case rtcp::Rtpfb::kPacketType:
496 switch (rtcp_block.fmt()) {
497 case rtcp::Nack::kFeedbackMessageType:
498 HandleNack(rtcp_block, packet_information);
499 break;
500 case rtcp::Tmmbr::kFeedbackMessageType:
501 HandleTmmbr(rtcp_block, packet_information);
502 break;
503 case rtcp::Tmmbn::kFeedbackMessageType:
504 HandleTmmbn(rtcp_block, packet_information);
505 break;
506 case rtcp::RapidResyncRequest::kFeedbackMessageType:
507 HandleSrReq(rtcp_block, packet_information);
508 break;
509 case rtcp::TransportFeedback::kFeedbackMessageType:
510 HandleTransportFeedback(rtcp_block, packet_information);
511 break;
512 default:
513 ++num_skipped_packets_;
514 break;
515 }
516 break;
517 case rtcp::Psfb::kPacketType:
518 switch (rtcp_block.fmt()) {
519 case rtcp::Pli::kFeedbackMessageType:
520 HandlePli(rtcp_block, packet_information);
521 break;
522 case rtcp::Fir::kFeedbackMessageType:
523 HandleFir(rtcp_block, packet_information);
524 break;
525 case rtcp::Psfb::kAfbMessageType:
526 HandlePsfbApp(rtcp_block, packet_information);
527 break;
528 default:
529 ++num_skipped_packets_;
530 break;
531 }
532 break;
533 default:
534 ++num_skipped_packets_;
535 break;
536 }
537 }
538
539 for (const auto& rb : received_blocks) {
540 if (rb.second.sender_report && !rb.second.dlrr) {
541 auto rtt_stats = non_sender_rtts_.find(rb.first);
542 if (rtt_stats != non_sender_rtts_.end()) {
543 rtt_stats->second.Invalidate();
544 }
545 }
546 }
547
548 if (packet_type_counter_observer_) {
549 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
550 local_media_ssrc(), packet_type_counter_);
551 }
552
553 if (num_skipped_packets_ > 0) {
554 const int64_t now_ms = clock_->TimeInMilliseconds();
555 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs) {
556 last_skipped_packets_warning_ms_ = now_ms;
557 RTC_LOG(LS_WARNING)
558 << num_skipped_packets_
559 << " RTCP blocks were skipped due to being malformed or of "
560 "unrecognized/unsupported type, during the past "
561 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
562 }
563 }
564
565 return true;
566 }
567
HandleSenderReport(const CommonHeader & rtcp_block,PacketInformation * packet_information)568 void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
569 PacketInformation* packet_information) {
570 rtcp::SenderReport sender_report;
571 if (!sender_report.Parse(rtcp_block)) {
572 ++num_skipped_packets_;
573 return;
574 }
575
576 const uint32_t remote_ssrc = sender_report.sender_ssrc();
577
578 packet_information->remote_ssrc = remote_ssrc;
579
580 UpdateTmmbrRemoteIsAlive(remote_ssrc);
581
582 // Have I received RTP packets from this party?
583 if (remote_ssrc_ == remote_ssrc) {
584 // Only signal that we have received a SR when we accept one.
585 packet_information->packet_type_flags |= kRtcpSr;
586
587 remote_sender_ntp_time_ = sender_report.ntp();
588 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
589 last_received_sr_ntp_ = clock_->CurrentNtpTime();
590 remote_sender_packet_count_ = sender_report.sender_packet_count();
591 remote_sender_octet_count_ = sender_report.sender_octet_count();
592 remote_sender_reports_count_++;
593 } else {
594 // We will only store the send report from one source, but
595 // we will store all the receive blocks.
596 packet_information->packet_type_flags |= kRtcpRr;
597 }
598
599 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
600 HandleReportBlock(report_block, packet_information, remote_ssrc);
601 }
602
HandleReceiverReport(const CommonHeader & rtcp_block,PacketInformation * packet_information)603 void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
604 PacketInformation* packet_information) {
605 rtcp::ReceiverReport receiver_report;
606 if (!receiver_report.Parse(rtcp_block)) {
607 ++num_skipped_packets_;
608 return;
609 }
610
611 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
612
613 packet_information->remote_ssrc = remote_ssrc;
614
615 UpdateTmmbrRemoteIsAlive(remote_ssrc);
616
617 packet_information->packet_type_flags |= kRtcpRr;
618
619 for (const ReportBlock& report_block : receiver_report.report_blocks())
620 HandleReportBlock(report_block, packet_information, remote_ssrc);
621 }
622
HandleReportBlock(const ReportBlock & report_block,PacketInformation * packet_information,uint32_t remote_ssrc)623 void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
624 PacketInformation* packet_information,
625 uint32_t remote_ssrc) {
626 // This will be called once per report block in the RTCP packet.
627 // We filter out all report blocks that are not for us.
628 // Each packet has max 31 RR blocks.
629 //
630 // We can calc RTT if we send a send report and get a report block back.
631
632 // `report_block.source_ssrc()` is the SSRC identifier of the source to
633 // which the information in this reception report block pertains.
634
635 // Filter out all report blocks that are not for us.
636 if (!registered_ssrcs_.contains(report_block.source_ssrc()))
637 return;
638
639 last_received_rb_ = clock_->CurrentTime();
640
641 ReportBlockData* report_block_data =
642 &received_report_blocks_[report_block.source_ssrc()];
643 RTCPReportBlock rtcp_report_block;
644 rtcp_report_block.sender_ssrc = remote_ssrc;
645 rtcp_report_block.source_ssrc = report_block.source_ssrc();
646 rtcp_report_block.fraction_lost = report_block.fraction_lost();
647 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
648 if (report_block.extended_high_seq_num() >
649 report_block_data->report_block().extended_highest_sequence_number) {
650 // We have successfully delivered new RTP packets to the remote side after
651 // the last RR was sent from the remote side.
652 last_increased_sequence_number_ = last_received_rb_;
653 }
654 rtcp_report_block.extended_highest_sequence_number =
655 report_block.extended_high_seq_num();
656 rtcp_report_block.jitter = report_block.jitter();
657 rtcp_report_block.delay_since_last_sender_report =
658 report_block.delay_since_last_sr();
659 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
660 // Number of seconds since 1900 January 1 00:00 GMT (see
661 // https://tools.ietf.org/html/rfc868).
662 report_block_data->SetReportBlock(
663 rtcp_report_block,
664 (clock_->CurrentNtpInMilliseconds() - rtc::kNtpJan1970Millisecs) *
665 rtc::kNumMicrosecsPerMillisec);
666
667 uint32_t send_time_ntp = report_block.last_sr();
668 // RFC3550, section 6.4.1, LSR field discription states:
669 // If no SR has been received yet, the field is set to zero.
670 // Receiver rtp_rtcp module is not expected to calculate rtt using
671 // Sender Reports even if it accidentally can.
672 if (send_time_ntp != 0) {
673 uint32_t delay_ntp = report_block.delay_since_last_sr();
674 // Local NTP time.
675 uint32_t receive_time_ntp =
676 CompactNtp(clock_->ConvertTimestampToNtpTime(last_received_rb_));
677
678 // RTT in 1/(2^16) seconds.
679 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
680 // Convert to 1/1000 seconds (milliseconds).
681 TimeDelta rtt = CompactNtpRttToTimeDelta(rtt_ntp);
682 report_block_data->AddRoundTripTimeSample(rtt.ms());
683 if (report_block.source_ssrc() == local_media_ssrc()) {
684 rtts_[remote_ssrc].AddRtt(rtt);
685 }
686
687 packet_information->rtt_ms = rtt.ms();
688 }
689
690 packet_information->report_blocks.push_back(
691 report_block_data->report_block());
692 packet_information->report_block_datas.push_back(*report_block_data);
693 }
694
FindOrCreateTmmbrInfo(uint32_t remote_ssrc)695 RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
696 uint32_t remote_ssrc) {
697 // Create or find receive information.
698 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
699 // Update that this remote is alive.
700 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
701 return tmmbr_info;
702 }
703
UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc)704 void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
705 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
706 if (tmmbr_it != tmmbr_infos_.end())
707 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
708 }
709
GetTmmbrInformation(uint32_t remote_ssrc)710 RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
711 uint32_t remote_ssrc) {
712 auto it = tmmbr_infos_.find(remote_ssrc);
713 if (it == tmmbr_infos_.end())
714 return nullptr;
715 return &it->second;
716 }
717
718 // These two methods (RtcpRrTimeout and RtcpRrSequenceNumberTimeout) only exist
719 // for tests and legacy code (rtp_rtcp_impl.cc). We should be able to to delete
720 // the methods and require that access to the locked variables only happens on
721 // the worker thread and thus no locking is needed.
RtcpRrTimeout()722 bool RTCPReceiver::RtcpRrTimeout() {
723 MutexLock lock(&rtcp_receiver_lock_);
724 return RtcpRrTimeoutLocked(clock_->CurrentTime());
725 }
726
RtcpRrSequenceNumberTimeout()727 bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
728 MutexLock lock(&rtcp_receiver_lock_);
729 return RtcpRrSequenceNumberTimeoutLocked(clock_->CurrentTime());
730 }
731
UpdateTmmbrTimers()732 bool RTCPReceiver::UpdateTmmbrTimers() {
733 MutexLock lock(&rtcp_receiver_lock_);
734
735 int64_t now_ms = clock_->TimeInMilliseconds();
736 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
737
738 if (oldest_tmmbr_info_ms_ >= timeout_ms)
739 return false;
740
741 bool update_bounding_set = false;
742 oldest_tmmbr_info_ms_ = -1;
743 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
744 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
745 if (tmmbr_info->last_time_received_ms > 0) {
746 if (tmmbr_info->last_time_received_ms < timeout_ms) {
747 // No rtcp packet for the last 5 regular intervals, reset limitations.
748 tmmbr_info->tmmbr.clear();
749 // Prevent that we call this over and over again.
750 tmmbr_info->last_time_received_ms = 0;
751 // Send new TMMBN to all channels using the default codec.
752 update_bounding_set = true;
753 } else if (oldest_tmmbr_info_ms_ == -1 ||
754 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
755 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
756 }
757 ++tmmbr_it;
758 } else if (tmmbr_info->ready_for_delete) {
759 // When we dont have a last_time_received_ms and the object is marked
760 // ready_for_delete it's removed from the map.
761 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
762 } else {
763 ++tmmbr_it;
764 }
765 }
766 return update_bounding_set;
767 }
768
BoundingSet(bool * tmmbr_owner)769 std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
770 MutexLock lock(&rtcp_receiver_lock_);
771 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
772 if (!tmmbr_info)
773 return std::vector<rtcp::TmmbItem>();
774
775 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, local_media_ssrc());
776 return tmmbr_info->tmmbn;
777 }
778
HandleSdes(const CommonHeader & rtcp_block,PacketInformation * packet_information)779 void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
780 PacketInformation* packet_information) {
781 rtcp::Sdes sdes;
782 if (!sdes.Parse(rtcp_block)) {
783 ++num_skipped_packets_;
784 return;
785 }
786
787 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
788 if (cname_callback_)
789 cname_callback_->OnCname(chunk.ssrc, chunk.cname);
790 }
791 packet_information->packet_type_flags |= kRtcpSdes;
792 }
793
HandleNack(const CommonHeader & rtcp_block,PacketInformation * packet_information)794 void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
795 PacketInformation* packet_information) {
796 rtcp::Nack nack;
797 if (!nack.Parse(rtcp_block)) {
798 ++num_skipped_packets_;
799 return;
800 }
801
802 if (receiver_only_ || local_media_ssrc() != nack.media_ssrc()) // Not to us.
803 return;
804
805 packet_information->nack_sequence_numbers.insert(
806 packet_information->nack_sequence_numbers.end(),
807 nack.packet_ids().begin(), nack.packet_ids().end());
808 for (uint16_t packet_id : nack.packet_ids())
809 nack_stats_.ReportRequest(packet_id);
810
811 if (!nack.packet_ids().empty()) {
812 packet_information->packet_type_flags |= kRtcpNack;
813 ++packet_type_counter_.nack_packets;
814 packet_type_counter_.nack_requests = nack_stats_.requests();
815 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
816 }
817 }
818
HandleApp(const rtcp::CommonHeader & rtcp_block,PacketInformation * packet_information)819 void RTCPReceiver::HandleApp(const rtcp::CommonHeader& rtcp_block,
820 PacketInformation* packet_information) {
821 rtcp::App app;
822 if (app.Parse(rtcp_block)) {
823 if (app.name() == rtcp::RemoteEstimate::kName &&
824 app.sub_type() == rtcp::RemoteEstimate::kSubType) {
825 rtcp::RemoteEstimate estimate(std::move(app));
826 if (estimate.ParseData()) {
827 packet_information->network_state_estimate = estimate.estimate();
828 return;
829 }
830 }
831 }
832 ++num_skipped_packets_;
833 }
834
HandleBye(const CommonHeader & rtcp_block)835 void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
836 rtcp::Bye bye;
837 if (!bye.Parse(rtcp_block)) {
838 ++num_skipped_packets_;
839 return;
840 }
841
842 // Clear our lists.
843 rtts_.erase(bye.sender_ssrc());
844 EraseIf(received_report_blocks_, [&](const auto& elem) {
845 return elem.second.report_block().sender_ssrc == bye.sender_ssrc();
846 });
847
848 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
849 if (tmmbr_info)
850 tmmbr_info->ready_for_delete = true;
851
852 last_fir_.erase(bye.sender_ssrc());
853 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
854 if (it != received_rrtrs_ssrc_it_.end()) {
855 received_rrtrs_.erase(it->second);
856 received_rrtrs_ssrc_it_.erase(it);
857 }
858 xr_rr_rtt_ms_ = 0;
859 }
860
HandleXr(const CommonHeader & rtcp_block,PacketInformation * packet_information,bool & contains_dlrr,uint32_t & ssrc)861 void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
862 PacketInformation* packet_information,
863 bool& contains_dlrr,
864 uint32_t& ssrc) {
865 rtcp::ExtendedReports xr;
866 if (!xr.Parse(rtcp_block)) {
867 ++num_skipped_packets_;
868 return;
869 }
870 ssrc = xr.sender_ssrc();
871 contains_dlrr = !xr.dlrr().sub_blocks().empty();
872
873 if (xr.rrtr())
874 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
875
876 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
877 HandleXrDlrrReportBlock(xr.sender_ssrc(), time_info);
878
879 if (xr.target_bitrate()) {
880 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
881 packet_information);
882 }
883 }
884
HandleXrReceiveReferenceTime(uint32_t sender_ssrc,const rtcp::Rrtr & rrtr)885 void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
886 const rtcp::Rrtr& rrtr) {
887 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
888 uint32_t local_receive_mid_ntp_time = CompactNtp(clock_->CurrentNtpTime());
889
890 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
891 if (it != received_rrtrs_ssrc_it_.end()) {
892 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
893 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
894 } else {
895 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
896 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
897 local_receive_mid_ntp_time);
898 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
899 } else {
900 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
901 << ", reached maximum number of stored RRTRs.";
902 }
903 }
904 }
905
HandleXrDlrrReportBlock(uint32_t sender_ssrc,const rtcp::ReceiveTimeInfo & rti)906 void RTCPReceiver::HandleXrDlrrReportBlock(uint32_t sender_ssrc,
907 const rtcp::ReceiveTimeInfo& rti) {
908 if (!registered_ssrcs_.contains(rti.ssrc)) // Not to us.
909 return;
910
911 // Caller should explicitly enable rtt calculation using extended reports.
912 if (!xr_rrtr_status_)
913 return;
914
915 // The send_time and delay_rr fields are in units of 1/2^16 sec.
916 uint32_t send_time_ntp = rti.last_rr;
917 // RFC3611, section 4.5, LRR field discription states:
918 // If no such block has been received, the field is set to zero.
919 if (send_time_ntp == 0) {
920 auto rtt_stats = non_sender_rtts_.find(sender_ssrc);
921 if (rtt_stats != non_sender_rtts_.end()) {
922 rtt_stats->second.Invalidate();
923 }
924 return;
925 }
926
927 uint32_t delay_ntp = rti.delay_since_last_rr;
928 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
929
930 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
931 TimeDelta rtt = CompactNtpRttToTimeDelta(rtt_ntp);
932 xr_rr_rtt_ms_ = rtt.ms();
933
934 non_sender_rtts_[sender_ssrc].Update(rtt);
935 }
936
HandleXrTargetBitrate(uint32_t ssrc,const rtcp::TargetBitrate & target_bitrate,PacketInformation * packet_information)937 void RTCPReceiver::HandleXrTargetBitrate(
938 uint32_t ssrc,
939 const rtcp::TargetBitrate& target_bitrate,
940 PacketInformation* packet_information) {
941 if (ssrc != remote_ssrc_) {
942 return; // Not for us.
943 }
944
945 VideoBitrateAllocation bitrate_allocation;
946 for (const auto& item : target_bitrate.GetTargetBitrates()) {
947 if (item.spatial_layer >= kMaxSpatialLayers ||
948 item.temporal_layer >= kMaxTemporalStreams) {
949 RTC_LOG(LS_WARNING)
950 << "Invalid layer in XR target bitrate pack: spatial index "
951 << item.spatial_layer << ", temporal index " << item.temporal_layer
952 << ", dropping.";
953 } else {
954 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
955 item.target_bitrate_kbps * 1000);
956 }
957 }
958 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
959 }
960
HandlePli(const CommonHeader & rtcp_block,PacketInformation * packet_information)961 void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
962 PacketInformation* packet_information) {
963 rtcp::Pli pli;
964 if (!pli.Parse(rtcp_block)) {
965 ++num_skipped_packets_;
966 return;
967 }
968
969 if (local_media_ssrc() == pli.media_ssrc()) {
970 ++packet_type_counter_.pli_packets;
971 // Received a signal that we need to send a new key frame.
972 packet_information->packet_type_flags |= kRtcpPli;
973 }
974 }
975
HandleTmmbr(const CommonHeader & rtcp_block,PacketInformation * packet_information)976 void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
977 PacketInformation* packet_information) {
978 rtcp::Tmmbr tmmbr;
979 if (!tmmbr.Parse(rtcp_block)) {
980 ++num_skipped_packets_;
981 return;
982 }
983
984 uint32_t sender_ssrc = tmmbr.sender_ssrc();
985 if (tmmbr.media_ssrc()) {
986 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
987 // In relay mode this is a valid number.
988 sender_ssrc = tmmbr.media_ssrc();
989 }
990
991 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
992 if (local_media_ssrc() != request.ssrc() || request.bitrate_bps() == 0)
993 continue;
994
995 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
996 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
997 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
998 request.packet_overhead());
999 // FindOrCreateTmmbrInfo always sets `last_time_received_ms` to
1000 // `clock_->TimeInMilliseconds()`.
1001 entry->last_updated_ms = tmmbr_info->last_time_received_ms;
1002
1003 packet_information->packet_type_flags |= kRtcpTmmbr;
1004 break;
1005 }
1006 }
1007
HandleTmmbn(const CommonHeader & rtcp_block,PacketInformation * packet_information)1008 void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
1009 PacketInformation* packet_information) {
1010 rtcp::Tmmbn tmmbn;
1011 if (!tmmbn.Parse(rtcp_block)) {
1012 ++num_skipped_packets_;
1013 return;
1014 }
1015
1016 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
1017
1018 packet_information->packet_type_flags |= kRtcpTmmbn;
1019
1020 tmmbr_info->tmmbn = tmmbn.items();
1021 }
1022
HandleSrReq(const CommonHeader & rtcp_block,PacketInformation * packet_information)1023 void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
1024 PacketInformation* packet_information) {
1025 rtcp::RapidResyncRequest sr_req;
1026 if (!sr_req.Parse(rtcp_block)) {
1027 ++num_skipped_packets_;
1028 return;
1029 }
1030
1031 packet_information->packet_type_flags |= kRtcpSrReq;
1032 }
1033
HandlePsfbApp(const CommonHeader & rtcp_block,PacketInformation * packet_information)1034 void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
1035 PacketInformation* packet_information) {
1036 {
1037 rtcp::Remb remb;
1038 if (remb.Parse(rtcp_block)) {
1039 packet_information->packet_type_flags |= kRtcpRemb;
1040 packet_information->receiver_estimated_max_bitrate_bps =
1041 remb.bitrate_bps();
1042 return;
1043 }
1044 }
1045
1046 {
1047 auto loss_notification = std::make_unique<rtcp::LossNotification>();
1048 if (loss_notification->Parse(rtcp_block)) {
1049 packet_information->packet_type_flags |= kRtcpLossNotification;
1050 packet_information->loss_notification = std::move(loss_notification);
1051 return;
1052 }
1053 }
1054
1055 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
1056
1057 ++num_skipped_packets_;
1058 }
1059
HandleFir(const CommonHeader & rtcp_block,PacketInformation * packet_information)1060 void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
1061 PacketInformation* packet_information) {
1062 rtcp::Fir fir;
1063 if (!fir.Parse(rtcp_block)) {
1064 ++num_skipped_packets_;
1065 return;
1066 }
1067
1068 if (fir.requests().empty())
1069 return;
1070
1071 const int64_t now_ms = clock_->TimeInMilliseconds();
1072 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
1073 // Is it our sender that is requested to generate a new keyframe.
1074 if (local_media_ssrc() != fir_request.ssrc)
1075 continue;
1076
1077 ++packet_type_counter_.fir_packets;
1078
1079 auto inserted = last_fir_.insert(std::make_pair(
1080 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
1081 if (!inserted.second) { // There was already an entry.
1082 LastFirStatus* last_fir = &inserted.first->second;
1083
1084 // Check if we have reported this FIRSequenceNumber before.
1085 if (fir_request.seq_nr == last_fir->sequence_number)
1086 continue;
1087
1088 // Sanity: don't go crazy with the callbacks.
1089 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
1090 continue;
1091
1092 last_fir->request_ms = now_ms;
1093 last_fir->sequence_number = fir_request.seq_nr;
1094 }
1095 // Received signal that we need to send a new key frame.
1096 packet_information->packet_type_flags |= kRtcpFir;
1097 }
1098 }
1099
HandleTransportFeedback(const CommonHeader & rtcp_block,PacketInformation * packet_information)1100 void RTCPReceiver::HandleTransportFeedback(
1101 const CommonHeader& rtcp_block,
1102 PacketInformation* packet_information) {
1103 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
1104 new rtcp::TransportFeedback());
1105 if (!transport_feedback->Parse(rtcp_block)) {
1106 ++num_skipped_packets_;
1107 return;
1108 }
1109
1110 packet_information->packet_type_flags |= kRtcpTransportFeedback;
1111 packet_information->transport_feedback = std::move(transport_feedback);
1112 }
1113
NotifyTmmbrUpdated()1114 void RTCPReceiver::NotifyTmmbrUpdated() {
1115 // Find bounding set.
1116 std::vector<rtcp::TmmbItem> bounding =
1117 TMMBRHelp::FindBoundingSet(TmmbrReceived());
1118
1119 if (!bounding.empty() && rtcp_bandwidth_observer_) {
1120 // We have a new bandwidth estimate on this channel.
1121 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
1122 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
1123 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
1124 }
1125
1126 // Send tmmbn to inform remote clients about the new bandwidth.
1127 rtp_rtcp_->SetTmmbn(std::move(bounding));
1128 }
1129
1130 // Holding no Critical section.
TriggerCallbacksFromRtcpPacket(const PacketInformation & packet_information)1131 void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
1132 const PacketInformation& packet_information) {
1133 // Process TMMBR and REMB first to avoid multiple callbacks
1134 // to OnNetworkChanged.
1135 if (packet_information.packet_type_flags & kRtcpTmmbr) {
1136 // Might trigger a OnReceivedBandwidthEstimateUpdate.
1137 NotifyTmmbrUpdated();
1138 }
1139
1140 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
1141 rtp_rtcp_->OnRequestSendReport();
1142 }
1143 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1144 if (!packet_information.nack_sequence_numbers.empty()) {
1145 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1146 << packet_information.nack_sequence_numbers.size();
1147 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
1148 }
1149 }
1150
1151 // We need feedback that we have received a report block(s) so that we
1152 // can generate a new packet in a conference relay scenario, one received
1153 // report can generate several RTCP packets, based on number relayed/mixed
1154 // a send report block should go out to all receivers.
1155 if (rtcp_intra_frame_observer_) {
1156 RTC_DCHECK(!receiver_only_);
1157 if ((packet_information.packet_type_flags & kRtcpPli) ||
1158 (packet_information.packet_type_flags & kRtcpFir)) {
1159 if (packet_information.packet_type_flags & kRtcpPli) {
1160 RTC_LOG(LS_VERBOSE)
1161 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
1162 } else {
1163 RTC_LOG(LS_VERBOSE)
1164 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
1165 }
1166 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(
1167 local_media_ssrc());
1168 }
1169 }
1170 if (rtcp_loss_notification_observer_ &&
1171 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1172 rtcp::LossNotification* loss_notification =
1173 packet_information.loss_notification.get();
1174 RTC_DCHECK(loss_notification);
1175 if (loss_notification->media_ssrc() == local_media_ssrc()) {
1176 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1177 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1178 loss_notification->last_received(),
1179 loss_notification->decodability_flag());
1180 }
1181 }
1182 if (rtcp_bandwidth_observer_) {
1183 RTC_DCHECK(!receiver_only_);
1184 if (packet_information.packet_type_flags & kRtcpRemb) {
1185 RTC_LOG(LS_VERBOSE)
1186 << "Incoming REMB: "
1187 << packet_information.receiver_estimated_max_bitrate_bps;
1188 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1189 packet_information.receiver_estimated_max_bitrate_bps);
1190 }
1191 if ((packet_information.packet_type_flags & kRtcpSr) ||
1192 (packet_information.packet_type_flags & kRtcpRr)) {
1193 int64_t now_ms = clock_->TimeInMilliseconds();
1194 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
1195 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
1196 }
1197 }
1198 if ((packet_information.packet_type_flags & kRtcpSr) ||
1199 (packet_information.packet_type_flags & kRtcpRr)) {
1200 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
1201 }
1202
1203 if (transport_feedback_observer_ &&
1204 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1205 uint32_t media_source_ssrc =
1206 packet_information.transport_feedback->media_ssrc();
1207 if (media_source_ssrc == local_media_ssrc() ||
1208 registered_ssrcs_.contains(media_source_ssrc)) {
1209 transport_feedback_observer_->OnTransportFeedback(
1210 *packet_information.transport_feedback);
1211 }
1212 }
1213
1214 if (network_state_estimate_observer_ &&
1215 packet_information.network_state_estimate) {
1216 network_state_estimate_observer_->OnRemoteNetworkEstimate(
1217 *packet_information.network_state_estimate);
1218 }
1219
1220 if (bitrate_allocation_observer_ &&
1221 packet_information.target_bitrate_allocation) {
1222 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1223 *packet_information.target_bitrate_allocation);
1224 }
1225
1226 if (!receiver_only_) {
1227 if (report_block_data_observer_) {
1228 for (const auto& report_block_data :
1229 packet_information.report_block_datas) {
1230 report_block_data_observer_->OnReportBlockDataUpdated(
1231 report_block_data);
1232 }
1233 }
1234 }
1235 }
1236
TmmbrReceived()1237 std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
1238 MutexLock lock(&rtcp_receiver_lock_);
1239 std::vector<rtcp::TmmbItem> candidates;
1240
1241 int64_t now_ms = clock_->TimeInMilliseconds();
1242 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
1243
1244 for (auto& kv : tmmbr_infos_) {
1245 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
1246 if (it->second.last_updated_ms < timeout_ms) {
1247 // Erase timeout entries.
1248 it = kv.second.tmmbr.erase(it);
1249 } else {
1250 candidates.push_back(it->second.tmmbr_item);
1251 ++it;
1252 }
1253 }
1254 }
1255 return candidates;
1256 }
1257
RtcpRrTimeoutLocked(Timestamp now)1258 bool RTCPReceiver::RtcpRrTimeoutLocked(Timestamp now) {
1259 return ResetTimestampIfExpired(now, last_received_rb_, report_interval_);
1260 }
1261
RtcpRrSequenceNumberTimeoutLocked(Timestamp now)1262 bool RTCPReceiver::RtcpRrSequenceNumberTimeoutLocked(Timestamp now) {
1263 return ResetTimestampIfExpired(now, last_increased_sequence_number_,
1264 report_interval_);
1265 }
1266
1267 } // namespace webrtc
1268