blob: 666e1e23dc759b811417a4de75a1cae9478bf88a [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
sprang6d314c72016-12-06 06:08:53 -080021#include "webrtc/common_types.h"
spranga790d832016-12-02 07:29:44 -080022#include "webrtc/common_video/include/video_bitrate_allocator.h"
danilchap1b1863a2016-09-20 01:39:54 -070023#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
24#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
25#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
26#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
27#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/fir.h"
28#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
29#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/pli.h"
30#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
31#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
32#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h"
danilchap1b1863a2016-09-20 01:39:54 -070033#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
34#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
danilchap1b1863a2016-09-20 01:39:54 -070035#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
36#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
Erik Språng6b8d3552015-09-24 15:06:57 +020037#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
danilchap84432382017-02-09 05:21:42 -080038#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010039#include "webrtc/modules/rtp_rtcp/source/time_util.h"
danilchap13deaad2016-05-24 13:25:27 -070040#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020041#include "webrtc/rtc_base/checks.h"
42#include "webrtc/rtc_base/logging.h"
43#include "webrtc/rtc_base/trace_event.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),
danilchapa04d9c32017-07-25 04:03:39 -0700122 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100123 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000124 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700125 oldest_tmmbr_info_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800126 last_received_rr_ms_(0),
127 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800128 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200129 packet_type_counter_observer_(packet_type_counter_observer),
130 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800131 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
132 RTC_DCHECK(owner);
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.
danilchap0b4b7272016-10-06 09:24:45 -0700158 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800159 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
161
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000162uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800163 rtc::CritScope lock(&rtcp_receiver_lock_);
164 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000165}
166
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000167void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
168 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800169 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700170 main_ssrc_ = main_ssrc;
171 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
danilchap28b03eb2016-10-05 06:59:44 -0700174int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
175 int64_t* last_rtt_ms,
176 int64_t* avg_rtt_ms,
177 int64_t* min_rtt_ms,
178 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800179 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
danilchap28b03eb2016-10-05 06:59:44 -0700181 auto it = received_report_blocks_.find(main_ssrc_);
182 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000183 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700184
185 auto it_info = it->second.find(remote_ssrc);
186 if (it_info == it->second.end())
187 return -1;
188
189 const ReportBlockWithRtt* report_block = &it_info->second;
190
191 if (report_block->num_rtts == 0)
192 return -1;
193
194 if (last_rtt_ms)
195 *last_rtt_ms = report_block->last_rtt_ms;
196
197 if (avg_rtt_ms)
198 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
199
200 if (min_rtt_ms)
201 *min_rtt_ms = report_block->min_rtt_ms;
202
203 if (max_rtt_ms)
204 *max_rtt_ms = report_block->max_rtt_ms;
205
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000206 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000207}
208
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100209void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800210 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100211 xr_rrtr_status_ = enable;
212}
213
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000214bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800215 RTC_DCHECK(rtt_ms);
216 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000217 if (xr_rr_rtt_ms_ == 0) {
218 return false;
219 }
220 *rtt_ms = xr_rr_rtt_ms_;
221 xr_rr_rtt_ms_ = 0;
222 return true;
223}
224
danilchap8bab7962016-12-20 02:46:46 -0800225bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
226 uint32_t* received_ntp_frac,
227 uint32_t* rtcp_arrival_time_secs,
228 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700229 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800230 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700231 if (!last_received_sr_ntp_.Valid())
232 return false;
233
234 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800235 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700236 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800237 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700238 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700239
240 // Rtp time from incoming SenderReport.
241 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700242 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700243
244 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800245 if (rtcp_arrival_time_secs)
246 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
247 if (rtcp_arrival_time_frac)
248 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700249
danilchapda161d72016-08-19 07:29:46 -0700250 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251}
252
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000253bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
danilchap798896a2016-09-28 02:54:25 -0700254 rtcp::ReceiveTimeInfo* info) const {
danilchap0b4b7272016-10-06 09:24:45 -0700255 RTC_DCHECK(info);
danilchap8bab7962016-12-20 02:46:46 -0800256 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700257 if (!last_received_xr_ntp_.Valid())
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000258 return false;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000259
danilchap798896a2016-09-28 02:54:25 -0700260 info->ssrc = remote_time_info_.ssrc;
261 info->last_rr = remote_time_info_.last_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000262
263 // Get the delay since last received report (RFC 3611).
danilchap8bab7962016-12-20 02:46:46 -0800264 uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
danilchap37953762017-02-09 11:15:25 -0800265 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000266
danilchap8bab7962016-12-20 02:46:46 -0800267 info->delay_since_last_rr = now_ntp - receive_time_ntp;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000268 return true;
269}
270
danilchap28b03eb2016-10-05 06:59:44 -0700271// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000272int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700273 std::vector<RTCPReportBlock>* receive_blocks) const {
274 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800275 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700276 for (const auto& reports_per_receiver : received_report_blocks_)
277 for (const auto& report : reports_per_receiver.second)
278 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000279 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000280}
281
danilchap92ea6012016-09-23 10:36:03 -0700282bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
283 const uint8_t* packet_end,
284 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800285 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
danilchap1b1863a2016-09-20 01:39:54 -0700287 CommonHeader rtcp_block;
288 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
289 next_block = rtcp_block.NextPacket()) {
290 ptrdiff_t remaining_blocks_size = packet_end - next_block;
291 RTC_DCHECK_GT(remaining_blocks_size, 0);
292 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
293 if (next_block == packet_begin) {
294 // Failed to parse 1st header, nothing was extracted from this packet.
295 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
296 return false;
297 }
298 ++num_skipped_packets_;
299 break;
300 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000301
danilchap1b1863a2016-09-20 01:39:54 -0700302 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800303 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700304
danilchap1b1863a2016-09-20 01:39:54 -0700305 switch (rtcp_block.type()) {
306 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700307 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200308 break;
danilchap1b1863a2016-09-20 01:39:54 -0700309 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700310 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700311 break;
danilchap1b1863a2016-09-20 01:39:54 -0700312 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800313 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700314 break;
danilchap1b1863a2016-09-20 01:39:54 -0700315 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700316 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700317 break;
danilchap1b1863a2016-09-20 01:39:54 -0700318 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800319 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700320 break;
danilchap1b1863a2016-09-20 01:39:54 -0700321 case rtcp::Rtpfb::kPacketType:
322 switch (rtcp_block.fmt()) {
323 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800324 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700325 break;
326 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800327 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700328 break;
329 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800330 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700331 break;
332 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800333 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700334 break;
335 case rtcp::TransportFeedback::kFeedbackMessageType:
336 HandleTransportFeedback(rtcp_block, packet_information);
337 break;
338 default:
339 ++num_skipped_packets_;
340 break;
341 }
danilchapda161d72016-08-19 07:29:46 -0700342 break;
danilchap1b1863a2016-09-20 01:39:54 -0700343 case rtcp::Psfb::kPacketType:
344 switch (rtcp_block.fmt()) {
345 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800346 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700347 break;
danilchap1b1863a2016-09-20 01:39:54 -0700348 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800349 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700350 break;
351 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700352 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700353 break;
354 default:
355 ++num_skipped_packets_;
356 break;
357 }
danilchapda161d72016-08-19 07:29:46 -0700358 break;
359 default:
danilchap1b1863a2016-09-20 01:39:54 -0700360 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700361 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000362 }
danilchapda161d72016-08-19 07:29:46 -0700363 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000364
danilchap8bab7962016-12-20 02:46:46 -0800365 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700366 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
367 main_ssrc_, packet_type_counter_);
368 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000369
danilchap8bab7962016-12-20 02:46:46 -0800370 int64_t now_ms = clock_->TimeInMilliseconds();
371 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700372 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800373 last_skipped_packets_warning_ms_ = now_ms;
danilchapda161d72016-08-19 07:29:46 -0700374 LOG(LS_WARNING) << num_skipped_packets_
375 << " RTCP blocks were skipped due to being malformed or of "
376 "unrecognized/unsupported type, during the past "
377 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
378 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200379
danilchap1b1863a2016-09-20 01:39:54 -0700380 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000381}
382
danilchap92ea6012016-09-23 10:36:03 -0700383void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
384 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700385 rtcp::SenderReport sender_report;
386 if (!sender_report.Parse(rtcp_block)) {
387 ++num_skipped_packets_;
388 return;
389 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000390
danilchap8bab7962016-12-20 02:46:46 -0800391 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000392
danilchap8bab7962016-12-20 02:46:46 -0800393 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
danilchapec067e92017-02-21 05:38:19 -0800395 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700396
Danil Chapovalov91511f12016-09-15 16:24:02 +0200397 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800398 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700399
Danil Chapovalov91511f12016-09-15 16:24:02 +0200400 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800401 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200402 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700403 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700404
danilchapa04d9c32017-07-25 04:03:39 -0700405 remote_sender_ntp_time_ = sender_report.ntp();
406 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
danilchap37953762017-02-09 11:15:25 -0800407 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700408 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200409 // We will only store the send report from one source, but
410 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700411 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700412 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000413
danilchap1b1863a2016-09-20 01:39:54 -0700414 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800415 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000416}
417
danilchap92ea6012016-09-23 10:36:03 -0700418void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
419 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700420 rtcp::ReceiverReport receiver_report;
421 if (!receiver_report.Parse(rtcp_block)) {
422 ++num_skipped_packets_;
423 return;
424 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200425
stefanb33eed22017-02-02 03:57:02 -0800426 last_received_rr_ms_ = clock_->TimeInMilliseconds();
427
danilchap8bab7962016-12-20 02:46:46 -0800428 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200429
danilchap8bab7962016-12-20 02:46:46 -0800430 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200431
danilchapec067e92017-02-21 05:38:19 -0800432 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200433
434 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800435 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200436
danilchap92ea6012016-09-23 10:36:03 -0700437 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200438
danilchap1b1863a2016-09-20 01:39:54 -0700439 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800440 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200441}
442
danilchap92ea6012016-09-23 10:36:03 -0700443void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
444 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700445 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000446 // This will be called once per report block in the RTCP packet.
447 // We filter out all report blocks that are not for us.
448 // Each packet has max 31 RR blocks.
449 //
450 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000451
danilchap28b03eb2016-10-05 06:59:44 -0700452 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000453 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000454
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000455 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700456 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000457 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000458
danilchap28b03eb2016-10-05 06:59:44 -0700459 ReportBlockWithRtt* report_block_info =
460 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700461 report_block_info->report_block.sender_ssrc = remote_ssrc;
462 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
463 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
464 report_block_info->report_block.packets_lost = report_block.cumulative_lost();
danilchap1b1863a2016-09-20 01:39:54 -0700465 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700466 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000467 // We have successfully delivered new RTP packets to the remote side after
468 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800469 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000470 }
srte3e69e5c2017-08-09 06:13:45 -0700471 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700472 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700473 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700474 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700475 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700476 report_block_info->report_block.last_sender_report_timestamp =
477 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000478
danilchap28b03eb2016-10-05 06:59:44 -0700479 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800480 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100481 // RFC3550, section 6.4.1, LSR field discription states:
482 // If no SR has been received yet, the field is set to zero.
483 // Receiver rtp_rtcp module is not expected to calculate rtt using
484 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800485 if (!receiver_only_ && send_time_ntp != 0) {
486 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100487 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800488 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000489
Danil Chapovalova094fd12016-02-22 18:59:36 +0100490 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800491 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100492 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700493 rtt_ms = CompactNtpRttToMs(rtt_ntp);
494 if (rtt_ms > report_block_info->max_rtt_ms)
495 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000496
danilchap28b03eb2016-10-05 06:59:44 -0700497 if (report_block_info->num_rtts == 0 ||
498 rtt_ms < report_block_info->min_rtt_ms)
499 report_block_info->min_rtt_ms = rtt_ms;
500
501 report_block_info->last_rtt_ms = rtt_ms;
502 report_block_info->sum_rtt_ms += rtt_ms;
503 ++report_block_info->num_rtts;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000504 }
505
danilchap1b1863a2016-09-20 01:39:54 -0700506 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700507 report_block.source_ssrc(), rtt_ms);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000508
danilchap28b03eb2016-10-05 06:59:44 -0700509 packet_information->rtt_ms = rtt_ms;
510 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
danilchapec067e92017-02-21 05:38:19 -0800513RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
514 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700515 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800516 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700517 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800518 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800519 return tmmbr_info;
520}
521
522void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
523 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
524 if (tmmbr_it != tmmbr_infos_.end())
525 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000526}
527
danilchap9bf610e2017-02-20 06:03:01 -0800528RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700529 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800530 auto it = tmmbr_infos_.find(remote_ssrc);
531 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700532 return nullptr;
533 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000534}
535
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000536bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800537 rtc::CritScope lock(&rtcp_receiver_lock_);
538 if (last_received_rr_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000539 return false;
540
541 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800542 if (clock_->TimeInMilliseconds() > last_received_rr_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000543 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800544 last_received_rr_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000545 return true;
546 }
547 return false;
548}
549
550bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800551 rtc::CritScope lock(&rtcp_receiver_lock_);
552 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000553 return false;
554
555 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800556 if (clock_->TimeInMilliseconds() >
557 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000558 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800559 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000560 return true;
561 }
562 return false;
563}
564
danilchap9bf610e2017-02-20 06:03:01 -0800565bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800566 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000567
danilchap8bab7962016-12-20 02:46:46 -0800568 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700569 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -0800570 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
danilchap9bf610e2017-02-20 06:03:01 -0800572 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800573 return false;
574
575 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800576 oldest_tmmbr_info_ms_ = -1;
577 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
578 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
579 if (tmmbr_info->last_time_received_ms > 0) {
580 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700581 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800582 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700583 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800584 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700585 // Send new TMMBN to all channels using the default codec.
586 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800587 } else if (oldest_tmmbr_info_ms_ == -1 ||
588 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
589 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000590 }
danilchap9bf610e2017-02-20 06:03:01 -0800591 ++tmmbr_it;
592 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700593 // When we dont have a last_time_received_ms and the object is marked
594 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800595 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000596 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800597 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000598 }
599 }
danilchap7851bda2016-09-29 15:28:07 -0700600 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601}
602
danilchap2b616392016-08-18 06:17:42 -0700603std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800604 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800605 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
606 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700607 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700608
danilchap9bf610e2017-02-20 06:03:01 -0800609 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
610 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611}
612
danilchap8bab7962016-12-20 02:46:46 -0800613void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700614 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700615 rtcp::Sdes sdes;
616 if (!sdes.Parse(rtcp_block)) {
617 ++num_skipped_packets_;
618 return;
619 }
620
621 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700622 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200623 {
spranga790d832016-12-02 07:29:44 -0800624 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700625 if (stats_callback_)
626 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200627 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000628 }
danilchap92ea6012016-09-23 10:36:03 -0700629 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000630}
631
danilchap8bab7962016-12-20 02:46:46 -0800632void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700633 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700634 rtcp::Nack nack;
635 if (!nack.Parse(rtcp_block)) {
636 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000637 return;
638 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000639
danilchap1b1863a2016-09-20 01:39:54 -0700640 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
641 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200642
danilchap142f0192016-10-20 08:22:42 -0700643 packet_information->nack_sequence_numbers.insert(
644 packet_information->nack_sequence_numbers.end(),
645 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700646 for (uint16_t packet_id : nack.packet_ids())
647 nack_stats_.ReportRequest(packet_id);
648
649 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700650 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000651 ++packet_type_counter_.nack_packets;
652 packet_type_counter_.nack_requests = nack_stats_.requests();
653 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
654 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000655}
656
danilchap8bab7962016-12-20 02:46:46 -0800657void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700658 rtcp::Bye bye;
659 if (!bye.Parse(rtcp_block)) {
660 ++num_skipped_packets_;
661 return;
662 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000663
danilchap8bab7962016-12-20 02:46:46 -0800664 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700665 for (auto& reports_per_receiver : received_report_blocks_)
666 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000667
danilchap9bf610e2017-02-20 06:03:01 -0800668 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
669 if (tmmbr_info)
670 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000671
danilchapefa966b2017-02-17 06:23:15 -0800672 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700673 received_cnames_.erase(bye.sender_ssrc());
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000674 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000675}
676
danilchap1b1863a2016-09-20 01:39:54 -0700677void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700678 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700679 rtcp::ExtendedReports xr;
680 if (!xr.Parse(rtcp_block)) {
681 ++num_skipped_packets_;
682 return;
683 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000684
danilchap80ac24d2016-10-31 08:40:47 -0700685 if (xr.rrtr())
686 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000687
danilchap80ac24d2016-10-31 08:40:47 -0700688 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
689 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800690
sprangb32aaf92017-08-28 05:49:12 -0700691 if (xr.target_bitrate()) {
692 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
693 packet_information);
694 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000695}
696
danilchap8bab7962016-12-20 02:46:46 -0800697void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
698 const rtcp::Rrtr& rrtr) {
danilchap798896a2016-09-28 02:54:25 -0700699 remote_time_info_.ssrc = sender_ssrc;
700 remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
danilchap37953762017-02-09 11:15:25 -0800701 last_received_xr_ntp_ = clock_->CurrentNtpTime();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000702}
703
danilchap92ea6012016-09-23 10:36:03 -0700704void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700705 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
706 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000707
danilchap1b1863a2016-09-20 01:39:54 -0700708 // Caller should explicitly enable rtt calculation using extended reports.
709 if (!xr_rrtr_status_)
710 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200711
danilchap1b1863a2016-09-20 01:39:54 -0700712 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800713 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700714 // RFC3611, section 4.5, LRR field discription states:
715 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800716 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700717 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200718
danilchap8bab7962016-12-20 02:46:46 -0800719 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800720 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200721
danilchap8bab7962016-12-20 02:46:46 -0800722 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700723 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000724}
725
spranga790d832016-12-02 07:29:44 -0800726void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700727 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800728 const rtcp::TargetBitrate& target_bitrate,
729 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700730 if (ssrc != remote_ssrc_) {
731 return; // Not for us.
732 }
733
spranga790d832016-12-02 07:29:44 -0800734 BitrateAllocation bitrate_allocation;
735 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800736 if (item.spatial_layer >= kMaxSpatialLayers ||
737 item.temporal_layer >= kMaxTemporalStreams) {
738 LOG(LS_WARNING)
739 << "Invalid layer in XR target bitrate pack: spatial index "
740 << item.spatial_layer << ", temporal index " << item.temporal_layer
741 << ", dropping.";
742 } else {
743 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
744 item.target_bitrate_kbps * 1000);
745 }
spranga790d832016-12-02 07:29:44 -0800746 }
747 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
748}
749
danilchap8bab7962016-12-20 02:46:46 -0800750void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700751 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700752 rtcp::Pli pli;
753 if (!pli.Parse(rtcp_block)) {
754 ++num_skipped_packets_;
755 return;
756 }
757
758 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000759 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000760
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000761 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000762 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700763 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000764 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
766
danilchap8bab7962016-12-20 02:46:46 -0800767void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700768 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700769 rtcp::Tmmbr tmmbr;
770 if (!tmmbr.Parse(rtcp_block)) {
771 ++num_skipped_packets_;
772 return;
773 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000774
danilchap7851bda2016-09-29 15:28:07 -0700775 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700776 if (tmmbr.media_ssrc()) {
777 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
778 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700779 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000780 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000781
danilchap1b1863a2016-09-20 01:39:54 -0700782 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800783 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
784 continue;
danilchap7851bda2016-09-29 15:28:07 -0700785
danilchapec067e92017-02-21 05:38:19 -0800786 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
787 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
788 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
789 request.bitrate_bps(),
790 request.packet_overhead());
791 entry->last_updated_ms = clock_->TimeInMilliseconds();
792
793 packet_information->packet_type_flags |= kRtcpTmmbr;
794 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000795 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
danilchap8bab7962016-12-20 02:46:46 -0800798void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700799 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700800 rtcp::Tmmbn tmmbn;
801 if (!tmmbn.Parse(rtcp_block)) {
802 ++num_skipped_packets_;
803 return;
804 }
805
danilchapec067e92017-02-21 05:38:19 -0800806 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700807
danilchap92ea6012016-09-23 10:36:03 -0700808 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000809
danilchapec067e92017-02-21 05:38:19 -0800810 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700811}
812
danilchap8bab7962016-12-20 02:46:46 -0800813void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
814 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700815 rtcp::RapidResyncRequest sr_req;
816 if (!sr_req.Parse(rtcp_block)) {
817 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000818 return;
819 }
820
danilchap92ea6012016-09-23 10:36:03 -0700821 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
danilchap1b1863a2016-09-20 01:39:54 -0700824void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700825 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700826 rtcp::Remb remb;
827 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700828 packet_information->packet_type_flags |= kRtcpRemb;
829 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700830 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000831 }
danilchap1b1863a2016-09-20 01:39:54 -0700832
833 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000834}
835
danilchap8bab7962016-12-20 02:46:46 -0800836void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700837 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700838 rtcp::Fir fir;
839 if (!fir.Parse(rtcp_block)) {
840 ++num_skipped_packets_;
841 return;
842 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000843
danilchap1b1863a2016-09-20 01:39:54 -0700844 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800845 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700846 if (main_ssrc_ != fir_request.ssrc)
847 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200848
849 ++packet_type_counter_.fir_packets;
850
danilchapefa966b2017-02-17 06:23:15 -0800851 int64_t now_ms = clock_->TimeInMilliseconds();
852 auto inserted = last_fir_.insert(std::make_pair(
853 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
854 if (!inserted.second) { // There was already an entry.
855 LastFirStatus* last_fir = &inserted.first->second;
856
danilchap7851bda2016-09-29 15:28:07 -0700857 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800858 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700859 continue;
860
danilchap7851bda2016-09-29 15:28:07 -0700861 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800862 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700863 continue;
864
danilchapefa966b2017-02-17 06:23:15 -0800865 last_fir->request_ms = now_ms;
866 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200867 }
danilchap7851bda2016-09-29 15:28:07 -0700868 // Received signal that we need to send a new key frame.
869 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000870 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000871}
872
Erik Språng6b8d3552015-09-24 15:06:57 +0200873void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700874 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700875 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700876 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
877 new rtcp::TransportFeedback());
878 if (!transport_feedback->Parse(rtcp_block)) {
879 ++num_skipped_packets_;
880 return;
881 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200882
danilchap92ea6012016-09-23 10:36:03 -0700883 packet_information->packet_type_flags |= kRtcpTransportFeedback;
884 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200885}
danilchap287e5482016-08-16 15:15:39 -0700886
danilchap9bf610e2017-02-20 06:03:01 -0800887void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700888 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700889 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700890 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000891
spranga790d832016-12-02 07:29:44 -0800892 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700893 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700894 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
895 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800896 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000897 }
danilchap853ecb22016-08-22 08:26:15 -0700898
danilchap9bf610e2017-02-20 06:03:01 -0800899 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800900 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000901}
902
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000903void RTCPReceiver::RegisterRtcpStatisticsCallback(
904 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800905 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000906 stats_callback_ = callback;
907}
908
909RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800910 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000911 return stats_callback_;
912}
913
danilchap8bab7962016-12-20 02:46:46 -0800914// Holding no Critical section.
915void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700916 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000917 // Process TMMBR and REMB first to avoid multiple callbacks
918 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700919 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000920 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800921 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000922 }
sprang7dc39f32015-10-13 09:17:48 -0700923 uint32_t local_ssrc;
924 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000925 {
926 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800927 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000928 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700929 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000930 }
danilchap92ea6012016-09-23 10:36:03 -0700931 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800932 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000933 }
danilchap92ea6012016-09-23 10:36:03 -0700934 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
935 if (!packet_information.nack_sequence_numbers.empty()) {
andresp@webrtc.org4436b442014-06-04 09:05:30 +0000936 LOG(LS_VERBOSE) << "Incoming NACK length: "
danilchap92ea6012016-09-23 10:36:03 -0700937 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800938 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000939 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000940 }
spranga790d832016-12-02 07:29:44 -0800941
942 // We need feedback that we have received a report block(s) so that we
943 // can generate a new packet in a conference relay scenario, one received
944 // report can generate several RTCP packets, based on number relayed/mixed
945 // a send report block should go out to all receivers.
946 if (rtcp_intra_frame_observer_) {
947 RTC_DCHECK(!receiver_only_);
948 if ((packet_information.packet_type_flags & kRtcpPli) ||
949 (packet_information.packet_type_flags & kRtcpFir)) {
950 if (packet_information.packet_type_flags & kRtcpPli) {
951 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
952 << packet_information.remote_ssrc;
953 } else {
954 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
955 << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000956 }
spranga790d832016-12-02 07:29:44 -0800957 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000958 }
spranga790d832016-12-02 07:29:44 -0800959 }
960 if (rtcp_bandwidth_observer_) {
961 RTC_DCHECK(!receiver_only_);
962 if (packet_information.packet_type_flags & kRtcpRemb) {
963 LOG(LS_VERBOSE) << "Incoming REMB: "
964 << packet_information.receiver_estimated_max_bitrate_bps;
965 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
966 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +0000967 }
danilchap92ea6012016-09-23 10:36:03 -0700968 if ((packet_information.packet_type_flags & kRtcpSr) ||
969 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800970 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -0800971 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -0800972 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -0700973 }
spranga790d832016-12-02 07:29:44 -0800974 }
975 if ((packet_information.packet_type_flags & kRtcpSr) ||
976 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800977 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -0800978 }
isheriff6b4b5f32016-06-08 00:24:21 -0700979
spranga790d832016-12-02 07:29:44 -0800980 if (transport_feedback_observer_ &&
981 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
982 uint32_t media_source_ssrc =
983 packet_information.transport_feedback->media_ssrc();
984 if (media_source_ssrc == local_ssrc ||
985 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
986 transport_feedback_observer_->OnTransportFeedback(
987 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200988 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000989 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000990
spranga790d832016-12-02 07:29:44 -0800991 if (bitrate_allocation_observer_ &&
992 packet_information.target_bitrate_allocation) {
993 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
994 *packet_information.target_bitrate_allocation);
995 }
996
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200997 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -0800998 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000999 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001000 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001001 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001002 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001003 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001004 report_block.extended_highest_sequence_number;
1005 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001006 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001007
srte3e69e5c2017-08-09 06:13:45 -07001008 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001009 }
1010 }
1011 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001012}
1013
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001014int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001015 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001016 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001017
danilchap8bab7962016-12-20 02:46:46 -08001018 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001019 auto received_cname_it = received_cnames_.find(remoteSSRC);
1020 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001021 return -1;
danilchap95321242016-09-27 07:05:32 -07001022
1023 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1024 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001025 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001026}
1027
danilchap7851bda2016-09-29 15:28:07 -07001028std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001029 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001030 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001031
danilchap8bab7962016-12-20 02:46:46 -08001032 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -07001033 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -08001034 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
danilchap287e5482016-08-16 15:15:39 -07001035
danilchap9bf610e2017-02-20 06:03:01 -08001036 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001037 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001038 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001039 // Erase timeout entries.
1040 it = kv.second.tmmbr.erase(it);
1041 } else {
1042 candidates.push_back(it->second.tmmbr_item);
1043 ++it;
1044 }
1045 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001046 }
danilchap287e5482016-08-16 15:15:39 -07001047 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001048}
1049
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001050} // namespace webrtc