blob: e49fc62dddeea8ca69fc2fbda9f5a2b39ccb0271 [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),
Erik Språng8782a582018-10-04 15:36:06 +0200153 packet_type_counter_observer_(packet_type_counter_observer),
154 send_video_bitrate_allocation_(false) {
sprang86fd9ed2015-09-29 04:45:43 -0700155 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200156
157 builders_[kRtcpSr] = &RTCPSender::BuildSR;
158 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200159 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200160 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
161 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200162 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
163 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
164 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
165 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
166 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
167 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800168 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000169}
170
danilchap162abd32015-12-10 02:39:40 -0800171RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
pbosda903ea2015-10-02 02:36:56 -0700173RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700174 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200175 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176}
177
skvlad1c392cc2016-04-01 14:46:44 -0700178void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700179 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000180
skvlad1c392cc2016-04-01 14:46:44 -0700181 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
182 // When switching on, reschedule the next packet
Jiawei Ou3587b832018-01-31 22:08:26 -0800183 int64_t interval_ms = audio_ ? interval_config_.audio_interval_ms
184 : interval_config_.video_interval_ms;
185 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + (interval_ms / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700186 }
187 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188}
189
Erik Språng61be2a42015-04-27 13:32:52 +0200190bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700191 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200192 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000193}
194
Erik Språng61be2a42015-04-27 13:32:52 +0200195int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
196 bool sending) {
197 bool sendRTCPBye = false;
198 {
danilchap56036ff2016-03-22 11:14:09 -0700199 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200
pbosda903ea2015-10-02 02:36:56 -0700201 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200202 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200203 // Trigger RTCP bye
204 sendRTCPBye = true;
205 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 }
Erik Språng242e22b2015-05-11 10:17:43 +0200207 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200208 }
209 if (sendRTCPBye)
210 return SendRTCP(feedback_state, kRtcpBye);
211 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100214void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
215 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700216 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100217 remb_bitrate_ = bitrate_bps;
218 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000219
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200220 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000221 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
222 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200223 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000224}
225
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200226void RTCPSender::UnsetRemb() {
227 rtc::CritScope lock(&critical_section_rtcp_sender_);
228 // Stop sending REMB each report until it is reenabled and REMB data set.
229 ConsumeFlag(kRtcpRemb, /*forced=*/true);
230}
231
Erik Språng61be2a42015-04-27 13:32:52 +0200232bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700233 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200234 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235}
236
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000237void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700238 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200239 if (enable) {
240 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
241 } else {
242 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
243 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
nisse284542b2017-01-10 08:58:32 -0800246void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800247 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800248 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700249}
250
danilchap71fead22016-08-18 02:01:49 -0700251void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700252 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700253 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000254}
255
256void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
257 int64_t capture_time_ms) {
danilchap56036ff2016-03-22 11:14:09 -0700258 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000259 last_rtp_timestamp_ = rtp_timestamp;
260 if (capture_time_ms < 0) {
261 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200262 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000263 } else {
264 last_frame_capture_time_ms_ = capture_time_ms;
265 }
266}
267
nisse14adba72017-03-20 03:52:39 -0700268uint32_t RTCPSender::SSRC() const {
269 rtc::CritScope lock(&critical_section_rtcp_sender_);
270 return ssrc_;
271}
272
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000273void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700274 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000275
Erik Språng242e22b2015-05-11 10:17:43 +0200276 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200277 // not first SetSSRC, probably due to a collision
278 // schedule a new RTCP report
279 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200280 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200281 }
Erik Språng242e22b2015-05-11 10:17:43 +0200282 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283}
284
Erik Språng61be2a42015-04-27 13:32:52 +0200285void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700286 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200287 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000288}
289
Peter Boström9ba52f82015-06-01 14:12:28 +0200290int32_t RTCPSender::SetCNAME(const char* c_name) {
291 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000292 return -1;
293
kwiberg352444f2016-11-28 15:58:53 -0800294 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700295 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200296 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000297 return 0;
298}
299
Erik Språng0ea42d32015-06-25 14:46:16 +0200300int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700301 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800302 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700303 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700304 // One spot is reserved for ssrc_/cname_.
305 // TODO(danilchap): Add support for more than 30 contributes by sending
306 // several sdes packets.
307 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000308 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200309
310 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000311 return 0;
312}
313
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000314int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700315 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200316 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000317
Erik Språng242e22b2015-05-11 10:17:43 +0200318 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000319 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200320
Erik Språng242e22b2015-05-11 10:17:43 +0200321 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000322 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323}
324
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000325bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800326 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800327 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000328
Jiawei Ou3587b832018-01-31 22:08:26 -0800329 For video we use a configurable interval (default: 1 second) for a BW
330 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
331 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000332
333
danilchap162abd32015-12-10 02:39:40 -0800334 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000335
danilchap162abd32015-12-10 02:39:40 -0800336 MAX RTCP BW is 5% if the session BW
337 A send report is approximately 65 bytes inc CNAME
338 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000339
danilchap162abd32015-12-10 02:39:40 -0800340 The RECOMMENDED value for the reduced minimum in seconds is 360
341 divided by the session bandwidth in kilobits/second. This minimum
342 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
danilchap162abd32015-12-10 02:39:40 -0800344 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800345 initial is true), the constant Tmin is set to half of the configured
346 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
danilchap162abd32015-12-10 02:39:40 -0800348 The interval between RTCP packets is varied randomly over the
349 range [0.5,1.5] times the calculated interval to avoid unintended
350 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000351
danilchap162abd32015-12-10 02:39:40 -0800352 if we send
353 If the participant is a sender (we_sent true), the constant C is
354 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
355 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
356 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000357
danilchap162abd32015-12-10 02:39:40 -0800358 if we receive only
359 If we_sent is not true, the constant C is set
360 to the average RTCP packet size divided by 75% of the RTCP
361 bandwidth. The constant n is set to the number of receivers
362 (members - senders). If the number of senders is greater than
363 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000364
danilchap162abd32015-12-10 02:39:40 -0800365 reconsideration NOT required for peer-to-peer
366 "timer reconsideration" is
367 employed. This algorithm implements a simple back-off mechanism
368 which causes users to hold back RTCP packet transmission if the
369 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
danilchap162abd32015-12-10 02:39:40 -0800371 n = number of members
372 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
danilchap162abd32015-12-10 02:39:40 -0800374 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
danilchap162abd32015-12-10 02:39:40 -0800376 4. The calculated interval T is set to a number uniformly distributed
377 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000378
danilchap162abd32015-12-10 02:39:40 -0800379 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
380 for the fact that the timer reconsideration algorithm converges to
381 a value of the RTCP bandwidth below the intended average
382 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
Erik Språng242e22b2015-05-11 10:17:43 +0200384 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000385
danilchap56036ff2016-03-22 11:14:09 -0700386 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000387
pbosda903ea2015-10-02 02:36:56 -0700388 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000390
Erik Språng242e22b2015-05-11 10:17:43 +0200391 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200392 // for video key-frames we want to send the RTCP before the large key-frame
393 // if we have a 100 ms margin
394 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
395 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
Erik Språng242e22b2015-05-11 10:17:43 +0200397 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200398 return true;
399 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200400 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200401 // wrap
402 return true;
403 }
404 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000405}
406
danilchap56036ff2016-03-22 11:14:09 -0700407std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200408 // Timestamp shouldn't be estimated before first media frame.
409 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200410 // The timestamp of this RTCP packet should be estimated as the timestamp of
411 // the frame being captured at this moment. We are calculating that
412 // timestamp as the last frame's timestamp + the time since the last frame
413 // was captured.
solenbergb19d2882016-10-03 06:22:25 -0700414 uint32_t rtp_rate =
415 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) / 1000;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200416 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700417 timestamp_offset_ + last_rtp_timestamp_ +
solenbergb19d2882016-10-03 06:22:25 -0700418 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
Erik Språngf7c57762015-12-04 10:40:35 +0100420 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700421 report->SetSenderSsrc(ssrc_);
danilchap51813b32016-12-16 02:44:36 -0800422 report->SetNtp(ctx.now_);
danilchap822a16f2016-09-27 09:27:47 -0700423 report->SetRtpTimestamp(rtp_timestamp);
424 report->SetPacketCount(ctx.feedback_state_.packets_sent);
425 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700426 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100427
danilchap56036ff2016-03-22 11:14:09 -0700428 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000429}
430
danilchap56036ff2016-03-22 11:14:09 -0700431std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100432 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200433 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800434 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435
Erik Språngf7c57762015-12-04 10:40:35 +0100436 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700437 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200438
danilchap74e8df8f2017-03-16 08:04:08 -0700439 for (const auto& it : csrc_cnames_)
440 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200441
danilchap56036ff2016-03-22 11:14:09 -0700442 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
danilchap56036ff2016-03-22 11:14:09 -0700445std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100446 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700447 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700448 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200449
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::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100454 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700455 pli->SetSenderSsrc(ssrc_);
456 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200457
Erik Språng242e22b2015-05-11 10:17:43 +0200458 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200459
danilchap56036ff2016-03-22 11:14:09 -0700460 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200461}
462
danilchap56036ff2016-03-22 11:14:09 -0700463std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800464 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000465
Erik Språngf7c57762015-12-04 10:40:35 +0100466 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700467 fir->SetSenderSsrc(ssrc_);
468 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200469
Erik Språng242e22b2015-05-11 10:17:43 +0200470 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200471
danilchap56036ff2016-03-22 11:14:09 -0700472 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
danilchap56036ff2016-03-22 11:14:09 -0700475std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100476 const RtcpContext& ctx) {
477 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700478 remb->SetSenderSsrc(ssrc_);
479 remb->SetBitrateBps(remb_bitrate_);
480 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200481
danilchap56036ff2016-03-22 11:14:09 -0700482 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000483}
484
Erik Språng61be2a42015-04-27 13:32:52 +0200485void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700486 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700487 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000488}
489
danilchap56036ff2016-03-22 11:14:09 -0700490std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100491 const RtcpContext& ctx) {
492 if (ctx.feedback_state_.module == nullptr)
493 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200494 // Before sending the TMMBR check the received TMMBN, only an owner is
495 // allowed to raise the bitrate:
496 // * If the sender is an owner of the TMMBN -> send TMMBR
497 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000498
Erik Språng61be2a42015-04-27 13:32:52 +0200499 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700500 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
Erik Språng242e22b2015-05-11 10:17:43 +0200502 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
503 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200504 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700505 std::vector<rtcp::TmmbItem> candidates =
506 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
danilchap2b616392016-08-18 06:17:42 -0700508 if (!candidates.empty()) {
509 for (const auto& candidate : candidates) {
510 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
511 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100512 // Do not send the same tuple.
513 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200514 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 }
danilchap2b616392016-08-18 06:17:42 -0700516 if (!tmmbr_owner) {
517 // Use received bounding set as candidate set.
518 // Add current tuple.
519 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
danilchap2b616392016-08-18 06:17:42 -0700521 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700522 std::vector<rtcp::TmmbItem> bounding =
523 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700524 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
525 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100526 // Did not enter bounding set, no meaning to send this request.
527 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200528 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000529 }
Erik Språng61be2a42015-04-27 13:32:52 +0200530 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
danilchap2b616392016-08-18 06:17:42 -0700532 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100533 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700534
Erik Språngf7c57762015-12-04 10:40:35 +0100535 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700536 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800537 rtcp::TmmbItem request;
538 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700539 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800540 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700541 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100542
danilchap56036ff2016-03-22 11:14:09 -0700543 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200544}
545
danilchap56036ff2016-03-22 11:14:09 -0700546std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100547 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100548 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700549 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700550 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
551 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700552 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000553 }
Erik Språng61be2a42015-04-27 13:32:52 +0200554 }
sprangd83df502015-08-27 01:05:08 -0700555
danilchap56036ff2016-03-22 11:14:09 -0700556 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000557}
558
danilchap56036ff2016-03-22 11:14:09 -0700559std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100560 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700561 app->SetSsrc(ssrc_);
562 app->SetSubType(app_sub_type_);
563 app->SetName(app_name_);
564 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200565
danilchap56036ff2016-03-22 11:14:09 -0700566 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200567}
568
danilchap56036ff2016-03-22 11:14:09 -0700569std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100570 const RtcpContext& ctx) {
571 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700572 nack->SetSenderSsrc(ssrc_);
573 nack->SetMediaSsrc(remote_ssrc_);
574 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200575
576 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100577 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100578 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200579 }
Erik Språng61be2a42015-04-27 13:32:52 +0200580 packet_type_counter_.nack_requests = nack_stats_.requests();
581 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200582
Erik Språng242e22b2015-05-11 10:17:43 +0200583 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200584
danilchap56036ff2016-03-22 11:14:09 -0700585 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200586}
587
danilchap56036ff2016-03-22 11:14:09 -0700588std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100589 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700590 bye->SetSenderSsrc(ssrc_);
591 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700592
danilchap56036ff2016-03-22 11:14:09 -0700593 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594}
595
sprang5e38c962016-12-01 05:18:09 -0800596std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100597 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800598 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700599 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000600
sprang5e38c962016-12-01 05:18:09 -0800601 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
602 rtcp::Rrtr rrtr;
danilchap51813b32016-12-16 02:44:36 -0800603 rrtr.SetNtp(ctx.now_);
sprang5e38c962016-12-01 05:18:09 -0800604 xr->SetRrtr(rrtr);
605 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000606
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200607 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
608 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800609 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000610
Erik Språng8782a582018-10-04 15:36:06 +0200611 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800612 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000613
sprang5e38c962016-12-01 05:18:09 -0800614 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
615 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200616 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100617 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200618 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100619 }
sprang5e38c962016-12-01 05:18:09 -0800620 }
621 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000622
sprang5e38c962016-12-01 05:18:09 -0800623 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200624 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800625 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200626
sprang5e38c962016-12-01 05:18:09 -0800627 if (xr_voip_metric_) {
628 rtcp::VoipMetric voip;
629 voip.SetMediaSsrc(remote_ssrc_);
630 voip.SetVoipMetric(*xr_voip_metric_);
631 xr_voip_metric_.reset();
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000632
sprang5e38c962016-12-01 05:18:09 -0800633 xr->SetVoipMetric(voip);
634 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200635
sprang5e38c962016-12-01 05:18:09 -0800636 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000637}
638
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000639int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200640 RTCPPacketType packetType,
641 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700642 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200643 return SendCompoundRTCP(
644 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700645 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200646}
647
648int32_t RTCPSender::SendCompoundRTCP(
649 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100650 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200651 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700652 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800653 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800654 size_t max_packet_size;
655
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000656 {
danilchap56036ff2016-03-22 11:14:09 -0700657 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700658 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100659 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200660 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000661 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200662 // Add all flags as volatile. Non volatile entries will not be overwritten.
663 // All new volatile flags added will be consumed by the end of this call.
664 SetFlags(packet_types, true);
665
666 // Prevent sending streams to send SR before any media has been sent.
667 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
668 if (!can_calculate_rtp_timestamp) {
669 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
670 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
671 bool sender_report = consumed_report_flag || consumed_sr_flag;
672 if (sender_report && AllVolatileFlagsConsumed()) {
673 // This call was for Sender Report and nothing else.
674 return 0;
675 }
676 if (sending_ && method_ == RtcpMode::kCompound) {
677 // Not allowed to send any RTCP packet without sender report.
678 return -1;
679 }
680 }
681
682 if (packet_type_counter_.first_packet_time_ms == -1)
683 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100684
685 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700686 RtcpContext context(feedback_state, nack_size, nack_list,
danilchap37953762017-02-09 11:15:25 -0800687 clock_->CurrentNtpTime());
Erik Språngf7c57762015-12-04 10:40:35 +0100688
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200689 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100690
danilchap56036ff2016-03-22 11:14:09 -0700691 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800692
Erik Språngf7c57762015-12-04 10:40:35 +0100693 auto it = report_flags_.begin();
694 while (it != report_flags_.end()) {
695 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800696 RTC_DCHECK(builder_it != builders_.end())
697 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100698 if (it->is_volatile) {
699 report_flags_.erase(it++);
700 } else {
701 ++it;
702 }
703
704 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700705 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100706 if (packet.get() == nullptr)
707 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800708 // If there is a BYE, don't append now - save it and append it
709 // at the end later.
710 if (builder_it->first == kRtcpBye) {
711 packet_bye = std::move(packet);
712 } else {
713 container.Append(packet.release());
714 }
715 }
716
717 // Append the BYE now at the end
718 if (packet_bye) {
719 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100720 }
721
722 if (packet_type_counter_observer_ != nullptr) {
723 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
724 remote_ssrc_, packet_type_counter_);
725 }
726
727 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800728 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000729 }
Erik Språng61be2a42015-04-27 13:32:52 +0200730
nisse6f142eb2017-02-21 07:32:47 -0800731 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100732 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000733}
734
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200735void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200736 bool generate_report;
737 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
738 // Report type already explicitly set, don't automatically populate.
739 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700740 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200741 } else {
742 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700743 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
744 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200745 if (generate_report)
746 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000747 }
748
Erik Språng0ea42d32015-06-25 14:46:16 +0200749 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200750 SetFlag(kRtcpSdes, true);
751
Erik Språng242e22b2015-05-11 10:17:43 +0200752 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800753 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200754 !feedback_state.last_xr_rtis.empty() ||
755 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800756 SetFlag(kRtcpAnyExtendedReports, true);
757 }
Erik Språng242e22b2015-05-11 10:17:43 +0200758
759 // generate next time to send an RTCP report
Jiawei Ou3587b832018-01-31 22:08:26 -0800760 uint32_t minIntervalMs =
761 rtc::dchecked_cast<uint32_t>(interval_config_.audio_interval_ms);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000762
danilchap47a740b2015-12-15 00:30:07 -0800763 if (!audio_) {
Erik Språng242e22b2015-05-11 10:17:43 +0200764 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200765 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
766 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
767 if (send_bitrate_kbit != 0)
768 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000769 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800770 if (minIntervalMs >
771 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms)) {
772 minIntervalMs =
773 rtc::dchecked_cast<uint32_t>(interval_config_.video_interval_ms);
774 }
Erik Språng61be2a42015-04-27 13:32:52 +0200775 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800776
danilchap47a740b2015-12-15 00:30:07 -0800777 // The interval between RTCP packets is varied randomly over the
778 // range [1/2,3/2] times the calculated interval.
779 uint32_t timeToNext =
780 random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
Erik Språng242e22b2015-05-11 10:17:43 +0200781 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000782
danilchap96b69bd2017-07-25 09:15:14 -0700783 // RtcpSender expected to be used for sending either just sender reports
784 // or just receiver reports.
785 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000786 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000787}
788
danilchap96b69bd2017-07-25 09:15:14 -0700789std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
790 const FeedbackState& feedback_state) {
791 std::vector<rtcp::ReportBlock> result;
792 if (!receive_statistics_)
793 return result;
danilchapa72e7342015-12-22 08:07:45 -0800794
danilchapf5f793c2017-07-27 04:44:18 -0700795 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
796 // compound rtcp packet when single rtcp module is used for multiple media
797 // streams.
798 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700799
800 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
801 (feedback_state.last_rr_ntp_frac != 0))) {
802 // Get our NTP as late as possible to avoid a race.
803 uint32_t now = CompactNtp(clock_->CurrentNtpTime());
804
805 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
806 receive_time <<= 16;
807 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
808
809 uint32_t delay_since_last_sr = now - receive_time;
810 // TODO(danilchap): Instead of setting same value on all report blocks,
811 // set only when media_ssrc match sender ssrc of the sender report
812 // remote times were taken from.
813 for (auto& report_block : result) {
814 report_block.SetLastSr(feedback_state.remote_sr);
815 report_block.SetDelayLastSr(delay_since_last_sr);
816 }
danilchapa72e7342015-12-22 08:07:45 -0800817 }
danilchap96b69bd2017-07-25 09:15:14 -0700818 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000821void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800822 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700823 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000824 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000825}
826
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000827int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
828 uint32_t name,
829 const uint8_t* data,
830 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200831 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100832 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200833 return -1;
834 }
danilchap56036ff2016-03-22 11:14:09 -0700835 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000836
Erik Språng242e22b2015-05-11 10:17:43 +0200837 SetFlag(kRtcpApp, true);
838 app_sub_type_ = subType;
839 app_name_ = name;
840 app_data_.reset(new uint8_t[length]);
841 app_length_ = length;
842 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200843 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000844}
845
spranga790d832016-12-02 07:29:44 -0800846// TODO(sprang): Remove support for VoIP metrics? (Not used in receiver.)
Erik Språng61be2a42015-04-27 13:32:52 +0200847int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
danilchap56036ff2016-03-22 11:14:09 -0700848 rtc::CritScope lock(&critical_section_rtcp_sender_);
sprang5e38c962016-12-01 05:18:09 -0800849 xr_voip_metric_.emplace(*VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000850
sprang5e38c962016-12-01 05:18:09 -0800851 SetFlag(kRtcpAnyExtendedReports, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200852 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000853}
854
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000855void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700856 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200857 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000858}
859
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000860bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700861 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200862 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000863}
864
danilchap853ecb22016-08-22 08:26:15 -0700865void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700866 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700867 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700868 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000869}
Erik Språng61be2a42015-04-27 13:32:52 +0200870
sprang5e38c962016-12-01 05:18:09 -0800871void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
872 if (type & kRtcpAnyExtendedReports) {
873 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
874 } else {
875 report_flags_.insert(ReportFlag(type, is_volatile));
876 }
Erik Språng242e22b2015-05-11 10:17:43 +0200877}
878
879void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
880 bool is_volatile) {
881 for (RTCPPacketType type : types)
882 SetFlag(type, is_volatile);
883}
884
sprang5e38c962016-12-01 05:18:09 -0800885bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200886 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
887}
888
sprang5e38c962016-12-01 05:18:09 -0800889bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200890 auto it = report_flags_.find(ReportFlag(type, false));
891 if (it == report_flags_.end())
892 return false;
893 if (it->is_volatile || forced)
894 report_flags_.erase((it));
895 return true;
896}
897
898bool RTCPSender::AllVolatileFlagsConsumed() const {
899 for (const ReportFlag& flag : report_flags_) {
900 if (flag.is_volatile)
901 return false;
902 }
903 return true;
904}
905
Erik Språng566124a2018-04-23 12:32:22 +0200906void RTCPSender::SetVideoBitrateAllocation(
907 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800908 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200909 // Check if this allocation is first ever, or has a different set of
910 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
911 // as soon as possible.
912 if (HasNewLayerStructure(bitrate)) {
913 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
914 }
915
916 video_bitrate_allocation_ = bitrate;
917 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800918 SetFlag(kRtcpAnyExtendedReports, true);
919}
920
Erik Språng8782a582018-10-04 15:36:06 +0200921bool RTCPSender::HasNewLayerStructure(
922 const VideoBitrateAllocation& bitrate) const {
923 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
924 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
925 if (bitrate.HasBitrate(si, ti) !=
926 video_bitrate_allocation_.HasBitrate(si, ti) ||
927 (bitrate.GetBitrate(si, ti) == 0) !=
928 (video_bitrate_allocation_.GetBitrate(si, ti) == 0)) {
929 return true;
930 }
931 }
932 }
933
934 return false;
935}
936
sprang233bd872015-09-08 13:25:16 -0700937bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800938 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800939 {
940 rtc::CritScope lock(&critical_section_rtcp_sender_);
941 if (method_ == RtcpMode::kOff)
942 return false;
nisse6f142eb2017-02-21 07:32:47 -0800943 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800944 }
945
nisse6f142eb2017-02-21 07:32:47 -0800946 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100947 bool send_failure = false;
948 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
949 if (transport_->SendRtcp(packet.data(), packet.size())) {
950 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200951 event_log_->Log(absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100952 } else {
953 send_failure = true;
954 }
955 };
956 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700957}
958
Jiawei Ou3587b832018-01-31 22:08:26 -0800959int64_t RTCPSender::RtcpAudioReportInverval() const {
960 return interval_config_.audio_interval_ms;
961}
962
963int64_t RTCPSender::RtcpVideoReportInverval() const {
964 return interval_config_.video_interval_ms;
965}
966
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000967} // namespace webrtc