blob: d45a86f8c5c76712a60781eea389f2e77e0dacdb [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_sender.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
stefan@webrtc.org9354cc92012-06-07 08:10:14 +000013#include <string.h> // memcpy
niklase@google.com470e71d2011-07-07 08:21:25 +000014
Danil Chapovalov70ffead2016-07-20 15:26:59 +020015#include <utility>
16
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "common_types.h" // NOLINT(build/include)
Elad Alon4a87e1c2017-10-03 16:11:34 +020018#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "logging/rtc_event_log/rtc_event_log.h"
20#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
21#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
22#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
34#include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
35#include "modules/rtp_rtcp/source/time_util.h"
36#include "modules/rtp_rtcp/source/tmmbr_help.h"
37#include "rtc_base/checks.h"
38#include "rtc_base/constructormagic.h"
39#include "rtc_base/logging.h"
Jiawei Ou3587b832018-01-31 22:08:26 -080040#include "rtc_base/numerics/safe_conversions.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020041#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/trace_event.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000043
niklase@google.com470e71d2011-07-07 08:21:25 +000044namespace webrtc {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000045
sprang5e38c962016-12-01 05:18:09 -080046namespace {
47const uint32_t kRtcpAnyExtendedReports =
48 kRtcpXrVoipMetric | kRtcpXrReceiverReferenceTime | kRtcpXrDlrrReportBlock |
49 kRtcpXrTargetBitrate;
50} // namespace
51
Erik Språng61be2a42015-04-27 13:32:52 +020052NACKStringBuilder::NACKStringBuilder()
danilchap162abd32015-12-10 02:39:40 -080053 : stream_(""), count_(0), prevNack_(0), consecutive_(false) {}
edjee@google.com79b02892013-04-04 19:43:34 +000054
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000055NACKStringBuilder::~NACKStringBuilder() {}
56
danilchap162abd32015-12-10 02:39:40 -080057void NACKStringBuilder::PushNACK(uint16_t nack) {
Erik Språng242e22b2015-05-11 10:17:43 +020058 if (count_ == 0) {
59 stream_ << nack;
60 } else if (nack == prevNack_ + 1) {
61 consecutive_ = true;
Erik Språng61be2a42015-04-27 13:32:52 +020062 } else {
Erik Språng242e22b2015-05-11 10:17:43 +020063 if (consecutive_) {
64 stream_ << "-" << prevNack_;
65 consecutive_ = false;
edjee@google.com79b02892013-04-04 19:43:34 +000066 }
Erik Språng242e22b2015-05-11 10:17:43 +020067 stream_ << "," << nack;
Erik Språng61be2a42015-04-27 13:32:52 +020068 }
Erik Språng242e22b2015-05-11 10:17:43 +020069 count_++;
70 prevNack_ = nack;
edjee@google.com79b02892013-04-04 19:43:34 +000071}
72
Erik Språng61be2a42015-04-27 13:32:52 +020073std::string NACKStringBuilder::GetResult() {
Erik Språng242e22b2015-05-11 10:17:43 +020074 if (consecutive_) {
75 stream_ << "-" << prevNack_;
76 consecutive_ = false;
Erik Språng61be2a42015-04-27 13:32:52 +020077 }
Erik Språng242e22b2015-05-11 10:17:43 +020078 return stream_.str();
edjee@google.com79b02892013-04-04 19:43:34 +000079}
80
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000081RTCPSender::FeedbackState::FeedbackState()
nisse40ba3ad2017-03-17 07:04:00 -070082 : packets_sent(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000083 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000084 send_bitrate(0),
85 last_rr_ntp_secs(0),
86 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000087 remote_sr(0),
Erik Språng61be2a42015-04-27 13:32:52 +020088 has_last_xr_rr(false),
danilchap162abd32015-12-10 02:39:40 -080089 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000090
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010091class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010092 public:
terelius429c3452016-01-21 05:42:04 -080093 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010094 : transport_(transport), event_log_(event_log) {}
Erik Språngf7c57762015-12-04 10:40:35 +010095 virtual ~PacketContainer() {
96 for (RtcpPacket* packet : appended_packets_)
97 delete packet;
98 }
99
danilchap41befce2016-03-30 11:11:51 -0700100 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100101 size_t bytes_sent = 0;
102 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
103 if (transport_->SendRtcp(packet.data(), packet.size())) {
104 bytes_sent += packet.size();
105 if (event_log_) {
106 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(packet));
107 }
108 }
109 });
110 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +0100111 }
112
113 private:
114 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -0800115 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -0800116
117 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +0100118};
119
120class RTCPSender::RtcpContext {
121 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200122 RtcpContext(const FeedbackState& feedback_state,
123 int32_t nack_size,
124 const uint16_t* nack_list,
danilchap51813b32016-12-16 02:44:36 -0800125 NtpTime now)
Erik Språngf7c57762015-12-04 10:40:35 +0100126 : feedback_state_(feedback_state),
127 nack_size_(nack_size),
128 nack_list_(nack_list),
danilchap51813b32016-12-16 02:44:36 -0800129 now_(now) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200130
Erik Språngf7c57762015-12-04 10:40:35 +0100131 const FeedbackState& feedback_state_;
132 const int32_t nack_size_;
133 const uint16_t* nack_list_;
danilchap51813b32016-12-16 02:44:36 -0800134 const NtpTime now_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200135};
136
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000137RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000138 bool audio,
139 Clock* clock,
danilchapf5f793c2017-07-27 04:44:18 -0700140 ReceiveStatisticsProvider* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700141 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800142 RtcEventLog* event_log,
Jiawei Ou3587b832018-01-31 22:08:26 -0800143 Transport* outgoing_transport,
144 RtcpIntervalConfig interval_config)
Peter Boströmac547a62015-09-17 23:03:57 +0200145 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200146 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800147 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700148 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800149 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700150 transport_(outgoing_transport),
Jiawei Ou3587b832018-01-31 22:08:26 -0800151 interval_config_(interval_config),
Erik Språng242e22b2015-05-11 10:17:43 +0200152 using_nack_(false),
153 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200154 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700155 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000156 last_rtp_timestamp_(0),
157 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200158 ssrc_(0),
159 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000160 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
Erik Språng242e22b2015-05-11 10:17:43 +0200162 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000163
Erik Språng242e22b2015-05-11 10:17:43 +0200164 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
danilchap2b616392016-08-18 06:17:42 -0700166 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200167 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800168 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000169
Erik Språng242e22b2015-05-11 10:17:43 +0200170 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200171 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200172 app_data_(nullptr),
173 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
Erik Språng242e22b2015-05-11 10:17:43 +0200175 xr_send_receiver_reference_time_enabled_(false),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000176 packet_type_counter_observer_(packet_type_counter_observer) {
sprang86fd9ed2015-09-29 04:45:43 -0700177 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200178
179 builders_[kRtcpSr] = &RTCPSender::BuildSR;
180 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200181 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200182 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
183 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200184 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
185 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
186 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
187 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
188 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
189 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800190 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
danilchap162abd32015-12-10 02:39:40 -0800193RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
pbosda903ea2015-10-02 02:36:56 -0700195RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700196 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200197 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
skvlad1c392cc2016-04-01 14:46:44 -0700200void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700201 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000202
skvlad1c392cc2016-04-01 14:46:44 -0700203 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
204 // When switching on, reschedule the next packet
Jiawei Ou3587b832018-01-31 22:08:26 -0800205 int64_t interval_ms = audio_ ? interval_config_.audio_interval_ms
206 : interval_config_.video_interval_ms;
207 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + (interval_ms / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700208 }
209 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
Erik Språng61be2a42015-04-27 13:32:52 +0200212bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700213 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200214 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000215}
216
Erik Språng61be2a42015-04-27 13:32:52 +0200217int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
218 bool sending) {
219 bool sendRTCPBye = false;
220 {
danilchap56036ff2016-03-22 11:14:09 -0700221 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
pbosda903ea2015-10-02 02:36:56 -0700223 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200224 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200225 // Trigger RTCP bye
226 sendRTCPBye = true;
227 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 }
Erik Språng242e22b2015-05-11 10:17:43 +0200229 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200230 }
231 if (sendRTCPBye)
232 return SendRTCP(feedback_state, kRtcpBye);
233 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100236void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
237 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700238 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100239 remb_bitrate_ = bitrate_bps;
240 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000241
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200242 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000243 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
244 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200245 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000246}
247
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200248void RTCPSender::UnsetRemb() {
249 rtc::CritScope lock(&critical_section_rtcp_sender_);
250 // Stop sending REMB each report until it is reenabled and REMB data set.
251 ConsumeFlag(kRtcpRemb, /*forced=*/true);
252}
253
Erik Språng61be2a42015-04-27 13:32:52 +0200254bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700255 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200256 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000257}
258
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000259void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700260 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200261 if (enable) {
262 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
263 } else {
264 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
265 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
nisse284542b2017-01-10 08:58:32 -0800268void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800269 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800270 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700271}
272
danilchap71fead22016-08-18 02:01:49 -0700273void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700274 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700275 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000276}
277
278void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
279 int64_t capture_time_ms) {
danilchap56036ff2016-03-22 11:14:09 -0700280 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000281 last_rtp_timestamp_ = rtp_timestamp;
282 if (capture_time_ms < 0) {
283 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200284 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000285 } else {
286 last_frame_capture_time_ms_ = capture_time_ms;
287 }
288}
289
nisse14adba72017-03-20 03:52:39 -0700290uint32_t RTCPSender::SSRC() const {
291 rtc::CritScope lock(&critical_section_rtcp_sender_);
292 return ssrc_;
293}
294
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000295void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700296 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000297
Erik Språng242e22b2015-05-11 10:17:43 +0200298 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200299 // not first SetSSRC, probably due to a collision
300 // schedule a new RTCP report
301 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200302 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200303 }
Erik Språng242e22b2015-05-11 10:17:43 +0200304 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000305}
306
Erik Språng61be2a42015-04-27 13:32:52 +0200307void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700308 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200309 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000310}
311
Peter Boström9ba52f82015-06-01 14:12:28 +0200312int32_t RTCPSender::SetCNAME(const char* c_name) {
313 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000314 return -1;
315
kwiberg352444f2016-11-28 15:58:53 -0800316 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700317 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200318 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000319 return 0;
320}
321
Erik Språng0ea42d32015-06-25 14:46:16 +0200322int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700323 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800324 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700325 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700326 // One spot is reserved for ssrc_/cname_.
327 // TODO(danilchap): Add support for more than 30 contributes by sending
328 // several sdes packets.
329 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000330 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200331
332 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000333 return 0;
334}
335
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000336int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700337 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200338 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000339
Erik Språng242e22b2015-05-11 10:17:43 +0200340 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000341 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200342
Erik Språng242e22b2015-05-11 10:17:43 +0200343 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000344 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000345}
346
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000347bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800348 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800349 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
Jiawei Ou3587b832018-01-31 22:08:26 -0800351 For video we use a configurable interval (default: 1 second) for a BW
352 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
353 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000354
355
danilchap162abd32015-12-10 02:39:40 -0800356 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000357
danilchap162abd32015-12-10 02:39:40 -0800358 MAX RTCP BW is 5% if the session BW
359 A send report is approximately 65 bytes inc CNAME
360 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000361
danilchap162abd32015-12-10 02:39:40 -0800362 The RECOMMENDED value for the reduced minimum in seconds is 360
363 divided by the session bandwidth in kilobits/second. This minimum
364 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
danilchap162abd32015-12-10 02:39:40 -0800366 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800367 initial is true), the constant Tmin is set to half of the configured
368 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
danilchap162abd32015-12-10 02:39:40 -0800370 The interval between RTCP packets is varied randomly over the
371 range [0.5,1.5] times the calculated interval to avoid unintended
372 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
danilchap162abd32015-12-10 02:39:40 -0800374 if we send
375 If the participant is a sender (we_sent true), the constant C is
376 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
377 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
378 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000379
danilchap162abd32015-12-10 02:39:40 -0800380 if we receive only
381 If we_sent is not true, the constant C is set
382 to the average RTCP packet size divided by 75% of the RTCP
383 bandwidth. The constant n is set to the number of receivers
384 (members - senders). If the number of senders is greater than
385 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
danilchap162abd32015-12-10 02:39:40 -0800387 reconsideration NOT required for peer-to-peer
388 "timer reconsideration" is
389 employed. This algorithm implements a simple back-off mechanism
390 which causes users to hold back RTCP packet transmission if the
391 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000392
danilchap162abd32015-12-10 02:39:40 -0800393 n = number of members
394 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
danilchap162abd32015-12-10 02:39:40 -0800396 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000397
danilchap162abd32015-12-10 02:39:40 -0800398 4. The calculated interval T is set to a number uniformly distributed
399 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
danilchap162abd32015-12-10 02:39:40 -0800401 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
402 for the fact that the timer reconsideration algorithm converges to
403 a value of the RTCP bandwidth below the intended average
404 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
Erik Språng242e22b2015-05-11 10:17:43 +0200406 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000407
danilchap56036ff2016-03-22 11:14:09 -0700408 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000409
pbosda903ea2015-10-02 02:36:56 -0700410 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000411 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
Erik Språng242e22b2015-05-11 10:17:43 +0200413 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200414 // for video key-frames we want to send the RTCP before the large key-frame
415 // if we have a 100 ms margin
416 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
Erik Språng242e22b2015-05-11 10:17:43 +0200419 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200420 return true;
421 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200422 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200423 // wrap
424 return true;
425 }
426 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000427}
428
danilchap56036ff2016-03-22 11:14:09 -0700429std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200430 // Timestamp shouldn't be estimated before first media frame.
431 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200432 // The timestamp of this RTCP packet should be estimated as the timestamp of
433 // the frame being captured at this moment. We are calculating that
434 // timestamp as the last frame's timestamp + the time since the last frame
435 // was captured.
solenbergb19d2882016-10-03 06:22:25 -0700436 uint32_t rtp_rate =
437 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) / 1000;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200438 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700439 timestamp_offset_ + last_rtp_timestamp_ +
solenbergb19d2882016-10-03 06:22:25 -0700440 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
Erik Språngf7c57762015-12-04 10:40:35 +0100442 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700443 report->SetSenderSsrc(ssrc_);
danilchap51813b32016-12-16 02:44:36 -0800444 report->SetNtp(ctx.now_);
danilchap822a16f2016-09-27 09:27:47 -0700445 report->SetRtpTimestamp(rtp_timestamp);
446 report->SetPacketCount(ctx.feedback_state_.packets_sent);
447 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700448 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100449
danilchap56036ff2016-03-22 11:14:09 -0700450 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
danilchap56036ff2016-03-22 11:14:09 -0700453std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100454 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200455 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800456 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
Erik Språngf7c57762015-12-04 10:40:35 +0100458 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700459 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200460
danilchap74e8df8f2017-03-16 08:04:08 -0700461 for (const auto& it : csrc_cnames_)
462 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200463
danilchap56036ff2016-03-22 11:14:09 -0700464 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
danilchap56036ff2016-03-22 11:14:09 -0700467std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100468 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700469 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700470 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200471
danilchap56036ff2016-03-22 11:14:09 -0700472 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
danilchap56036ff2016-03-22 11:14:09 -0700475std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100476 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700477 pli->SetSenderSsrc(ssrc_);
478 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200479
Erik Språng242e22b2015-05-11 10:17:43 +0200480 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
481 "RTCPSender::PLI");
482 ++packet_type_counter_.pli_packets;
483 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_PLICount",
484 ssrc_, packet_type_counter_.pli_packets);
485
danilchap56036ff2016-03-22 11:14:09 -0700486 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200487}
488
danilchap56036ff2016-03-22 11:14:09 -0700489std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800490 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
Erik Språngf7c57762015-12-04 10:40:35 +0100492 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700493 fir->SetSenderSsrc(ssrc_);
494 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200495
496 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
497 "RTCPSender::FIR");
498 ++packet_type_counter_.fir_packets;
499 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_FIRCount",
500 ssrc_, packet_type_counter_.fir_packets);
501
danilchap56036ff2016-03-22 11:14:09 -0700502 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503}
504
danilchap56036ff2016-03-22 11:14:09 -0700505std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100506 const RtcpContext& ctx) {
507 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700508 remb->SetSenderSsrc(ssrc_);
509 remb->SetBitrateBps(remb_bitrate_);
510 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200511
Erik Språng242e22b2015-05-11 10:17:43 +0200512 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
513 "RTCPSender::REMB");
514
danilchap56036ff2016-03-22 11:14:09 -0700515 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000516}
517
Erik Språng61be2a42015-04-27 13:32:52 +0200518void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700519 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700520 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000521}
522
danilchap56036ff2016-03-22 11:14:09 -0700523std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100524 const RtcpContext& ctx) {
525 if (ctx.feedback_state_.module == nullptr)
526 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200527 // Before sending the TMMBR check the received TMMBN, only an owner is
528 // allowed to raise the bitrate:
529 // * If the sender is an owner of the TMMBN -> send TMMBR
530 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
Erik Språng61be2a42015-04-27 13:32:52 +0200532 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700533 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000534
Erik Språng242e22b2015-05-11 10:17:43 +0200535 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
536 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200537 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700538 std::vector<rtcp::TmmbItem> candidates =
539 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000540
danilchap2b616392016-08-18 06:17:42 -0700541 if (!candidates.empty()) {
542 for (const auto& candidate : candidates) {
543 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
544 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100545 // Do not send the same tuple.
546 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200547 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000548 }
danilchap2b616392016-08-18 06:17:42 -0700549 if (!tmmbr_owner) {
550 // Use received bounding set as candidate set.
551 // Add current tuple.
552 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
danilchap2b616392016-08-18 06:17:42 -0700554 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700555 std::vector<rtcp::TmmbItem> bounding =
556 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700557 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
558 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100559 // Did not enter bounding set, no meaning to send this request.
560 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 }
Erik Språng61be2a42015-04-27 13:32:52 +0200563 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000564
danilchap2b616392016-08-18 06:17:42 -0700565 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100566 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700567
Erik Språngf7c57762015-12-04 10:40:35 +0100568 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700569 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800570 rtcp::TmmbItem request;
571 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700572 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800573 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700574 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100575
danilchap56036ff2016-03-22 11:14:09 -0700576 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200577}
578
danilchap56036ff2016-03-22 11:14:09 -0700579std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100580 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100581 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700582 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700583 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
584 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700585 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000586 }
Erik Språng61be2a42015-04-27 13:32:52 +0200587 }
sprangd83df502015-08-27 01:05:08 -0700588
danilchap56036ff2016-03-22 11:14:09 -0700589 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000590}
591
danilchap56036ff2016-03-22 11:14:09 -0700592std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100593 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700594 app->SetSsrc(ssrc_);
595 app->SetSubType(app_sub_type_);
596 app->SetName(app_name_);
597 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200598
danilchap56036ff2016-03-22 11:14:09 -0700599 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200600}
601
danilchap56036ff2016-03-22 11:14:09 -0700602std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100603 const RtcpContext& ctx) {
604 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700605 nack->SetSenderSsrc(ssrc_);
606 nack->SetMediaSsrc(remote_ssrc_);
607 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200608
609 // Report stats.
610 NACKStringBuilder stringBuilder;
Erik Språngf7c57762015-12-04 10:40:35 +0100611 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
612 stringBuilder.PushNACK(ctx.nack_list_[idx]);
613 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200614 }
Erik Språng61be2a42015-04-27 13:32:52 +0200615 packet_type_counter_.nack_requests = nack_stats_.requests();
616 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200617
618 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
619 "RTCPSender::NACK", "nacks",
620 TRACE_STR_COPY(stringBuilder.GetResult().c_str()));
621 ++packet_type_counter_.nack_packets;
622 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_NACKCount",
623 ssrc_, packet_type_counter_.nack_packets);
624
danilchap56036ff2016-03-22 11:14:09 -0700625 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200626}
627
danilchap56036ff2016-03-22 11:14:09 -0700628std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100629 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700630 bye->SetSenderSsrc(ssrc_);
631 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700632
danilchap56036ff2016-03-22 11:14:09 -0700633 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000634}
635
sprang5e38c962016-12-01 05:18:09 -0800636std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100637 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800638 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700639 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000640
sprang5e38c962016-12-01 05:18:09 -0800641 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
642 rtcp::Rrtr rrtr;
danilchap51813b32016-12-16 02:44:36 -0800643 rrtr.SetNtp(ctx.now_);
sprang5e38c962016-12-01 05:18:09 -0800644 xr->SetRrtr(rrtr);
645 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000646
sprang5e38c962016-12-01 05:18:09 -0800647 if (ctx.feedback_state_.has_last_xr_rr) {
648 xr->AddDlrrItem(ctx.feedback_state_.last_xr_rr);
649 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000650
sprang5e38c962016-12-01 05:18:09 -0800651 if (video_bitrate_allocation_) {
652 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000653
sprang5e38c962016-12-01 05:18:09 -0800654 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
655 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100656 if (video_bitrate_allocation_->HasBitrate(sl, tl)) {
657 target_bitrate.AddTargetBitrate(
658 sl, tl, video_bitrate_allocation_->GetBitrate(sl, tl) / 1000);
659 }
sprang5e38c962016-12-01 05:18:09 -0800660 }
661 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000662
sprang5e38c962016-12-01 05:18:09 -0800663 xr->SetTargetBitrate(target_bitrate);
664 video_bitrate_allocation_.reset();
665 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200666
sprang5e38c962016-12-01 05:18:09 -0800667 if (xr_voip_metric_) {
668 rtcp::VoipMetric voip;
669 voip.SetMediaSsrc(remote_ssrc_);
670 voip.SetVoipMetric(*xr_voip_metric_);
671 xr_voip_metric_.reset();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000672
sprang5e38c962016-12-01 05:18:09 -0800673 xr->SetVoipMetric(voip);
674 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200675
sprang5e38c962016-12-01 05:18:09 -0800676 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000677}
678
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000679int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200680 RTCPPacketType packetType,
681 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700682 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200683 return SendCompoundRTCP(
684 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700685 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200686}
687
688int32_t RTCPSender::SendCompoundRTCP(
689 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100690 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200691 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700692 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800693 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800694 size_t max_packet_size;
695
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000696 {
danilchap56036ff2016-03-22 11:14:09 -0700697 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700698 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200700 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000701 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200702 // Add all flags as volatile. Non volatile entries will not be overwritten.
703 // All new volatile flags added will be consumed by the end of this call.
704 SetFlags(packet_types, true);
705
706 // Prevent sending streams to send SR before any media has been sent.
707 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
708 if (!can_calculate_rtp_timestamp) {
709 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
710 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
711 bool sender_report = consumed_report_flag || consumed_sr_flag;
712 if (sender_report && AllVolatileFlagsConsumed()) {
713 // This call was for Sender Report and nothing else.
714 return 0;
715 }
716 if (sending_ && method_ == RtcpMode::kCompound) {
717 // Not allowed to send any RTCP packet without sender report.
718 return -1;
719 }
720 }
721
722 if (packet_type_counter_.first_packet_time_ms == -1)
723 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100724
725 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700726 RtcpContext context(feedback_state, nack_size, nack_list,
danilchap37953762017-02-09 11:15:25 -0800727 clock_->CurrentNtpTime());
Erik Språngf7c57762015-12-04 10:40:35 +0100728
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200729 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100730
danilchap56036ff2016-03-22 11:14:09 -0700731 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800732
Erik Språngf7c57762015-12-04 10:40:35 +0100733 auto it = report_flags_.begin();
734 while (it != report_flags_.end()) {
735 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800736 RTC_DCHECK(builder_it != builders_.end())
737 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100738 if (it->is_volatile) {
739 report_flags_.erase(it++);
740 } else {
741 ++it;
742 }
743
744 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700745 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100746 if (packet.get() == nullptr)
747 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800748 // If there is a BYE, don't append now - save it and append it
749 // at the end later.
750 if (builder_it->first == kRtcpBye) {
751 packet_bye = std::move(packet);
752 } else {
753 container.Append(packet.release());
754 }
755 }
756
757 // Append the BYE now at the end
758 if (packet_bye) {
759 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100760 }
761
762 if (packet_type_counter_observer_ != nullptr) {
763 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
764 remote_ssrc_, packet_type_counter_);
765 }
766
767 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800768 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000769 }
Erik Språng61be2a42015-04-27 13:32:52 +0200770
nisse6f142eb2017-02-21 07:32:47 -0800771 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100772 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000773}
774
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200775void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200776 bool generate_report;
777 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
778 // Report type already explicitly set, don't automatically populate.
779 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700780 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200781 } else {
782 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700783 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
784 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200785 if (generate_report)
786 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000787 }
788
Erik Språng0ea42d32015-06-25 14:46:16 +0200789 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200790 SetFlag(kRtcpSdes, true);
791
Erik Språng242e22b2015-05-11 10:17:43 +0200792 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800793 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
794 feedback_state.has_last_xr_rr || video_bitrate_allocation_) {
795 SetFlag(kRtcpAnyExtendedReports, true);
796 }
Erik Språng242e22b2015-05-11 10:17:43 +0200797
798 // generate next time to send an RTCP report
Jiawei Ou3587b832018-01-31 22:08:26 -0800799 uint32_t minIntervalMs =
800 rtc::dchecked_cast<uint32_t>(interval_config_.audio_interval_ms);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000801
danilchap47a740b2015-12-15 00:30:07 -0800802 if (!audio_) {
Erik Språng242e22b2015-05-11 10:17:43 +0200803 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200804 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
805 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
806 if (send_bitrate_kbit != 0)
807 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000808 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800809 if (minIntervalMs >
810 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms)) {
811 minIntervalMs =
812 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms);
813 }
Erik Språng61be2a42015-04-27 13:32:52 +0200814 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800815
danilchap47a740b2015-12-15 00:30:07 -0800816 // The interval between RTCP packets is varied randomly over the
817 // range [1/2,3/2] times the calculated interval.
818 uint32_t timeToNext =
819 random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
Erik Språng242e22b2015-05-11 10:17:43 +0200820 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000821
danilchap96b69bd2017-07-25 09:15:14 -0700822 // RtcpSender expected to be used for sending either just sender reports
823 // or just receiver reports.
824 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000825 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000826}
827
danilchap96b69bd2017-07-25 09:15:14 -0700828std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
829 const FeedbackState& feedback_state) {
830 std::vector<rtcp::ReportBlock> result;
831 if (!receive_statistics_)
832 return result;
danilchapa72e7342015-12-22 08:07:45 -0800833
danilchapf5f793c2017-07-27 04:44:18 -0700834 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
835 // compound rtcp packet when single rtcp module is used for multiple media
836 // streams.
837 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700838
839 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
840 (feedback_state.last_rr_ntp_frac != 0))) {
841 // Get our NTP as late as possible to avoid a race.
842 uint32_t now = CompactNtp(clock_->CurrentNtpTime());
843
844 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
845 receive_time <<= 16;
846 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
847
848 uint32_t delay_since_last_sr = now - receive_time;
849 // TODO(danilchap): Instead of setting same value on all report blocks,
850 // set only when media_ssrc match sender ssrc of the sender report
851 // remote times were taken from.
852 for (auto& report_block : result) {
853 report_block.SetLastSr(feedback_state.remote_sr);
854 report_block.SetDelayLastSr(delay_since_last_sr);
855 }
danilchapa72e7342015-12-22 08:07:45 -0800856 }
danilchap96b69bd2017-07-25 09:15:14 -0700857 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000858}
859
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000860void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800861 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700862 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000863 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000864}
865
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000866int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
867 uint32_t name,
868 const uint8_t* data,
869 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200870 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100871 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200872 return -1;
873 }
danilchap56036ff2016-03-22 11:14:09 -0700874 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000875
Erik Språng242e22b2015-05-11 10:17:43 +0200876 SetFlag(kRtcpApp, true);
877 app_sub_type_ = subType;
878 app_name_ = name;
879 app_data_.reset(new uint8_t[length]);
880 app_length_ = length;
881 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200882 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000883}
884
spranga790d832016-12-02 07:29:44 -0800885// TODO(sprang): Remove support for VoIP metrics? (Not used in receiver.)
Erik Språng61be2a42015-04-27 13:32:52 +0200886int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
danilchap56036ff2016-03-22 11:14:09 -0700887 rtc::CritScope lock(&critical_section_rtcp_sender_);
sprang5e38c962016-12-01 05:18:09 -0800888 xr_voip_metric_.emplace(*VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000889
sprang5e38c962016-12-01 05:18:09 -0800890 SetFlag(kRtcpAnyExtendedReports, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200891 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000892}
893
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000894void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700895 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200896 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000897}
898
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000899bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700900 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200901 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000902}
903
danilchap853ecb22016-08-22 08:26:15 -0700904void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700905 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700906 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700907 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000908}
Erik Språng61be2a42015-04-27 13:32:52 +0200909
sprang5e38c962016-12-01 05:18:09 -0800910void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
911 if (type & kRtcpAnyExtendedReports) {
912 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
913 } else {
914 report_flags_.insert(ReportFlag(type, is_volatile));
915 }
Erik Språng242e22b2015-05-11 10:17:43 +0200916}
917
918void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
919 bool is_volatile) {
920 for (RTCPPacketType type : types)
921 SetFlag(type, is_volatile);
922}
923
sprang5e38c962016-12-01 05:18:09 -0800924bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200925 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
926}
927
sprang5e38c962016-12-01 05:18:09 -0800928bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200929 auto it = report_flags_.find(ReportFlag(type, false));
930 if (it == report_flags_.end())
931 return false;
932 if (it->is_volatile || forced)
933 report_flags_.erase((it));
934 return true;
935}
936
937bool RTCPSender::AllVolatileFlagsConsumed() const {
938 for (const ReportFlag& flag : report_flags_) {
939 if (flag.is_volatile)
940 return false;
941 }
942 return true;
943}
944
sprang5e38c962016-12-01 05:18:09 -0800945void RTCPSender::SetVideoBitrateAllocation(const BitrateAllocation& bitrate) {
946 rtc::CritScope lock(&critical_section_rtcp_sender_);
947 video_bitrate_allocation_.emplace(bitrate);
948 SetFlag(kRtcpAnyExtendedReports, true);
949}
950
sprang233bd872015-09-08 13:25:16 -0700951bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800952 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800953 {
954 rtc::CritScope lock(&critical_section_rtcp_sender_);
955 if (method_ == RtcpMode::kOff)
956 return false;
nisse6f142eb2017-02-21 07:32:47 -0800957 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800958 }
959
nisse6f142eb2017-02-21 07:32:47 -0800960 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100961 bool send_failure = false;
962 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
963 if (transport_->SendRtcp(packet.data(), packet.size())) {
964 if (event_log_)
965 event_log_->Log(rtc::MakeUnique<RtcEventRtcpPacketOutgoing>(packet));
966 } else {
967 send_failure = true;
968 }
969 };
970 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700971}
972
Jiawei Ou3587b832018-01-31 22:08:26 -0800973int64_t RTCPSender::RtcpAudioReportInverval() const {
974 return interval_config_.audio_interval_ms;
975}
976
977int64_t RTCPSender::RtcpVideoReportInverval() const {
978 return interval_config_.video_interval_ms;
979}
980
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000981} // namespace webrtc