blob: 3977ee4f20e3c6e48fec646b9ef4dbaf82dbce4a [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
Peter Boströmfe7a80c2015-04-23 17:53:17 +020021#include "webrtc/base/checks.h"
Peter Boströmebc0b4e2015-10-28 16:39:33 +010022#include "webrtc/base/logging.h"
tommie4f96502015-10-20 23:00:48 -070023#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"
36#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rpsi.h"
37#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
38#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
39#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sli.h"
40#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
41#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
Erik Språng6b8d3552015-09-24 15:06:57 +020042#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
danilchap84432382017-02-09 05:21:42 -080043#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010044#include "webrtc/modules/rtp_rtcp/source/time_util.h"
danilchap13deaad2016-05-24 13:25:27 -070045#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010046#include "webrtc/system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000047
niklase@google.com470e71d2011-07-07 08:21:25 +000048namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070049namespace {
50
51using rtcp::CommonHeader;
52using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000054// The number of RTCP time intervals needed to trigger a timeout.
55const int kRrTimeoutIntervals = 3;
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
danilchap1b1863a2016-09-20 01:39:54 -070060} // namespace
61
danilchap92ea6012016-09-23 10:36:03 -070062struct RTCPReceiver::PacketInformation {
63 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
64
65 uint32_t remote_ssrc = 0;
66 std::vector<uint16_t> nack_sequence_numbers;
67 ReportBlockList report_blocks;
68 int64_t rtt_ms = 0;
69 uint8_t sli_picture_id = 0;
70 uint64_t rpsi_picture_id = 0;
71 uint32_t receiver_estimated_max_bitrate_bps = 0;
72 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
spranga790d832016-12-02 07:29:44 -080073 rtc::Optional<BitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070074};
75
danilchap7851bda2016-09-29 15:28:07 -070076struct RTCPReceiver::ReceiveInformation {
77 struct TimedTmmbrItem {
78 rtcp::TmmbItem tmmbr_item;
79 int64_t last_updated_ms;
80 };
81
82 int64_t last_time_received_ms = 0;
83
danilchap7851bda2016-09-29 15:28:07 -070084 bool ready_for_delete = false;
85
86 std::vector<rtcp::TmmbItem> tmmbn;
87 std::map<uint32_t, TimedTmmbrItem> tmmbr;
88};
89
danilchap28b03eb2016-10-05 06:59:44 -070090struct RTCPReceiver::ReportBlockWithRtt {
91 RTCPReportBlock report_block;
92
93 int64_t last_rtt_ms = 0;
94 int64_t min_rtt_ms = 0;
95 int64_t max_rtt_ms = 0;
96 int64_t sum_rtt_ms = 0;
97 size_t num_rtts = 0;
98};
99
danilchapefa966b2017-02-17 06:23:15 -0800100struct RTCPReceiver::LastFirStatus {
101 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
102 : request_ms(now_ms), sequence_number(sequence_number) {}
103 int64_t request_ms;
104 uint8_t sequence_number;
105};
106
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000107RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000108 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200109 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000110 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000111 RtcpBandwidthObserver* rtcp_bandwidth_observer,
112 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200113 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800114 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700115 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800116 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200117 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800118 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800119 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
120 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
121 transport_feedback_observer_(transport_feedback_observer),
122 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000123 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800124 remote_ssrc_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100125 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000126 xr_rr_rtt_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800127 last_received_rr_ms_(0),
128 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800129 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200130 packet_type_counter_observer_(packet_type_counter_observer),
131 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800132 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
133 RTC_DCHECK(owner);
134 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
danilchap28b03eb2016-10-05 06:59:44 -0700137RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
danilchap59cb2bd2016-08-29 11:08:47 -0700139bool RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700140 if (packet_size == 0) {
141 LOG(LS_WARNING) << "Incoming empty RTCP packet";
danilchap59cb2bd2016-08-29 11:08:47 -0700142 return false;
143 }
danilchap59cb2bd2016-08-29 11:08:47 -0700144
danilchap92ea6012016-09-23 10:36:03 -0700145 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700146 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
147 return false;
danilchap8bab7962016-12-20 02:46:46 -0800148 TriggerCallbacksFromRtcpPacket(packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700149 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150}
151
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000152int64_t RTCPReceiver::LastReceivedReceiverReport() const {
danilchap8bab7962016-12-20 02:46:46 -0800153 rtc::CritScope lock(&rtcp_receiver_lock_);
stefanb33eed22017-02-02 03:57:02 -0800154 return last_received_rr_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000155}
156
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000157void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800158 rtc::CritScope lock(&rtcp_receiver_lock_);
159 // New SSRC reset old reports.
160 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
danilchap0b4b7272016-10-06 09:24:45 -0700161 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800162 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000163}
164
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000165uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800166 rtc::CritScope lock(&rtcp_receiver_lock_);
167 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000168}
169
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000170void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
171 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800172 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700173 main_ssrc_ = main_ssrc;
174 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
danilchap28b03eb2016-10-05 06:59:44 -0700177int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
178 int64_t* last_rtt_ms,
179 int64_t* avg_rtt_ms,
180 int64_t* min_rtt_ms,
181 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800182 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
danilchap28b03eb2016-10-05 06:59:44 -0700184 auto it = received_report_blocks_.find(main_ssrc_);
185 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000186 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700187
188 auto it_info = it->second.find(remote_ssrc);
189 if (it_info == it->second.end())
190 return -1;
191
192 const ReportBlockWithRtt* report_block = &it_info->second;
193
194 if (report_block->num_rtts == 0)
195 return -1;
196
197 if (last_rtt_ms)
198 *last_rtt_ms = report_block->last_rtt_ms;
199
200 if (avg_rtt_ms)
201 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
202
203 if (min_rtt_ms)
204 *min_rtt_ms = report_block->min_rtt_ms;
205
206 if (max_rtt_ms)
207 *max_rtt_ms = report_block->max_rtt_ms;
208
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000209 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100212void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800213 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100214 xr_rrtr_status_ = enable;
215}
216
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000217bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800218 RTC_DCHECK(rtt_ms);
219 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000220 if (xr_rr_rtt_ms_ == 0) {
221 return false;
222 }
223 *rtt_ms = xr_rr_rtt_ms_;
224 xr_rr_rtt_ms_ = 0;
225 return true;
226}
227
danilchap8bab7962016-12-20 02:46:46 -0800228bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
229 uint32_t* received_ntp_frac,
230 uint32_t* rtcp_arrival_time_secs,
231 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700232 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800233 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700234 if (!last_received_sr_ntp_.Valid())
235 return false;
236
237 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800238 if (received_ntp_secs)
239 *received_ntp_secs = remote_sender_info_.NTPseconds;
240 if (received_ntp_frac)
241 *received_ntp_frac = remote_sender_info_.NTPfraction;
danilchap0b4b7272016-10-06 09:24:45 -0700242
243 // Rtp time from incoming SenderReport.
244 if (rtcp_timestamp)
danilchap8bab7962016-12-20 02:46:46 -0800245 *rtcp_timestamp = remote_sender_info_.RTPtimeStamp;
danilchap0b4b7272016-10-06 09:24:45 -0700246
247 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800248 if (rtcp_arrival_time_secs)
249 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
250 if (rtcp_arrival_time_frac)
251 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700252
danilchapda161d72016-08-19 07:29:46 -0700253 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000256bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
danilchap798896a2016-09-28 02:54:25 -0700257 rtcp::ReceiveTimeInfo* info) const {
danilchap0b4b7272016-10-06 09:24:45 -0700258 RTC_DCHECK(info);
danilchap8bab7962016-12-20 02:46:46 -0800259 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700260 if (!last_received_xr_ntp_.Valid())
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000261 return false;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000262
danilchap798896a2016-09-28 02:54:25 -0700263 info->ssrc = remote_time_info_.ssrc;
264 info->last_rr = remote_time_info_.last_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000265
266 // Get the delay since last received report (RFC 3611).
danilchap8bab7962016-12-20 02:46:46 -0800267 uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
danilchap37953762017-02-09 11:15:25 -0800268 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000269
danilchap8bab7962016-12-20 02:46:46 -0800270 info->delay_since_last_rr = now_ntp - receive_time_ntp;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000271 return true;
272}
273
danilchap8bab7962016-12-20 02:46:46 -0800274int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* sender_info) const {
275 RTC_DCHECK(sender_info);
276 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700277 if (!last_received_sr_ntp_.Valid())
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000278 return -1;
danilchap0b4b7272016-10-06 09:24:45 -0700279
danilchap8bab7962016-12-20 02:46:46 -0800280 memcpy(sender_info, &remote_sender_info_, sizeof(RTCPSenderInfo));
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000281 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
danilchap28b03eb2016-10-05 06:59:44 -0700284// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000285int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700286 std::vector<RTCPReportBlock>* receive_blocks) const {
287 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800288 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700289 for (const auto& reports_per_receiver : received_report_blocks_)
290 for (const auto& report : reports_per_receiver.second)
291 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000292 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000293}
294
danilchap92ea6012016-09-23 10:36:03 -0700295bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
296 const uint8_t* packet_end,
297 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800298 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
danilchap1b1863a2016-09-20 01:39:54 -0700300 CommonHeader rtcp_block;
301 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
302 next_block = rtcp_block.NextPacket()) {
303 ptrdiff_t remaining_blocks_size = packet_end - next_block;
304 RTC_DCHECK_GT(remaining_blocks_size, 0);
305 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
306 if (next_block == packet_begin) {
307 // Failed to parse 1st header, nothing was extracted from this packet.
308 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
309 return false;
310 }
311 ++num_skipped_packets_;
312 break;
313 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000314
danilchap1b1863a2016-09-20 01:39:54 -0700315 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800316 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700317
danilchap1b1863a2016-09-20 01:39:54 -0700318 switch (rtcp_block.type()) {
319 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700320 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200321 break;
danilchap1b1863a2016-09-20 01:39:54 -0700322 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700323 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700324 break;
danilchap1b1863a2016-09-20 01:39:54 -0700325 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800326 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700327 break;
danilchap1b1863a2016-09-20 01:39:54 -0700328 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700329 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700330 break;
danilchap1b1863a2016-09-20 01:39:54 -0700331 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800332 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700333 break;
danilchap1b1863a2016-09-20 01:39:54 -0700334 case rtcp::Rtpfb::kPacketType:
335 switch (rtcp_block.fmt()) {
336 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800337 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700338 break;
339 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800340 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700341 break;
342 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800343 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700344 break;
345 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800346 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700347 break;
348 case rtcp::TransportFeedback::kFeedbackMessageType:
349 HandleTransportFeedback(rtcp_block, packet_information);
350 break;
351 default:
352 ++num_skipped_packets_;
353 break;
354 }
danilchapda161d72016-08-19 07:29:46 -0700355 break;
danilchap1b1863a2016-09-20 01:39:54 -0700356 case rtcp::Psfb::kPacketType:
357 switch (rtcp_block.fmt()) {
358 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800359 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700360 break;
361 case rtcp::Sli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800362 HandleSli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700363 break;
364 case rtcp::Rpsi::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800365 HandleRpsi(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700366 break;
367 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800368 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700369 break;
370 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700371 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700372 break;
373 default:
374 ++num_skipped_packets_;
375 break;
376 }
danilchapda161d72016-08-19 07:29:46 -0700377 break;
378 default:
danilchap1b1863a2016-09-20 01:39:54 -0700379 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700380 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000381 }
danilchapda161d72016-08-19 07:29:46 -0700382 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000383
danilchap8bab7962016-12-20 02:46:46 -0800384 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700385 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
386 main_ssrc_, packet_type_counter_);
387 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000388
danilchap8bab7962016-12-20 02:46:46 -0800389 int64_t now_ms = clock_->TimeInMilliseconds();
390 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700391 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800392 last_skipped_packets_warning_ms_ = now_ms;
danilchapda161d72016-08-19 07:29:46 -0700393 LOG(LS_WARNING) << num_skipped_packets_
394 << " RTCP blocks were skipped due to being malformed or of "
395 "unrecognized/unsupported type, during the past "
396 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
397 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200398
danilchap1b1863a2016-09-20 01:39:54 -0700399 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000400}
401
danilchap92ea6012016-09-23 10:36:03 -0700402void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
403 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700404 rtcp::SenderReport sender_report;
405 if (!sender_report.Parse(rtcp_block)) {
406 ++num_skipped_packets_;
407 return;
408 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
danilchap8bab7962016-12-20 02:46:46 -0800410 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000411
danilchap8bab7962016-12-20 02:46:46 -0800412 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
danilchap8bab7962016-12-20 02:46:46 -0800414 CreateReceiveInformation(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700415
Danil Chapovalov91511f12016-09-15 16:24:02 +0200416 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800417 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700418
Danil Chapovalov91511f12016-09-15 16:24:02 +0200419 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800420 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200421 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700422 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700423
Danil Chapovalov91511f12016-09-15 16:24:02 +0200424 // Save the NTP time of this report.
danilchap8bab7962016-12-20 02:46:46 -0800425 remote_sender_info_.NTPseconds = sender_report.ntp().seconds();
426 remote_sender_info_.NTPfraction = sender_report.ntp().fractions();
427 remote_sender_info_.RTPtimeStamp = sender_report.rtp_timestamp();
428 remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
429 remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
danilchapda161d72016-08-19 07:29:46 -0700430
danilchap37953762017-02-09 11:15:25 -0800431 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700432 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200433 // We will only store the send report from one source, but
434 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700435 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700436 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000437
danilchap1b1863a2016-09-20 01:39:54 -0700438 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800439 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
danilchap92ea6012016-09-23 10:36:03 -0700442void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
443 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700444 rtcp::ReceiverReport receiver_report;
445 if (!receiver_report.Parse(rtcp_block)) {
446 ++num_skipped_packets_;
447 return;
448 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200449
stefanb33eed22017-02-02 03:57:02 -0800450 last_received_rr_ms_ = clock_->TimeInMilliseconds();
451
danilchap8bab7962016-12-20 02:46:46 -0800452 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200453
danilchap8bab7962016-12-20 02:46:46 -0800454 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200455
danilchap8bab7962016-12-20 02:46:46 -0800456 CreateReceiveInformation(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200457
458 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800459 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200460
danilchap92ea6012016-09-23 10:36:03 -0700461 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200462
danilchap1b1863a2016-09-20 01:39:54 -0700463 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800464 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200465}
466
danilchap92ea6012016-09-23 10:36:03 -0700467void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
468 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700469 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000470 // This will be called once per report block in the RTCP packet.
471 // We filter out all report blocks that are not for us.
472 // Each packet has max 31 RR blocks.
473 //
474 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
danilchap28b03eb2016-10-05 06:59:44 -0700476 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000477 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000479 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700480 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000481 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000482
danilchap28b03eb2016-10-05 06:59:44 -0700483 ReportBlockWithRtt* report_block_info =
484 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
danilchap28b03eb2016-10-05 06:59:44 -0700485 report_block_info->report_block.remoteSSRC = remote_ssrc;
486 report_block_info->report_block.sourceSSRC = report_block.source_ssrc();
487 report_block_info->report_block.fractionLost = report_block.fraction_lost();
488 report_block_info->report_block.cumulativeLost =
danilchap1b1863a2016-09-20 01:39:54 -0700489 report_block.cumulative_lost();
490 if (report_block.extended_high_seq_num() >
danilchap28b03eb2016-10-05 06:59:44 -0700491 report_block_info->report_block.extendedHighSeqNum) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000492 // We have successfully delivered new RTP packets to the remote side after
493 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800494 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000495 }
danilchap28b03eb2016-10-05 06:59:44 -0700496 report_block_info->report_block.extendedHighSeqNum =
danilchap1b1863a2016-09-20 01:39:54 -0700497 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700498 report_block_info->report_block.jitter = report_block.jitter();
499 report_block_info->report_block.delaySinceLastSR =
danilchap1b1863a2016-09-20 01:39:54 -0700500 report_block.delay_since_last_sr();
danilchap28b03eb2016-10-05 06:59:44 -0700501 report_block_info->report_block.lastSR = report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000502
danilchap28b03eb2016-10-05 06:59:44 -0700503 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800504 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100505 // RFC3550, section 6.4.1, LSR field discription states:
506 // If no SR has been received yet, the field is set to zero.
507 // Receiver rtp_rtcp module is not expected to calculate rtt using
508 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800509 if (!receiver_only_ && send_time_ntp != 0) {
510 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100511 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800512 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000513
Danil Chapovalova094fd12016-02-22 18:59:36 +0100514 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800515 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100516 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700517 rtt_ms = CompactNtpRttToMs(rtt_ntp);
518 if (rtt_ms > report_block_info->max_rtt_ms)
519 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
danilchap28b03eb2016-10-05 06:59:44 -0700521 if (report_block_info->num_rtts == 0 ||
522 rtt_ms < report_block_info->min_rtt_ms)
523 report_block_info->min_rtt_ms = rtt_ms;
524
525 report_block_info->last_rtt_ms = rtt_ms;
526 report_block_info->sum_rtt_ms += rtt_ms;
527 ++report_block_info->num_rtts;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000528 }
529
danilchap1b1863a2016-09-20 01:39:54 -0700530 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700531 report_block.source_ssrc(), rtt_ms);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000532
danilchap28b03eb2016-10-05 06:59:44 -0700533 packet_information->rtt_ms = rtt_ms;
534 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000535}
536
danilchap7851bda2016-09-29 15:28:07 -0700537void RTCPReceiver::CreateReceiveInformation(uint32_t remote_ssrc) {
538 // Create or find receive information.
539 ReceiveInformation* receive_info = &received_infos_[remote_ssrc];
540 // Update that this remote is alive.
danilchap8bab7962016-12-20 02:46:46 -0800541 receive_info->last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000542}
543
danilchap7851bda2016-09-29 15:28:07 -0700544RTCPReceiver::ReceiveInformation* RTCPReceiver::GetReceiveInformation(
545 uint32_t remote_ssrc) {
546 auto it = received_infos_.find(remote_ssrc);
547 if (it == received_infos_.end())
548 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
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000581bool RTCPReceiver::UpdateRTCPReceiveInformationTimers() {
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
stefanb33eed22017-02-02 03:57:02 -0800588 if (oldest_received_info_ms_ >= timeout_ms)
589 return false;
590
591 bool update_bounding_set = false;
592 oldest_received_info_ms_ = -1;
danilchap7851bda2016-09-29 15:28:07 -0700593 for (auto receive_info_it = received_infos_.begin();
594 receive_info_it != received_infos_.end();) {
595 ReceiveInformation* receive_info = &receive_info_it->second;
596 if (receive_info->last_time_received_ms > 0) {
stefanb33eed22017-02-02 03:57:02 -0800597 if (receive_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700598 // No rtcp packet for the last 5 regular intervals, reset limitations.
599 receive_info->tmmbr.clear();
600 // Prevent that we call this over and over again.
601 receive_info->last_time_received_ms = 0;
602 // Send new TMMBN to all channels using the default codec.
603 update_bounding_set = true;
stefanb33eed22017-02-02 03:57:02 -0800604 } else if (oldest_received_info_ms_ == -1 ||
605 receive_info->last_time_received_ms <
606 oldest_received_info_ms_) {
607 oldest_received_info_ms_ = receive_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000608 }
danilchap7851bda2016-09-29 15:28:07 -0700609 ++receive_info_it;
610 } else if (receive_info->ready_for_delete) {
611 // When we dont have a last_time_received_ms and the object is marked
612 // ready_for_delete it's removed from the map.
613 receive_info_it = received_infos_.erase(receive_info_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000614 } else {
danilchap7851bda2016-09-29 15:28:07 -0700615 ++receive_info_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000616 }
617 }
danilchap7851bda2016-09-29 15:28:07 -0700618 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000619}
620
danilchap2b616392016-08-18 06:17:42 -0700621std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800622 rtc::CritScope lock(&rtcp_receiver_lock_);
623 ReceiveInformation* receive_info = GetReceiveInformation(remote_ssrc_);
danilchap7851bda2016-09-29 15:28:07 -0700624 if (!receive_info)
danilchap2b616392016-08-18 06:17:42 -0700625 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700626
danilchap7851bda2016-09-29 15:28:07 -0700627 *tmmbr_owner = TMMBRHelp::IsOwner(receive_info->tmmbn, main_ssrc_);
628 return receive_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000629}
630
danilchap8bab7962016-12-20 02:46:46 -0800631void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700632 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700633 rtcp::Sdes sdes;
634 if (!sdes.Parse(rtcp_block)) {
635 ++num_skipped_packets_;
636 return;
637 }
638
639 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700640 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200641 {
spranga790d832016-12-02 07:29:44 -0800642 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700643 if (stats_callback_)
644 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200645 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000646 }
danilchap92ea6012016-09-23 10:36:03 -0700647 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000648}
649
danilchap8bab7962016-12-20 02:46:46 -0800650void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700651 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700652 rtcp::Nack nack;
653 if (!nack.Parse(rtcp_block)) {
654 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000655 return;
656 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000657
danilchap1b1863a2016-09-20 01:39:54 -0700658 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
659 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200660
danilchap142f0192016-10-20 08:22:42 -0700661 packet_information->nack_sequence_numbers.insert(
662 packet_information->nack_sequence_numbers.end(),
663 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700664 for (uint16_t packet_id : nack.packet_ids())
665 nack_stats_.ReportRequest(packet_id);
666
667 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700668 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000669 ++packet_type_counter_.nack_packets;
670 packet_type_counter_.nack_requests = nack_stats_.requests();
671 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
672 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000673}
674
danilchap8bab7962016-12-20 02:46:46 -0800675void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700676 rtcp::Bye bye;
677 if (!bye.Parse(rtcp_block)) {
678 ++num_skipped_packets_;
679 return;
680 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
danilchap8bab7962016-12-20 02:46:46 -0800682 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700683 for (auto& reports_per_receiver : received_report_blocks_)
684 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000685
danilchap7851bda2016-09-29 15:28:07 -0700686 // We can't delete it due to TMMBR.
687 ReceiveInformation* receive_info = GetReceiveInformation(bye.sender_ssrc());
688 if (receive_info)
689 receive_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000690
danilchapefa966b2017-02-17 06:23:15 -0800691 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700692 received_cnames_.erase(bye.sender_ssrc());
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000693 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000694}
695
danilchap1b1863a2016-09-20 01:39:54 -0700696void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700697 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700698 rtcp::ExtendedReports xr;
699 if (!xr.Parse(rtcp_block)) {
700 ++num_skipped_packets_;
701 return;
702 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000703
danilchap80ac24d2016-10-31 08:40:47 -0700704 if (xr.rrtr())
705 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000706
danilchap80ac24d2016-10-31 08:40:47 -0700707 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
708 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800709
710 if (xr.target_bitrate())
711 HandleXrTargetBitrate(*xr.target_bitrate(), packet_information);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000712}
713
danilchap8bab7962016-12-20 02:46:46 -0800714void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
715 const rtcp::Rrtr& rrtr) {
danilchap798896a2016-09-28 02:54:25 -0700716 remote_time_info_.ssrc = sender_ssrc;
717 remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
danilchap37953762017-02-09 11:15:25 -0800718 last_received_xr_ntp_ = clock_->CurrentNtpTime();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000719}
720
danilchap92ea6012016-09-23 10:36:03 -0700721void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700722 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
723 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000724
danilchap1b1863a2016-09-20 01:39:54 -0700725 // Caller should explicitly enable rtt calculation using extended reports.
726 if (!xr_rrtr_status_)
727 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200728
danilchap1b1863a2016-09-20 01:39:54 -0700729 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800730 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700731 // RFC3611, section 4.5, LRR field discription states:
732 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800733 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700734 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200735
danilchap8bab7962016-12-20 02:46:46 -0800736 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800737 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200738
danilchap8bab7962016-12-20 02:46:46 -0800739 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700740 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000741}
742
spranga790d832016-12-02 07:29:44 -0800743void RTCPReceiver::HandleXrTargetBitrate(
744 const rtcp::TargetBitrate& target_bitrate,
745 PacketInformation* packet_information) {
746 BitrateAllocation bitrate_allocation;
747 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800748 if (item.spatial_layer >= kMaxSpatialLayers ||
749 item.temporal_layer >= kMaxTemporalStreams) {
750 LOG(LS_WARNING)
751 << "Invalid layer in XR target bitrate pack: spatial index "
752 << item.spatial_layer << ", temporal index " << item.temporal_layer
753 << ", dropping.";
754 } else {
755 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
756 item.target_bitrate_kbps * 1000);
757 }
spranga790d832016-12-02 07:29:44 -0800758 }
759 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
760}
761
danilchap8bab7962016-12-20 02:46:46 -0800762void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700763 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700764 rtcp::Pli pli;
765 if (!pli.Parse(rtcp_block)) {
766 ++num_skipped_packets_;
767 return;
768 }
769
770 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000771 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000772
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000773 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000774 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700775 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000776 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
danilchap8bab7962016-12-20 02:46:46 -0800779void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700780 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700781 rtcp::Tmmbr tmmbr;
782 if (!tmmbr.Parse(rtcp_block)) {
783 ++num_skipped_packets_;
784 return;
785 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000786
danilchap7851bda2016-09-29 15:28:07 -0700787 uint32_t sender_ssrc = tmmbr.sender_ssrc();
788 ReceiveInformation* receive_info = GetReceiveInformation(sender_ssrc);
789 if (!receive_info) // This remote SSRC must be saved before.
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000790 return;
danilchap1b1863a2016-09-20 01:39:54 -0700791
792 if (tmmbr.media_ssrc()) {
793 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
794 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700795 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000796 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000797
danilchap1b1863a2016-09-20 01:39:54 -0700798 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
799 if (main_ssrc_ == request.ssrc() && request.bitrate_bps()) {
danilchap7851bda2016-09-29 15:28:07 -0700800 auto* entry = &receive_info->tmmbr[sender_ssrc];
801 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
802 request.bitrate_bps(),
803 request.packet_overhead());
danilchap8bab7962016-12-20 02:46:46 -0800804 entry->last_updated_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700805
danilchap92ea6012016-09-23 10:36:03 -0700806 packet_information->packet_type_flags |= kRtcpTmmbr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200807 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000808 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
danilchap8bab7962016-12-20 02:46:46 -0800811void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700812 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700813 rtcp::Tmmbn tmmbn;
814 if (!tmmbn.Parse(rtcp_block)) {
815 ++num_skipped_packets_;
816 return;
817 }
818
danilchap7851bda2016-09-29 15:28:07 -0700819 ReceiveInformation* receive_info = GetReceiveInformation(tmmbn.sender_ssrc());
820 if (!receive_info) // This remote SSRC must be saved before.
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000821 return;
danilchap1b1863a2016-09-20 01:39:54 -0700822
danilchap92ea6012016-09-23 10:36:03 -0700823 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000824
danilchap1b1863a2016-09-20 01:39:54 -0700825 for (const auto& item : tmmbn.items())
danilchap7851bda2016-09-29 15:28:07 -0700826 receive_info->tmmbn.push_back(item);
danilchap1b1863a2016-09-20 01:39:54 -0700827}
828
danilchap8bab7962016-12-20 02:46:46 -0800829void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
830 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700831 rtcp::RapidResyncRequest sr_req;
832 if (!sr_req.Parse(rtcp_block)) {
833 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000834 return;
835 }
836
danilchap92ea6012016-09-23 10:36:03 -0700837 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000838}
839
danilchap8bab7962016-12-20 02:46:46 -0800840void RTCPReceiver::HandleSli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700841 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700842 rtcp::Sli sli;
843 if (!sli.Parse(rtcp_block)) {
844 ++num_skipped_packets_;
845 return;
846 }
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200847
danilchap1b1863a2016-09-20 01:39:54 -0700848 for (const rtcp::Sli::Macroblocks& item : sli.macroblocks()) {
849 // In theory there could be multiple slices lost.
850 // Received signal that we need to refresh a slice.
danilchap92ea6012016-09-23 10:36:03 -0700851 packet_information->packet_type_flags |= kRtcpSli;
852 packet_information->sli_picture_id = item.picture_id();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000853 }
854}
855
danilchap8bab7962016-12-20 02:46:46 -0800856void RTCPReceiver::HandleRpsi(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700857 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700858 rtcp::Rpsi rpsi;
859 if (!rpsi.Parse(rtcp_block)) {
860 ++num_skipped_packets_;
861 return;
danilchapda161d72016-08-19 07:29:46 -0700862 }
danilchap1b1863a2016-09-20 01:39:54 -0700863
864 // Received signal that we have a confirmed reference picture.
danilchap92ea6012016-09-23 10:36:03 -0700865 packet_information->packet_type_flags |= kRtcpRpsi;
866 packet_information->rpsi_picture_id = rpsi.picture_id();
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
868
danilchap1b1863a2016-09-20 01:39:54 -0700869void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700870 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700871 rtcp::Remb remb;
872 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700873 packet_information->packet_type_flags |= kRtcpRemb;
874 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700875 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000876 }
danilchap1b1863a2016-09-20 01:39:54 -0700877
878 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000879}
880
danilchap8bab7962016-12-20 02:46:46 -0800881void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700882 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700883 rtcp::Fir fir;
884 if (!fir.Parse(rtcp_block)) {
885 ++num_skipped_packets_;
886 return;
887 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000888
danilchap1b1863a2016-09-20 01:39:54 -0700889 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800890 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700891 if (main_ssrc_ != fir_request.ssrc)
892 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200893
894 ++packet_type_counter_.fir_packets;
895
danilchapefa966b2017-02-17 06:23:15 -0800896 int64_t now_ms = clock_->TimeInMilliseconds();
897 auto inserted = last_fir_.insert(std::make_pair(
898 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
899 if (!inserted.second) { // There was already an entry.
900 LastFirStatus* last_fir = &inserted.first->second;
901
danilchap7851bda2016-09-29 15:28:07 -0700902 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800903 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700904 continue;
905
danilchap7851bda2016-09-29 15:28:07 -0700906 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800907 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700908 continue;
909
danilchapefa966b2017-02-17 06:23:15 -0800910 last_fir->request_ms = now_ms;
911 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200912 }
danilchap7851bda2016-09-29 15:28:07 -0700913 // Received signal that we need to send a new key frame.
914 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000915 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000916}
917
Erik Språng6b8d3552015-09-24 15:06:57 +0200918void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700919 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700920 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700921 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
922 new rtcp::TransportFeedback());
923 if (!transport_feedback->Parse(rtcp_block)) {
924 ++num_skipped_packets_;
925 return;
926 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200927
danilchap92ea6012016-09-23 10:36:03 -0700928 packet_information->packet_type_flags |= kRtcpTransportFeedback;
929 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200930}
danilchap287e5482016-08-16 15:15:39 -0700931
danilchap853ecb22016-08-22 08:26:15 -0700932void RTCPReceiver::UpdateTmmbr() {
933 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700934 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700935 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000936
spranga790d832016-12-02 07:29:44 -0800937 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700938 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700939 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
940 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800941 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000942 }
danilchap853ecb22016-08-22 08:26:15 -0700943
944 // Set bounding set: inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800945 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000946}
947
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000948void RTCPReceiver::RegisterRtcpStatisticsCallback(
949 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800950 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000951 stats_callback_ = callback;
952}
953
954RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800955 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000956 return stats_callback_;
957}
958
danilchap8bab7962016-12-20 02:46:46 -0800959// Holding no Critical section.
960void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700961 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000962 // Process TMMBR and REMB first to avoid multiple callbacks
963 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700964 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000965 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap853ecb22016-08-22 08:26:15 -0700966 UpdateTmmbr();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000967 }
sprang7dc39f32015-10-13 09:17:48 -0700968 uint32_t local_ssrc;
969 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000970 {
971 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800972 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000973 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700974 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000975 }
danilchap92ea6012016-09-23 10:36:03 -0700976 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800977 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000978 }
danilchap92ea6012016-09-23 10:36:03 -0700979 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
980 if (!packet_information.nack_sequence_numbers.empty()) {
andresp@webrtc.org4436b442014-06-04 09:05:30 +0000981 LOG(LS_VERBOSE) << "Incoming NACK length: "
danilchap92ea6012016-09-23 10:36:03 -0700982 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800983 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000984 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000985 }
spranga790d832016-12-02 07:29:44 -0800986
987 // We need feedback that we have received a report block(s) so that we
988 // can generate a new packet in a conference relay scenario, one received
989 // report can generate several RTCP packets, based on number relayed/mixed
990 // a send report block should go out to all receivers.
991 if (rtcp_intra_frame_observer_) {
992 RTC_DCHECK(!receiver_only_);
993 if ((packet_information.packet_type_flags & kRtcpPli) ||
994 (packet_information.packet_type_flags & kRtcpFir)) {
995 if (packet_information.packet_type_flags & kRtcpPli) {
996 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
997 << packet_information.remote_ssrc;
998 } else {
999 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
1000 << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001001 }
spranga790d832016-12-02 07:29:44 -08001002 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 }
spranga790d832016-12-02 07:29:44 -08001004 if (packet_information.packet_type_flags & kRtcpSli) {
1005 rtcp_intra_frame_observer_->OnReceivedSLI(
1006 local_ssrc, packet_information.sli_picture_id);
1007 }
1008 if (packet_information.packet_type_flags & kRtcpRpsi) {
1009 rtcp_intra_frame_observer_->OnReceivedRPSI(
1010 local_ssrc, packet_information.rpsi_picture_id);
1011 }
1012 }
1013 if (rtcp_bandwidth_observer_) {
1014 RTC_DCHECK(!receiver_only_);
1015 if (packet_information.packet_type_flags & kRtcpRemb) {
1016 LOG(LS_VERBOSE) << "Incoming REMB: "
1017 << packet_information.receiver_estimated_max_bitrate_bps;
1018 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1019 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001020 }
danilchap92ea6012016-09-23 10:36:03 -07001021 if ((packet_information.packet_type_flags & kRtcpSr) ||
1022 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001023 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001024 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001025 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001026 }
spranga790d832016-12-02 07:29:44 -08001027 }
1028 if ((packet_information.packet_type_flags & kRtcpSr) ||
1029 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001030 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001031 }
isheriff6b4b5f32016-06-08 00:24:21 -07001032
spranga790d832016-12-02 07:29:44 -08001033 if (transport_feedback_observer_ &&
1034 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1035 uint32_t media_source_ssrc =
1036 packet_information.transport_feedback->media_ssrc();
1037 if (media_source_ssrc == local_ssrc ||
1038 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1039 transport_feedback_observer_->OnTransportFeedback(
1040 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001041 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001042 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001043
spranga790d832016-12-02 07:29:44 -08001044 if (bitrate_allocation_observer_ &&
1045 packet_information.target_bitrate_allocation) {
1046 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1047 *packet_information.target_bitrate_allocation);
1048 }
1049
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001050 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001051 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001052 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001053 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001054 RtcpStatistics stats;
danilchap92ea6012016-09-23 10:36:03 -07001055 stats.cumulative_lost = report_block.cumulativeLost;
1056 stats.extended_max_sequence_number = report_block.extendedHighSeqNum;
1057 stats.fraction_lost = report_block.fractionLost;
1058 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001059
danilchap92ea6012016-09-23 10:36:03 -07001060 stats_callback_->StatisticsUpdated(stats, report_block.sourceSSRC);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001061 }
1062 }
1063 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001064}
1065
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001066int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001067 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001068 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001069
danilchap8bab7962016-12-20 02:46:46 -08001070 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001071 auto received_cname_it = received_cnames_.find(remoteSSRC);
1072 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001073 return -1;
danilchap95321242016-09-27 07:05:32 -07001074
1075 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1076 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001077 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001078}
1079
danilchap7851bda2016-09-29 15:28:07 -07001080std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001081 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001082 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001083
danilchap8bab7962016-12-20 02:46:46 -08001084 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -07001085 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -08001086 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
danilchap287e5482016-08-16 15:15:39 -07001087
danilchap7851bda2016-09-29 15:28:07 -07001088 for (auto& kv : received_infos_) {
1089 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001090 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001091 // Erase timeout entries.
1092 it = kv.second.tmmbr.erase(it);
1093 } else {
1094 candidates.push_back(it->second.tmmbr_item);
1095 ++it;
1096 }
1097 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001098 }
danilchap287e5482016-08-16 15:15:39 -07001099 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001100}
1101
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001102} // namespace webrtc