blob: 618c53303052b2e9d44f67f9fb13dd1d7127c211 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Peter Boströmfe7a80c2015-04-23 17:53:17 +020013#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
danilchap2f69ce92016-08-16 03:21:38 -070015#include <limits>
danilchap7851bda2016-09-29 15:28:07 -070016#include <map>
danilchap92ea6012016-09-23 10:36:03 -070017#include <memory>
18#include <utility>
danilchap7851bda2016-09-29 15:28:07 -070019#include <vector>
danilchap2f69ce92016-08-16 03:21:38 -070020
Erik Språngeeaa8f92018-05-17 12:35:56 +020021#include "api/video/video_bitrate_allocation.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020022#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_video/include/video_bitrate_allocator.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/common_header.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
35#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
39#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
40#include "modules/rtp_rtcp/source/time_util.h"
41#include "modules/rtp_rtcp/source/tmmbr_help.h"
42#include "rtc_base/checks.h"
43#include "rtc_base/logging.h"
44#include "rtc_base/trace_event.h"
45#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000046
niklase@google.com470e71d2011-07-07 08:21:25 +000047namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070048namespace {
49
50using rtcp::CommonHeader;
51using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000053// The number of RTCP time intervals needed to trigger a timeout.
54const int kRrTimeoutIntervals = 3;
55
Jiawei Ou3587b832018-01-31 22:08:26 -080056const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
57
Erik Språng6b8d3552015-09-24 15:06:57 +020058const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080059const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020060
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020061// Maximum number of received RRTRs that will be stored.
62const size_t kMaxNumberOfStoredRrtrs = 200;
63
danilchap1b1863a2016-09-20 01:39:54 -070064} // namespace
65
danilchap92ea6012016-09-23 10:36:03 -070066struct RTCPReceiver::PacketInformation {
67 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
68
69 uint32_t remote_ssrc = 0;
70 std::vector<uint16_t> nack_sequence_numbers;
71 ReportBlockList report_blocks;
72 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070073 uint32_t receiver_estimated_max_bitrate_bps = 0;
74 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Erik Språng566124a2018-04-23 12:32:22 +020075 rtc::Optional<VideoBitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070076};
77
danilchap9bf610e2017-02-20 06:03:01 -080078// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
79struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070080 struct TimedTmmbrItem {
81 rtcp::TmmbItem tmmbr_item;
82 int64_t last_updated_ms;
83 };
84
85 int64_t last_time_received_ms = 0;
86
danilchap7851bda2016-09-29 15:28:07 -070087 bool ready_for_delete = false;
88
89 std::vector<rtcp::TmmbItem> tmmbn;
90 std::map<uint32_t, TimedTmmbrItem> tmmbr;
91};
92
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020093// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
94struct RTCPReceiver::RrtrInformation {
95 RrtrInformation(uint32_t ssrc,
96 uint32_t received_remote_mid_ntp_time,
97 uint32_t local_receive_mid_ntp_time)
98 : ssrc(ssrc),
99 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
100 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
101
102 uint32_t ssrc;
103 // Received NTP timestamp in compact representation.
104 uint32_t received_remote_mid_ntp_time;
105 // NTP time when the report was received in compact representation.
106 uint32_t local_receive_mid_ntp_time;
107};
108
danilchap28b03eb2016-10-05 06:59:44 -0700109struct RTCPReceiver::ReportBlockWithRtt {
110 RTCPReportBlock report_block;
111
112 int64_t last_rtt_ms = 0;
113 int64_t min_rtt_ms = 0;
114 int64_t max_rtt_ms = 0;
115 int64_t sum_rtt_ms = 0;
116 size_t num_rtts = 0;
117};
118
danilchapefa966b2017-02-17 06:23:15 -0800119struct RTCPReceiver::LastFirStatus {
120 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
121 : request_ms(now_ms), sequence_number(sequence_number) {}
122 int64_t request_ms;
123 uint8_t sequence_number;
124};
125
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000126RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000127 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200128 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000129 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000130 RtcpBandwidthObserver* rtcp_bandwidth_observer,
131 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200132 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800133 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700134 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800135 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200136 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800137 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800138 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
139 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
140 transport_feedback_observer_(transport_feedback_observer),
141 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000142 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800143 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700144 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100145 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000146 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700147 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200148 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800149 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800150 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200151 packet_type_counter_observer_(packet_type_counter_observer),
152 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800153 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
154 RTC_DCHECK(owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
danilchap28b03eb2016-10-05 06:59:44 -0700157RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
nisse479d3d72017-09-13 07:53:37 -0700159void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700160 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100161 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700162 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700163 }
danilchap59cb2bd2016-08-29 11:08:47 -0700164
danilchap92ea6012016-09-23 10:36:03 -0700165 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700166 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700167 return;
danilchap8bab7962016-12-20 02:46:46 -0800168 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200171int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800172 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200173 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000174}
175
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000176void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800177 rtc::CritScope lock(&rtcp_receiver_lock_);
178 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700179 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800180 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181}
182
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000183uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800184 rtc::CritScope lock(&rtcp_receiver_lock_);
185 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000186}
187
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000188void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
189 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800190 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700191 main_ssrc_ = main_ssrc;
192 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
danilchap28b03eb2016-10-05 06:59:44 -0700195int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
196 int64_t* last_rtt_ms,
197 int64_t* avg_rtt_ms,
198 int64_t* min_rtt_ms,
199 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800200 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
danilchap28b03eb2016-10-05 06:59:44 -0700202 auto it = received_report_blocks_.find(main_ssrc_);
203 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000204 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700205
206 auto it_info = it->second.find(remote_ssrc);
207 if (it_info == it->second.end())
208 return -1;
209
210 const ReportBlockWithRtt* report_block = &it_info->second;
211
212 if (report_block->num_rtts == 0)
213 return -1;
214
215 if (last_rtt_ms)
216 *last_rtt_ms = report_block->last_rtt_ms;
217
218 if (avg_rtt_ms)
219 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
220
221 if (min_rtt_ms)
222 *min_rtt_ms = report_block->min_rtt_ms;
223
224 if (max_rtt_ms)
225 *max_rtt_ms = report_block->max_rtt_ms;
226
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000227 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100230void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800231 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100232 xr_rrtr_status_ = enable;
233}
234
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000235bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800236 RTC_DCHECK(rtt_ms);
237 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000238 if (xr_rr_rtt_ms_ == 0) {
239 return false;
240 }
241 *rtt_ms = xr_rr_rtt_ms_;
242 xr_rr_rtt_ms_ = 0;
243 return true;
244}
245
danilchap8bab7962016-12-20 02:46:46 -0800246bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
247 uint32_t* received_ntp_frac,
248 uint32_t* rtcp_arrival_time_secs,
249 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700250 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800251 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700252 if (!last_received_sr_ntp_.Valid())
253 return false;
254
255 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800256 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700257 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800258 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700259 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700260
261 // Rtp time from incoming SenderReport.
262 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700263 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700264
265 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800266 if (rtcp_arrival_time_secs)
267 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
268 if (rtcp_arrival_time_frac)
269 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700270
danilchapda161d72016-08-19 07:29:46 -0700271 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200274std::vector<rtcp::ReceiveTimeInfo>
275RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800276 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000277
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200278 const size_t last_xr_rtis_size = std::min(
279 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
280 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
281 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000282
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200283 const uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000284
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200285 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
286 RrtrInformation& rrtr = received_rrtrs_.front();
287 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
288 now_ntp - rrtr.local_receive_mid_ntp_time);
289 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
290 received_rrtrs_.pop_front();
291 }
292
293 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000294}
295
danilchap28b03eb2016-10-05 06:59:44 -0700296// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700298 std::vector<RTCPReportBlock>* receive_blocks) const {
299 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800300 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700301 for (const auto& reports_per_receiver : received_report_blocks_)
302 for (const auto& report : reports_per_receiver.second)
303 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
danilchap92ea6012016-09-23 10:36:03 -0700307bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
308 const uint8_t* packet_end,
309 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800310 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000311
danilchap1b1863a2016-09-20 01:39:54 -0700312 CommonHeader rtcp_block;
313 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
314 next_block = rtcp_block.NextPacket()) {
315 ptrdiff_t remaining_blocks_size = packet_end - next_block;
316 RTC_DCHECK_GT(remaining_blocks_size, 0);
317 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
318 if (next_block == packet_begin) {
319 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100320 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700321 return false;
322 }
323 ++num_skipped_packets_;
324 break;
325 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000326
danilchap1b1863a2016-09-20 01:39:54 -0700327 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800328 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700329
danilchap1b1863a2016-09-20 01:39:54 -0700330 switch (rtcp_block.type()) {
331 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700332 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200333 break;
danilchap1b1863a2016-09-20 01:39:54 -0700334 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700335 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700336 break;
danilchap1b1863a2016-09-20 01:39:54 -0700337 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800338 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700339 break;
danilchap1b1863a2016-09-20 01:39:54 -0700340 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700341 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700342 break;
danilchap1b1863a2016-09-20 01:39:54 -0700343 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800344 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700345 break;
danilchap1b1863a2016-09-20 01:39:54 -0700346 case rtcp::Rtpfb::kPacketType:
347 switch (rtcp_block.fmt()) {
348 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800349 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700350 break;
351 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800352 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700353 break;
354 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800355 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700356 break;
357 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800358 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700359 break;
360 case rtcp::TransportFeedback::kFeedbackMessageType:
361 HandleTransportFeedback(rtcp_block, packet_information);
362 break;
363 default:
364 ++num_skipped_packets_;
365 break;
366 }
danilchapda161d72016-08-19 07:29:46 -0700367 break;
danilchap1b1863a2016-09-20 01:39:54 -0700368 case rtcp::Psfb::kPacketType:
369 switch (rtcp_block.fmt()) {
370 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800371 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700372 break;
danilchap1b1863a2016-09-20 01:39:54 -0700373 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800374 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700375 break;
376 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700377 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700378 break;
379 default:
380 ++num_skipped_packets_;
381 break;
382 }
danilchapda161d72016-08-19 07:29:46 -0700383 break;
384 default:
danilchap1b1863a2016-09-20 01:39:54 -0700385 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700386 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000387 }
danilchapda161d72016-08-19 07:29:46 -0700388 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000389
danilchap8bab7962016-12-20 02:46:46 -0800390 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700391 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
392 main_ssrc_, packet_type_counter_);
393 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000394
danilchap8bab7962016-12-20 02:46:46 -0800395 int64_t now_ms = clock_->TimeInMilliseconds();
396 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700397 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800398 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100399 RTC_LOG(LS_WARNING)
400 << num_skipped_packets_
401 << " RTCP blocks were skipped due to being malformed or of "
402 "unrecognized/unsupported type, during the past "
403 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700404 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200405
danilchap1b1863a2016-09-20 01:39:54 -0700406 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000407}
408
danilchap92ea6012016-09-23 10:36:03 -0700409void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
410 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700411 rtcp::SenderReport sender_report;
412 if (!sender_report.Parse(rtcp_block)) {
413 ++num_skipped_packets_;
414 return;
415 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000416
danilchap8bab7962016-12-20 02:46:46 -0800417 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
danilchap8bab7962016-12-20 02:46:46 -0800419 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
danilchapec067e92017-02-21 05:38:19 -0800421 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700422
Danil Chapovalov91511f12016-09-15 16:24:02 +0200423 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800424 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200425 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700426 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700427
danilchapa04d9c32017-07-25 04:03:39 -0700428 remote_sender_ntp_time_ = sender_report.ntp();
429 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
danilchap37953762017-02-09 11:15:25 -0800430 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700431 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200432 // We will only store the send report from one source, but
433 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700434 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700435 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000436
danilchap1b1863a2016-09-20 01:39:54 -0700437 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800438 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
danilchap92ea6012016-09-23 10:36:03 -0700441void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
442 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700443 rtcp::ReceiverReport receiver_report;
444 if (!receiver_report.Parse(rtcp_block)) {
445 ++num_skipped_packets_;
446 return;
447 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200448
danilchap8bab7962016-12-20 02:46:46 -0800449 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200450
danilchap8bab7962016-12-20 02:46:46 -0800451 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200452
danilchapec067e92017-02-21 05:38:19 -0800453 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200454
danilchap92ea6012016-09-23 10:36:03 -0700455 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200456
danilchap1b1863a2016-09-20 01:39:54 -0700457 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800458 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200459}
460
danilchap92ea6012016-09-23 10:36:03 -0700461void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
462 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700463 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000464 // This will be called once per report block in the RTCP packet.
465 // We filter out all report blocks that are not for us.
466 // Each packet has max 31 RR blocks.
467 //
468 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
danilchap28b03eb2016-10-05 06:59:44 -0700470 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000471 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000473 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700474 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000475 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000476
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200477 last_received_rb_ms_ = clock_->TimeInMilliseconds();
478
danilchap28b03eb2016-10-05 06:59:44 -0700479 ReportBlockWithRtt* report_block_info =
480 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700481 report_block_info->report_block.sender_ssrc = remote_ssrc;
482 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
483 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
Harald Alvestrand70206d62017-12-08 08:59:07 +0100484 report_block_info->report_block.packets_lost =
485 report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700486 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700487 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000488 // We have successfully delivered new RTP packets to the remote side after
489 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800490 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000491 }
srte3e69e5c2017-08-09 06:13:45 -0700492 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700493 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700494 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700495 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700496 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700497 report_block_info->report_block.last_sender_report_timestamp =
498 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000499
danilchap28b03eb2016-10-05 06:59:44 -0700500 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800501 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100502 // RFC3550, section 6.4.1, LSR field discription states:
503 // If no SR has been received yet, the field is set to zero.
504 // Receiver rtp_rtcp module is not expected to calculate rtt using
505 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800506 if (!receiver_only_ && send_time_ntp != 0) {
507 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100508 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800509 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000510
Danil Chapovalova094fd12016-02-22 18:59:36 +0100511 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800512 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100513 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700514 rtt_ms = CompactNtpRttToMs(rtt_ntp);
515 if (rtt_ms > report_block_info->max_rtt_ms)
516 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000517
danilchap28b03eb2016-10-05 06:59:44 -0700518 if (report_block_info->num_rtts == 0 ||
519 rtt_ms < report_block_info->min_rtt_ms)
520 report_block_info->min_rtt_ms = rtt_ms;
521
522 report_block_info->last_rtt_ms = rtt_ms;
523 report_block_info->sum_rtt_ms += rtt_ms;
524 ++report_block_info->num_rtts;
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100525
526 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000527 }
528
danilchap28b03eb2016-10-05 06:59:44 -0700529 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000530}
531
danilchapec067e92017-02-21 05:38:19 -0800532RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
533 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700534 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800535 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700536 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800537 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800538 return tmmbr_info;
539}
540
541void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
542 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
543 if (tmmbr_it != tmmbr_infos_.end())
544 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
danilchap9bf610e2017-02-20 06:03:01 -0800547RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700548 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800549 auto it = tmmbr_infos_.find(remote_ssrc);
550 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700551 return nullptr;
552 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000553}
554
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000555bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800556 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200557 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000558 return false;
559
560 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200561 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000562 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200563 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000564 return true;
565 }
566 return false;
567}
568
569bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800570 rtc::CritScope lock(&rtcp_receiver_lock_);
571 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000572 return false;
573
574 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800575 if (clock_->TimeInMilliseconds() >
576 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000577 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800578 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000579 return true;
580 }
581 return false;
582}
583
danilchap9bf610e2017-02-20 06:03:01 -0800584bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800585 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000586
danilchap8bab7962016-12-20 02:46:46 -0800587 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800588 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
danilchap9bf610e2017-02-20 06:03:01 -0800590 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800591 return false;
592
593 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800594 oldest_tmmbr_info_ms_ = -1;
595 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
596 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
597 if (tmmbr_info->last_time_received_ms > 0) {
598 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700599 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800600 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700601 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800602 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700603 // Send new TMMBN to all channels using the default codec.
604 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800605 } else if (oldest_tmmbr_info_ms_ == -1 ||
606 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
607 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000608 }
danilchap9bf610e2017-02-20 06:03:01 -0800609 ++tmmbr_it;
610 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700611 // When we dont have a last_time_received_ms and the object is marked
612 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800613 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000614 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800615 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000616 }
617 }
danilchap7851bda2016-09-29 15:28:07 -0700618 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000619}
620
danilchap2b616392016-08-18 06:17:42 -0700621std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800622 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800623 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
624 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700625 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700626
danilchap9bf610e2017-02-20 06:03:01 -0800627 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
628 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000629}
630
danilchap8bab7962016-12-20 02:46:46 -0800631void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700632 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700633 rtcp::Sdes sdes;
634 if (!sdes.Parse(rtcp_block)) {
635 ++num_skipped_packets_;
636 return;
637 }
638
639 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700640 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200641 {
spranga790d832016-12-02 07:29:44 -0800642 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700643 if (stats_callback_)
644 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200645 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000646 }
danilchap92ea6012016-09-23 10:36:03 -0700647 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000648}
649
danilchap8bab7962016-12-20 02:46:46 -0800650void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700651 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700652 rtcp::Nack nack;
653 if (!nack.Parse(rtcp_block)) {
654 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000655 return;
656 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000657
danilchap1b1863a2016-09-20 01:39:54 -0700658 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
659 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200660
danilchap142f0192016-10-20 08:22:42 -0700661 packet_information->nack_sequence_numbers.insert(
662 packet_information->nack_sequence_numbers.end(),
663 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700664 for (uint16_t packet_id : nack.packet_ids())
665 nack_stats_.ReportRequest(packet_id);
666
667 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700668 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000669 ++packet_type_counter_.nack_packets;
670 packet_type_counter_.nack_requests = nack_stats_.requests();
671 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
672 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000673}
674
danilchap8bab7962016-12-20 02:46:46 -0800675void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700676 rtcp::Bye bye;
677 if (!bye.Parse(rtcp_block)) {
678 ++num_skipped_packets_;
679 return;
680 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
danilchap8bab7962016-12-20 02:46:46 -0800682 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700683 for (auto& reports_per_receiver : received_report_blocks_)
684 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000685
danilchap9bf610e2017-02-20 06:03:01 -0800686 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
687 if (tmmbr_info)
688 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000689
danilchapefa966b2017-02-17 06:23:15 -0800690 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700691 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200692 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
693 if (it != received_rrtrs_ssrc_it_.end()) {
694 received_rrtrs_.erase(it->second);
695 received_rrtrs_ssrc_it_.erase(it);
696 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000697 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000698}
699
danilchap1b1863a2016-09-20 01:39:54 -0700700void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700701 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700702 rtcp::ExtendedReports xr;
703 if (!xr.Parse(rtcp_block)) {
704 ++num_skipped_packets_;
705 return;
706 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000707
danilchap80ac24d2016-10-31 08:40:47 -0700708 if (xr.rrtr())
709 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000710
danilchap80ac24d2016-10-31 08:40:47 -0700711 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
712 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800713
sprangb32aaf92017-08-28 05:49:12 -0700714 if (xr.target_bitrate()) {
715 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
716 packet_information);
717 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000718}
719
danilchap8bab7962016-12-20 02:46:46 -0800720void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
721 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200722 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
723 uint32_t local_receive_mid_ntp_time = CompactNtp(clock_->CurrentNtpTime());
724
725 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
726 if (it != received_rrtrs_ssrc_it_.end()) {
727 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
728 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
729 } else {
730 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
731 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
732 local_receive_mid_ntp_time);
733 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
734 } else {
735 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
736 << ", reached maximum number of stored RRTRs.";
737 }
738 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000739}
740
danilchap92ea6012016-09-23 10:36:03 -0700741void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700742 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
743 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000744
danilchap1b1863a2016-09-20 01:39:54 -0700745 // Caller should explicitly enable rtt calculation using extended reports.
746 if (!xr_rrtr_status_)
747 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200748
danilchap1b1863a2016-09-20 01:39:54 -0700749 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800750 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700751 // RFC3611, section 4.5, LRR field discription states:
752 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800753 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700754 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200755
danilchap8bab7962016-12-20 02:46:46 -0800756 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800757 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200758
danilchap8bab7962016-12-20 02:46:46 -0800759 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700760 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000761}
762
spranga790d832016-12-02 07:29:44 -0800763void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700764 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800765 const rtcp::TargetBitrate& target_bitrate,
766 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700767 if (ssrc != remote_ssrc_) {
768 return; // Not for us.
769 }
770
Erik Språng566124a2018-04-23 12:32:22 +0200771 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800772 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800773 if (item.spatial_layer >= kMaxSpatialLayers ||
774 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100775 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800776 << "Invalid layer in XR target bitrate pack: spatial index "
777 << item.spatial_layer << ", temporal index " << item.temporal_layer
778 << ", dropping.";
779 } else {
780 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
781 item.target_bitrate_kbps * 1000);
782 }
spranga790d832016-12-02 07:29:44 -0800783 }
784 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
785}
786
danilchap8bab7962016-12-20 02:46:46 -0800787void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700788 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700789 rtcp::Pli pli;
790 if (!pli.Parse(rtcp_block)) {
791 ++num_skipped_packets_;
792 return;
793 }
794
795 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000796 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000797 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700798 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000799 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
danilchap8bab7962016-12-20 02:46:46 -0800802void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700803 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700804 rtcp::Tmmbr tmmbr;
805 if (!tmmbr.Parse(rtcp_block)) {
806 ++num_skipped_packets_;
807 return;
808 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000809
danilchap7851bda2016-09-29 15:28:07 -0700810 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700811 if (tmmbr.media_ssrc()) {
812 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
813 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700814 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000815 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000816
danilchap1b1863a2016-09-20 01:39:54 -0700817 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800818 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
819 continue;
danilchap7851bda2016-09-29 15:28:07 -0700820
danilchapec067e92017-02-21 05:38:19 -0800821 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
822 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
823 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
824 request.bitrate_bps(),
825 request.packet_overhead());
826 entry->last_updated_ms = clock_->TimeInMilliseconds();
827
828 packet_information->packet_type_flags |= kRtcpTmmbr;
829 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000830 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000831}
832
danilchap8bab7962016-12-20 02:46:46 -0800833void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700834 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700835 rtcp::Tmmbn tmmbn;
836 if (!tmmbn.Parse(rtcp_block)) {
837 ++num_skipped_packets_;
838 return;
839 }
840
danilchapec067e92017-02-21 05:38:19 -0800841 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700842
danilchap92ea6012016-09-23 10:36:03 -0700843 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000844
danilchapec067e92017-02-21 05:38:19 -0800845 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700846}
847
danilchap8bab7962016-12-20 02:46:46 -0800848void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
849 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700850 rtcp::RapidResyncRequest sr_req;
851 if (!sr_req.Parse(rtcp_block)) {
852 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000853 return;
854 }
855
danilchap92ea6012016-09-23 10:36:03 -0700856 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000857}
858
danilchap1b1863a2016-09-20 01:39:54 -0700859void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700860 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700861 rtcp::Remb remb;
862 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700863 packet_information->packet_type_flags |= kRtcpRemb;
864 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700865 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000866 }
danilchap1b1863a2016-09-20 01:39:54 -0700867
868 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000869}
870
danilchap8bab7962016-12-20 02:46:46 -0800871void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700872 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700873 rtcp::Fir fir;
874 if (!fir.Parse(rtcp_block)) {
875 ++num_skipped_packets_;
876 return;
877 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000878
danilchap1b1863a2016-09-20 01:39:54 -0700879 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800880 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700881 if (main_ssrc_ != fir_request.ssrc)
882 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200883
884 ++packet_type_counter_.fir_packets;
885
danilchapefa966b2017-02-17 06:23:15 -0800886 int64_t now_ms = clock_->TimeInMilliseconds();
887 auto inserted = last_fir_.insert(std::make_pair(
888 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
889 if (!inserted.second) { // There was already an entry.
890 LastFirStatus* last_fir = &inserted.first->second;
891
danilchap7851bda2016-09-29 15:28:07 -0700892 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800893 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700894 continue;
895
danilchap7851bda2016-09-29 15:28:07 -0700896 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800897 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700898 continue;
899
danilchapefa966b2017-02-17 06:23:15 -0800900 last_fir->request_ms = now_ms;
901 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200902 }
danilchap7851bda2016-09-29 15:28:07 -0700903 // Received signal that we need to send a new key frame.
904 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000905 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000906}
907
Erik Språng6b8d3552015-09-24 15:06:57 +0200908void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700909 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700910 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700911 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
912 new rtcp::TransportFeedback());
913 if (!transport_feedback->Parse(rtcp_block)) {
914 ++num_skipped_packets_;
915 return;
916 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200917
danilchap92ea6012016-09-23 10:36:03 -0700918 packet_information->packet_type_flags |= kRtcpTransportFeedback;
919 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200920}
danilchap287e5482016-08-16 15:15:39 -0700921
danilchap9bf610e2017-02-20 06:03:01 -0800922void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700923 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700924 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700925 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000926
spranga790d832016-12-02 07:29:44 -0800927 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700928 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700929 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
930 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800931 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000932 }
danilchap853ecb22016-08-22 08:26:15 -0700933
danilchap9bf610e2017-02-20 06:03:01 -0800934 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800935 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000936}
937
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000938void RTCPReceiver::RegisterRtcpStatisticsCallback(
939 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800940 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000941 stats_callback_ = callback;
942}
943
944RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800945 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000946 return stats_callback_;
947}
948
danilchap8bab7962016-12-20 02:46:46 -0800949// Holding no Critical section.
950void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700951 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000952 // Process TMMBR and REMB first to avoid multiple callbacks
953 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700954 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000955 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800956 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000957 }
sprang7dc39f32015-10-13 09:17:48 -0700958 uint32_t local_ssrc;
959 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000960 {
961 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800962 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000963 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700964 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000965 }
danilchap92ea6012016-09-23 10:36:03 -0700966 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800967 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000968 }
danilchap92ea6012016-09-23 10:36:03 -0700969 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
970 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100971 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
972 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800973 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000974 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000975 }
spranga790d832016-12-02 07:29:44 -0800976
977 // We need feedback that we have received a report block(s) so that we
978 // can generate a new packet in a conference relay scenario, one received
979 // report can generate several RTCP packets, based on number relayed/mixed
980 // a send report block should go out to all receivers.
981 if (rtcp_intra_frame_observer_) {
982 RTC_DCHECK(!receiver_only_);
983 if ((packet_information.packet_type_flags & kRtcpPli) ||
984 (packet_information.packet_type_flags & kRtcpFir)) {
985 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100986 RTC_LOG(LS_VERBOSE)
987 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -0800988 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100989 RTC_LOG(LS_VERBOSE)
990 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000991 }
spranga790d832016-12-02 07:29:44 -0800992 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000993 }
spranga790d832016-12-02 07:29:44 -0800994 }
995 if (rtcp_bandwidth_observer_) {
996 RTC_DCHECK(!receiver_only_);
997 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100998 RTC_LOG(LS_VERBOSE)
999 << "Incoming REMB: "
1000 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001001 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1002 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 }
danilchap92ea6012016-09-23 10:36:03 -07001004 if ((packet_information.packet_type_flags & kRtcpSr) ||
1005 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001006 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001007 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001008 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001009 }
spranga790d832016-12-02 07:29:44 -08001010 }
1011 if ((packet_information.packet_type_flags & kRtcpSr) ||
1012 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001013 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001014 }
isheriff6b4b5f32016-06-08 00:24:21 -07001015
spranga790d832016-12-02 07:29:44 -08001016 if (transport_feedback_observer_ &&
1017 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1018 uint32_t media_source_ssrc =
1019 packet_information.transport_feedback->media_ssrc();
1020 if (media_source_ssrc == local_ssrc ||
1021 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1022 transport_feedback_observer_->OnTransportFeedback(
1023 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001024 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001025 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001026
spranga790d832016-12-02 07:29:44 -08001027 if (bitrate_allocation_observer_ &&
1028 packet_information.target_bitrate_allocation) {
1029 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1030 *packet_information.target_bitrate_allocation);
1031 }
1032
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001033 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001034 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001035 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001036 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001037 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001038 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001039 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001040 report_block.extended_highest_sequence_number;
1041 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001042 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001043
srte3e69e5c2017-08-09 06:13:45 -07001044 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001045 }
1046 }
1047 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001048}
1049
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001050int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001051 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001052 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001053
danilchap8bab7962016-12-20 02:46:46 -08001054 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001055 auto received_cname_it = received_cnames_.find(remoteSSRC);
1056 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001057 return -1;
danilchap95321242016-09-27 07:05:32 -07001058
1059 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1060 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001061 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001062}
1063
danilchap7851bda2016-09-29 15:28:07 -07001064std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001065 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001066 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067
danilchap8bab7962016-12-20 02:46:46 -08001068 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001069 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001070
danilchap9bf610e2017-02-20 06:03:01 -08001071 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001072 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001073 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001074 // Erase timeout entries.
1075 it = kv.second.tmmbr.erase(it);
1076 } else {
1077 candidates.push_back(it->second.tmmbr_item);
1078 ++it;
1079 }
1080 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001081 }
danilchap287e5482016-08-16 15:15:39 -07001082 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001083}
1084
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001085} // namespace webrtc