blob: 99b55efb491d62be7c78c011d08bd4f01e542e6a [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"
35#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
39#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
40#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
41#include "modules/rtp_rtcp/source/time_util.h"
42#include "modules/rtp_rtcp/source/tmmbr_help.h"
43#include "rtc_base/checks.h"
44#include "rtc_base/logging.h"
Henrik Boströmf2047872019-05-16 13:32:20 +020045#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/trace_event.h"
47#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000048
niklase@google.com470e71d2011-07-07 08:21:25 +000049namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070050namespace {
51
52using rtcp::CommonHeader;
53using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000054
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000055// The number of RTCP time intervals needed to trigger a timeout.
56const int kRrTimeoutIntervals = 3;
57
Jiawei Ou3587b832018-01-31 22:08:26 -080058const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
59
Erik Språng6b8d3552015-09-24 15:06:57 +020060const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080061const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020062
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020063// Maximum number of received RRTRs that will be stored.
64const size_t kMaxNumberOfStoredRrtrs = 200;
65
Mirko Bonadei3b676722019-07-12 17:35:05 +000066constexpr int32_t kDefaultVideoReportInterval = 1000;
67constexpr int32_t kDefaultAudioReportInterval = 5000;
danilchap1b1863a2016-09-20 01:39:54 -070068} // namespace
69
danilchap92ea6012016-09-23 10:36:03 -070070struct RTCPReceiver::PacketInformation {
71 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
72
73 uint32_t remote_ssrc = 0;
74 std::vector<uint16_t> nack_sequence_numbers;
Henrik Boströmf2047872019-05-16 13:32:20 +020075 // TODO(hbos): Remove |report_blocks| in favor of |report_block_datas|.
danilchap92ea6012016-09-23 10:36:03 -070076 ReportBlockList report_blocks;
Henrik Boströmf2047872019-05-16 13:32:20 +020077 std::vector<ReportBlockData> report_block_datas;
danilchap92ea6012016-09-23 10:36:03 -070078 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070079 uint32_t receiver_estimated_max_bitrate_bps = 0;
80 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020081 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010082 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070083};
84
danilchap9bf610e2017-02-20 06:03:01 -080085// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
86struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070087 struct TimedTmmbrItem {
88 rtcp::TmmbItem tmmbr_item;
89 int64_t last_updated_ms;
90 };
91
92 int64_t last_time_received_ms = 0;
93
danilchap7851bda2016-09-29 15:28:07 -070094 bool ready_for_delete = false;
95
96 std::vector<rtcp::TmmbItem> tmmbn;
97 std::map<uint32_t, TimedTmmbrItem> tmmbr;
98};
99
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200100// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
101struct RTCPReceiver::RrtrInformation {
102 RrtrInformation(uint32_t ssrc,
103 uint32_t received_remote_mid_ntp_time,
104 uint32_t local_receive_mid_ntp_time)
105 : ssrc(ssrc),
106 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
107 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
108
109 uint32_t ssrc;
110 // Received NTP timestamp in compact representation.
111 uint32_t received_remote_mid_ntp_time;
112 // NTP time when the report was received in compact representation.
113 uint32_t local_receive_mid_ntp_time;
114};
115
danilchapefa966b2017-02-17 06:23:15 -0800116struct RTCPReceiver::LastFirStatus {
117 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
118 : request_ms(now_ms), sequence_number(sequence_number) {}
119 int64_t request_ms;
120 uint8_t sequence_number;
121};
122
Mirko Bonadei3b676722019-07-12 17:35:05 +0000123RTCPReceiver::RTCPReceiver(const RtpRtcp::Configuration& config,
124 ModuleRtpRtcp* owner)
125 : clock_(config.clock),
126 receiver_only_(config.receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800127 rtp_rtcp_(owner),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000128 rtcp_bandwidth_observer_(config.bandwidth_callback),
129 rtcp_intra_frame_observer_(config.intra_frame_callback),
130 rtcp_loss_notification_observer_(config.rtcp_loss_notification_observer),
131 transport_feedback_observer_(config.transport_feedback_callback),
132 bitrate_allocation_observer_(config.bitrate_allocation_observer),
133 report_interval_ms_(config.rtcp_report_interval_ms > 0
134 ? config.rtcp_report_interval_ms
135 : (config.audio ? kDefaultAudioReportInterval
136 : kDefaultVideoReportInterval)),
137 main_ssrc_(config.media_send_ssrc.value_or(0)),
danilchap8bab7962016-12-20 02:46:46 -0800138 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700139 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100140 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000141 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700142 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200143 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800144 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800145 stats_callback_(nullptr),
Henrik Boströmf2047872019-05-16 13:32:20 +0200146 report_block_data_observer_(nullptr),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000147 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Erik Språng6b8d3552015-09-24 15:06:57 +0200148 num_skipped_packets_(0),
Mirko Bonadei3b676722019-07-12 17:35:05 +0000149 last_skipped_packets_warning_ms_(clock_->TimeInMilliseconds()) {
danilchap8bab7962016-12-20 02:46:46 -0800150 RTC_DCHECK(owner);
Mirko Bonadei3b676722019-07-12 17:35:05 +0000151 if (config.media_send_ssrc) {
152 registered_ssrcs_.insert(*config.media_send_ssrc);
153 }
154 if (config.rtx_send_ssrc) {
155 registered_ssrcs_.insert(*config.rtx_send_ssrc);
156 }
157 if (config.flexfec_sender) {
158 registered_ssrcs_.insert(config.flexfec_sender->ssrc());
159 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
161
danilchap28b03eb2016-10-05 06:59:44 -0700162RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
nisse479d3d72017-09-13 07:53:37 -0700164void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700165 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100166 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700167 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700168 }
danilchap59cb2bd2016-08-29 11:08:47 -0700169
danilchap92ea6012016-09-23 10:36:03 -0700170 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700171 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700172 return;
danilchap8bab7962016-12-20 02:46:46 -0800173 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174}
175
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200176int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800177 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200178 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000179}
180
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000181void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800182 rtc::CritScope lock(&rtcp_receiver_lock_);
183 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700184 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800185 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000188uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800189 rtc::CritScope lock(&rtcp_receiver_lock_);
190 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000191}
192
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000193void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
194 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800195 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700196 main_ssrc_ = main_ssrc;
197 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
danilchap28b03eb2016-10-05 06:59:44 -0700200int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
201 int64_t* last_rtt_ms,
202 int64_t* avg_rtt_ms,
203 int64_t* min_rtt_ms,
204 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800205 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
danilchap28b03eb2016-10-05 06:59:44 -0700207 auto it = received_report_blocks_.find(main_ssrc_);
208 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000209 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700210
211 auto it_info = it->second.find(remote_ssrc);
212 if (it_info == it->second.end())
213 return -1;
214
Henrik Boströmf2047872019-05-16 13:32:20 +0200215 const ReportBlockData* report_block_data = &it_info->second;
danilchap28b03eb2016-10-05 06:59:44 -0700216
Henrik Boströmf2047872019-05-16 13:32:20 +0200217 if (report_block_data->num_rtts() == 0)
danilchap28b03eb2016-10-05 06:59:44 -0700218 return -1;
219
220 if (last_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200221 *last_rtt_ms = report_block_data->last_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700222
Henrik Boströmf2047872019-05-16 13:32:20 +0200223 if (avg_rtt_ms) {
224 *avg_rtt_ms =
225 report_block_data->sum_rtt_ms() / report_block_data->num_rtts();
226 }
danilchap28b03eb2016-10-05 06:59:44 -0700227
228 if (min_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200229 *min_rtt_ms = report_block_data->min_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700230
231 if (max_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200232 *max_rtt_ms = report_block_data->max_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700233
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000234 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100237void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800238 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100239 xr_rrtr_status_ = enable;
240}
241
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000242bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800243 RTC_DCHECK(rtt_ms);
244 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000245 if (xr_rr_rtt_ms_ == 0) {
246 return false;
247 }
248 *rtt_ms = xr_rr_rtt_ms_;
249 xr_rr_rtt_ms_ = 0;
250 return true;
251}
252
danilchap8bab7962016-12-20 02:46:46 -0800253bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
254 uint32_t* received_ntp_frac,
255 uint32_t* rtcp_arrival_time_secs,
256 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700257 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800258 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700259 if (!last_received_sr_ntp_.Valid())
260 return false;
261
262 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800263 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700264 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800265 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700266 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700267
268 // Rtp time from incoming SenderReport.
269 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700270 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700271
272 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800273 if (rtcp_arrival_time_secs)
274 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
275 if (rtcp_arrival_time_frac)
276 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700277
danilchapda161d72016-08-19 07:29:46 -0700278 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000279}
280
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200281std::vector<rtcp::ReceiveTimeInfo>
282RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800283 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000284
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200285 const size_t last_xr_rtis_size = std::min(
286 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
287 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
288 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000289
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200290 const uint32_t now_ntp =
291 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000292
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200293 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
294 RrtrInformation& rrtr = received_rrtrs_.front();
295 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
296 now_ntp - rrtr.local_receive_mid_ntp_time);
297 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
298 received_rrtrs_.pop_front();
299 }
300
301 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000302}
303
danilchap28b03eb2016-10-05 06:59:44 -0700304// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000305int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700306 std::vector<RTCPReportBlock>* receive_blocks) const {
307 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800308 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700309 for (const auto& reports_per_receiver : received_report_blocks_)
310 for (const auto& report : reports_per_receiver.second)
Henrik Boströmf2047872019-05-16 13:32:20 +0200311 receive_blocks->push_back(report.second.report_block());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000312 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
Henrik Boströmf2047872019-05-16 13:32:20 +0200315std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
316 std::vector<ReportBlockData> result;
317 rtc::CritScope lock(&rtcp_receiver_lock_);
318 for (const auto& reports_per_receiver : received_report_blocks_)
319 for (const auto& report : reports_per_receiver.second)
320 result.push_back(report.second);
321 return result;
322}
323
danilchap92ea6012016-09-23 10:36:03 -0700324bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
325 const uint8_t* packet_end,
326 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800327 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
danilchap1b1863a2016-09-20 01:39:54 -0700329 CommonHeader rtcp_block;
330 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
331 next_block = rtcp_block.NextPacket()) {
332 ptrdiff_t remaining_blocks_size = packet_end - next_block;
333 RTC_DCHECK_GT(remaining_blocks_size, 0);
334 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
335 if (next_block == packet_begin) {
336 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100337 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700338 return false;
339 }
340 ++num_skipped_packets_;
341 break;
342 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
danilchap1b1863a2016-09-20 01:39:54 -0700344 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800345 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700346
danilchap1b1863a2016-09-20 01:39:54 -0700347 switch (rtcp_block.type()) {
348 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700349 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200350 break;
danilchap1b1863a2016-09-20 01:39:54 -0700351 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700352 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700353 break;
danilchap1b1863a2016-09-20 01:39:54 -0700354 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800355 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700356 break;
danilchap1b1863a2016-09-20 01:39:54 -0700357 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700358 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700359 break;
danilchap1b1863a2016-09-20 01:39:54 -0700360 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800361 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700362 break;
danilchap1b1863a2016-09-20 01:39:54 -0700363 case rtcp::Rtpfb::kPacketType:
364 switch (rtcp_block.fmt()) {
365 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800366 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700367 break;
368 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800369 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700370 break;
371 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800372 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700373 break;
374 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800375 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700376 break;
377 case rtcp::TransportFeedback::kFeedbackMessageType:
378 HandleTransportFeedback(rtcp_block, packet_information);
379 break;
380 default:
381 ++num_skipped_packets_;
382 break;
383 }
danilchapda161d72016-08-19 07:29:46 -0700384 break;
danilchap1b1863a2016-09-20 01:39:54 -0700385 case rtcp::Psfb::kPacketType:
386 switch (rtcp_block.fmt()) {
387 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800388 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700389 break;
danilchap1b1863a2016-09-20 01:39:54 -0700390 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800391 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700392 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100393 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700394 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700395 break;
396 default:
397 ++num_skipped_packets_;
398 break;
399 }
danilchapda161d72016-08-19 07:29:46 -0700400 break;
401 default:
danilchap1b1863a2016-09-20 01:39:54 -0700402 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700403 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000404 }
danilchapda161d72016-08-19 07:29:46 -0700405 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000406
danilchap8bab7962016-12-20 02:46:46 -0800407 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700408 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
409 main_ssrc_, packet_type_counter_);
410 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000411
danilchap8bab7962016-12-20 02:46:46 -0800412 int64_t now_ms = clock_->TimeInMilliseconds();
413 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700414 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800415 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100416 RTC_LOG(LS_WARNING)
417 << num_skipped_packets_
418 << " RTCP blocks were skipped due to being malformed or of "
419 "unrecognized/unsupported type, during the past "
420 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700421 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200422
danilchap1b1863a2016-09-20 01:39:54 -0700423 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000424}
425
danilchap92ea6012016-09-23 10:36:03 -0700426void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
427 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700428 rtcp::SenderReport sender_report;
429 if (!sender_report.Parse(rtcp_block)) {
430 ++num_skipped_packets_;
431 return;
432 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
danilchap8bab7962016-12-20 02:46:46 -0800434 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
danilchap8bab7962016-12-20 02:46:46 -0800436 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437
danilchapec067e92017-02-21 05:38:19 -0800438 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700439
Danil Chapovalov91511f12016-09-15 16:24:02 +0200440 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800441 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200442 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700443 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700444
danilchapa04d9c32017-07-25 04:03:39 -0700445 remote_sender_ntp_time_ = sender_report.ntp();
446 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200447 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700448 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200449 // We will only store the send report from one source, but
450 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700451 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700452 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000453
Mirko Bonadei739baf02019-01-27 17:29:42 +0100454 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800455 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
danilchap92ea6012016-09-23 10:36:03 -0700458void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
459 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700460 rtcp::ReceiverReport receiver_report;
461 if (!receiver_report.Parse(rtcp_block)) {
462 ++num_skipped_packets_;
463 return;
464 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200465
danilchap8bab7962016-12-20 02:46:46 -0800466 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200467
danilchap8bab7962016-12-20 02:46:46 -0800468 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200469
danilchapec067e92017-02-21 05:38:19 -0800470 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200471
danilchap92ea6012016-09-23 10:36:03 -0700472 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200473
danilchap1b1863a2016-09-20 01:39:54 -0700474 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800475 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200476}
477
danilchap92ea6012016-09-23 10:36:03 -0700478void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
479 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700480 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000481 // This will be called once per report block in the RTCP packet.
482 // We filter out all report blocks that are not for us.
483 // Each packet has max 31 RR blocks.
484 //
485 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000486
danilchap28b03eb2016-10-05 06:59:44 -0700487 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000488 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000490 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700491 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000492 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000493
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200494 last_received_rb_ms_ = clock_->TimeInMilliseconds();
495
Henrik Boströmf2047872019-05-16 13:32:20 +0200496 ReportBlockData* report_block_data =
danilchap28b03eb2016-10-05 06:59:44 -0700497 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
Henrik Boströmf2047872019-05-16 13:32:20 +0200498 RTCPReportBlock rtcp_report_block;
499 rtcp_report_block.sender_ssrc = remote_ssrc;
500 rtcp_report_block.source_ssrc = report_block.source_ssrc();
501 rtcp_report_block.fraction_lost = report_block.fraction_lost();
502 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700503 if (report_block.extended_high_seq_num() >
Henrik Boströmf2047872019-05-16 13:32:20 +0200504 report_block_data->report_block().extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000505 // We have successfully delivered new RTP packets to the remote side after
506 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800507 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000508 }
Henrik Boströmf2047872019-05-16 13:32:20 +0200509 rtcp_report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700510 report_block.extended_high_seq_num();
Henrik Boströmf2047872019-05-16 13:32:20 +0200511 rtcp_report_block.jitter = report_block.jitter();
512 rtcp_report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700513 report_block.delay_since_last_sr();
Henrik Boströmf2047872019-05-16 13:32:20 +0200514 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
515 report_block_data->SetReportBlock(rtcp_report_block, rtc::TimeUTCMicros());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000516
danilchap28b03eb2016-10-05 06:59:44 -0700517 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800518 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100519 // RFC3550, section 6.4.1, LSR field discription states:
520 // If no SR has been received yet, the field is set to zero.
521 // Receiver rtp_rtcp module is not expected to calculate rtt using
522 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100523
524 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
525 // is false. However, that currently breaks the tests of the
526 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
527 // delete all dependencies on RTT measurements for audio receive streams, or
528 // ensure that audio receive streams that need RTT and stats that depend on it
529 // are configured with an associated audio send stream.
530 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800531 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100532 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200533 uint32_t receive_time_ntp =
534 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000535
Danil Chapovalova094fd12016-02-22 18:59:36 +0100536 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800537 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100538 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700539 rtt_ms = CompactNtpRttToMs(rtt_ntp);
Henrik Boströmf2047872019-05-16 13:32:20 +0200540 report_block_data->AddRoundTripTimeSample(rtt_ms);
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100541
542 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000543 }
544
Henrik Boströmf2047872019-05-16 13:32:20 +0200545 packet_information->report_blocks.push_back(
546 report_block_data->report_block());
547 packet_information->report_block_datas.push_back(*report_block_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000548}
549
danilchapec067e92017-02-21 05:38:19 -0800550RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
551 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700552 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800553 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700554 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800555 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800556 return tmmbr_info;
557}
558
559void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
560 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
561 if (tmmbr_it != tmmbr_infos_.end())
562 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000563}
564
danilchap9bf610e2017-02-20 06:03:01 -0800565RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700566 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800567 auto it = tmmbr_infos_.find(remote_ssrc);
568 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700569 return nullptr;
570 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000571}
572
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800573bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800574 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200575 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000576 return false;
577
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800578 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200579 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000580 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200581 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000582 return true;
583 }
584 return false;
585}
586
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800587bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800588 rtc::CritScope lock(&rtcp_receiver_lock_);
589 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000590 return false;
591
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800592 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800593 if (clock_->TimeInMilliseconds() >
594 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000595 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800596 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000597 return true;
598 }
599 return false;
600}
601
danilchap9bf610e2017-02-20 06:03:01 -0800602bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800603 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000604
danilchap8bab7962016-12-20 02:46:46 -0800605 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800606 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000607
danilchap9bf610e2017-02-20 06:03:01 -0800608 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800609 return false;
610
611 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800612 oldest_tmmbr_info_ms_ = -1;
613 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
614 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
615 if (tmmbr_info->last_time_received_ms > 0) {
616 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700617 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800618 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700619 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800620 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700621 // Send new TMMBN to all channels using the default codec.
622 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800623 } else if (oldest_tmmbr_info_ms_ == -1 ||
624 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
625 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000626 }
danilchap9bf610e2017-02-20 06:03:01 -0800627 ++tmmbr_it;
628 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700629 // When we dont have a last_time_received_ms and the object is marked
630 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800631 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000632 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800633 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000634 }
635 }
danilchap7851bda2016-09-29 15:28:07 -0700636 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000637}
638
danilchap2b616392016-08-18 06:17:42 -0700639std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800640 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800641 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
642 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700643 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700644
danilchap9bf610e2017-02-20 06:03:01 -0800645 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
646 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647}
648
danilchap8bab7962016-12-20 02:46:46 -0800649void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700650 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700651 rtcp::Sdes sdes;
652 if (!sdes.Parse(rtcp_block)) {
653 ++num_skipped_packets_;
654 return;
655 }
656
657 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700658 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200659 {
spranga790d832016-12-02 07:29:44 -0800660 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700661 if (stats_callback_)
662 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200663 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000664 }
danilchap92ea6012016-09-23 10:36:03 -0700665 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000666}
667
danilchap8bab7962016-12-20 02:46:46 -0800668void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700669 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700670 rtcp::Nack nack;
671 if (!nack.Parse(rtcp_block)) {
672 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000673 return;
674 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000675
danilchap1b1863a2016-09-20 01:39:54 -0700676 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
677 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200678
danilchap142f0192016-10-20 08:22:42 -0700679 packet_information->nack_sequence_numbers.insert(
680 packet_information->nack_sequence_numbers.end(),
681 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700682 for (uint16_t packet_id : nack.packet_ids())
683 nack_stats_.ReportRequest(packet_id);
684
685 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700686 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000687 ++packet_type_counter_.nack_packets;
688 packet_type_counter_.nack_requests = nack_stats_.requests();
689 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
690 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000691}
692
danilchap8bab7962016-12-20 02:46:46 -0800693void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700694 rtcp::Bye bye;
695 if (!bye.Parse(rtcp_block)) {
696 ++num_skipped_packets_;
697 return;
698 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000699
danilchap8bab7962016-12-20 02:46:46 -0800700 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700701 for (auto& reports_per_receiver : received_report_blocks_)
702 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000703
danilchap9bf610e2017-02-20 06:03:01 -0800704 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
705 if (tmmbr_info)
706 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000707
danilchapefa966b2017-02-17 06:23:15 -0800708 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700709 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200710 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
711 if (it != received_rrtrs_ssrc_it_.end()) {
712 received_rrtrs_.erase(it->second);
713 received_rrtrs_ssrc_it_.erase(it);
714 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000715 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000716}
717
danilchap1b1863a2016-09-20 01:39:54 -0700718void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700719 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700720 rtcp::ExtendedReports xr;
721 if (!xr.Parse(rtcp_block)) {
722 ++num_skipped_packets_;
723 return;
724 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000725
danilchap80ac24d2016-10-31 08:40:47 -0700726 if (xr.rrtr())
727 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000728
danilchap80ac24d2016-10-31 08:40:47 -0700729 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
730 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800731
sprangb32aaf92017-08-28 05:49:12 -0700732 if (xr.target_bitrate()) {
733 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
734 packet_information);
735 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000736}
737
danilchap8bab7962016-12-20 02:46:46 -0800738void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
739 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200740 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200741 uint32_t local_receive_mid_ntp_time =
742 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200743
744 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
745 if (it != received_rrtrs_ssrc_it_.end()) {
746 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
747 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
748 } else {
749 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
750 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
751 local_receive_mid_ntp_time);
752 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
753 } else {
754 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
755 << ", reached maximum number of stored RRTRs.";
756 }
757 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000758}
759
danilchap92ea6012016-09-23 10:36:03 -0700760void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700761 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
762 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000763
danilchap1b1863a2016-09-20 01:39:54 -0700764 // Caller should explicitly enable rtt calculation using extended reports.
765 if (!xr_rrtr_status_)
766 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200767
danilchap1b1863a2016-09-20 01:39:54 -0700768 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800769 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700770 // RFC3611, section 4.5, LRR field discription states:
771 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800772 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700773 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200774
danilchap8bab7962016-12-20 02:46:46 -0800775 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200776 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200777
danilchap8bab7962016-12-20 02:46:46 -0800778 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700779 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000780}
781
spranga790d832016-12-02 07:29:44 -0800782void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700783 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800784 const rtcp::TargetBitrate& target_bitrate,
785 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700786 if (ssrc != remote_ssrc_) {
787 return; // Not for us.
788 }
789
Erik Språng566124a2018-04-23 12:32:22 +0200790 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800791 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800792 if (item.spatial_layer >= kMaxSpatialLayers ||
793 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100794 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800795 << "Invalid layer in XR target bitrate pack: spatial index "
796 << item.spatial_layer << ", temporal index " << item.temporal_layer
797 << ", dropping.";
798 } else {
799 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
800 item.target_bitrate_kbps * 1000);
801 }
spranga790d832016-12-02 07:29:44 -0800802 }
803 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
804}
805
danilchap8bab7962016-12-20 02:46:46 -0800806void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700807 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700808 rtcp::Pli pli;
809 if (!pli.Parse(rtcp_block)) {
810 ++num_skipped_packets_;
811 return;
812 }
813
814 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000815 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000816 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700817 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000818 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
danilchap8bab7962016-12-20 02:46:46 -0800821void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700822 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700823 rtcp::Tmmbr tmmbr;
824 if (!tmmbr.Parse(rtcp_block)) {
825 ++num_skipped_packets_;
826 return;
827 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000828
danilchap7851bda2016-09-29 15:28:07 -0700829 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700830 if (tmmbr.media_ssrc()) {
831 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
832 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700833 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000834 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000835
danilchap1b1863a2016-09-20 01:39:54 -0700836 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800837 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
838 continue;
danilchap7851bda2016-09-29 15:28:07 -0700839
danilchapec067e92017-02-21 05:38:19 -0800840 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
841 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200842 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800843 request.packet_overhead());
844 entry->last_updated_ms = clock_->TimeInMilliseconds();
845
846 packet_information->packet_type_flags |= kRtcpTmmbr;
847 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000848 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000849}
850
danilchap8bab7962016-12-20 02:46:46 -0800851void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700852 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700853 rtcp::Tmmbn tmmbn;
854 if (!tmmbn.Parse(rtcp_block)) {
855 ++num_skipped_packets_;
856 return;
857 }
858
danilchapec067e92017-02-21 05:38:19 -0800859 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700860
danilchap92ea6012016-09-23 10:36:03 -0700861 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000862
danilchapec067e92017-02-21 05:38:19 -0800863 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700864}
865
danilchap8bab7962016-12-20 02:46:46 -0800866void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
867 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700868 rtcp::RapidResyncRequest sr_req;
869 if (!sr_req.Parse(rtcp_block)) {
870 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000871 return;
872 }
873
danilchap92ea6012016-09-23 10:36:03 -0700874 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000875}
876
danilchap1b1863a2016-09-20 01:39:54 -0700877void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700878 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100879 {
880 rtcp::Remb remb;
881 if (remb.Parse(rtcp_block)) {
882 packet_information->packet_type_flags |= kRtcpRemb;
883 packet_information->receiver_estimated_max_bitrate_bps =
884 remb.bitrate_bps();
885 return;
886 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000887 }
danilchap1b1863a2016-09-20 01:39:54 -0700888
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100889 {
890 auto loss_notification = absl::make_unique<rtcp::LossNotification>();
891 if (loss_notification->Parse(rtcp_block)) {
892 packet_information->packet_type_flags |= kRtcpLossNotification;
893 packet_information->loss_notification = std::move(loss_notification);
894 return;
895 }
896 }
897
898 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
899
danilchap1b1863a2016-09-20 01:39:54 -0700900 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000901}
902
danilchap8bab7962016-12-20 02:46:46 -0800903void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700904 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700905 rtcp::Fir fir;
906 if (!fir.Parse(rtcp_block)) {
907 ++num_skipped_packets_;
908 return;
909 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000910
danilchap1b1863a2016-09-20 01:39:54 -0700911 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800912 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700913 if (main_ssrc_ != fir_request.ssrc)
914 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200915
916 ++packet_type_counter_.fir_packets;
917
danilchapefa966b2017-02-17 06:23:15 -0800918 int64_t now_ms = clock_->TimeInMilliseconds();
919 auto inserted = last_fir_.insert(std::make_pair(
920 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
921 if (!inserted.second) { // There was already an entry.
922 LastFirStatus* last_fir = &inserted.first->second;
923
danilchap7851bda2016-09-29 15:28:07 -0700924 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800925 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700926 continue;
927
danilchap7851bda2016-09-29 15:28:07 -0700928 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800929 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700930 continue;
931
danilchapefa966b2017-02-17 06:23:15 -0800932 last_fir->request_ms = now_ms;
933 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200934 }
danilchap7851bda2016-09-29 15:28:07 -0700935 // Received signal that we need to send a new key frame.
936 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000937 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000938}
939
Erik Språng6b8d3552015-09-24 15:06:57 +0200940void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700941 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700942 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700943 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
944 new rtcp::TransportFeedback());
945 if (!transport_feedback->Parse(rtcp_block)) {
946 ++num_skipped_packets_;
947 return;
948 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200949
danilchap92ea6012016-09-23 10:36:03 -0700950 packet_information->packet_type_flags |= kRtcpTransportFeedback;
951 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200952}
danilchap287e5482016-08-16 15:15:39 -0700953
danilchap9bf610e2017-02-20 06:03:01 -0800954void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700955 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700956 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700957 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000958
spranga790d832016-12-02 07:29:44 -0800959 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700960 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700961 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
962 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800963 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000964 }
danilchap853ecb22016-08-22 08:26:15 -0700965
danilchap9bf610e2017-02-20 06:03:01 -0800966 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800967 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000968}
969
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000970void RTCPReceiver::RegisterRtcpStatisticsCallback(
971 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800972 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000973 stats_callback_ = callback;
974}
975
976RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800977 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000978 return stats_callback_;
979}
980
Henrik Boströmf2047872019-05-16 13:32:20 +0200981void RTCPReceiver::SetReportBlockDataObserver(
982 ReportBlockDataObserver* observer) {
983 rtc::CritScope cs(&feedbacks_lock_);
984 report_block_data_observer_ = observer;
985}
986
danilchap8bab7962016-12-20 02:46:46 -0800987// Holding no Critical section.
988void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700989 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000990 // Process TMMBR and REMB first to avoid multiple callbacks
991 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700992 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000993 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800994 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000995 }
sprang7dc39f32015-10-13 09:17:48 -0700996 uint32_t local_ssrc;
997 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000998 {
999 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -08001000 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +00001001 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -07001002 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +00001003 }
danilchap92ea6012016-09-23 10:36:03 -07001004 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001005 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001006 }
danilchap92ea6012016-09-23 10:36:03 -07001007 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1008 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001009 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1010 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001011 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001012 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001013 }
spranga790d832016-12-02 07:29:44 -08001014
1015 // We need feedback that we have received a report block(s) so that we
1016 // can generate a new packet in a conference relay scenario, one received
1017 // report can generate several RTCP packets, based on number relayed/mixed
1018 // a send report block should go out to all receivers.
1019 if (rtcp_intra_frame_observer_) {
1020 RTC_DCHECK(!receiver_only_);
1021 if ((packet_information.packet_type_flags & kRtcpPli) ||
1022 (packet_information.packet_type_flags & kRtcpFir)) {
1023 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001024 RTC_LOG(LS_VERBOSE)
1025 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001026 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001027 RTC_LOG(LS_VERBOSE)
1028 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001029 }
spranga790d832016-12-02 07:29:44 -08001030 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001031 }
spranga790d832016-12-02 07:29:44 -08001032 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001033 if (rtcp_loss_notification_observer_ &&
1034 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1035 rtcp::LossNotification* loss_notification =
1036 packet_information.loss_notification.get();
1037 RTC_DCHECK(loss_notification);
1038 if (loss_notification->media_ssrc() == local_ssrc) {
1039 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1040 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1041 loss_notification->last_received(),
1042 loss_notification->decodability_flag());
1043 }
1044 }
spranga790d832016-12-02 07:29:44 -08001045 if (rtcp_bandwidth_observer_) {
1046 RTC_DCHECK(!receiver_only_);
1047 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001048 RTC_LOG(LS_VERBOSE)
1049 << "Incoming REMB: "
1050 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001051 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1052 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 }
danilchap92ea6012016-09-23 10:36:03 -07001054 if ((packet_information.packet_type_flags & kRtcpSr) ||
1055 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001056 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001057 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001058 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001059 }
spranga790d832016-12-02 07:29:44 -08001060 }
1061 if ((packet_information.packet_type_flags & kRtcpSr) ||
1062 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001063 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001064 }
isheriff6b4b5f32016-06-08 00:24:21 -07001065
spranga790d832016-12-02 07:29:44 -08001066 if (transport_feedback_observer_ &&
1067 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1068 uint32_t media_source_ssrc =
1069 packet_information.transport_feedback->media_ssrc();
1070 if (media_source_ssrc == local_ssrc ||
1071 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1072 transport_feedback_observer_->OnTransportFeedback(
1073 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001074 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001075 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001076
spranga790d832016-12-02 07:29:44 -08001077 if (bitrate_allocation_observer_ &&
1078 packet_information.target_bitrate_allocation) {
1079 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1080 *packet_information.target_bitrate_allocation);
1081 }
1082
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001083 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001084 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001085 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001086 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001087 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001088 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001089 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001090 report_block.extended_highest_sequence_number;
1091 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001092 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001093
srte3e69e5c2017-08-09 06:13:45 -07001094 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001095 }
1096 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001097 if (report_block_data_observer_) {
1098 for (const auto& report_block_data :
1099 packet_information.report_block_datas) {
1100 report_block_data_observer_->OnReportBlockDataUpdated(
1101 report_block_data);
1102 }
1103 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001104 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001105}
1106
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001107int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001108 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001109 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001110
danilchap8bab7962016-12-20 02:46:46 -08001111 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001112 auto received_cname_it = received_cnames_.find(remoteSSRC);
1113 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001114 return -1;
danilchap95321242016-09-27 07:05:32 -07001115
1116 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1117 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001118 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001119}
1120
danilchap7851bda2016-09-29 15:28:07 -07001121std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001122 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001123 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001124
danilchap8bab7962016-12-20 02:46:46 -08001125 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001126 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001127
danilchap9bf610e2017-02-20 06:03:01 -08001128 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001129 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001130 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001131 // Erase timeout entries.
1132 it = kv.second.tmmbr.erase(it);
1133 } else {
1134 candidates.push_back(it->second.tmmbr_item);
1135 ++it;
1136 }
1137 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001138 }
danilchap287e5482016-08-16 15:15:39 -07001139 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001140}
1141
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001142} // namespace webrtc