blob: 7754ab13e9dbcf8c71afd0b9402cb95888b6fc41 [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) {
Sebastian Janssona72d5832019-07-25 09:50:48 +0200701 rtcp::App app;
702 if (app.Parse(rtcp_block)) {
703 if (app.name() == rtcp::RemoteEstimate::kName &&
704 app.sub_type() == rtcp::RemoteEstimate::kSubType) {
705 rtcp::RemoteEstimate estimate(std::move(app));
706 if (estimate.ParseData()) {
707 packet_information->network_state_estimate = estimate.estimate();
708 return;
709 }
Sebastian Janssone1795f42019-07-24 11:38:03 +0200710 }
711 }
712 ++num_skipped_packets_;
713}
714
danilchap8bab7962016-12-20 02:46:46 -0800715void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700716 rtcp::Bye bye;
717 if (!bye.Parse(rtcp_block)) {
718 ++num_skipped_packets_;
719 return;
720 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000721
danilchap8bab7962016-12-20 02:46:46 -0800722 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700723 for (auto& reports_per_receiver : received_report_blocks_)
724 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000725
danilchap9bf610e2017-02-20 06:03:01 -0800726 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
727 if (tmmbr_info)
728 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000729
danilchapefa966b2017-02-17 06:23:15 -0800730 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700731 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200732 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
733 if (it != received_rrtrs_ssrc_it_.end()) {
734 received_rrtrs_.erase(it->second);
735 received_rrtrs_ssrc_it_.erase(it);
736 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000737 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000738}
739
danilchap1b1863a2016-09-20 01:39:54 -0700740void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700741 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700742 rtcp::ExtendedReports xr;
743 if (!xr.Parse(rtcp_block)) {
744 ++num_skipped_packets_;
745 return;
746 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000747
danilchap80ac24d2016-10-31 08:40:47 -0700748 if (xr.rrtr())
749 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000750
danilchap80ac24d2016-10-31 08:40:47 -0700751 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
752 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800753
sprangb32aaf92017-08-28 05:49:12 -0700754 if (xr.target_bitrate()) {
755 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
756 packet_information);
757 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000758}
759
danilchap8bab7962016-12-20 02:46:46 -0800760void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
761 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200762 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200763 uint32_t local_receive_mid_ntp_time =
764 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200765
766 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
767 if (it != received_rrtrs_ssrc_it_.end()) {
768 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
769 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
770 } else {
771 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
772 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
773 local_receive_mid_ntp_time);
774 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
775 } else {
776 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
777 << ", reached maximum number of stored RRTRs.";
778 }
779 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000780}
781
danilchap92ea6012016-09-23 10:36:03 -0700782void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700783 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
784 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000785
danilchap1b1863a2016-09-20 01:39:54 -0700786 // Caller should explicitly enable rtt calculation using extended reports.
787 if (!xr_rrtr_status_)
788 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200789
danilchap1b1863a2016-09-20 01:39:54 -0700790 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800791 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700792 // RFC3611, section 4.5, LRR field discription states:
793 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800794 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700795 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200796
danilchap8bab7962016-12-20 02:46:46 -0800797 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200798 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200799
danilchap8bab7962016-12-20 02:46:46 -0800800 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700801 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000802}
803
spranga790d832016-12-02 07:29:44 -0800804void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700805 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800806 const rtcp::TargetBitrate& target_bitrate,
807 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700808 if (ssrc != remote_ssrc_) {
809 return; // Not for us.
810 }
811
Erik Språng566124a2018-04-23 12:32:22 +0200812 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800813 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800814 if (item.spatial_layer >= kMaxSpatialLayers ||
815 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100816 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800817 << "Invalid layer in XR target bitrate pack: spatial index "
818 << item.spatial_layer << ", temporal index " << item.temporal_layer
819 << ", dropping.";
820 } else {
821 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
822 item.target_bitrate_kbps * 1000);
823 }
spranga790d832016-12-02 07:29:44 -0800824 }
825 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
826}
827
danilchap8bab7962016-12-20 02:46:46 -0800828void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700829 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700830 rtcp::Pli pli;
831 if (!pli.Parse(rtcp_block)) {
832 ++num_skipped_packets_;
833 return;
834 }
835
836 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000837 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000838 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700839 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000840 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000841}
842
danilchap8bab7962016-12-20 02:46:46 -0800843void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700844 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700845 rtcp::Tmmbr tmmbr;
846 if (!tmmbr.Parse(rtcp_block)) {
847 ++num_skipped_packets_;
848 return;
849 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000850
danilchap7851bda2016-09-29 15:28:07 -0700851 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700852 if (tmmbr.media_ssrc()) {
853 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
854 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700855 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000856 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000857
danilchap1b1863a2016-09-20 01:39:54 -0700858 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800859 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
860 continue;
danilchap7851bda2016-09-29 15:28:07 -0700861
danilchapec067e92017-02-21 05:38:19 -0800862 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
863 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200864 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800865 request.packet_overhead());
866 entry->last_updated_ms = clock_->TimeInMilliseconds();
867
868 packet_information->packet_type_flags |= kRtcpTmmbr;
869 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000870 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000871}
872
danilchap8bab7962016-12-20 02:46:46 -0800873void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700874 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700875 rtcp::Tmmbn tmmbn;
876 if (!tmmbn.Parse(rtcp_block)) {
877 ++num_skipped_packets_;
878 return;
879 }
880
danilchapec067e92017-02-21 05:38:19 -0800881 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700882
danilchap92ea6012016-09-23 10:36:03 -0700883 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000884
danilchapec067e92017-02-21 05:38:19 -0800885 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700886}
887
danilchap8bab7962016-12-20 02:46:46 -0800888void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
889 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700890 rtcp::RapidResyncRequest sr_req;
891 if (!sr_req.Parse(rtcp_block)) {
892 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000893 return;
894 }
895
danilchap92ea6012016-09-23 10:36:03 -0700896 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000897}
898
danilchap1b1863a2016-09-20 01:39:54 -0700899void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700900 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100901 {
902 rtcp::Remb remb;
903 if (remb.Parse(rtcp_block)) {
904 packet_information->packet_type_flags |= kRtcpRemb;
905 packet_information->receiver_estimated_max_bitrate_bps =
906 remb.bitrate_bps();
907 return;
908 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000909 }
danilchap1b1863a2016-09-20 01:39:54 -0700910
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100911 {
912 auto loss_notification = absl::make_unique<rtcp::LossNotification>();
913 if (loss_notification->Parse(rtcp_block)) {
914 packet_information->packet_type_flags |= kRtcpLossNotification;
915 packet_information->loss_notification = std::move(loss_notification);
916 return;
917 }
918 }
919
920 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
921
danilchap1b1863a2016-09-20 01:39:54 -0700922 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000923}
924
danilchap8bab7962016-12-20 02:46:46 -0800925void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700926 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700927 rtcp::Fir fir;
928 if (!fir.Parse(rtcp_block)) {
929 ++num_skipped_packets_;
930 return;
931 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000932
danilchap1b1863a2016-09-20 01:39:54 -0700933 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800934 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700935 if (main_ssrc_ != fir_request.ssrc)
936 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200937
938 ++packet_type_counter_.fir_packets;
939
danilchapefa966b2017-02-17 06:23:15 -0800940 int64_t now_ms = clock_->TimeInMilliseconds();
941 auto inserted = last_fir_.insert(std::make_pair(
942 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
943 if (!inserted.second) { // There was already an entry.
944 LastFirStatus* last_fir = &inserted.first->second;
945
danilchap7851bda2016-09-29 15:28:07 -0700946 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800947 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700948 continue;
949
danilchap7851bda2016-09-29 15:28:07 -0700950 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800951 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700952 continue;
953
danilchapefa966b2017-02-17 06:23:15 -0800954 last_fir->request_ms = now_ms;
955 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200956 }
danilchap7851bda2016-09-29 15:28:07 -0700957 // Received signal that we need to send a new key frame.
958 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000959 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000960}
961
Erik Språng6b8d3552015-09-24 15:06:57 +0200962void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700963 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700964 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700965 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
966 new rtcp::TransportFeedback());
967 if (!transport_feedback->Parse(rtcp_block)) {
968 ++num_skipped_packets_;
969 return;
970 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200971
danilchap92ea6012016-09-23 10:36:03 -0700972 packet_information->packet_type_flags |= kRtcpTransportFeedback;
973 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200974}
danilchap287e5482016-08-16 15:15:39 -0700975
danilchap9bf610e2017-02-20 06:03:01 -0800976void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700977 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700978 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700979 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000980
spranga790d832016-12-02 07:29:44 -0800981 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700982 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700983 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
984 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800985 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000986 }
danilchap853ecb22016-08-22 08:26:15 -0700987
danilchap9bf610e2017-02-20 06:03:01 -0800988 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800989 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000990}
991
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000992void RTCPReceiver::RegisterRtcpStatisticsCallback(
993 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800994 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000995 stats_callback_ = callback;
996}
997
998RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800999 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001000 return stats_callback_;
1001}
1002
Henrik Boströmf2047872019-05-16 13:32:20 +02001003void RTCPReceiver::SetReportBlockDataObserver(
1004 ReportBlockDataObserver* observer) {
1005 rtc::CritScope cs(&feedbacks_lock_);
1006 report_block_data_observer_ = observer;
1007}
1008
danilchap8bab7962016-12-20 02:46:46 -08001009// Holding no Critical section.
1010void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -07001011 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001012 // Process TMMBR and REMB first to avoid multiple callbacks
1013 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -07001014 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001015 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -08001016 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001017 }
sprang7dc39f32015-10-13 09:17:48 -07001018 uint32_t local_ssrc;
1019 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001020 {
1021 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -08001022 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +00001023 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -07001024 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001025 }
danilchap92ea6012016-09-23 10:36:03 -07001026 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001027 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001028 }
danilchap92ea6012016-09-23 10:36:03 -07001029 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1030 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001031 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1032 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001033 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001034 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001035 }
spranga790d832016-12-02 07:29:44 -08001036
1037 // We need feedback that we have received a report block(s) so that we
1038 // can generate a new packet in a conference relay scenario, one received
1039 // report can generate several RTCP packets, based on number relayed/mixed
1040 // a send report block should go out to all receivers.
1041 if (rtcp_intra_frame_observer_) {
1042 RTC_DCHECK(!receiver_only_);
1043 if ((packet_information.packet_type_flags & kRtcpPli) ||
1044 (packet_information.packet_type_flags & kRtcpFir)) {
1045 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001046 RTC_LOG(LS_VERBOSE)
1047 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001048 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001049 RTC_LOG(LS_VERBOSE)
1050 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001051 }
spranga790d832016-12-02 07:29:44 -08001052 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
spranga790d832016-12-02 07:29:44 -08001054 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001055 if (rtcp_loss_notification_observer_ &&
1056 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1057 rtcp::LossNotification* loss_notification =
1058 packet_information.loss_notification.get();
1059 RTC_DCHECK(loss_notification);
1060 if (loss_notification->media_ssrc() == local_ssrc) {
1061 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1062 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1063 loss_notification->last_received(),
1064 loss_notification->decodability_flag());
1065 }
1066 }
spranga790d832016-12-02 07:29:44 -08001067 if (rtcp_bandwidth_observer_) {
1068 RTC_DCHECK(!receiver_only_);
1069 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001070 RTC_LOG(LS_VERBOSE)
1071 << "Incoming REMB: "
1072 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001073 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1074 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001075 }
danilchap92ea6012016-09-23 10:36:03 -07001076 if ((packet_information.packet_type_flags & kRtcpSr) ||
1077 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001078 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001079 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001080 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001081 }
spranga790d832016-12-02 07:29:44 -08001082 }
1083 if ((packet_information.packet_type_flags & kRtcpSr) ||
1084 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001085 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001086 }
isheriff6b4b5f32016-06-08 00:24:21 -07001087
spranga790d832016-12-02 07:29:44 -08001088 if (transport_feedback_observer_ &&
1089 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1090 uint32_t media_source_ssrc =
1091 packet_information.transport_feedback->media_ssrc();
1092 if (media_source_ssrc == local_ssrc ||
1093 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1094 transport_feedback_observer_->OnTransportFeedback(
1095 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001096 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001097 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001098
Sebastian Janssone1795f42019-07-24 11:38:03 +02001099 if (network_state_estimate_observer_ &&
1100 packet_information.network_state_estimate) {
1101 network_state_estimate_observer_->OnRemoteNetworkEstimate(
1102 *packet_information.network_state_estimate);
1103 }
1104
spranga790d832016-12-02 07:29:44 -08001105 if (bitrate_allocation_observer_ &&
1106 packet_information.target_bitrate_allocation) {
1107 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1108 *packet_information.target_bitrate_allocation);
1109 }
1110
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001111 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001112 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001113 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001114 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001115 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001116 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001117 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001118 report_block.extended_highest_sequence_number;
1119 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001120 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001121
srte3e69e5c2017-08-09 06:13:45 -07001122 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001123 }
1124 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001125 if (report_block_data_observer_) {
1126 for (const auto& report_block_data :
1127 packet_information.report_block_datas) {
1128 report_block_data_observer_->OnReportBlockDataUpdated(
1129 report_block_data);
1130 }
1131 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001132 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001133}
1134
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001135int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001136 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001137 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001138
danilchap8bab7962016-12-20 02:46:46 -08001139 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001140 auto received_cname_it = received_cnames_.find(remoteSSRC);
1141 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001142 return -1;
danilchap95321242016-09-27 07:05:32 -07001143
1144 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1145 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001146 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001147}
1148
danilchap7851bda2016-09-29 15:28:07 -07001149std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001150 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001151 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001152
danilchap8bab7962016-12-20 02:46:46 -08001153 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001154 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001155
danilchap9bf610e2017-02-20 06:03:01 -08001156 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001157 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001158 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001159 // Erase timeout entries.
1160 it = kv.second.tmmbr.erase(it);
1161 } else {
1162 candidates.push_back(it->second.tmmbr_item);
1163 ++it;
1164 }
1165 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001166 }
danilchap287e5482016-08-16 15:15:39 -07001167 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168}
1169
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001170} // namespace webrtc