blob: b5e0d9a18ea4bc76d445762c65987c60becea78a [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"
danilchap1b1863a2016-09-20 01:39:54 -070036#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
37#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
danilchap1b1863a2016-09-20 01:39:54 -070038#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
39#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
Erik Språng6b8d3552015-09-24 15:06:57 +020040#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
danilchap84432382017-02-09 05:21:42 -080041#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010042#include "webrtc/modules/rtp_rtcp/source/time_util.h"
danilchap13deaad2016-05-24 13:25:27 -070043#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010044#include "webrtc/system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000045
niklase@google.com470e71d2011-07-07 08:21:25 +000046namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070047namespace {
48
49using rtcp::CommonHeader;
50using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000052// The number of RTCP time intervals needed to trigger a timeout.
53const int kRrTimeoutIntervals = 3;
54
Erik Språng6b8d3552015-09-24 15:06:57 +020055const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080056const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020057
danilchap1b1863a2016-09-20 01:39:54 -070058} // namespace
59
danilchap92ea6012016-09-23 10:36:03 -070060struct RTCPReceiver::PacketInformation {
61 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
62
63 uint32_t remote_ssrc = 0;
64 std::vector<uint16_t> nack_sequence_numbers;
65 ReportBlockList report_blocks;
66 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070067 uint32_t receiver_estimated_max_bitrate_bps = 0;
68 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
spranga790d832016-12-02 07:29:44 -080069 rtc::Optional<BitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070070};
71
danilchap9bf610e2017-02-20 06:03:01 -080072// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
73struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070074 struct TimedTmmbrItem {
75 rtcp::TmmbItem tmmbr_item;
76 int64_t last_updated_ms;
77 };
78
79 int64_t last_time_received_ms = 0;
80
danilchap7851bda2016-09-29 15:28:07 -070081 bool ready_for_delete = false;
82
83 std::vector<rtcp::TmmbItem> tmmbn;
84 std::map<uint32_t, TimedTmmbrItem> tmmbr;
85};
86
danilchap28b03eb2016-10-05 06:59:44 -070087struct RTCPReceiver::ReportBlockWithRtt {
88 RTCPReportBlock report_block;
89
90 int64_t last_rtt_ms = 0;
91 int64_t min_rtt_ms = 0;
92 int64_t max_rtt_ms = 0;
93 int64_t sum_rtt_ms = 0;
94 size_t num_rtts = 0;
95};
96
danilchapefa966b2017-02-17 06:23:15 -080097struct RTCPReceiver::LastFirStatus {
98 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
99 : request_ms(now_ms), sequence_number(sequence_number) {}
100 int64_t request_ms;
101 uint8_t sequence_number;
102};
103
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000104RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000105 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200106 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000107 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000108 RtcpBandwidthObserver* rtcp_bandwidth_observer,
109 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200110 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800111 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700112 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800113 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200114 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800115 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800116 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
117 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
118 transport_feedback_observer_(transport_feedback_observer),
119 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000120 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800121 remote_ssrc_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100122 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000123 xr_rr_rtt_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800124 last_received_rr_ms_(0),
125 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800126 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200127 packet_type_counter_observer_(packet_type_counter_observer),
128 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800129 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
130 RTC_DCHECK(owner);
131 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000132}
133
danilchap28b03eb2016-10-05 06:59:44 -0700134RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
danilchap59cb2bd2016-08-29 11:08:47 -0700136bool RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700137 if (packet_size == 0) {
138 LOG(LS_WARNING) << "Incoming empty RTCP packet";
danilchap59cb2bd2016-08-29 11:08:47 -0700139 return false;
140 }
danilchap59cb2bd2016-08-29 11:08:47 -0700141
danilchap92ea6012016-09-23 10:36:03 -0700142 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700143 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
144 return false;
danilchap8bab7962016-12-20 02:46:46 -0800145 TriggerCallbacksFromRtcpPacket(packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700146 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147}
148
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000149int64_t RTCPReceiver::LastReceivedReceiverReport() const {
danilchap8bab7962016-12-20 02:46:46 -0800150 rtc::CritScope lock(&rtcp_receiver_lock_);
stefanb33eed22017-02-02 03:57:02 -0800151 return last_received_rr_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000152}
153
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000154void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800155 rtc::CritScope lock(&rtcp_receiver_lock_);
156 // New SSRC reset old reports.
157 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
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)
236 *received_ntp_secs = remote_sender_info_.NTPseconds;
237 if (received_ntp_frac)
238 *received_ntp_frac = remote_sender_info_.NTPfraction;
danilchap0b4b7272016-10-06 09:24:45 -0700239
240 // Rtp time from incoming SenderReport.
241 if (rtcp_timestamp)
danilchap8bab7962016-12-20 02:46:46 -0800242 *rtcp_timestamp = remote_sender_info_.RTPtimeStamp;
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
danilchap8bab7962016-12-20 02:46:46 -0800271int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* sender_info) const {
272 RTC_DCHECK(sender_info);
273 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700274 if (!last_received_sr_ntp_.Valid())
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000275 return -1;
danilchap0b4b7272016-10-06 09:24:45 -0700276
danilchap8bab7962016-12-20 02:46:46 -0800277 memcpy(sender_info, &remote_sender_info_, sizeof(RTCPSenderInfo));
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000278 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000279}
280
danilchap28b03eb2016-10-05 06:59:44 -0700281// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000282int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700283 std::vector<RTCPReportBlock>* receive_blocks) const {
284 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800285 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700286 for (const auto& reports_per_receiver : received_report_blocks_)
287 for (const auto& report : reports_per_receiver.second)
288 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000289 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290}
291
danilchap92ea6012016-09-23 10:36:03 -0700292bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
293 const uint8_t* packet_end,
294 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800295 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
danilchap1b1863a2016-09-20 01:39:54 -0700297 CommonHeader rtcp_block;
298 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
299 next_block = rtcp_block.NextPacket()) {
300 ptrdiff_t remaining_blocks_size = packet_end - next_block;
301 RTC_DCHECK_GT(remaining_blocks_size, 0);
302 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
303 if (next_block == packet_begin) {
304 // Failed to parse 1st header, nothing was extracted from this packet.
305 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
306 return false;
307 }
308 ++num_skipped_packets_;
309 break;
310 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000311
danilchap1b1863a2016-09-20 01:39:54 -0700312 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800313 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700314
danilchap1b1863a2016-09-20 01:39:54 -0700315 switch (rtcp_block.type()) {
316 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700317 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200318 break;
danilchap1b1863a2016-09-20 01:39:54 -0700319 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700320 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700321 break;
danilchap1b1863a2016-09-20 01:39:54 -0700322 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800323 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700324 break;
danilchap1b1863a2016-09-20 01:39:54 -0700325 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700326 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700327 break;
danilchap1b1863a2016-09-20 01:39:54 -0700328 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800329 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700330 break;
danilchap1b1863a2016-09-20 01:39:54 -0700331 case rtcp::Rtpfb::kPacketType:
332 switch (rtcp_block.fmt()) {
333 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800334 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700335 break;
336 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800337 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700338 break;
339 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800340 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700341 break;
342 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800343 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700344 break;
345 case rtcp::TransportFeedback::kFeedbackMessageType:
346 HandleTransportFeedback(rtcp_block, packet_information);
347 break;
348 default:
349 ++num_skipped_packets_;
350 break;
351 }
danilchapda161d72016-08-19 07:29:46 -0700352 break;
danilchap1b1863a2016-09-20 01:39:54 -0700353 case rtcp::Psfb::kPacketType:
354 switch (rtcp_block.fmt()) {
355 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800356 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700357 break;
danilchap1b1863a2016-09-20 01:39:54 -0700358 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800359 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700360 break;
361 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700362 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700363 break;
364 default:
365 ++num_skipped_packets_;
366 break;
367 }
danilchapda161d72016-08-19 07:29:46 -0700368 break;
369 default:
danilchap1b1863a2016-09-20 01:39:54 -0700370 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700371 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000372 }
danilchapda161d72016-08-19 07:29:46 -0700373 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000374
danilchap8bab7962016-12-20 02:46:46 -0800375 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700376 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
377 main_ssrc_, packet_type_counter_);
378 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000379
danilchap8bab7962016-12-20 02:46:46 -0800380 int64_t now_ms = clock_->TimeInMilliseconds();
381 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700382 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800383 last_skipped_packets_warning_ms_ = now_ms;
danilchapda161d72016-08-19 07:29:46 -0700384 LOG(LS_WARNING) << num_skipped_packets_
385 << " RTCP blocks were skipped due to being malformed or of "
386 "unrecognized/unsupported type, during the past "
387 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
388 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200389
danilchap1b1863a2016-09-20 01:39:54 -0700390 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000391}
392
danilchap92ea6012016-09-23 10:36:03 -0700393void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
394 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700395 rtcp::SenderReport sender_report;
396 if (!sender_report.Parse(rtcp_block)) {
397 ++num_skipped_packets_;
398 return;
399 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
danilchap8bab7962016-12-20 02:46:46 -0800401 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
danilchap8bab7962016-12-20 02:46:46 -0800403 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
danilchapec067e92017-02-21 05:38:19 -0800405 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700406
Danil Chapovalov91511f12016-09-15 16:24:02 +0200407 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800408 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700409
Danil Chapovalov91511f12016-09-15 16:24:02 +0200410 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800411 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200412 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700413 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700414
Danil Chapovalov91511f12016-09-15 16:24:02 +0200415 // Save the NTP time of this report.
danilchap8bab7962016-12-20 02:46:46 -0800416 remote_sender_info_.NTPseconds = sender_report.ntp().seconds();
417 remote_sender_info_.NTPfraction = sender_report.ntp().fractions();
418 remote_sender_info_.RTPtimeStamp = sender_report.rtp_timestamp();
419 remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
420 remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
danilchapda161d72016-08-19 07:29:46 -0700421
danilchap37953762017-02-09 11:15:25 -0800422 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700423 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200424 // We will only store the send report from one source, but
425 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700426 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700427 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000428
danilchap1b1863a2016-09-20 01:39:54 -0700429 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800430 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000431}
432
danilchap92ea6012016-09-23 10:36:03 -0700433void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
434 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700435 rtcp::ReceiverReport receiver_report;
436 if (!receiver_report.Parse(rtcp_block)) {
437 ++num_skipped_packets_;
438 return;
439 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200440
stefanb33eed22017-02-02 03:57:02 -0800441 last_received_rr_ms_ = clock_->TimeInMilliseconds();
442
danilchap8bab7962016-12-20 02:46:46 -0800443 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200444
danilchap8bab7962016-12-20 02:46:46 -0800445 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200446
danilchapec067e92017-02-21 05:38:19 -0800447 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200448
449 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800450 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200451
danilchap92ea6012016-09-23 10:36:03 -0700452 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200453
danilchap1b1863a2016-09-20 01:39:54 -0700454 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800455 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200456}
457
danilchap92ea6012016-09-23 10:36:03 -0700458void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
459 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700460 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000461 // This will be called once per report block in the RTCP packet.
462 // We filter out all report blocks that are not for us.
463 // Each packet has max 31 RR blocks.
464 //
465 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
danilchap28b03eb2016-10-05 06:59:44 -0700467 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000468 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000470 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700471 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000472 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000473
danilchap28b03eb2016-10-05 06:59:44 -0700474 ReportBlockWithRtt* report_block_info =
475 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
danilchap28b03eb2016-10-05 06:59:44 -0700476 report_block_info->report_block.remoteSSRC = remote_ssrc;
477 report_block_info->report_block.sourceSSRC = report_block.source_ssrc();
478 report_block_info->report_block.fractionLost = report_block.fraction_lost();
479 report_block_info->report_block.cumulativeLost =
danilchap1b1863a2016-09-20 01:39:54 -0700480 report_block.cumulative_lost();
481 if (report_block.extended_high_seq_num() >
danilchap28b03eb2016-10-05 06:59:44 -0700482 report_block_info->report_block.extendedHighSeqNum) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000483 // We have successfully delivered new RTP packets to the remote side after
484 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800485 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000486 }
danilchap28b03eb2016-10-05 06:59:44 -0700487 report_block_info->report_block.extendedHighSeqNum =
danilchap1b1863a2016-09-20 01:39:54 -0700488 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700489 report_block_info->report_block.jitter = report_block.jitter();
490 report_block_info->report_block.delaySinceLastSR =
danilchap1b1863a2016-09-20 01:39:54 -0700491 report_block.delay_since_last_sr();
danilchap28b03eb2016-10-05 06:59:44 -0700492 report_block_info->report_block.lastSR = report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000493
danilchap28b03eb2016-10-05 06:59:44 -0700494 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800495 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100496 // RFC3550, section 6.4.1, LSR field discription states:
497 // If no SR has been received yet, the field is set to zero.
498 // Receiver rtp_rtcp module is not expected to calculate rtt using
499 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800500 if (!receiver_only_ && send_time_ntp != 0) {
501 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100502 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800503 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000504
Danil Chapovalova094fd12016-02-22 18:59:36 +0100505 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800506 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100507 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700508 rtt_ms = CompactNtpRttToMs(rtt_ntp);
509 if (rtt_ms > report_block_info->max_rtt_ms)
510 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511
danilchap28b03eb2016-10-05 06:59:44 -0700512 if (report_block_info->num_rtts == 0 ||
513 rtt_ms < report_block_info->min_rtt_ms)
514 report_block_info->min_rtt_ms = rtt_ms;
515
516 report_block_info->last_rtt_ms = rtt_ms;
517 report_block_info->sum_rtt_ms += rtt_ms;
518 ++report_block_info->num_rtts;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000519 }
520
danilchap1b1863a2016-09-20 01:39:54 -0700521 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700522 report_block.source_ssrc(), rtt_ms);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000523
danilchap28b03eb2016-10-05 06:59:44 -0700524 packet_information->rtt_ms = rtt_ms;
525 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000526}
527
danilchapec067e92017-02-21 05:38:19 -0800528RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
529 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700530 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800531 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700532 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800533 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800534 return tmmbr_info;
535}
536
537void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
538 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
539 if (tmmbr_it != tmmbr_infos_.end())
540 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000541}
542
danilchap9bf610e2017-02-20 06:03:01 -0800543RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700544 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800545 auto it = tmmbr_infos_.find(remote_ssrc);
546 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700547 return nullptr;
548 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000549}
550
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000551bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800552 rtc::CritScope lock(&rtcp_receiver_lock_);
553 if (last_received_rr_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000554 return false;
555
556 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800557 if (clock_->TimeInMilliseconds() > last_received_rr_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_received_rr_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000560 return true;
561 }
562 return false;
563}
564
565bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800566 rtc::CritScope lock(&rtcp_receiver_lock_);
567 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000568 return false;
569
570 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800571 if (clock_->TimeInMilliseconds() >
572 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000573 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800574 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000575 return true;
576 }
577 return false;
578}
579
danilchap9bf610e2017-02-20 06:03:01 -0800580bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800581 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000582
danilchap8bab7962016-12-20 02:46:46 -0800583 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700584 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -0800585 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000586
danilchap9bf610e2017-02-20 06:03:01 -0800587 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800588 return false;
589
590 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800591 oldest_tmmbr_info_ms_ = -1;
592 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
593 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
594 if (tmmbr_info->last_time_received_ms > 0) {
595 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700596 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800597 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700598 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800599 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700600 // Send new TMMBN to all channels using the default codec.
601 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800602 } else if (oldest_tmmbr_info_ms_ == -1 ||
603 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
604 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000605 }
danilchap9bf610e2017-02-20 06:03:01 -0800606 ++tmmbr_it;
607 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700608 // When we dont have a last_time_received_ms and the object is marked
609 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800610 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000611 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800612 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000613 }
614 }
danilchap7851bda2016-09-29 15:28:07 -0700615 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000616}
617
danilchap2b616392016-08-18 06:17:42 -0700618std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800619 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800620 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
621 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700622 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700623
danilchap9bf610e2017-02-20 06:03:01 -0800624 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
625 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000626}
627
danilchap8bab7962016-12-20 02:46:46 -0800628void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700629 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700630 rtcp::Sdes sdes;
631 if (!sdes.Parse(rtcp_block)) {
632 ++num_skipped_packets_;
633 return;
634 }
635
636 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700637 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200638 {
spranga790d832016-12-02 07:29:44 -0800639 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700640 if (stats_callback_)
641 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200642 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000643 }
danilchap92ea6012016-09-23 10:36:03 -0700644 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000645}
646
danilchap8bab7962016-12-20 02:46:46 -0800647void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700648 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700649 rtcp::Nack nack;
650 if (!nack.Parse(rtcp_block)) {
651 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000652 return;
653 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000654
danilchap1b1863a2016-09-20 01:39:54 -0700655 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
656 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200657
danilchap142f0192016-10-20 08:22:42 -0700658 packet_information->nack_sequence_numbers.insert(
659 packet_information->nack_sequence_numbers.end(),
660 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700661 for (uint16_t packet_id : nack.packet_ids())
662 nack_stats_.ReportRequest(packet_id);
663
664 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700665 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000666 ++packet_type_counter_.nack_packets;
667 packet_type_counter_.nack_requests = nack_stats_.requests();
668 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
669 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000670}
671
danilchap8bab7962016-12-20 02:46:46 -0800672void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700673 rtcp::Bye bye;
674 if (!bye.Parse(rtcp_block)) {
675 ++num_skipped_packets_;
676 return;
677 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000678
danilchap8bab7962016-12-20 02:46:46 -0800679 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700680 for (auto& reports_per_receiver : received_report_blocks_)
681 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000682
danilchap9bf610e2017-02-20 06:03:01 -0800683 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
684 if (tmmbr_info)
685 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000686
danilchapefa966b2017-02-17 06:23:15 -0800687 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700688 received_cnames_.erase(bye.sender_ssrc());
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000689 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000690}
691
danilchap1b1863a2016-09-20 01:39:54 -0700692void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700693 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700694 rtcp::ExtendedReports xr;
695 if (!xr.Parse(rtcp_block)) {
696 ++num_skipped_packets_;
697 return;
698 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000699
danilchap80ac24d2016-10-31 08:40:47 -0700700 if (xr.rrtr())
701 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000702
danilchap80ac24d2016-10-31 08:40:47 -0700703 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
704 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800705
706 if (xr.target_bitrate())
707 HandleXrTargetBitrate(*xr.target_bitrate(), packet_information);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000708}
709
danilchap8bab7962016-12-20 02:46:46 -0800710void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
711 const rtcp::Rrtr& rrtr) {
danilchap798896a2016-09-28 02:54:25 -0700712 remote_time_info_.ssrc = sender_ssrc;
713 remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
danilchap37953762017-02-09 11:15:25 -0800714 last_received_xr_ntp_ = clock_->CurrentNtpTime();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000715}
716
danilchap92ea6012016-09-23 10:36:03 -0700717void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700718 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
719 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000720
danilchap1b1863a2016-09-20 01:39:54 -0700721 // Caller should explicitly enable rtt calculation using extended reports.
722 if (!xr_rrtr_status_)
723 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200724
danilchap1b1863a2016-09-20 01:39:54 -0700725 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800726 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700727 // RFC3611, section 4.5, LRR field discription states:
728 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800729 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700730 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200731
danilchap8bab7962016-12-20 02:46:46 -0800732 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800733 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200734
danilchap8bab7962016-12-20 02:46:46 -0800735 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700736 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000737}
738
spranga790d832016-12-02 07:29:44 -0800739void RTCPReceiver::HandleXrTargetBitrate(
740 const rtcp::TargetBitrate& target_bitrate,
741 PacketInformation* packet_information) {
742 BitrateAllocation bitrate_allocation;
743 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800744 if (item.spatial_layer >= kMaxSpatialLayers ||
745 item.temporal_layer >= kMaxTemporalStreams) {
746 LOG(LS_WARNING)
747 << "Invalid layer in XR target bitrate pack: spatial index "
748 << item.spatial_layer << ", temporal index " << item.temporal_layer
749 << ", dropping.";
750 } else {
751 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
752 item.target_bitrate_kbps * 1000);
753 }
spranga790d832016-12-02 07:29:44 -0800754 }
755 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
756}
757
danilchap8bab7962016-12-20 02:46:46 -0800758void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700759 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700760 rtcp::Pli pli;
761 if (!pli.Parse(rtcp_block)) {
762 ++num_skipped_packets_;
763 return;
764 }
765
766 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000767 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000768
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000769 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000770 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700771 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000772 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000773}
774
danilchap8bab7962016-12-20 02:46:46 -0800775void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700776 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700777 rtcp::Tmmbr tmmbr;
778 if (!tmmbr.Parse(rtcp_block)) {
779 ++num_skipped_packets_;
780 return;
781 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000782
danilchap7851bda2016-09-29 15:28:07 -0700783 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700784 if (tmmbr.media_ssrc()) {
785 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
786 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700787 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000788 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
danilchap1b1863a2016-09-20 01:39:54 -0700790 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800791 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
792 continue;
danilchap7851bda2016-09-29 15:28:07 -0700793
danilchapec067e92017-02-21 05:38:19 -0800794 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
795 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
796 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
797 request.bitrate_bps(),
798 request.packet_overhead());
799 entry->last_updated_ms = clock_->TimeInMilliseconds();
800
801 packet_information->packet_type_flags |= kRtcpTmmbr;
802 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000803 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
danilchap8bab7962016-12-20 02:46:46 -0800806void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700807 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700808 rtcp::Tmmbn tmmbn;
809 if (!tmmbn.Parse(rtcp_block)) {
810 ++num_skipped_packets_;
811 return;
812 }
813
danilchapec067e92017-02-21 05:38:19 -0800814 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700815
danilchap92ea6012016-09-23 10:36:03 -0700816 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000817
danilchapec067e92017-02-21 05:38:19 -0800818 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700819}
820
danilchap8bab7962016-12-20 02:46:46 -0800821void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
822 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700823 rtcp::RapidResyncRequest sr_req;
824 if (!sr_req.Parse(rtcp_block)) {
825 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000826 return;
827 }
828
danilchap92ea6012016-09-23 10:36:03 -0700829 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000830}
831
danilchap1b1863a2016-09-20 01:39:54 -0700832void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700833 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700834 rtcp::Remb remb;
835 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700836 packet_information->packet_type_flags |= kRtcpRemb;
837 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700838 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000839 }
danilchap1b1863a2016-09-20 01:39:54 -0700840
841 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000842}
843
danilchap8bab7962016-12-20 02:46:46 -0800844void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700845 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700846 rtcp::Fir fir;
847 if (!fir.Parse(rtcp_block)) {
848 ++num_skipped_packets_;
849 return;
850 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000851
danilchap1b1863a2016-09-20 01:39:54 -0700852 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800853 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700854 if (main_ssrc_ != fir_request.ssrc)
855 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200856
857 ++packet_type_counter_.fir_packets;
858
danilchapefa966b2017-02-17 06:23:15 -0800859 int64_t now_ms = clock_->TimeInMilliseconds();
860 auto inserted = last_fir_.insert(std::make_pair(
861 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
862 if (!inserted.second) { // There was already an entry.
863 LastFirStatus* last_fir = &inserted.first->second;
864
danilchap7851bda2016-09-29 15:28:07 -0700865 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800866 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700867 continue;
868
danilchap7851bda2016-09-29 15:28:07 -0700869 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800870 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700871 continue;
872
danilchapefa966b2017-02-17 06:23:15 -0800873 last_fir->request_ms = now_ms;
874 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200875 }
danilchap7851bda2016-09-29 15:28:07 -0700876 // Received signal that we need to send a new key frame.
877 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000878 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000879}
880
Erik Språng6b8d3552015-09-24 15:06:57 +0200881void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700882 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700883 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700884 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
885 new rtcp::TransportFeedback());
886 if (!transport_feedback->Parse(rtcp_block)) {
887 ++num_skipped_packets_;
888 return;
889 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200890
danilchap92ea6012016-09-23 10:36:03 -0700891 packet_information->packet_type_flags |= kRtcpTransportFeedback;
892 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200893}
danilchap287e5482016-08-16 15:15:39 -0700894
danilchap9bf610e2017-02-20 06:03:01 -0800895void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700896 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700897 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700898 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000899
spranga790d832016-12-02 07:29:44 -0800900 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700901 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700902 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
903 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800904 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000905 }
danilchap853ecb22016-08-22 08:26:15 -0700906
danilchap9bf610e2017-02-20 06:03:01 -0800907 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800908 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000909}
910
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000911void RTCPReceiver::RegisterRtcpStatisticsCallback(
912 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800913 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000914 stats_callback_ = callback;
915}
916
917RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800918 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000919 return stats_callback_;
920}
921
danilchap8bab7962016-12-20 02:46:46 -0800922// Holding no Critical section.
923void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700924 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000925 // Process TMMBR and REMB first to avoid multiple callbacks
926 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700927 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000928 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800929 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000930 }
sprang7dc39f32015-10-13 09:17:48 -0700931 uint32_t local_ssrc;
932 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000933 {
934 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800935 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000936 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700937 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000938 }
danilchap92ea6012016-09-23 10:36:03 -0700939 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800940 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000941 }
danilchap92ea6012016-09-23 10:36:03 -0700942 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
943 if (!packet_information.nack_sequence_numbers.empty()) {
andresp@webrtc.org4436b442014-06-04 09:05:30 +0000944 LOG(LS_VERBOSE) << "Incoming NACK length: "
danilchap92ea6012016-09-23 10:36:03 -0700945 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800946 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000947 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000948 }
spranga790d832016-12-02 07:29:44 -0800949
950 // We need feedback that we have received a report block(s) so that we
951 // can generate a new packet in a conference relay scenario, one received
952 // report can generate several RTCP packets, based on number relayed/mixed
953 // a send report block should go out to all receivers.
954 if (rtcp_intra_frame_observer_) {
955 RTC_DCHECK(!receiver_only_);
956 if ((packet_information.packet_type_flags & kRtcpPli) ||
957 (packet_information.packet_type_flags & kRtcpFir)) {
958 if (packet_information.packet_type_flags & kRtcpPli) {
959 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
960 << packet_information.remote_ssrc;
961 } else {
962 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
963 << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000964 }
spranga790d832016-12-02 07:29:44 -0800965 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000966 }
spranga790d832016-12-02 07:29:44 -0800967 }
968 if (rtcp_bandwidth_observer_) {
969 RTC_DCHECK(!receiver_only_);
970 if (packet_information.packet_type_flags & kRtcpRemb) {
971 LOG(LS_VERBOSE) << "Incoming REMB: "
972 << packet_information.receiver_estimated_max_bitrate_bps;
973 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
974 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +0000975 }
danilchap92ea6012016-09-23 10:36:03 -0700976 if ((packet_information.packet_type_flags & kRtcpSr) ||
977 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800978 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -0800979 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -0800980 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -0700981 }
spranga790d832016-12-02 07:29:44 -0800982 }
983 if ((packet_information.packet_type_flags & kRtcpSr) ||
984 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -0800985 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -0800986 }
isheriff6b4b5f32016-06-08 00:24:21 -0700987
spranga790d832016-12-02 07:29:44 -0800988 if (transport_feedback_observer_ &&
989 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
990 uint32_t media_source_ssrc =
991 packet_information.transport_feedback->media_ssrc();
992 if (media_source_ssrc == local_ssrc ||
993 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
994 transport_feedback_observer_->OnTransportFeedback(
995 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200996 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000997 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000998
spranga790d832016-12-02 07:29:44 -0800999 if (bitrate_allocation_observer_ &&
1000 packet_information.target_bitrate_allocation) {
1001 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1002 *packet_information.target_bitrate_allocation);
1003 }
1004
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001005 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001006 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001007 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001008 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001009 RtcpStatistics stats;
danilchap92ea6012016-09-23 10:36:03 -07001010 stats.cumulative_lost = report_block.cumulativeLost;
1011 stats.extended_max_sequence_number = report_block.extendedHighSeqNum;
1012 stats.fraction_lost = report_block.fractionLost;
1013 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001014
danilchap92ea6012016-09-23 10:36:03 -07001015 stats_callback_->StatisticsUpdated(stats, report_block.sourceSSRC);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001016 }
1017 }
1018 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001019}
1020
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001021int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001022 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001023 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001024
danilchap8bab7962016-12-20 02:46:46 -08001025 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001026 auto received_cname_it = received_cnames_.find(remoteSSRC);
1027 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001028 return -1;
danilchap95321242016-09-27 07:05:32 -07001029
1030 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1031 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001032 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001033}
1034
danilchap7851bda2016-09-29 15:28:07 -07001035std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001036 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001037 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001038
danilchap8bab7962016-12-20 02:46:46 -08001039 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -07001040 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -08001041 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
danilchap287e5482016-08-16 15:15:39 -07001042
danilchap9bf610e2017-02-20 06:03:01 -08001043 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001044 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001045 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001046 // Erase timeout entries.
1047 it = kv.second.tmmbr.erase(it);
1048 } else {
1049 candidates.push_back(it->second.tmmbr_item);
1050 ++it;
1051 }
1052 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001053 }
danilchap287e5482016-08-16 15:15:39 -07001054 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001055}
1056
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001057} // namespace webrtc