blob: abe135e2ed5f3e582a70765d48dd807a946019c9 [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
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_video/include/video_bitrate_allocator.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
35#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
38#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
39#include "modules/rtp_rtcp/source/time_util.h"
40#include "modules/rtp_rtcp/source/tmmbr_help.h"
41#include "rtc_base/checks.h"
42#include "rtc_base/logging.h"
43#include "rtc_base/trace_event.h"
44#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000045
niklase@google.com470e71d2011-07-07 08:21:25 +000046namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070047namespace {
48
49using rtcp::CommonHeader;
50using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000052// The number of RTCP time intervals needed to trigger a timeout.
53const int kRrTimeoutIntervals = 3;
54
Jiawei Ou3587b832018-01-31 22:08:26 -080055const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
56
Erik Språng6b8d3552015-09-24 15:06:57 +020057const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080058const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020059
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020060// Maximum number of received RRTRs that will be stored.
61const size_t kMaxNumberOfStoredRrtrs = 200;
62
danilchap1b1863a2016-09-20 01:39:54 -070063} // namespace
64
danilchap92ea6012016-09-23 10:36:03 -070065struct RTCPReceiver::PacketInformation {
66 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
67
68 uint32_t remote_ssrc = 0;
69 std::vector<uint16_t> nack_sequence_numbers;
70 ReportBlockList report_blocks;
71 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070072 uint32_t receiver_estimated_max_bitrate_bps = 0;
73 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
spranga790d832016-12-02 07:29:44 -080074 rtc::Optional<BitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070075};
76
danilchap9bf610e2017-02-20 06:03:01 -080077// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
78struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070079 struct TimedTmmbrItem {
80 rtcp::TmmbItem tmmbr_item;
81 int64_t last_updated_ms;
82 };
83
84 int64_t last_time_received_ms = 0;
85
danilchap7851bda2016-09-29 15:28:07 -070086 bool ready_for_delete = false;
87
88 std::vector<rtcp::TmmbItem> tmmbn;
89 std::map<uint32_t, TimedTmmbrItem> tmmbr;
90};
91
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020092// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
93struct RTCPReceiver::RrtrInformation {
94 RrtrInformation(uint32_t ssrc,
95 uint32_t received_remote_mid_ntp_time,
96 uint32_t local_receive_mid_ntp_time)
97 : ssrc(ssrc),
98 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
99 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
100
101 uint32_t ssrc;
102 // Received NTP timestamp in compact representation.
103 uint32_t received_remote_mid_ntp_time;
104 // NTP time when the report was received in compact representation.
105 uint32_t local_receive_mid_ntp_time;
106};
107
danilchap28b03eb2016-10-05 06:59:44 -0700108struct RTCPReceiver::ReportBlockWithRtt {
109 RTCPReportBlock report_block;
110
111 int64_t last_rtt_ms = 0;
112 int64_t min_rtt_ms = 0;
113 int64_t max_rtt_ms = 0;
114 int64_t sum_rtt_ms = 0;
115 size_t num_rtts = 0;
116};
117
danilchapefa966b2017-02-17 06:23:15 -0800118struct RTCPReceiver::LastFirStatus {
119 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
120 : request_ms(now_ms), sequence_number(sequence_number) {}
121 int64_t request_ms;
122 uint8_t sequence_number;
123};
124
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000125RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000126 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200127 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000128 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000129 RtcpBandwidthObserver* rtcp_bandwidth_observer,
130 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200131 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800132 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700133 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800134 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200135 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800136 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800137 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
138 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
139 transport_feedback_observer_(transport_feedback_observer),
140 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000141 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),
Erik Språng6b8d3552015-09-24 15:06:57 +0200150 packet_type_counter_observer_(packet_type_counter_observer),
151 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800152 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
153 RTC_DCHECK(owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154}
155
danilchap28b03eb2016-10-05 06:59:44 -0700156RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
nisse479d3d72017-09-13 07:53:37 -0700158void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700159 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100160 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700161 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700162 }
danilchap59cb2bd2016-08-29 11:08:47 -0700163
danilchap92ea6012016-09-23 10:36:03 -0700164 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700165 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700166 return;
danilchap8bab7962016-12-20 02:46:46 -0800167 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200170int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800171 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200172 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000173}
174
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000175void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800176 rtc::CritScope lock(&rtcp_receiver_lock_);
177 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700178 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800179 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180}
181
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000182uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800183 rtc::CritScope lock(&rtcp_receiver_lock_);
184 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000185}
186
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000187void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
188 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800189 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700190 main_ssrc_ = main_ssrc;
191 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
danilchap28b03eb2016-10-05 06:59:44 -0700194int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
195 int64_t* last_rtt_ms,
196 int64_t* avg_rtt_ms,
197 int64_t* min_rtt_ms,
198 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800199 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
danilchap28b03eb2016-10-05 06:59:44 -0700201 auto it = received_report_blocks_.find(main_ssrc_);
202 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000203 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700204
205 auto it_info = it->second.find(remote_ssrc);
206 if (it_info == it->second.end())
207 return -1;
208
209 const ReportBlockWithRtt* report_block = &it_info->second;
210
211 if (report_block->num_rtts == 0)
212 return -1;
213
214 if (last_rtt_ms)
215 *last_rtt_ms = report_block->last_rtt_ms;
216
217 if (avg_rtt_ms)
218 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
219
220 if (min_rtt_ms)
221 *min_rtt_ms = report_block->min_rtt_ms;
222
223 if (max_rtt_ms)
224 *max_rtt_ms = report_block->max_rtt_ms;
225
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000226 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000227}
228
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100229void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800230 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100231 xr_rrtr_status_ = enable;
232}
233
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000234bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800235 RTC_DCHECK(rtt_ms);
236 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000237 if (xr_rr_rtt_ms_ == 0) {
238 return false;
239 }
240 *rtt_ms = xr_rr_rtt_ms_;
241 xr_rr_rtt_ms_ = 0;
242 return true;
243}
244
danilchap8bab7962016-12-20 02:46:46 -0800245bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
246 uint32_t* received_ntp_frac,
247 uint32_t* rtcp_arrival_time_secs,
248 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700249 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800250 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700251 if (!last_received_sr_ntp_.Valid())
252 return false;
253
254 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800255 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700256 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800257 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700258 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700259
260 // Rtp time from incoming SenderReport.
261 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700262 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700263
264 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800265 if (rtcp_arrival_time_secs)
266 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
267 if (rtcp_arrival_time_frac)
268 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700269
danilchapda161d72016-08-19 07:29:46 -0700270 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200273std::vector<rtcp::ReceiveTimeInfo>
274RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800275 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000276
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200277 const size_t last_xr_rtis_size = std::min(
278 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
279 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
280 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000281
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200282 const uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000283
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200284 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
285 RrtrInformation& rrtr = received_rrtrs_.front();
286 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
287 now_ntp - rrtr.local_receive_mid_ntp_time);
288 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
289 received_rrtrs_.pop_front();
290 }
291
292 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000293}
294
danilchap28b03eb2016-10-05 06:59:44 -0700295// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000296int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700297 std::vector<RTCPReportBlock>* receive_blocks) const {
298 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800299 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700300 for (const auto& reports_per_receiver : received_report_blocks_)
301 for (const auto& report : reports_per_receiver.second)
302 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000303 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000304}
305
danilchap92ea6012016-09-23 10:36:03 -0700306bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
307 const uint8_t* packet_end,
308 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800309 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000310
danilchap1b1863a2016-09-20 01:39:54 -0700311 CommonHeader rtcp_block;
312 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
313 next_block = rtcp_block.NextPacket()) {
314 ptrdiff_t remaining_blocks_size = packet_end - next_block;
315 RTC_DCHECK_GT(remaining_blocks_size, 0);
316 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
317 if (next_block == packet_begin) {
318 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100319 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700320 return false;
321 }
322 ++num_skipped_packets_;
323 break;
324 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000325
danilchap1b1863a2016-09-20 01:39:54 -0700326 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800327 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700328
danilchap1b1863a2016-09-20 01:39:54 -0700329 switch (rtcp_block.type()) {
330 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700331 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200332 break;
danilchap1b1863a2016-09-20 01:39:54 -0700333 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700334 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700335 break;
danilchap1b1863a2016-09-20 01:39:54 -0700336 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800337 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700338 break;
danilchap1b1863a2016-09-20 01:39:54 -0700339 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700340 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700341 break;
danilchap1b1863a2016-09-20 01:39:54 -0700342 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800343 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700344 break;
danilchap1b1863a2016-09-20 01:39:54 -0700345 case rtcp::Rtpfb::kPacketType:
346 switch (rtcp_block.fmt()) {
347 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800348 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700349 break;
350 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800351 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700352 break;
353 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800354 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700355 break;
356 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800357 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700358 break;
359 case rtcp::TransportFeedback::kFeedbackMessageType:
360 HandleTransportFeedback(rtcp_block, packet_information);
361 break;
362 default:
363 ++num_skipped_packets_;
364 break;
365 }
danilchapda161d72016-08-19 07:29:46 -0700366 break;
danilchap1b1863a2016-09-20 01:39:54 -0700367 case rtcp::Psfb::kPacketType:
368 switch (rtcp_block.fmt()) {
369 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800370 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700371 break;
danilchap1b1863a2016-09-20 01:39:54 -0700372 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800373 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700374 break;
375 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700376 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700377 break;
378 default:
379 ++num_skipped_packets_;
380 break;
381 }
danilchapda161d72016-08-19 07:29:46 -0700382 break;
383 default:
danilchap1b1863a2016-09-20 01:39:54 -0700384 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700385 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000386 }
danilchapda161d72016-08-19 07:29:46 -0700387 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000388
danilchap8bab7962016-12-20 02:46:46 -0800389 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700390 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
391 main_ssrc_, packet_type_counter_);
392 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000393
danilchap8bab7962016-12-20 02:46:46 -0800394 int64_t now_ms = clock_->TimeInMilliseconds();
395 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700396 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800397 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100398 RTC_LOG(LS_WARNING)
399 << num_skipped_packets_
400 << " RTCP blocks were skipped due to being malformed or of "
401 "unrecognized/unsupported type, during the past "
402 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700403 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200404
danilchap1b1863a2016-09-20 01:39:54 -0700405 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406}
407
danilchap92ea6012016-09-23 10:36:03 -0700408void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
409 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700410 rtcp::SenderReport sender_report;
411 if (!sender_report.Parse(rtcp_block)) {
412 ++num_skipped_packets_;
413 return;
414 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
danilchap8bab7962016-12-20 02:46:46 -0800416 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
danilchap8bab7962016-12-20 02:46:46 -0800418 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
danilchapec067e92017-02-21 05:38:19 -0800420 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700421
Danil Chapovalov91511f12016-09-15 16:24:02 +0200422 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800423 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700424
Danil Chapovalov91511f12016-09-15 16:24:02 +0200425 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800426 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200427 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700428 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700429
danilchapa04d9c32017-07-25 04:03:39 -0700430 remote_sender_ntp_time_ = sender_report.ntp();
431 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
danilchap37953762017-02-09 11:15:25 -0800432 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700433 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200434 // We will only store the send report from one source, but
435 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700436 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700437 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000438
danilchap1b1863a2016-09-20 01:39:54 -0700439 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800440 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
danilchap92ea6012016-09-23 10:36:03 -0700443void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
444 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700445 rtcp::ReceiverReport receiver_report;
446 if (!receiver_report.Parse(rtcp_block)) {
447 ++num_skipped_packets_;
448 return;
449 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200450
danilchap8bab7962016-12-20 02:46:46 -0800451 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200452
danilchap8bab7962016-12-20 02:46:46 -0800453 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200454
danilchapec067e92017-02-21 05:38:19 -0800455 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200456
457 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800458 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200459
danilchap92ea6012016-09-23 10:36:03 -0700460 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200461
danilchap1b1863a2016-09-20 01:39:54 -0700462 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800463 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200464}
465
danilchap92ea6012016-09-23 10:36:03 -0700466void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
467 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700468 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000469 // This will be called once per report block in the RTCP packet.
470 // We filter out all report blocks that are not for us.
471 // Each packet has max 31 RR blocks.
472 //
473 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
danilchap28b03eb2016-10-05 06:59:44 -0700475 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000476 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000477
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000478 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700479 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000480 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000481
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200482 last_received_rb_ms_ = clock_->TimeInMilliseconds();
483
danilchap28b03eb2016-10-05 06:59:44 -0700484 ReportBlockWithRtt* report_block_info =
485 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700486 report_block_info->report_block.sender_ssrc = remote_ssrc;
487 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
488 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
Harald Alvestrand70206d62017-12-08 08:59:07 +0100489 report_block_info->report_block.packets_lost =
490 report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700491 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700492 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000493 // We have successfully delivered new RTP packets to the remote side after
494 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800495 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000496 }
srte3e69e5c2017-08-09 06:13:45 -0700497 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700498 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700499 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700500 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700501 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700502 report_block_info->report_block.last_sender_report_timestamp =
503 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000504
danilchap28b03eb2016-10-05 06:59:44 -0700505 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800506 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100507 // RFC3550, section 6.4.1, LSR field discription states:
508 // If no SR has been received yet, the field is set to zero.
509 // Receiver rtp_rtcp module is not expected to calculate rtt using
510 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800511 if (!receiver_only_ && send_time_ntp != 0) {
512 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100513 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800514 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000515
Danil Chapovalova094fd12016-02-22 18:59:36 +0100516 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800517 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100518 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700519 rtt_ms = CompactNtpRttToMs(rtt_ntp);
520 if (rtt_ms > report_block_info->max_rtt_ms)
521 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000522
danilchap28b03eb2016-10-05 06:59:44 -0700523 if (report_block_info->num_rtts == 0 ||
524 rtt_ms < report_block_info->min_rtt_ms)
525 report_block_info->min_rtt_ms = rtt_ms;
526
527 report_block_info->last_rtt_ms = rtt_ms;
528 report_block_info->sum_rtt_ms += rtt_ms;
529 ++report_block_info->num_rtts;
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100530
531 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000532 }
533
danilchap1b1863a2016-09-20 01:39:54 -0700534 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700535 report_block.source_ssrc(), rtt_ms);
danilchap28b03eb2016-10-05 06:59:44 -0700536 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000537}
538
danilchapec067e92017-02-21 05:38:19 -0800539RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
540 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700541 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800542 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700543 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800544 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800545 return tmmbr_info;
546}
547
548void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
549 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
550 if (tmmbr_it != tmmbr_infos_.end())
551 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000552}
553
danilchap9bf610e2017-02-20 06:03:01 -0800554RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700555 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800556 auto it = tmmbr_infos_.find(remote_ssrc);
557 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700558 return nullptr;
559 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000560}
561
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000562bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800563 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200564 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000565 return false;
566
567 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200568 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000569 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200570 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000571 return true;
572 }
573 return false;
574}
575
576bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800577 rtc::CritScope lock(&rtcp_receiver_lock_);
578 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000579 return false;
580
581 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800582 if (clock_->TimeInMilliseconds() >
583 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000584 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800585 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000586 return true;
587 }
588 return false;
589}
590
danilchap9bf610e2017-02-20 06:03:01 -0800591bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800592 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
danilchap8bab7962016-12-20 02:46:46 -0800594 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800595 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000596
danilchap9bf610e2017-02-20 06:03:01 -0800597 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800598 return false;
599
600 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800601 oldest_tmmbr_info_ms_ = -1;
602 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
603 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
604 if (tmmbr_info->last_time_received_ms > 0) {
605 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700606 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800607 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700608 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800609 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700610 // Send new TMMBN to all channels using the default codec.
611 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800612 } else if (oldest_tmmbr_info_ms_ == -1 ||
613 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
614 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000615 }
danilchap9bf610e2017-02-20 06:03:01 -0800616 ++tmmbr_it;
617 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700618 // When we dont have a last_time_received_ms and the object is marked
619 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800620 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000621 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800622 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000623 }
624 }
danilchap7851bda2016-09-29 15:28:07 -0700625 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000626}
627
danilchap2b616392016-08-18 06:17:42 -0700628std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800629 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800630 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
631 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700632 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700633
danilchap9bf610e2017-02-20 06:03:01 -0800634 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
635 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000636}
637
danilchap8bab7962016-12-20 02:46:46 -0800638void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700639 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700640 rtcp::Sdes sdes;
641 if (!sdes.Parse(rtcp_block)) {
642 ++num_skipped_packets_;
643 return;
644 }
645
646 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700647 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200648 {
spranga790d832016-12-02 07:29:44 -0800649 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700650 if (stats_callback_)
651 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200652 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000653 }
danilchap92ea6012016-09-23 10:36:03 -0700654 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000655}
656
danilchap8bab7962016-12-20 02:46:46 -0800657void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700658 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700659 rtcp::Nack nack;
660 if (!nack.Parse(rtcp_block)) {
661 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000662 return;
663 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000664
danilchap1b1863a2016-09-20 01:39:54 -0700665 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
666 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200667
danilchap142f0192016-10-20 08:22:42 -0700668 packet_information->nack_sequence_numbers.insert(
669 packet_information->nack_sequence_numbers.end(),
670 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700671 for (uint16_t packet_id : nack.packet_ids())
672 nack_stats_.ReportRequest(packet_id);
673
674 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700675 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000676 ++packet_type_counter_.nack_packets;
677 packet_type_counter_.nack_requests = nack_stats_.requests();
678 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
679 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000680}
681
danilchap8bab7962016-12-20 02:46:46 -0800682void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700683 rtcp::Bye bye;
684 if (!bye.Parse(rtcp_block)) {
685 ++num_skipped_packets_;
686 return;
687 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000688
danilchap8bab7962016-12-20 02:46:46 -0800689 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700690 for (auto& reports_per_receiver : received_report_blocks_)
691 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000692
danilchap9bf610e2017-02-20 06:03:01 -0800693 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
694 if (tmmbr_info)
695 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000696
danilchapefa966b2017-02-17 06:23:15 -0800697 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700698 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200699 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
700 if (it != received_rrtrs_ssrc_it_.end()) {
701 received_rrtrs_.erase(it->second);
702 received_rrtrs_ssrc_it_.erase(it);
703 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000704 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000705}
706
danilchap1b1863a2016-09-20 01:39:54 -0700707void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700708 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700709 rtcp::ExtendedReports xr;
710 if (!xr.Parse(rtcp_block)) {
711 ++num_skipped_packets_;
712 return;
713 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000714
danilchap80ac24d2016-10-31 08:40:47 -0700715 if (xr.rrtr())
716 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000717
danilchap80ac24d2016-10-31 08:40:47 -0700718 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
719 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800720
sprangb32aaf92017-08-28 05:49:12 -0700721 if (xr.target_bitrate()) {
722 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
723 packet_information);
724 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000725}
726
danilchap8bab7962016-12-20 02:46:46 -0800727void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
728 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200729 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
730 uint32_t local_receive_mid_ntp_time = CompactNtp(clock_->CurrentNtpTime());
731
732 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
733 if (it != received_rrtrs_ssrc_it_.end()) {
734 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
735 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
736 } else {
737 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
738 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
739 local_receive_mid_ntp_time);
740 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
741 } else {
742 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
743 << ", reached maximum number of stored RRTRs.";
744 }
745 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000746}
747
danilchap92ea6012016-09-23 10:36:03 -0700748void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700749 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
750 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000751
danilchap1b1863a2016-09-20 01:39:54 -0700752 // Caller should explicitly enable rtt calculation using extended reports.
753 if (!xr_rrtr_status_)
754 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200755
danilchap1b1863a2016-09-20 01:39:54 -0700756 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800757 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700758 // RFC3611, section 4.5, LRR field discription states:
759 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800760 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700761 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200762
danilchap8bab7962016-12-20 02:46:46 -0800763 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800764 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200765
danilchap8bab7962016-12-20 02:46:46 -0800766 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700767 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000768}
769
spranga790d832016-12-02 07:29:44 -0800770void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700771 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800772 const rtcp::TargetBitrate& target_bitrate,
773 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700774 if (ssrc != remote_ssrc_) {
775 return; // Not for us.
776 }
777
spranga790d832016-12-02 07:29:44 -0800778 BitrateAllocation bitrate_allocation;
779 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800780 if (item.spatial_layer >= kMaxSpatialLayers ||
781 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100782 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800783 << "Invalid layer in XR target bitrate pack: spatial index "
784 << item.spatial_layer << ", temporal index " << item.temporal_layer
785 << ", dropping.";
786 } else {
787 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
788 item.target_bitrate_kbps * 1000);
789 }
spranga790d832016-12-02 07:29:44 -0800790 }
791 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
792}
793
danilchap8bab7962016-12-20 02:46:46 -0800794void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700795 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700796 rtcp::Pli pli;
797 if (!pli.Parse(rtcp_block)) {
798 ++num_skipped_packets_;
799 return;
800 }
801
802 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000803 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000804
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000805 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000806 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700807 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000808 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
danilchap8bab7962016-12-20 02:46:46 -0800811void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700812 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700813 rtcp::Tmmbr tmmbr;
814 if (!tmmbr.Parse(rtcp_block)) {
815 ++num_skipped_packets_;
816 return;
817 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000818
danilchap7851bda2016-09-29 15:28:07 -0700819 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700820 if (tmmbr.media_ssrc()) {
821 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
822 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700823 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000824 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000825
danilchap1b1863a2016-09-20 01:39:54 -0700826 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800827 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
828 continue;
danilchap7851bda2016-09-29 15:28:07 -0700829
danilchapec067e92017-02-21 05:38:19 -0800830 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
831 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
832 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
833 request.bitrate_bps(),
834 request.packet_overhead());
835 entry->last_updated_ms = clock_->TimeInMilliseconds();
836
837 packet_information->packet_type_flags |= kRtcpTmmbr;
838 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000839 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000840}
841
danilchap8bab7962016-12-20 02:46:46 -0800842void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700843 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700844 rtcp::Tmmbn tmmbn;
845 if (!tmmbn.Parse(rtcp_block)) {
846 ++num_skipped_packets_;
847 return;
848 }
849
danilchapec067e92017-02-21 05:38:19 -0800850 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700851
danilchap92ea6012016-09-23 10:36:03 -0700852 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000853
danilchapec067e92017-02-21 05:38:19 -0800854 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700855}
856
danilchap8bab7962016-12-20 02:46:46 -0800857void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
858 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700859 rtcp::RapidResyncRequest sr_req;
860 if (!sr_req.Parse(rtcp_block)) {
861 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000862 return;
863 }
864
danilchap92ea6012016-09-23 10:36:03 -0700865 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000866}
867
danilchap1b1863a2016-09-20 01:39:54 -0700868void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700869 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700870 rtcp::Remb remb;
871 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700872 packet_information->packet_type_flags |= kRtcpRemb;
873 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700874 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000875 }
danilchap1b1863a2016-09-20 01:39:54 -0700876
877 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000878}
879
danilchap8bab7962016-12-20 02:46:46 -0800880void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700881 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700882 rtcp::Fir fir;
883 if (!fir.Parse(rtcp_block)) {
884 ++num_skipped_packets_;
885 return;
886 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000887
danilchap1b1863a2016-09-20 01:39:54 -0700888 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800889 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700890 if (main_ssrc_ != fir_request.ssrc)
891 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200892
893 ++packet_type_counter_.fir_packets;
894
danilchapefa966b2017-02-17 06:23:15 -0800895 int64_t now_ms = clock_->TimeInMilliseconds();
896 auto inserted = last_fir_.insert(std::make_pair(
897 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
898 if (!inserted.second) { // There was already an entry.
899 LastFirStatus* last_fir = &inserted.first->second;
900
danilchap7851bda2016-09-29 15:28:07 -0700901 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800902 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700903 continue;
904
danilchap7851bda2016-09-29 15:28:07 -0700905 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800906 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700907 continue;
908
danilchapefa966b2017-02-17 06:23:15 -0800909 last_fir->request_ms = now_ms;
910 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200911 }
danilchap7851bda2016-09-29 15:28:07 -0700912 // Received signal that we need to send a new key frame.
913 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000914 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000915}
916
Erik Språng6b8d3552015-09-24 15:06:57 +0200917void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700918 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700919 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700920 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
921 new rtcp::TransportFeedback());
922 if (!transport_feedback->Parse(rtcp_block)) {
923 ++num_skipped_packets_;
924 return;
925 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200926
danilchap92ea6012016-09-23 10:36:03 -0700927 packet_information->packet_type_flags |= kRtcpTransportFeedback;
928 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200929}
danilchap287e5482016-08-16 15:15:39 -0700930
danilchap9bf610e2017-02-20 06:03:01 -0800931void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700932 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700933 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700934 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000935
spranga790d832016-12-02 07:29:44 -0800936 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700937 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700938 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
939 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800940 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000941 }
danilchap853ecb22016-08-22 08:26:15 -0700942
danilchap9bf610e2017-02-20 06:03:01 -0800943 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800944 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000945}
946
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000947void RTCPReceiver::RegisterRtcpStatisticsCallback(
948 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800949 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000950 stats_callback_ = callback;
951}
952
953RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800954 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000955 return stats_callback_;
956}
957
danilchap8bab7962016-12-20 02:46:46 -0800958// Holding no Critical section.
959void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700960 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000961 // Process TMMBR and REMB first to avoid multiple callbacks
962 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700963 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000964 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800965 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000966 }
sprang7dc39f32015-10-13 09:17:48 -0700967 uint32_t local_ssrc;
968 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000969 {
970 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800971 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000972 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700973 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000974 }
danilchap92ea6012016-09-23 10:36:03 -0700975 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800976 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000977 }
danilchap92ea6012016-09-23 10:36:03 -0700978 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
979 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100980 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
981 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800982 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000983 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000984 }
spranga790d832016-12-02 07:29:44 -0800985
986 // We need feedback that we have received a report block(s) so that we
987 // can generate a new packet in a conference relay scenario, one received
988 // report can generate several RTCP packets, based on number relayed/mixed
989 // a send report block should go out to all receivers.
990 if (rtcp_intra_frame_observer_) {
991 RTC_DCHECK(!receiver_only_);
992 if ((packet_information.packet_type_flags & kRtcpPli) ||
993 (packet_information.packet_type_flags & kRtcpFir)) {
994 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100995 RTC_LOG(LS_VERBOSE)
996 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -0800997 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100998 RTC_LOG(LS_VERBOSE)
999 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001000 }
spranga790d832016-12-02 07:29:44 -08001001 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001002 }
spranga790d832016-12-02 07:29:44 -08001003 }
1004 if (rtcp_bandwidth_observer_) {
1005 RTC_DCHECK(!receiver_only_);
1006 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001007 RTC_LOG(LS_VERBOSE)
1008 << "Incoming REMB: "
1009 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001010 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1011 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001012 }
danilchap92ea6012016-09-23 10:36:03 -07001013 if ((packet_information.packet_type_flags & kRtcpSr) ||
1014 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001015 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001016 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001017 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001018 }
spranga790d832016-12-02 07:29:44 -08001019 }
1020 if ((packet_information.packet_type_flags & kRtcpSr) ||
1021 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001022 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001023 }
isheriff6b4b5f32016-06-08 00:24:21 -07001024
spranga790d832016-12-02 07:29:44 -08001025 if (transport_feedback_observer_ &&
1026 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1027 uint32_t media_source_ssrc =
1028 packet_information.transport_feedback->media_ssrc();
1029 if (media_source_ssrc == local_ssrc ||
1030 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1031 transport_feedback_observer_->OnTransportFeedback(
1032 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001033 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001034 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001035
spranga790d832016-12-02 07:29:44 -08001036 if (bitrate_allocation_observer_ &&
1037 packet_information.target_bitrate_allocation) {
1038 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1039 *packet_information.target_bitrate_allocation);
1040 }
1041
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001042 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001043 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001044 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001045 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001046 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001047 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001048 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001049 report_block.extended_highest_sequence_number;
1050 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001051 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001052
srte3e69e5c2017-08-09 06:13:45 -07001053 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001054 }
1055 }
1056 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001057}
1058
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001059int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001060 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001061 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001062
danilchap8bab7962016-12-20 02:46:46 -08001063 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001064 auto received_cname_it = received_cnames_.find(remoteSSRC);
1065 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001066 return -1;
danilchap95321242016-09-27 07:05:32 -07001067
1068 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1069 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001070 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
danilchap7851bda2016-09-29 15:28:07 -07001073std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001074 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001075 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001076
danilchap8bab7962016-12-20 02:46:46 -08001077 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001078 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001079
danilchap9bf610e2017-02-20 06:03:01 -08001080 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001081 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001082 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001083 // Erase timeout entries.
1084 it = kv.second.tmmbr.erase(it);
1085 } else {
1086 candidates.push_back(it->second.tmmbr_item);
1087 ++it;
1088 }
1089 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001090 }
danilchap287e5482016-08-16 15:15:39 -07001091 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001094} // namespace webrtc