blob: 4bbc3079d55feb7ee9a8d35dc83c0006f4a45310 [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
Jiawei Ou8b5d9d82018-11-15 16:44:37 -080013#include <string.h> // memcpy
14#include <algorithm> // std::min
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Danil Chapovalov70ffead2016-07-20 15:26:59 +020016#include <utility>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020019#include "common_types.h" // NOLINT(build/include)
Elad Alon4a87e1c2017-10-03 16:11:34 +020020#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "logging/rtc_event_log/rtc_event_log.h"
22#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
26#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
Elad Alon7d6a4c02019-02-25 13:00:51 +010027#include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
35#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
36#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
37#include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
38#include "modules/rtp_rtcp/source/time_util.h"
39#include "modules/rtp_rtcp/source/tmmbr_help.h"
40#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/logging.h"
Jiawei Ou3587b832018-01-31 22:08:26 -080043#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/trace_event.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000045
niklase@google.com470e71d2011-07-07 08:21:25 +000046namespace webrtc {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000047
sprang5e38c962016-12-01 05:18:09 -080048namespace {
Niels Möller44b384d2018-10-05 11:15:57 +020049const uint32_t kRtcpAnyExtendedReports = kRtcpXrReceiverReferenceTime |
50 kRtcpXrDlrrReportBlock |
51 kRtcpXrTargetBitrate;
sprang5e38c962016-12-01 05:18:09 -080052} // namespace
53
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000054RTCPSender::FeedbackState::FeedbackState()
nisse40ba3ad2017-03-17 07:04:00 -070055 : packets_sent(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000056 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000057 send_bitrate(0),
58 last_rr_ntp_secs(0),
59 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000060 remote_sr(0),
danilchap162abd32015-12-10 02:39:40 -080061 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000062
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020063RTCPSender::FeedbackState::FeedbackState(const FeedbackState&) = default;
64
65RTCPSender::FeedbackState::FeedbackState(FeedbackState&&) = default;
66
67RTCPSender::FeedbackState::~FeedbackState() = default;
68
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010069class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010070 public:
terelius429c3452016-01-21 05:42:04 -080071 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010072 : transport_(transport), event_log_(event_log) {}
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010073 ~PacketContainer() override {
Erik Språngf7c57762015-12-04 10:40:35 +010074 for (RtcpPacket* packet : appended_packets_)
75 delete packet;
76 }
77
danilchap41befce2016-03-30 11:11:51 -070078 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010079 size_t bytes_sent = 0;
80 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
81 if (transport_->SendRtcp(packet.data(), packet.size())) {
82 bytes_sent += packet.size();
83 if (event_log_) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020084 event_log_->Log(
85 absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010086 }
87 }
88 });
89 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +010090 }
91
92 private:
93 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -080094 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -080095
96 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +010097};
98
99class RTCPSender::RtcpContext {
100 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200101 RtcpContext(const FeedbackState& feedback_state,
102 int32_t nack_size,
103 const uint16_t* nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200104 int64_t now_us)
Erik Språngf7c57762015-12-04 10:40:35 +0100105 : feedback_state_(feedback_state),
106 nack_size_(nack_size),
107 nack_list_(nack_list),
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200108 now_us_(now_us) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200109
Erik Språngf7c57762015-12-04 10:40:35 +0100110 const FeedbackState& feedback_state_;
111 const int32_t nack_size_;
112 const uint16_t* nack_list_;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200113 const int64_t now_us_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200114};
115
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000116RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000117 bool audio,
118 Clock* clock,
danilchapf5f793c2017-07-27 04:44:18 -0700119 ReceiveStatisticsProvider* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700120 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800121 RtcEventLog* event_log,
Jiawei Ou3587b832018-01-31 22:08:26 -0800122 Transport* outgoing_transport,
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800123 int report_interval_ms)
Peter Boströmac547a62015-09-17 23:03:57 +0200124 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200125 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800126 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700127 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800128 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700129 transport_(outgoing_transport),
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800130 report_interval_ms_(report_interval_ms),
Erik Språng242e22b2015-05-11 10:17:43 +0200131 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200132 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700133 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000134 last_rtp_timestamp_(0),
135 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200136 ssrc_(0),
137 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000138 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
Erik Språng242e22b2015-05-11 10:17:43 +0200140 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000141
Erik Språng242e22b2015-05-11 10:17:43 +0200142 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
danilchap2b616392016-08-18 06:17:42 -0700144 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200145 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800146 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000147
Erik Språng242e22b2015-05-11 10:17:43 +0200148 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200149 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200150 app_data_(nullptr),
151 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
Erik Språng242e22b2015-05-11 10:17:43 +0200153 xr_send_receiver_reference_time_enabled_(false),
Erik Språng8782a582018-10-04 15:36:06 +0200154 packet_type_counter_observer_(packet_type_counter_observer),
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200155 send_video_bitrate_allocation_(false),
156 last_payload_type_(-1) {
sprang86fd9ed2015-09-29 04:45:43 -0700157 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200158
159 builders_[kRtcpSr] = &RTCPSender::BuildSR;
160 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200161 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200162 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
163 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200164 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
165 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
166 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100167 builders_[kRtcpLossNotification] = &RTCPSender::BuildLossNotification;
Erik Språng242e22b2015-05-11 10:17:43 +0200168 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
169 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
170 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800171 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
danilchap162abd32015-12-10 02:39:40 -0800174RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000175
pbosda903ea2015-10-02 02:36:56 -0700176RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700177 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200178 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
skvlad1c392cc2016-04-01 14:46:44 -0700181void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700182 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000183
skvlad1c392cc2016-04-01 14:46:44 -0700184 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
185 // When switching on, reschedule the next packet
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800186 next_time_to_send_rtcp_ =
187 clock_->TimeInMilliseconds() + (report_interval_ms_ / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700188 }
189 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000190}
191
Erik Språng61be2a42015-04-27 13:32:52 +0200192bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700193 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200194 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
Erik Språng61be2a42015-04-27 13:32:52 +0200197int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
198 bool sending) {
199 bool sendRTCPBye = false;
200 {
danilchap56036ff2016-03-22 11:14:09 -0700201 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
pbosda903ea2015-10-02 02:36:56 -0700203 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200204 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200205 // Trigger RTCP bye
206 sendRTCPBye = true;
207 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 }
Erik Språng242e22b2015-05-11 10:17:43 +0200209 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200210 }
211 if (sendRTCPBye)
212 return SendRTCP(feedback_state, kRtcpBye);
213 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000214}
215
Elad Alon7d6a4c02019-02-25 13:00:51 +0100216int32_t RTCPSender::SendLossNotification(const FeedbackState& feedback_state,
217 uint16_t last_decoded_seq_num,
218 uint16_t last_received_seq_num,
219 bool decodability_flag) {
220 rtc::CritScope lock(&critical_section_rtcp_sender_);
221
222 loss_notification_state_.last_decoded_seq_num = last_decoded_seq_num;
223 loss_notification_state_.last_received_seq_num = last_received_seq_num;
224 loss_notification_state_.decodability_flag = decodability_flag;
225
226 SetFlag(kRtcpLossNotification, /*is_volatile=*/true);
227
228 // Send immediately.
229 return SendCompoundRTCP(feedback_state,
230 {RTCPPacketType::kRtcpLossNotification});
231}
232
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100233void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
234 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700235 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100236 remb_bitrate_ = bitrate_bps;
237 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000238
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200239 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000240 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
241 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200242 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000243}
244
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200245void RTCPSender::UnsetRemb() {
246 rtc::CritScope lock(&critical_section_rtcp_sender_);
247 // Stop sending REMB each report until it is reenabled and REMB data set.
248 ConsumeFlag(kRtcpRemb, /*forced=*/true);
249}
250
Erik Språng61be2a42015-04-27 13:32:52 +0200251bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700252 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200253 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000256void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700257 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200258 if (enable) {
259 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
260 } else {
261 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
262 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
nisse284542b2017-01-10 08:58:32 -0800265void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800266 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800267 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700268}
269
danilchap71fead22016-08-18 02:01:49 -0700270void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700271 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700272 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000273}
274
275void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200276 int64_t capture_time_ms,
277 int8_t payload_type) {
danilchap56036ff2016-03-22 11:14:09 -0700278 rtc::CritScope lock(&critical_section_rtcp_sender_);
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200279 // For compatibility with clients who don't set payload type correctly on all
280 // calls.
281 if (payload_type != -1) {
282 last_payload_type_ = payload_type;
283 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000284 last_rtp_timestamp_ = rtp_timestamp;
285 if (capture_time_ms < 0) {
286 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200287 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000288 } else {
289 last_frame_capture_time_ms_ = capture_time_ms;
290 }
291}
292
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200293void RTCPSender::SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) {
294 rtc::CritScope lock(&critical_section_rtcp_sender_);
295 rtp_clock_rates_khz_[payload_type] = rtp_clock_rate_hz / 1000;
296}
297
nisse14adba72017-03-20 03:52:39 -0700298uint32_t RTCPSender::SSRC() const {
299 rtc::CritScope lock(&critical_section_rtcp_sender_);
300 return ssrc_;
301}
302
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000303void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700304 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000305
Erik Språng242e22b2015-05-11 10:17:43 +0200306 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200307 // not first SetSSRC, probably due to a collision
308 // schedule a new RTCP report
309 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200310 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200311 }
Erik Språng242e22b2015-05-11 10:17:43 +0200312 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
Erik Språng61be2a42015-04-27 13:32:52 +0200315void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700316 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200317 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
Peter Boström9ba52f82015-06-01 14:12:28 +0200320int32_t RTCPSender::SetCNAME(const char* c_name) {
321 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000322 return -1;
323
kwiberg352444f2016-11-28 15:58:53 -0800324 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700325 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200326 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000327 return 0;
328}
329
Erik Språng0ea42d32015-06-25 14:46:16 +0200330int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700331 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800332 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700333 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700334 // One spot is reserved for ssrc_/cname_.
335 // TODO(danilchap): Add support for more than 30 contributes by sending
336 // several sdes packets.
337 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000338 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200339
340 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000341 return 0;
342}
343
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000344int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700345 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200346 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000347
Erik Språng242e22b2015-05-11 10:17:43 +0200348 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000349 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200350
Erik Språng242e22b2015-05-11 10:17:43 +0200351 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000352 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000353}
354
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000355bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800356 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800357 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000358
Jiawei Ou3587b832018-01-31 22:08:26 -0800359 For video we use a configurable interval (default: 1 second) for a BW
360 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
361 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
363
danilchap162abd32015-12-10 02:39:40 -0800364 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000365
danilchap162abd32015-12-10 02:39:40 -0800366 MAX RTCP BW is 5% if the session BW
367 A send report is approximately 65 bytes inc CNAME
368 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
danilchap162abd32015-12-10 02:39:40 -0800370 The RECOMMENDED value for the reduced minimum in seconds is 360
371 divided by the session bandwidth in kilobits/second. This minimum
372 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
danilchap162abd32015-12-10 02:39:40 -0800374 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800375 initial is true), the constant Tmin is set to half of the configured
376 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
danilchap162abd32015-12-10 02:39:40 -0800378 The interval between RTCP packets is varied randomly over the
379 range [0.5,1.5] times the calculated interval to avoid unintended
380 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
danilchap162abd32015-12-10 02:39:40 -0800382 if we send
383 If the participant is a sender (we_sent true), the constant C is
384 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
385 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
386 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000387
danilchap162abd32015-12-10 02:39:40 -0800388 if we receive only
389 If we_sent is not true, the constant C is set
390 to the average RTCP packet size divided by 75% of the RTCP
391 bandwidth. The constant n is set to the number of receivers
392 (members - senders). If the number of senders is greater than
393 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
danilchap162abd32015-12-10 02:39:40 -0800395 reconsideration NOT required for peer-to-peer
396 "timer reconsideration" is
397 employed. This algorithm implements a simple back-off mechanism
398 which causes users to hold back RTCP packet transmission if the
399 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
danilchap162abd32015-12-10 02:39:40 -0800401 n = number of members
402 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
danilchap162abd32015-12-10 02:39:40 -0800404 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
danilchap162abd32015-12-10 02:39:40 -0800406 4. The calculated interval T is set to a number uniformly distributed
407 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000408
danilchap162abd32015-12-10 02:39:40 -0800409 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
410 for the fact that the timer reconsideration algorithm converges to
411 a value of the RTCP bandwidth below the intended average
412 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
Erik Språng242e22b2015-05-11 10:17:43 +0200414 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000415
danilchap56036ff2016-03-22 11:14:09 -0700416 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000417
pbosda903ea2015-10-02 02:36:56 -0700418 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000419 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
Erik Språng242e22b2015-05-11 10:17:43 +0200421 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200422 // for video key-frames we want to send the RTCP before the large key-frame
423 // if we have a 100 ms margin
424 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
Erik Språng242e22b2015-05-11 10:17:43 +0200427 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200428 return true;
429 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200430 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200431 // wrap
432 return true;
433 }
434 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
danilchap56036ff2016-03-22 11:14:09 -0700437std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200438 // Timestamp shouldn't be estimated before first media frame.
439 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200440 // The timestamp of this RTCP packet should be estimated as the timestamp of
441 // the frame being captured at this moment. We are calculating that
442 // timestamp as the last frame's timestamp + the time since the last frame
443 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200444 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
445 if (rtp_rate <= 0) {
446 rtp_rate =
447 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
448 1000;
449 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200450 // Round now_us_ to the closest millisecond, because Ntp time is rounded
451 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200452 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700453 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200454 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
Erik Språngf7c57762015-12-04 10:40:35 +0100456 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700457 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200458 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700459 report->SetRtpTimestamp(rtp_timestamp);
460 report->SetPacketCount(ctx.feedback_state_.packets_sent);
461 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700462 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100463
danilchap56036ff2016-03-22 11:14:09 -0700464 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
danilchap56036ff2016-03-22 11:14:09 -0700467std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100468 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200469 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800470 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
Erik Språngf7c57762015-12-04 10:40:35 +0100472 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700473 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200474
danilchap74e8df8f2017-03-16 08:04:08 -0700475 for (const auto& it : csrc_cnames_)
476 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200477
danilchap56036ff2016-03-22 11:14:09 -0700478 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000479}
480
danilchap56036ff2016-03-22 11:14:09 -0700481std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100482 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700483 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700484 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200485
danilchap56036ff2016-03-22 11:14:09 -0700486 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000487}
488
danilchap56036ff2016-03-22 11:14:09 -0700489std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100490 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700491 pli->SetSenderSsrc(ssrc_);
492 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200493
Erik Språng242e22b2015-05-11 10:17:43 +0200494 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200495
danilchap56036ff2016-03-22 11:14:09 -0700496 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200497}
498
danilchap56036ff2016-03-22 11:14:09 -0700499std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800500 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000501
Erik Språngf7c57762015-12-04 10:40:35 +0100502 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700503 fir->SetSenderSsrc(ssrc_);
504 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200505
Erik Språng242e22b2015-05-11 10:17:43 +0200506 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200507
danilchap56036ff2016-03-22 11:14:09 -0700508 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000509}
510
danilchap56036ff2016-03-22 11:14:09 -0700511std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100512 const RtcpContext& ctx) {
513 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700514 remb->SetSenderSsrc(ssrc_);
515 remb->SetBitrateBps(remb_bitrate_);
516 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200517
danilchap56036ff2016-03-22 11:14:09 -0700518 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000519}
520
Erik Språng61be2a42015-04-27 13:32:52 +0200521void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700522 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700523 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000524}
525
danilchap56036ff2016-03-22 11:14:09 -0700526std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100527 const RtcpContext& ctx) {
528 if (ctx.feedback_state_.module == nullptr)
529 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200530 // Before sending the TMMBR check the received TMMBN, only an owner is
531 // allowed to raise the bitrate:
532 // * If the sender is an owner of the TMMBN -> send TMMBR
533 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000534
Erik Språng61be2a42015-04-27 13:32:52 +0200535 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700536 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
Erik Språng242e22b2015-05-11 10:17:43 +0200538 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
539 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200540 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700541 std::vector<rtcp::TmmbItem> candidates =
542 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000543
danilchap2b616392016-08-18 06:17:42 -0700544 if (!candidates.empty()) {
545 for (const auto& candidate : candidates) {
546 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
547 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100548 // Do not send the same tuple.
549 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200550 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000551 }
danilchap2b616392016-08-18 06:17:42 -0700552 if (!tmmbr_owner) {
553 // Use received bounding set as candidate set.
554 // Add current tuple.
555 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000556
danilchap2b616392016-08-18 06:17:42 -0700557 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700558 std::vector<rtcp::TmmbItem> bounding =
559 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700560 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
561 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100562 // Did not enter bounding set, no meaning to send this request.
563 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200564 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000565 }
Erik Språng61be2a42015-04-27 13:32:52 +0200566 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000567
danilchap2b616392016-08-18 06:17:42 -0700568 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100569 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700570
Erik Språngf7c57762015-12-04 10:40:35 +0100571 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700572 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800573 rtcp::TmmbItem request;
574 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700575 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800576 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700577 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100578
danilchap56036ff2016-03-22 11:14:09 -0700579 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200580}
581
danilchap56036ff2016-03-22 11:14:09 -0700582std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100583 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100584 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700585 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700586 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
587 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700588 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000589 }
Erik Språng61be2a42015-04-27 13:32:52 +0200590 }
sprangd83df502015-08-27 01:05:08 -0700591
danilchap56036ff2016-03-22 11:14:09 -0700592 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000593}
594
danilchap56036ff2016-03-22 11:14:09 -0700595std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100596 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700597 app->SetSsrc(ssrc_);
598 app->SetSubType(app_sub_type_);
599 app->SetName(app_name_);
600 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200601
danilchap56036ff2016-03-22 11:14:09 -0700602 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200603}
604
Elad Alon7d6a4c02019-02-25 13:00:51 +0100605std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
606 const RtcpContext& ctx) {
607 auto loss_notification = absl::make_unique<rtcp::LossNotification>(
608 loss_notification_state_.last_decoded_seq_num,
609 loss_notification_state_.last_received_seq_num,
610 loss_notification_state_.decodability_flag);
611 loss_notification->SetSenderSsrc(ssrc_);
612 loss_notification->SetMediaSsrc(remote_ssrc_);
613 return std::move(loss_notification);
614}
615
danilchap56036ff2016-03-22 11:14:09 -0700616std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100617 const RtcpContext& ctx) {
618 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700619 nack->SetSenderSsrc(ssrc_);
620 nack->SetMediaSsrc(remote_ssrc_);
621 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200622
623 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100624 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100625 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200626 }
Erik Språng61be2a42015-04-27 13:32:52 +0200627 packet_type_counter_.nack_requests = nack_stats_.requests();
628 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200629
Erik Språng242e22b2015-05-11 10:17:43 +0200630 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200631
danilchap56036ff2016-03-22 11:14:09 -0700632 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200633}
634
danilchap56036ff2016-03-22 11:14:09 -0700635std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100636 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700637 bye->SetSenderSsrc(ssrc_);
638 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700639
danilchap56036ff2016-03-22 11:14:09 -0700640 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000641}
642
sprang5e38c962016-12-01 05:18:09 -0800643std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100644 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800645 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700646 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000647
sprang5e38c962016-12-01 05:18:09 -0800648 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
649 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200650 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800651 xr->SetRrtr(rrtr);
652 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000653
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200654 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
655 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800656 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000657
Erik Språng8782a582018-10-04 15:36:06 +0200658 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800659 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000660
sprang5e38c962016-12-01 05:18:09 -0800661 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
662 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200663 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100664 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200665 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100666 }
sprang5e38c962016-12-01 05:18:09 -0800667 }
668 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000669
sprang5e38c962016-12-01 05:18:09 -0800670 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200671 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800672 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200673
sprang5e38c962016-12-01 05:18:09 -0800674 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000675}
676
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000677int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200678 RTCPPacketType packetType,
679 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700680 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200681 return SendCompoundRTCP(
682 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700683 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200684}
685
686int32_t RTCPSender::SendCompoundRTCP(
687 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100688 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200689 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700690 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800691 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800692 size_t max_packet_size;
693
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000694 {
danilchap56036ff2016-03-22 11:14:09 -0700695 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700696 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100697 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200698 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000699 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200700 // Add all flags as volatile. Non volatile entries will not be overwritten.
701 // All new volatile flags added will be consumed by the end of this call.
702 SetFlags(packet_types, true);
703
704 // Prevent sending streams to send SR before any media has been sent.
705 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
706 if (!can_calculate_rtp_timestamp) {
707 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
708 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
709 bool sender_report = consumed_report_flag || consumed_sr_flag;
710 if (sender_report && AllVolatileFlagsConsumed()) {
711 // This call was for Sender Report and nothing else.
712 return 0;
713 }
714 if (sending_ && method_ == RtcpMode::kCompound) {
715 // Not allowed to send any RTCP packet without sender report.
716 return -1;
717 }
718 }
719
720 if (packet_type_counter_.first_packet_time_ms == -1)
721 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100722
723 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700724 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200725 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100726
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200727 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100728
danilchap56036ff2016-03-22 11:14:09 -0700729 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800730
Erik Språngf7c57762015-12-04 10:40:35 +0100731 auto it = report_flags_.begin();
732 while (it != report_flags_.end()) {
733 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800734 RTC_DCHECK(builder_it != builders_.end())
735 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100736 if (it->is_volatile) {
737 report_flags_.erase(it++);
738 } else {
739 ++it;
740 }
741
742 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700743 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100744 if (packet.get() == nullptr)
745 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800746 // If there is a BYE, don't append now - save it and append it
747 // at the end later.
748 if (builder_it->first == kRtcpBye) {
749 packet_bye = std::move(packet);
750 } else {
751 container.Append(packet.release());
752 }
753 }
754
755 // Append the BYE now at the end
756 if (packet_bye) {
757 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100758 }
759
760 if (packet_type_counter_observer_ != nullptr) {
761 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
762 remote_ssrc_, packet_type_counter_);
763 }
764
765 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800766 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000767 }
Erik Språng61be2a42015-04-27 13:32:52 +0200768
nisse6f142eb2017-02-21 07:32:47 -0800769 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100770 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000771}
772
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200773void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200774 bool generate_report;
775 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
776 // Report type already explicitly set, don't automatically populate.
777 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700778 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200779 } else {
780 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700781 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
782 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200783 if (generate_report)
784 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000785 }
786
Erik Språng0ea42d32015-06-25 14:46:16 +0200787 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200788 SetFlag(kRtcpSdes, true);
789
Erik Språng242e22b2015-05-11 10:17:43 +0200790 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800791 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200792 !feedback_state.last_xr_rtis.empty() ||
793 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800794 SetFlag(kRtcpAnyExtendedReports, true);
795 }
Erik Språng242e22b2015-05-11 10:17:43 +0200796
797 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800798 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000799
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800800 if (!audio_ && sending_) {
801 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
802 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
803 if (send_bitrate_kbit != 0) {
804 min_interval_ms = 360000 / send_bitrate_kbit;
805 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800806 }
Erik Språng61be2a42015-04-27 13:32:52 +0200807 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800808
danilchap47a740b2015-12-15 00:30:07 -0800809 // The interval between RTCP packets is varied randomly over the
810 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800811 int time_to_next =
812 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
813
814 RTC_DCHECK_GT(time_to_next, 0);
815 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000816
danilchap96b69bd2017-07-25 09:15:14 -0700817 // RtcpSender expected to be used for sending either just sender reports
818 // or just receiver reports.
819 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000820 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000821}
822
danilchap96b69bd2017-07-25 09:15:14 -0700823std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
824 const FeedbackState& feedback_state) {
825 std::vector<rtcp::ReportBlock> result;
826 if (!receive_statistics_)
827 return result;
danilchapa72e7342015-12-22 08:07:45 -0800828
danilchapf5f793c2017-07-27 04:44:18 -0700829 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
830 // compound rtcp packet when single rtcp module is used for multiple media
831 // streams.
832 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700833
834 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
835 (feedback_state.last_rr_ntp_frac != 0))) {
836 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200837 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700838
839 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
840 receive_time <<= 16;
841 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
842
843 uint32_t delay_since_last_sr = now - receive_time;
844 // TODO(danilchap): Instead of setting same value on all report blocks,
845 // set only when media_ssrc match sender ssrc of the sender report
846 // remote times were taken from.
847 for (auto& report_block : result) {
848 report_block.SetLastSr(feedback_state.remote_sr);
849 report_block.SetDelayLastSr(delay_since_last_sr);
850 }
danilchapa72e7342015-12-22 08:07:45 -0800851 }
danilchap96b69bd2017-07-25 09:15:14 -0700852 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000853}
854
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000855void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800856 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700857 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000858 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000859}
860
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000861int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
862 uint32_t name,
863 const uint8_t* data,
864 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200865 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100866 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200867 return -1;
868 }
danilchap56036ff2016-03-22 11:14:09 -0700869 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000870
Erik Språng242e22b2015-05-11 10:17:43 +0200871 SetFlag(kRtcpApp, true);
872 app_sub_type_ = subType;
873 app_name_ = name;
874 app_data_.reset(new uint8_t[length]);
875 app_length_ = length;
876 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200877 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000878}
879
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000880void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700881 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200882 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000883}
884
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000885bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700886 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200887 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000888}
889
danilchap853ecb22016-08-22 08:26:15 -0700890void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700891 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700892 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700893 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000894}
Erik Språng61be2a42015-04-27 13:32:52 +0200895
sprang5e38c962016-12-01 05:18:09 -0800896void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
897 if (type & kRtcpAnyExtendedReports) {
898 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
899 } else {
900 report_flags_.insert(ReportFlag(type, is_volatile));
901 }
Erik Språng242e22b2015-05-11 10:17:43 +0200902}
903
904void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
905 bool is_volatile) {
906 for (RTCPPacketType type : types)
907 SetFlag(type, is_volatile);
908}
909
sprang5e38c962016-12-01 05:18:09 -0800910bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200911 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
912}
913
sprang5e38c962016-12-01 05:18:09 -0800914bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200915 auto it = report_flags_.find(ReportFlag(type, false));
916 if (it == report_flags_.end())
917 return false;
918 if (it->is_volatile || forced)
919 report_flags_.erase((it));
920 return true;
921}
922
923bool RTCPSender::AllVolatileFlagsConsumed() const {
924 for (const ReportFlag& flag : report_flags_) {
925 if (flag.is_volatile)
926 return false;
927 }
928 return true;
929}
930
Erik Språng566124a2018-04-23 12:32:22 +0200931void RTCPSender::SetVideoBitrateAllocation(
932 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800933 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200934 // Check if this allocation is first ever, or has a different set of
935 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
936 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200937 absl::optional<VideoBitrateAllocation> new_bitrate =
938 CheckAndUpdateLayerStructure(bitrate);
939 if (new_bitrate) {
940 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100941 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
942 << " with new layers enabled/disabled: "
943 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200944 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200945 } else {
946 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200947 }
948
Erik Språng8782a582018-10-04 15:36:06 +0200949 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800950 SetFlag(kRtcpAnyExtendedReports, true);
951}
952
Erik Språng1cd73912018-10-05 12:57:59 +0200953absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200954 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200955 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200956 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
957 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200958 if (!updated_bitrate &&
959 (bitrate.HasBitrate(si, ti) !=
960 video_bitrate_allocation_.HasBitrate(si, ti) ||
961 (bitrate.GetBitrate(si, ti) == 0) !=
962 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
963 updated_bitrate = bitrate;
964 }
965 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
966 bitrate.GetBitrate(si, ti) == 0) {
967 // Make sure this stream disabling is explicitly signaled.
968 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +0200969 }
970 }
971 }
972
Erik Språng1cd73912018-10-05 12:57:59 +0200973 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200974}
975
sprang233bd872015-09-08 13:25:16 -0700976bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800977 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800978 {
979 rtc::CritScope lock(&critical_section_rtcp_sender_);
980 if (method_ == RtcpMode::kOff)
981 return false;
nisse6f142eb2017-02-21 07:32:47 -0800982 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800983 }
984
nisse6f142eb2017-02-21 07:32:47 -0800985 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100986 bool send_failure = false;
987 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
988 if (transport_->SendRtcp(packet.data(), packet.size())) {
989 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200990 event_log_->Log(absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100991 } else {
992 send_failure = true;
993 }
994 };
995 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700996}
997
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000998} // namespace webrtc