blob: c73c7adf3b4c9b952ea72e082804c8c564788681 [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
Elad Alonf8e7ccb2019-01-24 12:38:36 +010021#include "absl/memory/memory.h"
Erik Språngeeaa8f92018-05-17 12:35:56 +020022#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070023#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
Elad Alonf8e7ccb2019-01-24 12:38:36 +010029#include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
Sebastian Janssone1795f42019-07-24 11:38:03 +020035#include "modules/rtp_rtcp/source/rtcp_packet/remote_estimate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
39#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
40#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
41#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
42#include "modules/rtp_rtcp/source/time_util.h"
43#include "modules/rtp_rtcp/source/tmmbr_help.h"
44#include "rtc_base/checks.h"
45#include "rtc_base/logging.h"
Henrik Boströmf2047872019-05-16 13:32:20 +020046#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/trace_event.h"
48#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000049
niklase@google.com470e71d2011-07-07 08:21:25 +000050namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070051namespace {
52
53using rtcp::CommonHeader;
54using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000055
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000056// The number of RTCP time intervals needed to trigger a timeout.
57const int kRrTimeoutIntervals = 3;
58
Jiawei Ou3587b832018-01-31 22:08:26 -080059const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
60
Erik Språng6b8d3552015-09-24 15:06:57 +020061const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080062const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020063
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020064// Maximum number of received RRTRs that will be stored.
65const size_t kMaxNumberOfStoredRrtrs = 200;
66
Mirko Bonadei3b676722019-07-12 17:35:05 +000067constexpr int32_t kDefaultVideoReportInterval = 1000;
68constexpr int32_t kDefaultAudioReportInterval = 5000;
danilchap1b1863a2016-09-20 01:39:54 -070069} // namespace
70
danilchap92ea6012016-09-23 10:36:03 -070071struct RTCPReceiver::PacketInformation {
72 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
73
74 uint32_t remote_ssrc = 0;
75 std::vector<uint16_t> nack_sequence_numbers;
Henrik Boströmf2047872019-05-16 13:32:20 +020076 // TODO(hbos): Remove |report_blocks| in favor of |report_block_datas|.
danilchap92ea6012016-09-23 10:36:03 -070077 ReportBlockList report_blocks;
Henrik Boströmf2047872019-05-16 13:32:20 +020078 std::vector<ReportBlockData> report_block_datas;
danilchap92ea6012016-09-23 10:36:03 -070079 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070080 uint32_t receiver_estimated_max_bitrate_bps = 0;
81 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020082 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Sebastian Janssone1795f42019-07-24 11:38:03 +020083 absl::optional<NetworkStateEstimate> network_state_estimate;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010084 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070085};
86
danilchap9bf610e2017-02-20 06:03:01 -080087// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
88struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070089 struct TimedTmmbrItem {
90 rtcp::TmmbItem tmmbr_item;
91 int64_t last_updated_ms;
92 };
93
94 int64_t last_time_received_ms = 0;
95
danilchap7851bda2016-09-29 15:28:07 -070096 bool ready_for_delete = false;
97
98 std::vector<rtcp::TmmbItem> tmmbn;
99 std::map<uint32_t, TimedTmmbrItem> tmmbr;
100};
101
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200102// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
103struct RTCPReceiver::RrtrInformation {
104 RrtrInformation(uint32_t ssrc,
105 uint32_t received_remote_mid_ntp_time,
106 uint32_t local_receive_mid_ntp_time)
107 : ssrc(ssrc),
108 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
109 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
110
111 uint32_t ssrc;
112 // Received NTP timestamp in compact representation.
113 uint32_t received_remote_mid_ntp_time;
114 // NTP time when the report was received in compact representation.
115 uint32_t local_receive_mid_ntp_time;
116};
117
danilchapefa966b2017-02-17 06:23:15 -0800118struct RTCPReceiver::LastFirStatus {
119 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
120 : request_ms(now_ms), sequence_number(sequence_number) {}
121 int64_t request_ms;
122 uint8_t sequence_number;
123};
124
Mirko Bonadei3b676722019-07-12 17:35:05 +0000125RTCPReceiver::RTCPReceiver(const RtpRtcp::Configuration& config,
126 ModuleRtpRtcp* owner)
127 : clock_(config.clock),
128 receiver_only_(config.receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800129 rtp_rtcp_(owner),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000130 rtcp_bandwidth_observer_(config.bandwidth_callback),
131 rtcp_intra_frame_observer_(config.intra_frame_callback),
132 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
Sebastian Janssone1795f42019-07-24 11:38:03 +0200133 network_state_estimate_observer_(config.network_state_estimate_observer),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000134 transport_feedback_observer_(config.transport_feedback_callback),
135 bitrate_allocation_observer_(config.bitrate_allocation_observer),
136 report_interval_ms_(config.rtcp_report_interval_ms > 0
137 ? config.rtcp_report_interval_ms
138 : (config.audio ? kDefaultAudioReportInterval
139 : kDefaultVideoReportInterval)),
140 main_ssrc_(config.media_send_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),
Henrik Boströmf2047872019-05-16 13:32:20 +0200149 report_block_data_observer_(nullptr),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000150 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Erik Språng6b8d3552015-09-24 15:06:57 +0200151 num_skipped_packets_(0),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000152 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
danilchap8bab7962016-12-20 02:46:46 -0800153 RTC_DCHECK(owner);
Mirko Bonadei3b676722019-07-12 17:35:05 +0000154 if (config.media_send_ssrc) {
155 registered_ssrcs_.insert(*config.media_send_ssrc);
156 }
157 if (config.rtx_send_ssrc) {
158 registered_ssrcs_.insert(*config.rtx_send_ssrc);
159 }
160 if (config.flexfec_sender) {
161 registered_ssrcs_.insert(config.flexfec_sender->ssrc());
162 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000163}
164
danilchap28b03eb2016-10-05 06:59:44 -0700165RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
nisse479d3d72017-09-13 07:53:37 -0700167void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700168 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100169 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700170 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700171 }
danilchap59cb2bd2016-08-29 11:08:47 -0700172
danilchap92ea6012016-09-23 10:36:03 -0700173 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700174 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700175 return;
danilchap8bab7962016-12-20 02:46:46 -0800176 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000177}
178
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200179int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800180 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200181 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000182}
183
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000184void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800185 rtc::CritScope lock(&rtcp_receiver_lock_);
186 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700187 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800188 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000191uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800192 rtc::CritScope lock(&rtcp_receiver_lock_);
193 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000194}
195
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000196void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
197 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800198 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700199 main_ssrc_ = main_ssrc;
200 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000201}
202
danilchap28b03eb2016-10-05 06:59:44 -0700203int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
204 int64_t* last_rtt_ms,
205 int64_t* avg_rtt_ms,
206 int64_t* min_rtt_ms,
207 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800208 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
danilchap28b03eb2016-10-05 06:59:44 -0700210 auto it = received_report_blocks_.find(main_ssrc_);
211 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000212 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700213
214 auto it_info = it->second.find(remote_ssrc);
215 if (it_info == it->second.end())
216 return -1;
217
Henrik Boströmf2047872019-05-16 13:32:20 +0200218 const ReportBlockData* report_block_data = &it_info->second;
danilchap28b03eb2016-10-05 06:59:44 -0700219
Henrik Boströmf2047872019-05-16 13:32:20 +0200220 if (report_block_data->num_rtts() == 0)
danilchap28b03eb2016-10-05 06:59:44 -0700221 return -1;
222
223 if (last_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200224 *last_rtt_ms = report_block_data->last_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700225
Henrik Boströmf2047872019-05-16 13:32:20 +0200226 if (avg_rtt_ms) {
227 *avg_rtt_ms =
228 report_block_data->sum_rtt_ms() / report_block_data->num_rtts();
229 }
danilchap28b03eb2016-10-05 06:59:44 -0700230
231 if (min_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200232 *min_rtt_ms = report_block_data->min_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700233
234 if (max_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200235 *max_rtt_ms = report_block_data->max_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700236
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000237 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238}
239
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100240void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800241 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100242 xr_rrtr_status_ = enable;
243}
244
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000245bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800246 RTC_DCHECK(rtt_ms);
247 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000248 if (xr_rr_rtt_ms_ == 0) {
249 return false;
250 }
251 *rtt_ms = xr_rr_rtt_ms_;
252 xr_rr_rtt_ms_ = 0;
253 return true;
254}
255
danilchap8bab7962016-12-20 02:46:46 -0800256bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
257 uint32_t* received_ntp_frac,
258 uint32_t* rtcp_arrival_time_secs,
259 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700260 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800261 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700262 if (!last_received_sr_ntp_.Valid())
263 return false;
264
265 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800266 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700267 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800268 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700269 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700270
271 // Rtp time from incoming SenderReport.
272 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700273 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700274
275 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800276 if (rtcp_arrival_time_secs)
277 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
278 if (rtcp_arrival_time_frac)
279 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700280
danilchapda161d72016-08-19 07:29:46 -0700281 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200284std::vector<rtcp::ReceiveTimeInfo>
285RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800286 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000287
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200288 const size_t last_xr_rtis_size = std::min(
289 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
290 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
291 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000292
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200293 const uint32_t now_ntp =
294 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000295
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200296 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
297 RrtrInformation& rrtr = received_rrtrs_.front();
298 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
299 now_ntp - rrtr.local_receive_mid_ntp_time);
300 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
301 received_rrtrs_.pop_front();
302 }
303
304 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000305}
306
danilchap28b03eb2016-10-05 06:59:44 -0700307// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000308int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700309 std::vector<RTCPReportBlock>* receive_blocks) const {
310 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800311 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700312 for (const auto& reports_per_receiver : received_report_blocks_)
313 for (const auto& report : reports_per_receiver.second)
Henrik Boströmf2047872019-05-16 13:32:20 +0200314 receive_blocks->push_back(report.second.report_block());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000315 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316}
317
Henrik Boströmf2047872019-05-16 13:32:20 +0200318std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
319 std::vector<ReportBlockData> result;
320 rtc::CritScope lock(&rtcp_receiver_lock_);
321 for (const auto& reports_per_receiver : received_report_blocks_)
322 for (const auto& report : reports_per_receiver.second)
323 result.push_back(report.second);
324 return result;
325}
326
danilchap92ea6012016-09-23 10:36:03 -0700327bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
328 const uint8_t* packet_end,
329 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800330 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000331
danilchap1b1863a2016-09-20 01:39:54 -0700332 CommonHeader rtcp_block;
333 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
334 next_block = rtcp_block.NextPacket()) {
335 ptrdiff_t remaining_blocks_size = packet_end - next_block;
336 RTC_DCHECK_GT(remaining_blocks_size, 0);
337 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
338 if (next_block == packet_begin) {
339 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100340 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700341 return false;
342 }
343 ++num_skipped_packets_;
344 break;
345 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000346
danilchap1b1863a2016-09-20 01:39:54 -0700347 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800348 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700349
danilchap1b1863a2016-09-20 01:39:54 -0700350 switch (rtcp_block.type()) {
351 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700352 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200353 break;
danilchap1b1863a2016-09-20 01:39:54 -0700354 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700355 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700356 break;
danilchap1b1863a2016-09-20 01:39:54 -0700357 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800358 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700359 break;
danilchap1b1863a2016-09-20 01:39:54 -0700360 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700361 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700362 break;
danilchap1b1863a2016-09-20 01:39:54 -0700363 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800364 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700365 break;
Sebastian Janssone1795f42019-07-24 11:38:03 +0200366 case rtcp::App::kPacketType:
367 HandleApp(rtcp_block, packet_information);
368 break;
danilchap1b1863a2016-09-20 01:39:54 -0700369 case rtcp::Rtpfb::kPacketType:
370 switch (rtcp_block.fmt()) {
371 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800372 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700373 break;
374 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800375 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700376 break;
377 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800378 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700379 break;
380 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800381 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700382 break;
383 case rtcp::TransportFeedback::kFeedbackMessageType:
384 HandleTransportFeedback(rtcp_block, packet_information);
385 break;
386 default:
387 ++num_skipped_packets_;
388 break;
389 }
danilchapda161d72016-08-19 07:29:46 -0700390 break;
danilchap1b1863a2016-09-20 01:39:54 -0700391 case rtcp::Psfb::kPacketType:
392 switch (rtcp_block.fmt()) {
393 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800394 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700395 break;
danilchap1b1863a2016-09-20 01:39:54 -0700396 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800397 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700398 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100399 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700400 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700401 break;
402 default:
403 ++num_skipped_packets_;
404 break;
405 }
danilchapda161d72016-08-19 07:29:46 -0700406 break;
407 default:
danilchap1b1863a2016-09-20 01:39:54 -0700408 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700409 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000410 }
danilchapda161d72016-08-19 07:29:46 -0700411 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000412
danilchap8bab7962016-12-20 02:46:46 -0800413 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700414 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
415 main_ssrc_, packet_type_counter_);
416 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000417
danilchap8bab7962016-12-20 02:46:46 -0800418 int64_t now_ms = clock_->TimeInMilliseconds();
419 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700420 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800421 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100422 RTC_LOG(LS_WARNING)
423 << num_skipped_packets_
424 << " RTCP blocks were skipped due to being malformed or of "
425 "unrecognized/unsupported type, during the past "
426 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700427 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200428
danilchap1b1863a2016-09-20 01:39:54 -0700429 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000430}
431
danilchap92ea6012016-09-23 10:36:03 -0700432void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
433 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700434 rtcp::SenderReport sender_report;
435 if (!sender_report.Parse(rtcp_block)) {
436 ++num_skipped_packets_;
437 return;
438 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
danilchap8bab7962016-12-20 02:46:46 -0800440 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
danilchap8bab7962016-12-20 02:46:46 -0800442 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000443
danilchapec067e92017-02-21 05:38:19 -0800444 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700445
Danil Chapovalov91511f12016-09-15 16:24:02 +0200446 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800447 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200448 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700449 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700450
danilchapa04d9c32017-07-25 04:03:39 -0700451 remote_sender_ntp_time_ = sender_report.ntp();
452 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200453 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700454 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200455 // We will only store the send report from one source, but
456 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700457 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700458 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000459
Mirko Bonadei739baf02019-01-27 17:29:42 +0100460 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800461 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
danilchap92ea6012016-09-23 10:36:03 -0700464void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
465 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700466 rtcp::ReceiverReport receiver_report;
467 if (!receiver_report.Parse(rtcp_block)) {
468 ++num_skipped_packets_;
469 return;
470 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200471
danilchap8bab7962016-12-20 02:46:46 -0800472 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200473
danilchap8bab7962016-12-20 02:46:46 -0800474 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200475
danilchapec067e92017-02-21 05:38:19 -0800476 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200477
danilchap92ea6012016-09-23 10:36:03 -0700478 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200479
danilchap1b1863a2016-09-20 01:39:54 -0700480 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800481 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200482}
483
danilchap92ea6012016-09-23 10:36:03 -0700484void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
485 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700486 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000487 // This will be called once per report block in the RTCP packet.
488 // We filter out all report blocks that are not for us.
489 // Each packet has max 31 RR blocks.
490 //
491 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000492
danilchap28b03eb2016-10-05 06:59:44 -0700493 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000494 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000495
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000496 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700497 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000498 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000499
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200500 last_received_rb_ms_ = clock_->TimeInMilliseconds();
501
Henrik Boströmf2047872019-05-16 13:32:20 +0200502 ReportBlockData* report_block_data =
danilchap28b03eb2016-10-05 06:59:44 -0700503 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
Henrik Boströmf2047872019-05-16 13:32:20 +0200504 RTCPReportBlock rtcp_report_block;
505 rtcp_report_block.sender_ssrc = remote_ssrc;
506 rtcp_report_block.source_ssrc = report_block.source_ssrc();
507 rtcp_report_block.fraction_lost = report_block.fraction_lost();
508 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700509 if (report_block.extended_high_seq_num() >
Henrik Boströmf2047872019-05-16 13:32:20 +0200510 report_block_data->report_block().extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000511 // We have successfully delivered new RTP packets to the remote side after
512 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800513 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000514 }
Henrik Boströmf2047872019-05-16 13:32:20 +0200515 rtcp_report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700516 report_block.extended_high_seq_num();
Henrik Boströmf2047872019-05-16 13:32:20 +0200517 rtcp_report_block.jitter = report_block.jitter();
518 rtcp_report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700519 report_block.delay_since_last_sr();
Henrik Boströmf2047872019-05-16 13:32:20 +0200520 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
521 report_block_data->SetReportBlock(rtcp_report_block, rtc::TimeUTCMicros());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000522
danilchap28b03eb2016-10-05 06:59:44 -0700523 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800524 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100525 // RFC3550, section 6.4.1, LSR field discription states:
526 // If no SR has been received yet, the field is set to zero.
527 // Receiver rtp_rtcp module is not expected to calculate rtt using
528 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100529
530 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
531 // is false. However, that currently breaks the tests of the
532 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
533 // delete all dependencies on RTT measurements for audio receive streams, or
534 // ensure that audio receive streams that need RTT and stats that depend on it
535 // are configured with an associated audio send stream.
536 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800537 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100538 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200539 uint32_t receive_time_ntp =
540 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000541
Danil Chapovalova094fd12016-02-22 18:59:36 +0100542 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800543 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100544 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700545 rtt_ms = CompactNtpRttToMs(rtt_ntp);
Henrik Boströmf2047872019-05-16 13:32:20 +0200546 report_block_data->AddRoundTripTimeSample(rtt_ms);
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100547
548 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000549 }
550
Henrik Boströmf2047872019-05-16 13:32:20 +0200551 packet_information->report_blocks.push_back(
552 report_block_data->report_block());
553 packet_information->report_block_datas.push_back(*report_block_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000554}
555
danilchapec067e92017-02-21 05:38:19 -0800556RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
557 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700558 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800559 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700560 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800561 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800562 return tmmbr_info;
563}
564
565void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
566 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
567 if (tmmbr_it != tmmbr_infos_.end())
568 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000569}
570
danilchap9bf610e2017-02-20 06:03:01 -0800571RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700572 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800573 auto it = tmmbr_infos_.find(remote_ssrc);
574 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700575 return nullptr;
576 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000577}
578
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800579bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800580 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200581 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000582 return false;
583
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800584 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200585 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000586 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200587 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000588 return true;
589 }
590 return false;
591}
592
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800593bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800594 rtc::CritScope lock(&rtcp_receiver_lock_);
595 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000596 return false;
597
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800598 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800599 if (clock_->TimeInMilliseconds() >
600 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000601 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800602 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000603 return true;
604 }
605 return false;
606}
607
danilchap9bf610e2017-02-20 06:03:01 -0800608bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800609 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000610
danilchap8bab7962016-12-20 02:46:46 -0800611 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800612 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000613
danilchap9bf610e2017-02-20 06:03:01 -0800614 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800615 return false;
616
617 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800618 oldest_tmmbr_info_ms_ = -1;
619 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
620 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
621 if (tmmbr_info->last_time_received_ms > 0) {
622 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700623 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800624 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700625 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800626 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700627 // Send new TMMBN to all channels using the default codec.
628 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800629 } else if (oldest_tmmbr_info_ms_ == -1 ||
630 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
631 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000632 }
danilchap9bf610e2017-02-20 06:03:01 -0800633 ++tmmbr_it;
634 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700635 // When we dont have a last_time_received_ms and the object is marked
636 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800637 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000638 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800639 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000640 }
641 }
danilchap7851bda2016-09-29 15:28:07 -0700642 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
danilchap2b616392016-08-18 06:17:42 -0700645std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800646 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800647 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
648 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700649 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700650
danilchap9bf610e2017-02-20 06:03:01 -0800651 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
652 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000653}
654
danilchap8bab7962016-12-20 02:46:46 -0800655void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700656 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700657 rtcp::Sdes sdes;
658 if (!sdes.Parse(rtcp_block)) {
659 ++num_skipped_packets_;
660 return;
661 }
662
663 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700664 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200665 {
spranga790d832016-12-02 07:29:44 -0800666 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700667 if (stats_callback_)
668 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200669 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000670 }
danilchap92ea6012016-09-23 10:36:03 -0700671 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000672}
673
danilchap8bab7962016-12-20 02:46:46 -0800674void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700675 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700676 rtcp::Nack nack;
677 if (!nack.Parse(rtcp_block)) {
678 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000679 return;
680 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
danilchap1b1863a2016-09-20 01:39:54 -0700682 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
683 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200684
danilchap142f0192016-10-20 08:22:42 -0700685 packet_information->nack_sequence_numbers.insert(
686 packet_information->nack_sequence_numbers.end(),
687 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700688 for (uint16_t packet_id : nack.packet_ids())
689 nack_stats_.ReportRequest(packet_id);
690
691 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700692 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000693 ++packet_type_counter_.nack_packets;
694 packet_type_counter_.nack_requests = nack_stats_.requests();
695 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
696 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000697}
698
Sebastian Janssone1795f42019-07-24 11:38:03 +0200699void RTCPReceiver::HandleApp(const rtcp::CommonHeader& rtcp_block,
700 PacketInformation* packet_information) {
701 if (rtcp::RemoteEstimate::IsNetworkEstimate(rtcp_block)) {
702 rtcp::RemoteEstimate estimate;
703 if (estimate.Parse(rtcp_block)) {
704 packet_information->network_state_estimate = estimate.estimate();
705 return;
706 }
707 }
708 ++num_skipped_packets_;
709}
710
danilchap8bab7962016-12-20 02:46:46 -0800711void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700712 rtcp::Bye bye;
713 if (!bye.Parse(rtcp_block)) {
714 ++num_skipped_packets_;
715 return;
716 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
danilchap8bab7962016-12-20 02:46:46 -0800718 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700719 for (auto& reports_per_receiver : received_report_blocks_)
720 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000721
danilchap9bf610e2017-02-20 06:03:01 -0800722 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
723 if (tmmbr_info)
724 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000725
danilchapefa966b2017-02-17 06:23:15 -0800726 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700727 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200728 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
729 if (it != received_rrtrs_ssrc_it_.end()) {
730 received_rrtrs_.erase(it->second);
731 received_rrtrs_ssrc_it_.erase(it);
732 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000733 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000734}
735
danilchap1b1863a2016-09-20 01:39:54 -0700736void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700737 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700738 rtcp::ExtendedReports xr;
739 if (!xr.Parse(rtcp_block)) {
740 ++num_skipped_packets_;
741 return;
742 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000743
danilchap80ac24d2016-10-31 08:40:47 -0700744 if (xr.rrtr())
745 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000746
danilchap80ac24d2016-10-31 08:40:47 -0700747 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
748 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800749
sprangb32aaf92017-08-28 05:49:12 -0700750 if (xr.target_bitrate()) {
751 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
752 packet_information);
753 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000754}
755
danilchap8bab7962016-12-20 02:46:46 -0800756void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
757 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200758 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200759 uint32_t local_receive_mid_ntp_time =
760 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200761
762 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
763 if (it != received_rrtrs_ssrc_it_.end()) {
764 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
765 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
766 } else {
767 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
768 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
769 local_receive_mid_ntp_time);
770 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
771 } else {
772 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
773 << ", reached maximum number of stored RRTRs.";
774 }
775 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000776}
777
danilchap92ea6012016-09-23 10:36:03 -0700778void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700779 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
780 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000781
danilchap1b1863a2016-09-20 01:39:54 -0700782 // Caller should explicitly enable rtt calculation using extended reports.
783 if (!xr_rrtr_status_)
784 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200785
danilchap1b1863a2016-09-20 01:39:54 -0700786 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800787 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700788 // RFC3611, section 4.5, LRR field discription states:
789 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800790 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700791 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200792
danilchap8bab7962016-12-20 02:46:46 -0800793 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200794 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200795
danilchap8bab7962016-12-20 02:46:46 -0800796 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700797 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000798}
799
spranga790d832016-12-02 07:29:44 -0800800void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700801 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800802 const rtcp::TargetBitrate& target_bitrate,
803 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700804 if (ssrc != remote_ssrc_) {
805 return; // Not for us.
806 }
807
Erik Språng566124a2018-04-23 12:32:22 +0200808 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800809 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800810 if (item.spatial_layer >= kMaxSpatialLayers ||
811 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100812 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800813 << "Invalid layer in XR target bitrate pack: spatial index "
814 << item.spatial_layer << ", temporal index " << item.temporal_layer
815 << ", dropping.";
816 } else {
817 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
818 item.target_bitrate_kbps * 1000);
819 }
spranga790d832016-12-02 07:29:44 -0800820 }
821 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
822}
823
danilchap8bab7962016-12-20 02:46:46 -0800824void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700825 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700826 rtcp::Pli pli;
827 if (!pli.Parse(rtcp_block)) {
828 ++num_skipped_packets_;
829 return;
830 }
831
832 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000833 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000834 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700835 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000836 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000837}
838
danilchap8bab7962016-12-20 02:46:46 -0800839void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700840 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700841 rtcp::Tmmbr tmmbr;
842 if (!tmmbr.Parse(rtcp_block)) {
843 ++num_skipped_packets_;
844 return;
845 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000846
danilchap7851bda2016-09-29 15:28:07 -0700847 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700848 if (tmmbr.media_ssrc()) {
849 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
850 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700851 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000852 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000853
danilchap1b1863a2016-09-20 01:39:54 -0700854 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800855 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
856 continue;
danilchap7851bda2016-09-29 15:28:07 -0700857
danilchapec067e92017-02-21 05:38:19 -0800858 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
859 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200860 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800861 request.packet_overhead());
862 entry->last_updated_ms = clock_->TimeInMilliseconds();
863
864 packet_information->packet_type_flags |= kRtcpTmmbr;
865 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000866 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
868
danilchap8bab7962016-12-20 02:46:46 -0800869void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700870 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700871 rtcp::Tmmbn tmmbn;
872 if (!tmmbn.Parse(rtcp_block)) {
873 ++num_skipped_packets_;
874 return;
875 }
876
danilchapec067e92017-02-21 05:38:19 -0800877 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700878
danilchap92ea6012016-09-23 10:36:03 -0700879 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000880
danilchapec067e92017-02-21 05:38:19 -0800881 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700882}
883
danilchap8bab7962016-12-20 02:46:46 -0800884void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
885 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700886 rtcp::RapidResyncRequest sr_req;
887 if (!sr_req.Parse(rtcp_block)) {
888 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000889 return;
890 }
891
danilchap92ea6012016-09-23 10:36:03 -0700892 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000893}
894
danilchap1b1863a2016-09-20 01:39:54 -0700895void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700896 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100897 {
898 rtcp::Remb remb;
899 if (remb.Parse(rtcp_block)) {
900 packet_information->packet_type_flags |= kRtcpRemb;
901 packet_information->receiver_estimated_max_bitrate_bps =
902 remb.bitrate_bps();
903 return;
904 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000905 }
danilchap1b1863a2016-09-20 01:39:54 -0700906
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100907 {
908 auto loss_notification = absl::make_unique<rtcp::LossNotification>();
909 if (loss_notification->Parse(rtcp_block)) {
910 packet_information->packet_type_flags |= kRtcpLossNotification;
911 packet_information->loss_notification = std::move(loss_notification);
912 return;
913 }
914 }
915
916 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
917
danilchap1b1863a2016-09-20 01:39:54 -0700918 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000919}
920
danilchap8bab7962016-12-20 02:46:46 -0800921void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700922 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700923 rtcp::Fir fir;
924 if (!fir.Parse(rtcp_block)) {
925 ++num_skipped_packets_;
926 return;
927 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000928
danilchap1b1863a2016-09-20 01:39:54 -0700929 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800930 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700931 if (main_ssrc_ != fir_request.ssrc)
932 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200933
934 ++packet_type_counter_.fir_packets;
935
danilchapefa966b2017-02-17 06:23:15 -0800936 int64_t now_ms = clock_->TimeInMilliseconds();
937 auto inserted = last_fir_.insert(std::make_pair(
938 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
939 if (!inserted.second) { // There was already an entry.
940 LastFirStatus* last_fir = &inserted.first->second;
941
danilchap7851bda2016-09-29 15:28:07 -0700942 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800943 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700944 continue;
945
danilchap7851bda2016-09-29 15:28:07 -0700946 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800947 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700948 continue;
949
danilchapefa966b2017-02-17 06:23:15 -0800950 last_fir->request_ms = now_ms;
951 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200952 }
danilchap7851bda2016-09-29 15:28:07 -0700953 // Received signal that we need to send a new key frame.
954 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000955 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000956}
957
Erik Språng6b8d3552015-09-24 15:06:57 +0200958void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700959 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700960 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700961 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
962 new rtcp::TransportFeedback());
963 if (!transport_feedback->Parse(rtcp_block)) {
964 ++num_skipped_packets_;
965 return;
966 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200967
danilchap92ea6012016-09-23 10:36:03 -0700968 packet_information->packet_type_flags |= kRtcpTransportFeedback;
969 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200970}
danilchap287e5482016-08-16 15:15:39 -0700971
danilchap9bf610e2017-02-20 06:03:01 -0800972void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700973 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700974 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700975 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000976
spranga790d832016-12-02 07:29:44 -0800977 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700978 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700979 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
980 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800981 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000982 }
danilchap853ecb22016-08-22 08:26:15 -0700983
danilchap9bf610e2017-02-20 06:03:01 -0800984 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800985 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000986}
987
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000988void RTCPReceiver::RegisterRtcpStatisticsCallback(
989 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800990 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000991 stats_callback_ = callback;
992}
993
994RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800995 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000996 return stats_callback_;
997}
998
Henrik Boströmf2047872019-05-16 13:32:20 +0200999void RTCPReceiver::SetReportBlockDataObserver(
1000 ReportBlockDataObserver* observer) {
1001 rtc::CritScope cs(&feedbacks_lock_);
1002 report_block_data_observer_ = observer;
1003}
1004
danilchap8bab7962016-12-20 02:46:46 -08001005// Holding no Critical section.
1006void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -07001007 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001008 // Process TMMBR and REMB first to avoid multiple callbacks
1009 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -07001010 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001011 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -08001012 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001013 }
sprang7dc39f32015-10-13 09:17:48 -07001014 uint32_t local_ssrc;
1015 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001016 {
1017 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -08001018 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +00001019 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -07001020 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001021 }
danilchap92ea6012016-09-23 10:36:03 -07001022 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001023 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001024 }
danilchap92ea6012016-09-23 10:36:03 -07001025 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1026 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001027 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1028 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001029 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001030 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001031 }
spranga790d832016-12-02 07:29:44 -08001032
1033 // We need feedback that we have received a report block(s) so that we
1034 // can generate a new packet in a conference relay scenario, one received
1035 // report can generate several RTCP packets, based on number relayed/mixed
1036 // a send report block should go out to all receivers.
1037 if (rtcp_intra_frame_observer_) {
1038 RTC_DCHECK(!receiver_only_);
1039 if ((packet_information.packet_type_flags & kRtcpPli) ||
1040 (packet_information.packet_type_flags & kRtcpFir)) {
1041 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001042 RTC_LOG(LS_VERBOSE)
1043 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001044 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001045 RTC_LOG(LS_VERBOSE)
1046 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001047 }
spranga790d832016-12-02 07:29:44 -08001048 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001049 }
spranga790d832016-12-02 07:29:44 -08001050 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001051 if (rtcp_loss_notification_observer_ &&
1052 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1053 rtcp::LossNotification* loss_notification =
1054 packet_information.loss_notification.get();
1055 RTC_DCHECK(loss_notification);
1056 if (loss_notification->media_ssrc() == local_ssrc) {
1057 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1058 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1059 loss_notification->last_received(),
1060 loss_notification->decodability_flag());
1061 }
1062 }
spranga790d832016-12-02 07:29:44 -08001063 if (rtcp_bandwidth_observer_) {
1064 RTC_DCHECK(!receiver_only_);
1065 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001066 RTC_LOG(LS_VERBOSE)
1067 << "Incoming REMB: "
1068 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001069 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1070 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001071 }
danilchap92ea6012016-09-23 10:36:03 -07001072 if ((packet_information.packet_type_flags & kRtcpSr) ||
1073 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001074 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001075 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001076 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001077 }
spranga790d832016-12-02 07:29:44 -08001078 }
1079 if ((packet_information.packet_type_flags & kRtcpSr) ||
1080 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001081 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001082 }
isheriff6b4b5f32016-06-08 00:24:21 -07001083
spranga790d832016-12-02 07:29:44 -08001084 if (transport_feedback_observer_ &&
1085 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1086 uint32_t media_source_ssrc =
1087 packet_information.transport_feedback->media_ssrc();
1088 if (media_source_ssrc == local_ssrc ||
1089 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1090 transport_feedback_observer_->OnTransportFeedback(
1091 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001092 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001093 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001094
Sebastian Janssone1795f42019-07-24 11:38:03 +02001095 if (network_state_estimate_observer_ &&
1096 packet_information.network_state_estimate) {
1097 network_state_estimate_observer_->OnRemoteNetworkEstimate(
1098 *packet_information.network_state_estimate);
1099 }
1100
spranga790d832016-12-02 07:29:44 -08001101 if (bitrate_allocation_observer_ &&
1102 packet_information.target_bitrate_allocation) {
1103 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1104 *packet_information.target_bitrate_allocation);
1105 }
1106
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001107 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001108 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001109 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001110 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001111 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001112 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001113 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001114 report_block.extended_highest_sequence_number;
1115 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001116 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001117
srte3e69e5c2017-08-09 06:13:45 -07001118 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001119 }
1120 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001121 if (report_block_data_observer_) {
1122 for (const auto& report_block_data :
1123 packet_information.report_block_datas) {
1124 report_block_data_observer_->OnReportBlockDataUpdated(
1125 report_block_data);
1126 }
1127 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001128 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001129}
1130
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001131int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001132 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001133 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001134
danilchap8bab7962016-12-20 02:46:46 -08001135 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001136 auto received_cname_it = received_cnames_.find(remoteSSRC);
1137 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001138 return -1;
danilchap95321242016-09-27 07:05:32 -07001139
1140 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1141 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001142 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001143}
1144
danilchap7851bda2016-09-29 15:28:07 -07001145std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001146 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001147 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001148
danilchap8bab7962016-12-20 02:46:46 -08001149 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001150 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001151
danilchap9bf610e2017-02-20 06:03:01 -08001152 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001153 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001154 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001155 // Erase timeout entries.
1156 it = kv.second.tmmbr.erase(it);
1157 } else {
1158 candidates.push_back(it->second.tmmbr_item);
1159 ++it;
1160 }
1161 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001162 }
danilchap287e5482016-08-16 15:15:39 -07001163 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001164}
1165
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001166} // namespace webrtc