blob: 0c1c326e4fa9ca858b66182e7c2fcdc4b9fb501d [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
Elad Alonf8e7ccb2019-01-24 12:38:36 +010021#include "absl/memory/memory.h"
Erik Språngeeaa8f92018-05-17 12:35:56 +020022#include "api/video/video_bitrate_allocation.h"
Jiawei Ou4206a0a2018-07-20 15:49:43 -070023#include "api/video/video_bitrate_allocator.h"
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"
Elad Alonf8e7ccb2019-01-24 12:38:36 +010029#include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
35#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
37#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
38#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
39#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
40#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
41#include "modules/rtp_rtcp/source/time_util.h"
42#include "modules/rtp_rtcp/source/tmmbr_help.h"
43#include "rtc_base/checks.h"
44#include "rtc_base/logging.h"
45#include "rtc_base/trace_event.h"
46#include "system_wrappers/include/ntp_time.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000047
niklase@google.com470e71d2011-07-07 08:21:25 +000048namespace webrtc {
danilchap1b1863a2016-09-20 01:39:54 -070049namespace {
50
51using rtcp::CommonHeader;
52using rtcp::ReportBlock;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +000054// The number of RTCP time intervals needed to trigger a timeout.
55const int kRrTimeoutIntervals = 3;
56
Jiawei Ou3587b832018-01-31 22:08:26 -080057const int64_t kTmmbrTimeoutIntervalMs = 5 * 5000;
58
Erik Språng6b8d3552015-09-24 15:06:57 +020059const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080060const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020061
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020062// Maximum number of received RRTRs that will be stored.
63const size_t kMaxNumberOfStoredRrtrs = 200;
64
danilchap1b1863a2016-09-20 01:39:54 -070065} // namespace
66
danilchap92ea6012016-09-23 10:36:03 -070067struct RTCPReceiver::PacketInformation {
68 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
69
70 uint32_t remote_ssrc = 0;
71 std::vector<uint16_t> nack_sequence_numbers;
72 ReportBlockList report_blocks;
73 int64_t rtt_ms = 0;
danilchap92ea6012016-09-23 10:36:03 -070074 uint32_t receiver_estimated_max_bitrate_bps = 0;
75 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
Danil Chapovalovd264df52018-06-14 12:59:38 +020076 absl::optional<VideoBitrateAllocation> target_bitrate_allocation;
Elad Alonf8e7ccb2019-01-24 12:38:36 +010077 std::unique_ptr<rtcp::LossNotification> loss_notification;
danilchap92ea6012016-09-23 10:36:03 -070078};
79
danilchap9bf610e2017-02-20 06:03:01 -080080// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
81struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070082 struct TimedTmmbrItem {
83 rtcp::TmmbItem tmmbr_item;
84 int64_t last_updated_ms;
85 };
86
87 int64_t last_time_received_ms = 0;
88
danilchap7851bda2016-09-29 15:28:07 -070089 bool ready_for_delete = false;
90
91 std::vector<rtcp::TmmbItem> tmmbn;
92 std::map<uint32_t, TimedTmmbrItem> tmmbr;
93};
94
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020095// Structure for storing received RRTR RTCP messages (RFC3611, section 4.4).
96struct RTCPReceiver::RrtrInformation {
97 RrtrInformation(uint32_t ssrc,
98 uint32_t received_remote_mid_ntp_time,
99 uint32_t local_receive_mid_ntp_time)
100 : ssrc(ssrc),
101 received_remote_mid_ntp_time(received_remote_mid_ntp_time),
102 local_receive_mid_ntp_time(local_receive_mid_ntp_time) {}
103
104 uint32_t ssrc;
105 // Received NTP timestamp in compact representation.
106 uint32_t received_remote_mid_ntp_time;
107 // NTP time when the report was received in compact representation.
108 uint32_t local_receive_mid_ntp_time;
109};
110
danilchap28b03eb2016-10-05 06:59:44 -0700111struct RTCPReceiver::ReportBlockWithRtt {
112 RTCPReportBlock report_block;
113
114 int64_t last_rtt_ms = 0;
115 int64_t min_rtt_ms = 0;
116 int64_t max_rtt_ms = 0;
117 int64_t sum_rtt_ms = 0;
118 size_t num_rtts = 0;
119};
120
danilchapefa966b2017-02-17 06:23:15 -0800121struct RTCPReceiver::LastFirStatus {
122 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
123 : request_ms(now_ms), sequence_number(sequence_number) {}
124 int64_t request_ms;
125 uint8_t sequence_number;
126};
127
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000128RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000129 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200130 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000131 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000132 RtcpBandwidthObserver* rtcp_bandwidth_observer,
133 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Elad Alon0a8562e2019-04-09 11:55:13 +0200134 RtcpLossNotificationObserver* rtcp_loss_notification_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200135 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800136 VideoBitrateAllocationObserver* bitrate_allocation_observer,
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800137 int report_interval_ms,
danilchap59cb2bd2016-08-29 11:08:47 -0700138 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800139 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200140 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800141 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800142 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
143 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
Elad Alon0a8562e2019-04-09 11:55:13 +0200144 rtcp_loss_notification_observer_(rtcp_loss_notification_observer),
spranga790d832016-12-02 07:29:44 -0800145 transport_feedback_observer_(transport_feedback_observer),
146 bitrate_allocation_observer_(bitrate_allocation_observer),
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800147 report_interval_ms_(report_interval_ms),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000148 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800149 remote_ssrc_(0),
danilchapa04d9c32017-07-25 04:03:39 -0700150 remote_sender_rtp_time_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100151 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000152 xr_rr_rtt_ms_(0),
sprangd0fc37a2017-06-22 05:40:25 -0700153 oldest_tmmbr_info_ms_(0),
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200154 last_received_rb_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800155 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800156 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200157 packet_type_counter_observer_(packet_type_counter_observer),
158 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800159 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
160 RTC_DCHECK(owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
danilchap28b03eb2016-10-05 06:59:44 -0700163RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
nisse479d3d72017-09-13 07:53:37 -0700165void RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700166 if (packet_size == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100167 RTC_LOG(LS_WARNING) << "Incoming empty RTCP packet";
nisse479d3d72017-09-13 07:53:37 -0700168 return;
danilchap59cb2bd2016-08-29 11:08:47 -0700169 }
danilchap59cb2bd2016-08-29 11:08:47 -0700170
danilchap92ea6012016-09-23 10:36:03 -0700171 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700172 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
nisse479d3d72017-09-13 07:53:37 -0700173 return;
danilchap8bab7962016-12-20 02:46:46 -0800174 TriggerCallbacksFromRtcpPacket(packet_information);
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200177int64_t RTCPReceiver::LastReceivedReportBlockMs() const {
danilchap8bab7962016-12-20 02:46:46 -0800178 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200179 return last_received_rb_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000180}
181
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000182void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800183 rtc::CritScope lock(&rtcp_receiver_lock_);
184 // New SSRC reset old reports.
danilchap0b4b7272016-10-06 09:24:45 -0700185 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800186 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000189uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800190 rtc::CritScope lock(&rtcp_receiver_lock_);
191 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000192}
193
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000194void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
195 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800196 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700197 main_ssrc_ = main_ssrc;
198 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199}
200
danilchap28b03eb2016-10-05 06:59:44 -0700201int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
202 int64_t* last_rtt_ms,
203 int64_t* avg_rtt_ms,
204 int64_t* min_rtt_ms,
205 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800206 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
danilchap28b03eb2016-10-05 06:59:44 -0700208 auto it = received_report_blocks_.find(main_ssrc_);
209 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000210 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700211
212 auto it_info = it->second.find(remote_ssrc);
213 if (it_info == it->second.end())
214 return -1;
215
216 const ReportBlockWithRtt* report_block = &it_info->second;
217
218 if (report_block->num_rtts == 0)
219 return -1;
220
221 if (last_rtt_ms)
222 *last_rtt_ms = report_block->last_rtt_ms;
223
224 if (avg_rtt_ms)
225 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
226
227 if (min_rtt_ms)
228 *min_rtt_ms = report_block->min_rtt_ms;
229
230 if (max_rtt_ms)
231 *max_rtt_ms = report_block->max_rtt_ms;
232
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100236void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800237 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100238 xr_rrtr_status_ = enable;
239}
240
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000241bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800242 RTC_DCHECK(rtt_ms);
243 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000244 if (xr_rr_rtt_ms_ == 0) {
245 return false;
246 }
247 *rtt_ms = xr_rr_rtt_ms_;
248 xr_rr_rtt_ms_ = 0;
249 return true;
250}
251
danilchap8bab7962016-12-20 02:46:46 -0800252bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
253 uint32_t* received_ntp_frac,
254 uint32_t* rtcp_arrival_time_secs,
255 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700256 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800257 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700258 if (!last_received_sr_ntp_.Valid())
259 return false;
260
261 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800262 if (received_ntp_secs)
danilchapa04d9c32017-07-25 04:03:39 -0700263 *received_ntp_secs = remote_sender_ntp_time_.seconds();
danilchap8bab7962016-12-20 02:46:46 -0800264 if (received_ntp_frac)
danilchapa04d9c32017-07-25 04:03:39 -0700265 *received_ntp_frac = remote_sender_ntp_time_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700266
267 // Rtp time from incoming SenderReport.
268 if (rtcp_timestamp)
danilchapa04d9c32017-07-25 04:03:39 -0700269 *rtcp_timestamp = remote_sender_rtp_time_;
danilchap0b4b7272016-10-06 09:24:45 -0700270
271 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800272 if (rtcp_arrival_time_secs)
273 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
274 if (rtcp_arrival_time_frac)
275 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700276
danilchapda161d72016-08-19 07:29:46 -0700277 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278}
279
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200280std::vector<rtcp::ReceiveTimeInfo>
281RTCPReceiver::ConsumeReceivedXrReferenceTimeInfo() {
danilchap8bab7962016-12-20 02:46:46 -0800282 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000283
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200284 const size_t last_xr_rtis_size = std::min(
285 received_rrtrs_.size(), rtcp::ExtendedReports::kMaxNumberOfDlrrItems);
286 std::vector<rtcp::ReceiveTimeInfo> last_xr_rtis;
287 last_xr_rtis.reserve(last_xr_rtis_size);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000288
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200289 const uint32_t now_ntp =
290 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000291
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200292 for (size_t i = 0; i < last_xr_rtis_size; ++i) {
293 RrtrInformation& rrtr = received_rrtrs_.front();
294 last_xr_rtis.emplace_back(rrtr.ssrc, rrtr.received_remote_mid_ntp_time,
295 now_ntp - rrtr.local_receive_mid_ntp_time);
296 received_rrtrs_ssrc_it_.erase(rrtr.ssrc);
297 received_rrtrs_.pop_front();
298 }
299
300 return last_xr_rtis;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000301}
302
danilchap28b03eb2016-10-05 06:59:44 -0700303// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000304int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700305 std::vector<RTCPReportBlock>* receive_blocks) const {
306 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800307 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700308 for (const auto& reports_per_receiver : received_report_blocks_)
309 for (const auto& report : reports_per_receiver.second)
310 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000311 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
danilchap92ea6012016-09-23 10:36:03 -0700314bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
315 const uint8_t* packet_end,
316 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800317 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
danilchap1b1863a2016-09-20 01:39:54 -0700319 CommonHeader rtcp_block;
320 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
321 next_block = rtcp_block.NextPacket()) {
322 ptrdiff_t remaining_blocks_size = packet_end - next_block;
323 RTC_DCHECK_GT(remaining_blocks_size, 0);
324 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
325 if (next_block == packet_begin) {
326 // Failed to parse 1st header, nothing was extracted from this packet.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100327 RTC_LOG(LS_WARNING) << "Incoming invalid RTCP packet";
danilchap1b1863a2016-09-20 01:39:54 -0700328 return false;
329 }
330 ++num_skipped_packets_;
331 break;
332 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000333
danilchap1b1863a2016-09-20 01:39:54 -0700334 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800335 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700336
danilchap1b1863a2016-09-20 01:39:54 -0700337 switch (rtcp_block.type()) {
338 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700339 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200340 break;
danilchap1b1863a2016-09-20 01:39:54 -0700341 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700342 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700343 break;
danilchap1b1863a2016-09-20 01:39:54 -0700344 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800345 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700346 break;
danilchap1b1863a2016-09-20 01:39:54 -0700347 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700348 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700349 break;
danilchap1b1863a2016-09-20 01:39:54 -0700350 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800351 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700352 break;
danilchap1b1863a2016-09-20 01:39:54 -0700353 case rtcp::Rtpfb::kPacketType:
354 switch (rtcp_block.fmt()) {
355 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800356 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700357 break;
358 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800359 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700360 break;
361 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800362 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700363 break;
364 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800365 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700366 break;
367 case rtcp::TransportFeedback::kFeedbackMessageType:
368 HandleTransportFeedback(rtcp_block, packet_information);
369 break;
370 default:
371 ++num_skipped_packets_;
372 break;
373 }
danilchapda161d72016-08-19 07:29:46 -0700374 break;
danilchap1b1863a2016-09-20 01:39:54 -0700375 case rtcp::Psfb::kPacketType:
376 switch (rtcp_block.fmt()) {
377 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800378 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700379 break;
danilchap1b1863a2016-09-20 01:39:54 -0700380 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800381 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700382 break;
Elad Alon74f0a512019-02-23 22:16:52 +0100383 case rtcp::Psfb::kAfbMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700384 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700385 break;
386 default:
387 ++num_skipped_packets_;
388 break;
389 }
danilchapda161d72016-08-19 07:29:46 -0700390 break;
391 default:
danilchap1b1863a2016-09-20 01:39:54 -0700392 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700393 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000394 }
danilchapda161d72016-08-19 07:29:46 -0700395 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000396
danilchap8bab7962016-12-20 02:46:46 -0800397 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700398 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
399 main_ssrc_, packet_type_counter_);
400 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000401
danilchap8bab7962016-12-20 02:46:46 -0800402 int64_t now_ms = clock_->TimeInMilliseconds();
403 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700404 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800405 last_skipped_packets_warning_ms_ = now_ms;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_WARNING)
407 << num_skipped_packets_
408 << " RTCP blocks were skipped due to being malformed or of "
409 "unrecognized/unsupported type, during the past "
410 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
danilchapda161d72016-08-19 07:29:46 -0700411 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200412
danilchap1b1863a2016-09-20 01:39:54 -0700413 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414}
415
danilchap92ea6012016-09-23 10:36:03 -0700416void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
417 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700418 rtcp::SenderReport sender_report;
419 if (!sender_report.Parse(rtcp_block)) {
420 ++num_skipped_packets_;
421 return;
422 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
danilchap8bab7962016-12-20 02:46:46 -0800424 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
danilchap8bab7962016-12-20 02:46:46 -0800426 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
danilchapec067e92017-02-21 05:38:19 -0800428 UpdateTmmbrRemoteIsAlive(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700429
Danil Chapovalov91511f12016-09-15 16:24:02 +0200430 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800431 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200432 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700433 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700434
danilchapa04d9c32017-07-25 04:03:39 -0700435 remote_sender_ntp_time_ = sender_report.ntp();
436 remote_sender_rtp_time_ = sender_report.rtp_timestamp();
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200437 last_received_sr_ntp_ = TimeMicrosToNtp(clock_->TimeInMicroseconds());
danilchapda161d72016-08-19 07:29:46 -0700438 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200439 // We will only store the send report from one source, but
440 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700441 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700442 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000443
Mirko Bonadei739baf02019-01-27 17:29:42 +0100444 for (const rtcp::ReportBlock& report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800445 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
danilchap92ea6012016-09-23 10:36:03 -0700448void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
449 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700450 rtcp::ReceiverReport receiver_report;
451 if (!receiver_report.Parse(rtcp_block)) {
452 ++num_skipped_packets_;
453 return;
454 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200455
danilchap8bab7962016-12-20 02:46:46 -0800456 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200457
danilchap8bab7962016-12-20 02:46:46 -0800458 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200459
danilchapec067e92017-02-21 05:38:19 -0800460 UpdateTmmbrRemoteIsAlive(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200461
danilchap92ea6012016-09-23 10:36:03 -0700462 packet_information->packet_type_flags |= kRtcpRr;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200463
danilchap1b1863a2016-09-20 01:39:54 -0700464 for (const ReportBlock& report_block : receiver_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800465 HandleReportBlock(report_block, packet_information, remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200466}
467
danilchap92ea6012016-09-23 10:36:03 -0700468void RTCPReceiver::HandleReportBlock(const ReportBlock& report_block,
469 PacketInformation* packet_information,
danilchap28b03eb2016-10-05 06:59:44 -0700470 uint32_t remote_ssrc) {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000471 // This will be called once per report block in the RTCP packet.
472 // We filter out all report blocks that are not for us.
473 // Each packet has max 31 RR blocks.
474 //
475 // We can calc RTT if we send a send report and get a report block back.
niklase@google.com470e71d2011-07-07 08:21:25 +0000476
danilchap28b03eb2016-10-05 06:59:44 -0700477 // |report_block.source_ssrc()| is the SSRC identifier of the source to
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000478 // which the information in this reception report block pertains.
niklase@google.com470e71d2011-07-07 08:21:25 +0000479
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000480 // Filter out all report blocks that are not for us.
danilchap1b1863a2016-09-20 01:39:54 -0700481 if (registered_ssrcs_.count(report_block.source_ssrc()) == 0)
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000482 return;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000483
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200484 last_received_rb_ms_ = clock_->TimeInMilliseconds();
485
danilchap28b03eb2016-10-05 06:59:44 -0700486 ReportBlockWithRtt* report_block_info =
487 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
srte3e69e5c2017-08-09 06:13:45 -0700488 report_block_info->report_block.sender_ssrc = remote_ssrc;
489 report_block_info->report_block.source_ssrc = report_block.source_ssrc();
490 report_block_info->report_block.fraction_lost = report_block.fraction_lost();
Harald Alvestrand70206d62017-12-08 08:59:07 +0100491 report_block_info->report_block.packets_lost =
492 report_block.cumulative_lost_signed();
danilchap1b1863a2016-09-20 01:39:54 -0700493 if (report_block.extended_high_seq_num() >
srte3e69e5c2017-08-09 06:13:45 -0700494 report_block_info->report_block.extended_highest_sequence_number) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000495 // We have successfully delivered new RTP packets to the remote side after
496 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800497 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000498 }
srte3e69e5c2017-08-09 06:13:45 -0700499 report_block_info->report_block.extended_highest_sequence_number =
danilchap1b1863a2016-09-20 01:39:54 -0700500 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700501 report_block_info->report_block.jitter = report_block.jitter();
srte3e69e5c2017-08-09 06:13:45 -0700502 report_block_info->report_block.delay_since_last_sender_report =
danilchap1b1863a2016-09-20 01:39:54 -0700503 report_block.delay_since_last_sr();
srte3e69e5c2017-08-09 06:13:45 -0700504 report_block_info->report_block.last_sender_report_timestamp =
505 report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000506
danilchap28b03eb2016-10-05 06:59:44 -0700507 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800508 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100509 // RFC3550, section 6.4.1, LSR field discription states:
510 // If no SR has been received yet, the field is set to zero.
511 // Receiver rtp_rtcp module is not expected to calculate rtt using
512 // Sender Reports even if it accidentally can.
Niels Möllerfd1a2fb2018-10-31 15:25:26 +0100513
514 // TODO(nisse): Use this way to determine the RTT only when |receiver_only_|
515 // is false. However, that currently breaks the tests of the
516 // googCaptureStartNtpTimeMs stat for audio receive streams. To fix, either
517 // delete all dependencies on RTT measurements for audio receive streams, or
518 // ensure that audio receive streams that need RTT and stats that depend on it
519 // are configured with an associated audio send stream.
520 if (send_time_ntp != 0) {
danilchap8bab7962016-12-20 02:46:46 -0800521 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100522 // Local NTP time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200523 uint32_t receive_time_ntp =
524 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000525
Danil Chapovalova094fd12016-02-22 18:59:36 +0100526 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800527 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100528 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700529 rtt_ms = CompactNtpRttToMs(rtt_ntp);
530 if (rtt_ms > report_block_info->max_rtt_ms)
531 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000532
danilchap28b03eb2016-10-05 06:59:44 -0700533 if (report_block_info->num_rtts == 0 ||
534 rtt_ms < report_block_info->min_rtt_ms)
535 report_block_info->min_rtt_ms = rtt_ms;
536
537 report_block_info->last_rtt_ms = rtt_ms;
538 report_block_info->sum_rtt_ms += rtt_ms;
539 ++report_block_info->num_rtts;
Danil Chapovalov04164cc2018-01-26 20:01:48 +0100540
541 packet_information->rtt_ms = rtt_ms;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000542 }
543
danilchap28b03eb2016-10-05 06:59:44 -0700544 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
danilchapec067e92017-02-21 05:38:19 -0800547RTCPReceiver::TmmbrInformation* RTCPReceiver::FindOrCreateTmmbrInfo(
548 uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700549 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800550 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700551 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800552 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
danilchapec067e92017-02-21 05:38:19 -0800553 return tmmbr_info;
554}
555
556void RTCPReceiver::UpdateTmmbrRemoteIsAlive(uint32_t remote_ssrc) {
557 auto tmmbr_it = tmmbr_infos_.find(remote_ssrc);
558 if (tmmbr_it != tmmbr_infos_.end())
559 tmmbr_it->second.last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000560}
561
danilchap9bf610e2017-02-20 06:03:01 -0800562RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700563 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800564 auto it = tmmbr_infos_.find(remote_ssrc);
565 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700566 return nullptr;
567 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000568}
569
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800570bool RTCPReceiver::RtcpRrTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800571 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200572 if (last_received_rb_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000573 return false;
574
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800575 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200576 if (clock_->TimeInMilliseconds() > last_received_rb_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000577 // Reset the timer to only trigger one log.
Danil Chapovalov760c4b42017-09-27 13:25:24 +0200578 last_received_rb_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000579 return true;
580 }
581 return false;
582}
583
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800584bool RTCPReceiver::RtcpRrSequenceNumberTimeout() {
danilchap8bab7962016-12-20 02:46:46 -0800585 rtc::CritScope lock(&rtcp_receiver_lock_);
586 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000587 return false;
588
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800589 int64_t time_out_ms = kRrTimeoutIntervals * report_interval_ms_;
danilchap8bab7962016-12-20 02:46:46 -0800590 if (clock_->TimeInMilliseconds() >
591 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000592 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800593 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000594 return true;
595 }
596 return false;
597}
598
danilchap9bf610e2017-02-20 06:03:01 -0800599bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800600 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
danilchap8bab7962016-12-20 02:46:46 -0800602 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -0800603 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000604
danilchap9bf610e2017-02-20 06:03:01 -0800605 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800606 return false;
607
608 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800609 oldest_tmmbr_info_ms_ = -1;
610 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
611 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
612 if (tmmbr_info->last_time_received_ms > 0) {
613 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700614 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800615 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700616 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800617 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700618 // Send new TMMBN to all channels using the default codec.
619 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800620 } else if (oldest_tmmbr_info_ms_ == -1 ||
621 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
622 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000623 }
danilchap9bf610e2017-02-20 06:03:01 -0800624 ++tmmbr_it;
625 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700626 // When we dont have a last_time_received_ms and the object is marked
627 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800628 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000629 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800630 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000631 }
632 }
danilchap7851bda2016-09-29 15:28:07 -0700633 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
danilchap2b616392016-08-18 06:17:42 -0700636std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800637 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800638 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
639 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700640 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700641
danilchap9bf610e2017-02-20 06:03:01 -0800642 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
643 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000644}
645
danilchap8bab7962016-12-20 02:46:46 -0800646void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700647 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700648 rtcp::Sdes sdes;
649 if (!sdes.Parse(rtcp_block)) {
650 ++num_skipped_packets_;
651 return;
652 }
653
654 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700655 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200656 {
spranga790d832016-12-02 07:29:44 -0800657 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700658 if (stats_callback_)
659 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200660 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000661 }
danilchap92ea6012016-09-23 10:36:03 -0700662 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000663}
664
danilchap8bab7962016-12-20 02:46:46 -0800665void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700666 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700667 rtcp::Nack nack;
668 if (!nack.Parse(rtcp_block)) {
669 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000670 return;
671 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000672
danilchap1b1863a2016-09-20 01:39:54 -0700673 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
674 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200675
danilchap142f0192016-10-20 08:22:42 -0700676 packet_information->nack_sequence_numbers.insert(
677 packet_information->nack_sequence_numbers.end(),
678 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700679 for (uint16_t packet_id : nack.packet_ids())
680 nack_stats_.ReportRequest(packet_id);
681
682 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700683 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000684 ++packet_type_counter_.nack_packets;
685 packet_type_counter_.nack_requests = nack_stats_.requests();
686 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
687 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000688}
689
danilchap8bab7962016-12-20 02:46:46 -0800690void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700691 rtcp::Bye bye;
692 if (!bye.Parse(rtcp_block)) {
693 ++num_skipped_packets_;
694 return;
695 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000696
danilchap8bab7962016-12-20 02:46:46 -0800697 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700698 for (auto& reports_per_receiver : received_report_blocks_)
699 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000700
danilchap9bf610e2017-02-20 06:03:01 -0800701 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
702 if (tmmbr_info)
703 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000704
danilchapefa966b2017-02-17 06:23:15 -0800705 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700706 received_cnames_.erase(bye.sender_ssrc());
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200707 auto it = received_rrtrs_ssrc_it_.find(bye.sender_ssrc());
708 if (it != received_rrtrs_ssrc_it_.end()) {
709 received_rrtrs_.erase(it->second);
710 received_rrtrs_ssrc_it_.erase(it);
711 }
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000712 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713}
714
danilchap1b1863a2016-09-20 01:39:54 -0700715void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700716 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700717 rtcp::ExtendedReports xr;
718 if (!xr.Parse(rtcp_block)) {
719 ++num_skipped_packets_;
720 return;
721 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000722
danilchap80ac24d2016-10-31 08:40:47 -0700723 if (xr.rrtr())
724 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000725
danilchap80ac24d2016-10-31 08:40:47 -0700726 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
727 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800728
sprangb32aaf92017-08-28 05:49:12 -0700729 if (xr.target_bitrate()) {
730 HandleXrTargetBitrate(xr.sender_ssrc(), *xr.target_bitrate(),
731 packet_information);
732 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000733}
734
danilchap8bab7962016-12-20 02:46:46 -0800735void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
736 const rtcp::Rrtr& rrtr) {
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200737 uint32_t received_remote_mid_ntp_time = CompactNtp(rrtr.ntp());
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200738 uint32_t local_receive_mid_ntp_time =
739 CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200740
741 auto it = received_rrtrs_ssrc_it_.find(sender_ssrc);
742 if (it != received_rrtrs_ssrc_it_.end()) {
743 it->second->received_remote_mid_ntp_time = received_remote_mid_ntp_time;
744 it->second->local_receive_mid_ntp_time = local_receive_mid_ntp_time;
745 } else {
746 if (received_rrtrs_.size() < kMaxNumberOfStoredRrtrs) {
747 received_rrtrs_.emplace_back(sender_ssrc, received_remote_mid_ntp_time,
748 local_receive_mid_ntp_time);
749 received_rrtrs_ssrc_it_[sender_ssrc] = std::prev(received_rrtrs_.end());
750 } else {
751 RTC_LOG(LS_WARNING) << "Discarding received RRTR for ssrc " << sender_ssrc
752 << ", reached maximum number of stored RRTRs.";
753 }
754 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000755}
756
danilchap92ea6012016-09-23 10:36:03 -0700757void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700758 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
759 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000760
danilchap1b1863a2016-09-20 01:39:54 -0700761 // Caller should explicitly enable rtt calculation using extended reports.
762 if (!xr_rrtr_status_)
763 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200764
danilchap1b1863a2016-09-20 01:39:54 -0700765 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800766 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700767 // RFC3611, section 4.5, LRR field discription states:
768 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800769 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700770 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200771
danilchap8bab7962016-12-20 02:46:46 -0800772 uint32_t delay_ntp = rti.delay_since_last_rr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200773 uint32_t now_ntp = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200774
danilchap8bab7962016-12-20 02:46:46 -0800775 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700776 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000777}
778
spranga790d832016-12-02 07:29:44 -0800779void RTCPReceiver::HandleXrTargetBitrate(
sprangb32aaf92017-08-28 05:49:12 -0700780 uint32_t ssrc,
spranga790d832016-12-02 07:29:44 -0800781 const rtcp::TargetBitrate& target_bitrate,
782 PacketInformation* packet_information) {
sprangb32aaf92017-08-28 05:49:12 -0700783 if (ssrc != remote_ssrc_) {
784 return; // Not for us.
785 }
786
Erik Språng566124a2018-04-23 12:32:22 +0200787 VideoBitrateAllocation bitrate_allocation;
spranga790d832016-12-02 07:29:44 -0800788 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800789 if (item.spatial_layer >= kMaxSpatialLayers ||
790 item.temporal_layer >= kMaxTemporalStreams) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100791 RTC_LOG(LS_WARNING)
sprang6d314c72016-12-06 06:08:53 -0800792 << "Invalid layer in XR target bitrate pack: spatial index "
793 << item.spatial_layer << ", temporal index " << item.temporal_layer
794 << ", dropping.";
795 } else {
796 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
797 item.target_bitrate_kbps * 1000);
798 }
spranga790d832016-12-02 07:29:44 -0800799 }
800 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
801}
802
danilchap8bab7962016-12-20 02:46:46 -0800803void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700804 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700805 rtcp::Pli pli;
806 if (!pli.Parse(rtcp_block)) {
807 ++num_skipped_packets_;
808 return;
809 }
810
811 if (main_ssrc_ == pli.media_ssrc()) {
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000812 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000813 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700814 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000815 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000816}
817
danilchap8bab7962016-12-20 02:46:46 -0800818void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700819 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700820 rtcp::Tmmbr tmmbr;
821 if (!tmmbr.Parse(rtcp_block)) {
822 ++num_skipped_packets_;
823 return;
824 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000825
danilchap7851bda2016-09-29 15:28:07 -0700826 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap1b1863a2016-09-20 01:39:54 -0700827 if (tmmbr.media_ssrc()) {
828 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
829 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700830 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000831 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000832
danilchap1b1863a2016-09-20 01:39:54 -0700833 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
danilchapec067e92017-02-21 05:38:19 -0800834 if (main_ssrc_ != request.ssrc() || request.bitrate_bps() == 0)
835 continue;
danilchap7851bda2016-09-29 15:28:07 -0700836
danilchapec067e92017-02-21 05:38:19 -0800837 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbr.sender_ssrc());
838 auto* entry = &tmmbr_info->tmmbr[sender_ssrc];
Yves Gerey665174f2018-06-19 15:03:05 +0200839 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc, request.bitrate_bps(),
danilchapec067e92017-02-21 05:38:19 -0800840 request.packet_overhead());
841 entry->last_updated_ms = clock_->TimeInMilliseconds();
842
843 packet_information->packet_type_flags |= kRtcpTmmbr;
844 break;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000845 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000846}
847
danilchap8bab7962016-12-20 02:46:46 -0800848void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700849 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700850 rtcp::Tmmbn tmmbn;
851 if (!tmmbn.Parse(rtcp_block)) {
852 ++num_skipped_packets_;
853 return;
854 }
855
danilchapec067e92017-02-21 05:38:19 -0800856 TmmbrInformation* tmmbr_info = FindOrCreateTmmbrInfo(tmmbn.sender_ssrc());
danilchap1b1863a2016-09-20 01:39:54 -0700857
danilchap92ea6012016-09-23 10:36:03 -0700858 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000859
danilchapec067e92017-02-21 05:38:19 -0800860 tmmbr_info->tmmbn = tmmbn.items();
danilchap1b1863a2016-09-20 01:39:54 -0700861}
862
danilchap8bab7962016-12-20 02:46:46 -0800863void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
864 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700865 rtcp::RapidResyncRequest sr_req;
866 if (!sr_req.Parse(rtcp_block)) {
867 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000868 return;
869 }
870
danilchap92ea6012016-09-23 10:36:03 -0700871 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000872}
873
danilchap1b1863a2016-09-20 01:39:54 -0700874void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700875 PacketInformation* packet_information) {
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100876 {
877 rtcp::Remb remb;
878 if (remb.Parse(rtcp_block)) {
879 packet_information->packet_type_flags |= kRtcpRemb;
880 packet_information->receiver_estimated_max_bitrate_bps =
881 remb.bitrate_bps();
882 return;
883 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000884 }
danilchap1b1863a2016-09-20 01:39:54 -0700885
Elad Alonf8e7ccb2019-01-24 12:38:36 +0100886 {
887 auto loss_notification = absl::make_unique<rtcp::LossNotification>();
888 if (loss_notification->Parse(rtcp_block)) {
889 packet_information->packet_type_flags |= kRtcpLossNotification;
890 packet_information->loss_notification = std::move(loss_notification);
891 return;
892 }
893 }
894
895 RTC_LOG(LS_WARNING) << "Unknown PSFB-APP packet.";
896
danilchap1b1863a2016-09-20 01:39:54 -0700897 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000898}
899
danilchap8bab7962016-12-20 02:46:46 -0800900void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700901 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700902 rtcp::Fir fir;
903 if (!fir.Parse(rtcp_block)) {
904 ++num_skipped_packets_;
905 return;
906 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000907
danilchap1b1863a2016-09-20 01:39:54 -0700908 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800909 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700910 if (main_ssrc_ != fir_request.ssrc)
911 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200912
913 ++packet_type_counter_.fir_packets;
914
danilchapefa966b2017-02-17 06:23:15 -0800915 int64_t now_ms = clock_->TimeInMilliseconds();
916 auto inserted = last_fir_.insert(std::make_pair(
917 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
918 if (!inserted.second) { // There was already an entry.
919 LastFirStatus* last_fir = &inserted.first->second;
920
danilchap7851bda2016-09-29 15:28:07 -0700921 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800922 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700923 continue;
924
danilchap7851bda2016-09-29 15:28:07 -0700925 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800926 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700927 continue;
928
danilchapefa966b2017-02-17 06:23:15 -0800929 last_fir->request_ms = now_ms;
930 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200931 }
danilchap7851bda2016-09-29 15:28:07 -0700932 // Received signal that we need to send a new key frame.
933 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000934 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000935}
936
Erik Språng6b8d3552015-09-24 15:06:57 +0200937void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700938 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700939 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700940 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
941 new rtcp::TransportFeedback());
942 if (!transport_feedback->Parse(rtcp_block)) {
943 ++num_skipped_packets_;
944 return;
945 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200946
danilchap92ea6012016-09-23 10:36:03 -0700947 packet_information->packet_type_flags |= kRtcpTransportFeedback;
948 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200949}
danilchap287e5482016-08-16 15:15:39 -0700950
danilchap9bf610e2017-02-20 06:03:01 -0800951void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700952 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700953 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700954 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000955
spranga790d832016-12-02 07:29:44 -0800956 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700957 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700958 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
959 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800960 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000961 }
danilchap853ecb22016-08-22 08:26:15 -0700962
danilchap9bf610e2017-02-20 06:03:01 -0800963 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800964 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000965}
966
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000967void RTCPReceiver::RegisterRtcpStatisticsCallback(
968 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800969 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000970 stats_callback_ = callback;
971}
972
973RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800974 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000975 return stats_callback_;
976}
977
danilchap8bab7962016-12-20 02:46:46 -0800978// Holding no Critical section.
979void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700980 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000981 // Process TMMBR and REMB first to avoid multiple callbacks
982 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700983 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000984 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800985 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000986 }
sprang7dc39f32015-10-13 09:17:48 -0700987 uint32_t local_ssrc;
988 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000989 {
990 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800991 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000992 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700993 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000994 }
danilchap92ea6012016-09-23 10:36:03 -0700995 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800996 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000997 }
danilchap92ea6012016-09-23 10:36:03 -0700998 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
999 if (!packet_information.nack_sequence_numbers.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001000 RTC_LOG(LS_VERBOSE) << "Incoming NACK length: "
1001 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -08001002 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +00001003 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001004 }
spranga790d832016-12-02 07:29:44 -08001005
1006 // We need feedback that we have received a report block(s) so that we
1007 // can generate a new packet in a conference relay scenario, one received
1008 // report can generate several RTCP packets, based on number relayed/mixed
1009 // a send report block should go out to all receivers.
1010 if (rtcp_intra_frame_observer_) {
1011 RTC_DCHECK(!receiver_only_);
1012 if ((packet_information.packet_type_flags & kRtcpPli) ||
1013 (packet_information.packet_type_flags & kRtcpFir)) {
1014 if (packet_information.packet_type_flags & kRtcpPli) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001015 RTC_LOG(LS_VERBOSE)
1016 << "Incoming PLI from SSRC " << packet_information.remote_ssrc;
spranga790d832016-12-02 07:29:44 -08001017 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001018 RTC_LOG(LS_VERBOSE)
1019 << "Incoming FIR from SSRC " << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001020 }
spranga790d832016-12-02 07:29:44 -08001021 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001022 }
spranga790d832016-12-02 07:29:44 -08001023 }
Elad Alon0a8562e2019-04-09 11:55:13 +02001024 if (rtcp_loss_notification_observer_ &&
1025 (packet_information.packet_type_flags & kRtcpLossNotification)) {
1026 rtcp::LossNotification* loss_notification =
1027 packet_information.loss_notification.get();
1028 RTC_DCHECK(loss_notification);
1029 if (loss_notification->media_ssrc() == local_ssrc) {
1030 rtcp_loss_notification_observer_->OnReceivedLossNotification(
1031 loss_notification->media_ssrc(), loss_notification->last_decoded(),
1032 loss_notification->last_received(),
1033 loss_notification->decodability_flag());
1034 }
1035 }
spranga790d832016-12-02 07:29:44 -08001036 if (rtcp_bandwidth_observer_) {
1037 RTC_DCHECK(!receiver_only_);
1038 if (packet_information.packet_type_flags & kRtcpRemb) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001039 RTC_LOG(LS_VERBOSE)
1040 << "Incoming REMB: "
1041 << packet_information.receiver_estimated_max_bitrate_bps;
spranga790d832016-12-02 07:29:44 -08001042 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1043 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001044 }
danilchap92ea6012016-09-23 10:36:03 -07001045 if ((packet_information.packet_type_flags & kRtcpSr) ||
1046 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001047 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001048 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001049 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001050 }
spranga790d832016-12-02 07:29:44 -08001051 }
1052 if ((packet_information.packet_type_flags & kRtcpSr) ||
1053 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001054 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001055 }
isheriff6b4b5f32016-06-08 00:24:21 -07001056
spranga790d832016-12-02 07:29:44 -08001057 if (transport_feedback_observer_ &&
1058 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1059 uint32_t media_source_ssrc =
1060 packet_information.transport_feedback->media_ssrc();
1061 if (media_source_ssrc == local_ssrc ||
1062 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1063 transport_feedback_observer_->OnTransportFeedback(
1064 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001065 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001066 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001067
spranga790d832016-12-02 07:29:44 -08001068 if (bitrate_allocation_observer_ &&
1069 packet_information.target_bitrate_allocation) {
1070 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1071 *packet_information.target_bitrate_allocation);
1072 }
1073
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001074 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001075 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001076 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001077 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001078 RtcpStatistics stats;
srte3e69e5c2017-08-09 06:13:45 -07001079 stats.packets_lost = report_block.packets_lost;
srte186d9c32017-08-04 05:03:53 -07001080 stats.extended_highest_sequence_number =
srte3e69e5c2017-08-09 06:13:45 -07001081 report_block.extended_highest_sequence_number;
1082 stats.fraction_lost = report_block.fraction_lost;
danilchap92ea6012016-09-23 10:36:03 -07001083 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001084
srte3e69e5c2017-08-09 06:13:45 -07001085 stats_callback_->StatisticsUpdated(stats, report_block.source_ssrc);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001086 }
1087 }
1088 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001089}
1090
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001091int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001092 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001093 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001094
danilchap8bab7962016-12-20 02:46:46 -08001095 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001096 auto received_cname_it = received_cnames_.find(remoteSSRC);
1097 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001098 return -1;
danilchap95321242016-09-27 07:05:32 -07001099
1100 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1101 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001102 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001103}
1104
danilchap7851bda2016-09-29 15:28:07 -07001105std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001106 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001107 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001108
danilchap8bab7962016-12-20 02:46:46 -08001109 int64_t now_ms = clock_->TimeInMilliseconds();
Jiawei Ou3587b832018-01-31 22:08:26 -08001110 int64_t timeout_ms = now_ms - kTmmbrTimeoutIntervalMs;
danilchap287e5482016-08-16 15:15:39 -07001111
danilchap9bf610e2017-02-20 06:03:01 -08001112 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001113 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001114 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001115 // Erase timeout entries.
1116 it = kv.second.tmmbr.erase(it);
1117 } else {
1118 candidates.push_back(it->second.tmmbr_item);
1119 ++it;
1120 }
1121 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001122 }
danilchap287e5482016-08-16 15:15:39 -07001123 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001124}
1125
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001126} // namespace webrtc