blob: 6b64473eea6c612c5a04a0a68f5d899aea812a46 [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;
Erik Språng6841d252019-10-15 14:29:11 +020068
69std::set<uint32_t> GetRegisteredSsrcs(const RtpRtcp::Configuration& config) {
70 std::set<uint32_t> ssrcs;
71 ssrcs.insert(config.local_media_ssrc);
72 if (config.rtx_send_ssrc) {
73 ssrcs.insert(*config.rtx_send_ssrc);
74 }
75 if (config.flexfec_sender) {
76 ssrcs.insert(config.flexfec_sender->ssrc());
77 }
78 return ssrcs;
79}
danilchap1b1863a2016-09-20 01:39:54 -070080} // namespace
81
danilchap92ea6012016-09-23 10:36:03 -070082struct RTCPReceiver::PacketInformation {
83 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
84
85 uint32_t remote_ssrc = 0;
86 std::vector<uint16_t> nack_sequence_numbers;
Henrik Boströmf2047872019-05-16 13:32:20 +020087 // TODO(hbos): Remove |report_blocks| in favor of |report_block_datas|.
danilchap92ea6012016-09-23 10:36:03 -070088 ReportBlockList report_blocks;
Henrik Boströmf2047872019-05-16 13:32:20 +020089 std::vector<ReportBlockData> report_block_datas;
danilchap92ea6012016-09-23 10:36:03 -070090 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070091 uint32_t receiver_estimated_max_bitrate_bps = 0;
92 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020093 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Sebastian Janssone1795f42019-07-24 11:38:03 +020094 absl::optional<NetworkStateEstimate> network_state_estimate;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010095 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070096};
97
danilchap9bf610e2017-02-20 06:03:01 -080098// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
99struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -0700100 struct TimedTmmbrItem {
101 rtcp::TmmbItem tmmbr_item;
102 int64_t last_updated_ms;
103 };
104
105 int64_t last_time_received_ms = 0;
106
danilchap7851bda2016-09-29 15:28:07 -0700107 bool ready_for_delete = false;
108
109 std::vector<rtcp::TmmbItem> tmmbn;
110 std::map<uint32_t, TimedTmmbrItem> tmmbr;
111};
112
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200113// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
114struct RTCPReceiver::RrtrInformation {
115 RrtrInformation(uint32_t ssrc,
116 uint32_t received_remote_mid_ntp_time,
117 uint32_t local_receive_mid_ntp_time)
118 : ssrc(ssrc),
119 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
120 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
121
122 uint32_t ssrc;
123 // Received NTP timestamp in compact representation.
124 uint32_t received_remote_mid_ntp_time;
125 // NTP time when the report was received in compact representation.
126 uint32_t local_receive_mid_ntp_time;
127};
128
danilchapefa966b2017-02-17 06:23:15 -0800129struct RTCPReceiver::LastFirStatus {
130 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
131 : request_ms(now_ms), sequence_number(sequence_number) {}
132 int64_t request_ms;
133 uint8_t sequence_number;
134};
135
Mirko Bonadei3b676722019-07-12 17:35:05 +0000136RTCPReceiver::RTCPReceiver(const RtpRtcp::Configuration& config,
137 ModuleRtpRtcp* owner)
138 : clock_(config.clock),
139 receiver_only_(config.receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800140 rtp_rtcp_(owner),
Erik Språng6841d252019-10-15 14:29:11 +0200141 main_ssrc_(config.local_media_ssrc),
142 registered_ssrcs_(GetRegisteredSsrcs(config)),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000143 rtcp_bandwidth_observer_(config.bandwidth_callback),
144 rtcp_intra_frame_observer_(config.intra_frame_callback),
145 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
Sebastian Janssone1795f42019-07-24 11:38:03 +0200146 network_state_estimate_observer_(config.network_state_estimate_observer),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000147 transport_feedback_observer_(config.transport_feedback_callback),
148 bitrate_allocation_observer_(config.bitrate_allocation_observer),
149 report_interval_ms_(config.rtcp_report_interval_ms > 0
150 ? config.rtcp_report_interval_ms
151 : (config.audio ? kDefaultAudioReportInterval
152 : kDefaultVideoReportInterval)),
Erik Språng54d5d2c2019-08-20 17:22:36 +0200153 // TODO(bugs.webrtc.org/10774): Remove fallback.
danilchap8bab7962016-12-20 02:46:46 -0800154 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700155 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100156 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000157 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700158 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200159 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800160 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800161 stats_callback_(nullptr),
Niels Möller4d7c4052019-08-05 12:45:19 +0200162 cname_callback_(nullptr),
Henrik Boströmf2047872019-05-16 13:32:20 +0200163 report_block_data_observer_(nullptr),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000164 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Erik Språng6b8d3552015-09-24 15:06:57 +0200165 num_skipped_packets_(0),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000166 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
danilchap8bab7962016-12-20 02:46:46 -0800167 RTC_DCHECK(owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
danilchap28b03eb2016-10-05 06:59:44 -0700170RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
nisse479d3d72017-09-13 07:53:37 -0700172void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700173 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700175 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700176 }
danilchap59cb2bd2016-08-29 11:08:47 -0700177
danilchap92ea6012016-09-23 10:36:03 -0700178 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700179 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700180 return;
danilchap8bab7962016-12-20 02:46:46 -0800181 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
183
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200184int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800185 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200186 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000187}
188
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000189void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800190 rtc::CritScope lock(&rtcp_receiver_lock_);
191 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700192 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800193 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000196uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800197 rtc::CritScope lock(&rtcp_receiver_lock_);
198 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000199}
200
danilchap28b03eb2016-10-05 06:59:44 -0700201int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
202 int64_t* last_rtt_ms,
203 int64_t* avg_rtt_ms,
204 int64_t* min_rtt_ms,
205 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800206 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
danilchap28b03eb2016-10-05 06:59:44 -0700208 auto it = received_report_blocks_.find(main_ssrc_);
209 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000210 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700211
212 auto it_info = it->second.find(remote_ssrc);
213 if (it_info == it->second.end())
214 return -1;
215
Henrik Boströmf2047872019-05-16 13:32:20 +0200216 const ReportBlockData* report_block_data = &it_info->second;
danilchap28b03eb2016-10-05 06:59:44 -0700217
Henrik Boströmf2047872019-05-16 13:32:20 +0200218 if (report_block_data->num_rtts() == 0)
danilchap28b03eb2016-10-05 06:59:44 -0700219 return -1;
220
221 if (last_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200222 *last_rtt_ms = report_block_data->last_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700223
Henrik Boströmf2047872019-05-16 13:32:20 +0200224 if (avg_rtt_ms) {
225 *avg_rtt_ms =
226 report_block_data->sum_rtt_ms() / report_block_data->num_rtts();
227 }
danilchap28b03eb2016-10-05 06:59:44 -0700228
229 if (min_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200230 *min_rtt_ms = report_block_data->min_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700231
232 if (max_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200233 *max_rtt_ms = report_block_data->max_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700234
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000235 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
237
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100238void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800239 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100240 xr_rrtr_status_ = enable;
241}
242
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000243bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800244 RTC_DCHECK(rtt_ms);
245 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000246 if (xr_rr_rtt_ms_ == 0) {
247 return false;
248 }
249 *rtt_ms = xr_rr_rtt_ms_;
250 xr_rr_rtt_ms_ = 0;
251 return true;
252}
253
danilchap8bab7962016-12-20 02:46:46 -0800254bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
255 uint32_t* received_ntp_frac,
256 uint32_t* rtcp_arrival_time_secs,
257 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700258 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800259 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700260 if (!last_received_sr_ntp_.Valid())
261 return false;
262
263 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800264 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700265 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800266 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700267 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700268
269 // Rtp time from incoming SenderReport.
270 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700271 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700272
273 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800274 if (rtcp_arrival_time_secs)
275 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
276 if (rtcp_arrival_time_frac)
277 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700278
danilchapda161d72016-08-19 07:29:46 -0700279 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200282std::vector<rtcp::ReceiveTimeInfo>
283RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800284 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000285
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200286 const size_t last_xr_rtis_size = std::min(
287 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
288 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
289 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000290
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200291 const uint32_t now_ntp =
292 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000293
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200294 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
295 RrtrInformation& rrtr = received_rrtrs_.front();
296 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
297 now_ntp - rrtr.local_receive_mid_ntp_time);
298 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
299 received_rrtrs_.pop_front();
300 }
301
302 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000303}
304
danilchap28b03eb2016-10-05 06:59:44 -0700305// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000306int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700307 std::vector<RTCPReportBlock>* receive_blocks) const {
308 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800309 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700310 for (const auto& reports_per_receiver : received_report_blocks_)
311 for (const auto& report : reports_per_receiver.second)
Henrik Boströmf2047872019-05-16 13:32:20 +0200312 receive_blocks->push_back(report.second.report_block());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000313 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
Henrik Boströmf2047872019-05-16 13:32:20 +0200316std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
317 std::vector<ReportBlockData> result;
318 rtc::CritScope lock(&rtcp_receiver_lock_);
319 for (const auto& reports_per_receiver : received_report_blocks_)
320 for (const auto& report : reports_per_receiver.second)
321 result.push_back(report.second);
322 return result;
323}
324
danilchap92ea6012016-09-23 10:36:03 -0700325bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
326 const uint8_t* packet_end,
327 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800328 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000329
danilchap1b1863a2016-09-20 01:39:54 -0700330 CommonHeader rtcp_block;
331 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
332 next_block = rtcp_block.NextPacket()) {
333 ptrdiff_t remaining_blocks_size = packet_end - next_block;
334 RTC_DCHECK_GT(remaining_blocks_size, 0);
335 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
336 if (next_block == packet_begin) {
337 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100338 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700339 return false;
340 }
341 ++num_skipped_packets_;
342 break;
343 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
danilchap1b1863a2016-09-20 01:39:54 -0700345 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800346 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700347
danilchap1b1863a2016-09-20 01:39:54 -0700348 switch (rtcp_block.type()) {
349 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700350 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200351 break;
danilchap1b1863a2016-09-20 01:39:54 -0700352 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700353 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700354 break;
danilchap1b1863a2016-09-20 01:39:54 -0700355 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800356 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700357 break;
danilchap1b1863a2016-09-20 01:39:54 -0700358 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700359 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700360 break;
danilchap1b1863a2016-09-20 01:39:54 -0700361 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800362 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700363 break;
Sebastian Janssone1795f42019-07-24 11:38:03 +0200364 case rtcp::App::kPacketType:
365 HandleApp(rtcp_block, packet_information);
366 break;
danilchap1b1863a2016-09-20 01:39:54 -0700367 case rtcp::Rtpfb::kPacketType:
368 switch (rtcp_block.fmt()) {
369 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800370 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700371 break;
372 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800373 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700374 break;
375 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800376 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700377 break;
378 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800379 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700380 break;
381 case rtcp::TransportFeedback::kFeedbackMessageType:
382 HandleTransportFeedback(rtcp_block, packet_information);
383 break;
384 default:
385 ++num_skipped_packets_;
386 break;
387 }
danilchapda161d72016-08-19 07:29:46 -0700388 break;
danilchap1b1863a2016-09-20 01:39:54 -0700389 case rtcp::Psfb::kPacketType:
390 switch (rtcp_block.fmt()) {
391 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800392 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700393 break;
danilchap1b1863a2016-09-20 01:39:54 -0700394 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800395 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700396 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100397 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700398 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700399 break;
400 default:
401 ++num_skipped_packets_;
402 break;
403 }
danilchapda161d72016-08-19 07:29:46 -0700404 break;
405 default:
danilchap1b1863a2016-09-20 01:39:54 -0700406 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700407 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000408 }
danilchapda161d72016-08-19 07:29:46 -0700409 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000410
danilchap8bab7962016-12-20 02:46:46 -0800411 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700412 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
413 main_ssrc_, packet_type_counter_);
414 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000415
danilchap8bab7962016-12-20 02:46:46 -0800416 int64_t now_ms = clock_->TimeInMilliseconds();
417 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700418 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800419 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100420 RTC_LOG(LS_WARNING)
421 << num_skipped_packets_
422 << " RTCP blocks were skipped due to being malformed or of "
423 "unrecognized/unsupported type, during the past "
424 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700425 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200426
danilchap1b1863a2016-09-20 01:39:54 -0700427 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000428}
429
danilchap92ea6012016-09-23 10:36:03 -0700430void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
431 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700432 rtcp::SenderReport sender_report;
433 if (!sender_report.Parse(rtcp_block)) {
434 ++num_skipped_packets_;
435 return;
436 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
danilchap8bab7962016-12-20 02:46:46 -0800438 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
danilchap8bab7962016-12-20 02:46:46 -0800440 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
danilchapec067e92017-02-21 05:38:19 -0800442 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700443
Danil Chapovalov91511f12016-09-15 16:24:02 +0200444 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800445 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200446 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700447 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700448
danilchapa04d9c32017-07-25 04:03:39 -0700449 remote_sender_ntp_time_ = sender_report.ntp();
450 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200451 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700452 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200453 // We will only store the send report from one source, but
454 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700455 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700456 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000457
Mirko Bonadei739baf02019-01-27 17:29:42 +0100458 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800459 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
danilchap92ea6012016-09-23 10:36:03 -0700462void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
463 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700464 rtcp::ReceiverReport receiver_report;
465 if (!receiver_report.Parse(rtcp_block)) {
466 ++num_skipped_packets_;
467 return;
468 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200469
danilchap8bab7962016-12-20 02:46:46 -0800470 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200471
danilchap8bab7962016-12-20 02:46:46 -0800472 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200473
danilchapec067e92017-02-21 05:38:19 -0800474 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200475
danilchap92ea6012016-09-23 10:36:03 -0700476 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200477
danilchap1b1863a2016-09-20 01:39:54 -0700478 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800479 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200480}
481
danilchap92ea6012016-09-23 10:36:03 -0700482void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
483 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700484 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000485 // This will be called once per report block in the RTCP packet.
486 // We filter out all report blocks that are not for us.
487 // Each packet has max 31 RR blocks.
488 //
489 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
danilchap28b03eb2016-10-05 06:59:44 -0700491 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000492 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000494 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700495 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000496 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000497
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200498 last_received_rb_ms_ = clock_->TimeInMilliseconds();
499
Henrik Boströmf2047872019-05-16 13:32:20 +0200500 ReportBlockData* report_block_data =
danilchap28b03eb2016-10-05 06:59:44 -0700501 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
Henrik Boströmf2047872019-05-16 13:32:20 +0200502 RTCPReportBlock rtcp_report_block;
503 rtcp_report_block.sender_ssrc = remote_ssrc;
504 rtcp_report_block.source_ssrc = report_block.source_ssrc();
505 rtcp_report_block.fraction_lost = report_block.fraction_lost();
506 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700507 if (report_block.extended_high_seq_num() >
Henrik Boströmf2047872019-05-16 13:32:20 +0200508 report_block_data->report_block().extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000509 // We have successfully delivered new RTP packets to the remote side after
510 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800511 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000512 }
Henrik Boströmf2047872019-05-16 13:32:20 +0200513 rtcp_report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700514 report_block.extended_high_seq_num();
Henrik Boströmf2047872019-05-16 13:32:20 +0200515 rtcp_report_block.jitter = report_block.jitter();
516 rtcp_report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700517 report_block.delay_since_last_sr();
Henrik Boströmf2047872019-05-16 13:32:20 +0200518 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
519 report_block_data->SetReportBlock(rtcp_report_block, rtc::TimeUTCMicros());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000520
danilchap28b03eb2016-10-05 06:59:44 -0700521 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800522 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100523 // RFC3550, section 6.4.1, LSR field discription states:
524 // If no SR has been received yet, the field is set to zero.
525 // Receiver rtp_rtcp module is not expected to calculate rtt using
526 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100527
528 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
529 // is false. However, that currently breaks the tests of the
530 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
531 // delete all dependencies on RTT measurements for audio receive streams, or
532 // ensure that audio receive streams that need RTT and stats that depend on it
533 // are configured with an associated audio send stream.
534 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800535 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100536 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200537 uint32_t receive_time_ntp =
538 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000539
Danil Chapovalova094fd12016-02-22 18:59:36 +0100540 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800541 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100542 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700543 rtt_ms = CompactNtpRttToMs(rtt_ntp);
Henrik Boströmf2047872019-05-16 13:32:20 +0200544 report_block_data->AddRoundTripTimeSample(rtt_ms);
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100545
546 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000547 }
548
Henrik Boströmf2047872019-05-16 13:32:20 +0200549 packet_information->report_blocks.push_back(
550 report_block_data->report_block());
551 packet_information->report_block_datas.push_back(*report_block_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000552}
553
danilchapec067e92017-02-21 05:38:19 -0800554RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
555 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700556 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800557 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700558 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800559 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800560 return tmmbr_info;
561}
562
563void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
564 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
565 if (tmmbr_it != tmmbr_infos_.end())
566 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
danilchap9bf610e2017-02-20 06:03:01 -0800569RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700570 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800571 auto it = tmmbr_infos_.find(remote_ssrc);
572 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700573 return nullptr;
574 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000575}
576
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800577bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800578 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200579 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000580 return false;
581
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800582 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200583 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000584 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200585 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000586 return true;
587 }
588 return false;
589}
590
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800591bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800592 rtc::CritScope lock(&rtcp_receiver_lock_);
593 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000594 return false;
595
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800596 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800597 if (clock_->TimeInMilliseconds() >
598 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000599 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800600 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000601 return true;
602 }
603 return false;
604}
605
danilchap9bf610e2017-02-20 06:03:01 -0800606bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800607 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000608
danilchap8bab7962016-12-20 02:46:46 -0800609 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800610 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611
danilchap9bf610e2017-02-20 06:03:01 -0800612 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800613 return false;
614
615 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800616 oldest_tmmbr_info_ms_ = -1;
617 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
618 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
619 if (tmmbr_info->last_time_received_ms > 0) {
620 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700621 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800622 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700623 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800624 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700625 // Send new TMMBN to all channels using the default codec.
626 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800627 } else if (oldest_tmmbr_info_ms_ == -1 ||
628 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
629 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000630 }
danilchap9bf610e2017-02-20 06:03:01 -0800631 ++tmmbr_it;
632 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700633 // When we dont have a last_time_received_ms and the object is marked
634 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800635 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000636 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800637 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000638 }
639 }
danilchap7851bda2016-09-29 15:28:07 -0700640 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000641}
642
danilchap2b616392016-08-18 06:17:42 -0700643std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800644 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800645 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
646 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700647 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700648
danilchap9bf610e2017-02-20 06:03:01 -0800649 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
650 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000651}
652
danilchap8bab7962016-12-20 02:46:46 -0800653void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700654 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700655 rtcp::Sdes sdes;
656 if (!sdes.Parse(rtcp_block)) {
657 ++num_skipped_packets_;
658 return;
659 }
660
661 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700662 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200663 {
spranga790d832016-12-02 07:29:44 -0800664 rtc::CritScope lock(&feedbacks_lock_);
Niels Möller4d7c4052019-08-05 12:45:19 +0200665 if (cname_callback_)
666 cname_callback_->OnCname(chunk.ssrc, chunk.cname);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200667 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000668 }
danilchap92ea6012016-09-23 10:36:03 -0700669 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000670}
671
danilchap8bab7962016-12-20 02:46:46 -0800672void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700673 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700674 rtcp::Nack nack;
675 if (!nack.Parse(rtcp_block)) {
676 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000677 return;
678 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000679
danilchap1b1863a2016-09-20 01:39:54 -0700680 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
681 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200682
danilchap142f0192016-10-20 08:22:42 -0700683 packet_information->nack_sequence_numbers.insert(
684 packet_information->nack_sequence_numbers.end(),
685 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700686 for (uint16_t packet_id : nack.packet_ids())
687 nack_stats_.ReportRequest(packet_id);
688
689 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700690 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000691 ++packet_type_counter_.nack_packets;
692 packet_type_counter_.nack_requests = nack_stats_.requests();
693 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
694 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000695}
696
Sebastian Janssone1795f42019-07-24 11:38:03 +0200697void RTCPReceiver::HandleApp(const rtcp::CommonHeader& rtcp_block,
698 PacketInformation* packet_information) {
Sebastian Janssona72d5832019-07-25 09:50:48 +0200699 rtcp::App app;
700 if (app.Parse(rtcp_block)) {
701 if (app.name() == rtcp::RemoteEstimate::kName &&
702 app.sub_type() == rtcp::RemoteEstimate::kSubType) {
703 rtcp::RemoteEstimate estimate(std::move(app));
704 if (estimate.ParseData()) {
705 packet_information->network_state_estimate = estimate.estimate();
706 return;
707 }
Sebastian Janssone1795f42019-07-24 11:38:03 +0200708 }
709 }
710 ++num_skipped_packets_;
711}
712
danilchap8bab7962016-12-20 02:46:46 -0800713void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700714 rtcp::Bye bye;
715 if (!bye.Parse(rtcp_block)) {
716 ++num_skipped_packets_;
717 return;
718 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000719
danilchap8bab7962016-12-20 02:46:46 -0800720 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700721 for (auto& reports_per_receiver : received_report_blocks_)
722 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000723
danilchap9bf610e2017-02-20 06:03:01 -0800724 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
725 if (tmmbr_info)
726 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000727
danilchapefa966b2017-02-17 06:23:15 -0800728 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700729 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200730 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
731 if (it != received_rrtrs_ssrc_it_.end()) {
732 received_rrtrs_.erase(it->second);
733 received_rrtrs_ssrc_it_.erase(it);
734 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000735 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000736}
737
danilchap1b1863a2016-09-20 01:39:54 -0700738void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700739 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700740 rtcp::ExtendedReports xr;
741 if (!xr.Parse(rtcp_block)) {
742 ++num_skipped_packets_;
743 return;
744 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000745
danilchap80ac24d2016-10-31 08:40:47 -0700746 if (xr.rrtr())
747 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000748
danilchap80ac24d2016-10-31 08:40:47 -0700749 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
750 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800751
sprangb32aaf92017-08-28 05:49:12 -0700752 if (xr.target_bitrate()) {
753 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
754 packet_information);
755 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000756}
757
danilchap8bab7962016-12-20 02:46:46 -0800758void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
759 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200760 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200761 uint32_t local_receive_mid_ntp_time =
762 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200763
764 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
765 if (it != received_rrtrs_ssrc_it_.end()) {
766 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
767 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
768 } else {
769 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
770 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
771 local_receive_mid_ntp_time);
772 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
773 } else {
774 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
775 << ", reached maximum number of stored RRTRs.";
776 }
777 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000778}
779
danilchap92ea6012016-09-23 10:36:03 -0700780void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700781 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
782 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000783
danilchap1b1863a2016-09-20 01:39:54 -0700784 // Caller should explicitly enable rtt calculation using extended reports.
785 if (!xr_rrtr_status_)
786 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200787
danilchap1b1863a2016-09-20 01:39:54 -0700788 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800789 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700790 // RFC3611, section 4.5, LRR field discription states:
791 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800792 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700793 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200794
danilchap8bab7962016-12-20 02:46:46 -0800795 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200796 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200797
danilchap8bab7962016-12-20 02:46:46 -0800798 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700799 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000800}
801
spranga790d832016-12-02 07:29:44 -0800802void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700803 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800804 const rtcp::TargetBitrate& target_bitrate,
805 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700806 if (ssrc != remote_ssrc_) {
807 return; // Not for us.
808 }
809
Erik Språng566124a2018-04-23 12:32:22 +0200810 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800811 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800812 if (item.spatial_layer >= kMaxSpatialLayers ||
813 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100814 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800815 << "Invalid layer in XR target bitrate pack: spatial index "
816 << item.spatial_layer << ", temporal index " << item.temporal_layer
817 << ", dropping.";
818 } else {
819 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
820 item.target_bitrate_kbps * 1000);
821 }
spranga790d832016-12-02 07:29:44 -0800822 }
823 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
824}
825
danilchap8bab7962016-12-20 02:46:46 -0800826void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700827 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700828 rtcp::Pli pli;
829 if (!pli.Parse(rtcp_block)) {
830 ++num_skipped_packets_;
831 return;
832 }
833
834 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000835 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000836 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700837 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000838 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000839}
840
danilchap8bab7962016-12-20 02:46:46 -0800841void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700842 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700843 rtcp::Tmmbr tmmbr;
844 if (!tmmbr.Parse(rtcp_block)) {
845 ++num_skipped_packets_;
846 return;
847 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000848
danilchap7851bda2016-09-29 15:28:07 -0700849 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700850 if (tmmbr.media_ssrc()) {
851 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
852 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700853 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000854 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000855
danilchap1b1863a2016-09-20 01:39:54 -0700856 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800857 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
858 continue;
danilchap7851bda2016-09-29 15:28:07 -0700859
danilchapec067e92017-02-21 05:38:19 -0800860 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
861 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200862 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800863 request.packet_overhead());
864 entry->last_updated_ms = clock_->TimeInMilliseconds();
865
866 packet_information->packet_type_flags |= kRtcpTmmbr;
867 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000868 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000869}
870
danilchap8bab7962016-12-20 02:46:46 -0800871void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700872 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700873 rtcp::Tmmbn tmmbn;
874 if (!tmmbn.Parse(rtcp_block)) {
875 ++num_skipped_packets_;
876 return;
877 }
878
danilchapec067e92017-02-21 05:38:19 -0800879 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700880
danilchap92ea6012016-09-23 10:36:03 -0700881 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000882
danilchapec067e92017-02-21 05:38:19 -0800883 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700884}
885
danilchap8bab7962016-12-20 02:46:46 -0800886void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
887 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700888 rtcp::RapidResyncRequest sr_req;
889 if (!sr_req.Parse(rtcp_block)) {
890 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000891 return;
892 }
893
danilchap92ea6012016-09-23 10:36:03 -0700894 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000895}
896
danilchap1b1863a2016-09-20 01:39:54 -0700897void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700898 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100899 {
900 rtcp::Remb remb;
901 if (remb.Parse(rtcp_block)) {
902 packet_information->packet_type_flags |= kRtcpRemb;
903 packet_information->receiver_estimated_max_bitrate_bps =
904 remb.bitrate_bps();
905 return;
906 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000907 }
danilchap1b1863a2016-09-20 01:39:54 -0700908
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100909 {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200910 auto loss_notification = std::make_unique<rtcp::LossNotification>();
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100911 if (loss_notification->Parse(rtcp_block)) {
912 packet_information->packet_type_flags |= kRtcpLossNotification;
913 packet_information->loss_notification = std::move(loss_notification);
914 return;
915 }
916 }
917
918 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
919
danilchap1b1863a2016-09-20 01:39:54 -0700920 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000921}
922
danilchap8bab7962016-12-20 02:46:46 -0800923void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700924 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700925 rtcp::Fir fir;
926 if (!fir.Parse(rtcp_block)) {
927 ++num_skipped_packets_;
928 return;
929 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000930
danilchap1b1863a2016-09-20 01:39:54 -0700931 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800932 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700933 if (main_ssrc_ != fir_request.ssrc)
934 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200935
936 ++packet_type_counter_.fir_packets;
937
danilchapefa966b2017-02-17 06:23:15 -0800938 int64_t now_ms = clock_->TimeInMilliseconds();
939 auto inserted = last_fir_.insert(std::make_pair(
940 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
941 if (!inserted.second) { // There was already an entry.
942 LastFirStatus* last_fir = &inserted.first->second;
943
danilchap7851bda2016-09-29 15:28:07 -0700944 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800945 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700946 continue;
947
danilchap7851bda2016-09-29 15:28:07 -0700948 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800949 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700950 continue;
951
danilchapefa966b2017-02-17 06:23:15 -0800952 last_fir->request_ms = now_ms;
953 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200954 }
danilchap7851bda2016-09-29 15:28:07 -0700955 // Received signal that we need to send a new key frame.
956 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000957 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000958}
959
Erik Språng6b8d3552015-09-24 15:06:57 +0200960void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700961 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700962 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700963 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
964 new rtcp::TransportFeedback());
965 if (!transport_feedback->Parse(rtcp_block)) {
966 ++num_skipped_packets_;
967 return;
968 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200969
danilchap92ea6012016-09-23 10:36:03 -0700970 packet_information->packet_type_flags |= kRtcpTransportFeedback;
971 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200972}
danilchap287e5482016-08-16 15:15:39 -0700973
danilchap9bf610e2017-02-20 06:03:01 -0800974void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700975 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700976 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700977 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000978
spranga790d832016-12-02 07:29:44 -0800979 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700980 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700981 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
982 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800983 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000984 }
danilchap853ecb22016-08-22 08:26:15 -0700985
danilchap9bf610e2017-02-20 06:03:01 -0800986 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800987 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000988}
989
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000990void RTCPReceiver::RegisterRtcpStatisticsCallback(
991 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800992 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000993 stats_callback_ = callback;
994}
995
996RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800997 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000998 return stats_callback_;
999}
1000
Niels Möller4d7c4052019-08-05 12:45:19 +02001001void RTCPReceiver::RegisterRtcpCnameCallback(RtcpCnameCallback* callback) {
1002 rtc::CritScope cs(&feedbacks_lock_);
1003 cname_callback_ = callback;
1004}
1005
Henrik Boströmf2047872019-05-16 13:32:20 +02001006void RTCPReceiver::SetReportBlockDataObserver(
1007 ReportBlockDataObserver* observer) {
1008 rtc::CritScope cs(&feedbacks_lock_);
1009 report_block_data_observer_ = observer;
1010}
1011
danilchap8bab7962016-12-20 02:46:46 -08001012// Holding no Critical section.
1013void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -07001014 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001015 // Process TMMBR and REMB first to avoid multiple callbacks
1016 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -07001017 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001018 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -08001019 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001020 }
sprang7dc39f32015-10-13 09:17:48 -07001021 uint32_t local_ssrc;
1022 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001023 {
1024 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -08001025 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +00001026 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -07001027 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001028 }
danilchap92ea6012016-09-23 10:36:03 -07001029 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001030 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001031 }
danilchap92ea6012016-09-23 10:36:03 -07001032 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1033 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001034 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1035 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001036 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001037 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001038 }
spranga790d832016-12-02 07:29:44 -08001039
1040 // We need feedback that we have received a report block(s) so that we
1041 // can generate a new packet in a conference relay scenario, one received
1042 // report can generate several RTCP packets, based on number relayed/mixed
1043 // a send report block should go out to all receivers.
1044 if (rtcp_intra_frame_observer_) {
1045 RTC_DCHECK(!receiver_only_);
1046 if ((packet_information.packet_type_flags & kRtcpPli) ||
1047 (packet_information.packet_type_flags & kRtcpFir)) {
1048 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001049 RTC_LOG(LS_VERBOSE)
1050 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001051 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001052 RTC_LOG(LS_VERBOSE)
1053 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001054 }
spranga790d832016-12-02 07:29:44 -08001055 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001056 }
spranga790d832016-12-02 07:29:44 -08001057 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001058 if (rtcp_loss_notification_observer_ &&
1059 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1060 rtcp::LossNotification* loss_notification =
1061 packet_information.loss_notification.get();
1062 RTC_DCHECK(loss_notification);
1063 if (loss_notification->media_ssrc() == local_ssrc) {
1064 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1065 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1066 loss_notification->last_received(),
1067 loss_notification->decodability_flag());
1068 }
1069 }
spranga790d832016-12-02 07:29:44 -08001070 if (rtcp_bandwidth_observer_) {
1071 RTC_DCHECK(!receiver_only_);
1072 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001073 RTC_LOG(LS_VERBOSE)
1074 << "Incoming REMB: "
1075 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001076 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1077 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001078 }
danilchap92ea6012016-09-23 10:36:03 -07001079 if ((packet_information.packet_type_flags & kRtcpSr) ||
1080 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001081 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001082 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001083 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001084 }
spranga790d832016-12-02 07:29:44 -08001085 }
1086 if ((packet_information.packet_type_flags & kRtcpSr) ||
1087 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001088 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001089 }
isheriff6b4b5f32016-06-08 00:24:21 -07001090
spranga790d832016-12-02 07:29:44 -08001091 if (transport_feedback_observer_ &&
1092 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1093 uint32_t media_source_ssrc =
1094 packet_information.transport_feedback->media_ssrc();
1095 if (media_source_ssrc == local_ssrc ||
1096 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1097 transport_feedback_observer_->OnTransportFeedback(
1098 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001099 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001100 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001101
Sebastian Janssone1795f42019-07-24 11:38:03 +02001102 if (network_state_estimate_observer_ &&
1103 packet_information.network_state_estimate) {
1104 network_state_estimate_observer_->OnRemoteNetworkEstimate(
1105 *packet_information.network_state_estimate);
1106 }
1107
spranga790d832016-12-02 07:29:44 -08001108 if (bitrate_allocation_observer_ &&
1109 packet_information.target_bitrate_allocation) {
1110 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1111 *packet_information.target_bitrate_allocation);
1112 }
1113
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001114 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001115 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001116 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001117 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001118 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001119 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001120 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001121 report_block.extended_highest_sequence_number;
1122 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001123 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001124
srte3e69e5c2017-08-09 06:13:45 -07001125 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001126 }
1127 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001128 if (report_block_data_observer_) {
1129 for (const auto& report_block_data :
1130 packet_information.report_block_datas) {
1131 report_block_data_observer_->OnReportBlockDataUpdated(
1132 report_block_data);
1133 }
1134 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001136}
1137
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001138int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001139 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001140 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001141
danilchap8bab7962016-12-20 02:46:46 -08001142 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001143 auto received_cname_it = received_cnames_.find(remoteSSRC);
1144 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001145 return -1;
danilchap95321242016-09-27 07:05:32 -07001146
1147 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1148 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001149 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001150}
1151
danilchap7851bda2016-09-29 15:28:07 -07001152std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001153 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001154 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001155
danilchap8bab7962016-12-20 02:46:46 -08001156 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001157 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001158
danilchap9bf610e2017-02-20 06:03:01 -08001159 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001160 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001161 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001162 // Erase timeout entries.
1163 it = kv.second.tmmbr.erase(it);
1164 } else {
1165 candidates.push_back(it->second.tmmbr_item);
1166 ++it;
1167 }
1168 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001169 }
danilchap287e5482016-08-16 15:15:39 -07001170 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001171}
1172
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001173} // namespace webrtc