blob: 55f545c9675f127f4596c23924b9acbfe124b1f6 [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020017#include "absl/memory/memory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Elad Alon4a87e1c2017-10-03 16:11:34 +020019#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "logging/rtc_event_log/rtc_event_log.h"
21#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
22#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
27#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
35#include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
36#include "modules/rtp_rtcp/source/time_util.h"
37#include "modules/rtp_rtcp/source/tmmbr_help.h"
38#include "rtc_base/checks.h"
39#include "rtc_base/constructormagic.h"
40#include "rtc_base/logging.h"
Jiawei Ou3587b832018-01-31 22:08:26 -080041#include "rtc_base/numerics/safe_conversions.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
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000052RTCPSender::FeedbackState::FeedbackState()
nisse40ba3ad2017-03-17 07:04:00 -070053 : packets_sent(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000054 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000055 send_bitrate(0),
56 last_rr_ntp_secs(0),
57 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000058 remote_sr(0),
danilchap162abd32015-12-10 02:39:40 -080059 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000060
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020061RTCPSender::FeedbackState::FeedbackState(const FeedbackState&) = default;
62
63RTCPSender::FeedbackState::FeedbackState(FeedbackState&&) = default;
64
65RTCPSender::FeedbackState::~FeedbackState() = default;
66
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010067class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010068 public:
terelius429c3452016-01-21 05:42:04 -080069 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010070 : transport_(transport), event_log_(event_log) {}
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010071 ~PacketContainer() override {
Erik Språngf7c57762015-12-04 10:40:35 +010072 for (RtcpPacket* packet : appended_packets_)
73 delete packet;
74 }
75
danilchap41befce2016-03-30 11:11:51 -070076 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010077 size_t bytes_sent = 0;
78 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
79 if (transport_->SendRtcp(packet.data(), packet.size())) {
80 bytes_sent += packet.size();
81 if (event_log_) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020082 event_log_->Log(
83 absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010084 }
85 }
86 });
87 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +010088 }
89
90 private:
91 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -080092 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -080093
94 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +010095};
96
97class RTCPSender::RtcpContext {
98 public:
Erik Språng242e22b2015-05-11 10:17:43 +020099 RtcpContext(const FeedbackState& feedback_state,
100 int32_t nack_size,
101 const uint16_t* nack_list,
danilchap51813b32016-12-16 02:44:36 -0800102 NtpTime now)
Erik Språngf7c57762015-12-04 10:40:35 +0100103 : feedback_state_(feedback_state),
104 nack_size_(nack_size),
105 nack_list_(nack_list),
danilchap51813b32016-12-16 02:44:36 -0800106 now_(now) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200107
Erik Språngf7c57762015-12-04 10:40:35 +0100108 const FeedbackState& feedback_state_;
109 const int32_t nack_size_;
110 const uint16_t* nack_list_;
danilchap51813b32016-12-16 02:44:36 -0800111 const NtpTime now_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200112};
113
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000114RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000115 bool audio,
116 Clock* clock,
danilchapf5f793c2017-07-27 04:44:18 -0700117 ReceiveStatisticsProvider* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700118 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800119 RtcEventLog* event_log,
Jiawei Ou3587b832018-01-31 22:08:26 -0800120 Transport* outgoing_transport,
121 RtcpIntervalConfig interval_config)
Peter Boströmac547a62015-09-17 23:03:57 +0200122 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200123 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800124 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700125 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800126 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700127 transport_(outgoing_transport),
Jiawei Ou3587b832018-01-31 22:08:26 -0800128 interval_config_(interval_config),
Erik Språng242e22b2015-05-11 10:17:43 +0200129 using_nack_(false),
130 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200131 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700132 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000133 last_rtp_timestamp_(0),
134 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200135 ssrc_(0),
136 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000137 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
Erik Språng242e22b2015-05-11 10:17:43 +0200139 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000140
Erik Språng242e22b2015-05-11 10:17:43 +0200141 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
danilchap2b616392016-08-18 06:17:42 -0700143 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200144 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800145 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000146
Erik Språng242e22b2015-05-11 10:17:43 +0200147 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200148 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200149 app_data_(nullptr),
150 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
Erik Språng242e22b2015-05-11 10:17:43 +0200152 xr_send_receiver_reference_time_enabled_(false),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000153 packet_type_counter_observer_(packet_type_counter_observer) {
sprang86fd9ed2015-09-29 04:45:43 -0700154 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200155
156 builders_[kRtcpSr] = &RTCPSender::BuildSR;
157 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200158 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200159 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
160 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200161 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
162 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
163 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
164 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
165 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
166 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800167 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
danilchap162abd32015-12-10 02:39:40 -0800170RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
pbosda903ea2015-10-02 02:36:56 -0700172RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700173 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200174 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
skvlad1c392cc2016-04-01 14:46:44 -0700177void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700178 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000179
skvlad1c392cc2016-04-01 14:46:44 -0700180 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
181 // When switching on, reschedule the next packet
Jiawei Ou3587b832018-01-31 22:08:26 -0800182 int64_t interval_ms = audio_ ? interval_config_.audio_interval_ms
183 : interval_config_.video_interval_ms;
184 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + (interval_ms / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700185 }
186 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Erik Språng61be2a42015-04-27 13:32:52 +0200189bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700190 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200191 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
Erik Språng61be2a42015-04-27 13:32:52 +0200194int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
195 bool sending) {
196 bool sendRTCPBye = false;
197 {
danilchap56036ff2016-03-22 11:14:09 -0700198 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
pbosda903ea2015-10-02 02:36:56 -0700200 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200201 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200202 // Trigger RTCP bye
203 sendRTCPBye = true;
204 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 }
Erik Språng242e22b2015-05-11 10:17:43 +0200206 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200207 }
208 if (sendRTCPBye)
209 return SendRTCP(feedback_state, kRtcpBye);
210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100213void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
214 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700215 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100216 remb_bitrate_ = bitrate_bps;
217 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000218
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200219 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000220 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
221 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200222 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000223}
224
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200225void RTCPSender::UnsetRemb() {
226 rtc::CritScope lock(&critical_section_rtcp_sender_);
227 // Stop sending REMB each report until it is reenabled and REMB data set.
228 ConsumeFlag(kRtcpRemb, /*forced=*/true);
229}
230
Erik Språng61be2a42015-04-27 13:32:52 +0200231bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700232 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200233 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234}
235
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000236void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700237 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200238 if (enable) {
239 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
240 } else {
241 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
242 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000243}
244
nisse284542b2017-01-10 08:58:32 -0800245void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800246 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800247 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700248}
249
danilchap71fead22016-08-18 02:01:49 -0700250void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700251 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700252 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000253}
254
255void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
256 int64_t capture_time_ms) {
danilchap56036ff2016-03-22 11:14:09 -0700257 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000258 last_rtp_timestamp_ = rtp_timestamp;
259 if (capture_time_ms < 0) {
260 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200261 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000262 } else {
263 last_frame_capture_time_ms_ = capture_time_ms;
264 }
265}
266
nisse14adba72017-03-20 03:52:39 -0700267uint32_t RTCPSender::SSRC() const {
268 rtc::CritScope lock(&critical_section_rtcp_sender_);
269 return ssrc_;
270}
271
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000272void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700273 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
Erik Språng242e22b2015-05-11 10:17:43 +0200275 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200276 // not first SetSSRC, probably due to a collision
277 // schedule a new RTCP report
278 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200279 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200280 }
Erik Språng242e22b2015-05-11 10:17:43 +0200281 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000282}
283
Erik Språng61be2a42015-04-27 13:32:52 +0200284void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700285 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200286 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000287}
288
Peter Boström9ba52f82015-06-01 14:12:28 +0200289int32_t RTCPSender::SetCNAME(const char* c_name) {
290 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000291 return -1;
292
kwiberg352444f2016-11-28 15:58:53 -0800293 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700294 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200295 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000296 return 0;
297}
298
Erik Språng0ea42d32015-06-25 14:46:16 +0200299int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700300 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800301 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700302 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700303 // One spot is reserved for ssrc_/cname_.
304 // TODO(danilchap): Add support for more than 30 contributes by sending
305 // several sdes packets.
306 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000307 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200308
309 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000310 return 0;
311}
312
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000313int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700314 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200315 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000316
Erik Språng242e22b2015-05-11 10:17:43 +0200317 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000318 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200319
Erik Språng242e22b2015-05-11 10:17:43 +0200320 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000321 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000324bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800325 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800326 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
Jiawei Ou3587b832018-01-31 22:08:26 -0800328 For video we use a configurable interval (default: 1 second) for a BW
329 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
330 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000331
332
danilchap162abd32015-12-10 02:39:40 -0800333 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000334
danilchap162abd32015-12-10 02:39:40 -0800335 MAX RTCP BW is 5% if the session BW
336 A send report is approximately 65 bytes inc CNAME
337 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000338
danilchap162abd32015-12-10 02:39:40 -0800339 The RECOMMENDED value for the reduced minimum in seconds is 360
340 divided by the session bandwidth in kilobits/second. This minimum
341 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000342
danilchap162abd32015-12-10 02:39:40 -0800343 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800344 initial is true), the constant Tmin is set to half of the configured
345 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000346
danilchap162abd32015-12-10 02:39:40 -0800347 The interval between RTCP packets is varied randomly over the
348 range [0.5,1.5] times the calculated interval to avoid unintended
349 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
danilchap162abd32015-12-10 02:39:40 -0800351 if we send
352 If the participant is a sender (we_sent true), the constant C is
353 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
354 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
355 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
danilchap162abd32015-12-10 02:39:40 -0800357 if we receive only
358 If we_sent is not true, the constant C is set
359 to the average RTCP packet size divided by 75% of the RTCP
360 bandwidth. The constant n is set to the number of receivers
361 (members - senders). If the number of senders is greater than
362 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
danilchap162abd32015-12-10 02:39:40 -0800364 reconsideration NOT required for peer-to-peer
365 "timer reconsideration" is
366 employed. This algorithm implements a simple back-off mechanism
367 which causes users to hold back RTCP packet transmission if the
368 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
danilchap162abd32015-12-10 02:39:40 -0800370 n = number of members
371 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000372
danilchap162abd32015-12-10 02:39:40 -0800373 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
danilchap162abd32015-12-10 02:39:40 -0800375 4. The calculated interval T is set to a number uniformly distributed
376 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
danilchap162abd32015-12-10 02:39:40 -0800378 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
379 for the fact that the timer reconsideration algorithm converges to
380 a value of the RTCP bandwidth below the intended average
381 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
Erik Språng242e22b2015-05-11 10:17:43 +0200383 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000384
danilchap56036ff2016-03-22 11:14:09 -0700385 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000386
pbosda903ea2015-10-02 02:36:56 -0700387 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
Erik Språng242e22b2015-05-11 10:17:43 +0200390 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200391 // for video key-frames we want to send the RTCP before the large key-frame
392 // if we have a 100 ms margin
393 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
394 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
Erik Språng242e22b2015-05-11 10:17:43 +0200396 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200397 return true;
398 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200399 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200400 // wrap
401 return true;
402 }
403 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000404}
405
danilchap56036ff2016-03-22 11:14:09 -0700406std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200407 // Timestamp shouldn't be estimated before first media frame.
408 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200409 // The timestamp of this RTCP packet should be estimated as the timestamp of
410 // the frame being captured at this moment. We are calculating that
411 // timestamp as the last frame's timestamp + the time since the last frame
412 // was captured.
solenbergb19d2882016-10-03 06:22:25 -0700413 uint32_t rtp_rate =
414 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) / 1000;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200415 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700416 timestamp_offset_ + last_rtp_timestamp_ +
solenbergb19d2882016-10-03 06:22:25 -0700417 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000418
Erik Språngf7c57762015-12-04 10:40:35 +0100419 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700420 report->SetSenderSsrc(ssrc_);
danilchap51813b32016-12-16 02:44:36 -0800421 report->SetNtp(ctx.now_);
danilchap822a16f2016-09-27 09:27:47 -0700422 report->SetRtpTimestamp(rtp_timestamp);
423 report->SetPacketCount(ctx.feedback_state_.packets_sent);
424 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700425 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100426
danilchap56036ff2016-03-22 11:14:09 -0700427 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000428}
429
danilchap56036ff2016-03-22 11:14:09 -0700430std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100431 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200432 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800433 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
Erik Språngf7c57762015-12-04 10:40:35 +0100435 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700436 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200437
danilchap74e8df8f2017-03-16 08:04:08 -0700438 for (const auto& it : csrc_cnames_)
439 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200440
danilchap56036ff2016-03-22 11:14:09 -0700441 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
danilchap56036ff2016-03-22 11:14:09 -0700444std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100445 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700446 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700447 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200448
danilchap56036ff2016-03-22 11:14:09 -0700449 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
danilchap56036ff2016-03-22 11:14:09 -0700452std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100453 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700454 pli->SetSenderSsrc(ssrc_);
455 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200456
Erik Språng242e22b2015-05-11 10:17:43 +0200457 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200458
danilchap56036ff2016-03-22 11:14:09 -0700459 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200460}
461
danilchap56036ff2016-03-22 11:14:09 -0700462std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800463 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
Erik Språngf7c57762015-12-04 10:40:35 +0100465 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700466 fir->SetSenderSsrc(ssrc_);
467 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200468
Erik Språng242e22b2015-05-11 10:17:43 +0200469 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200470
danilchap56036ff2016-03-22 11:14:09 -0700471 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000472}
473
danilchap56036ff2016-03-22 11:14:09 -0700474std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100475 const RtcpContext& ctx) {
476 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700477 remb->SetSenderSsrc(ssrc_);
478 remb->SetBitrateBps(remb_bitrate_);
479 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200480
danilchap56036ff2016-03-22 11:14:09 -0700481 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000482}
483
Erik Språng61be2a42015-04-27 13:32:52 +0200484void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700485 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700486 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000487}
488
danilchap56036ff2016-03-22 11:14:09 -0700489std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100490 const RtcpContext& ctx) {
491 if (ctx.feedback_state_.module == nullptr)
492 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200493 // Before sending the TMMBR check the received TMMBN, only an owner is
494 // allowed to raise the bitrate:
495 // * If the sender is an owner of the TMMBN -> send TMMBR
496 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000497
Erik Språng61be2a42015-04-27 13:32:52 +0200498 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700499 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000500
Erik Språng242e22b2015-05-11 10:17:43 +0200501 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
502 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200503 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700504 std::vector<rtcp::TmmbItem> candidates =
505 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
danilchap2b616392016-08-18 06:17:42 -0700507 if (!candidates.empty()) {
508 for (const auto& candidate : candidates) {
509 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
510 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100511 // Do not send the same tuple.
512 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
danilchap2b616392016-08-18 06:17:42 -0700515 if (!tmmbr_owner) {
516 // Use received bounding set as candidate set.
517 // Add current tuple.
518 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
danilchap2b616392016-08-18 06:17:42 -0700520 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700521 std::vector<rtcp::TmmbItem> bounding =
522 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700523 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
524 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100525 // Did not enter bounding set, no meaning to send this request.
526 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200527 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000528 }
Erik Språng61be2a42015-04-27 13:32:52 +0200529 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000530
danilchap2b616392016-08-18 06:17:42 -0700531 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100532 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700533
Erik Språngf7c57762015-12-04 10:40:35 +0100534 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700535 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800536 rtcp::TmmbItem request;
537 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700538 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800539 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700540 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100541
danilchap56036ff2016-03-22 11:14:09 -0700542 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200543}
544
danilchap56036ff2016-03-22 11:14:09 -0700545std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100546 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100547 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700548 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700549 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
550 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700551 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000552 }
Erik Språng61be2a42015-04-27 13:32:52 +0200553 }
sprangd83df502015-08-27 01:05:08 -0700554
danilchap56036ff2016-03-22 11:14:09 -0700555 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000556}
557
danilchap56036ff2016-03-22 11:14:09 -0700558std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100559 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700560 app->SetSsrc(ssrc_);
561 app->SetSubType(app_sub_type_);
562 app->SetName(app_name_);
563 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200564
danilchap56036ff2016-03-22 11:14:09 -0700565 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200566}
567
danilchap56036ff2016-03-22 11:14:09 -0700568std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100569 const RtcpContext& ctx) {
570 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700571 nack->SetSenderSsrc(ssrc_);
572 nack->SetMediaSsrc(remote_ssrc_);
573 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200574
575 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100576 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100577 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200578 }
Erik Språng61be2a42015-04-27 13:32:52 +0200579 packet_type_counter_.nack_requests = nack_stats_.requests();
580 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200581
Erik Språng242e22b2015-05-11 10:17:43 +0200582 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200583
danilchap56036ff2016-03-22 11:14:09 -0700584 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200585}
586
danilchap56036ff2016-03-22 11:14:09 -0700587std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100588 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700589 bye->SetSenderSsrc(ssrc_);
590 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700591
danilchap56036ff2016-03-22 11:14:09 -0700592 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593}
594
sprang5e38c962016-12-01 05:18:09 -0800595std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100596 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800597 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700598 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000599
sprang5e38c962016-12-01 05:18:09 -0800600 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
601 rtcp::Rrtr rrtr;
danilchap51813b32016-12-16 02:44:36 -0800602 rrtr.SetNtp(ctx.now_);
sprang5e38c962016-12-01 05:18:09 -0800603 xr->SetRrtr(rrtr);
604 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000605
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200606 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
607 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800608 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000609
sprang5e38c962016-12-01 05:18:09 -0800610 if (video_bitrate_allocation_) {
611 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000612
sprang5e38c962016-12-01 05:18:09 -0800613 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
614 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100615 if (video_bitrate_allocation_->HasBitrate(sl, tl)) {
616 target_bitrate.AddTargetBitrate(
617 sl, tl, video_bitrate_allocation_->GetBitrate(sl, tl) / 1000);
618 }
sprang5e38c962016-12-01 05:18:09 -0800619 }
620 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000621
sprang5e38c962016-12-01 05:18:09 -0800622 xr->SetTargetBitrate(target_bitrate);
623 video_bitrate_allocation_.reset();
624 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200625
sprang5e38c962016-12-01 05:18:09 -0800626 if (xr_voip_metric_) {
627 rtcp::VoipMetric voip;
628 voip.SetMediaSsrc(remote_ssrc_);
629 voip.SetVoipMetric(*xr_voip_metric_);
630 xr_voip_metric_.reset();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000631
sprang5e38c962016-12-01 05:18:09 -0800632 xr->SetVoipMetric(voip);
633 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200634
sprang5e38c962016-12-01 05:18:09 -0800635 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000636}
637
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000638int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200639 RTCPPacketType packetType,
640 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700641 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200642 return SendCompoundRTCP(
643 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700644 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200645}
646
647int32_t RTCPSender::SendCompoundRTCP(
648 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100649 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200650 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700651 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800652 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800653 size_t max_packet_size;
654
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000655 {
danilchap56036ff2016-03-22 11:14:09 -0700656 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700657 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100658 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200659 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000660 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200661 // Add all flags as volatile. Non volatile entries will not be overwritten.
662 // All new volatile flags added will be consumed by the end of this call.
663 SetFlags(packet_types, true);
664
665 // Prevent sending streams to send SR before any media has been sent.
666 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
667 if (!can_calculate_rtp_timestamp) {
668 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
669 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
670 bool sender_report = consumed_report_flag || consumed_sr_flag;
671 if (sender_report && AllVolatileFlagsConsumed()) {
672 // This call was for Sender Report and nothing else.
673 return 0;
674 }
675 if (sending_ && method_ == RtcpMode::kCompound) {
676 // Not allowed to send any RTCP packet without sender report.
677 return -1;
678 }
679 }
680
681 if (packet_type_counter_.first_packet_time_ms == -1)
682 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100683
684 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700685 RtcpContext context(feedback_state, nack_size, nack_list,
danilchap37953762017-02-09 11:15:25 -0800686 clock_->CurrentNtpTime());
Erik Språngf7c57762015-12-04 10:40:35 +0100687
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200688 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100689
danilchap56036ff2016-03-22 11:14:09 -0700690 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800691
Erik Språngf7c57762015-12-04 10:40:35 +0100692 auto it = report_flags_.begin();
693 while (it != report_flags_.end()) {
694 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800695 RTC_DCHECK(builder_it != builders_.end())
696 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100697 if (it->is_volatile) {
698 report_flags_.erase(it++);
699 } else {
700 ++it;
701 }
702
703 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700704 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100705 if (packet.get() == nullptr)
706 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800707 // If there is a BYE, don't append now - save it and append it
708 // at the end later.
709 if (builder_it->first == kRtcpBye) {
710 packet_bye = std::move(packet);
711 } else {
712 container.Append(packet.release());
713 }
714 }
715
716 // Append the BYE now at the end
717 if (packet_bye) {
718 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100719 }
720
721 if (packet_type_counter_observer_ != nullptr) {
722 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
723 remote_ssrc_, packet_type_counter_);
724 }
725
726 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800727 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000728 }
Erik Språng61be2a42015-04-27 13:32:52 +0200729
nisse6f142eb2017-02-21 07:32:47 -0800730 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100731 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000732}
733
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200734void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200735 bool generate_report;
736 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
737 // Report type already explicitly set, don't automatically populate.
738 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700739 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200740 } else {
741 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700742 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
743 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200744 if (generate_report)
745 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000746 }
747
Erik Språng0ea42d32015-06-25 14:46:16 +0200748 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200749 SetFlag(kRtcpSdes, true);
750
Erik Språng242e22b2015-05-11 10:17:43 +0200751 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800752 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200753 !feedback_state.last_xr_rtis.empty() || video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800754 SetFlag(kRtcpAnyExtendedReports, true);
755 }
Erik Språng242e22b2015-05-11 10:17:43 +0200756
757 // generate next time to send an RTCP report
Jiawei Ou3587b832018-01-31 22:08:26 -0800758 uint32_t minIntervalMs =
759 rtc::dchecked_cast<uint32_t>(interval_config_.audio_interval_ms);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000760
danilchap47a740b2015-12-15 00:30:07 -0800761 if (!audio_) {
Erik Språng242e22b2015-05-11 10:17:43 +0200762 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200763 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
764 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
765 if (send_bitrate_kbit != 0)
766 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000767 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800768 if (minIntervalMs >
769 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms)) {
770 minIntervalMs =
771 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms);
772 }
Erik Språng61be2a42015-04-27 13:32:52 +0200773 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800774
danilchap47a740b2015-12-15 00:30:07 -0800775 // The interval between RTCP packets is varied randomly over the
776 // range [1/2,3/2] times the calculated interval.
777 uint32_t timeToNext =
778 random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
Erik Språng242e22b2015-05-11 10:17:43 +0200779 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000780
danilchap96b69bd2017-07-25 09:15:14 -0700781 // RtcpSender expected to be used for sending either just sender reports
782 // or just receiver reports.
783 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000784 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000785}
786
danilchap96b69bd2017-07-25 09:15:14 -0700787std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
788 const FeedbackState& feedback_state) {
789 std::vector<rtcp::ReportBlock> result;
790 if (!receive_statistics_)
791 return result;
danilchapa72e7342015-12-22 08:07:45 -0800792
danilchapf5f793c2017-07-27 04:44:18 -0700793 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
794 // compound rtcp packet when single rtcp module is used for multiple media
795 // streams.
796 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700797
798 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
799 (feedback_state.last_rr_ntp_frac != 0))) {
800 // Get our NTP as late as possible to avoid a race.
801 uint32_t now = CompactNtp(clock_->CurrentNtpTime());
802
803 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
804 receive_time <<= 16;
805 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
806
807 uint32_t delay_since_last_sr = now - receive_time;
808 // TODO(danilchap): Instead of setting same value on all report blocks,
809 // set only when media_ssrc match sender ssrc of the sender report
810 // remote times were taken from.
811 for (auto& report_block : result) {
812 report_block.SetLastSr(feedback_state.remote_sr);
813 report_block.SetDelayLastSr(delay_since_last_sr);
814 }
danilchapa72e7342015-12-22 08:07:45 -0800815 }
danilchap96b69bd2017-07-25 09:15:14 -0700816 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000817}
818
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000819void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800820 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700821 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000822 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000823}
824
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000825int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
826 uint32_t name,
827 const uint8_t* data,
828 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200829 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100830 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200831 return -1;
832 }
danilchap56036ff2016-03-22 11:14:09 -0700833 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000834
Erik Språng242e22b2015-05-11 10:17:43 +0200835 SetFlag(kRtcpApp, true);
836 app_sub_type_ = subType;
837 app_name_ = name;
838 app_data_.reset(new uint8_t[length]);
839 app_length_ = length;
840 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200841 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000842}
843
spranga790d832016-12-02 07:29:44 -0800844// TODO(sprang): Remove support for VoIP metrics? (Not used in receiver.)
Erik Språng61be2a42015-04-27 13:32:52 +0200845int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
danilchap56036ff2016-03-22 11:14:09 -0700846 rtc::CritScope lock(&critical_section_rtcp_sender_);
sprang5e38c962016-12-01 05:18:09 -0800847 xr_voip_metric_.emplace(*VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000848
sprang5e38c962016-12-01 05:18:09 -0800849 SetFlag(kRtcpAnyExtendedReports, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200850 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000851}
852
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000853void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700854 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200855 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000856}
857
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000858bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700859 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200860 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000861}
862
danilchap853ecb22016-08-22 08:26:15 -0700863void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700864 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700865 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700866 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000867}
Erik Språng61be2a42015-04-27 13:32:52 +0200868
sprang5e38c962016-12-01 05:18:09 -0800869void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
870 if (type & kRtcpAnyExtendedReports) {
871 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
872 } else {
873 report_flags_.insert(ReportFlag(type, is_volatile));
874 }
Erik Språng242e22b2015-05-11 10:17:43 +0200875}
876
877void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
878 bool is_volatile) {
879 for (RTCPPacketType type : types)
880 SetFlag(type, is_volatile);
881}
882
sprang5e38c962016-12-01 05:18:09 -0800883bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200884 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
885}
886
sprang5e38c962016-12-01 05:18:09 -0800887bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200888 auto it = report_flags_.find(ReportFlag(type, false));
889 if (it == report_flags_.end())
890 return false;
891 if (it->is_volatile || forced)
892 report_flags_.erase((it));
893 return true;
894}
895
896bool RTCPSender::AllVolatileFlagsConsumed() const {
897 for (const ReportFlag& flag : report_flags_) {
898 if (flag.is_volatile)
899 return false;
900 }
901 return true;
902}
903
Erik Språng566124a2018-04-23 12:32:22 +0200904void RTCPSender::SetVideoBitrateAllocation(
905 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800906 rtc::CritScope lock(&critical_section_rtcp_sender_);
907 video_bitrate_allocation_.emplace(bitrate);
908 SetFlag(kRtcpAnyExtendedReports, true);
909}
910
sprang233bd872015-09-08 13:25:16 -0700911bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800912 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800913 {
914 rtc::CritScope lock(&critical_section_rtcp_sender_);
915 if (method_ == RtcpMode::kOff)
916 return false;
nisse6f142eb2017-02-21 07:32:47 -0800917 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800918 }
919
nisse6f142eb2017-02-21 07:32:47 -0800920 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100921 bool send_failure = false;
922 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
923 if (transport_->SendRtcp(packet.data(), packet.size())) {
924 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200925 event_log_->Log(absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100926 } else {
927 send_failure = true;
928 }
929 };
930 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700931}
932
Jiawei Ou3587b832018-01-31 22:08:26 -0800933int64_t RTCPSender::RtcpAudioReportInverval() const {
934 return interval_config_.audio_interval_ms;
935}
936
937int64_t RTCPSender::RtcpVideoReportInverval() const {
938 return interval_config_.video_interval_ms;
939}
940
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000941} // namespace webrtc