blob: 1105493b64f2688090335addaf11e668e4174445 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000011#include "webrtc/modules/rtp_rtcp/source/rtcp_receiver.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Peter Boströmfe7a80c2015-04-23 17:53:17 +020013#include <string.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
danilchap2f69ce92016-08-16 03:21:38 -070015#include <limits>
danilchap7851bda2016-09-29 15:28:07 -070016#include <map>
danilchap92ea6012016-09-23 10:36:03 -070017#include <memory>
18#include <utility>
danilchap7851bda2016-09-29 15:28:07 -070019#include <vector>
danilchap2f69ce92016-08-16 03:21:38 -070020
Peter Boströmfe7a80c2015-04-23 17:53:17 +020021#include "webrtc/base/checks.h"
Peter Boströmebc0b4e2015-10-28 16:39:33 +010022#include "webrtc/base/logging.h"
tommie4f96502015-10-20 23:00:48 -070023#include "webrtc/base/trace_event.h"
sprang6d314c72016-12-06 06:08:53 -080024#include "webrtc/common_types.h"
spranga790d832016-12-02 07:29:44 -080025#include "webrtc/common_video/include/video_bitrate_allocator.h"
danilchap1b1863a2016-09-20 01:39:54 -070026#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
27#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/common_header.h"
28#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
29#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
30#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/fir.h"
31#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
32#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/pli.h"
33#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rapid_resync_request.h"
34#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
35#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h"
36#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rpsi.h"
37#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
38#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
39#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sli.h"
40#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
41#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
Erik Språng6b8d3552015-09-24 15:06:57 +020042#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
danilchap84432382017-02-09 05:21:42 -080043#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010044#include "webrtc/modules/rtp_rtcp/source/time_util.h"
danilchap13deaad2016-05-24 13:25:27 -070045#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
Danil Chapovalova094fd12016-02-22 18:59:36 +010046#include "webrtc/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
Erik Språng6b8d3552015-09-24 15:06:57 +020057const int64_t kMaxWarningLogIntervalMs = 10000;
danilchapefa966b2017-02-17 06:23:15 -080058const int64_t kRtcpMinFrameLengthMs = 17;
Erik Språng6b8d3552015-09-24 15:06:57 +020059
danilchap1b1863a2016-09-20 01:39:54 -070060} // namespace
61
danilchap92ea6012016-09-23 10:36:03 -070062struct RTCPReceiver::PacketInformation {
63 uint32_t packet_type_flags = 0; // RTCPPacketTypeFlags bit field.
64
65 uint32_t remote_ssrc = 0;
66 std::vector<uint16_t> nack_sequence_numbers;
67 ReportBlockList report_blocks;
68 int64_t rtt_ms = 0;
69 uint8_t sli_picture_id = 0;
70 uint64_t rpsi_picture_id = 0;
71 uint32_t receiver_estimated_max_bitrate_bps = 0;
72 std::unique_ptr<rtcp::TransportFeedback> transport_feedback;
spranga790d832016-12-02 07:29:44 -080073 rtc::Optional<BitrateAllocation> target_bitrate_allocation;
danilchap92ea6012016-09-23 10:36:03 -070074};
75
danilchap9bf610e2017-02-20 06:03:01 -080076// Structure for handing TMMBR and TMMBN rtcp messages (RFC5104, section 3.5.4).
77struct RTCPReceiver::TmmbrInformation {
danilchap7851bda2016-09-29 15:28:07 -070078 struct TimedTmmbrItem {
79 rtcp::TmmbItem tmmbr_item;
80 int64_t last_updated_ms;
81 };
82
83 int64_t last_time_received_ms = 0;
84
danilchap7851bda2016-09-29 15:28:07 -070085 bool ready_for_delete = false;
86
87 std::vector<rtcp::TmmbItem> tmmbn;
88 std::map<uint32_t, TimedTmmbrItem> tmmbr;
89};
90
danilchap28b03eb2016-10-05 06:59:44 -070091struct RTCPReceiver::ReportBlockWithRtt {
92 RTCPReportBlock report_block;
93
94 int64_t last_rtt_ms = 0;
95 int64_t min_rtt_ms = 0;
96 int64_t max_rtt_ms = 0;
97 int64_t sum_rtt_ms = 0;
98 size_t num_rtts = 0;
99};
100
danilchapefa966b2017-02-17 06:23:15 -0800101struct RTCPReceiver::LastFirStatus {
102 LastFirStatus(int64_t now_ms, uint8_t sequence_number)
103 : request_ms(now_ms), sequence_number(sequence_number) {}
104 int64_t request_ms;
105 uint8_t sequence_number;
106};
107
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000108RTCPReceiver::RTCPReceiver(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000109 Clock* clock,
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200110 bool receiver_only,
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000111 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
mflodman@webrtc.org96abda02015-02-25 13:50:10 +0000112 RtcpBandwidthObserver* rtcp_bandwidth_observer,
113 RtcpIntraFrameObserver* rtcp_intra_frame_observer,
Erik Språng6b8d3552015-09-24 15:06:57 +0200114 TransportFeedbackObserver* transport_feedback_observer,
spranga790d832016-12-02 07:29:44 -0800115 VideoBitrateAllocationObserver* bitrate_allocation_observer,
danilchap59cb2bd2016-08-29 11:08:47 -0700116 ModuleRtpRtcp* owner)
danilchap8bab7962016-12-20 02:46:46 -0800117 : clock_(clock),
Peter Boströmfe7a80c2015-04-23 17:53:17 +0200118 receiver_only_(receiver_only),
danilchap8bab7962016-12-20 02:46:46 -0800119 rtp_rtcp_(owner),
spranga790d832016-12-02 07:29:44 -0800120 rtcp_bandwidth_observer_(rtcp_bandwidth_observer),
121 rtcp_intra_frame_observer_(rtcp_intra_frame_observer),
122 transport_feedback_observer_(transport_feedback_observer),
123 bitrate_allocation_observer_(bitrate_allocation_observer),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000124 main_ssrc_(0),
danilchap8bab7962016-12-20 02:46:46 -0800125 remote_ssrc_(0),
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100126 xr_rrtr_status_(false),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000127 xr_rr_rtt_ms_(0),
danilchap8bab7962016-12-20 02:46:46 -0800128 last_received_rr_ms_(0),
129 last_increased_sequence_number_ms_(0),
spranga790d832016-12-02 07:29:44 -0800130 stats_callback_(nullptr),
Erik Språng6b8d3552015-09-24 15:06:57 +0200131 packet_type_counter_observer_(packet_type_counter_observer),
132 num_skipped_packets_(0),
danilchap8bab7962016-12-20 02:46:46 -0800133 last_skipped_packets_warning_ms_(clock->TimeInMilliseconds()) {
134 RTC_DCHECK(owner);
135 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
danilchap28b03eb2016-10-05 06:59:44 -0700138RTCPReceiver::~RTCPReceiver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
danilchap59cb2bd2016-08-29 11:08:47 -0700140bool RTCPReceiver::IncomingPacket(const uint8_t* packet, size_t packet_size) {
danilchap1b1863a2016-09-20 01:39:54 -0700141 if (packet_size == 0) {
142 LOG(LS_WARNING) << "Incoming empty RTCP packet";
danilchap59cb2bd2016-08-29 11:08:47 -0700143 return false;
144 }
danilchap59cb2bd2016-08-29 11:08:47 -0700145
danilchap92ea6012016-09-23 10:36:03 -0700146 PacketInformation packet_information;
danilchap1b1863a2016-09-20 01:39:54 -0700147 if (!ParseCompoundPacket(packet, packet + packet_size, &packet_information))
148 return false;
danilchap8bab7962016-12-20 02:46:46 -0800149 TriggerCallbacksFromRtcpPacket(packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700150 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151}
152
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000153int64_t RTCPReceiver::LastReceivedReceiverReport() const {
danilchap8bab7962016-12-20 02:46:46 -0800154 rtc::CritScope lock(&rtcp_receiver_lock_);
stefanb33eed22017-02-02 03:57:02 -0800155 return last_received_rr_ms_;
stefan@webrtc.orgb5865072013-02-01 14:33:42 +0000156}
157
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000158void RTCPReceiver::SetRemoteSSRC(uint32_t ssrc) {
danilchap8bab7962016-12-20 02:46:46 -0800159 rtc::CritScope lock(&rtcp_receiver_lock_);
160 // New SSRC reset old reports.
161 memset(&remote_sender_info_, 0, sizeof(remote_sender_info_));
danilchap0b4b7272016-10-06 09:24:45 -0700162 last_received_sr_ntp_.Reset();
danilchap8bab7962016-12-20 02:46:46 -0800163 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000164}
165
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000166uint32_t RTCPReceiver::RemoteSSRC() const {
danilchap8bab7962016-12-20 02:46:46 -0800167 rtc::CritScope lock(&rtcp_receiver_lock_);
168 return remote_ssrc_;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000169}
170
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000171void RTCPReceiver::SetSsrcs(uint32_t main_ssrc,
172 const std::set<uint32_t>& registered_ssrcs) {
danilchap8bab7962016-12-20 02:46:46 -0800173 rtc::CritScope lock(&rtcp_receiver_lock_);
mflodman15d83572016-10-06 08:35:11 -0700174 main_ssrc_ = main_ssrc;
175 registered_ssrcs_ = registered_ssrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
danilchap28b03eb2016-10-05 06:59:44 -0700178int32_t RTCPReceiver::RTT(uint32_t remote_ssrc,
179 int64_t* last_rtt_ms,
180 int64_t* avg_rtt_ms,
181 int64_t* min_rtt_ms,
182 int64_t* max_rtt_ms) const {
danilchap8bab7962016-12-20 02:46:46 -0800183 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
danilchap28b03eb2016-10-05 06:59:44 -0700185 auto it = received_report_blocks_.find(main_ssrc_);
186 if (it == received_report_blocks_.end())
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000187 return -1;
danilchap28b03eb2016-10-05 06:59:44 -0700188
189 auto it_info = it->second.find(remote_ssrc);
190 if (it_info == it->second.end())
191 return -1;
192
193 const ReportBlockWithRtt* report_block = &it_info->second;
194
195 if (report_block->num_rtts == 0)
196 return -1;
197
198 if (last_rtt_ms)
199 *last_rtt_ms = report_block->last_rtt_ms;
200
201 if (avg_rtt_ms)
202 *avg_rtt_ms = report_block->sum_rtt_ms / report_block->num_rtts;
203
204 if (min_rtt_ms)
205 *min_rtt_ms = report_block->min_rtt_ms;
206
207 if (max_rtt_ms)
208 *max_rtt_ms = report_block->max_rtt_ms;
209
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100213void RTCPReceiver::SetRtcpXrRrtrStatus(bool enable) {
danilchap8bab7962016-12-20 02:46:46 -0800214 rtc::CritScope lock(&rtcp_receiver_lock_);
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100215 xr_rrtr_status_ = enable;
216}
217
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000218bool RTCPReceiver::GetAndResetXrRrRtt(int64_t* rtt_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800219 RTC_DCHECK(rtt_ms);
220 rtc::CritScope lock(&rtcp_receiver_lock_);
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000221 if (xr_rr_rtt_ms_ == 0) {
222 return false;
223 }
224 *rtt_ms = xr_rr_rtt_ms_;
225 xr_rr_rtt_ms_ = 0;
226 return true;
227}
228
danilchap8bab7962016-12-20 02:46:46 -0800229bool RTCPReceiver::NTP(uint32_t* received_ntp_secs,
230 uint32_t* received_ntp_frac,
231 uint32_t* rtcp_arrival_time_secs,
232 uint32_t* rtcp_arrival_time_frac,
danilchapda161d72016-08-19 07:29:46 -0700233 uint32_t* rtcp_timestamp) const {
danilchap8bab7962016-12-20 02:46:46 -0800234 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700235 if (!last_received_sr_ntp_.Valid())
236 return false;
237
238 // NTP from incoming SenderReport.
danilchap8bab7962016-12-20 02:46:46 -0800239 if (received_ntp_secs)
240 *received_ntp_secs = remote_sender_info_.NTPseconds;
241 if (received_ntp_frac)
242 *received_ntp_frac = remote_sender_info_.NTPfraction;
danilchap0b4b7272016-10-06 09:24:45 -0700243
244 // Rtp time from incoming SenderReport.
245 if (rtcp_timestamp)
danilchap8bab7962016-12-20 02:46:46 -0800246 *rtcp_timestamp = remote_sender_info_.RTPtimeStamp;
danilchap0b4b7272016-10-06 09:24:45 -0700247
248 // Local NTP time when we received a RTCP packet with a send block.
danilchap8bab7962016-12-20 02:46:46 -0800249 if (rtcp_arrival_time_secs)
250 *rtcp_arrival_time_secs = last_received_sr_ntp_.seconds();
251 if (rtcp_arrival_time_frac)
252 *rtcp_arrival_time_frac = last_received_sr_ntp_.fractions();
danilchap0b4b7272016-10-06 09:24:45 -0700253
danilchapda161d72016-08-19 07:29:46 -0700254 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255}
256
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000257bool RTCPReceiver::LastReceivedXrReferenceTimeInfo(
danilchap798896a2016-09-28 02:54:25 -0700258 rtcp::ReceiveTimeInfo* info) const {
danilchap0b4b7272016-10-06 09:24:45 -0700259 RTC_DCHECK(info);
danilchap8bab7962016-12-20 02:46:46 -0800260 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700261 if (!last_received_xr_ntp_.Valid())
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000262 return false;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000263
danilchap798896a2016-09-28 02:54:25 -0700264 info->ssrc = remote_time_info_.ssrc;
265 info->last_rr = remote_time_info_.last_rr;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000266
267 // Get the delay since last received report (RFC 3611).
danilchap8bab7962016-12-20 02:46:46 -0800268 uint32_t receive_time_ntp = CompactNtp(last_received_xr_ntp_);
danilchap37953762017-02-09 11:15:25 -0800269 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000270
danilchap8bab7962016-12-20 02:46:46 -0800271 info->delay_since_last_rr = now_ntp - receive_time_ntp;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000272 return true;
273}
274
danilchap8bab7962016-12-20 02:46:46 -0800275int32_t RTCPReceiver::SenderInfoReceived(RTCPSenderInfo* sender_info) const {
276 RTC_DCHECK(sender_info);
277 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap0b4b7272016-10-06 09:24:45 -0700278 if (!last_received_sr_ntp_.Valid())
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000279 return -1;
danilchap0b4b7272016-10-06 09:24:45 -0700280
danilchap8bab7962016-12-20 02:46:46 -0800281 memcpy(sender_info, &remote_sender_info_, sizeof(RTCPSenderInfo));
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000282 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
danilchap28b03eb2016-10-05 06:59:44 -0700285// We can get multiple receive reports when we receive the report from a CE.
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000286int32_t RTCPReceiver::StatisticsReceived(
danilchap28b03eb2016-10-05 06:59:44 -0700287 std::vector<RTCPReportBlock>* receive_blocks) const {
288 RTC_DCHECK(receive_blocks);
danilchap8bab7962016-12-20 02:46:46 -0800289 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap28b03eb2016-10-05 06:59:44 -0700290 for (const auto& reports_per_receiver : received_report_blocks_)
291 for (const auto& report : reports_per_receiver.second)
292 receive_blocks->push_back(report.second.report_block);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000293 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000294}
295
danilchap92ea6012016-09-23 10:36:03 -0700296bool RTCPReceiver::ParseCompoundPacket(const uint8_t* packet_begin,
297 const uint8_t* packet_end,
298 PacketInformation* packet_information) {
danilchap8bab7962016-12-20 02:46:46 -0800299 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000300
danilchap1b1863a2016-09-20 01:39:54 -0700301 CommonHeader rtcp_block;
302 for (const uint8_t* next_block = packet_begin; next_block != packet_end;
303 next_block = rtcp_block.NextPacket()) {
304 ptrdiff_t remaining_blocks_size = packet_end - next_block;
305 RTC_DCHECK_GT(remaining_blocks_size, 0);
306 if (!rtcp_block.Parse(next_block, remaining_blocks_size)) {
307 if (next_block == packet_begin) {
308 // Failed to parse 1st header, nothing was extracted from this packet.
309 LOG(LS_WARNING) << "Incoming invalid RTCP packet";
310 return false;
311 }
312 ++num_skipped_packets_;
313 break;
314 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000315
danilchap1b1863a2016-09-20 01:39:54 -0700316 if (packet_type_counter_.first_packet_time_ms == -1)
danilchap8bab7962016-12-20 02:46:46 -0800317 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
danilchapda161d72016-08-19 07:29:46 -0700318
danilchap1b1863a2016-09-20 01:39:54 -0700319 switch (rtcp_block.type()) {
320 case rtcp::SenderReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700321 HandleSenderReport(rtcp_block, packet_information);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200322 break;
danilchap1b1863a2016-09-20 01:39:54 -0700323 case rtcp::ReceiverReport::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700324 HandleReceiverReport(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700325 break;
danilchap1b1863a2016-09-20 01:39:54 -0700326 case rtcp::Sdes::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800327 HandleSdes(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700328 break;
danilchap1b1863a2016-09-20 01:39:54 -0700329 case rtcp::ExtendedReports::kPacketType:
danilchap92ea6012016-09-23 10:36:03 -0700330 HandleXr(rtcp_block, packet_information);
danilchapda161d72016-08-19 07:29:46 -0700331 break;
danilchap1b1863a2016-09-20 01:39:54 -0700332 case rtcp::Bye::kPacketType:
danilchap8bab7962016-12-20 02:46:46 -0800333 HandleBye(rtcp_block);
danilchapda161d72016-08-19 07:29:46 -0700334 break;
danilchap1b1863a2016-09-20 01:39:54 -0700335 case rtcp::Rtpfb::kPacketType:
336 switch (rtcp_block.fmt()) {
337 case rtcp::Nack::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800338 HandleNack(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700339 break;
340 case rtcp::Tmmbr::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800341 HandleTmmbr(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700342 break;
343 case rtcp::Tmmbn::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800344 HandleTmmbn(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700345 break;
346 case rtcp::RapidResyncRequest::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800347 HandleSrReq(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700348 break;
349 case rtcp::TransportFeedback::kFeedbackMessageType:
350 HandleTransportFeedback(rtcp_block, packet_information);
351 break;
352 default:
353 ++num_skipped_packets_;
354 break;
355 }
danilchapda161d72016-08-19 07:29:46 -0700356 break;
danilchap1b1863a2016-09-20 01:39:54 -0700357 case rtcp::Psfb::kPacketType:
358 switch (rtcp_block.fmt()) {
359 case rtcp::Pli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800360 HandlePli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700361 break;
362 case rtcp::Sli::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800363 HandleSli(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700364 break;
365 case rtcp::Rpsi::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800366 HandleRpsi(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700367 break;
368 case rtcp::Fir::kFeedbackMessageType:
danilchap8bab7962016-12-20 02:46:46 -0800369 HandleFir(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700370 break;
371 case rtcp::Remb::kFeedbackMessageType:
danilchap92ea6012016-09-23 10:36:03 -0700372 HandlePsfbApp(rtcp_block, packet_information);
danilchap1b1863a2016-09-20 01:39:54 -0700373 break;
374 default:
375 ++num_skipped_packets_;
376 break;
377 }
danilchapda161d72016-08-19 07:29:46 -0700378 break;
379 default:
danilchap1b1863a2016-09-20 01:39:54 -0700380 ++num_skipped_packets_;
danilchapda161d72016-08-19 07:29:46 -0700381 break;
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000382 }
danilchapda161d72016-08-19 07:29:46 -0700383 }
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000384
danilchap8bab7962016-12-20 02:46:46 -0800385 if (packet_type_counter_observer_) {
danilchapda161d72016-08-19 07:29:46 -0700386 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
387 main_ssrc_, packet_type_counter_);
388 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000389
danilchap8bab7962016-12-20 02:46:46 -0800390 int64_t now_ms = clock_->TimeInMilliseconds();
391 if (now_ms - last_skipped_packets_warning_ms_ >= kMaxWarningLogIntervalMs &&
danilchapda161d72016-08-19 07:29:46 -0700392 num_skipped_packets_ > 0) {
danilchap8bab7962016-12-20 02:46:46 -0800393 last_skipped_packets_warning_ms_ = now_ms;
danilchapda161d72016-08-19 07:29:46 -0700394 LOG(LS_WARNING) << num_skipped_packets_
395 << " RTCP blocks were skipped due to being malformed or of "
396 "unrecognized/unsupported type, during the past "
397 << (kMaxWarningLogIntervalMs / 1000) << " second period.";
398 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200399
danilchap1b1863a2016-09-20 01:39:54 -0700400 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000401}
402
danilchap92ea6012016-09-23 10:36:03 -0700403void RTCPReceiver::HandleSenderReport(const CommonHeader& rtcp_block,
404 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700405 rtcp::SenderReport sender_report;
406 if (!sender_report.Parse(rtcp_block)) {
407 ++num_skipped_packets_;
408 return;
409 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
danilchap8bab7962016-12-20 02:46:46 -0800411 const uint32_t remote_ssrc = sender_report.sender_ssrc();
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
danilchap8bab7962016-12-20 02:46:46 -0800413 packet_information->remote_ssrc = remote_ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
danilchap9bf610e2017-02-20 06:03:01 -0800415 CreateTmmbrInformation(remote_ssrc);
danilchapda161d72016-08-19 07:29:46 -0700416
Danil Chapovalov91511f12016-09-15 16:24:02 +0200417 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "SR",
danilchap8bab7962016-12-20 02:46:46 -0800418 "remote_ssrc", remote_ssrc, "ssrc", main_ssrc_);
danilchapda161d72016-08-19 07:29:46 -0700419
Danil Chapovalov91511f12016-09-15 16:24:02 +0200420 // Have I received RTP packets from this party?
danilchap8bab7962016-12-20 02:46:46 -0800421 if (remote_ssrc_ == remote_ssrc) {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200422 // Only signal that we have received a SR when we accept one.
danilchap92ea6012016-09-23 10:36:03 -0700423 packet_information->packet_type_flags |= kRtcpSr;
danilchapda161d72016-08-19 07:29:46 -0700424
Danil Chapovalov91511f12016-09-15 16:24:02 +0200425 // Save the NTP time of this report.
danilchap8bab7962016-12-20 02:46:46 -0800426 remote_sender_info_.NTPseconds = sender_report.ntp().seconds();
427 remote_sender_info_.NTPfraction = sender_report.ntp().fractions();
428 remote_sender_info_.RTPtimeStamp = sender_report.rtp_timestamp();
429 remote_sender_info_.sendPacketCount = sender_report.sender_packet_count();
430 remote_sender_info_.sendOctetCount = sender_report.sender_octet_count();
danilchapda161d72016-08-19 07:29:46 -0700431
danilchap37953762017-02-09 11:15:25 -0800432 last_received_sr_ntp_ = clock_->CurrentNtpTime();
danilchapda161d72016-08-19 07:29:46 -0700433 } else {
Danil Chapovalov91511f12016-09-15 16:24:02 +0200434 // We will only store the send report from one source, but
435 // we will store all the receive blocks.
danilchap92ea6012016-09-23 10:36:03 -0700436 packet_information->packet_type_flags |= kRtcpRr;
danilchapda161d72016-08-19 07:29:46 -0700437 }
elham@webrtc.orgb7eda432013-07-15 21:08:27 +0000438
danilchap1b1863a2016-09-20 01:39:54 -0700439 for (const rtcp::ReportBlock report_block : sender_report.report_blocks())
danilchap8bab7962016-12-20 02:46:46 -0800440 HandleReportBlock(report_block, packet_information, remote_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000441}
442
danilchap92ea6012016-09-23 10:36:03 -0700443void RTCPReceiver::HandleReceiverReport(const CommonHeader& rtcp_block,
444 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700445 rtcp::ReceiverReport receiver_report;
446 if (!receiver_report.Parse(rtcp_block)) {
447 ++num_skipped_packets_;
448 return;
449 }
Danil Chapovalov91511f12016-09-15 16:24:02 +0200450
stefanb33eed22017-02-02 03:57:02 -0800451 last_received_rr_ms_ = clock_->TimeInMilliseconds();
452
danilchap8bab7962016-12-20 02:46:46 -0800453 const uint32_t remote_ssrc = receiver_report.sender_ssrc();
Danil Chapovalov91511f12016-09-15 16:24:02 +0200454
danilchap8bab7962016-12-20 02:46:46 -0800455 packet_information->remote_ssrc = remote_ssrc;
Danil Chapovalov91511f12016-09-15 16:24:02 +0200456
danilchap9bf610e2017-02-20 06:03:01 -0800457 CreateTmmbrInformation(remote_ssrc);
Danil Chapovalov91511f12016-09-15 16:24:02 +0200458
459 TRACE_EVENT_INSTANT2(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR",
danilchap8bab7962016-12-20 02:46:46 -0800460 "remote_ssrc", remote_ssrc, "ssrc", main_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
danilchap28b03eb2016-10-05 06:59:44 -0700484 ReportBlockWithRtt* report_block_info =
485 &received_report_blocks_[report_block.source_ssrc()][remote_ssrc];
danilchap28b03eb2016-10-05 06:59:44 -0700486 report_block_info->report_block.remoteSSRC = remote_ssrc;
487 report_block_info->report_block.sourceSSRC = report_block.source_ssrc();
488 report_block_info->report_block.fractionLost = report_block.fraction_lost();
489 report_block_info->report_block.cumulativeLost =
danilchap1b1863a2016-09-20 01:39:54 -0700490 report_block.cumulative_lost();
491 if (report_block.extended_high_seq_num() >
danilchap28b03eb2016-10-05 06:59:44 -0700492 report_block_info->report_block.extendedHighSeqNum) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000493 // We have successfully delivered new RTP packets to the remote side after
494 // the last RR was sent from the remote side.
stefanb33eed22017-02-02 03:57:02 -0800495 last_increased_sequence_number_ms_ = clock_->TimeInMilliseconds();
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000496 }
danilchap28b03eb2016-10-05 06:59:44 -0700497 report_block_info->report_block.extendedHighSeqNum =
danilchap1b1863a2016-09-20 01:39:54 -0700498 report_block.extended_high_seq_num();
danilchap28b03eb2016-10-05 06:59:44 -0700499 report_block_info->report_block.jitter = report_block.jitter();
500 report_block_info->report_block.delaySinceLastSR =
danilchap1b1863a2016-09-20 01:39:54 -0700501 report_block.delay_since_last_sr();
danilchap28b03eb2016-10-05 06:59:44 -0700502 report_block_info->report_block.lastSR = report_block.last_sr();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000503
danilchap28b03eb2016-10-05 06:59:44 -0700504 int64_t rtt_ms = 0;
danilchap8bab7962016-12-20 02:46:46 -0800505 uint32_t send_time_ntp = report_block.last_sr();
Danil Chapovalovc1e55c72016-03-09 15:14:35 +0100506 // RFC3550, section 6.4.1, LSR field discription states:
507 // If no SR has been received yet, the field is set to zero.
508 // Receiver rtp_rtcp module is not expected to calculate rtt using
509 // Sender Reports even if it accidentally can.
danilchap8bab7962016-12-20 02:46:46 -0800510 if (!receiver_only_ && send_time_ntp != 0) {
511 uint32_t delay_ntp = report_block.delay_since_last_sr();
Danil Chapovalova094fd12016-02-22 18:59:36 +0100512 // Local NTP time.
danilchap37953762017-02-09 11:15:25 -0800513 uint32_t receive_time_ntp = CompactNtp(clock_->CurrentNtpTime());
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000514
Danil Chapovalova094fd12016-02-22 18:59:36 +0100515 // RTT in 1/(2^16) seconds.
danilchap8bab7962016-12-20 02:46:46 -0800516 uint32_t rtt_ntp = receive_time_ntp - delay_ntp - send_time_ntp;
Danil Chapovalova094fd12016-02-22 18:59:36 +0100517 // Convert to 1/1000 seconds (milliseconds).
danilchap28b03eb2016-10-05 06:59:44 -0700518 rtt_ms = CompactNtpRttToMs(rtt_ntp);
519 if (rtt_ms > report_block_info->max_rtt_ms)
520 report_block_info->max_rtt_ms = rtt_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +0000521
danilchap28b03eb2016-10-05 06:59:44 -0700522 if (report_block_info->num_rtts == 0 ||
523 rtt_ms < report_block_info->min_rtt_ms)
524 report_block_info->min_rtt_ms = rtt_ms;
525
526 report_block_info->last_rtt_ms = rtt_ms;
527 report_block_info->sum_rtt_ms += rtt_ms;
528 ++report_block_info->num_rtts;
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000529 }
530
danilchap1b1863a2016-09-20 01:39:54 -0700531 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RR_RTT",
danilchap28b03eb2016-10-05 06:59:44 -0700532 report_block.source_ssrc(), rtt_ms);
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +0000533
danilchap28b03eb2016-10-05 06:59:44 -0700534 packet_information->rtt_ms = rtt_ms;
535 packet_information->report_blocks.push_back(report_block_info->report_block);
niklase@google.com470e71d2011-07-07 08:21:25 +0000536}
537
danilchap9bf610e2017-02-20 06:03:01 -0800538void RTCPReceiver::CreateTmmbrInformation(uint32_t remote_ssrc) {
danilchap7851bda2016-09-29 15:28:07 -0700539 // Create or find receive information.
danilchap9bf610e2017-02-20 06:03:01 -0800540 TmmbrInformation* tmmbr_info = &tmmbr_infos_[remote_ssrc];
danilchap7851bda2016-09-29 15:28:07 -0700541 // Update that this remote is alive.
danilchap9bf610e2017-02-20 06:03:01 -0800542 tmmbr_info->last_time_received_ms = clock_->TimeInMilliseconds();
niklase@google.com470e71d2011-07-07 08:21:25 +0000543}
544
danilchap9bf610e2017-02-20 06:03:01 -0800545RTCPReceiver::TmmbrInformation* RTCPReceiver::GetTmmbrInformation(
danilchap7851bda2016-09-29 15:28:07 -0700546 uint32_t remote_ssrc) {
danilchap9bf610e2017-02-20 06:03:01 -0800547 auto it = tmmbr_infos_.find(remote_ssrc);
548 if (it == tmmbr_infos_.end())
danilchap7851bda2016-09-29 15:28:07 -0700549 return nullptr;
550 return &it->second;
niklase@google.com470e71d2011-07-07 08:21:25 +0000551}
552
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000553bool RTCPReceiver::RtcpRrTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800554 rtc::CritScope lock(&rtcp_receiver_lock_);
555 if (last_received_rr_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000556 return false;
557
558 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800559 if (clock_->TimeInMilliseconds() > last_received_rr_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000560 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800561 last_received_rr_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000562 return true;
563 }
564 return false;
565}
566
567bool RTCPReceiver::RtcpRrSequenceNumberTimeout(int64_t rtcp_interval_ms) {
danilchap8bab7962016-12-20 02:46:46 -0800568 rtc::CritScope lock(&rtcp_receiver_lock_);
569 if (last_increased_sequence_number_ms_ == 0)
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000570 return false;
571
572 int64_t time_out_ms = kRrTimeoutIntervals * rtcp_interval_ms;
danilchap8bab7962016-12-20 02:46:46 -0800573 if (clock_->TimeInMilliseconds() >
574 last_increased_sequence_number_ms_ + time_out_ms) {
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000575 // Reset the timer to only trigger one log.
danilchap8bab7962016-12-20 02:46:46 -0800576 last_increased_sequence_number_ms_ = 0;
mflodman@webrtc.org2f225ca2013-01-09 13:54:43 +0000577 return true;
578 }
579 return false;
580}
581
danilchap9bf610e2017-02-20 06:03:01 -0800582bool RTCPReceiver::UpdateTmmbrTimers() {
danilchap8bab7962016-12-20 02:46:46 -0800583 rtc::CritScope lock(&rtcp_receiver_lock_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000584
danilchap8bab7962016-12-20 02:46:46 -0800585 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700586 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -0800587 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
danilchap9bf610e2017-02-20 06:03:01 -0800589 if (oldest_tmmbr_info_ms_ >= timeout_ms)
stefanb33eed22017-02-02 03:57:02 -0800590 return false;
591
592 bool update_bounding_set = false;
danilchap9bf610e2017-02-20 06:03:01 -0800593 oldest_tmmbr_info_ms_ = -1;
594 for (auto tmmbr_it = tmmbr_infos_.begin(); tmmbr_it != tmmbr_infos_.end();) {
595 TmmbrInformation* tmmbr_info = &tmmbr_it->second;
596 if (tmmbr_info->last_time_received_ms > 0) {
597 if (tmmbr_info->last_time_received_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -0700598 // No rtcp packet for the last 5 regular intervals, reset limitations.
danilchap9bf610e2017-02-20 06:03:01 -0800599 tmmbr_info->tmmbr.clear();
danilchap7851bda2016-09-29 15:28:07 -0700600 // Prevent that we call this over and over again.
danilchap9bf610e2017-02-20 06:03:01 -0800601 tmmbr_info->last_time_received_ms = 0;
danilchap7851bda2016-09-29 15:28:07 -0700602 // Send new TMMBN to all channels using the default codec.
603 update_bounding_set = true;
danilchap9bf610e2017-02-20 06:03:01 -0800604 } else if (oldest_tmmbr_info_ms_ == -1 ||
605 tmmbr_info->last_time_received_ms < oldest_tmmbr_info_ms_) {
606 oldest_tmmbr_info_ms_ = tmmbr_info->last_time_received_ms;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000607 }
danilchap9bf610e2017-02-20 06:03:01 -0800608 ++tmmbr_it;
609 } else if (tmmbr_info->ready_for_delete) {
danilchap7851bda2016-09-29 15:28:07 -0700610 // When we dont have a last_time_received_ms and the object is marked
611 // ready_for_delete it's removed from the map.
danilchap9bf610e2017-02-20 06:03:01 -0800612 tmmbr_it = tmmbr_infos_.erase(tmmbr_it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000613 } else {
danilchap9bf610e2017-02-20 06:03:01 -0800614 ++tmmbr_it;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000615 }
616 }
danilchap7851bda2016-09-29 15:28:07 -0700617 return update_bounding_set;
niklase@google.com470e71d2011-07-07 08:21:25 +0000618}
619
danilchap2b616392016-08-18 06:17:42 -0700620std::vector<rtcp::TmmbItem> RTCPReceiver::BoundingSet(bool* tmmbr_owner) {
danilchap8bab7962016-12-20 02:46:46 -0800621 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap9bf610e2017-02-20 06:03:01 -0800622 TmmbrInformation* tmmbr_info = GetTmmbrInformation(remote_ssrc_);
623 if (!tmmbr_info)
danilchap2b616392016-08-18 06:17:42 -0700624 return std::vector<rtcp::TmmbItem>();
danilchap2b616392016-08-18 06:17:42 -0700625
danilchap9bf610e2017-02-20 06:03:01 -0800626 *tmmbr_owner = TMMBRHelp::IsOwner(tmmbr_info->tmmbn, main_ssrc_);
627 return tmmbr_info->tmmbn;
niklase@google.com470e71d2011-07-07 08:21:25 +0000628}
629
danilchap8bab7962016-12-20 02:46:46 -0800630void RTCPReceiver::HandleSdes(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700631 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700632 rtcp::Sdes sdes;
633 if (!sdes.Parse(rtcp_block)) {
634 ++num_skipped_packets_;
635 return;
636 }
637
638 for (const rtcp::Sdes::Chunk& chunk : sdes.chunks()) {
danilchap95321242016-09-27 07:05:32 -0700639 received_cnames_[chunk.ssrc] = chunk.cname;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200640 {
spranga790d832016-12-02 07:29:44 -0800641 rtc::CritScope lock(&feedbacks_lock_);
danilchap1b1863a2016-09-20 01:39:54 -0700642 if (stats_callback_)
643 stats_callback_->CNameChanged(chunk.cname.c_str(), chunk.ssrc);
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200644 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000645 }
danilchap92ea6012016-09-23 10:36:03 -0700646 packet_information->packet_type_flags |= kRtcpSdes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000647}
648
danilchap8bab7962016-12-20 02:46:46 -0800649void RTCPReceiver::HandleNack(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700650 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700651 rtcp::Nack nack;
652 if (!nack.Parse(rtcp_block)) {
653 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000654 return;
655 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000656
danilchap1b1863a2016-09-20 01:39:54 -0700657 if (receiver_only_ || main_ssrc_ != nack.media_ssrc()) // Not to us.
658 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200659
danilchap142f0192016-10-20 08:22:42 -0700660 packet_information->nack_sequence_numbers.insert(
661 packet_information->nack_sequence_numbers.end(),
662 nack.packet_ids().begin(), nack.packet_ids().end());
danilchap1b1863a2016-09-20 01:39:54 -0700663 for (uint16_t packet_id : nack.packet_ids())
664 nack_stats_.ReportRequest(packet_id);
665
666 if (!nack.packet_ids().empty()) {
danilchap92ea6012016-09-23 10:36:03 -0700667 packet_information->packet_type_flags |= kRtcpNack;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000668 ++packet_type_counter_.nack_packets;
669 packet_type_counter_.nack_requests = nack_stats_.requests();
670 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
671 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000672}
673
danilchap8bab7962016-12-20 02:46:46 -0800674void RTCPReceiver::HandleBye(const CommonHeader& rtcp_block) {
danilchap1b1863a2016-09-20 01:39:54 -0700675 rtcp::Bye bye;
676 if (!bye.Parse(rtcp_block)) {
677 ++num_skipped_packets_;
678 return;
679 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000680
danilchap8bab7962016-12-20 02:46:46 -0800681 // Clear our lists.
danilchap28b03eb2016-10-05 06:59:44 -0700682 for (auto& reports_per_receiver : received_report_blocks_)
683 reports_per_receiver.second.erase(bye.sender_ssrc());
asapersson@webrtc.orgcb791412014-12-18 14:30:32 +0000684
danilchap9bf610e2017-02-20 06:03:01 -0800685 TmmbrInformation* tmmbr_info = GetTmmbrInformation(bye.sender_ssrc());
686 if (tmmbr_info)
687 tmmbr_info->ready_for_delete = true;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000688
danilchapefa966b2017-02-17 06:23:15 -0800689 last_fir_.erase(bye.sender_ssrc());
danilchap95321242016-09-27 07:05:32 -0700690 received_cnames_.erase(bye.sender_ssrc());
asapersson@webrtc.org7d6bd222013-10-31 12:14:34 +0000691 xr_rr_rtt_ms_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000692}
693
danilchap1b1863a2016-09-20 01:39:54 -0700694void RTCPReceiver::HandleXr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700695 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700696 rtcp::ExtendedReports xr;
697 if (!xr.Parse(rtcp_block)) {
698 ++num_skipped_packets_;
699 return;
700 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000701
danilchap80ac24d2016-10-31 08:40:47 -0700702 if (xr.rrtr())
703 HandleXrReceiveReferenceTime(xr.sender_ssrc(), *xr.rrtr());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000704
danilchap80ac24d2016-10-31 08:40:47 -0700705 for (const rtcp::ReceiveTimeInfo& time_info : xr.dlrr().sub_blocks())
706 HandleXrDlrrReportBlock(time_info);
spranga790d832016-12-02 07:29:44 -0800707
708 if (xr.target_bitrate())
709 HandleXrTargetBitrate(*xr.target_bitrate(), packet_information);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000710}
711
danilchap8bab7962016-12-20 02:46:46 -0800712void RTCPReceiver::HandleXrReceiveReferenceTime(uint32_t sender_ssrc,
713 const rtcp::Rrtr& rrtr) {
danilchap798896a2016-09-28 02:54:25 -0700714 remote_time_info_.ssrc = sender_ssrc;
715 remote_time_info_.last_rr = CompactNtp(rrtr.ntp());
danilchap37953762017-02-09 11:15:25 -0800716 last_received_xr_ntp_ = clock_->CurrentNtpTime();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000717}
718
danilchap92ea6012016-09-23 10:36:03 -0700719void RTCPReceiver::HandleXrDlrrReportBlock(const rtcp::ReceiveTimeInfo& rti) {
danilchap1b1863a2016-09-20 01:39:54 -0700720 if (registered_ssrcs_.count(rti.ssrc) == 0) // Not to us.
721 return;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000722
danilchap1b1863a2016-09-20 01:39:54 -0700723 // Caller should explicitly enable rtt calculation using extended reports.
724 if (!xr_rrtr_status_)
725 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200726
danilchap1b1863a2016-09-20 01:39:54 -0700727 // The send_time and delay_rr fields are in units of 1/2^16 sec.
danilchap8bab7962016-12-20 02:46:46 -0800728 uint32_t send_time_ntp = rti.last_rr;
danilchap1b1863a2016-09-20 01:39:54 -0700729 // RFC3611, section 4.5, LRR field discription states:
730 // If no such block has been received, the field is set to zero.
danilchap8bab7962016-12-20 02:46:46 -0800731 if (send_time_ntp == 0)
danilchap1b1863a2016-09-20 01:39:54 -0700732 return;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200733
danilchap8bab7962016-12-20 02:46:46 -0800734 uint32_t delay_ntp = rti.delay_since_last_rr;
danilchap37953762017-02-09 11:15:25 -0800735 uint32_t now_ntp = CompactNtp(clock_->CurrentNtpTime());
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200736
danilchap8bab7962016-12-20 02:46:46 -0800737 uint32_t rtt_ntp = now_ntp - delay_ntp - send_time_ntp;
danilchap1b1863a2016-09-20 01:39:54 -0700738 xr_rr_rtt_ms_ = CompactNtpRttToMs(rtt_ntp);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000739}
740
spranga790d832016-12-02 07:29:44 -0800741void RTCPReceiver::HandleXrTargetBitrate(
742 const rtcp::TargetBitrate& target_bitrate,
743 PacketInformation* packet_information) {
744 BitrateAllocation bitrate_allocation;
745 for (const auto& item : target_bitrate.GetTargetBitrates()) {
sprang6d314c72016-12-06 06:08:53 -0800746 if (item.spatial_layer >= kMaxSpatialLayers ||
747 item.temporal_layer >= kMaxTemporalStreams) {
748 LOG(LS_WARNING)
749 << "Invalid layer in XR target bitrate pack: spatial index "
750 << item.spatial_layer << ", temporal index " << item.temporal_layer
751 << ", dropping.";
752 } else {
753 bitrate_allocation.SetBitrate(item.spatial_layer, item.temporal_layer,
754 item.target_bitrate_kbps * 1000);
755 }
spranga790d832016-12-02 07:29:44 -0800756 }
757 packet_information->target_bitrate_allocation.emplace(bitrate_allocation);
758}
759
danilchap8bab7962016-12-20 02:46:46 -0800760void RTCPReceiver::HandlePli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700761 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700762 rtcp::Pli pli;
763 if (!pli.Parse(rtcp_block)) {
764 ++num_skipped_packets_;
765 return;
766 }
767
768 if (main_ssrc_ == pli.media_ssrc()) {
sprang@webrtc.org0200f702015-02-16 12:06:00 +0000769 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "PLI");
justinlin@chromium.org7bfb3a32013-05-13 22:59:00 +0000770
asapersson@webrtc.org8098e072014-02-19 11:59:02 +0000771 ++packet_type_counter_.pli_packets;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000772 // Received a signal that we need to send a new key frame.
danilchap92ea6012016-09-23 10:36:03 -0700773 packet_information->packet_type_flags |= kRtcpPli;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000774 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000775}
776
danilchap8bab7962016-12-20 02:46:46 -0800777void RTCPReceiver::HandleTmmbr(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700778 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700779 rtcp::Tmmbr tmmbr;
780 if (!tmmbr.Parse(rtcp_block)) {
781 ++num_skipped_packets_;
782 return;
783 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
danilchap7851bda2016-09-29 15:28:07 -0700785 uint32_t sender_ssrc = tmmbr.sender_ssrc();
danilchap9bf610e2017-02-20 06:03:01 -0800786 TmmbrInformation* receive_info = GetTmmbrInformation(sender_ssrc);
danilchap7851bda2016-09-29 15:28:07 -0700787 if (!receive_info) // This remote SSRC must be saved before.
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000788 return;
danilchap1b1863a2016-09-20 01:39:54 -0700789
790 if (tmmbr.media_ssrc()) {
791 // media_ssrc() SHOULD be 0 if same as SenderSSRC.
792 // In relay mode this is a valid number.
danilchap7851bda2016-09-29 15:28:07 -0700793 sender_ssrc = tmmbr.media_ssrc();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000794 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000795
danilchap1b1863a2016-09-20 01:39:54 -0700796 for (const rtcp::TmmbItem& request : tmmbr.requests()) {
797 if (main_ssrc_ == request.ssrc() && request.bitrate_bps()) {
danilchap7851bda2016-09-29 15:28:07 -0700798 auto* entry = &receive_info->tmmbr[sender_ssrc];
799 entry->tmmbr_item = rtcp::TmmbItem(sender_ssrc,
800 request.bitrate_bps(),
801 request.packet_overhead());
danilchap8bab7962016-12-20 02:46:46 -0800802 entry->last_updated_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -0700803
danilchap92ea6012016-09-23 10:36:03 -0700804 packet_information->packet_type_flags |= kRtcpTmmbr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200805 }
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000806 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
danilchap8bab7962016-12-20 02:46:46 -0800809void RTCPReceiver::HandleTmmbn(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700810 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700811 rtcp::Tmmbn tmmbn;
812 if (!tmmbn.Parse(rtcp_block)) {
813 ++num_skipped_packets_;
814 return;
815 }
816
danilchap9bf610e2017-02-20 06:03:01 -0800817 TmmbrInformation* receive_info = GetTmmbrInformation(tmmbn.sender_ssrc());
danilchap7851bda2016-09-29 15:28:07 -0700818 if (!receive_info) // This remote SSRC must be saved before.
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000819 return;
danilchap1b1863a2016-09-20 01:39:54 -0700820
danilchap92ea6012016-09-23 10:36:03 -0700821 packet_information->packet_type_flags |= kRtcpTmmbn;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000822
danilchap1b1863a2016-09-20 01:39:54 -0700823 for (const auto& item : tmmbn.items())
danilchap7851bda2016-09-29 15:28:07 -0700824 receive_info->tmmbn.push_back(item);
danilchap1b1863a2016-09-20 01:39:54 -0700825}
826
danilchap8bab7962016-12-20 02:46:46 -0800827void RTCPReceiver::HandleSrReq(const CommonHeader& rtcp_block,
828 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700829 rtcp::RapidResyncRequest sr_req;
830 if (!sr_req.Parse(rtcp_block)) {
831 ++num_skipped_packets_;
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000832 return;
833 }
834
danilchap92ea6012016-09-23 10:36:03 -0700835 packet_information->packet_type_flags |= kRtcpSrReq;
niklase@google.com470e71d2011-07-07 08:21:25 +0000836}
837
danilchap8bab7962016-12-20 02:46:46 -0800838void RTCPReceiver::HandleSli(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700839 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700840 rtcp::Sli sli;
841 if (!sli.Parse(rtcp_block)) {
842 ++num_skipped_packets_;
843 return;
844 }
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200845
danilchap1b1863a2016-09-20 01:39:54 -0700846 for (const rtcp::Sli::Macroblocks& item : sli.macroblocks()) {
847 // In theory there could be multiple slices lost.
848 // Received signal that we need to refresh a slice.
danilchap92ea6012016-09-23 10:36:03 -0700849 packet_information->packet_type_flags |= kRtcpSli;
850 packet_information->sli_picture_id = item.picture_id();
asapersson@webrtc.orgdf7b65b2015-01-21 13:07:04 +0000851 }
852}
853
danilchap8bab7962016-12-20 02:46:46 -0800854void RTCPReceiver::HandleRpsi(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700855 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700856 rtcp::Rpsi rpsi;
857 if (!rpsi.Parse(rtcp_block)) {
858 ++num_skipped_packets_;
859 return;
danilchapda161d72016-08-19 07:29:46 -0700860 }
danilchap1b1863a2016-09-20 01:39:54 -0700861
862 // Received signal that we have a confirmed reference picture.
danilchap92ea6012016-09-23 10:36:03 -0700863 packet_information->packet_type_flags |= kRtcpRpsi;
864 packet_information->rpsi_picture_id = rpsi.picture_id();
niklase@google.com470e71d2011-07-07 08:21:25 +0000865}
866
danilchap1b1863a2016-09-20 01:39:54 -0700867void RTCPReceiver::HandlePsfbApp(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700868 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700869 rtcp::Remb remb;
870 if (remb.Parse(rtcp_block)) {
danilchap92ea6012016-09-23 10:36:03 -0700871 packet_information->packet_type_flags |= kRtcpRemb;
872 packet_information->receiver_estimated_max_bitrate_bps = remb.bitrate_bps();
danilchap1b1863a2016-09-20 01:39:54 -0700873 return;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000874 }
danilchap1b1863a2016-09-20 01:39:54 -0700875
876 ++num_skipped_packets_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000877}
878
danilchap8bab7962016-12-20 02:46:46 -0800879void RTCPReceiver::HandleFir(const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700880 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700881 rtcp::Fir fir;
882 if (!fir.Parse(rtcp_block)) {
883 ++num_skipped_packets_;
884 return;
885 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000886
danilchap1b1863a2016-09-20 01:39:54 -0700887 for (const rtcp::Fir::Request& fir_request : fir.requests()) {
danilchap8bab7962016-12-20 02:46:46 -0800888 // Is it our sender that is requested to generate a new keyframe.
danilchap1b1863a2016-09-20 01:39:54 -0700889 if (main_ssrc_ != fir_request.ssrc)
890 continue;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200891
892 ++packet_type_counter_.fir_packets;
893
danilchapefa966b2017-02-17 06:23:15 -0800894 int64_t now_ms = clock_->TimeInMilliseconds();
895 auto inserted = last_fir_.insert(std::make_pair(
896 fir.sender_ssrc(), LastFirStatus(now_ms, fir_request.seq_nr)));
897 if (!inserted.second) { // There was already an entry.
898 LastFirStatus* last_fir = &inserted.first->second;
899
danilchap7851bda2016-09-29 15:28:07 -0700900 // Check if we have reported this FIRSequenceNumber before.
danilchapefa966b2017-02-17 06:23:15 -0800901 if (fir_request.seq_nr == last_fir->sequence_number)
danilchap7851bda2016-09-29 15:28:07 -0700902 continue;
903
danilchap7851bda2016-09-29 15:28:07 -0700904 // Sanity: don't go crazy with the callbacks.
danilchapefa966b2017-02-17 06:23:15 -0800905 if (now_ms - last_fir->request_ms < kRtcpMinFrameLengthMs)
danilchap7851bda2016-09-29 15:28:07 -0700906 continue;
907
danilchapefa966b2017-02-17 06:23:15 -0800908 last_fir->request_ms = now_ms;
909 last_fir->sequence_number = fir_request.seq_nr;
Danil Chapovalov530b3f52016-09-15 18:41:02 +0200910 }
danilchap7851bda2016-09-29 15:28:07 -0700911 // Received signal that we need to send a new key frame.
912 packet_information->packet_type_flags |= kRtcpFir;
pwestin@webrtc.orgb2179c22012-05-21 12:00:49 +0000913 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000914}
915
Erik Språng6b8d3552015-09-24 15:06:57 +0200916void RTCPReceiver::HandleTransportFeedback(
danilchap1b1863a2016-09-20 01:39:54 -0700917 const CommonHeader& rtcp_block,
danilchap92ea6012016-09-23 10:36:03 -0700918 PacketInformation* packet_information) {
danilchap1b1863a2016-09-20 01:39:54 -0700919 std::unique_ptr<rtcp::TransportFeedback> transport_feedback(
920 new rtcp::TransportFeedback());
921 if (!transport_feedback->Parse(rtcp_block)) {
922 ++num_skipped_packets_;
923 return;
924 }
Erik Språng6b8d3552015-09-24 15:06:57 +0200925
danilchap92ea6012016-09-23 10:36:03 -0700926 packet_information->packet_type_flags |= kRtcpTransportFeedback;
927 packet_information->transport_feedback = std::move(transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +0200928}
danilchap287e5482016-08-16 15:15:39 -0700929
danilchap9bf610e2017-02-20 06:03:01 -0800930void RTCPReceiver::NotifyTmmbrUpdated() {
danilchap853ecb22016-08-22 08:26:15 -0700931 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700932 std::vector<rtcp::TmmbItem> bounding =
danilchap853ecb22016-08-22 08:26:15 -0700933 TMMBRHelp::FindBoundingSet(TmmbrReceived());
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000934
spranga790d832016-12-02 07:29:44 -0800935 if (!bounding.empty() && rtcp_bandwidth_observer_) {
danilchap853ecb22016-08-22 08:26:15 -0700936 // We have a new bandwidth estimate on this channel.
danilchap2f69ce92016-08-16 03:21:38 -0700937 uint64_t bitrate_bps = TMMBRHelp::CalcMinBitrateBps(bounding);
938 if (bitrate_bps <= std::numeric_limits<uint32_t>::max())
spranga790d832016-12-02 07:29:44 -0800939 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(bitrate_bps);
pwestin@webrtc.orgcac78782012-04-05 08:30:10 +0000940 }
danilchap853ecb22016-08-22 08:26:15 -0700941
danilchap9bf610e2017-02-20 06:03:01 -0800942 // Send tmmbn to inform remote clients about the new bandwidth.
danilchap8bab7962016-12-20 02:46:46 -0800943 rtp_rtcp_->SetTmmbn(std::move(bounding));
niklase@google.com470e71d2011-07-07 08:21:25 +0000944}
945
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000946void RTCPReceiver::RegisterRtcpStatisticsCallback(
947 RtcpStatisticsCallback* callback) {
spranga790d832016-12-02 07:29:44 -0800948 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000949 stats_callback_ = callback;
950}
951
952RtcpStatisticsCallback* RTCPReceiver::GetRtcpStatisticsCallback() {
spranga790d832016-12-02 07:29:44 -0800953 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +0000954 return stats_callback_;
955}
956
danilchap8bab7962016-12-20 02:46:46 -0800957// Holding no Critical section.
958void RTCPReceiver::TriggerCallbacksFromRtcpPacket(
danilchap92ea6012016-09-23 10:36:03 -0700959 const PacketInformation& packet_information) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000960 // Process TMMBR and REMB first to avoid multiple callbacks
961 // to OnNetworkChanged.
danilchap92ea6012016-09-23 10:36:03 -0700962 if (packet_information.packet_type_flags & kRtcpTmmbr) {
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000963 // Might trigger a OnReceivedBandwidthEstimateUpdate.
danilchap9bf610e2017-02-20 06:03:01 -0800964 NotifyTmmbrUpdated();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000965 }
sprang7dc39f32015-10-13 09:17:48 -0700966 uint32_t local_ssrc;
967 std::set<uint32_t> registered_ssrcs;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000968 {
969 // We don't want to hold this critsect when triggering the callbacks below.
danilchap8bab7962016-12-20 02:46:46 -0800970 rtc::CritScope lock(&rtcp_receiver_lock_);
stefan@webrtc.org28a331e2013-09-17 07:49:56 +0000971 local_ssrc = main_ssrc_;
sprang7dc39f32015-10-13 09:17:48 -0700972 registered_ssrcs = registered_ssrcs_;
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000973 }
danilchap92ea6012016-09-23 10:36:03 -0700974 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpSrReq)) {
danilchap8bab7962016-12-20 02:46:46 -0800975 rtp_rtcp_->OnRequestSendReport();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000976 }
danilchap92ea6012016-09-23 10:36:03 -0700977 if (!receiver_only_ && (packet_information.packet_type_flags & kRtcpNack)) {
978 if (!packet_information.nack_sequence_numbers.empty()) {
andresp@webrtc.org4436b442014-06-04 09:05:30 +0000979 LOG(LS_VERBOSE) << "Incoming NACK length: "
danilchap92ea6012016-09-23 10:36:03 -0700980 << packet_information.nack_sequence_numbers.size();
danilchap8bab7962016-12-20 02:46:46 -0800981 rtp_rtcp_->OnReceivedNack(packet_information.nack_sequence_numbers);
pwestin@webrtc.org3aa25de2012-01-05 08:40:56 +0000982 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000983 }
spranga790d832016-12-02 07:29:44 -0800984
985 // We need feedback that we have received a report block(s) so that we
986 // can generate a new packet in a conference relay scenario, one received
987 // report can generate several RTCP packets, based on number relayed/mixed
988 // a send report block should go out to all receivers.
989 if (rtcp_intra_frame_observer_) {
990 RTC_DCHECK(!receiver_only_);
991 if ((packet_information.packet_type_flags & kRtcpPli) ||
992 (packet_information.packet_type_flags & kRtcpFir)) {
993 if (packet_information.packet_type_flags & kRtcpPli) {
994 LOG(LS_VERBOSE) << "Incoming PLI from SSRC "
995 << packet_information.remote_ssrc;
996 } else {
997 LOG(LS_VERBOSE) << "Incoming FIR from SSRC "
998 << packet_information.remote_ssrc;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000999 }
spranga790d832016-12-02 07:29:44 -08001000 rtcp_intra_frame_observer_->OnReceivedIntraFrameRequest(local_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00001001 }
spranga790d832016-12-02 07:29:44 -08001002 if (packet_information.packet_type_flags & kRtcpSli) {
1003 rtcp_intra_frame_observer_->OnReceivedSLI(
1004 local_ssrc, packet_information.sli_picture_id);
1005 }
1006 if (packet_information.packet_type_flags & kRtcpRpsi) {
1007 rtcp_intra_frame_observer_->OnReceivedRPSI(
1008 local_ssrc, packet_information.rpsi_picture_id);
1009 }
1010 }
1011 if (rtcp_bandwidth_observer_) {
1012 RTC_DCHECK(!receiver_only_);
1013 if (packet_information.packet_type_flags & kRtcpRemb) {
1014 LOG(LS_VERBOSE) << "Incoming REMB: "
1015 << packet_information.receiver_estimated_max_bitrate_bps;
1016 rtcp_bandwidth_observer_->OnReceivedEstimatedBitrate(
1017 packet_information.receiver_estimated_max_bitrate_bps);
niklase@google.com470e71d2011-07-07 08:21:25 +00001018 }
danilchap92ea6012016-09-23 10:36:03 -07001019 if ((packet_information.packet_type_flags & kRtcpSr) ||
1020 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001021 int64_t now_ms = clock_->TimeInMilliseconds();
spranga790d832016-12-02 07:29:44 -08001022 rtcp_bandwidth_observer_->OnReceivedRtcpReceiverReport(
danilchap8bab7962016-12-20 02:46:46 -08001023 packet_information.report_blocks, packet_information.rtt_ms, now_ms);
isheriff6b4b5f32016-06-08 00:24:21 -07001024 }
spranga790d832016-12-02 07:29:44 -08001025 }
1026 if ((packet_information.packet_type_flags & kRtcpSr) ||
1027 (packet_information.packet_type_flags & kRtcpRr)) {
danilchap8bab7962016-12-20 02:46:46 -08001028 rtp_rtcp_->OnReceivedRtcpReportBlocks(packet_information.report_blocks);
spranga790d832016-12-02 07:29:44 -08001029 }
isheriff6b4b5f32016-06-08 00:24:21 -07001030
spranga790d832016-12-02 07:29:44 -08001031 if (transport_feedback_observer_ &&
1032 (packet_information.packet_type_flags & kRtcpTransportFeedback)) {
1033 uint32_t media_source_ssrc =
1034 packet_information.transport_feedback->media_ssrc();
1035 if (media_source_ssrc == local_ssrc ||
1036 registered_ssrcs.find(media_source_ssrc) != registered_ssrcs.end()) {
1037 transport_feedback_observer_->OnTransportFeedback(
1038 *packet_information.transport_feedback);
Erik Språng6b8d3552015-09-24 15:06:57 +02001039 }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001040 }
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001041
spranga790d832016-12-02 07:29:44 -08001042 if (bitrate_allocation_observer_ &&
1043 packet_information.target_bitrate_allocation) {
1044 bitrate_allocation_observer_->OnBitrateAllocationUpdated(
1045 *packet_information.target_bitrate_allocation);
1046 }
1047
Peter Boströmfe7a80c2015-04-23 17:53:17 +02001048 if (!receiver_only_) {
spranga790d832016-12-02 07:29:44 -08001049 rtc::CritScope cs(&feedbacks_lock_);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001050 if (stats_callback_) {
danilchap92ea6012016-09-23 10:36:03 -07001051 for (const auto& report_block : packet_information.report_blocks) {
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001052 RtcpStatistics stats;
danilchap92ea6012016-09-23 10:36:03 -07001053 stats.cumulative_lost = report_block.cumulativeLost;
1054 stats.extended_max_sequence_number = report_block.extendedHighSeqNum;
1055 stats.fraction_lost = report_block.fractionLost;
1056 stats.jitter = report_block.jitter;
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001057
danilchap92ea6012016-09-23 10:36:03 -07001058 stats_callback_->StatisticsUpdated(stats, report_block.sourceSSRC);
sprang@webrtc.orga6ad6e52013-12-05 09:48:44 +00001059 }
1060 }
1061 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001062}
1063
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001064int32_t RTCPReceiver::CNAME(uint32_t remoteSSRC,
pbos@webrtc.org2f446732013-04-08 11:08:41 +00001065 char cName[RTCP_CNAME_SIZE]) const {
danilchap95321242016-09-27 07:05:32 -07001066 RTC_DCHECK(cName);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001067
danilchap8bab7962016-12-20 02:46:46 -08001068 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap95321242016-09-27 07:05:32 -07001069 auto received_cname_it = received_cnames_.find(remoteSSRC);
1070 if (received_cname_it == received_cnames_.end())
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +00001071 return -1;
danilchap95321242016-09-27 07:05:32 -07001072
1073 size_t length = received_cname_it->second.copy(cName, RTCP_CNAME_SIZE - 1);
1074 cName[length] = 0;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00001075 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001076}
1077
danilchap7851bda2016-09-29 15:28:07 -07001078std::vector<rtcp::TmmbItem> RTCPReceiver::TmmbrReceived() {
danilchap8bab7962016-12-20 02:46:46 -08001079 rtc::CritScope lock(&rtcp_receiver_lock_);
danilchap287e5482016-08-16 15:15:39 -07001080 std::vector<rtcp::TmmbItem> candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001081
danilchap8bab7962016-12-20 02:46:46 -08001082 int64_t now_ms = clock_->TimeInMilliseconds();
danilchap7851bda2016-09-29 15:28:07 -07001083 // Use audio define since we don't know what interval the remote peer use.
stefanb33eed22017-02-02 03:57:02 -08001084 int64_t timeout_ms = now_ms - 5 * RTCP_INTERVAL_AUDIO_MS;
danilchap287e5482016-08-16 15:15:39 -07001085
danilchap9bf610e2017-02-20 06:03:01 -08001086 for (auto& kv : tmmbr_infos_) {
danilchap7851bda2016-09-29 15:28:07 -07001087 for (auto it = kv.second.tmmbr.begin(); it != kv.second.tmmbr.end();) {
stefanb33eed22017-02-02 03:57:02 -08001088 if (it->second.last_updated_ms < timeout_ms) {
danilchap7851bda2016-09-29 15:28:07 -07001089 // Erase timeout entries.
1090 it = kv.second.tmmbr.erase(it);
1091 } else {
1092 candidates.push_back(it->second.tmmbr_item);
1093 ++it;
1094 }
1095 }
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +00001096 }
danilchap287e5482016-08-16 15:15:39 -07001097 return candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +00001098}
1099
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001100} // namespace webrtc