blob: 4c1a2410bb6a13696a3c85e53bd747194f0fc316 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020013#include <string.h> // memcpy
niklase@google.com470e71d2011-07-07 08:21:25 +000014
Jonas Olssona4d87372019-07-05 19:08:33 +020015#include <algorithm> // std::min
Danil Chapovalov70ffead2016-07-20 15:26:59 +020016#include <utility>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020019#include "api/rtc_event_log/rtc_event_log.h"
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 "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"
Elad Alon7d6a4c02019-02-25 13:00:51 +010026#include "modules/rtp_rtcp/source/rtcp_packet/loss_notification.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
28#include "modules/rtp_rtcp/source/rtcp_packet/pli.h"
29#include "modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
30#include "modules/rtp_rtcp/source/rtcp_packet/remb.h"
31#include "modules/rtp_rtcp/source/rtcp_packet/sdes.h"
32#include "modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
33#include "modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
34#include "modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
35#include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
36#include "modules/rtp_rtcp/source/rtp_rtcp_impl.h"
37#include "modules/rtp_rtcp/source/time_util.h"
38#include "modules/rtp_rtcp/source/tmmbr_help.h"
39#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080040#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/logging.h"
Jiawei Ou3587b832018-01-31 22:08:26 -080042#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/trace_event.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000044
niklase@google.com470e71d2011-07-07 08:21:25 +000045namespace webrtc {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000046
sprang5e38c962016-12-01 05:18:09 -080047namespace {
Niels Möller44b384d2018-10-05 11:15:57 +020048const uint32_t kRtcpAnyExtendedReports = kRtcpXrReceiverReferenceTime |
49 kRtcpXrDlrrReportBlock |
50 kRtcpXrTargetBitrate;
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +000051constexpr int32_t kDefaultVideoReportInterval = 1000;
52constexpr int32_t kDefaultAudioReportInterval = 5000;
sprang5e38c962016-12-01 05:18:09 -080053} // namespace
54
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000055RTCPSender::FeedbackState::FeedbackState()
nisse40ba3ad2017-03-17 07:04:00 -070056 : packets_sent(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000057 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000058 send_bitrate(0),
59 last_rr_ntp_secs(0),
60 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000061 remote_sr(0),
danilchap162abd32015-12-10 02:39:40 -080062 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000063
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020064RTCPSender::FeedbackState::FeedbackState(const FeedbackState&) = default;
65
66RTCPSender::FeedbackState::FeedbackState(FeedbackState&&) = default;
67
68RTCPSender::FeedbackState::~FeedbackState() = default;
69
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010070class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010071 public:
terelius429c3452016-01-21 05:42:04 -080072 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010073 : transport_(transport), event_log_(event_log) {}
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010074 ~PacketContainer() override {
Erik Språngf7c57762015-12-04 10:40:35 +010075 for (RtcpPacket* packet : appended_packets_)
76 delete packet;
77 }
78
danilchap41befce2016-03-30 11:11:51 -070079 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010080 size_t bytes_sent = 0;
81 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
82 if (transport_->SendRtcp(packet.data(), packet.size())) {
83 bytes_sent += packet.size();
84 if (event_log_) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020085 event_log_->Log(
86 absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010087 }
88 }
89 });
90 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +010091 }
92
93 private:
94 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -080095 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -080096
97 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +010098};
99
100class RTCPSender::RtcpContext {
101 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200102 RtcpContext(const FeedbackState& feedback_state,
103 int32_t nack_size,
104 const uint16_t* nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200105 int64_t now_us)
Erik Språngf7c57762015-12-04 10:40:35 +0100106 : feedback_state_(feedback_state),
107 nack_size_(nack_size),
108 nack_list_(nack_list),
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200109 now_us_(now_us) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200110
Erik Språngf7c57762015-12-04 10:40:35 +0100111 const FeedbackState& feedback_state_;
112 const int32_t nack_size_;
113 const uint16_t* nack_list_;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200114 const int64_t now_us_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200115};
116
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000117RTCPSender::RTCPSender(const RtpRtcp::Configuration& config)
118 : audio_(config.audio),
119 clock_(config.clock),
danilchap47a740b2015-12-15 00:30:07 -0800120 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700121 method_(RtcpMode::kOff),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000122 event_log_(config.event_log),
123 transport_(config.outgoing_transport),
124 report_interval_ms_(config.rtcp_report_interval_ms > 0
125 ? config.rtcp_report_interval_ms
126 : (config.audio ? kDefaultAudioReportInterval
127 : kDefaultVideoReportInterval)),
Erik Språng242e22b2015-05-11 10:17:43 +0200128 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200129 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700130 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000131 last_rtp_timestamp_(0),
132 last_frame_capture_time_ms_(-1),
Erik Språngc15f92a2019-08-21 15:54:16 +0200133 ssrc_(config.local_media_ssrc.value_or(0)),
Erik Språng242e22b2015-05-11 10:17:43 +0200134 remote_ssrc_(0),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000135 receive_statistics_(config.receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
Erik Språng242e22b2015-05-11 10:17:43 +0200137 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000138
Erik Språng242e22b2015-05-11 10:17:43 +0200139 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
danilchap2b616392016-08-18 06:17:42 -0700141 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200142 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800143 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000144
Erik Språng242e22b2015-05-11 10:17:43 +0200145 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200146 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200147 app_data_(nullptr),
148 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
Erik Språng242e22b2015-05-11 10:17:43 +0200150 xr_send_receiver_reference_time_enabled_(false),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000151 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200152 send_video_bitrate_allocation_(false),
153 last_payload_type_(-1) {
sprang86fd9ed2015-09-29 04:45:43 -0700154 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200155
156 builders_[kRtcpSr] = &RTCPSender::BuildSR;
157 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200158 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200159 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
160 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200161 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
162 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
163 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100164 builders_[kRtcpLossNotification] = &RTCPSender::BuildLossNotification;
Erik Språng242e22b2015-05-11 10:17:43 +0200165 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 Ou8b5d9d82018-11-15 16:44:37 -0800183 next_time_to_send_rtcp_ =
184 clock_->TimeInMilliseconds() + (report_interval_ms_ / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700185 }
186 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187}
188
Erik Språng61be2a42015-04-27 13:32:52 +0200189bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700190 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200191 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192}
193
Erik Språng61be2a42015-04-27 13:32:52 +0200194int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
195 bool sending) {
196 bool sendRTCPBye = false;
197 {
danilchap56036ff2016-03-22 11:14:09 -0700198 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
pbosda903ea2015-10-02 02:36:56 -0700200 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200201 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200202 // Trigger RTCP bye
203 sendRTCPBye = true;
204 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 }
Erik Språng242e22b2015-05-11 10:17:43 +0200206 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200207 }
208 if (sendRTCPBye)
209 return SendRTCP(feedback_state, kRtcpBye);
210 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
Elad Alon7d6a4c02019-02-25 13:00:51 +0100213int32_t RTCPSender::SendLossNotification(const FeedbackState& feedback_state,
214 uint16_t last_decoded_seq_num,
215 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200216 bool decodability_flag,
217 bool buffering_allowed) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100218 rtc::CritScope lock(&critical_section_rtcp_sender_);
219
220 loss_notification_state_.last_decoded_seq_num = last_decoded_seq_num;
221 loss_notification_state_.last_received_seq_num = last_received_seq_num;
222 loss_notification_state_.decodability_flag = decodability_flag;
223
224 SetFlag(kRtcpLossNotification, /*is_volatile=*/true);
225
Elad Alone86af2c2019-06-03 14:37:50 +0200226 if (buffering_allowed) {
227 // The loss notification will be batched with additional feedback messages.
228 return 0;
229 }
230
Elad Alon7d6a4c02019-02-25 13:00:51 +0100231 return SendCompoundRTCP(feedback_state,
232 {RTCPPacketType::kRtcpLossNotification});
233}
234
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100235void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
236 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700237 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100238 remb_bitrate_ = bitrate_bps;
239 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000240
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200241 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000242 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
243 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200244 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000245}
246
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200247void RTCPSender::UnsetRemb() {
248 rtc::CritScope lock(&critical_section_rtcp_sender_);
249 // Stop sending REMB each report until it is reenabled and REMB data set.
250 ConsumeFlag(kRtcpRemb, /*forced=*/true);
251}
252
Erik Språng61be2a42015-04-27 13:32:52 +0200253bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700254 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200255 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000256}
257
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000258void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700259 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200260 if (enable) {
261 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
262 } else {
263 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
264 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000265}
266
nisse284542b2017-01-10 08:58:32 -0800267void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800268 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800269 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700270}
271
danilchap71fead22016-08-18 02:01:49 -0700272void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700273 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700274 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000275}
276
277void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200278 int64_t capture_time_ms,
279 int8_t payload_type) {
danilchap56036ff2016-03-22 11:14:09 -0700280 rtc::CritScope lock(&critical_section_rtcp_sender_);
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200281 // For compatibility with clients who don't set payload type correctly on all
282 // calls.
283 if (payload_type != -1) {
284 last_payload_type_ = payload_type;
285 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000286 last_rtp_timestamp_ = rtp_timestamp;
287 if (capture_time_ms < 0) {
288 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200289 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000290 } else {
291 last_frame_capture_time_ms_ = capture_time_ms;
292 }
293}
294
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200295void RTCPSender::SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) {
296 rtc::CritScope lock(&critical_section_rtcp_sender_);
297 rtp_clock_rates_khz_[payload_type] = rtp_clock_rate_hz / 1000;
298}
299
nisse14adba72017-03-20 03:52:39 -0700300uint32_t RTCPSender::SSRC() const {
301 rtc::CritScope lock(&critical_section_rtcp_sender_);
302 return ssrc_;
303}
304
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000305void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700306 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000307
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000308 if (ssrc_ != 0 && ssrc != ssrc_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200309 // not first SetSSRC, probably due to a collision
310 // schedule a new RTCP report
311 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200312 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200313 }
Erik Språng242e22b2015-05-11 10:17:43 +0200314 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
Erik Språng61be2a42015-04-27 13:32:52 +0200317void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700318 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200319 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
Peter Boström9ba52f82015-06-01 14:12:28 +0200322int32_t RTCPSender::SetCNAME(const char* c_name) {
323 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000324 return -1;
325
kwiberg352444f2016-11-28 15:58:53 -0800326 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700327 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200328 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000329 return 0;
330}
331
Erik Språng0ea42d32015-06-25 14:46:16 +0200332int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700333 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800334 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700335 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700336 // One spot is reserved for ssrc_/cname_.
337 // TODO(danilchap): Add support for more than 30 contributes by sending
338 // several sdes packets.
339 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000340 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200341
342 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000343 return 0;
344}
345
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000346int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700347 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200348 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000349
Erik Språng242e22b2015-05-11 10:17:43 +0200350 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000351 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200352
Erik Språng242e22b2015-05-11 10:17:43 +0200353 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000357bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800358 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800359 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
Jiawei Ou3587b832018-01-31 22:08:26 -0800361 For video we use a configurable interval (default: 1 second) for a BW
362 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
363 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000364
365
danilchap162abd32015-12-10 02:39:40 -0800366 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000367
danilchap162abd32015-12-10 02:39:40 -0800368 MAX RTCP BW is 5% if the session BW
369 A send report is approximately 65 bytes inc CNAME
370 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
danilchap162abd32015-12-10 02:39:40 -0800372 The RECOMMENDED value for the reduced minimum in seconds is 360
373 divided by the session bandwidth in kilobits/second. This minimum
374 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
danilchap162abd32015-12-10 02:39:40 -0800376 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800377 initial is true), the constant Tmin is set to half of the configured
378 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000379
danilchap162abd32015-12-10 02:39:40 -0800380 The interval between RTCP packets is varied randomly over the
381 range [0.5,1.5] times the calculated interval to avoid unintended
382 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
danilchap162abd32015-12-10 02:39:40 -0800384 if we send
385 If the participant is a sender (we_sent true), the constant C is
386 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
387 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
388 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
danilchap162abd32015-12-10 02:39:40 -0800390 if we receive only
391 If we_sent is not true, the constant C is set
392 to the average RTCP packet size divided by 75% of the RTCP
393 bandwidth. The constant n is set to the number of receivers
394 (members - senders). If the number of senders is greater than
395 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
danilchap162abd32015-12-10 02:39:40 -0800397 reconsideration NOT required for peer-to-peer
398 "timer reconsideration" is
399 employed. This algorithm implements a simple back-off mechanism
400 which causes users to hold back RTCP packet transmission if the
401 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
danilchap162abd32015-12-10 02:39:40 -0800403 n = number of members
404 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
danilchap162abd32015-12-10 02:39:40 -0800406 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
danilchap162abd32015-12-10 02:39:40 -0800408 4. The calculated interval T is set to a number uniformly distributed
409 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
danilchap162abd32015-12-10 02:39:40 -0800411 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
412 for the fact that the timer reconsideration algorithm converges to
413 a value of the RTCP bandwidth below the intended average
414 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
Erik Språng242e22b2015-05-11 10:17:43 +0200416 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000417
danilchap56036ff2016-03-22 11:14:09 -0700418 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000419
pbosda903ea2015-10-02 02:36:56 -0700420 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000421 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
Erik Språng242e22b2015-05-11 10:17:43 +0200423 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200424 // for video key-frames we want to send the RTCP before the large key-frame
425 // if we have a 100 ms margin
426 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
427 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
Erik Språng242e22b2015-05-11 10:17:43 +0200429 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200430 return true;
431 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200432 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200433 // wrap
434 return true;
435 }
436 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437}
438
danilchap56036ff2016-03-22 11:14:09 -0700439std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200440 // Timestamp shouldn't be estimated before first media frame.
441 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200442 // The timestamp of this RTCP packet should be estimated as the timestamp of
443 // the frame being captured at this moment. We are calculating that
444 // timestamp as the last frame's timestamp + the time since the last frame
445 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200446 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
447 if (rtp_rate <= 0) {
448 rtp_rate =
449 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
450 1000;
451 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200452 // Round now_us_ to the closest millisecond, because Ntp time is rounded
453 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200454 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700455 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200456 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
Erik Språngf7c57762015-12-04 10:40:35 +0100458 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700459 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200460 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700461 report->SetRtpTimestamp(rtp_timestamp);
462 report->SetPacketCount(ctx.feedback_state_.packets_sent);
463 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700464 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100465
danilchap56036ff2016-03-22 11:14:09 -0700466 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000467}
468
danilchap56036ff2016-03-22 11:14:09 -0700469std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100470 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200471 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800472 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000473
Erik Språngf7c57762015-12-04 10:40:35 +0100474 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700475 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200476
danilchap74e8df8f2017-03-16 08:04:08 -0700477 for (const auto& it : csrc_cnames_)
478 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200479
danilchap56036ff2016-03-22 11:14:09 -0700480 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
danilchap56036ff2016-03-22 11:14:09 -0700483std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100484 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700485 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700486 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200487
danilchap56036ff2016-03-22 11:14:09 -0700488 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000489}
490
danilchap56036ff2016-03-22 11:14:09 -0700491std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100492 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700493 pli->SetSenderSsrc(ssrc_);
494 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200495
Erik Språng242e22b2015-05-11 10:17:43 +0200496 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200497
danilchap56036ff2016-03-22 11:14:09 -0700498 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200499}
500
danilchap56036ff2016-03-22 11:14:09 -0700501std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800502 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000503
Erik Språngf7c57762015-12-04 10:40:35 +0100504 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700505 fir->SetSenderSsrc(ssrc_);
506 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200507
Erik Språng242e22b2015-05-11 10:17:43 +0200508 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200509
danilchap56036ff2016-03-22 11:14:09 -0700510 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000511}
512
danilchap56036ff2016-03-22 11:14:09 -0700513std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100514 const RtcpContext& ctx) {
515 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700516 remb->SetSenderSsrc(ssrc_);
517 remb->SetBitrateBps(remb_bitrate_);
518 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200519
danilchap56036ff2016-03-22 11:14:09 -0700520 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000521}
522
Erik Språng61be2a42015-04-27 13:32:52 +0200523void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700524 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700525 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000526}
527
danilchap56036ff2016-03-22 11:14:09 -0700528std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100529 const RtcpContext& ctx) {
530 if (ctx.feedback_state_.module == nullptr)
531 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200532 // Before sending the TMMBR check the received TMMBN, only an owner is
533 // allowed to raise the bitrate:
534 // * If the sender is an owner of the TMMBN -> send TMMBR
535 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
Erik Språng61be2a42015-04-27 13:32:52 +0200537 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700538 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000539
Erik Språng242e22b2015-05-11 10:17:43 +0200540 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
541 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200542 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700543 std::vector<rtcp::TmmbItem> candidates =
544 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545
danilchap2b616392016-08-18 06:17:42 -0700546 if (!candidates.empty()) {
547 for (const auto& candidate : candidates) {
548 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
549 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100550 // Do not send the same tuple.
551 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200552 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 }
danilchap2b616392016-08-18 06:17:42 -0700554 if (!tmmbr_owner) {
555 // Use received bounding set as candidate set.
556 // Add current tuple.
557 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000558
danilchap2b616392016-08-18 06:17:42 -0700559 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700560 std::vector<rtcp::TmmbItem> bounding =
561 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700562 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
563 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100564 // Did not enter bounding set, no meaning to send this request.
565 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200566 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000567 }
Erik Språng61be2a42015-04-27 13:32:52 +0200568 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
danilchap2b616392016-08-18 06:17:42 -0700570 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100571 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700572
Erik Språngf7c57762015-12-04 10:40:35 +0100573 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700574 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800575 rtcp::TmmbItem request;
576 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700577 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800578 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700579 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100580
danilchap56036ff2016-03-22 11:14:09 -0700581 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200582}
583
danilchap56036ff2016-03-22 11:14:09 -0700584std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100585 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100586 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700587 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700588 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
589 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700590 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000591 }
Erik Språng61be2a42015-04-27 13:32:52 +0200592 }
sprangd83df502015-08-27 01:05:08 -0700593
danilchap56036ff2016-03-22 11:14:09 -0700594 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000595}
596
danilchap56036ff2016-03-22 11:14:09 -0700597std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100598 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700599 app->SetSsrc(ssrc_);
600 app->SetSubType(app_sub_type_);
601 app->SetName(app_name_);
602 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200603
danilchap56036ff2016-03-22 11:14:09 -0700604 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200605}
606
Elad Alon7d6a4c02019-02-25 13:00:51 +0100607std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
608 const RtcpContext& ctx) {
609 auto loss_notification = absl::make_unique<rtcp::LossNotification>(
610 loss_notification_state_.last_decoded_seq_num,
611 loss_notification_state_.last_received_seq_num,
612 loss_notification_state_.decodability_flag);
613 loss_notification->SetSenderSsrc(ssrc_);
614 loss_notification->SetMediaSsrc(remote_ssrc_);
615 return std::move(loss_notification);
616}
617
danilchap56036ff2016-03-22 11:14:09 -0700618std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100619 const RtcpContext& ctx) {
620 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700621 nack->SetSenderSsrc(ssrc_);
622 nack->SetMediaSsrc(remote_ssrc_);
623 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200624
625 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100626 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100627 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200628 }
Erik Språng61be2a42015-04-27 13:32:52 +0200629 packet_type_counter_.nack_requests = nack_stats_.requests();
630 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200631
Erik Språng242e22b2015-05-11 10:17:43 +0200632 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200633
danilchap56036ff2016-03-22 11:14:09 -0700634 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200635}
636
danilchap56036ff2016-03-22 11:14:09 -0700637std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100638 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700639 bye->SetSenderSsrc(ssrc_);
640 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700641
danilchap56036ff2016-03-22 11:14:09 -0700642 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
sprang5e38c962016-12-01 05:18:09 -0800645std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100646 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800647 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700648 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000649
sprang5e38c962016-12-01 05:18:09 -0800650 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
651 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200652 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800653 xr->SetRrtr(rrtr);
654 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000655
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200656 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
657 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800658 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000659
Erik Språng8782a582018-10-04 15:36:06 +0200660 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800661 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000662
sprang5e38c962016-12-01 05:18:09 -0800663 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
664 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200665 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100666 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200667 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100668 }
sprang5e38c962016-12-01 05:18:09 -0800669 }
670 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000671
sprang5e38c962016-12-01 05:18:09 -0800672 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200673 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800674 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200675
sprang5e38c962016-12-01 05:18:09 -0800676 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000677}
678
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000679int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200680 RTCPPacketType packetType,
681 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700682 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200683 return SendCompoundRTCP(
684 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700685 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200686}
687
688int32_t RTCPSender::SendCompoundRTCP(
689 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100690 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200691 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700692 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800693 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800694 size_t max_packet_size;
695
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000696 {
danilchap56036ff2016-03-22 11:14:09 -0700697 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700698 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200700 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000701 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200702 // Add all flags as volatile. Non volatile entries will not be overwritten.
703 // All new volatile flags added will be consumed by the end of this call.
704 SetFlags(packet_types, true);
705
706 // Prevent sending streams to send SR before any media has been sent.
707 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
708 if (!can_calculate_rtp_timestamp) {
709 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
710 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
711 bool sender_report = consumed_report_flag || consumed_sr_flag;
712 if (sender_report && AllVolatileFlagsConsumed()) {
713 // This call was for Sender Report and nothing else.
714 return 0;
715 }
716 if (sending_ && method_ == RtcpMode::kCompound) {
717 // Not allowed to send any RTCP packet without sender report.
718 return -1;
719 }
720 }
721
722 if (packet_type_counter_.first_packet_time_ms == -1)
723 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100724
725 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700726 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200727 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100728
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200729 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100730
danilchap56036ff2016-03-22 11:14:09 -0700731 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800732
Erik Språngf7c57762015-12-04 10:40:35 +0100733 auto it = report_flags_.begin();
734 while (it != report_flags_.end()) {
735 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800736 RTC_DCHECK(builder_it != builders_.end())
737 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100738 if (it->is_volatile) {
739 report_flags_.erase(it++);
740 } else {
741 ++it;
742 }
743
744 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700745 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Danil Chapovalov59e14642019-09-03 12:41:41 +0200746 if (packet == nullptr)
Erik Språngf7c57762015-12-04 10:40:35 +0100747 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800748 // If there is a BYE, don't append now - save it and append it
749 // at the end later.
750 if (builder_it->first == kRtcpBye) {
751 packet_bye = std::move(packet);
752 } else {
753 container.Append(packet.release());
754 }
755 }
756
757 // Append the BYE now at the end
758 if (packet_bye) {
759 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100760 }
761
762 if (packet_type_counter_observer_ != nullptr) {
763 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
764 remote_ssrc_, packet_type_counter_);
765 }
766
767 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800768 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000769 }
Erik Språng61be2a42015-04-27 13:32:52 +0200770
nisse6f142eb2017-02-21 07:32:47 -0800771 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100772 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000773}
774
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200775void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200776 bool generate_report;
777 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
778 // Report type already explicitly set, don't automatically populate.
779 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700780 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200781 } else {
782 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700783 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
784 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200785 if (generate_report)
786 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000787 }
788
Erik Språng0ea42d32015-06-25 14:46:16 +0200789 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200790 SetFlag(kRtcpSdes, true);
791
Erik Språng242e22b2015-05-11 10:17:43 +0200792 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800793 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200794 !feedback_state.last_xr_rtis.empty() ||
795 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800796 SetFlag(kRtcpAnyExtendedReports, true);
797 }
Erik Språng242e22b2015-05-11 10:17:43 +0200798
799 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800800 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000801
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800802 if (!audio_ && sending_) {
803 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
804 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
805 if (send_bitrate_kbit != 0) {
806 min_interval_ms = 360000 / send_bitrate_kbit;
807 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800808 }
Erik Språng61be2a42015-04-27 13:32:52 +0200809 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800810
danilchap47a740b2015-12-15 00:30:07 -0800811 // The interval between RTCP packets is varied randomly over the
812 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800813 int time_to_next =
814 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
815
816 RTC_DCHECK_GT(time_to_next, 0);
817 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000818
danilchap96b69bd2017-07-25 09:15:14 -0700819 // RtcpSender expected to be used for sending either just sender reports
820 // or just receiver reports.
821 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000822 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000823}
824
danilchap96b69bd2017-07-25 09:15:14 -0700825std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
826 const FeedbackState& feedback_state) {
827 std::vector<rtcp::ReportBlock> result;
828 if (!receive_statistics_)
829 return result;
danilchapa72e7342015-12-22 08:07:45 -0800830
danilchapf5f793c2017-07-27 04:44:18 -0700831 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
832 // compound rtcp packet when single rtcp module is used for multiple media
833 // streams.
834 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700835
836 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
837 (feedback_state.last_rr_ntp_frac != 0))) {
838 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200839 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700840
841 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
842 receive_time <<= 16;
843 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
844
845 uint32_t delay_since_last_sr = now - receive_time;
846 // TODO(danilchap): Instead of setting same value on all report blocks,
847 // set only when media_ssrc match sender ssrc of the sender report
848 // remote times were taken from.
849 for (auto& report_block : result) {
850 report_block.SetLastSr(feedback_state.remote_sr);
851 report_block.SetDelayLastSr(delay_since_last_sr);
852 }
danilchapa72e7342015-12-22 08:07:45 -0800853 }
danilchap96b69bd2017-07-25 09:15:14 -0700854 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000855}
856
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000857void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800858 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700859 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000860 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000861}
862
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000863int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
864 uint32_t name,
865 const uint8_t* data,
866 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200867 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100868 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200869 return -1;
870 }
danilchap56036ff2016-03-22 11:14:09 -0700871 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000872
Erik Språng242e22b2015-05-11 10:17:43 +0200873 SetFlag(kRtcpApp, true);
874 app_sub_type_ = subType;
875 app_name_ = name;
876 app_data_.reset(new uint8_t[length]);
877 app_length_ = length;
878 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200879 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000880}
881
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000882void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700883 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200884 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000885}
886
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000887bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700888 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200889 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000890}
891
danilchap853ecb22016-08-22 08:26:15 -0700892void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700893 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700894 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700895 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000896}
Erik Språng61be2a42015-04-27 13:32:52 +0200897
sprang5e38c962016-12-01 05:18:09 -0800898void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
899 if (type & kRtcpAnyExtendedReports) {
900 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
901 } else {
902 report_flags_.insert(ReportFlag(type, is_volatile));
903 }
Erik Språng242e22b2015-05-11 10:17:43 +0200904}
905
906void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
907 bool is_volatile) {
908 for (RTCPPacketType type : types)
909 SetFlag(type, is_volatile);
910}
911
sprang5e38c962016-12-01 05:18:09 -0800912bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200913 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
914}
915
sprang5e38c962016-12-01 05:18:09 -0800916bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200917 auto it = report_flags_.find(ReportFlag(type, false));
918 if (it == report_flags_.end())
919 return false;
920 if (it->is_volatile || forced)
921 report_flags_.erase((it));
922 return true;
923}
924
925bool RTCPSender::AllVolatileFlagsConsumed() const {
926 for (const ReportFlag& flag : report_flags_) {
927 if (flag.is_volatile)
928 return false;
929 }
930 return true;
931}
932
Erik Språng566124a2018-04-23 12:32:22 +0200933void RTCPSender::SetVideoBitrateAllocation(
934 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800935 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200936 // Check if this allocation is first ever, or has a different set of
937 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
938 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200939 absl::optional<VideoBitrateAllocation> new_bitrate =
940 CheckAndUpdateLayerStructure(bitrate);
941 if (new_bitrate) {
942 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100943 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
944 << " with new layers enabled/disabled: "
945 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200946 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200947 } else {
948 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200949 }
950
Erik Språng8782a582018-10-04 15:36:06 +0200951 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800952 SetFlag(kRtcpAnyExtendedReports, true);
953}
954
Erik Språng1cd73912018-10-05 12:57:59 +0200955absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200956 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200957 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200958 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
959 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200960 if (!updated_bitrate &&
961 (bitrate.HasBitrate(si, ti) !=
962 video_bitrate_allocation_.HasBitrate(si, ti) ||
963 (bitrate.GetBitrate(si, ti) == 0) !=
964 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
965 updated_bitrate = bitrate;
966 }
967 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
968 bitrate.GetBitrate(si, ti) == 0) {
969 // Make sure this stream disabling is explicitly signaled.
970 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +0200971 }
972 }
973 }
974
Erik Språng1cd73912018-10-05 12:57:59 +0200975 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200976}
977
sprang233bd872015-09-08 13:25:16 -0700978bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800979 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800980 {
981 rtc::CritScope lock(&critical_section_rtcp_sender_);
982 if (method_ == RtcpMode::kOff)
983 return false;
nisse6f142eb2017-02-21 07:32:47 -0800984 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800985 }
986
nisse6f142eb2017-02-21 07:32:47 -0800987 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100988 bool send_failure = false;
989 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
990 if (transport_->SendRtcp(packet.data(), packet.size())) {
991 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200992 event_log_->Log(absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100993 } else {
994 send_failure = true;
995 }
996 };
997 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700998}
999
Sebastian Janssone1795f42019-07-24 11:38:03 +02001000bool RTCPSender::SendNetworkStateEstimatePacket(
1001 const rtcp::RemoteEstimate& packet) {
1002 size_t max_packet_size;
1003 {
1004 rtc::CritScope lock(&critical_section_rtcp_sender_);
1005 if (method_ == RtcpMode::kOff)
1006 return false;
1007 max_packet_size = max_packet_size_;
1008 }
1009
1010 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1011 bool send_success = false;
1012 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1013 send_success = transport_->SendRtcp(packet.data(), packet.size());
1014 };
1015 return packet.Build(max_packet_size, callback) && send_success;
1016}
1017
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001018} // namespace webrtc