blob: 1b5a868a1f0048d6f2b8886892a5c98aa8b5e3f5 [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 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800424 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700425
Danil Chapovalov91511f12016-09-15 16:24:02 +0200426 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800427 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200428 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700429 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700430
danilchapa04d9c32017-07-25 04:03:39 -0700431 remote_sender_ntp_time_ = sender_report.ntp();
432 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
danilchap37953762017-02-09 11:15:25 -0800433 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700434 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200435 // We will only store the send report from one source, but
436 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700437 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700438 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000439
danilchap1b1863a2016-09-20 01:39:54 -0700440 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800441 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
danilchap92ea6012016-09-23 10:36:03 -0700444void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
445 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700446 rtcp::ReceiverReport receiver_report;
447 if (!receiver_report.Parse(rtcp_block)) {
448 ++num_skipped_packets_;
449 return;
450 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200451
danilchap8bab7962016-12-20 02:46:46 -0800452 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200453
danilchap8bab7962016-12-20 02:46:46 -0800454 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200455
danilchapec067e92017-02-21 05:38:19 -0800456 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200457
458 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800459 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200460
danilchap92ea6012016-09-23 10:36:03 -0700461 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200462
danilchap1b1863a2016-09-20 01:39:54 -0700463 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800464 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200465}
466
danilchap92ea6012016-09-23 10:36:03 -0700467void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
468 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700469 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000470 // This will be called once per report block in the RTCP packet.
471 // We filter out all report blocks that are not for us.
472 // Each packet has max 31 RR blocks.
473 //
474 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
danilchap28b03eb2016-10-05 06:59:44 -0700476 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000477 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000478
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000479 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700480 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000481 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000482
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200483 last_received_rb_ms_ = clock_->TimeInMilliseconds();
484
danilchap28b03eb2016-10-05 06:59:44 -0700485 ReportBlockWithRtt* report_block_info =
486 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700487 report_block_info->report_block.sender_ssrc = remote_ssrc;
488 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
489 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
Harald Alvestrand70206d62017-12-08 08:59:07 +0100490 report_block_info->report_block.packets_lost =
491 report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700492 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700493 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000494 // We have successfully delivered new RTP packets to the remote side after
495 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800496 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000497 }
srte3e69e5c2017-08-09 06:13:45 -0700498 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700499 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700500 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700501 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700502 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700503 report_block_info->report_block.last_sender_report_timestamp =
504 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000505
danilchap28b03eb2016-10-05 06:59:44 -0700506 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800507 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100508 // RFC3550, section 6.4.1, LSR field discription states:
509 // If no SR has been received yet, the field is set to zero.
510 // Receiver rtp_rtcp module is not expected to calculate rtt using
511 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800512 if (!receiver_only_ && send_time_ntp != 0) {
513 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100514 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800515 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000516
Danil Chapovalova094fd12016-02-22 18:59:36 +0100517 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800518 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100519 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700520 rtt_ms = CompactNtpRttToMs(rtt_ntp);
521 if (rtt_ms > report_block_info->max_rtt_ms)
522 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000523
danilchap28b03eb2016-10-05 06:59:44 -0700524 if (report_block_info->num_rtts == 0 ||
525 rtt_ms < report_block_info->min_rtt_ms)
526 report_block_info->min_rtt_ms = rtt_ms;
527
528 report_block_info->last_rtt_ms = rtt_ms;
529 report_block_info->sum_rtt_ms += rtt_ms;
530 ++report_block_info->num_rtts;
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100531
532 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000533 }
534
danilchap1b1863a2016-09-20 01:39:54 -0700535 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700536 report_block.source_ssrc(), rtt_ms);
danilchap28b03eb2016-10-05 06:59:44 -0700537 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000538}
539
danilchapec067e92017-02-21 05:38:19 -0800540RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
541 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700542 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800543 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700544 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800545 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800546 return tmmbr_info;
547}
548
549void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
550 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
551 if (tmmbr_it != tmmbr_infos_.end())
552 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000553}
554
danilchap9bf610e2017-02-20 06:03:01 -0800555RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700556 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800557 auto it = tmmbr_infos_.find(remote_ssrc);
558 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700559 return nullptr;
560 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000561}
562
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000563bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800564 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200565 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000566 return false;
567
568 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200569 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000570 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200571 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000572 return true;
573 }
574 return false;
575}
576
577bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800578 rtc::CritScope lock(&rtcp_receiver_lock_);
579 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000580 return false;
581
582 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800583 if (clock_->TimeInMilliseconds() >
584 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000585 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800586 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000587 return true;
588 }
589 return false;
590}
591
danilchap9bf610e2017-02-20 06:03:01 -0800592bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800593 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594
danilchap8bab7962016-12-20 02:46:46 -0800595 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800596 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000597
danilchap9bf610e2017-02-20 06:03:01 -0800598 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800599 return false;
600
601 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800602 oldest_tmmbr_info_ms_ = -1;
603 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
604 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
605 if (tmmbr_info->last_time_received_ms > 0) {
606 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700607 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800608 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700609 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800610 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700611 // Send new TMMBN to all channels using the default codec.
612 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800613 } else if (oldest_tmmbr_info_ms_ == -1 ||
614 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
615 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000616 }
danilchap9bf610e2017-02-20 06:03:01 -0800617 ++tmmbr_it;
618 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700619 // When we dont have a last_time_received_ms and the object is marked
620 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800621 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000622 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800623 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000624 }
625 }
danilchap7851bda2016-09-29 15:28:07 -0700626 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000627}
628
danilchap2b616392016-08-18 06:17:42 -0700629std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800630 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800631 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
632 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700633 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700634
danilchap9bf610e2017-02-20 06:03:01 -0800635 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
636 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000637}
638
danilchap8bab7962016-12-20 02:46:46 -0800639void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700640 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700641 rtcp::Sdes sdes;
642 if (!sdes.Parse(rtcp_block)) {
643 ++num_skipped_packets_;
644 return;
645 }
646
647 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700648 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200649 {
spranga790d832016-12-02 07:29:44 -0800650 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700651 if (stats_callback_)
652 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200653 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000654 }
danilchap92ea6012016-09-23 10:36:03 -0700655 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000656}
657
danilchap8bab7962016-12-20 02:46:46 -0800658void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700659 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700660 rtcp::Nack nack;
661 if (!nack.Parse(rtcp_block)) {
662 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000663 return;
664 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000665
danilchap1b1863a2016-09-20 01:39:54 -0700666 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
667 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200668
danilchap142f0192016-10-20 08:22:42 -0700669 packet_information->nack_sequence_numbers.insert(
670 packet_information->nack_sequence_numbers.end(),
671 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700672 for (uint16_t packet_id : nack.packet_ids())
673 nack_stats_.ReportRequest(packet_id);
674
675 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700676 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000677 ++packet_type_counter_.nack_packets;
678 packet_type_counter_.nack_requests = nack_stats_.requests();
679 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
680 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000681}
682
danilchap8bab7962016-12-20 02:46:46 -0800683void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700684 rtcp::Bye bye;
685 if (!bye.Parse(rtcp_block)) {
686 ++num_skipped_packets_;
687 return;
688 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000689
danilchap8bab7962016-12-20 02:46:46 -0800690 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700691 for (auto& reports_per_receiver : received_report_blocks_)
692 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000693
danilchap9bf610e2017-02-20 06:03:01 -0800694 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
695 if (tmmbr_info)
696 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000697
danilchapefa966b2017-02-17 06:23:15 -0800698 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700699 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200700 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
701 if (it != received_rrtrs_ssrc_it_.end()) {
702 received_rrtrs_.erase(it->second);
703 received_rrtrs_ssrc_it_.erase(it);
704 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000705 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000706}
707
danilchap1b1863a2016-09-20 01:39:54 -0700708void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700709 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700710 rtcp::ExtendedReports xr;
711 if (!xr.Parse(rtcp_block)) {
712 ++num_skipped_packets_;
713 return;
714 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000715
danilchap80ac24d2016-10-31 08:40:47 -0700716 if (xr.rrtr())
717 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000718
danilchap80ac24d2016-10-31 08:40:47 -0700719 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
720 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800721
sprangb32aaf92017-08-28 05:49:12 -0700722 if (xr.target_bitrate()) {
723 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
724 packet_information);
725 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000726}
727
danilchap8bab7962016-12-20 02:46:46 -0800728void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
729 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200730 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
731 uint32_t local_receive_mid_ntp_time = CompactNtp(clock_->CurrentNtpTime());
732
733 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
734 if (it != received_rrtrs_ssrc_it_.end()) {
735 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
736 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
737 } else {
738 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
739 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
740 local_receive_mid_ntp_time);
741 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
742 } else {
743 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
744 << ", reached maximum number of stored RRTRs.";
745 }
746 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000747}
748
danilchap92ea6012016-09-23 10:36:03 -0700749void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700750 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
751 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000752
danilchap1b1863a2016-09-20 01:39:54 -0700753 // Caller should explicitly enable rtt calculation using extended reports.
754 if (!xr_rrtr_status_)
755 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200756
danilchap1b1863a2016-09-20 01:39:54 -0700757 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800758 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700759 // RFC3611, section 4.5, LRR field discription states:
760 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800761 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700762 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200763
danilchap8bab7962016-12-20 02:46:46 -0800764 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800765 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200766
danilchap8bab7962016-12-20 02:46:46 -0800767 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700768 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000769}
770
spranga790d832016-12-02 07:29:44 -0800771void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700772 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800773 const rtcp::TargetBitrate& target_bitrate,
774 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700775 if (ssrc != remote_ssrc_) {
776 return; // Not for us.
777 }
778
Erik Språng566124a2018-04-23 12:32:22 +0200779 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800780 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800781 if (item.spatial_layer >= kMaxSpatialLayers ||
782 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100783 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800784 << "Invalid layer in XR target bitrate pack: spatial index "
785 << item.spatial_layer << ", temporal index " << item.temporal_layer
786 << ", dropping.";
787 } else {
788 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
789 item.target_bitrate_kbps * 1000);
790 }
spranga790d832016-12-02 07:29:44 -0800791 }
792 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
793}
794
danilchap8bab7962016-12-20 02:46:46 -0800795void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700796 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700797 rtcp::Pli pli;
798 if (!pli.Parse(rtcp_block)) {
799 ++num_skipped_packets_;
800 return;
801 }
802
803 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000804 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000805
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000806 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000807 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700808 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000809 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
danilchap8bab7962016-12-20 02:46:46 -0800812void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700813 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700814 rtcp::Tmmbr tmmbr;
815 if (!tmmbr.Parse(rtcp_block)) {
816 ++num_skipped_packets_;
817 return;
818 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000819
danilchap7851bda2016-09-29 15:28:07 -0700820 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700821 if (tmmbr.media_ssrc()) {
822 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
823 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700824 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000825 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000826
danilchap1b1863a2016-09-20 01:39:54 -0700827 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800828 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
829 continue;
danilchap7851bda2016-09-29 15:28:07 -0700830
danilchapec067e92017-02-21 05:38:19 -0800831 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
832 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
833 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
834 request.bitrate_bps(),
835 request.packet_overhead());
836 entry->last_updated_ms = clock_->TimeInMilliseconds();
837
838 packet_information->packet_type_flags |= kRtcpTmmbr;
839 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000840 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000841}
842
danilchap8bab7962016-12-20 02:46:46 -0800843void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700844 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700845 rtcp::Tmmbn tmmbn;
846 if (!tmmbn.Parse(rtcp_block)) {
847 ++num_skipped_packets_;
848 return;
849 }
850
danilchapec067e92017-02-21 05:38:19 -0800851 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700852
danilchap92ea6012016-09-23 10:36:03 -0700853 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000854
danilchapec067e92017-02-21 05:38:19 -0800855 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700856}
857
danilchap8bab7962016-12-20 02:46:46 -0800858void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
859 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700860 rtcp::RapidResyncRequest sr_req;
861 if (!sr_req.Parse(rtcp_block)) {
862 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000863 return;
864 }
865
danilchap92ea6012016-09-23 10:36:03 -0700866 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
868
danilchap1b1863a2016-09-20 01:39:54 -0700869void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700870 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700871 rtcp::Remb remb;
872 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700873 packet_information->packet_type_flags |= kRtcpRemb;
874 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700875 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000876 }
danilchap1b1863a2016-09-20 01:39:54 -0700877
878 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000879}
880
danilchap8bab7962016-12-20 02:46:46 -0800881void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700882 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700883 rtcp::Fir fir;
884 if (!fir.Parse(rtcp_block)) {
885 ++num_skipped_packets_;
886 return;
887 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000888
danilchap1b1863a2016-09-20 01:39:54 -0700889 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800890 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700891 if (main_ssrc_ != fir_request.ssrc)
892 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200893
894 ++packet_type_counter_.fir_packets;
895
danilchapefa966b2017-02-17 06:23:15 -0800896 int64_t now_ms = clock_->TimeInMilliseconds();
897 auto inserted = last_fir_.insert(std::make_pair(
898 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
899 if (!inserted.second) { // There was already an entry.
900 LastFirStatus* last_fir = &inserted.first->second;
901
danilchap7851bda2016-09-29 15:28:07 -0700902 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800903 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700904 continue;
905
danilchap7851bda2016-09-29 15:28:07 -0700906 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800907 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700908 continue;
909
danilchapefa966b2017-02-17 06:23:15 -0800910 last_fir->request_ms = now_ms;
911 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200912 }
danilchap7851bda2016-09-29 15:28:07 -0700913 // Received signal that we need to send a new key frame.
914 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000915 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000916}
917
Erik Språng6b8d3552015-09-24 15:06:57 +0200918void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700919 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700920 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700921 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
922 new rtcp::TransportFeedback());
923 if (!transport_feedback->Parse(rtcp_block)) {
924 ++num_skipped_packets_;
925 return;
926 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200927
danilchap92ea6012016-09-23 10:36:03 -0700928 packet_information->packet_type_flags |= kRtcpTransportFeedback;
929 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200930}
danilchap287e5482016-08-16 15:15:39 -0700931
danilchap9bf610e2017-02-20 06:03:01 -0800932void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700933 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700934 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700935 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000936
spranga790d832016-12-02 07:29:44 -0800937 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700938 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700939 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
940 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800941 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000942 }
danilchap853ecb22016-08-22 08:26:15 -0700943
danilchap9bf610e2017-02-20 06:03:01 -0800944 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800945 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000946}
947
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000948void RTCPReceiver::RegisterRtcpStatisticsCallback(
949 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800950 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000951 stats_callback_ = callback;
952}
953
954RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800955 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000956 return stats_callback_;
957}
958
danilchap8bab7962016-12-20 02:46:46 -0800959// Holding no Critical section.
960void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700961 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000962 // Process TMMBR and REMB first to avoid multiple callbacks
963 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700964 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000965 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800966 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000967 }
sprang7dc39f32015-10-13 09:17:48 -0700968 uint32_t local_ssrc;
969 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000970 {
971 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800972 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000973 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700974 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000975 }
danilchap92ea6012016-09-23 10:36:03 -0700976 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800977 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000978 }
danilchap92ea6012016-09-23 10:36:03 -0700979 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
980 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100981 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
982 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800983 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000984 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000985 }
spranga790d832016-12-02 07:29:44 -0800986
987 // We need feedback that we have received a report block(s) so that we
988 // can generate a new packet in a conference relay scenario, one received
989 // report can generate several RTCP packets, based on number relayed/mixed
990 // a send report block should go out to all receivers.
991 if (rtcp_intra_frame_observer_) {
992 RTC_DCHECK(!receiver_only_);
993 if ((packet_information.packet_type_flags & kRtcpPli) ||
994 (packet_information.packet_type_flags & kRtcpFir)) {
995 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100996 RTC_LOG(LS_VERBOSE)
997 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -0800998 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100999 RTC_LOG(LS_VERBOSE)
1000 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001001 }
spranga790d832016-12-02 07:29:44 -08001002 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 }
spranga790d832016-12-02 07:29:44 -08001004 }
1005 if (rtcp_bandwidth_observer_) {
1006 RTC_DCHECK(!receiver_only_);
1007 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001008 RTC_LOG(LS_VERBOSE)
1009 << "Incoming REMB: "
1010 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001011 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1012 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001013 }
danilchap92ea6012016-09-23 10:36:03 -07001014 if ((packet_information.packet_type_flags & kRtcpSr) ||
1015 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001016 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001017 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001018 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001019 }
spranga790d832016-12-02 07:29:44 -08001020 }
1021 if ((packet_information.packet_type_flags & kRtcpSr) ||
1022 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001023 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001024 }
isheriff6b4b5f32016-06-08 00:24:21 -07001025
spranga790d832016-12-02 07:29:44 -08001026 if (transport_feedback_observer_ &&
1027 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1028 uint32_t media_source_ssrc =
1029 packet_information.transport_feedback->media_ssrc();
1030 if (media_source_ssrc == local_ssrc ||
1031 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1032 transport_feedback_observer_->OnTransportFeedback(
1033 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001034 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001035 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001036
spranga790d832016-12-02 07:29:44 -08001037 if (bitrate_allocation_observer_ &&
1038 packet_information.target_bitrate_allocation) {
1039 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1040 *packet_information.target_bitrate_allocation);
1041 }
1042
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001043 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001044 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001045 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001046 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001047 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001048 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001049 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001050 report_block.extended_highest_sequence_number;
1051 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001052 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001053
srte3e69e5c2017-08-09 06:13:45 -07001054 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001055 }
1056 }
1057 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001058}
1059
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001060int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001061 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001062 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001063
danilchap8bab7962016-12-20 02:46:46 -08001064 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001065 auto received_cname_it = received_cnames_.find(remoteSSRC);
1066 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001067 return -1;
danilchap95321242016-09-27 07:05:32 -07001068
1069 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1070 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001071 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001072}
1073
danilchap7851bda2016-09-29 15:28:07 -07001074std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001075 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001076 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001077
danilchap8bab7962016-12-20 02:46:46 -08001078 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001079 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001080
danilchap9bf610e2017-02-20 06:03:01 -08001081 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001082 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001083 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001084 // Erase timeout entries.
1085 it = kv.second.tmmbr.erase(it);
1086 } else {
1087 candidates.push_back(it->second.tmmbr_item);
1088 ++it;
1089 }
1090 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001091 }
danilchap287e5482016-08-16 15:15:39 -07001092 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001093}
1094
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001095} // namespace webrtc