blob: 3635c4aa029b818b22b7a9b1e94ae96e4437aa8d [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"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070022#include "api/video/video_bitrate_allocator.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020023#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#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;
Danil Chapovalovd264df52018-06-14 12:59:38 +020075 absl::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
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200283 const uint32_t now_ntp =
284 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000285
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200286 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
287 RrtrInformation& rrtr = received_rrtrs_.front();
288 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
289 now_ntp - rrtr.local_receive_mid_ntp_time);
290 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
291 received_rrtrs_.pop_front();
292 }
293
294 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000295}
296
danilchap28b03eb2016-10-05 06:59:44 -0700297// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000298int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700299 std::vector<RTCPReportBlock>* receive_blocks) const {
300 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800301 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700302 for (const auto& reports_per_receiver : received_report_blocks_)
303 for (const auto& report : reports_per_receiver.second)
304 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000305 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
danilchap92ea6012016-09-23 10:36:03 -0700308bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
309 const uint8_t* packet_end,
310 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800311 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000312
danilchap1b1863a2016-09-20 01:39:54 -0700313 CommonHeader rtcp_block;
314 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
315 next_block = rtcp_block.NextPacket()) {
316 ptrdiff_t remaining_blocks_size = packet_end - next_block;
317 RTC_DCHECK_GT(remaining_blocks_size, 0);
318 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
319 if (next_block == packet_begin) {
320 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100321 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700322 return false;
323 }
324 ++num_skipped_packets_;
325 break;
326 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
danilchap1b1863a2016-09-20 01:39:54 -0700328 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800329 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700330
danilchap1b1863a2016-09-20 01:39:54 -0700331 switch (rtcp_block.type()) {
332 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700333 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200334 break;
danilchap1b1863a2016-09-20 01:39:54 -0700335 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700336 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700337 break;
danilchap1b1863a2016-09-20 01:39:54 -0700338 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800339 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700340 break;
danilchap1b1863a2016-09-20 01:39:54 -0700341 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700342 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700343 break;
danilchap1b1863a2016-09-20 01:39:54 -0700344 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800345 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700346 break;
danilchap1b1863a2016-09-20 01:39:54 -0700347 case rtcp::Rtpfb::kPacketType:
348 switch (rtcp_block.fmt()) {
349 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800350 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700351 break;
352 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800353 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700354 break;
355 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800356 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700357 break;
358 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800359 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700360 break;
361 case rtcp::TransportFeedback::kFeedbackMessageType:
362 HandleTransportFeedback(rtcp_block, packet_information);
363 break;
364 default:
365 ++num_skipped_packets_;
366 break;
367 }
danilchapda161d72016-08-19 07:29:46 -0700368 break;
danilchap1b1863a2016-09-20 01:39:54 -0700369 case rtcp::Psfb::kPacketType:
370 switch (rtcp_block.fmt()) {
371 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800372 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700373 break;
danilchap1b1863a2016-09-20 01:39:54 -0700374 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800375 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700376 break;
377 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700378 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700379 break;
380 default:
381 ++num_skipped_packets_;
382 break;
383 }
danilchapda161d72016-08-19 07:29:46 -0700384 break;
385 default:
danilchap1b1863a2016-09-20 01:39:54 -0700386 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700387 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000388 }
danilchapda161d72016-08-19 07:29:46 -0700389 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000390
danilchap8bab7962016-12-20 02:46:46 -0800391 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700392 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
393 main_ssrc_, packet_type_counter_);
394 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000395
danilchap8bab7962016-12-20 02:46:46 -0800396 int64_t now_ms = clock_->TimeInMilliseconds();
397 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700398 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800399 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100400 RTC_LOG(LS_WARNING)
401 << num_skipped_packets_
402 << " RTCP blocks were skipped due to being malformed or of "
403 "unrecognized/unsupported type, during the past "
404 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700405 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200406
danilchap1b1863a2016-09-20 01:39:54 -0700407 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000408}
409
danilchap92ea6012016-09-23 10:36:03 -0700410void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
411 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700412 rtcp::SenderReport sender_report;
413 if (!sender_report.Parse(rtcp_block)) {
414 ++num_skipped_packets_;
415 return;
416 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
danilchap8bab7962016-12-20 02:46:46 -0800418 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
danilchap8bab7962016-12-20 02:46:46 -0800420 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
danilchapec067e92017-02-21 05:38:19 -0800422 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700423
Danil Chapovalov91511f12016-09-15 16:24:02 +0200424 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800425 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200426 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700427 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700428
danilchapa04d9c32017-07-25 04:03:39 -0700429 remote_sender_ntp_time_ = sender_report.ntp();
430 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200431 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700432 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200433 // We will only store the send report from one source, but
434 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700435 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700436 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000437
danilchap1b1863a2016-09-20 01:39:54 -0700438 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800439 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
danilchap92ea6012016-09-23 10:36:03 -0700442void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
443 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700444 rtcp::ReceiverReport receiver_report;
445 if (!receiver_report.Parse(rtcp_block)) {
446 ++num_skipped_packets_;
447 return;
448 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200449
danilchap8bab7962016-12-20 02:46:46 -0800450 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200451
danilchap8bab7962016-12-20 02:46:46 -0800452 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200453
danilchapec067e92017-02-21 05:38:19 -0800454 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200455
danilchap92ea6012016-09-23 10:36:03 -0700456 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200457
danilchap1b1863a2016-09-20 01:39:54 -0700458 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800459 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200460}
461
danilchap92ea6012016-09-23 10:36:03 -0700462void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
463 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700464 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000465 // This will be called once per report block in the RTCP packet.
466 // We filter out all report blocks that are not for us.
467 // Each packet has max 31 RR blocks.
468 //
469 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
danilchap28b03eb2016-10-05 06:59:44 -0700471 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000472 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000474 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700475 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000476 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000477
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200478 last_received_rb_ms_ = clock_->TimeInMilliseconds();
479
danilchap28b03eb2016-10-05 06:59:44 -0700480 ReportBlockWithRtt* report_block_info =
481 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700482 report_block_info->report_block.sender_ssrc = remote_ssrc;
483 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
484 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
Harald Alvestrand70206d62017-12-08 08:59:07 +0100485 report_block_info->report_block.packets_lost =
486 report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700487 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700488 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000489 // We have successfully delivered new RTP packets to the remote side after
490 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800491 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000492 }
srte3e69e5c2017-08-09 06:13:45 -0700493 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700494 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700495 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700496 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700497 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700498 report_block_info->report_block.last_sender_report_timestamp =
499 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000500
danilchap28b03eb2016-10-05 06:59:44 -0700501 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800502 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100503 // RFC3550, section 6.4.1, LSR field discription states:
504 // If no SR has been received yet, the field is set to zero.
505 // Receiver rtp_rtcp module is not expected to calculate rtt using
506 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100507
508 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
509 // is false. However, that currently breaks the tests of the
510 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
511 // delete all dependencies on RTT measurements for audio receive streams, or
512 // ensure that audio receive streams that need RTT and stats that depend on it
513 // are configured with an associated audio send stream.
514 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800515 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100516 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200517 uint32_t receive_time_ntp =
518 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000519
Danil Chapovalova094fd12016-02-22 18:59:36 +0100520 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800521 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100522 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700523 rtt_ms = CompactNtpRttToMs(rtt_ntp);
524 if (rtt_ms > report_block_info->max_rtt_ms)
525 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000526
danilchap28b03eb2016-10-05 06:59:44 -0700527 if (report_block_info->num_rtts == 0 ||
528 rtt_ms < report_block_info->min_rtt_ms)
529 report_block_info->min_rtt_ms = rtt_ms;
530
531 report_block_info->last_rtt_ms = rtt_ms;
532 report_block_info->sum_rtt_ms += rtt_ms;
533 ++report_block_info->num_rtts;
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100534
535 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000536 }
537
danilchap28b03eb2016-10-05 06:59:44 -0700538 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000539}
540
danilchapec067e92017-02-21 05:38:19 -0800541RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
542 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700543 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800544 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700545 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800546 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800547 return tmmbr_info;
548}
549
550void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
551 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
552 if (tmmbr_it != tmmbr_infos_.end())
553 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000554}
555
danilchap9bf610e2017-02-20 06:03:01 -0800556RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700557 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800558 auto it = tmmbr_infos_.find(remote_ssrc);
559 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700560 return nullptr;
561 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000562}
563
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000564bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800565 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200566 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000567 return false;
568
569 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200570 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000571 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200572 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000573 return true;
574 }
575 return false;
576}
577
578bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800579 rtc::CritScope lock(&rtcp_receiver_lock_);
580 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000581 return false;
582
583 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800584 if (clock_->TimeInMilliseconds() >
585 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000586 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800587 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000588 return true;
589 }
590 return false;
591}
592
danilchap9bf610e2017-02-20 06:03:01 -0800593bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800594 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
danilchap8bab7962016-12-20 02:46:46 -0800596 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800597 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000598
danilchap9bf610e2017-02-20 06:03:01 -0800599 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800600 return false;
601
602 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800603 oldest_tmmbr_info_ms_ = -1;
604 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
605 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
606 if (tmmbr_info->last_time_received_ms > 0) {
607 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700608 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800609 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700610 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800611 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700612 // Send new TMMBN to all channels using the default codec.
613 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800614 } else if (oldest_tmmbr_info_ms_ == -1 ||
615 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
616 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000617 }
danilchap9bf610e2017-02-20 06:03:01 -0800618 ++tmmbr_it;
619 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700620 // When we dont have a last_time_received_ms and the object is marked
621 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800622 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000623 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800624 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000625 }
626 }
danilchap7851bda2016-09-29 15:28:07 -0700627 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000628}
629
danilchap2b616392016-08-18 06:17:42 -0700630std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800631 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800632 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
633 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700634 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700635
danilchap9bf610e2017-02-20 06:03:01 -0800636 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
637 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000638}
639
danilchap8bab7962016-12-20 02:46:46 -0800640void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700641 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700642 rtcp::Sdes sdes;
643 if (!sdes.Parse(rtcp_block)) {
644 ++num_skipped_packets_;
645 return;
646 }
647
648 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700649 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200650 {
spranga790d832016-12-02 07:29:44 -0800651 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700652 if (stats_callback_)
653 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200654 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000655 }
danilchap92ea6012016-09-23 10:36:03 -0700656 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657}
658
danilchap8bab7962016-12-20 02:46:46 -0800659void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700660 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700661 rtcp::Nack nack;
662 if (!nack.Parse(rtcp_block)) {
663 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000664 return;
665 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000666
danilchap1b1863a2016-09-20 01:39:54 -0700667 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
668 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200669
danilchap142f0192016-10-20 08:22:42 -0700670 packet_information->nack_sequence_numbers.insert(
671 packet_information->nack_sequence_numbers.end(),
672 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700673 for (uint16_t packet_id : nack.packet_ids())
674 nack_stats_.ReportRequest(packet_id);
675
676 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700677 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000678 ++packet_type_counter_.nack_packets;
679 packet_type_counter_.nack_requests = nack_stats_.requests();
680 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
681 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000682}
683
danilchap8bab7962016-12-20 02:46:46 -0800684void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700685 rtcp::Bye bye;
686 if (!bye.Parse(rtcp_block)) {
687 ++num_skipped_packets_;
688 return;
689 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000690
danilchap8bab7962016-12-20 02:46:46 -0800691 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700692 for (auto& reports_per_receiver : received_report_blocks_)
693 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000694
danilchap9bf610e2017-02-20 06:03:01 -0800695 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
696 if (tmmbr_info)
697 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000698
danilchapefa966b2017-02-17 06:23:15 -0800699 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700700 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200701 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
702 if (it != received_rrtrs_ssrc_it_.end()) {
703 received_rrtrs_.erase(it->second);
704 received_rrtrs_ssrc_it_.erase(it);
705 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000706 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000707}
708
danilchap1b1863a2016-09-20 01:39:54 -0700709void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700710 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700711 rtcp::ExtendedReports xr;
712 if (!xr.Parse(rtcp_block)) {
713 ++num_skipped_packets_;
714 return;
715 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000716
danilchap80ac24d2016-10-31 08:40:47 -0700717 if (xr.rrtr())
718 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000719
danilchap80ac24d2016-10-31 08:40:47 -0700720 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
721 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800722
sprangb32aaf92017-08-28 05:49:12 -0700723 if (xr.target_bitrate()) {
724 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
725 packet_information);
726 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000727}
728
danilchap8bab7962016-12-20 02:46:46 -0800729void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
730 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200731 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200732 uint32_t local_receive_mid_ntp_time =
733 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200734
735 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
736 if (it != received_rrtrs_ssrc_it_.end()) {
737 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
738 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
739 } else {
740 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
741 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
742 local_receive_mid_ntp_time);
743 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
744 } else {
745 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
746 << ", reached maximum number of stored RRTRs.";
747 }
748 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000749}
750
danilchap92ea6012016-09-23 10:36:03 -0700751void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700752 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
753 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000754
danilchap1b1863a2016-09-20 01:39:54 -0700755 // Caller should explicitly enable rtt calculation using extended reports.
756 if (!xr_rrtr_status_)
757 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200758
danilchap1b1863a2016-09-20 01:39:54 -0700759 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800760 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700761 // RFC3611, section 4.5, LRR field discription states:
762 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800763 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700764 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200765
danilchap8bab7962016-12-20 02:46:46 -0800766 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200767 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200768
danilchap8bab7962016-12-20 02:46:46 -0800769 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700770 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000771}
772
spranga790d832016-12-02 07:29:44 -0800773void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700774 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800775 const rtcp::TargetBitrate& target_bitrate,
776 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700777 if (ssrc != remote_ssrc_) {
778 return; // Not for us.
779 }
780
Erik Språng566124a2018-04-23 12:32:22 +0200781 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800782 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800783 if (item.spatial_layer >= kMaxSpatialLayers ||
784 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100785 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800786 << "Invalid layer in XR target bitrate pack: spatial index "
787 << item.spatial_layer << ", temporal index " << item.temporal_layer
788 << ", dropping.";
789 } else {
790 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
791 item.target_bitrate_kbps * 1000);
792 }
spranga790d832016-12-02 07:29:44 -0800793 }
794 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
795}
796
danilchap8bab7962016-12-20 02:46:46 -0800797void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700798 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700799 rtcp::Pli pli;
800 if (!pli.Parse(rtcp_block)) {
801 ++num_skipped_packets_;
802 return;
803 }
804
805 if (main_ssrc_ == pli.media_ssrc()) {
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];
Yves Gerey665174f2018-06-19 15:03:05 +0200833 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800834 request.packet_overhead());
835 entry->last_updated_ms = clock_->TimeInMilliseconds();
836
837 packet_information->packet_type_flags |= kRtcpTmmbr;
838 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000839 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000840}
841
danilchap8bab7962016-12-20 02:46:46 -0800842void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700843 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700844 rtcp::Tmmbn tmmbn;
845 if (!tmmbn.Parse(rtcp_block)) {
846 ++num_skipped_packets_;
847 return;
848 }
849
danilchapec067e92017-02-21 05:38:19 -0800850 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700851
danilchap92ea6012016-09-23 10:36:03 -0700852 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000853
danilchapec067e92017-02-21 05:38:19 -0800854 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700855}
856
danilchap8bab7962016-12-20 02:46:46 -0800857void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
858 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700859 rtcp::RapidResyncRequest sr_req;
860 if (!sr_req.Parse(rtcp_block)) {
861 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000862 return;
863 }
864
danilchap92ea6012016-09-23 10:36:03 -0700865 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000866}
867
danilchap1b1863a2016-09-20 01:39:54 -0700868void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700869 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700870 rtcp::Remb remb;
871 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700872 packet_information->packet_type_flags |= kRtcpRemb;
873 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700874 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000875 }
danilchap1b1863a2016-09-20 01:39:54 -0700876
877 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000878}
879
danilchap8bab7962016-12-20 02:46:46 -0800880void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700881 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700882 rtcp::Fir fir;
883 if (!fir.Parse(rtcp_block)) {
884 ++num_skipped_packets_;
885 return;
886 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000887
danilchap1b1863a2016-09-20 01:39:54 -0700888 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800889 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700890 if (main_ssrc_ != fir_request.ssrc)
891 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200892
893 ++packet_type_counter_.fir_packets;
894
danilchapefa966b2017-02-17 06:23:15 -0800895 int64_t now_ms = clock_->TimeInMilliseconds();
896 auto inserted = last_fir_.insert(std::make_pair(
897 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
898 if (!inserted.second) { // There was already an entry.
899 LastFirStatus* last_fir = &inserted.first->second;
900
danilchap7851bda2016-09-29 15:28:07 -0700901 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800902 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700903 continue;
904
danilchap7851bda2016-09-29 15:28:07 -0700905 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800906 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700907 continue;
908
danilchapefa966b2017-02-17 06:23:15 -0800909 last_fir->request_ms = now_ms;
910 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200911 }
danilchap7851bda2016-09-29 15:28:07 -0700912 // Received signal that we need to send a new key frame.
913 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000914 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000915}
916
Erik Språng6b8d3552015-09-24 15:06:57 +0200917void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700918 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700919 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700920 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
921 new rtcp::TransportFeedback());
922 if (!transport_feedback->Parse(rtcp_block)) {
923 ++num_skipped_packets_;
924 return;
925 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200926
danilchap92ea6012016-09-23 10:36:03 -0700927 packet_information->packet_type_flags |= kRtcpTransportFeedback;
928 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200929}
danilchap287e5482016-08-16 15:15:39 -0700930
danilchap9bf610e2017-02-20 06:03:01 -0800931void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700932 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700933 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700934 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000935
spranga790d832016-12-02 07:29:44 -0800936 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700937 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700938 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
939 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800940 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000941 }
danilchap853ecb22016-08-22 08:26:15 -0700942
danilchap9bf610e2017-02-20 06:03:01 -0800943 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800944 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000945}
946
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000947void RTCPReceiver::RegisterRtcpStatisticsCallback(
948 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800949 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000950 stats_callback_ = callback;
951}
952
953RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800954 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000955 return stats_callback_;
956}
957
danilchap8bab7962016-12-20 02:46:46 -0800958// Holding no Critical section.
959void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700960 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000961 // Process TMMBR and REMB first to avoid multiple callbacks
962 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700963 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000964 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800965 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000966 }
sprang7dc39f32015-10-13 09:17:48 -0700967 uint32_t local_ssrc;
968 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000969 {
970 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800971 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000972 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700973 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000974 }
danilchap92ea6012016-09-23 10:36:03 -0700975 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800976 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000977 }
danilchap92ea6012016-09-23 10:36:03 -0700978 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
979 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100980 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
981 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800982 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000983 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000984 }
spranga790d832016-12-02 07:29:44 -0800985
986 // We need feedback that we have received a report block(s) so that we
987 // can generate a new packet in a conference relay scenario, one received
988 // report can generate several RTCP packets, based on number relayed/mixed
989 // a send report block should go out to all receivers.
990 if (rtcp_intra_frame_observer_) {
991 RTC_DCHECK(!receiver_only_);
992 if ((packet_information.packet_type_flags & kRtcpPli) ||
993 (packet_information.packet_type_flags & kRtcpFir)) {
994 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100995 RTC_LOG(LS_VERBOSE)
996 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -0800997 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100998 RTC_LOG(LS_VERBOSE)
999 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001000 }
spranga790d832016-12-02 07:29:44 -08001001 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001002 }
spranga790d832016-12-02 07:29:44 -08001003 }
1004 if (rtcp_bandwidth_observer_) {
1005 RTC_DCHECK(!receiver_only_);
1006 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001007 RTC_LOG(LS_VERBOSE)
1008 << "Incoming REMB: "
1009 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001010 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1011 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001012 }
danilchap92ea6012016-09-23 10:36:03 -07001013 if ((packet_information.packet_type_flags & kRtcpSr) ||
1014 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001015 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001016 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001017 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001018 }
spranga790d832016-12-02 07:29:44 -08001019 }
1020 if ((packet_information.packet_type_flags & kRtcpSr) ||
1021 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001022 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001023 }
isheriff6b4b5f32016-06-08 00:24:21 -07001024
spranga790d832016-12-02 07:29:44 -08001025 if (transport_feedback_observer_ &&
1026 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1027 uint32_t media_source_ssrc =
1028 packet_information.transport_feedback->media_ssrc();
1029 if (media_source_ssrc == local_ssrc ||
1030 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1031 transport_feedback_observer_->OnTransportFeedback(
1032 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001033 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001034 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001035
spranga790d832016-12-02 07:29:44 -08001036 if (bitrate_allocation_observer_ &&
1037 packet_information.target_bitrate_allocation) {
1038 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1039 *packet_information.target_bitrate_allocation);
1040 }
1041
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001042 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001043 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001044 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001045 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001046 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001047 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001048 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001049 report_block.extended_highest_sequence_number;
1050 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001051 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001052
srte3e69e5c2017-08-09 06:13:45 -07001053 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001054 }
1055 }
1056 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001057}
1058
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001059int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001060 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001061 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001062
danilchap8bab7962016-12-20 02:46:46 -08001063 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001064 auto received_cname_it = received_cnames_.find(remoteSSRC);
1065 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001066 return -1;
danilchap95321242016-09-27 07:05:32 -07001067
1068 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1069 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001070 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001071}
1072
danilchap7851bda2016-09-29 15:28:07 -07001073std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001074 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001075 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001076
danilchap8bab7962016-12-20 02:46:46 -08001077 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001078 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001079
danilchap9bf610e2017-02-20 06:03:01 -08001080 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001081 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001082 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001083 // Erase timeout entries.
1084 it = kv.second.tmmbr.erase(it);
1085 } else {
1086 candidates.push_back(it->second.tmmbr_item);
1087 ++it;
1088 }
1089 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001090 }
danilchap287e5482016-08-16 15:15:39 -07001091 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001092}
1093
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001094} // namespace webrtc