blob: ba46ecef587cb3eeba6f052e9e8562c5da0ff482 [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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016#include <memory>
Danil Chapovalov70ffead2016-07-20 15:26:59 +020017#include <utility>
18
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_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020085 event_log_->Log(std::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
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000116RTCPSender::RTCPSender(const RtpRtcp::Configuration& config)
117 : audio_(config.audio),
118 clock_(config.clock),
danilchap47a740b2015-12-15 00:30:07 -0800119 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700120 method_(RtcpMode::kOff),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000121 event_log_(config.event_log),
122 transport_(config.outgoing_transport),
123 report_interval_ms_(config.rtcp_report_interval_ms > 0
124 ? config.rtcp_report_interval_ms
125 : (config.audio ? kDefaultAudioReportInterval
126 : kDefaultVideoReportInterval)),
Erik Språng242e22b2015-05-11 10:17:43 +0200127 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200128 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700129 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000130 last_rtp_timestamp_(0),
131 last_frame_capture_time_ms_(-1),
Erik Språngc15f92a2019-08-21 15:54:16 +0200132 ssrc_(config.local_media_ssrc.value_or(0)),
Erik Språng242e22b2015-05-11 10:17:43 +0200133 remote_ssrc_(0),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000134 receive_statistics_(config.receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
Erik Språng242e22b2015-05-11 10:17:43 +0200136 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000137
Erik Språng242e22b2015-05-11 10:17:43 +0200138 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
danilchap2b616392016-08-18 06:17:42 -0700140 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200141 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800142 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000143
Erik Språng242e22b2015-05-11 10:17:43 +0200144 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200145 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200146 app_data_(nullptr),
147 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
Erik Språng242e22b2015-05-11 10:17:43 +0200149 xr_send_receiver_reference_time_enabled_(false),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000150 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200151 send_video_bitrate_allocation_(false),
152 last_payload_type_(-1) {
sprang86fd9ed2015-09-29 04:45:43 -0700153 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200154
155 builders_[kRtcpSr] = &RTCPSender::BuildSR;
156 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200157 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200158 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
159 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200160 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
161 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
162 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100163 builders_[kRtcpLossNotification] = &RTCPSender::BuildLossNotification;
Erik Språng242e22b2015-05-11 10:17:43 +0200164 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
165 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
166 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800167 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
danilchap162abd32015-12-10 02:39:40 -0800170RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
pbosda903ea2015-10-02 02:36:56 -0700172RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700173 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200174 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175}
176
skvlad1c392cc2016-04-01 14:46:44 -0700177void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700178 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000179
skvlad1c392cc2016-04-01 14:46:44 -0700180 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
181 // When switching on, reschedule the next packet
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800182 next_time_to_send_rtcp_ =
183 clock_->TimeInMilliseconds() + (report_interval_ms_ / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700184 }
185 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
Erik Språng61be2a42015-04-27 13:32:52 +0200188bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700189 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200190 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
Erik Språng61be2a42015-04-27 13:32:52 +0200193int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
194 bool sending) {
195 bool sendRTCPBye = false;
196 {
danilchap56036ff2016-03-22 11:14:09 -0700197 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
pbosda903ea2015-10-02 02:36:56 -0700199 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200200 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200201 // Trigger RTCP bye
202 sendRTCPBye = true;
203 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 }
Erik Språng242e22b2015-05-11 10:17:43 +0200205 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200206 }
207 if (sendRTCPBye)
208 return SendRTCP(feedback_state, kRtcpBye);
209 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
Elad Alon7d6a4c02019-02-25 13:00:51 +0100212int32_t RTCPSender::SendLossNotification(const FeedbackState& feedback_state,
213 uint16_t last_decoded_seq_num,
214 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200215 bool decodability_flag,
216 bool buffering_allowed) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100217 rtc::CritScope lock(&critical_section_rtcp_sender_);
218
219 loss_notification_state_.last_decoded_seq_num = last_decoded_seq_num;
220 loss_notification_state_.last_received_seq_num = last_received_seq_num;
221 loss_notification_state_.decodability_flag = decodability_flag;
222
223 SetFlag(kRtcpLossNotification, /*is_volatile=*/true);
224
Elad Alone86af2c2019-06-03 14:37:50 +0200225 if (buffering_allowed) {
226 // The loss notification will be batched with additional feedback messages.
227 return 0;
228 }
229
Elad Alon7d6a4c02019-02-25 13:00:51 +0100230 return SendCompoundRTCP(feedback_state,
231 {RTCPPacketType::kRtcpLossNotification});
232}
233
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100234void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
235 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700236 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100237 remb_bitrate_ = bitrate_bps;
238 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000239
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200240 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000241 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
242 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200243 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000244}
245
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200246void RTCPSender::UnsetRemb() {
247 rtc::CritScope lock(&critical_section_rtcp_sender_);
248 // Stop sending REMB each report until it is reenabled and REMB data set.
249 ConsumeFlag(kRtcpRemb, /*forced=*/true);
250}
251
Erik Språng61be2a42015-04-27 13:32:52 +0200252bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700253 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200254 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000255}
256
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000257void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700258 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200259 if (enable) {
260 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
261 } else {
262 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
263 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000264}
265
nisse284542b2017-01-10 08:58:32 -0800266void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800267 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800268 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700269}
270
danilchap71fead22016-08-18 02:01:49 -0700271void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700272 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700273 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000274}
275
276void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200277 int64_t capture_time_ms,
278 int8_t payload_type) {
danilchap56036ff2016-03-22 11:14:09 -0700279 rtc::CritScope lock(&critical_section_rtcp_sender_);
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200280 // For compatibility with clients who don't set payload type correctly on all
281 // calls.
282 if (payload_type != -1) {
283 last_payload_type_ = payload_type;
284 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000285 last_rtp_timestamp_ = rtp_timestamp;
286 if (capture_time_ms < 0) {
287 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200288 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000289 } else {
290 last_frame_capture_time_ms_ = capture_time_ms;
291 }
292}
293
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200294void RTCPSender::SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) {
295 rtc::CritScope lock(&critical_section_rtcp_sender_);
296 rtp_clock_rates_khz_[payload_type] = rtp_clock_rate_hz / 1000;
297}
298
nisse14adba72017-03-20 03:52:39 -0700299uint32_t RTCPSender::SSRC() const {
300 rtc::CritScope lock(&critical_section_rtcp_sender_);
301 return ssrc_;
302}
303
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000304void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700305 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000307 if (ssrc_ != 0 && ssrc != ssrc_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200308 // not first SetSSRC, probably due to a collision
309 // schedule a new RTCP report
310 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200311 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200312 }
Erik Språng242e22b2015-05-11 10:17:43 +0200313 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000314}
315
Erik Språng61be2a42015-04-27 13:32:52 +0200316void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700317 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200318 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
Peter Boström9ba52f82015-06-01 14:12:28 +0200321int32_t RTCPSender::SetCNAME(const char* c_name) {
322 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000323 return -1;
324
kwiberg352444f2016-11-28 15:58:53 -0800325 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700326 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200327 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000328 return 0;
329}
330
Erik Språng0ea42d32015-06-25 14:46:16 +0200331int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700332 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800333 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700334 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700335 // One spot is reserved for ssrc_/cname_.
336 // TODO(danilchap): Add support for more than 30 contributes by sending
337 // several sdes packets.
338 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000339 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200340
341 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000342 return 0;
343}
344
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000345int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700346 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200347 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000348
Erik Språng242e22b2015-05-11 10:17:43 +0200349 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000350 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200351
Erik Språng242e22b2015-05-11 10:17:43 +0200352 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000356bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800357 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800358 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
Jiawei Ou3587b832018-01-31 22:08:26 -0800360 For video we use a configurable interval (default: 1 second) for a BW
361 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
362 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
364
danilchap162abd32015-12-10 02:39:40 -0800365 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
danilchap162abd32015-12-10 02:39:40 -0800367 MAX RTCP BW is 5% if the session BW
368 A send report is approximately 65 bytes inc CNAME
369 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
danilchap162abd32015-12-10 02:39:40 -0800371 The RECOMMENDED value for the reduced minimum in seconds is 360
372 divided by the session bandwidth in kilobits/second. This minimum
373 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
danilchap162abd32015-12-10 02:39:40 -0800375 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800376 initial is true), the constant Tmin is set to half of the configured
377 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000378
danilchap162abd32015-12-10 02:39:40 -0800379 The interval between RTCP packets is varied randomly over the
380 range [0.5,1.5] times the calculated interval to avoid unintended
381 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
danilchap162abd32015-12-10 02:39:40 -0800383 if we send
384 If the participant is a sender (we_sent true), the constant C is
385 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
386 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
387 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
danilchap162abd32015-12-10 02:39:40 -0800389 if we receive only
390 If we_sent is not true, the constant C is set
391 to the average RTCP packet size divided by 75% of the RTCP
392 bandwidth. The constant n is set to the number of receivers
393 (members - senders). If the number of senders is greater than
394 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
danilchap162abd32015-12-10 02:39:40 -0800396 reconsideration NOT required for peer-to-peer
397 "timer reconsideration" is
398 employed. This algorithm implements a simple back-off mechanism
399 which causes users to hold back RTCP packet transmission if the
400 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
danilchap162abd32015-12-10 02:39:40 -0800402 n = number of members
403 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
danilchap162abd32015-12-10 02:39:40 -0800405 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
danilchap162abd32015-12-10 02:39:40 -0800407 4. The calculated interval T is set to a number uniformly distributed
408 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
danilchap162abd32015-12-10 02:39:40 -0800410 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
411 for the fact that the timer reconsideration algorithm converges to
412 a value of the RTCP bandwidth below the intended average
413 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
Erik Språng242e22b2015-05-11 10:17:43 +0200415 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000416
danilchap56036ff2016-03-22 11:14:09 -0700417 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000418
pbosda903ea2015-10-02 02:36:56 -0700419 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
Erik Språng242e22b2015-05-11 10:17:43 +0200422 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200423 // for video key-frames we want to send the RTCP before the large key-frame
424 // if we have a 100 ms margin
425 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
Erik Språng242e22b2015-05-11 10:17:43 +0200428 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200429 return true;
430 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200431 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200432 // wrap
433 return true;
434 }
435 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
danilchap56036ff2016-03-22 11:14:09 -0700438std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200439 // Timestamp shouldn't be estimated before first media frame.
440 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200441 // The timestamp of this RTCP packet should be estimated as the timestamp of
442 // the frame being captured at this moment. We are calculating that
443 // timestamp as the last frame's timestamp + the time since the last frame
444 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200445 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
446 if (rtp_rate <= 0) {
447 rtp_rate =
448 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
449 1000;
450 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200451 // Round now_us_ to the closest millisecond, because Ntp time is rounded
452 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200453 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700454 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200455 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
Erik Språngf7c57762015-12-04 10:40:35 +0100457 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700458 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200459 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700460 report->SetRtpTimestamp(rtp_timestamp);
461 report->SetPacketCount(ctx.feedback_state_.packets_sent);
462 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700463 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100464
danilchap56036ff2016-03-22 11:14:09 -0700465 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
danilchap56036ff2016-03-22 11:14:09 -0700468std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100469 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200470 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800471 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000472
Erik Språngf7c57762015-12-04 10:40:35 +0100473 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700474 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200475
danilchap74e8df8f2017-03-16 08:04:08 -0700476 for (const auto& it : csrc_cnames_)
477 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200478
danilchap56036ff2016-03-22 11:14:09 -0700479 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
danilchap56036ff2016-03-22 11:14:09 -0700482std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100483 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700484 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700485 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200486
danilchap56036ff2016-03-22 11:14:09 -0700487 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
danilchap56036ff2016-03-22 11:14:09 -0700490std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100491 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700492 pli->SetSenderSsrc(ssrc_);
493 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200494
Erik Språng242e22b2015-05-11 10:17:43 +0200495 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200496
danilchap56036ff2016-03-22 11:14:09 -0700497 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200498}
499
danilchap56036ff2016-03-22 11:14:09 -0700500std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800501 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000502
Erik Språngf7c57762015-12-04 10:40:35 +0100503 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700504 fir->SetSenderSsrc(ssrc_);
505 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200506
Erik Språng242e22b2015-05-11 10:17:43 +0200507 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200508
danilchap56036ff2016-03-22 11:14:09 -0700509 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000510}
511
danilchap56036ff2016-03-22 11:14:09 -0700512std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100513 const RtcpContext& ctx) {
514 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700515 remb->SetSenderSsrc(ssrc_);
516 remb->SetBitrateBps(remb_bitrate_);
517 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200518
danilchap56036ff2016-03-22 11:14:09 -0700519 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000520}
521
Erik Språng61be2a42015-04-27 13:32:52 +0200522void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700523 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700524 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000525}
526
danilchap56036ff2016-03-22 11:14:09 -0700527std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100528 const RtcpContext& ctx) {
529 if (ctx.feedback_state_.module == nullptr)
530 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200531 // Before sending the TMMBR check the received TMMBN, only an owner is
532 // allowed to raise the bitrate:
533 // * If the sender is an owner of the TMMBN -> send TMMBR
534 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
Erik Språng61be2a42015-04-27 13:32:52 +0200536 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700537 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000538
Erik Språng242e22b2015-05-11 10:17:43 +0200539 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
540 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200541 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700542 std::vector<rtcp::TmmbItem> candidates =
543 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000544
danilchap2b616392016-08-18 06:17:42 -0700545 if (!candidates.empty()) {
546 for (const auto& candidate : candidates) {
547 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
548 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100549 // Do not send the same tuple.
550 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200551 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000552 }
danilchap2b616392016-08-18 06:17:42 -0700553 if (!tmmbr_owner) {
554 // Use received bounding set as candidate set.
555 // Add current tuple.
556 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000557
danilchap2b616392016-08-18 06:17:42 -0700558 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700559 std::vector<rtcp::TmmbItem> bounding =
560 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700561 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
562 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100563 // Did not enter bounding set, no meaning to send this request.
564 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200565 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000566 }
Erik Språng61be2a42015-04-27 13:32:52 +0200567 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000568
danilchap2b616392016-08-18 06:17:42 -0700569 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100570 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700571
Erik Språngf7c57762015-12-04 10:40:35 +0100572 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700573 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800574 rtcp::TmmbItem request;
575 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700576 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800577 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700578 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100579
danilchap56036ff2016-03-22 11:14:09 -0700580 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200581}
582
danilchap56036ff2016-03-22 11:14:09 -0700583std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100584 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100585 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700586 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700587 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
588 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700589 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000590 }
Erik Språng61be2a42015-04-27 13:32:52 +0200591 }
sprangd83df502015-08-27 01:05:08 -0700592
danilchap56036ff2016-03-22 11:14:09 -0700593 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594}
595
danilchap56036ff2016-03-22 11:14:09 -0700596std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100597 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700598 app->SetSsrc(ssrc_);
599 app->SetSubType(app_sub_type_);
600 app->SetName(app_name_);
601 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200602
danilchap56036ff2016-03-22 11:14:09 -0700603 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200604}
605
Elad Alon7d6a4c02019-02-25 13:00:51 +0100606std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
607 const RtcpContext& ctx) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200608 auto loss_notification = std::make_unique<rtcp::LossNotification>(
Elad Alon7d6a4c02019-02-25 13:00:51 +0100609 loss_notification_state_.last_decoded_seq_num,
610 loss_notification_state_.last_received_seq_num,
611 loss_notification_state_.decodability_flag);
612 loss_notification->SetSenderSsrc(ssrc_);
613 loss_notification->SetMediaSsrc(remote_ssrc_);
614 return std::move(loss_notification);
615}
616
danilchap56036ff2016-03-22 11:14:09 -0700617std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100618 const RtcpContext& ctx) {
619 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700620 nack->SetSenderSsrc(ssrc_);
621 nack->SetMediaSsrc(remote_ssrc_);
622 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200623
624 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100625 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100626 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200627 }
Erik Språng61be2a42015-04-27 13:32:52 +0200628 packet_type_counter_.nack_requests = nack_stats_.requests();
629 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200630
Erik Språng242e22b2015-05-11 10:17:43 +0200631 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200632
danilchap56036ff2016-03-22 11:14:09 -0700633 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200634}
635
danilchap56036ff2016-03-22 11:14:09 -0700636std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100637 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700638 bye->SetSenderSsrc(ssrc_);
639 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700640
danilchap56036ff2016-03-22 11:14:09 -0700641 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000642}
643
sprang5e38c962016-12-01 05:18:09 -0800644std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100645 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800646 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700647 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000648
sprang5e38c962016-12-01 05:18:09 -0800649 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
650 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200651 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800652 xr->SetRrtr(rrtr);
653 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000654
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200655 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
656 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800657 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000658
Erik Språng8782a582018-10-04 15:36:06 +0200659 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800660 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000661
sprang5e38c962016-12-01 05:18:09 -0800662 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
663 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200664 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100665 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200666 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100667 }
sprang5e38c962016-12-01 05:18:09 -0800668 }
669 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000670
sprang5e38c962016-12-01 05:18:09 -0800671 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200672 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800673 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200674
sprang5e38c962016-12-01 05:18:09 -0800675 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676}
677
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000678int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200679 RTCPPacketType packetType,
680 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700681 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200682 return SendCompoundRTCP(
683 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700684 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200685}
686
687int32_t RTCPSender::SendCompoundRTCP(
688 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100689 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200690 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700691 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800692 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800693 size_t max_packet_size;
694
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000695 {
danilchap56036ff2016-03-22 11:14:09 -0700696 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700697 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100698 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200699 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000700 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200701 // Add all flags as volatile. Non volatile entries will not be overwritten.
702 // All new volatile flags added will be consumed by the end of this call.
703 SetFlags(packet_types, true);
704
705 // Prevent sending streams to send SR before any media has been sent.
706 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
707 if (!can_calculate_rtp_timestamp) {
708 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
709 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
710 bool sender_report = consumed_report_flag || consumed_sr_flag;
711 if (sender_report && AllVolatileFlagsConsumed()) {
712 // This call was for Sender Report and nothing else.
713 return 0;
714 }
715 if (sending_ && method_ == RtcpMode::kCompound) {
716 // Not allowed to send any RTCP packet without sender report.
717 return -1;
718 }
719 }
720
721 if (packet_type_counter_.first_packet_time_ms == -1)
722 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100723
724 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700725 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200726 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100727
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200728 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100729
danilchap56036ff2016-03-22 11:14:09 -0700730 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800731
Erik Språngf7c57762015-12-04 10:40:35 +0100732 auto it = report_flags_.begin();
733 while (it != report_flags_.end()) {
734 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800735 RTC_DCHECK(builder_it != builders_.end())
736 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100737 if (it->is_volatile) {
738 report_flags_.erase(it++);
739 } else {
740 ++it;
741 }
742
743 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700744 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Danil Chapovalov59e14642019-09-03 12:41:41 +0200745 if (packet == nullptr)
Erik Språngf7c57762015-12-04 10:40:35 +0100746 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800747 // If there is a BYE, don't append now - save it and append it
748 // at the end later.
749 if (builder_it->first == kRtcpBye) {
750 packet_bye = std::move(packet);
751 } else {
752 container.Append(packet.release());
753 }
754 }
755
756 // Append the BYE now at the end
757 if (packet_bye) {
758 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100759 }
760
761 if (packet_type_counter_observer_ != nullptr) {
762 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
763 remote_ssrc_, packet_type_counter_);
764 }
765
766 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800767 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000768 }
Erik Språng61be2a42015-04-27 13:32:52 +0200769
nisse6f142eb2017-02-21 07:32:47 -0800770 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100771 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000772}
773
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200774void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200775 bool generate_report;
776 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
777 // Report type already explicitly set, don't automatically populate.
778 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700779 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200780 } else {
781 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700782 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
783 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200784 if (generate_report)
785 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000786 }
787
Erik Språng0ea42d32015-06-25 14:46:16 +0200788 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200789 SetFlag(kRtcpSdes, true);
790
Erik Språng242e22b2015-05-11 10:17:43 +0200791 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800792 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200793 !feedback_state.last_xr_rtis.empty() ||
794 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800795 SetFlag(kRtcpAnyExtendedReports, true);
796 }
Erik Språng242e22b2015-05-11 10:17:43 +0200797
798 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800799 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000800
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800801 if (!audio_ && sending_) {
802 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
803 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
804 if (send_bitrate_kbit != 0) {
805 min_interval_ms = 360000 / send_bitrate_kbit;
806 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800807 }
Erik Språng61be2a42015-04-27 13:32:52 +0200808 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800809
danilchap47a740b2015-12-15 00:30:07 -0800810 // The interval between RTCP packets is varied randomly over the
811 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800812 int time_to_next =
813 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
814
815 RTC_DCHECK_GT(time_to_next, 0);
816 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000817
danilchap96b69bd2017-07-25 09:15:14 -0700818 // RtcpSender expected to be used for sending either just sender reports
819 // or just receiver reports.
820 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000821 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000822}
823
danilchap96b69bd2017-07-25 09:15:14 -0700824std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
825 const FeedbackState& feedback_state) {
826 std::vector<rtcp::ReportBlock> result;
827 if (!receive_statistics_)
828 return result;
danilchapa72e7342015-12-22 08:07:45 -0800829
danilchapf5f793c2017-07-27 04:44:18 -0700830 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
831 // compound rtcp packet when single rtcp module is used for multiple media
832 // streams.
833 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700834
835 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
836 (feedback_state.last_rr_ntp_frac != 0))) {
837 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200838 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700839
840 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
841 receive_time <<= 16;
842 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
843
844 uint32_t delay_since_last_sr = now - receive_time;
845 // TODO(danilchap): Instead of setting same value on all report blocks,
846 // set only when media_ssrc match sender ssrc of the sender report
847 // remote times were taken from.
848 for (auto& report_block : result) {
849 report_block.SetLastSr(feedback_state.remote_sr);
850 report_block.SetDelayLastSr(delay_since_last_sr);
851 }
danilchapa72e7342015-12-22 08:07:45 -0800852 }
danilchap96b69bd2017-07-25 09:15:14 -0700853 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000854}
855
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000856void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800857 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700858 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000859 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000860}
861
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000862int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
863 uint32_t name,
864 const uint8_t* data,
865 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200866 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100867 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200868 return -1;
869 }
danilchap56036ff2016-03-22 11:14:09 -0700870 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000871
Erik Språng242e22b2015-05-11 10:17:43 +0200872 SetFlag(kRtcpApp, true);
873 app_sub_type_ = subType;
874 app_name_ = name;
875 app_data_.reset(new uint8_t[length]);
876 app_length_ = length;
877 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200878 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000879}
880
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000881void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700882 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200883 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000884}
885
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000886bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700887 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200888 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000889}
890
danilchap853ecb22016-08-22 08:26:15 -0700891void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700892 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700893 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700894 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000895}
Erik Språng61be2a42015-04-27 13:32:52 +0200896
sprang5e38c962016-12-01 05:18:09 -0800897void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
898 if (type & kRtcpAnyExtendedReports) {
899 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
900 } else {
901 report_flags_.insert(ReportFlag(type, is_volatile));
902 }
Erik Språng242e22b2015-05-11 10:17:43 +0200903}
904
905void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
906 bool is_volatile) {
907 for (RTCPPacketType type : types)
908 SetFlag(type, is_volatile);
909}
910
sprang5e38c962016-12-01 05:18:09 -0800911bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200912 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
913}
914
sprang5e38c962016-12-01 05:18:09 -0800915bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200916 auto it = report_flags_.find(ReportFlag(type, false));
917 if (it == report_flags_.end())
918 return false;
919 if (it->is_volatile || forced)
920 report_flags_.erase((it));
921 return true;
922}
923
924bool RTCPSender::AllVolatileFlagsConsumed() const {
925 for (const ReportFlag& flag : report_flags_) {
926 if (flag.is_volatile)
927 return false;
928 }
929 return true;
930}
931
Erik Språng566124a2018-04-23 12:32:22 +0200932void RTCPSender::SetVideoBitrateAllocation(
933 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800934 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200935 // Check if this allocation is first ever, or has a different set of
936 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
937 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200938 absl::optional<VideoBitrateAllocation> new_bitrate =
939 CheckAndUpdateLayerStructure(bitrate);
940 if (new_bitrate) {
941 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100942 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
943 << " with new layers enabled/disabled: "
944 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200945 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200946 } else {
947 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200948 }
949
Erik Språng8782a582018-10-04 15:36:06 +0200950 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800951 SetFlag(kRtcpAnyExtendedReports, true);
952}
953
Erik Språng1cd73912018-10-05 12:57:59 +0200954absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200955 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200956 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200957 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
958 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200959 if (!updated_bitrate &&
960 (bitrate.HasBitrate(si, ti) !=
961 video_bitrate_allocation_.HasBitrate(si, ti) ||
962 (bitrate.GetBitrate(si, ti) == 0) !=
963 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
964 updated_bitrate = bitrate;
965 }
966 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
967 bitrate.GetBitrate(si, ti) == 0) {
968 // Make sure this stream disabling is explicitly signaled.
969 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +0200970 }
971 }
972 }
973
Erik Språng1cd73912018-10-05 12:57:59 +0200974 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200975}
976
sprang233bd872015-09-08 13:25:16 -0700977bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800978 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800979 {
980 rtc::CritScope lock(&critical_section_rtcp_sender_);
981 if (method_ == RtcpMode::kOff)
982 return false;
nisse6f142eb2017-02-21 07:32:47 -0800983 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800984 }
985
nisse6f142eb2017-02-21 07:32:47 -0800986 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100987 bool send_failure = false;
988 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
989 if (transport_->SendRtcp(packet.data(), packet.size())) {
990 if (event_log_)
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200991 event_log_->Log(std::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100992 } else {
993 send_failure = true;
994 }
995 };
996 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -0700997}
998
Sebastian Janssone1795f42019-07-24 11:38:03 +0200999bool RTCPSender::SendNetworkStateEstimatePacket(
1000 const rtcp::RemoteEstimate& packet) {
1001 size_t max_packet_size;
1002 {
1003 rtc::CritScope lock(&critical_section_rtcp_sender_);
1004 if (method_ == RtcpMode::kOff)
1005 return false;
1006 max_packet_size = max_packet_size_;
1007 }
1008
1009 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1010 bool send_success = false;
1011 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1012 send_success = transport_->SendRtcp(packet.data(), packet.size());
1013 };
1014 return packet.Build(max_packet_size, callback) && send_success;
1015}
1016
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001017} // namespace webrtc