blob: 4a3235fffed61a1f88e3eee555dcca8f226ade5c [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
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000011#include "webrtc/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
Henrik Kjellanderdca1e092017-07-01 16:42:22 +020021#include "webrtc/base/checks.h"
22#include "webrtc/base/logging.h"
23#include "webrtc/base/trace_event.h"
sprang6d314c72016-12-06 06:08:53 -080024#include "webrtc/common_types.h"
spranga790d832016-12-02 07:29:44 -080025#include "webrtc/common_video/include/video_bitrate_allocator.h"
danilchap1b1863a2016-09-20 01:39:54 -070026#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
27#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
28#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
29#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
30#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/fir.h"
31#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
32#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/pli.h"
33#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
34#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
35#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h"
danilchap1b1863a2016-09-20 01:39:54 -070036#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
37#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
danilchap1b1863a2016-09-20 01:39:54 -070038#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
39#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
Erik Språng6b8d3552015-09-24 15:06:57 +020040#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
danilchap84432382017-02-09 05:21:42 -080041#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010042#include "webrtc/modules/rtp_rtcp/source/time_util.h"
danilchap13deaad2016-05-24 13:25:27 -070043#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010044#include "webrtc/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
Erik Språng6b8d3552015-09-24 15:06:57 +020055const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080056const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020057
danilchap1b1863a2016-09-20 01:39:54 -070058} // namespace
59
danilchap92ea6012016-09-23 10:36:03 -070060struct RTCPReceiver::PacketInformation {
61 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
62
63 uint32_t remote_ssrc = 0;
64 std::vector<uint16_t> nack_sequence_numbers;
65 ReportBlockList report_blocks;
66 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070067 uint32_t receiver_estimated_max_bitrate_bps = 0;
68 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
spranga790d832016-12-02 07:29:44 -080069 rtc::Optional<BitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070070};
71
danilchap9bf610e2017-02-20 06:03:01 -080072// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
73struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070074 struct TimedTmmbrItem {
75 rtcp::TmmbItem tmmbr_item;
76 int64_t last_updated_ms;
77 };
78
79 int64_t last_time_received_ms = 0;
80
danilchap7851bda2016-09-29 15:28:07 -070081 bool ready_for_delete = false;
82
83 std::vector<rtcp::TmmbItem> tmmbn;
84 std::map<uint32_t, TimedTmmbrItem> tmmbr;
85};
86
danilchap28b03eb2016-10-05 06:59:44 -070087struct RTCPReceiver::ReportBlockWithRtt {
88 RTCPReportBlock report_block;
89
90 int64_t last_rtt_ms = 0;
91 int64_t min_rtt_ms = 0;
92 int64_t max_rtt_ms = 0;
93 int64_t sum_rtt_ms = 0;
94 size_t num_rtts = 0;
95};
96
danilchapefa966b2017-02-17 06:23:15 -080097struct RTCPReceiver::LastFirStatus {
98 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
99 : request_ms(now_ms), sequence_number(sequence_number) {}
100 int64_t request_ms;
101 uint8_t sequence_number;
102};
103
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000104RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000105 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200106 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000107 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000108 RtcpBandwidthObserver* rtcp_bandwidth_observer,
109 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200110 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800111 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700112 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800113 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200114 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800115 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800116 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
117 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
118 transport_feedback_observer_(transport_feedback_observer),
119 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000120 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800121 remote_ssrc_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100122 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000123 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700124 oldest_tmmbr_info_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800125 last_received_rr_ms_(0),
126 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800127 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200128 packet_type_counter_observer_(packet_type_counter_observer),
129 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800130 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
131 RTC_DCHECK(owner);
132 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}
134
danilchap28b03eb2016-10-05 06:59:44 -0700135RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
danilchap59cb2bd2016-08-29 11:08:47 -0700137bool RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700138 if (packet_size == 0) {
139 LOG(LS_WARNING) << "Incoming empty RTCP packet";
danilchap59cb2bd2016-08-29 11:08:47 -0700140 return false;
141 }
danilchap59cb2bd2016-08-29 11:08:47 -0700142
danilchap92ea6012016-09-23 10:36:03 -0700143 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700144 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
145 return false;
danilchap8bab7962016-12-20 02:46:46 -0800146 TriggerCallbacksFromRtcpPacket(packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700147 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000148}
149
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000150int64_t RTCPReceiver::LastReceivedReceiverReport() const {
danilchap8bab7962016-12-20 02:46:46 -0800151 rtc::CritScope lock(&rtcp_receiver_lock_);
stefanb33eed22017-02-02 03:57:02 -0800152 return last_received_rr_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000153}
154
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000155void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800156 rtc::CritScope lock(&rtcp_receiver_lock_);
157 // New SSRC reset old reports.
158 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
danilchap0b4b7272016-10-06 09:24:45 -0700159 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800160 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000163uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800164 rtc::CritScope lock(&rtcp_receiver_lock_);
165 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000166}
167
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000168void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
169 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800170 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700171 main_ssrc_ = main_ssrc;
172 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173}
174
danilchap28b03eb2016-10-05 06:59:44 -0700175int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
176 int64_t* last_rtt_ms,
177 int64_t* avg_rtt_ms,
178 int64_t* min_rtt_ms,
179 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800180 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
danilchap28b03eb2016-10-05 06:59:44 -0700182 auto it = received_report_blocks_.find(main_ssrc_);
183 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000184 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700185
186 auto it_info = it->second.find(remote_ssrc);
187 if (it_info == it->second.end())
188 return -1;
189
190 const ReportBlockWithRtt* report_block = &it_info->second;
191
192 if (report_block->num_rtts == 0)
193 return -1;
194
195 if (last_rtt_ms)
196 *last_rtt_ms = report_block->last_rtt_ms;
197
198 if (avg_rtt_ms)
199 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
200
201 if (min_rtt_ms)
202 *min_rtt_ms = report_block->min_rtt_ms;
203
204 if (max_rtt_ms)
205 *max_rtt_ms = report_block->max_rtt_ms;
206
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000207 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100210void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800211 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100212 xr_rrtr_status_ = enable;
213}
214
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000215bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800216 RTC_DCHECK(rtt_ms);
217 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000218 if (xr_rr_rtt_ms_ == 0) {
219 return false;
220 }
221 *rtt_ms = xr_rr_rtt_ms_;
222 xr_rr_rtt_ms_ = 0;
223 return true;
224}
225
danilchap8bab7962016-12-20 02:46:46 -0800226bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
227 uint32_t* received_ntp_frac,
228 uint32_t* rtcp_arrival_time_secs,
229 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700230 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800231 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700232 if (!last_received_sr_ntp_.Valid())
233 return false;
234
235 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800236 if (received_ntp_secs)
237 *received_ntp_secs = remote_sender_info_.NTPseconds;
238 if (received_ntp_frac)
239 *received_ntp_frac = remote_sender_info_.NTPfraction;
danilchap0b4b7272016-10-06 09:24:45 -0700240
241 // Rtp time from incoming SenderReport.
242 if (rtcp_timestamp)
danilchap8bab7962016-12-20 02:46:46 -0800243 *rtcp_timestamp = remote_sender_info_.RTPtimeStamp;
danilchap0b4b7272016-10-06 09:24:45 -0700244
245 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800246 if (rtcp_arrival_time_secs)
247 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
248 if (rtcp_arrival_time_frac)
249 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700250
danilchapda161d72016-08-19 07:29:46 -0700251 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000252}
253
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000254bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
danilchap798896a2016-09-28 02:54:25 -0700255 rtcp::ReceiveTimeInfo* info) const {
danilchap0b4b7272016-10-06 09:24:45 -0700256 RTC_DCHECK(info);
danilchap8bab7962016-12-20 02:46:46 -0800257 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700258 if (!last_received_xr_ntp_.Valid())
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000259 return false;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000260
danilchap798896a2016-09-28 02:54:25 -0700261 info->ssrc = remote_time_info_.ssrc;
262 info->last_rr = remote_time_info_.last_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000263
264 // Get the delay since last received report (RFC 3611).
danilchap8bab7962016-12-20 02:46:46 -0800265 uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
danilchap37953762017-02-09 11:15:25 -0800266 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000267
danilchap8bab7962016-12-20 02:46:46 -0800268 info->delay_since_last_rr = now_ntp - receive_time_ntp;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000269 return true;
270}
271
danilchap8bab7962016-12-20 02:46:46 -0800272int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* sender_info) const {
273 RTC_DCHECK(sender_info);
274 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700275 if (!last_received_sr_ntp_.Valid())
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000276 return -1;
danilchap0b4b7272016-10-06 09:24:45 -0700277
danilchap8bab7962016-12-20 02:46:46 -0800278 memcpy(sender_info, &remote_sender_info_, sizeof(RTCPSenderInfo));
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
danilchap28b03eb2016-10-05 06:59:44 -0700282// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000283int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700284 std::vector<RTCPReportBlock>* receive_blocks) const {
285 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800286 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700287 for (const auto& reports_per_receiver : received_report_blocks_)
288 for (const auto& report : reports_per_receiver.second)
289 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000290 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291}
292
danilchap92ea6012016-09-23 10:36:03 -0700293bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
294 const uint8_t* packet_end,
295 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800296 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000297
danilchap1b1863a2016-09-20 01:39:54 -0700298 CommonHeader rtcp_block;
299 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
300 next_block = rtcp_block.NextPacket()) {
301 ptrdiff_t remaining_blocks_size = packet_end - next_block;
302 RTC_DCHECK_GT(remaining_blocks_size, 0);
303 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
304 if (next_block == packet_begin) {
305 // Failed to parse 1st header, nothing was extracted from this packet.
306 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
307 return false;
308 }
309 ++num_skipped_packets_;
310 break;
311 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000312
danilchap1b1863a2016-09-20 01:39:54 -0700313 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800314 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700315
danilchap1b1863a2016-09-20 01:39:54 -0700316 switch (rtcp_block.type()) {
317 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700318 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200319 break;
danilchap1b1863a2016-09-20 01:39:54 -0700320 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700321 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700322 break;
danilchap1b1863a2016-09-20 01:39:54 -0700323 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800324 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700325 break;
danilchap1b1863a2016-09-20 01:39:54 -0700326 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700327 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700328 break;
danilchap1b1863a2016-09-20 01:39:54 -0700329 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800330 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700331 break;
danilchap1b1863a2016-09-20 01:39:54 -0700332 case rtcp::Rtpfb::kPacketType:
333 switch (rtcp_block.fmt()) {
334 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800335 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700336 break;
337 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800338 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700339 break;
340 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800341 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700342 break;
343 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800344 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700345 break;
346 case rtcp::TransportFeedback::kFeedbackMessageType:
347 HandleTransportFeedback(rtcp_block, packet_information);
348 break;
349 default:
350 ++num_skipped_packets_;
351 break;
352 }
danilchapda161d72016-08-19 07:29:46 -0700353 break;
danilchap1b1863a2016-09-20 01:39:54 -0700354 case rtcp::Psfb::kPacketType:
355 switch (rtcp_block.fmt()) {
356 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800357 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700358 break;
danilchap1b1863a2016-09-20 01:39:54 -0700359 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800360 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700361 break;
362 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700363 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700364 break;
365 default:
366 ++num_skipped_packets_;
367 break;
368 }
danilchapda161d72016-08-19 07:29:46 -0700369 break;
370 default:
danilchap1b1863a2016-09-20 01:39:54 -0700371 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700372 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000373 }
danilchapda161d72016-08-19 07:29:46 -0700374 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000375
danilchap8bab7962016-12-20 02:46:46 -0800376 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700377 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
378 main_ssrc_, packet_type_counter_);
379 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000380
danilchap8bab7962016-12-20 02:46:46 -0800381 int64_t now_ms = clock_->TimeInMilliseconds();
382 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700383 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800384 last_skipped_packets_warning_ms_ = now_ms;
danilchapda161d72016-08-19 07:29:46 -0700385 LOG(LS_WARNING) << num_skipped_packets_
386 << " RTCP blocks were skipped due to being malformed or of "
387 "unrecognized/unsupported type, during the past "
388 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
389 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200390
danilchap1b1863a2016-09-20 01:39:54 -0700391 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000392}
393
danilchap92ea6012016-09-23 10:36:03 -0700394void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
395 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700396 rtcp::SenderReport sender_report;
397 if (!sender_report.Parse(rtcp_block)) {
398 ++num_skipped_packets_;
399 return;
400 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
danilchap8bab7962016-12-20 02:46:46 -0800402 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
danilchap8bab7962016-12-20 02:46:46 -0800404 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
danilchapec067e92017-02-21 05:38:19 -0800406 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700407
Danil Chapovalov91511f12016-09-15 16:24:02 +0200408 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800409 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700410
Danil Chapovalov91511f12016-09-15 16:24:02 +0200411 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800412 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200413 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700414 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700415
Danil Chapovalov91511f12016-09-15 16:24:02 +0200416 // Save the NTP time of this report.
danilchap8bab7962016-12-20 02:46:46 -0800417 remote_sender_info_.NTPseconds = sender_report.ntp().seconds();
418 remote_sender_info_.NTPfraction = sender_report.ntp().fractions();
419 remote_sender_info_.RTPtimeStamp = sender_report.rtp_timestamp();
420 remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
421 remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
danilchapda161d72016-08-19 07:29:46 -0700422
danilchap37953762017-02-09 11:15:25 -0800423 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700424 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200425 // We will only store the send report from one source, but
426 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700427 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700428 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000429
danilchap1b1863a2016-09-20 01:39:54 -0700430 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800431 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
danilchap92ea6012016-09-23 10:36:03 -0700434void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
435 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700436 rtcp::ReceiverReport receiver_report;
437 if (!receiver_report.Parse(rtcp_block)) {
438 ++num_skipped_packets_;
439 return;
440 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200441
stefanb33eed22017-02-02 03:57:02 -0800442 last_received_rr_ms_ = clock_->TimeInMilliseconds();
443
danilchap8bab7962016-12-20 02:46:46 -0800444 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200445
danilchap8bab7962016-12-20 02:46:46 -0800446 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200447
danilchapec067e92017-02-21 05:38:19 -0800448 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200449
450 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800451 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200452
danilchap92ea6012016-09-23 10:36:03 -0700453 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200454
danilchap1b1863a2016-09-20 01:39:54 -0700455 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800456 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200457}
458
danilchap92ea6012016-09-23 10:36:03 -0700459void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
460 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700461 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000462 // This will be called once per report block in the RTCP packet.
463 // We filter out all report blocks that are not for us.
464 // Each packet has max 31 RR blocks.
465 //
466 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000467
danilchap28b03eb2016-10-05 06:59:44 -0700468 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000469 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000471 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700472 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000473 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000474
danilchap28b03eb2016-10-05 06:59:44 -0700475 ReportBlockWithRtt* report_block_info =
476 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
danilchap28b03eb2016-10-05 06:59:44 -0700477 report_block_info->report_block.remoteSSRC = remote_ssrc;
478 report_block_info->report_block.sourceSSRC = report_block.source_ssrc();
479 report_block_info->report_block.fractionLost = report_block.fraction_lost();
480 report_block_info->report_block.cumulativeLost =
danilchap1b1863a2016-09-20 01:39:54 -0700481 report_block.cumulative_lost();
482 if (report_block.extended_high_seq_num() >
danilchap28b03eb2016-10-05 06:59:44 -0700483 report_block_info->report_block.extendedHighSeqNum) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000484 // We have successfully delivered new RTP packets to the remote side after
485 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800486 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000487 }
danilchap28b03eb2016-10-05 06:59:44 -0700488 report_block_info->report_block.extendedHighSeqNum =
danilchap1b1863a2016-09-20 01:39:54 -0700489 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700490 report_block_info->report_block.jitter = report_block.jitter();
491 report_block_info->report_block.delaySinceLastSR =
danilchap1b1863a2016-09-20 01:39:54 -0700492 report_block.delay_since_last_sr();
danilchap28b03eb2016-10-05 06:59:44 -0700493 report_block_info->report_block.lastSR = report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000494
danilchap28b03eb2016-10-05 06:59:44 -0700495 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800496 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100497 // RFC3550, section 6.4.1, LSR field discription states:
498 // If no SR has been received yet, the field is set to zero.
499 // Receiver rtp_rtcp module is not expected to calculate rtt using
500 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800501 if (!receiver_only_ && send_time_ntp != 0) {
502 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100503 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800504 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000505
Danil Chapovalova094fd12016-02-22 18:59:36 +0100506 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800507 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100508 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700509 rtt_ms = CompactNtpRttToMs(rtt_ntp);
510 if (rtt_ms > report_block_info->max_rtt_ms)
511 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000512
danilchap28b03eb2016-10-05 06:59:44 -0700513 if (report_block_info->num_rtts == 0 ||
514 rtt_ms < report_block_info->min_rtt_ms)
515 report_block_info->min_rtt_ms = rtt_ms;
516
517 report_block_info->last_rtt_ms = rtt_ms;
518 report_block_info->sum_rtt_ms += rtt_ms;
519 ++report_block_info->num_rtts;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000520 }
521
danilchap1b1863a2016-09-20 01:39:54 -0700522 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700523 report_block.source_ssrc(), rtt_ms);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000524
danilchap28b03eb2016-10-05 06:59:44 -0700525 packet_information->rtt_ms = rtt_ms;
526 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527}
528
danilchapec067e92017-02-21 05:38:19 -0800529RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
530 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700531 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800532 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700533 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800534 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800535 return tmmbr_info;
536}
537
538void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
539 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
540 if (tmmbr_it != tmmbr_infos_.end())
541 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000542}
543
danilchap9bf610e2017-02-20 06:03:01 -0800544RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700545 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800546 auto it = tmmbr_infos_.find(remote_ssrc);
547 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700548 return nullptr;
549 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000550}
551
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000552bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800553 rtc::CritScope lock(&rtcp_receiver_lock_);
554 if (last_received_rr_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000555 return false;
556
557 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800558 if (clock_->TimeInMilliseconds() > last_received_rr_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000559 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800560 last_received_rr_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000561 return true;
562 }
563 return false;
564}
565
566bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800567 rtc::CritScope lock(&rtcp_receiver_lock_);
568 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000569 return false;
570
571 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800572 if (clock_->TimeInMilliseconds() >
573 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000574 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800575 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000576 return true;
577 }
578 return false;
579}
580
danilchap9bf610e2017-02-20 06:03:01 -0800581bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800582 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000583
danilchap8bab7962016-12-20 02:46:46 -0800584 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700585 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -0800586 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000587
danilchap9bf610e2017-02-20 06:03:01 -0800588 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800589 return false;
590
591 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800592 oldest_tmmbr_info_ms_ = -1;
593 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
594 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
595 if (tmmbr_info->last_time_received_ms > 0) {
596 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700597 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800598 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700599 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800600 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700601 // Send new TMMBN to all channels using the default codec.
602 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800603 } else if (oldest_tmmbr_info_ms_ == -1 ||
604 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
605 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000606 }
danilchap9bf610e2017-02-20 06:03:01 -0800607 ++tmmbr_it;
608 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700609 // When we dont have a last_time_received_ms and the object is marked
610 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800611 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000612 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800613 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000614 }
615 }
danilchap7851bda2016-09-29 15:28:07 -0700616 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000617}
618
danilchap2b616392016-08-18 06:17:42 -0700619std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800620 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800621 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
622 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700623 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700624
danilchap9bf610e2017-02-20 06:03:01 -0800625 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
626 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000627}
628
danilchap8bab7962016-12-20 02:46:46 -0800629void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700630 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700631 rtcp::Sdes sdes;
632 if (!sdes.Parse(rtcp_block)) {
633 ++num_skipped_packets_;
634 return;
635 }
636
637 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700638 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200639 {
spranga790d832016-12-02 07:29:44 -0800640 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700641 if (stats_callback_)
642 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200643 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000644 }
danilchap92ea6012016-09-23 10:36:03 -0700645 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000646}
647
danilchap8bab7962016-12-20 02:46:46 -0800648void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700649 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700650 rtcp::Nack nack;
651 if (!nack.Parse(rtcp_block)) {
652 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000653 return;
654 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
danilchap1b1863a2016-09-20 01:39:54 -0700656 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
657 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200658
danilchap142f0192016-10-20 08:22:42 -0700659 packet_information->nack_sequence_numbers.insert(
660 packet_information->nack_sequence_numbers.end(),
661 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700662 for (uint16_t packet_id : nack.packet_ids())
663 nack_stats_.ReportRequest(packet_id);
664
665 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700666 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000667 ++packet_type_counter_.nack_packets;
668 packet_type_counter_.nack_requests = nack_stats_.requests();
669 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
670 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000671}
672
danilchap8bab7962016-12-20 02:46:46 -0800673void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700674 rtcp::Bye bye;
675 if (!bye.Parse(rtcp_block)) {
676 ++num_skipped_packets_;
677 return;
678 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000679
danilchap8bab7962016-12-20 02:46:46 -0800680 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700681 for (auto& reports_per_receiver : received_report_blocks_)
682 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000683
danilchap9bf610e2017-02-20 06:03:01 -0800684 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
685 if (tmmbr_info)
686 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000687
danilchapefa966b2017-02-17 06:23:15 -0800688 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700689 received_cnames_.erase(bye.sender_ssrc());
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000690 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000691}
692
danilchap1b1863a2016-09-20 01:39:54 -0700693void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700694 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700695 rtcp::ExtendedReports xr;
696 if (!xr.Parse(rtcp_block)) {
697 ++num_skipped_packets_;
698 return;
699 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000700
danilchap80ac24d2016-10-31 08:40:47 -0700701 if (xr.rrtr())
702 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000703
danilchap80ac24d2016-10-31 08:40:47 -0700704 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
705 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800706
707 if (xr.target_bitrate())
708 HandleXrTargetBitrate(*xr.target_bitrate(), packet_information);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000709}
710
danilchap8bab7962016-12-20 02:46:46 -0800711void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
712 const rtcp::Rrtr& rrtr) {
danilchap798896a2016-09-28 02:54:25 -0700713 remote_time_info_.ssrc = sender_ssrc;
714 remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
danilchap37953762017-02-09 11:15:25 -0800715 last_received_xr_ntp_ = clock_->CurrentNtpTime();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000716}
717
danilchap92ea6012016-09-23 10:36:03 -0700718void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700719 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
720 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000721
danilchap1b1863a2016-09-20 01:39:54 -0700722 // Caller should explicitly enable rtt calculation using extended reports.
723 if (!xr_rrtr_status_)
724 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200725
danilchap1b1863a2016-09-20 01:39:54 -0700726 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800727 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700728 // RFC3611, section 4.5, LRR field discription states:
729 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800730 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700731 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200732
danilchap8bab7962016-12-20 02:46:46 -0800733 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800734 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200735
danilchap8bab7962016-12-20 02:46:46 -0800736 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700737 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000738}
739
spranga790d832016-12-02 07:29:44 -0800740void RTCPReceiver::HandleXrTargetBitrate(
741 const rtcp::TargetBitrate& target_bitrate,
742 PacketInformation* packet_information) {
743 BitrateAllocation bitrate_allocation;
744 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800745 if (item.spatial_layer >= kMaxSpatialLayers ||
746 item.temporal_layer >= kMaxTemporalStreams) {
747 LOG(LS_WARNING)
748 << "Invalid layer in XR target bitrate pack: spatial index "
749 << item.spatial_layer << ", temporal index " << item.temporal_layer
750 << ", dropping.";
751 } else {
752 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
753 item.target_bitrate_kbps * 1000);
754 }
spranga790d832016-12-02 07:29:44 -0800755 }
756 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
757}
758
danilchap8bab7962016-12-20 02:46:46 -0800759void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700760 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700761 rtcp::Pli pli;
762 if (!pli.Parse(rtcp_block)) {
763 ++num_skipped_packets_;
764 return;
765 }
766
767 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000768 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000769
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000770 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000771 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700772 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774}
775
danilchap8bab7962016-12-20 02:46:46 -0800776void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700777 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700778 rtcp::Tmmbr tmmbr;
779 if (!tmmbr.Parse(rtcp_block)) {
780 ++num_skipped_packets_;
781 return;
782 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000783
danilchap7851bda2016-09-29 15:28:07 -0700784 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700785 if (tmmbr.media_ssrc()) {
786 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
787 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700788 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000789 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000790
danilchap1b1863a2016-09-20 01:39:54 -0700791 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800792 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
793 continue;
danilchap7851bda2016-09-29 15:28:07 -0700794
danilchapec067e92017-02-21 05:38:19 -0800795 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
796 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
797 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
798 request.bitrate_bps(),
799 request.packet_overhead());
800 entry->last_updated_ms = clock_->TimeInMilliseconds();
801
802 packet_information->packet_type_flags |= kRtcpTmmbr;
803 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000804 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
danilchap8bab7962016-12-20 02:46:46 -0800807void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700808 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700809 rtcp::Tmmbn tmmbn;
810 if (!tmmbn.Parse(rtcp_block)) {
811 ++num_skipped_packets_;
812 return;
813 }
814
danilchapec067e92017-02-21 05:38:19 -0800815 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700816
danilchap92ea6012016-09-23 10:36:03 -0700817 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000818
danilchapec067e92017-02-21 05:38:19 -0800819 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700820}
821
danilchap8bab7962016-12-20 02:46:46 -0800822void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
823 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700824 rtcp::RapidResyncRequest sr_req;
825 if (!sr_req.Parse(rtcp_block)) {
826 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000827 return;
828 }
829
danilchap92ea6012016-09-23 10:36:03 -0700830 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000831}
832
danilchap1b1863a2016-09-20 01:39:54 -0700833void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700834 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700835 rtcp::Remb remb;
836 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700837 packet_information->packet_type_flags |= kRtcpRemb;
838 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700839 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000840 }
danilchap1b1863a2016-09-20 01:39:54 -0700841
842 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000843}
844
danilchap8bab7962016-12-20 02:46:46 -0800845void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700846 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700847 rtcp::Fir fir;
848 if (!fir.Parse(rtcp_block)) {
849 ++num_skipped_packets_;
850 return;
851 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000852
danilchap1b1863a2016-09-20 01:39:54 -0700853 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800854 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700855 if (main_ssrc_ != fir_request.ssrc)
856 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200857
858 ++packet_type_counter_.fir_packets;
859
danilchapefa966b2017-02-17 06:23:15 -0800860 int64_t now_ms = clock_->TimeInMilliseconds();
861 auto inserted = last_fir_.insert(std::make_pair(
862 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
863 if (!inserted.second) { // There was already an entry.
864 LastFirStatus* last_fir = &inserted.first->second;
865
danilchap7851bda2016-09-29 15:28:07 -0700866 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800867 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700868 continue;
869
danilchap7851bda2016-09-29 15:28:07 -0700870 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800871 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700872 continue;
873
danilchapefa966b2017-02-17 06:23:15 -0800874 last_fir->request_ms = now_ms;
875 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200876 }
danilchap7851bda2016-09-29 15:28:07 -0700877 // Received signal that we need to send a new key frame.
878 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000879 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000880}
881
Erik Språng6b8d3552015-09-24 15:06:57 +0200882void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700883 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700884 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700885 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
886 new rtcp::TransportFeedback());
887 if (!transport_feedback->Parse(rtcp_block)) {
888 ++num_skipped_packets_;
889 return;
890 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200891
danilchap92ea6012016-09-23 10:36:03 -0700892 packet_information->packet_type_flags |= kRtcpTransportFeedback;
893 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200894}
danilchap287e5482016-08-16 15:15:39 -0700895
danilchap9bf610e2017-02-20 06:03:01 -0800896void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700897 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700898 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700899 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000900
spranga790d832016-12-02 07:29:44 -0800901 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700902 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700903 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
904 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800905 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000906 }
danilchap853ecb22016-08-22 08:26:15 -0700907
danilchap9bf610e2017-02-20 06:03:01 -0800908 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800909 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000910}
911
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000912void RTCPReceiver::RegisterRtcpStatisticsCallback(
913 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800914 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000915 stats_callback_ = callback;
916}
917
918RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800919 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000920 return stats_callback_;
921}
922
danilchap8bab7962016-12-20 02:46:46 -0800923// Holding no Critical section.
924void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700925 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000926 // Process TMMBR and REMB first to avoid multiple callbacks
927 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700928 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000929 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800930 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000931 }
sprang7dc39f32015-10-13 09:17:48 -0700932 uint32_t local_ssrc;
933 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000934 {
935 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800936 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000937 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700938 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000939 }
danilchap92ea6012016-09-23 10:36:03 -0700940 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800941 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000942 }
danilchap92ea6012016-09-23 10:36:03 -0700943 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
944 if (!packet_information.nack_sequence_numbers.empty()) {
andresp@webrtc.org4436b442014-06-04 09:05:30 +0000945 LOG(LS_VERBOSE) << "Incoming NACK length: "
danilchap92ea6012016-09-23 10:36:03 -0700946 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800947 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000948 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000949 }
spranga790d832016-12-02 07:29:44 -0800950
951 // We need feedback that we have received a report block(s) so that we
952 // can generate a new packet in a conference relay scenario, one received
953 // report can generate several RTCP packets, based on number relayed/mixed
954 // a send report block should go out to all receivers.
955 if (rtcp_intra_frame_observer_) {
956 RTC_DCHECK(!receiver_only_);
957 if ((packet_information.packet_type_flags & kRtcpPli) ||
958 (packet_information.packet_type_flags & kRtcpFir)) {
959 if (packet_information.packet_type_flags & kRtcpPli) {
960 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
961 << packet_information.remote_ssrc;
962 } else {
963 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
964 << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000965 }
spranga790d832016-12-02 07:29:44 -0800966 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000967 }
spranga790d832016-12-02 07:29:44 -0800968 }
969 if (rtcp_bandwidth_observer_) {
970 RTC_DCHECK(!receiver_only_);
971 if (packet_information.packet_type_flags & kRtcpRemb) {
972 LOG(LS_VERBOSE) << "Incoming REMB: "
973 << packet_information.receiver_estimated_max_bitrate_bps;
974 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
975 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +0000976 }
danilchap92ea6012016-09-23 10:36:03 -0700977 if ((packet_information.packet_type_flags & kRtcpSr) ||
978 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800979 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -0800980 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -0800981 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -0700982 }
spranga790d832016-12-02 07:29:44 -0800983 }
984 if ((packet_information.packet_type_flags & kRtcpSr) ||
985 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800986 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -0800987 }
isheriff6b4b5f32016-06-08 00:24:21 -0700988
spranga790d832016-12-02 07:29:44 -0800989 if (transport_feedback_observer_ &&
990 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
991 uint32_t media_source_ssrc =
992 packet_information.transport_feedback->media_ssrc();
993 if (media_source_ssrc == local_ssrc ||
994 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
995 transport_feedback_observer_->OnTransportFeedback(
996 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200997 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000998 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000999
spranga790d832016-12-02 07:29:44 -08001000 if (bitrate_allocation_observer_ &&
1001 packet_information.target_bitrate_allocation) {
1002 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1003 *packet_information.target_bitrate_allocation);
1004 }
1005
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001006 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001007 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001008 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001009 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001010 RtcpStatistics stats;
danilchap92ea6012016-09-23 10:36:03 -07001011 stats.cumulative_lost = report_block.cumulativeLost;
1012 stats.extended_max_sequence_number = report_block.extendedHighSeqNum;
1013 stats.fraction_lost = report_block.fractionLost;
1014 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001015
danilchap92ea6012016-09-23 10:36:03 -07001016 stats_callback_->StatisticsUpdated(stats, report_block.sourceSSRC);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001017 }
1018 }
1019 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001020}
1021
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001022int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001023 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001024 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001025
danilchap8bab7962016-12-20 02:46:46 -08001026 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001027 auto received_cname_it = received_cnames_.find(remoteSSRC);
1028 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001029 return -1;
danilchap95321242016-09-27 07:05:32 -07001030
1031 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1032 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001033 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034}
1035
danilchap7851bda2016-09-29 15:28:07 -07001036std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001037 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001038 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001039
danilchap8bab7962016-12-20 02:46:46 -08001040 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -07001041 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -08001042 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
danilchap287e5482016-08-16 15:15:39 -07001043
danilchap9bf610e2017-02-20 06:03:01 -08001044 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001045 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001046 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001047 // Erase timeout entries.
1048 it = kv.second.tmmbr.erase(it);
1049 } else {
1050 candidates.push_back(it->second.tmmbr_item);
1051 ++it;
1052 }
1053 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001054 }
danilchap287e5482016-08-16 15:15:39 -07001055 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001056}
1057
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001058} // namespace webrtc