blob: c79143421a1373f8c9392a274a76d4912d2a2f53 [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
danilchap1b1863a2016-09-20 01:39:54 -070066} // namespace
67
danilchap92ea6012016-09-23 10:36:03 -070068struct RTCPReceiver::PacketInformation {
69 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
70
71 uint32_t remote_ssrc = 0;
72 std::vector<uint16_t> nack_sequence_numbers;
Henrik Boströmf2047872019-05-16 13:32:20 +020073 // TODO(hbos): Remove |report_blocks| in favor of |report_block_datas|.
danilchap92ea6012016-09-23 10:36:03 -070074 ReportBlockList report_blocks;
Henrik Boströmf2047872019-05-16 13:32:20 +020075 std::vector<ReportBlockData> report_block_datas;
danilchap92ea6012016-09-23 10:36:03 -070076 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070077 uint32_t receiver_estimated_max_bitrate_bps = 0;
78 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020079 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010080 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070081};
82
danilchap9bf610e2017-02-20 06:03:01 -080083// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
84struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070085 struct TimedTmmbrItem {
86 rtcp::TmmbItem tmmbr_item;
87 int64_t last_updated_ms;
88 };
89
90 int64_t last_time_received_ms = 0;
91
danilchap7851bda2016-09-29 15:28:07 -070092 bool ready_for_delete = false;
93
94 std::vector<rtcp::TmmbItem> tmmbn;
95 std::map<uint32_t, TimedTmmbrItem> tmmbr;
96};
97
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020098// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
99struct RTCPReceiver::RrtrInformation {
100 RrtrInformation(uint32_t ssrc,
101 uint32_t received_remote_mid_ntp_time,
102 uint32_t local_receive_mid_ntp_time)
103 : ssrc(ssrc),
104 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
105 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
106
107 uint32_t ssrc;
108 // Received NTP timestamp in compact representation.
109 uint32_t received_remote_mid_ntp_time;
110 // NTP time when the report was received in compact representation.
111 uint32_t local_receive_mid_ntp_time;
112};
113
danilchapefa966b2017-02-17 06:23:15 -0800114struct RTCPReceiver::LastFirStatus {
115 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
116 : request_ms(now_ms), sequence_number(sequence_number) {}
117 int64_t request_ms;
118 uint8_t sequence_number;
119};
120
Mirko Bonadei4d683142019-07-12 08:36:29 +0000121RTCPReceiver::RTCPReceiver(
122 Clock* clock,
123 bool receiver_only,
124 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
125 RtcpBandwidthObserver* rtcp_bandwidth_observer,
126 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
127 RtcpLossNotificationObserver* rtcp_loss_notification_observer,
128 TransportFeedbackObserver* transport_feedback_observer,
129 VideoBitrateAllocationObserver* bitrate_allocation_observer,
130 int report_interval_ms,
131 ModuleRtpRtcp* owner)
132 : clock_(clock),
133 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800134 rtp_rtcp_(owner),
Mirko Bonadei4d683142019-07-12 08:36:29 +0000135 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
136 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
137 rtcp_loss_notification_observer_(rtcp_loss_notification_observer),
138 transport_feedback_observer_(transport_feedback_observer),
139 bitrate_allocation_observer_(bitrate_allocation_observer),
140 report_interval_ms_(report_interval_ms),
141 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800142 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700143 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100144 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000145 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700146 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200147 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800148 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800149 stats_callback_(nullptr),
Henrik Boströmf2047872019-05-16 13:32:20 +0200150 report_block_data_observer_(nullptr),
Mirko Bonadei4d683142019-07-12 08:36:29 +0000151 packet_type_counter_observer_(packet_type_counter_observer),
Erik Språng6b8d3552015-09-24 15:06:57 +0200152 num_skipped_packets_(0),
Mirko Bonadei4d683142019-07-12 08:36:29 +0000153 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
danilchap8bab7962016-12-20 02:46:46 -0800154 RTC_DCHECK(owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
danilchap28b03eb2016-10-05 06:59:44 -0700157RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
nisse479d3d72017-09-13 07:53:37 -0700159void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700160 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700162 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700163 }
danilchap59cb2bd2016-08-29 11:08:47 -0700164
danilchap92ea6012016-09-23 10:36:03 -0700165 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700166 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700167 return;
danilchap8bab7962016-12-20 02:46:46 -0800168 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200171int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800172 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200173 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000174}
175
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000176void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800177 rtc::CritScope lock(&rtcp_receiver_lock_);
178 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700179 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800180 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800184 rtc::CritScope lock(&rtcp_receiver_lock_);
185 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000186}
187
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000188void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
189 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800190 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700191 main_ssrc_ = main_ssrc;
192 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
danilchap28b03eb2016-10-05 06:59:44 -0700195int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
196 int64_t* last_rtt_ms,
197 int64_t* avg_rtt_ms,
198 int64_t* min_rtt_ms,
199 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800200 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
danilchap28b03eb2016-10-05 06:59:44 -0700202 auto it = received_report_blocks_.find(main_ssrc_);
203 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000204 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700205
206 auto it_info = it->second.find(remote_ssrc);
207 if (it_info == it->second.end())
208 return -1;
209
Henrik Boströmf2047872019-05-16 13:32:20 +0200210 const ReportBlockData* report_block_data = &it_info->second;
danilchap28b03eb2016-10-05 06:59:44 -0700211
Henrik Boströmf2047872019-05-16 13:32:20 +0200212 if (report_block_data->num_rtts() == 0)
danilchap28b03eb2016-10-05 06:59:44 -0700213 return -1;
214
215 if (last_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200216 *last_rtt_ms = report_block_data->last_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700217
Henrik Boströmf2047872019-05-16 13:32:20 +0200218 if (avg_rtt_ms) {
219 *avg_rtt_ms =
220 report_block_data->sum_rtt_ms() / report_block_data->num_rtts();
221 }
danilchap28b03eb2016-10-05 06:59:44 -0700222
223 if (min_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200224 *min_rtt_ms = report_block_data->min_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700225
226 if (max_rtt_ms)
Henrik Boströmf2047872019-05-16 13:32:20 +0200227 *max_rtt_ms = report_block_data->max_rtt_ms();
danilchap28b03eb2016-10-05 06:59:44 -0700228
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000229 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000230}
231
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100232void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800233 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100234 xr_rrtr_status_ = enable;
235}
236
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000237bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800238 RTC_DCHECK(rtt_ms);
239 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000240 if (xr_rr_rtt_ms_ == 0) {
241 return false;
242 }
243 *rtt_ms = xr_rr_rtt_ms_;
244 xr_rr_rtt_ms_ = 0;
245 return true;
246}
247
danilchap8bab7962016-12-20 02:46:46 -0800248bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
249 uint32_t* received_ntp_frac,
250 uint32_t* rtcp_arrival_time_secs,
251 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700252 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800253 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700254 if (!last_received_sr_ntp_.Valid())
255 return false;
256
257 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800258 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700259 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800260 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700261 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700262
263 // Rtp time from incoming SenderReport.
264 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700265 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700266
267 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800268 if (rtcp_arrival_time_secs)
269 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
270 if (rtcp_arrival_time_frac)
271 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700272
danilchapda161d72016-08-19 07:29:46 -0700273 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274}
275
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200276std::vector<rtcp::ReceiveTimeInfo>
277RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800278 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000279
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200280 const size_t last_xr_rtis_size = std::min(
281 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
282 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
283 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000284
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200285 const uint32_t now_ntp =
286 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000287
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200288 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
289 RrtrInformation& rrtr = received_rrtrs_.front();
290 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
291 now_ntp - rrtr.local_receive_mid_ntp_time);
292 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
293 received_rrtrs_.pop_front();
294 }
295
296 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000297}
298
danilchap28b03eb2016-10-05 06:59:44 -0700299// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000300int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700301 std::vector<RTCPReportBlock>* receive_blocks) const {
302 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800303 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700304 for (const auto& reports_per_receiver : received_report_blocks_)
305 for (const auto& report : reports_per_receiver.second)
Henrik Boströmf2047872019-05-16 13:32:20 +0200306 receive_blocks->push_back(report.second.report_block());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000308}
309
Henrik Boströmf2047872019-05-16 13:32:20 +0200310std::vector<ReportBlockData> RTCPReceiver::GetLatestReportBlockData() const {
311 std::vector<ReportBlockData> result;
312 rtc::CritScope lock(&rtcp_receiver_lock_);
313 for (const auto& reports_per_receiver : received_report_blocks_)
314 for (const auto& report : reports_per_receiver.second)
315 result.push_back(report.second);
316 return result;
317}
318
danilchap92ea6012016-09-23 10:36:03 -0700319bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
320 const uint8_t* packet_end,
321 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800322 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000323
danilchap1b1863a2016-09-20 01:39:54 -0700324 CommonHeader rtcp_block;
325 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
326 next_block = rtcp_block.NextPacket()) {
327 ptrdiff_t remaining_blocks_size = packet_end - next_block;
328 RTC_DCHECK_GT(remaining_blocks_size, 0);
329 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
330 if (next_block == packet_begin) {
331 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700333 return false;
334 }
335 ++num_skipped_packets_;
336 break;
337 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000338
danilchap1b1863a2016-09-20 01:39:54 -0700339 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800340 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700341
danilchap1b1863a2016-09-20 01:39:54 -0700342 switch (rtcp_block.type()) {
343 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700344 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200345 break;
danilchap1b1863a2016-09-20 01:39:54 -0700346 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700347 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700348 break;
danilchap1b1863a2016-09-20 01:39:54 -0700349 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800350 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700351 break;
danilchap1b1863a2016-09-20 01:39:54 -0700352 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700353 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700354 break;
danilchap1b1863a2016-09-20 01:39:54 -0700355 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800356 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700357 break;
danilchap1b1863a2016-09-20 01:39:54 -0700358 case rtcp::Rtpfb::kPacketType:
359 switch (rtcp_block.fmt()) {
360 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800361 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700362 break;
363 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800364 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700365 break;
366 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800367 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700368 break;
369 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800370 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700371 break;
372 case rtcp::TransportFeedback::kFeedbackMessageType:
373 HandleTransportFeedback(rtcp_block, packet_information);
374 break;
375 default:
376 ++num_skipped_packets_;
377 break;
378 }
danilchapda161d72016-08-19 07:29:46 -0700379 break;
danilchap1b1863a2016-09-20 01:39:54 -0700380 case rtcp::Psfb::kPacketType:
381 switch (rtcp_block.fmt()) {
382 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800383 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700384 break;
danilchap1b1863a2016-09-20 01:39:54 -0700385 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800386 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700387 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100388 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700389 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700390 break;
391 default:
392 ++num_skipped_packets_;
393 break;
394 }
danilchapda161d72016-08-19 07:29:46 -0700395 break;
396 default:
danilchap1b1863a2016-09-20 01:39:54 -0700397 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700398 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000399 }
danilchapda161d72016-08-19 07:29:46 -0700400 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000401
danilchap8bab7962016-12-20 02:46:46 -0800402 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700403 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
404 main_ssrc_, packet_type_counter_);
405 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000406
danilchap8bab7962016-12-20 02:46:46 -0800407 int64_t now_ms = clock_->TimeInMilliseconds();
408 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700409 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800410 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100411 RTC_LOG(LS_WARNING)
412 << num_skipped_packets_
413 << " RTCP blocks were skipped due to being malformed or of "
414 "unrecognized/unsupported type, during the past "
415 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700416 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200417
danilchap1b1863a2016-09-20 01:39:54 -0700418 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419}
420
danilchap92ea6012016-09-23 10:36:03 -0700421void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
422 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700423 rtcp::SenderReport sender_report;
424 if (!sender_report.Parse(rtcp_block)) {
425 ++num_skipped_packets_;
426 return;
427 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
danilchap8bab7962016-12-20 02:46:46 -0800429 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
danilchap8bab7962016-12-20 02:46:46 -0800431 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
danilchapec067e92017-02-21 05:38:19 -0800433 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700434
Danil Chapovalov91511f12016-09-15 16:24:02 +0200435 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800436 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200437 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700438 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700439
danilchapa04d9c32017-07-25 04:03:39 -0700440 remote_sender_ntp_time_ = sender_report.ntp();
441 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200442 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700443 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200444 // We will only store the send report from one source, but
445 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700446 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700447 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000448
Mirko Bonadei739baf02019-01-27 17:29:42 +0100449 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800450 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
danilchap92ea6012016-09-23 10:36:03 -0700453void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
454 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700455 rtcp::ReceiverReport receiver_report;
456 if (!receiver_report.Parse(rtcp_block)) {
457 ++num_skipped_packets_;
458 return;
459 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200460
danilchap8bab7962016-12-20 02:46:46 -0800461 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200462
danilchap8bab7962016-12-20 02:46:46 -0800463 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200464
danilchapec067e92017-02-21 05:38:19 -0800465 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200466
danilchap92ea6012016-09-23 10:36:03 -0700467 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200468
danilchap1b1863a2016-09-20 01:39:54 -0700469 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800470 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200471}
472
danilchap92ea6012016-09-23 10:36:03 -0700473void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
474 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700475 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000476 // This will be called once per report block in the RTCP packet.
477 // We filter out all report blocks that are not for us.
478 // Each packet has max 31 RR blocks.
479 //
480 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
danilchap28b03eb2016-10-05 06:59:44 -0700482 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000483 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000484
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000485 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700486 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000487 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000488
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200489 last_received_rb_ms_ = clock_->TimeInMilliseconds();
490
Henrik Boströmf2047872019-05-16 13:32:20 +0200491 ReportBlockData* report_block_data =
danilchap28b03eb2016-10-05 06:59:44 -0700492 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
Henrik Boströmf2047872019-05-16 13:32:20 +0200493 RTCPReportBlock rtcp_report_block;
494 rtcp_report_block.sender_ssrc = remote_ssrc;
495 rtcp_report_block.source_ssrc = report_block.source_ssrc();
496 rtcp_report_block.fraction_lost = report_block.fraction_lost();
497 rtcp_report_block.packets_lost = report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700498 if (report_block.extended_high_seq_num() >
Henrik Boströmf2047872019-05-16 13:32:20 +0200499 report_block_data->report_block().extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000500 // We have successfully delivered new RTP packets to the remote side after
501 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800502 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000503 }
Henrik Boströmf2047872019-05-16 13:32:20 +0200504 rtcp_report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700505 report_block.extended_high_seq_num();
Henrik Boströmf2047872019-05-16 13:32:20 +0200506 rtcp_report_block.jitter = report_block.jitter();
507 rtcp_report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700508 report_block.delay_since_last_sr();
Henrik Boströmf2047872019-05-16 13:32:20 +0200509 rtcp_report_block.last_sender_report_timestamp = report_block.last_sr();
510 report_block_data->SetReportBlock(rtcp_report_block, rtc::TimeUTCMicros());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000511
danilchap28b03eb2016-10-05 06:59:44 -0700512 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800513 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100514 // RFC3550, section 6.4.1, LSR field discription states:
515 // If no SR has been received yet, the field is set to zero.
516 // Receiver rtp_rtcp module is not expected to calculate rtt using
517 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100518
519 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
520 // is false. However, that currently breaks the tests of the
521 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
522 // delete all dependencies on RTT measurements for audio receive streams, or
523 // ensure that audio receive streams that need RTT and stats that depend on it
524 // are configured with an associated audio send stream.
525 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800526 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100527 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200528 uint32_t receive_time_ntp =
529 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000530
Danil Chapovalova094fd12016-02-22 18:59:36 +0100531 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800532 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100533 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700534 rtt_ms = CompactNtpRttToMs(rtt_ntp);
Henrik Boströmf2047872019-05-16 13:32:20 +0200535 report_block_data->AddRoundTripTimeSample(rtt_ms);
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100536
537 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000538 }
539
Henrik Boströmf2047872019-05-16 13:32:20 +0200540 packet_information->report_blocks.push_back(
541 report_block_data->report_block());
542 packet_information->report_block_datas.push_back(*report_block_data);
niklase@google.com470e71d2011-07-07 08:21:25 +0000543}
544
danilchapec067e92017-02-21 05:38:19 -0800545RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
546 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700547 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800548 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700549 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800550 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800551 return tmmbr_info;
552}
553
554void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
555 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
556 if (tmmbr_it != tmmbr_infos_.end())
557 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000558}
559
danilchap9bf610e2017-02-20 06:03:01 -0800560RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700561 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800562 auto it = tmmbr_infos_.find(remote_ssrc);
563 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700564 return nullptr;
565 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000566}
567
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800568bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800569 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200570 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000571 return false;
572
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800573 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200574 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000575 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200576 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000577 return true;
578 }
579 return false;
580}
581
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800582bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800583 rtc::CritScope lock(&rtcp_receiver_lock_);
584 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000585 return false;
586
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800587 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800588 if (clock_->TimeInMilliseconds() >
589 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000590 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800591 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000592 return true;
593 }
594 return false;
595}
596
danilchap9bf610e2017-02-20 06:03:01 -0800597bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800598 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000599
danilchap8bab7962016-12-20 02:46:46 -0800600 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800601 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000602
danilchap9bf610e2017-02-20 06:03:01 -0800603 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800604 return false;
605
606 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800607 oldest_tmmbr_info_ms_ = -1;
608 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
609 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
610 if (tmmbr_info->last_time_received_ms > 0) {
611 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700612 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800613 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700614 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800615 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700616 // Send new TMMBN to all channels using the default codec.
617 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800618 } else if (oldest_tmmbr_info_ms_ == -1 ||
619 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
620 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000621 }
danilchap9bf610e2017-02-20 06:03:01 -0800622 ++tmmbr_it;
623 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700624 // When we dont have a last_time_received_ms and the object is marked
625 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800626 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000627 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800628 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000629 }
630 }
danilchap7851bda2016-09-29 15:28:07 -0700631 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000632}
633
danilchap2b616392016-08-18 06:17:42 -0700634std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800635 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800636 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
637 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700638 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700639
danilchap9bf610e2017-02-20 06:03:01 -0800640 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
641 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000642}
643
danilchap8bab7962016-12-20 02:46:46 -0800644void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700645 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700646 rtcp::Sdes sdes;
647 if (!sdes.Parse(rtcp_block)) {
648 ++num_skipped_packets_;
649 return;
650 }
651
652 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700653 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200654 {
spranga790d832016-12-02 07:29:44 -0800655 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700656 if (stats_callback_)
657 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200658 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000659 }
danilchap92ea6012016-09-23 10:36:03 -0700660 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000661}
662
danilchap8bab7962016-12-20 02:46:46 -0800663void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700664 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700665 rtcp::Nack nack;
666 if (!nack.Parse(rtcp_block)) {
667 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000668 return;
669 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000670
danilchap1b1863a2016-09-20 01:39:54 -0700671 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
672 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200673
danilchap142f0192016-10-20 08:22:42 -0700674 packet_information->nack_sequence_numbers.insert(
675 packet_information->nack_sequence_numbers.end(),
676 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700677 for (uint16_t packet_id : nack.packet_ids())
678 nack_stats_.ReportRequest(packet_id);
679
680 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700681 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000682 ++packet_type_counter_.nack_packets;
683 packet_type_counter_.nack_requests = nack_stats_.requests();
684 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
685 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000686}
687
danilchap8bab7962016-12-20 02:46:46 -0800688void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700689 rtcp::Bye bye;
690 if (!bye.Parse(rtcp_block)) {
691 ++num_skipped_packets_;
692 return;
693 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000694
danilchap8bab7962016-12-20 02:46:46 -0800695 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700696 for (auto& reports_per_receiver : received_report_blocks_)
697 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000698
danilchap9bf610e2017-02-20 06:03:01 -0800699 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
700 if (tmmbr_info)
701 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000702
danilchapefa966b2017-02-17 06:23:15 -0800703 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700704 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200705 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
706 if (it != received_rrtrs_ssrc_it_.end()) {
707 received_rrtrs_.erase(it->second);
708 received_rrtrs_ssrc_it_.erase(it);
709 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000710 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000711}
712
danilchap1b1863a2016-09-20 01:39:54 -0700713void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700714 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700715 rtcp::ExtendedReports xr;
716 if (!xr.Parse(rtcp_block)) {
717 ++num_skipped_packets_;
718 return;
719 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000720
danilchap80ac24d2016-10-31 08:40:47 -0700721 if (xr.rrtr())
722 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000723
danilchap80ac24d2016-10-31 08:40:47 -0700724 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
725 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800726
sprangb32aaf92017-08-28 05:49:12 -0700727 if (xr.target_bitrate()) {
728 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
729 packet_information);
730 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000731}
732
danilchap8bab7962016-12-20 02:46:46 -0800733void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
734 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200735 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200736 uint32_t local_receive_mid_ntp_time =
737 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200738
739 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
740 if (it != received_rrtrs_ssrc_it_.end()) {
741 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
742 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
743 } else {
744 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
745 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
746 local_receive_mid_ntp_time);
747 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
748 } else {
749 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
750 << ", reached maximum number of stored RRTRs.";
751 }
752 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000753}
754
danilchap92ea6012016-09-23 10:36:03 -0700755void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700756 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
757 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000758
danilchap1b1863a2016-09-20 01:39:54 -0700759 // Caller should explicitly enable rtt calculation using extended reports.
760 if (!xr_rrtr_status_)
761 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200762
danilchap1b1863a2016-09-20 01:39:54 -0700763 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800764 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700765 // RFC3611, section 4.5, LRR field discription states:
766 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800767 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700768 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200769
danilchap8bab7962016-12-20 02:46:46 -0800770 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200771 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200772
danilchap8bab7962016-12-20 02:46:46 -0800773 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700774 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000775}
776
spranga790d832016-12-02 07:29:44 -0800777void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700778 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800779 const rtcp::TargetBitrate& target_bitrate,
780 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700781 if (ssrc != remote_ssrc_) {
782 return; // Not for us.
783 }
784
Erik Språng566124a2018-04-23 12:32:22 +0200785 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800786 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800787 if (item.spatial_layer >= kMaxSpatialLayers ||
788 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100789 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800790 << "Invalid layer in XR target bitrate pack: spatial index "
791 << item.spatial_layer << ", temporal index " << item.temporal_layer
792 << ", dropping.";
793 } else {
794 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
795 item.target_bitrate_kbps * 1000);
796 }
spranga790d832016-12-02 07:29:44 -0800797 }
798 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
799}
800
danilchap8bab7962016-12-20 02:46:46 -0800801void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700802 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700803 rtcp::Pli pli;
804 if (!pli.Parse(rtcp_block)) {
805 ++num_skipped_packets_;
806 return;
807 }
808
809 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000810 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000811 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700812 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000813 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000814}
815
danilchap8bab7962016-12-20 02:46:46 -0800816void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700817 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700818 rtcp::Tmmbr tmmbr;
819 if (!tmmbr.Parse(rtcp_block)) {
820 ++num_skipped_packets_;
821 return;
822 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000823
danilchap7851bda2016-09-29 15:28:07 -0700824 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700825 if (tmmbr.media_ssrc()) {
826 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
827 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700828 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000829 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000830
danilchap1b1863a2016-09-20 01:39:54 -0700831 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800832 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
833 continue;
danilchap7851bda2016-09-29 15:28:07 -0700834
danilchapec067e92017-02-21 05:38:19 -0800835 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
836 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200837 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800838 request.packet_overhead());
839 entry->last_updated_ms = clock_->TimeInMilliseconds();
840
841 packet_information->packet_type_flags |= kRtcpTmmbr;
842 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000843 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000844}
845
danilchap8bab7962016-12-20 02:46:46 -0800846void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700847 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700848 rtcp::Tmmbn tmmbn;
849 if (!tmmbn.Parse(rtcp_block)) {
850 ++num_skipped_packets_;
851 return;
852 }
853
danilchapec067e92017-02-21 05:38:19 -0800854 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700855
danilchap92ea6012016-09-23 10:36:03 -0700856 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000857
danilchapec067e92017-02-21 05:38:19 -0800858 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700859}
860
danilchap8bab7962016-12-20 02:46:46 -0800861void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
862 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700863 rtcp::RapidResyncRequest sr_req;
864 if (!sr_req.Parse(rtcp_block)) {
865 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000866 return;
867 }
868
danilchap92ea6012016-09-23 10:36:03 -0700869 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000870}
871
danilchap1b1863a2016-09-20 01:39:54 -0700872void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700873 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100874 {
875 rtcp::Remb remb;
876 if (remb.Parse(rtcp_block)) {
877 packet_information->packet_type_flags |= kRtcpRemb;
878 packet_information->receiver_estimated_max_bitrate_bps =
879 remb.bitrate_bps();
880 return;
881 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000882 }
danilchap1b1863a2016-09-20 01:39:54 -0700883
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100884 {
885 auto loss_notification = absl::make_unique<rtcp::LossNotification>();
886 if (loss_notification->Parse(rtcp_block)) {
887 packet_information->packet_type_flags |= kRtcpLossNotification;
888 packet_information->loss_notification = std::move(loss_notification);
889 return;
890 }
891 }
892
893 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
894
danilchap1b1863a2016-09-20 01:39:54 -0700895 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000896}
897
danilchap8bab7962016-12-20 02:46:46 -0800898void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700899 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700900 rtcp::Fir fir;
901 if (!fir.Parse(rtcp_block)) {
902 ++num_skipped_packets_;
903 return;
904 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000905
danilchap1b1863a2016-09-20 01:39:54 -0700906 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800907 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700908 if (main_ssrc_ != fir_request.ssrc)
909 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200910
911 ++packet_type_counter_.fir_packets;
912
danilchapefa966b2017-02-17 06:23:15 -0800913 int64_t now_ms = clock_->TimeInMilliseconds();
914 auto inserted = last_fir_.insert(std::make_pair(
915 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
916 if (!inserted.second) { // There was already an entry.
917 LastFirStatus* last_fir = &inserted.first->second;
918
danilchap7851bda2016-09-29 15:28:07 -0700919 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800920 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700921 continue;
922
danilchap7851bda2016-09-29 15:28:07 -0700923 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800924 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700925 continue;
926
danilchapefa966b2017-02-17 06:23:15 -0800927 last_fir->request_ms = now_ms;
928 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200929 }
danilchap7851bda2016-09-29 15:28:07 -0700930 // Received signal that we need to send a new key frame.
931 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000932 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000933}
934
Erik Språng6b8d3552015-09-24 15:06:57 +0200935void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700936 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700937 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700938 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
939 new rtcp::TransportFeedback());
940 if (!transport_feedback->Parse(rtcp_block)) {
941 ++num_skipped_packets_;
942 return;
943 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200944
danilchap92ea6012016-09-23 10:36:03 -0700945 packet_information->packet_type_flags |= kRtcpTransportFeedback;
946 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200947}
danilchap287e5482016-08-16 15:15:39 -0700948
danilchap9bf610e2017-02-20 06:03:01 -0800949void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700950 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700951 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700952 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000953
spranga790d832016-12-02 07:29:44 -0800954 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700955 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700956 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
957 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800958 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000959 }
danilchap853ecb22016-08-22 08:26:15 -0700960
danilchap9bf610e2017-02-20 06:03:01 -0800961 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800962 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000963}
964
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000965void RTCPReceiver::RegisterRtcpStatisticsCallback(
966 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800967 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000968 stats_callback_ = callback;
969}
970
971RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800972 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000973 return stats_callback_;
974}
975
Henrik Boströmf2047872019-05-16 13:32:20 +0200976void RTCPReceiver::SetReportBlockDataObserver(
977 ReportBlockDataObserver* observer) {
978 rtc::CritScope cs(&feedbacks_lock_);
979 report_block_data_observer_ = observer;
980}
981
danilchap8bab7962016-12-20 02:46:46 -0800982// Holding no Critical section.
983void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700984 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000985 // Process TMMBR and REMB first to avoid multiple callbacks
986 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700987 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000988 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800989 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000990 }
sprang7dc39f32015-10-13 09:17:48 -0700991 uint32_t local_ssrc;
992 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000993 {
994 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800995 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000996 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700997 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000998 }
danilchap92ea6012016-09-23 10:36:03 -0700999 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -08001000 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001001 }
danilchap92ea6012016-09-23 10:36:03 -07001002 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
1003 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001004 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1005 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001006 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001007 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001008 }
spranga790d832016-12-02 07:29:44 -08001009
1010 // We need feedback that we have received a report block(s) so that we
1011 // can generate a new packet in a conference relay scenario, one received
1012 // report can generate several RTCP packets, based on number relayed/mixed
1013 // a send report block should go out to all receivers.
1014 if (rtcp_intra_frame_observer_) {
1015 RTC_DCHECK(!receiver_only_);
1016 if ((packet_information.packet_type_flags & kRtcpPli) ||
1017 (packet_information.packet_type_flags & kRtcpFir)) {
1018 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001019 RTC_LOG(LS_VERBOSE)
1020 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001021 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001022 RTC_LOG(LS_VERBOSE)
1023 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001024 }
spranga790d832016-12-02 07:29:44 -08001025 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001026 }
spranga790d832016-12-02 07:29:44 -08001027 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001028 if (rtcp_loss_notification_observer_ &&
1029 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1030 rtcp::LossNotification* loss_notification =
1031 packet_information.loss_notification.get();
1032 RTC_DCHECK(loss_notification);
1033 if (loss_notification->media_ssrc() == local_ssrc) {
1034 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1035 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1036 loss_notification->last_received(),
1037 loss_notification->decodability_flag());
1038 }
1039 }
spranga790d832016-12-02 07:29:44 -08001040 if (rtcp_bandwidth_observer_) {
1041 RTC_DCHECK(!receiver_only_);
1042 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001043 RTC_LOG(LS_VERBOSE)
1044 << "Incoming REMB: "
1045 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001046 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1047 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 }
danilchap92ea6012016-09-23 10:36:03 -07001049 if ((packet_information.packet_type_flags & kRtcpSr) ||
1050 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001051 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001052 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001053 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001054 }
spranga790d832016-12-02 07:29:44 -08001055 }
1056 if ((packet_information.packet_type_flags & kRtcpSr) ||
1057 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001058 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001059 }
isheriff6b4b5f32016-06-08 00:24:21 -07001060
spranga790d832016-12-02 07:29:44 -08001061 if (transport_feedback_observer_ &&
1062 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1063 uint32_t media_source_ssrc =
1064 packet_information.transport_feedback->media_ssrc();
1065 if (media_source_ssrc == local_ssrc ||
1066 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1067 transport_feedback_observer_->OnTransportFeedback(
1068 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001069 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001070 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001071
spranga790d832016-12-02 07:29:44 -08001072 if (bitrate_allocation_observer_ &&
1073 packet_information.target_bitrate_allocation) {
1074 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1075 *packet_information.target_bitrate_allocation);
1076 }
1077
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001078 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001079 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001080 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001081 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001082 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001083 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001084 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001085 report_block.extended_highest_sequence_number;
1086 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001087 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001088
srte3e69e5c2017-08-09 06:13:45 -07001089 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001090 }
1091 }
Henrik Boströmf2047872019-05-16 13:32:20 +02001092 if (report_block_data_observer_) {
1093 for (const auto& report_block_data :
1094 packet_information.report_block_datas) {
1095 report_block_data_observer_->OnReportBlockDataUpdated(
1096 report_block_data);
1097 }
1098 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001099 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001100}
1101
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001102int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001103 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001104 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001105
danilchap8bab7962016-12-20 02:46:46 -08001106 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001107 auto received_cname_it = received_cnames_.find(remoteSSRC);
1108 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001109 return -1;
danilchap95321242016-09-27 07:05:32 -07001110
1111 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1112 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001113 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114}
1115
danilchap7851bda2016-09-29 15:28:07 -07001116std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001117 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001118 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001119
danilchap8bab7962016-12-20 02:46:46 -08001120 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001121 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001122
danilchap9bf610e2017-02-20 06:03:01 -08001123 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001124 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001125 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001126 // Erase timeout entries.
1127 it = kv.second.tmmbr.erase(it);
1128 } else {
1129 candidates.push_back(it->second.tmmbr_item);
1130 ++it;
1131 }
1132 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001133 }
danilchap287e5482016-08-16 15:15:39 -07001134 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001135}
1136
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001137} // namespace webrtc