blob: f06fd1c36117234190da2e5a4aeed5588b5c38c0 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Peter Boströmfe7a80c2015-04-23 17:53:17 +020013#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
danilchap2f69ce92016-08-16 03:21:38 -070015#include <limits>
danilchap7851bda2016-09-29 15:28:07 -070016#include <map>
danilchap92ea6012016-09-23 10:36:03 -070017#include <memory>
18#include <utility>
danilchap7851bda2016-09-29 15:28:07 -070019#include <vector>
danilchap2f69ce92016-08-16 03:21:38 -070020
Erik Språngeeaa8f92018-05-17 12:35:56 +020021#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070022#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
Elad Alonf8e7ccb2019-01-24 12:38:36 +010028#include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
Sebastian Janssone1795f42019-07-24 11:38:03 +020034#include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
39#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
40#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
41#include "modules/rtp_rtcp/source/time_util.h"
42#include "modules/rtp_rtcp/source/tmmbr_help.h"
43#include "rtc_base/checks.h"
44#include "rtc_base/logging.h"
Henrik Boströmf2047872019-05-16 13:32:20 +020045#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/trace_event.h"
47#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000048
niklase@google.com470e71d2011-07-07 08:21:25 +000049namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070050namespace {
51
52using rtcp::CommonHeader;
53using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000054
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000055// The number of RTCP time intervals needed to trigger a timeout.
56const int kRrTimeoutIntervals = 3;
57
Jiawei Ou3587b832018-01-31 22:08:26 -080058const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
59
Erik Språng6b8d3552015-09-24 15:06:57 +020060const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080061const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020062
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020063// Maximum number of received RRTRs that will be stored.
64const size_t kMaxNumberOfStoredRrtrs = 200;
65
Mirko Bonadei3b676722019-07-12 17:35:05 +000066constexpr int32_t kDefaultVideoReportInterval = 1000;
67constexpr int32_t kDefaultAudioReportInterval = 5000;
danilchap1b1863a2016-09-20 01:39:54 -070068} // namespace
69
danilchap92ea6012016-09-23 10:36:03 -070070struct RTCPReceiver::PacketInformation {
71 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
72
73 uint32_t remote_ssrc = 0;
74 std::vector<uint16_t> nack_sequence_numbers;
Henrik Boströmf2047872019-05-16 13:32:20 +020075 // TODO(hbos): Remove |report_blocks| in favor of |report_block_datas|.
danilchap92ea6012016-09-23 10:36:03 -070076 ReportBlockList report_blocks;
Henrik Boströmf2047872019-05-16 13:32:20 +020077 std::vector<ReportBlockData> report_block_datas;
danilchap92ea6012016-09-23 10:36:03 -070078 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070079 uint32_t receiver_estimated_max_bitrate_bps = 0;
80 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020081 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Sebastian Janssone1795f42019-07-24 11:38:03 +020082 absl::optional<NetworkStateEstimate> network_state_estimate;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010083 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070084};
85
danilchap9bf610e2017-02-20 06:03:01 -080086// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
87struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070088 struct TimedTmmbrItem {
89 rtcp::TmmbItem tmmbr_item;
90 int64_t last_updated_ms;
91 };
92
93 int64_t last_time_received_ms = 0;
94
danilchap7851bda2016-09-29 15:28:07 -070095 bool ready_for_delete = false;
96
97 std::vector<rtcp::TmmbItem> tmmbn;
98 std::map<uint32_t, TimedTmmbrItem> tmmbr;
99};
100
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200101// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
102struct RTCPReceiver::RrtrInformation {
103 RrtrInformation(uint32_t ssrc,
104 uint32_t received_remote_mid_ntp_time,
105 uint32_t local_receive_mid_ntp_time)
106 : ssrc(ssrc),
107 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
108 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
109
110 uint32_t ssrc;
111 // Received NTP timestamp in compact representation.
112 uint32_t received_remote_mid_ntp_time;
113 // NTP time when the report was received in compact representation.
114 uint32_t local_receive_mid_ntp_time;
115};
116
danilchapefa966b2017-02-17 06:23:15 -0800117struct RTCPReceiver::LastFirStatus {
118 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
119 : request_ms(now_ms), sequence_number(sequence_number) {}
120 int64_t request_ms;
121 uint8_t sequence_number;
122};
123
Mirko Bonadei3b676722019-07-12 17:35:05 +0000124RTCPReceiver::RTCPReceiver(const RtpRtcp::Configuration& config,
125 ModuleRtpRtcp* owner)
126 : clock_(config.clock),
127 receiver_only_(config.receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800128 rtp_rtcp_(owner),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000129 rtcp_bandwidth_observer_(config.bandwidth_callback),
130 rtcp_intra_frame_observer_(config.intra_frame_callback),
131 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
Sebastian Janssone1795f42019-07-24 11:38:03 +0200132 network_state_estimate_observer_(config.network_state_estimate_observer),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000133 transport_feedback_observer_(config.transport_feedback_callback),
134 bitrate_allocation_observer_(config.bitrate_allocation_observer),
135 report_interval_ms_(config.rtcp_report_interval_ms > 0
136 ? config.rtcp_report_interval_ms
137 : (config.audio ? kDefaultAudioReportInterval
138 : kDefaultVideoReportInterval)),
Erik Språng54d5d2c2019-08-20 17:22:36 +0200139 // TODO(bugs.webrtc.org/10774): Remove fallback.
Erik Språngc15f92a2019-08-21 15:54:16 +0200140 main_ssrc_(config.local_media_ssrc.value_or(0)),
danilchap8bab7962016-12-20 02:46:46 -0800141 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700142 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100143 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000144 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700145 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200146 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800147 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800148 stats_callback_(nullptr),
Niels Möller4d7c4052019-08-05 12:45:19 +0200149 cname_callback_(nullptr),
Henrik Boströmf2047872019-05-16 13:32:20 +0200150 report_block_data_observer_(nullptr),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000151 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Erik Språng6b8d3552015-09-24 15:06:57 +0200152 num_skipped_packets_(0),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000153 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
danilchap8bab7962016-12-20 02:46:46 -0800154 RTC_DCHECK(owner);
Erik Språngc15f92a2019-08-21 15:54:16 +0200155 if (config.local_media_ssrc) {
156 registered_ssrcs_.insert(*config.local_media_ssrc);
Mirko Bonadei3b676722019-07-12 17:35:05 +0000157 }
158 if (config.rtx_send_ssrc) {
159 registered_ssrcs_.insert(*config.rtx_send_ssrc);
160 }
161 if (config.flexfec_sender) {
162 registered_ssrcs_.insert(config.flexfec_sender->ssrc());
163 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000164}
165
danilchap28b03eb2016-10-05 06:59:44 -0700166RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
nisse479d3d72017-09-13 07:53:37 -0700168void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700169 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100170 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700171 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700172 }
danilchap59cb2bd2016-08-29 11:08:47 -0700173
danilchap92ea6012016-09-23 10:36:03 -0700174 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700175 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700176 return;
danilchap8bab7962016-12-20 02:46:46 -0800177 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200180int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800181 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200182 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000183}
184
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000185void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800186 rtc::CritScope lock(&rtcp_receiver_lock_);
187 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700188 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800189 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000192uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800193 rtc::CritScope lock(&rtcp_receiver_lock_);
194 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000195}
196
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000197void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
198 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800199 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700200 main_ssrc_ = main_ssrc;
201 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202}
203
danilchap28b03eb2016-10-05 06:59:44 -0700204int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
205 int64_t* last_rtt_ms,
206 int64_t* avg_rtt_ms,
207 int64_t* min_rtt_ms,
208 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800209 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
danilchap28b03eb2016-10-05 06:59:44 -0700211 auto it = received_report_blocks_.find(main_ssrc_);
212 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000213 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700214
215 auto it_info = it->second.find(remote_ssrc);
216 if (it_info == it->second.end())
217 return -1;
218
Henrik Boströmf2047872019-05-16 13:32:20 +0200219 const ReportBlockData* report_block_data = &it_info->second;
danilchap28b03eb2016-10-05 06:59:44 -0700220
Henrik Boströmf2047872019-05-16 13:32:20 +0200221 if (report_block_data->num_rtts() == 0)
danilchap28b03eb2016-10-05 06:59:44 -0700222 return -1;
223
224 if (last_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200225 *last_rtt_ms = report_block_data->last_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700226
Henrik Boströmf2047872019-05-16 13:32:20 +0200227 if (avg_rtt_ms) {
228 *avg_rtt_ms =
229 report_block_data->sum_rtt_ms() / report_block_data->num_rtts();
230 }
danilchap28b03eb2016-10-05 06:59:44 -0700231
232 if (min_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200233 *min_rtt_ms = report_block_data->min_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700234
235 if (max_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200236 *max_rtt_ms = report_block_data->max_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700237
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000238 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000239}
240
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100241void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800242 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100243 xr_rrtr_status_ = enable;
244}
245
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000246bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800247 RTC_DCHECK(rtt_ms);
248 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000249 if (xr_rr_rtt_ms_ == 0) {
250 return false;
251 }
252 *rtt_ms = xr_rr_rtt_ms_;
253 xr_rr_rtt_ms_ = 0;
254 return true;
255}
256
danilchap8bab7962016-12-20 02:46:46 -0800257bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
258 uint32_t* received_ntp_frac,
259 uint32_t* rtcp_arrival_time_secs,
260 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700261 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800262 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700263 if (!last_received_sr_ntp_.Valid())
264 return false;
265
266 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800267 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700268 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800269 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700270 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700271
272 // Rtp time from incoming SenderReport.
273 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700274 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700275
276 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800277 if (rtcp_arrival_time_secs)
278 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
279 if (rtcp_arrival_time_frac)
280 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700281
danilchapda161d72016-08-19 07:29:46 -0700282 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200285std::vector<rtcp::ReceiveTimeInfo>
286RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800287 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000288
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200289 const size_t last_xr_rtis_size = std::min(
290 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
291 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
292 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000293
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200294 const uint32_t now_ntp =
295 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000296
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200297 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
298 RrtrInformation& rrtr = received_rrtrs_.front();
299 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
300 now_ntp - rrtr.local_receive_mid_ntp_time);
301 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
302 received_rrtrs_.pop_front();
303 }
304
305 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000306}
307
danilchap28b03eb2016-10-05 06:59:44 -0700308// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000309int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700310 std::vector<RTCPReportBlock>* receive_blocks) const {
311 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800312 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700313 for (const auto& reports_per_receiver : received_report_blocks_)
314 for (const auto& report : reports_per_receiver.second)
Henrik Boströmf2047872019-05-16 13:32:20 +0200315 receive_blocks->push_back(report.second.report_block());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
Henrik Boströmf2047872019-05-16 13:32:20 +0200319std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
320 std::vector<ReportBlockData> result;
321 rtc::CritScope lock(&rtcp_receiver_lock_);
322 for (const auto& reports_per_receiver : received_report_blocks_)
323 for (const auto& report : reports_per_receiver.second)
324 result.push_back(report.second);
325 return result;
326}
327
danilchap92ea6012016-09-23 10:36:03 -0700328bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
329 const uint8_t* packet_end,
330 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800331 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000332
danilchap1b1863a2016-09-20 01:39:54 -0700333 CommonHeader rtcp_block;
334 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
335 next_block = rtcp_block.NextPacket()) {
336 ptrdiff_t remaining_blocks_size = packet_end - next_block;
337 RTC_DCHECK_GT(remaining_blocks_size, 0);
338 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
339 if (next_block == packet_begin) {
340 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100341 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700342 return false;
343 }
344 ++num_skipped_packets_;
345 break;
346 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
danilchap1b1863a2016-09-20 01:39:54 -0700348 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800349 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700350
danilchap1b1863a2016-09-20 01:39:54 -0700351 switch (rtcp_block.type()) {
352 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700353 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200354 break;
danilchap1b1863a2016-09-20 01:39:54 -0700355 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700356 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700357 break;
danilchap1b1863a2016-09-20 01:39:54 -0700358 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800359 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700360 break;
danilchap1b1863a2016-09-20 01:39:54 -0700361 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700362 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700363 break;
danilchap1b1863a2016-09-20 01:39:54 -0700364 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800365 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700366 break;
Sebastian Janssone1795f42019-07-24 11:38:03 +0200367 case rtcp::App::kPacketType:
368 HandleApp(rtcp_block, packet_information);
369 break;
danilchap1b1863a2016-09-20 01:39:54 -0700370 case rtcp::Rtpfb::kPacketType:
371 switch (rtcp_block.fmt()) {
372 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800373 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700374 break;
375 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800376 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700377 break;
378 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800379 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700380 break;
381 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800382 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700383 break;
384 case rtcp::TransportFeedback::kFeedbackMessageType:
385 HandleTransportFeedback(rtcp_block, packet_information);
386 break;
387 default:
388 ++num_skipped_packets_;
389 break;
390 }
danilchapda161d72016-08-19 07:29:46 -0700391 break;
danilchap1b1863a2016-09-20 01:39:54 -0700392 case rtcp::Psfb::kPacketType:
393 switch (rtcp_block.fmt()) {
394 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800395 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700396 break;
danilchap1b1863a2016-09-20 01:39:54 -0700397 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800398 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700399 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100400 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700401 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700402 break;
403 default:
404 ++num_skipped_packets_;
405 break;
406 }
danilchapda161d72016-08-19 07:29:46 -0700407 break;
408 default:
danilchap1b1863a2016-09-20 01:39:54 -0700409 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700410 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000411 }
danilchapda161d72016-08-19 07:29:46 -0700412 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000413
danilchap8bab7962016-12-20 02:46:46 -0800414 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700415 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
416 main_ssrc_, packet_type_counter_);
417 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000418
danilchap8bab7962016-12-20 02:46:46 -0800419 int64_t now_ms = clock_->TimeInMilliseconds();
420 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700421 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800422 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100423 RTC_LOG(LS_WARNING)
424 << num_skipped_packets_
425 << " RTCP blocks were skipped due to being malformed or of "
426 "unrecognized/unsupported type, during the past "
427 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700428 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200429
danilchap1b1863a2016-09-20 01:39:54 -0700430 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000431}
432
danilchap92ea6012016-09-23 10:36:03 -0700433void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
434 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700435 rtcp::SenderReport sender_report;
436 if (!sender_report.Parse(rtcp_block)) {
437 ++num_skipped_packets_;
438 return;
439 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000440
danilchap8bab7962016-12-20 02:46:46 -0800441 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000442
danilchap8bab7962016-12-20 02:46:46 -0800443 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
danilchapec067e92017-02-21 05:38:19 -0800445 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700446
Danil Chapovalov91511f12016-09-15 16:24:02 +0200447 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800448 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200449 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700450 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700451
danilchapa04d9c32017-07-25 04:03:39 -0700452 remote_sender_ntp_time_ = sender_report.ntp();
453 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200454 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700455 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200456 // We will only store the send report from one source, but
457 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700458 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700459 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000460
Mirko Bonadei739baf02019-01-27 17:29:42 +0100461 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800462 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
danilchap92ea6012016-09-23 10:36:03 -0700465void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
466 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700467 rtcp::ReceiverReport receiver_report;
468 if (!receiver_report.Parse(rtcp_block)) {
469 ++num_skipped_packets_;
470 return;
471 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200472
danilchap8bab7962016-12-20 02:46:46 -0800473 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200474
danilchap8bab7962016-12-20 02:46:46 -0800475 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200476
danilchapec067e92017-02-21 05:38:19 -0800477 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200478
danilchap92ea6012016-09-23 10:36:03 -0700479 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200480
danilchap1b1863a2016-09-20 01:39:54 -0700481 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800482 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200483}
484
danilchap92ea6012016-09-23 10:36:03 -0700485void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
486 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700487 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000488 // This will be called once per report block in the RTCP packet.
489 // We filter out all report blocks that are not for us.
490 // Each packet has max 31 RR blocks.
491 //
492 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
danilchap28b03eb2016-10-05 06:59:44 -0700494 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000495 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000496
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000497 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700498 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000499 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000500
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200501 last_received_rb_ms_ = clock_->TimeInMilliseconds();
502
Henrik Boströmf2047872019-05-16 13:32:20 +0200503 ReportBlockData* report_block_data =
danilchap28b03eb2016-10-05 06:59:44 -0700504 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
Henrik Boströmf2047872019-05-16 13:32:20 +0200505 RTCPReportBlock rtcp_report_block;
506 rtcp_report_block.sender_ssrc = remote_ssrc;
507 rtcp_report_block.source_ssrc = report_block.source_ssrc();
508 rtcp_report_block.fraction_lost = report_block.fraction_lost();
509 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700510 if (report_block.extended_high_seq_num() >
Henrik Boströmf2047872019-05-16 13:32:20 +0200511 report_block_data->report_block().extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000512 // We have successfully delivered new RTP packets to the remote side after
513 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800514 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000515 }
Henrik Boströmf2047872019-05-16 13:32:20 +0200516 rtcp_report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700517 report_block.extended_high_seq_num();
Henrik Boströmf2047872019-05-16 13:32:20 +0200518 rtcp_report_block.jitter = report_block.jitter();
519 rtcp_report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700520 report_block.delay_since_last_sr();
Henrik Boströmf2047872019-05-16 13:32:20 +0200521 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
522 report_block_data->SetReportBlock(rtcp_report_block, rtc::TimeUTCMicros());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000523
danilchap28b03eb2016-10-05 06:59:44 -0700524 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800525 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100526 // RFC3550, section 6.4.1, LSR field discription states:
527 // If no SR has been received yet, the field is set to zero.
528 // Receiver rtp_rtcp module is not expected to calculate rtt using
529 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100530
531 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
532 // is false. However, that currently breaks the tests of the
533 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
534 // delete all dependencies on RTT measurements for audio receive streams, or
535 // ensure that audio receive streams that need RTT and stats that depend on it
536 // are configured with an associated audio send stream.
537 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800538 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100539 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200540 uint32_t receive_time_ntp =
541 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000542
Danil Chapovalova094fd12016-02-22 18:59:36 +0100543 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800544 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100545 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700546 rtt_ms = CompactNtpRttToMs(rtt_ntp);
Henrik Boströmf2047872019-05-16 13:32:20 +0200547 report_block_data->AddRoundTripTimeSample(rtt_ms);
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100548
549 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000550 }
551
Henrik Boströmf2047872019-05-16 13:32:20 +0200552 packet_information->report_blocks.push_back(
553 report_block_data->report_block());
554 packet_information->report_block_datas.push_back(*report_block_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000555}
556
danilchapec067e92017-02-21 05:38:19 -0800557RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
558 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700559 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800560 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700561 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800562 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800563 return tmmbr_info;
564}
565
566void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
567 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
568 if (tmmbr_it != tmmbr_infos_.end())
569 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000570}
571
danilchap9bf610e2017-02-20 06:03:01 -0800572RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700573 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800574 auto it = tmmbr_infos_.find(remote_ssrc);
575 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700576 return nullptr;
577 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000578}
579
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800580bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800581 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200582 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000583 return false;
584
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800585 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200586 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000587 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200588 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000589 return true;
590 }
591 return false;
592}
593
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800594bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800595 rtc::CritScope lock(&rtcp_receiver_lock_);
596 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000597 return false;
598
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800599 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800600 if (clock_->TimeInMilliseconds() >
601 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000602 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800603 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000604 return true;
605 }
606 return false;
607}
608
danilchap9bf610e2017-02-20 06:03:01 -0800609bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800610 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
danilchap8bab7962016-12-20 02:46:46 -0800612 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800613 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000614
danilchap9bf610e2017-02-20 06:03:01 -0800615 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800616 return false;
617
618 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800619 oldest_tmmbr_info_ms_ = -1;
620 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
621 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
622 if (tmmbr_info->last_time_received_ms > 0) {
623 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700624 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800625 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700626 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800627 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700628 // Send new TMMBN to all channels using the default codec.
629 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800630 } else if (oldest_tmmbr_info_ms_ == -1 ||
631 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
632 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000633 }
danilchap9bf610e2017-02-20 06:03:01 -0800634 ++tmmbr_it;
635 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700636 // When we dont have a last_time_received_ms and the object is marked
637 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800638 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000639 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800640 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000641 }
642 }
danilchap7851bda2016-09-29 15:28:07 -0700643 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000644}
645
danilchap2b616392016-08-18 06:17:42 -0700646std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800647 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800648 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
649 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700650 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700651
danilchap9bf610e2017-02-20 06:03:01 -0800652 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
653 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
655
danilchap8bab7962016-12-20 02:46:46 -0800656void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700657 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700658 rtcp::Sdes sdes;
659 if (!sdes.Parse(rtcp_block)) {
660 ++num_skipped_packets_;
661 return;
662 }
663
664 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700665 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200666 {
spranga790d832016-12-02 07:29:44 -0800667 rtc::CritScope lock(&feedbacks_lock_);
Niels Möller4d7c4052019-08-05 12:45:19 +0200668 if (cname_callback_)
669 cname_callback_->OnCname(chunk.ssrc, chunk.cname);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200670 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000671 }
danilchap92ea6012016-09-23 10:36:03 -0700672 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000673}
674
danilchap8bab7962016-12-20 02:46:46 -0800675void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700676 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700677 rtcp::Nack nack;
678 if (!nack.Parse(rtcp_block)) {
679 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000680 return;
681 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000682
danilchap1b1863a2016-09-20 01:39:54 -0700683 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
684 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200685
danilchap142f0192016-10-20 08:22:42 -0700686 packet_information->nack_sequence_numbers.insert(
687 packet_information->nack_sequence_numbers.end(),
688 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700689 for (uint16_t packet_id : nack.packet_ids())
690 nack_stats_.ReportRequest(packet_id);
691
692 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700693 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000694 ++packet_type_counter_.nack_packets;
695 packet_type_counter_.nack_requests = nack_stats_.requests();
696 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
697 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
Sebastian Janssone1795f42019-07-24 11:38:03 +0200700void RTCPReceiver::HandleApp(const rtcp::CommonHeader& rtcp_block,
701 PacketInformation* packet_information) {
Sebastian Janssona72d5832019-07-25 09:50:48 +0200702 rtcp::App app;
703 if (app.Parse(rtcp_block)) {
704 if (app.name() == rtcp::RemoteEstimate::kName &&
705 app.sub_type() == rtcp::RemoteEstimate::kSubType) {
706 rtcp::RemoteEstimate estimate(std::move(app));
707 if (estimate.ParseData()) {
708 packet_information->network_state_estimate = estimate.estimate();
709 return;
710 }
Sebastian Janssone1795f42019-07-24 11:38:03 +0200711 }
712 }
713 ++num_skipped_packets_;
714}
715
danilchap8bab7962016-12-20 02:46:46 -0800716void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700717 rtcp::Bye bye;
718 if (!bye.Parse(rtcp_block)) {
719 ++num_skipped_packets_;
720 return;
721 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
danilchap8bab7962016-12-20 02:46:46 -0800723 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700724 for (auto& reports_per_receiver : received_report_blocks_)
725 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000726
danilchap9bf610e2017-02-20 06:03:01 -0800727 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
728 if (tmmbr_info)
729 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000730
danilchapefa966b2017-02-17 06:23:15 -0800731 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700732 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200733 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
734 if (it != received_rrtrs_ssrc_it_.end()) {
735 received_rrtrs_.erase(it->second);
736 received_rrtrs_ssrc_it_.erase(it);
737 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000738 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000739}
740
danilchap1b1863a2016-09-20 01:39:54 -0700741void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700742 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700743 rtcp::ExtendedReports xr;
744 if (!xr.Parse(rtcp_block)) {
745 ++num_skipped_packets_;
746 return;
747 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000748
danilchap80ac24d2016-10-31 08:40:47 -0700749 if (xr.rrtr())
750 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000751
danilchap80ac24d2016-10-31 08:40:47 -0700752 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
753 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800754
sprangb32aaf92017-08-28 05:49:12 -0700755 if (xr.target_bitrate()) {
756 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
757 packet_information);
758 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000759}
760
danilchap8bab7962016-12-20 02:46:46 -0800761void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
762 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200763 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200764 uint32_t local_receive_mid_ntp_time =
765 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200766
767 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
768 if (it != received_rrtrs_ssrc_it_.end()) {
769 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
770 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
771 } else {
772 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
773 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
774 local_receive_mid_ntp_time);
775 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
776 } else {
777 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
778 << ", reached maximum number of stored RRTRs.";
779 }
780 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000781}
782
danilchap92ea6012016-09-23 10:36:03 -0700783void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700784 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
785 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000786
danilchap1b1863a2016-09-20 01:39:54 -0700787 // Caller should explicitly enable rtt calculation using extended reports.
788 if (!xr_rrtr_status_)
789 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200790
danilchap1b1863a2016-09-20 01:39:54 -0700791 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800792 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700793 // RFC3611, section 4.5, LRR field discription states:
794 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800795 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700796 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200797
danilchap8bab7962016-12-20 02:46:46 -0800798 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200799 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200800
danilchap8bab7962016-12-20 02:46:46 -0800801 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700802 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000803}
804
spranga790d832016-12-02 07:29:44 -0800805void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700806 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800807 const rtcp::TargetBitrate& target_bitrate,
808 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700809 if (ssrc != remote_ssrc_) {
810 return; // Not for us.
811 }
812
Erik Språng566124a2018-04-23 12:32:22 +0200813 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800814 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800815 if (item.spatial_layer >= kMaxSpatialLayers ||
816 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100817 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800818 << "Invalid layer in XR target bitrate pack: spatial index "
819 << item.spatial_layer << ", temporal index " << item.temporal_layer
820 << ", dropping.";
821 } else {
822 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
823 item.target_bitrate_kbps * 1000);
824 }
spranga790d832016-12-02 07:29:44 -0800825 }
826 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
827}
828
danilchap8bab7962016-12-20 02:46:46 -0800829void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700830 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700831 rtcp::Pli pli;
832 if (!pli.Parse(rtcp_block)) {
833 ++num_skipped_packets_;
834 return;
835 }
836
837 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000838 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000839 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700840 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000841 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000842}
843
danilchap8bab7962016-12-20 02:46:46 -0800844void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700845 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700846 rtcp::Tmmbr tmmbr;
847 if (!tmmbr.Parse(rtcp_block)) {
848 ++num_skipped_packets_;
849 return;
850 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000851
danilchap7851bda2016-09-29 15:28:07 -0700852 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700853 if (tmmbr.media_ssrc()) {
854 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
855 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700856 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000857 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000858
danilchap1b1863a2016-09-20 01:39:54 -0700859 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800860 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
861 continue;
danilchap7851bda2016-09-29 15:28:07 -0700862
danilchapec067e92017-02-21 05:38:19 -0800863 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
864 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200865 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800866 request.packet_overhead());
867 entry->last_updated_ms = clock_->TimeInMilliseconds();
868
869 packet_information->packet_type_flags |= kRtcpTmmbr;
870 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000871 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000872}
873
danilchap8bab7962016-12-20 02:46:46 -0800874void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700875 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700876 rtcp::Tmmbn tmmbn;
877 if (!tmmbn.Parse(rtcp_block)) {
878 ++num_skipped_packets_;
879 return;
880 }
881
danilchapec067e92017-02-21 05:38:19 -0800882 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700883
danilchap92ea6012016-09-23 10:36:03 -0700884 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000885
danilchapec067e92017-02-21 05:38:19 -0800886 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700887}
888
danilchap8bab7962016-12-20 02:46:46 -0800889void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
890 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700891 rtcp::RapidResyncRequest sr_req;
892 if (!sr_req.Parse(rtcp_block)) {
893 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000894 return;
895 }
896
danilchap92ea6012016-09-23 10:36:03 -0700897 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000898}
899
danilchap1b1863a2016-09-20 01:39:54 -0700900void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700901 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100902 {
903 rtcp::Remb remb;
904 if (remb.Parse(rtcp_block)) {
905 packet_information->packet_type_flags |= kRtcpRemb;
906 packet_information->receiver_estimated_max_bitrate_bps =
907 remb.bitrate_bps();
908 return;
909 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000910 }
danilchap1b1863a2016-09-20 01:39:54 -0700911
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100912 {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200913 auto loss_notification = std::make_unique<rtcp::LossNotification>();
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100914 if (loss_notification->Parse(rtcp_block)) {
915 packet_information->packet_type_flags |= kRtcpLossNotification;
916 packet_information->loss_notification = std::move(loss_notification);
917 return;
918 }
919 }
920
921 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
922
danilchap1b1863a2016-09-20 01:39:54 -0700923 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000924}
925
danilchap8bab7962016-12-20 02:46:46 -0800926void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700927 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700928 rtcp::Fir fir;
929 if (!fir.Parse(rtcp_block)) {
930 ++num_skipped_packets_;
931 return;
932 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000933
danilchap1b1863a2016-09-20 01:39:54 -0700934 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800935 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700936 if (main_ssrc_ != fir_request.ssrc)
937 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200938
939 ++packet_type_counter_.fir_packets;
940
danilchapefa966b2017-02-17 06:23:15 -0800941 int64_t now_ms = clock_->TimeInMilliseconds();
942 auto inserted = last_fir_.insert(std::make_pair(
943 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
944 if (!inserted.second) { // There was already an entry.
945 LastFirStatus* last_fir = &inserted.first->second;
946
danilchap7851bda2016-09-29 15:28:07 -0700947 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800948 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700949 continue;
950
danilchap7851bda2016-09-29 15:28:07 -0700951 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800952 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700953 continue;
954
danilchapefa966b2017-02-17 06:23:15 -0800955 last_fir->request_ms = now_ms;
956 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200957 }
danilchap7851bda2016-09-29 15:28:07 -0700958 // Received signal that we need to send a new key frame.
959 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000960 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000961}
962
Erik Språng6b8d3552015-09-24 15:06:57 +0200963void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700964 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700965 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700966 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
967 new rtcp::TransportFeedback());
968 if (!transport_feedback->Parse(rtcp_block)) {
969 ++num_skipped_packets_;
970 return;
971 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200972
danilchap92ea6012016-09-23 10:36:03 -0700973 packet_information->packet_type_flags |= kRtcpTransportFeedback;
974 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200975}
danilchap287e5482016-08-16 15:15:39 -0700976
danilchap9bf610e2017-02-20 06:03:01 -0800977void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700978 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700979 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700980 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000981
spranga790d832016-12-02 07:29:44 -0800982 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700983 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700984 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
985 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800986 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000987 }
danilchap853ecb22016-08-22 08:26:15 -0700988
danilchap9bf610e2017-02-20 06:03:01 -0800989 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800990 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000991}
992
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000993void RTCPReceiver::RegisterRtcpStatisticsCallback(
994 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800995 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000996 stats_callback_ = callback;
997}
998
999RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -08001000 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001001 return stats_callback_;
1002}
1003
Niels Möller4d7c4052019-08-05 12:45:19 +02001004void RTCPReceiver::RegisterRtcpCnameCallback(RtcpCnameCallback* callback) {
1005 rtc::CritScope cs(&feedbacks_lock_);
1006 cname_callback_ = callback;
1007}
1008
Henrik Boströmf2047872019-05-16 13:32:20 +02001009void RTCPReceiver::SetReportBlockDataObserver(
1010 ReportBlockDataObserver* observer) {
1011 rtc::CritScope cs(&feedbacks_lock_);
1012 report_block_data_observer_ = observer;
1013}
1014
danilchap8bab7962016-12-20 02:46:46 -08001015// Holding no Critical section.
1016void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -07001017 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001018 // Process TMMBR and REMB first to avoid multiple callbacks
1019 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -07001020 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001021 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -08001022 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001023 }
sprang7dc39f32015-10-13 09:17:48 -07001024 uint32_t local_ssrc;
1025 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001026 {
1027 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -08001028 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +00001029 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -07001030 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001031 }
danilchap92ea6012016-09-23 10:36:03 -07001032 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001033 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001034 }
danilchap92ea6012016-09-23 10:36:03 -07001035 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1036 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001037 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1038 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001039 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001040 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001041 }
spranga790d832016-12-02 07:29:44 -08001042
1043 // We need feedback that we have received a report block(s) so that we
1044 // can generate a new packet in a conference relay scenario, one received
1045 // report can generate several RTCP packets, based on number relayed/mixed
1046 // a send report block should go out to all receivers.
1047 if (rtcp_intra_frame_observer_) {
1048 RTC_DCHECK(!receiver_only_);
1049 if ((packet_information.packet_type_flags & kRtcpPli) ||
1050 (packet_information.packet_type_flags & kRtcpFir)) {
1051 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001052 RTC_LOG(LS_VERBOSE)
1053 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001054 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001055 RTC_LOG(LS_VERBOSE)
1056 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001057 }
spranga790d832016-12-02 07:29:44 -08001058 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001059 }
spranga790d832016-12-02 07:29:44 -08001060 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001061 if (rtcp_loss_notification_observer_ &&
1062 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1063 rtcp::LossNotification* loss_notification =
1064 packet_information.loss_notification.get();
1065 RTC_DCHECK(loss_notification);
1066 if (loss_notification->media_ssrc() == local_ssrc) {
1067 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1068 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1069 loss_notification->last_received(),
1070 loss_notification->decodability_flag());
1071 }
1072 }
spranga790d832016-12-02 07:29:44 -08001073 if (rtcp_bandwidth_observer_) {
1074 RTC_DCHECK(!receiver_only_);
1075 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001076 RTC_LOG(LS_VERBOSE)
1077 << "Incoming REMB: "
1078 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001079 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1080 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001081 }
danilchap92ea6012016-09-23 10:36:03 -07001082 if ((packet_information.packet_type_flags & kRtcpSr) ||
1083 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001084 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001085 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001086 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001087 }
spranga790d832016-12-02 07:29:44 -08001088 }
1089 if ((packet_information.packet_type_flags & kRtcpSr) ||
1090 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001091 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001092 }
isheriff6b4b5f32016-06-08 00:24:21 -07001093
spranga790d832016-12-02 07:29:44 -08001094 if (transport_feedback_observer_ &&
1095 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1096 uint32_t media_source_ssrc =
1097 packet_information.transport_feedback->media_ssrc();
1098 if (media_source_ssrc == local_ssrc ||
1099 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1100 transport_feedback_observer_->OnTransportFeedback(
1101 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001102 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001103 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001104
Sebastian Janssone1795f42019-07-24 11:38:03 +02001105 if (network_state_estimate_observer_ &&
1106 packet_information.network_state_estimate) {
1107 network_state_estimate_observer_->OnRemoteNetworkEstimate(
1108 *packet_information.network_state_estimate);
1109 }
1110
spranga790d832016-12-02 07:29:44 -08001111 if (bitrate_allocation_observer_ &&
1112 packet_information.target_bitrate_allocation) {
1113 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1114 *packet_information.target_bitrate_allocation);
1115 }
1116
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001117 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001118 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001119 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001120 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001121 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001122 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001123 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001124 report_block.extended_highest_sequence_number;
1125 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001126 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001127
srte3e69e5c2017-08-09 06:13:45 -07001128 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001129 }
1130 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001131 if (report_block_data_observer_) {
1132 for (const auto& report_block_data :
1133 packet_information.report_block_datas) {
1134 report_block_data_observer_->OnReportBlockDataUpdated(
1135 report_block_data);
1136 }
1137 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001138 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001139}
1140
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001141int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001142 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001143 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001144
danilchap8bab7962016-12-20 02:46:46 -08001145 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001146 auto received_cname_it = received_cnames_.find(remoteSSRC);
1147 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001148 return -1;
danilchap95321242016-09-27 07:05:32 -07001149
1150 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1151 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001152 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001153}
1154
danilchap7851bda2016-09-29 15:28:07 -07001155std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001156 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001157 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001158
danilchap8bab7962016-12-20 02:46:46 -08001159 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001160 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001161
danilchap9bf610e2017-02-20 06:03:01 -08001162 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001163 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001164 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001165 // Erase timeout entries.
1166 it = kv.second.tmmbr.erase(it);
1167 } else {
1168 candidates.push_back(it->second.tmmbr_item);
1169 ++it;
1170 }
1171 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001172 }
danilchap287e5482016-08-16 15:15:39 -07001173 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001174}
1175
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001176} // namespace webrtc