blob: 0f119ef235fcde1ded5838a2e8145960c6c4a21b [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;
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020053
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010054class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010055 public:
terelius429c3452016-01-21 05:42:04 -080056 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010057 : transport_(transport), event_log_(event_log) {}
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010058 ~PacketContainer() override {
Erik Språngf7c57762015-12-04 10:40:35 +010059 for (RtcpPacket* packet : appended_packets_)
60 delete packet;
61 }
62
danilchap41befce2016-03-30 11:11:51 -070063 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010064 size_t bytes_sent = 0;
65 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
66 if (transport_->SendRtcp(packet.data(), packet.size())) {
67 bytes_sent += packet.size();
68 if (event_log_) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +020069 event_log_->Log(std::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010070 }
71 }
72 });
73 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +010074 }
75
76 private:
77 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -080078 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -080079
80 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +010081};
82
Per Kjellander16999812019-10-10 12:57:28 +020083// Helper to put several RTCP packets into lower layer datagram RTCP packet.
84// Prefer to use this class instead of PacketContainer.
85class PacketSender {
86 public:
87 PacketSender(rtcp::RtcpPacket::PacketReadyCallback callback,
88 size_t max_packet_size)
89 : callback_(callback), max_packet_size_(max_packet_size) {
90 RTC_CHECK_LE(max_packet_size, IP_PACKET_SIZE);
91 }
92 ~PacketSender() { RTC_DCHECK_EQ(index_, 0) << "Unsent rtcp packet."; }
93
94 // Appends a packet to pending compound packet.
95 // Sends rtcp packet if buffer is full and resets the buffer.
96 void AppendPacket(const rtcp::RtcpPacket& packet) {
97 packet.Create(buffer_, &index_, max_packet_size_, callback_);
98 }
99
100 // Sends pending rtcp packet.
101 void Send() {
102 if (index_ > 0) {
103 callback_(rtc::ArrayView<const uint8_t>(buffer_, index_));
104 index_ = 0;
105 }
106 }
107
108 bool IsEmpty() const { return index_ == 0; }
109
110 private:
111 const rtcp::RtcpPacket::PacketReadyCallback callback_;
112 const size_t max_packet_size_;
113 size_t index_ = 0;
114 uint8_t buffer_[IP_PACKET_SIZE];
115};
116
117} // namespace
118
119RTCPSender::FeedbackState::FeedbackState()
120 : packets_sent(0),
121 media_bytes_sent(0),
122 send_bitrate(0),
123 last_rr_ntp_secs(0),
124 last_rr_ntp_frac(0),
125 remote_sr(0),
126 module(nullptr) {}
127
128RTCPSender::FeedbackState::FeedbackState(const FeedbackState&) = default;
129
130RTCPSender::FeedbackState::FeedbackState(FeedbackState&&) = default;
131
132RTCPSender::FeedbackState::~FeedbackState() = default;
133
Erik Språngf7c57762015-12-04 10:40:35 +0100134class RTCPSender::RtcpContext {
135 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200136 RtcpContext(const FeedbackState& feedback_state,
137 int32_t nack_size,
138 const uint16_t* nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200139 int64_t now_us)
Erik Språngf7c57762015-12-04 10:40:35 +0100140 : feedback_state_(feedback_state),
141 nack_size_(nack_size),
142 nack_list_(nack_list),
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200143 now_us_(now_us) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200144
Erik Språngf7c57762015-12-04 10:40:35 +0100145 const FeedbackState& feedback_state_;
146 const int32_t nack_size_;
147 const uint16_t* nack_list_;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200148 const int64_t now_us_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200149};
150
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000151RTCPSender::RTCPSender(const RtpRtcp::Configuration& config)
152 : audio_(config.audio),
Erik Språng6841d252019-10-15 14:29:11 +0200153 ssrc_(config.local_media_ssrc),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000154 clock_(config.clock),
danilchap47a740b2015-12-15 00:30:07 -0800155 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700156 method_(RtcpMode::kOff),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000157 event_log_(config.event_log),
158 transport_(config.outgoing_transport),
159 report_interval_ms_(config.rtcp_report_interval_ms > 0
160 ? config.rtcp_report_interval_ms
161 : (config.audio ? kDefaultAudioReportInterval
162 : kDefaultVideoReportInterval)),
Erik Språng242e22b2015-05-11 10:17:43 +0200163 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200164 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700165 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000166 last_rtp_timestamp_(0),
167 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200168 remote_ssrc_(0),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000169 receive_statistics_(config.receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
Erik Språng242e22b2015-05-11 10:17:43 +0200171 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000172
Erik Språng242e22b2015-05-11 10:17:43 +0200173 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
danilchap2b616392016-08-18 06:17:42 -0700175 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200176 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800177 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000178
Erik Språng242e22b2015-05-11 10:17:43 +0200179 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200180 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200181 app_data_(nullptr),
182 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
Erik Språng242e22b2015-05-11 10:17:43 +0200184 xr_send_receiver_reference_time_enabled_(false),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000185 packet_type_counter_observer_(config.rtcp_packet_type_counter_observer),
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200186 send_video_bitrate_allocation_(false),
187 last_payload_type_(-1) {
sprang86fd9ed2015-09-29 04:45:43 -0700188 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200189
190 builders_[kRtcpSr] = &RTCPSender::BuildSR;
191 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200192 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200193 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
194 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200195 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
196 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
197 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100198 builders_[kRtcpLossNotification] = &RTCPSender::BuildLossNotification;
Erik Språng242e22b2015-05-11 10:17:43 +0200199 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
200 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
201 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800202 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000203}
204
danilchap162abd32015-12-10 02:39:40 -0800205RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
pbosda903ea2015-10-02 02:36:56 -0700207RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700208 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200209 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210}
211
skvlad1c392cc2016-04-01 14:46:44 -0700212void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700213 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000214
skvlad1c392cc2016-04-01 14:46:44 -0700215 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
216 // When switching on, reschedule the next packet
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800217 next_time_to_send_rtcp_ =
218 clock_->TimeInMilliseconds() + (report_interval_ms_ / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700219 }
220 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000221}
222
Erik Språng61be2a42015-04-27 13:32:52 +0200223bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700224 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200225 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
Erik Språng61be2a42015-04-27 13:32:52 +0200228int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
229 bool sending) {
230 bool sendRTCPBye = false;
231 {
danilchap56036ff2016-03-22 11:14:09 -0700232 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
pbosda903ea2015-10-02 02:36:56 -0700234 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200235 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200236 // Trigger RTCP bye
237 sendRTCPBye = true;
238 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 }
Erik Språng242e22b2015-05-11 10:17:43 +0200240 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200241 }
242 if (sendRTCPBye)
243 return SendRTCP(feedback_state, kRtcpBye);
244 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
Elad Alon7d6a4c02019-02-25 13:00:51 +0100247int32_t RTCPSender::SendLossNotification(const FeedbackState& feedback_state,
248 uint16_t last_decoded_seq_num,
249 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200250 bool decodability_flag,
251 bool buffering_allowed) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100252 rtc::CritScope lock(&critical_section_rtcp_sender_);
253
254 loss_notification_state_.last_decoded_seq_num = last_decoded_seq_num;
255 loss_notification_state_.last_received_seq_num = last_received_seq_num;
256 loss_notification_state_.decodability_flag = decodability_flag;
257
258 SetFlag(kRtcpLossNotification, /*is_volatile=*/true);
259
Elad Alone86af2c2019-06-03 14:37:50 +0200260 if (buffering_allowed) {
261 // The loss notification will be batched with additional feedback messages.
262 return 0;
263 }
264
Elad Alon7d6a4c02019-02-25 13:00:51 +0100265 return SendCompoundRTCP(feedback_state,
266 {RTCPPacketType::kRtcpLossNotification});
267}
268
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100269void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
270 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700271 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100272 remb_bitrate_ = bitrate_bps;
273 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000274
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200275 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000276 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
277 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200278 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000279}
280
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200281void RTCPSender::UnsetRemb() {
282 rtc::CritScope lock(&critical_section_rtcp_sender_);
283 // Stop sending REMB each report until it is reenabled and REMB data set.
284 ConsumeFlag(kRtcpRemb, /*forced=*/true);
285}
286
Erik Språng61be2a42015-04-27 13:32:52 +0200287bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700288 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200289 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000290}
291
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000292void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700293 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200294 if (enable) {
295 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
296 } else {
297 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
298 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000299}
300
nisse284542b2017-01-10 08:58:32 -0800301void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800302 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800303 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700304}
305
danilchap71fead22016-08-18 02:01:49 -0700306void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700307 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700308 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000309}
310
311void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200312 int64_t capture_time_ms,
313 int8_t payload_type) {
danilchap56036ff2016-03-22 11:14:09 -0700314 rtc::CritScope lock(&critical_section_rtcp_sender_);
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200315 // For compatibility with clients who don't set payload type correctly on all
316 // calls.
317 if (payload_type != -1) {
318 last_payload_type_ = payload_type;
319 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000320 last_rtp_timestamp_ = rtp_timestamp;
321 if (capture_time_ms < 0) {
322 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200323 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000324 } else {
325 last_frame_capture_time_ms_ = capture_time_ms;
326 }
327}
328
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200329void RTCPSender::SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) {
330 rtc::CritScope lock(&critical_section_rtcp_sender_);
331 rtp_clock_rates_khz_[payload_type] = rtp_clock_rate_hz / 1000;
332}
333
Erik Språng61be2a42015-04-27 13:32:52 +0200334void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700335 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200336 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000337}
338
Peter Boström9ba52f82015-06-01 14:12:28 +0200339int32_t RTCPSender::SetCNAME(const char* c_name) {
340 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000341 return -1;
342
kwiberg352444f2016-11-28 15:58:53 -0800343 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700344 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200345 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000346 return 0;
347}
348
Erik Språng0ea42d32015-06-25 14:46:16 +0200349int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700350 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800351 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700352 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700353 // One spot is reserved for ssrc_/cname_.
354 // TODO(danilchap): Add support for more than 30 contributes by sending
355 // several sdes packets.
356 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000357 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200358
359 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000360 return 0;
361}
362
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000363int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700364 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200365 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000366
Erik Språng242e22b2015-05-11 10:17:43 +0200367 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000368 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200369
Erik Språng242e22b2015-05-11 10:17:43 +0200370 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000371 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
373
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000374bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800375 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800376 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
Jiawei Ou3587b832018-01-31 22:08:26 -0800378 For video we use a configurable interval (default: 1 second) for a BW
379 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
380 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
382
danilchap162abd32015-12-10 02:39:40 -0800383 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000384
danilchap162abd32015-12-10 02:39:40 -0800385 MAX RTCP BW is 5% if the session BW
386 A send report is approximately 65 bytes inc CNAME
387 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
danilchap162abd32015-12-10 02:39:40 -0800389 The RECOMMENDED value for the reduced minimum in seconds is 360
390 divided by the session bandwidth in kilobits/second. This minimum
391 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000392
danilchap162abd32015-12-10 02:39:40 -0800393 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800394 initial is true), the constant Tmin is set to half of the configured
395 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
danilchap162abd32015-12-10 02:39:40 -0800397 The interval between RTCP packets is varied randomly over the
398 range [0.5,1.5] times the calculated interval to avoid unintended
399 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
danilchap162abd32015-12-10 02:39:40 -0800401 if we send
402 If the participant is a sender (we_sent true), the constant C is
403 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
404 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
405 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
danilchap162abd32015-12-10 02:39:40 -0800407 if we receive only
408 If we_sent is not true, the constant C is set
409 to the average RTCP packet size divided by 75% of the RTCP
410 bandwidth. The constant n is set to the number of receivers
411 (members - senders). If the number of senders is greater than
412 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
danilchap162abd32015-12-10 02:39:40 -0800414 reconsideration NOT required for peer-to-peer
415 "timer reconsideration" is
416 employed. This algorithm implements a simple back-off mechanism
417 which causes users to hold back RTCP packet transmission if the
418 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000419
danilchap162abd32015-12-10 02:39:40 -0800420 n = number of members
421 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
danilchap162abd32015-12-10 02:39:40 -0800423 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
danilchap162abd32015-12-10 02:39:40 -0800425 4. The calculated interval T is set to a number uniformly distributed
426 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
danilchap162abd32015-12-10 02:39:40 -0800428 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
429 for the fact that the timer reconsideration algorithm converges to
430 a value of the RTCP bandwidth below the intended average
431 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000432
Erik Språng242e22b2015-05-11 10:17:43 +0200433 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000434
danilchap56036ff2016-03-22 11:14:09 -0700435 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000436
pbosda903ea2015-10-02 02:36:56 -0700437 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000438 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
Erik Språng242e22b2015-05-11 10:17:43 +0200440 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200441 // for video key-frames we want to send the RTCP before the large key-frame
442 // if we have a 100 ms margin
443 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
444 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
Erik Språng242e22b2015-05-11 10:17:43 +0200446 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200447 return true;
448 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200449 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200450 // wrap
451 return true;
452 }
453 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
danilchap56036ff2016-03-22 11:14:09 -0700456std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200457 // Timestamp shouldn't be estimated before first media frame.
458 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200459 // The timestamp of this RTCP packet should be estimated as the timestamp of
460 // the frame being captured at this moment. We are calculating that
461 // timestamp as the last frame's timestamp + the time since the last frame
462 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200463 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
464 if (rtp_rate <= 0) {
465 rtp_rate =
466 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
467 1000;
468 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200469 // Round now_us_ to the closest millisecond, because Ntp time is rounded
470 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200471 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700472 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200473 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000474
Erik Språngf7c57762015-12-04 10:40:35 +0100475 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700476 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200477 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700478 report->SetRtpTimestamp(rtp_timestamp);
479 report->SetPacketCount(ctx.feedback_state_.packets_sent);
480 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700481 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100482
danilchap56036ff2016-03-22 11:14:09 -0700483 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000484}
485
danilchap56036ff2016-03-22 11:14:09 -0700486std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100487 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200488 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800489 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
Erik Språngf7c57762015-12-04 10:40:35 +0100491 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700492 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200493
danilchap74e8df8f2017-03-16 08:04:08 -0700494 for (const auto& it : csrc_cnames_)
495 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200496
danilchap56036ff2016-03-22 11:14:09 -0700497 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000498}
499
danilchap56036ff2016-03-22 11:14:09 -0700500std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100501 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700502 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700503 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200504
danilchap56036ff2016-03-22 11:14:09 -0700505 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000506}
507
danilchap56036ff2016-03-22 11:14:09 -0700508std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100509 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700510 pli->SetSenderSsrc(ssrc_);
511 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200512
Erik Språng242e22b2015-05-11 10:17:43 +0200513 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200514
danilchap56036ff2016-03-22 11:14:09 -0700515 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200516}
517
danilchap56036ff2016-03-22 11:14:09 -0700518std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800519 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000520
Erik Språngf7c57762015-12-04 10:40:35 +0100521 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700522 fir->SetSenderSsrc(ssrc_);
523 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200524
Erik Språng242e22b2015-05-11 10:17:43 +0200525 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200526
danilchap56036ff2016-03-22 11:14:09 -0700527 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528}
529
danilchap56036ff2016-03-22 11:14:09 -0700530std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100531 const RtcpContext& ctx) {
532 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700533 remb->SetSenderSsrc(ssrc_);
534 remb->SetBitrateBps(remb_bitrate_);
535 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200536
danilchap56036ff2016-03-22 11:14:09 -0700537 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000538}
539
Erik Språng61be2a42015-04-27 13:32:52 +0200540void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700541 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700542 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000543}
544
danilchap56036ff2016-03-22 11:14:09 -0700545std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100546 const RtcpContext& ctx) {
547 if (ctx.feedback_state_.module == nullptr)
548 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200549 // Before sending the TMMBR check the received TMMBN, only an owner is
550 // allowed to raise the bitrate:
551 // * If the sender is an owner of the TMMBN -> send TMMBR
552 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000553
Erik Språng61be2a42015-04-27 13:32:52 +0200554 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700555 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000556
Erik Språng242e22b2015-05-11 10:17:43 +0200557 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
558 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200559 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700560 std::vector<rtcp::TmmbItem> candidates =
561 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000562
danilchap2b616392016-08-18 06:17:42 -0700563 if (!candidates.empty()) {
564 for (const auto& candidate : candidates) {
565 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
566 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100567 // Do not send the same tuple.
568 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200569 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000570 }
danilchap2b616392016-08-18 06:17:42 -0700571 if (!tmmbr_owner) {
572 // Use received bounding set as candidate set.
573 // Add current tuple.
574 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
danilchap2b616392016-08-18 06:17:42 -0700576 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700577 std::vector<rtcp::TmmbItem> bounding =
578 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700579 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
580 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100581 // Did not enter bounding set, no meaning to send this request.
582 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200583 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 }
Erik Språng61be2a42015-04-27 13:32:52 +0200585 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000586
danilchap2b616392016-08-18 06:17:42 -0700587 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100588 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700589
Erik Språngf7c57762015-12-04 10:40:35 +0100590 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700591 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800592 rtcp::TmmbItem request;
593 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700594 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800595 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700596 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100597
danilchap56036ff2016-03-22 11:14:09 -0700598 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200599}
600
danilchap56036ff2016-03-22 11:14:09 -0700601std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100602 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100603 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700604 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700605 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
606 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700607 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000608 }
Erik Språng61be2a42015-04-27 13:32:52 +0200609 }
sprangd83df502015-08-27 01:05:08 -0700610
danilchap56036ff2016-03-22 11:14:09 -0700611 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000612}
613
danilchap56036ff2016-03-22 11:14:09 -0700614std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100615 rtcp::App* app = new rtcp::App();
Per Kjellander955f8fd2019-10-09 13:30:36 +0200616 app->SetSenderSsrc(ssrc_);
danilchap822a16f2016-09-27 09:27:47 -0700617 app->SetSubType(app_sub_type_);
618 app->SetName(app_name_);
619 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200620
danilchap56036ff2016-03-22 11:14:09 -0700621 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200622}
623
Elad Alon7d6a4c02019-02-25 13:00:51 +0100624std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
625 const RtcpContext& ctx) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200626 auto loss_notification = std::make_unique<rtcp::LossNotification>(
Elad Alon7d6a4c02019-02-25 13:00:51 +0100627 loss_notification_state_.last_decoded_seq_num,
628 loss_notification_state_.last_received_seq_num,
629 loss_notification_state_.decodability_flag);
630 loss_notification->SetSenderSsrc(ssrc_);
631 loss_notification->SetMediaSsrc(remote_ssrc_);
632 return std::move(loss_notification);
633}
634
danilchap56036ff2016-03-22 11:14:09 -0700635std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100636 const RtcpContext& ctx) {
637 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700638 nack->SetSenderSsrc(ssrc_);
639 nack->SetMediaSsrc(remote_ssrc_);
640 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200641
642 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100643 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100644 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200645 }
Erik Språng61be2a42015-04-27 13:32:52 +0200646 packet_type_counter_.nack_requests = nack_stats_.requests();
647 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200648
Erik Språng242e22b2015-05-11 10:17:43 +0200649 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200650
danilchap56036ff2016-03-22 11:14:09 -0700651 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200652}
653
danilchap56036ff2016-03-22 11:14:09 -0700654std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100655 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700656 bye->SetSenderSsrc(ssrc_);
657 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700658
danilchap56036ff2016-03-22 11:14:09 -0700659 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000660}
661
sprang5e38c962016-12-01 05:18:09 -0800662std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100663 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800664 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700665 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000666
sprang5e38c962016-12-01 05:18:09 -0800667 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
668 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200669 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800670 xr->SetRrtr(rrtr);
671 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000672
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200673 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
674 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800675 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000676
Erik Språng8782a582018-10-04 15:36:06 +0200677 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800678 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000679
sprang5e38c962016-12-01 05:18:09 -0800680 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
681 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200682 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100683 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200684 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100685 }
sprang5e38c962016-12-01 05:18:09 -0800686 }
687 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000688
sprang5e38c962016-12-01 05:18:09 -0800689 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200690 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800691 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200692
sprang5e38c962016-12-01 05:18:09 -0800693 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000694}
695
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000696int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200697 RTCPPacketType packetType,
698 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700699 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200700 return SendCompoundRTCP(
701 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700702 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200703}
704
705int32_t RTCPSender::SendCompoundRTCP(
706 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100707 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200708 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700709 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800710 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800711 size_t max_packet_size;
712
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000713 {
danilchap56036ff2016-03-22 11:14:09 -0700714 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700715 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100716 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200717 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000718 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200719 // Add all flags as volatile. Non volatile entries will not be overwritten.
720 // All new volatile flags added will be consumed by the end of this call.
721 SetFlags(packet_types, true);
722
723 // Prevent sending streams to send SR before any media has been sent.
724 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
725 if (!can_calculate_rtp_timestamp) {
726 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
727 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
728 bool sender_report = consumed_report_flag || consumed_sr_flag;
729 if (sender_report && AllVolatileFlagsConsumed()) {
730 // This call was for Sender Report and nothing else.
731 return 0;
732 }
733 if (sending_ && method_ == RtcpMode::kCompound) {
734 // Not allowed to send any RTCP packet without sender report.
735 return -1;
736 }
737 }
738
739 if (packet_type_counter_.first_packet_time_ms == -1)
740 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100741
742 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700743 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200744 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100745
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200746 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100747
danilchap56036ff2016-03-22 11:14:09 -0700748 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800749
Erik Språngf7c57762015-12-04 10:40:35 +0100750 auto it = report_flags_.begin();
751 while (it != report_flags_.end()) {
752 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800753 RTC_DCHECK(builder_it != builders_.end())
754 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100755 if (it->is_volatile) {
756 report_flags_.erase(it++);
757 } else {
758 ++it;
759 }
760
761 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700762 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Danil Chapovalov59e14642019-09-03 12:41:41 +0200763 if (packet == nullptr)
Erik Språngf7c57762015-12-04 10:40:35 +0100764 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800765 // If there is a BYE, don't append now - save it and append it
766 // at the end later.
767 if (builder_it->first == kRtcpBye) {
768 packet_bye = std::move(packet);
769 } else {
770 container.Append(packet.release());
771 }
772 }
773
774 // Append the BYE now at the end
775 if (packet_bye) {
776 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100777 }
778
779 if (packet_type_counter_observer_ != nullptr) {
780 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
781 remote_ssrc_, packet_type_counter_);
782 }
783
784 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800785 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000786 }
Erik Språng61be2a42015-04-27 13:32:52 +0200787
nisse6f142eb2017-02-21 07:32:47 -0800788 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100789 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000790}
791
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200792void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200793 bool generate_report;
794 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
795 // Report type already explicitly set, don't automatically populate.
796 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700797 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200798 } else {
799 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700800 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
801 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200802 if (generate_report)
803 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000804 }
805
Erik Språng0ea42d32015-06-25 14:46:16 +0200806 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200807 SetFlag(kRtcpSdes, true);
808
Erik Språng242e22b2015-05-11 10:17:43 +0200809 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800810 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200811 !feedback_state.last_xr_rtis.empty() ||
812 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800813 SetFlag(kRtcpAnyExtendedReports, true);
814 }
Erik Språng242e22b2015-05-11 10:17:43 +0200815
816 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800817 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000818
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800819 if (!audio_ && sending_) {
820 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
821 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
822 if (send_bitrate_kbit != 0) {
823 min_interval_ms = 360000 / send_bitrate_kbit;
824 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800825 }
Erik Språng61be2a42015-04-27 13:32:52 +0200826 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800827
danilchap47a740b2015-12-15 00:30:07 -0800828 // The interval between RTCP packets is varied randomly over the
829 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800830 int time_to_next =
831 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
832
833 RTC_DCHECK_GT(time_to_next, 0);
834 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000835
danilchap96b69bd2017-07-25 09:15:14 -0700836 // RtcpSender expected to be used for sending either just sender reports
837 // or just receiver reports.
838 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000839 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000840}
841
danilchap96b69bd2017-07-25 09:15:14 -0700842std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
843 const FeedbackState& feedback_state) {
844 std::vector<rtcp::ReportBlock> result;
845 if (!receive_statistics_)
846 return result;
danilchapa72e7342015-12-22 08:07:45 -0800847
danilchapf5f793c2017-07-27 04:44:18 -0700848 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
849 // compound rtcp packet when single rtcp module is used for multiple media
850 // streams.
851 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700852
853 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
854 (feedback_state.last_rr_ntp_frac != 0))) {
855 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200856 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700857
858 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
859 receive_time <<= 16;
860 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
861
862 uint32_t delay_since_last_sr = now - receive_time;
863 // TODO(danilchap): Instead of setting same value on all report blocks,
864 // set only when media_ssrc match sender ssrc of the sender report
865 // remote times were taken from.
866 for (auto& report_block : result) {
867 report_block.SetLastSr(feedback_state.remote_sr);
868 report_block.SetDelayLastSr(delay_since_last_sr);
869 }
danilchapa72e7342015-12-22 08:07:45 -0800870 }
danilchap96b69bd2017-07-25 09:15:14 -0700871 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000872}
873
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000874void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800875 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700876 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000877 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000878}
879
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000880int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
881 uint32_t name,
882 const uint8_t* data,
883 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200884 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100885 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200886 return -1;
887 }
danilchap56036ff2016-03-22 11:14:09 -0700888 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000889
Erik Språng242e22b2015-05-11 10:17:43 +0200890 SetFlag(kRtcpApp, true);
891 app_sub_type_ = subType;
892 app_name_ = name;
893 app_data_.reset(new uint8_t[length]);
894 app_length_ = length;
895 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200896 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000897}
898
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000899void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700900 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200901 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000902}
903
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000904bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700905 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200906 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000907}
908
danilchap853ecb22016-08-22 08:26:15 -0700909void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700910 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700911 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700912 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000913}
Erik Språng61be2a42015-04-27 13:32:52 +0200914
sprang5e38c962016-12-01 05:18:09 -0800915void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
916 if (type & kRtcpAnyExtendedReports) {
917 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
918 } else {
919 report_flags_.insert(ReportFlag(type, is_volatile));
920 }
Erik Språng242e22b2015-05-11 10:17:43 +0200921}
922
923void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
924 bool is_volatile) {
925 for (RTCPPacketType type : types)
926 SetFlag(type, is_volatile);
927}
928
sprang5e38c962016-12-01 05:18:09 -0800929bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200930 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
931}
932
sprang5e38c962016-12-01 05:18:09 -0800933bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200934 auto it = report_flags_.find(ReportFlag(type, false));
935 if (it == report_flags_.end())
936 return false;
937 if (it->is_volatile || forced)
938 report_flags_.erase((it));
939 return true;
940}
941
942bool RTCPSender::AllVolatileFlagsConsumed() const {
943 for (const ReportFlag& flag : report_flags_) {
944 if (flag.is_volatile)
945 return false;
946 }
947 return true;
948}
949
Erik Språng566124a2018-04-23 12:32:22 +0200950void RTCPSender::SetVideoBitrateAllocation(
951 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800952 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200953 // Check if this allocation is first ever, or has a different set of
954 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
955 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200956 absl::optional<VideoBitrateAllocation> new_bitrate =
957 CheckAndUpdateLayerStructure(bitrate);
958 if (new_bitrate) {
959 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100960 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
961 << " with new layers enabled/disabled: "
962 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200963 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200964 } else {
965 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200966 }
967
Erik Språng8782a582018-10-04 15:36:06 +0200968 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800969 SetFlag(kRtcpAnyExtendedReports, true);
970}
971
Erik Språng1cd73912018-10-05 12:57:59 +0200972absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200973 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200974 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200975 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
976 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200977 if (!updated_bitrate &&
978 (bitrate.HasBitrate(si, ti) !=
979 video_bitrate_allocation_.HasBitrate(si, ti) ||
980 (bitrate.GetBitrate(si, ti) == 0) !=
981 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
982 updated_bitrate = bitrate;
983 }
984 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
985 bitrate.GetBitrate(si, ti) == 0) {
986 // Make sure this stream disabling is explicitly signaled.
987 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +0200988 }
989 }
990 }
991
Erik Språng1cd73912018-10-05 12:57:59 +0200992 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200993}
994
Per Kjellander16999812019-10-10 12:57:28 +0200995void RTCPSender::SendCombinedRtcpPacket(
996 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
997 size_t max_packet_size;
998 uint32_t ssrc;
999 {
1000 rtc::CritScope lock(&critical_section_rtcp_sender_);
1001 if (method_ == RtcpMode::kOff) {
1002 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
1003 return;
1004 }
1005
1006 max_packet_size = max_packet_size_;
1007 ssrc = ssrc_;
1008 }
1009 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1010 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1011 if (transport_->SendRtcp(packet.data(), packet.size())) {
1012 if (event_log_)
1013 event_log_->Log(std::make_unique<RtcEventRtcpPacketOutgoing>(packet));
1014 }
1015 };
1016 PacketSender sender(callback, max_packet_size);
1017 for (auto& rtcp_packet : rtcp_packets) {
1018 rtcp_packet->SetSenderSsrc(ssrc);
1019 sender.AppendPacket(*rtcp_packet);
1020 }
1021 sender.Send();
1022}
1023
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001024} // namespace webrtc