blob: af5cd270cdfec80f28fbac599ecbad08e9afe1e7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/rtp_rtcp/source/rtcp_sender.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Jonas Olssona4d87372019-07-05 19:08:33 +020013#include <string.h> // memcpy
niklase@google.com470e71d2011-07-07 08:21:25 +000014
Jonas Olssona4d87372019-07-05 19:08:33 +020015#include <algorithm> // std::min
Danil Chapovalov70ffead2016-07-20 15:26:59 +020016#include <utility>
17
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Elad Alon4a87e1c2017-10-03 16:11:34 +020019#include "logging/rtc_event_log/events/rtc_event_rtcp_packet_outgoing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "logging/rtc_event_log/rtc_event_log.h"
21#include "modules/rtp_rtcp/source/rtcp_packet/app.h"
22#include "modules/rtp_rtcp/source/rtcp_packet/bye.h"
23#include "modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
24#include "modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
25#include "modules/rtp_rtcp/source/rtcp_packet/fir.h"
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;
sprang5e38c962016-12-01 05:18:09 -080051} // namespace
52
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000053RTCPSender::FeedbackState::FeedbackState()
nisse40ba3ad2017-03-17 07:04:00 -070054 : packets_sent(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000055 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000056 send_bitrate(0),
57 last_rr_ntp_secs(0),
58 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000059 remote_sr(0),
danilchap162abd32015-12-10 02:39:40 -080060 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000061
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +020062RTCPSender::FeedbackState::FeedbackState(const FeedbackState&) = default;
63
64RTCPSender::FeedbackState::FeedbackState(FeedbackState&&) = default;
65
66RTCPSender::FeedbackState::~FeedbackState() = default;
67
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010068class PacketContainer : public rtcp::CompoundPacket {
Erik Språngf7c57762015-12-04 10:40:35 +010069 public:
terelius429c3452016-01-21 05:42:04 -080070 PacketContainer(Transport* transport, RtcEventLog* event_log)
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010071 : transport_(transport), event_log_(event_log) {}
Danil Chapovalov2a5ce2b2018-02-07 09:38:31 +010072 ~PacketContainer() override {
Erik Språngf7c57762015-12-04 10:40:35 +010073 for (RtcpPacket* packet : appended_packets_)
74 delete packet;
75 }
76
danilchap41befce2016-03-30 11:11:51 -070077 size_t SendPackets(size_t max_payload_length) {
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010078 size_t bytes_sent = 0;
79 Build(max_payload_length, [&](rtc::ArrayView<const uint8_t> packet) {
80 if (transport_->SendRtcp(packet.data(), packet.size())) {
81 bytes_sent += packet.size();
82 if (event_log_) {
Karl Wiberg918f50c2018-07-05 11:40:33 +020083 event_log_->Log(
84 absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010085 }
86 }
87 });
88 return bytes_sent;
Erik Språngf7c57762015-12-04 10:40:35 +010089 }
90
91 private:
92 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -080093 RtcEventLog* const event_log_;
terelius429c3452016-01-21 05:42:04 -080094
95 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +010096};
97
98class RTCPSender::RtcpContext {
99 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200100 RtcpContext(const FeedbackState& feedback_state,
101 int32_t nack_size,
102 const uint16_t* nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200103 int64_t now_us)
Erik Språngf7c57762015-12-04 10:40:35 +0100104 : feedback_state_(feedback_state),
105 nack_size_(nack_size),
106 nack_list_(nack_list),
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200107 now_us_(now_us) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200108
Erik Språngf7c57762015-12-04 10:40:35 +0100109 const FeedbackState& feedback_state_;
110 const int32_t nack_size_;
111 const uint16_t* nack_list_;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200112 const int64_t now_us_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200113};
114
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000115RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000116 bool audio,
117 Clock* clock,
danilchapf5f793c2017-07-27 04:44:18 -0700118 ReceiveStatisticsProvider* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700119 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800120 RtcEventLog* event_log,
Jiawei Ou3587b832018-01-31 22:08:26 -0800121 Transport* outgoing_transport,
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800122 int report_interval_ms)
Peter Boströmac547a62015-09-17 23:03:57 +0200123 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200124 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800125 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700126 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800127 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700128 transport_(outgoing_transport),
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800129 report_interval_ms_(report_interval_ms),
Erik Språng242e22b2015-05-11 10:17:43 +0200130 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200131 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700132 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000133 last_rtp_timestamp_(0),
134 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200135 ssrc_(0),
136 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000137 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
Erik Språng242e22b2015-05-11 10:17:43 +0200139 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000140
Erik Språng242e22b2015-05-11 10:17:43 +0200141 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
danilchap2b616392016-08-18 06:17:42 -0700143 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200144 packet_oh_send_(0),
nisse284542b2017-01-10 08:58:32 -0800145 max_packet_size_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000146
Erik Språng242e22b2015-05-11 10:17:43 +0200147 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200148 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200149 app_data_(nullptr),
150 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
Erik Språng242e22b2015-05-11 10:17:43 +0200152 xr_send_receiver_reference_time_enabled_(false),
Erik Språng8782a582018-10-04 15:36:06 +0200153 packet_type_counter_observer_(packet_type_counter_observer),
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200154 send_video_bitrate_allocation_(false),
155 last_payload_type_(-1) {
sprang86fd9ed2015-09-29 04:45:43 -0700156 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200157
158 builders_[kRtcpSr] = &RTCPSender::BuildSR;
159 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200160 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200161 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
162 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
Erik Språng242e22b2015-05-11 10:17:43 +0200163 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
164 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
165 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
Elad Alon7d6a4c02019-02-25 13:00:51 +0100166 builders_[kRtcpLossNotification] = &RTCPSender::BuildLossNotification;
Erik Språng242e22b2015-05-11 10:17:43 +0200167 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
168 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
169 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
sprang5e38c962016-12-01 05:18:09 -0800170 builders_[kRtcpAnyExtendedReports] = &RTCPSender::BuildExtendedReports;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171}
172
danilchap162abd32015-12-10 02:39:40 -0800173RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
pbosda903ea2015-10-02 02:36:56 -0700175RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700176 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200177 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178}
179
skvlad1c392cc2016-04-01 14:46:44 -0700180void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700181 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000182
skvlad1c392cc2016-04-01 14:46:44 -0700183 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
184 // When switching on, reschedule the next packet
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800185 next_time_to_send_rtcp_ =
186 clock_->TimeInMilliseconds() + (report_interval_ms_ / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700187 }
188 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189}
190
Erik Språng61be2a42015-04-27 13:32:52 +0200191bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700192 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200193 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000194}
195
Erik Språng61be2a42015-04-27 13:32:52 +0200196int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
197 bool sending) {
198 bool sendRTCPBye = false;
199 {
danilchap56036ff2016-03-22 11:14:09 -0700200 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201
pbosda903ea2015-10-02 02:36:56 -0700202 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200203 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200204 // Trigger RTCP bye
205 sendRTCPBye = true;
206 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 }
Erik Språng242e22b2015-05-11 10:17:43 +0200208 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200209 }
210 if (sendRTCPBye)
211 return SendRTCP(feedback_state, kRtcpBye);
212 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
Elad Alon7d6a4c02019-02-25 13:00:51 +0100215int32_t RTCPSender::SendLossNotification(const FeedbackState& feedback_state,
216 uint16_t last_decoded_seq_num,
217 uint16_t last_received_seq_num,
Elad Alone86af2c2019-06-03 14:37:50 +0200218 bool decodability_flag,
219 bool buffering_allowed) {
Elad Alon7d6a4c02019-02-25 13:00:51 +0100220 rtc::CritScope lock(&critical_section_rtcp_sender_);
221
222 loss_notification_state_.last_decoded_seq_num = last_decoded_seq_num;
223 loss_notification_state_.last_received_seq_num = last_received_seq_num;
224 loss_notification_state_.decodability_flag = decodability_flag;
225
226 SetFlag(kRtcpLossNotification, /*is_volatile=*/true);
227
Elad Alone86af2c2019-06-03 14:37:50 +0200228 if (buffering_allowed) {
229 // The loss notification will be batched with additional feedback messages.
230 return 0;
231 }
232
Elad Alon7d6a4c02019-02-25 13:00:51 +0100233 return SendCompoundRTCP(feedback_state,
234 {RTCPPacketType::kRtcpLossNotification});
235}
236
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100237void RTCPSender::SetRemb(int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
238 RTC_CHECK_GE(bitrate_bps, 0);
danilchap56036ff2016-03-22 11:14:09 -0700239 rtc::CritScope lock(&critical_section_rtcp_sender_);
Danil Chapovalov1de4b622017-12-13 13:35:10 +0100240 remb_bitrate_ = bitrate_bps;
241 remb_ssrcs_ = std::move(ssrcs);
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000242
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200243 SetFlag(kRtcpRemb, /*is_volatile=*/false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000244 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
245 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200246 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000247}
248
Danil Chapovalovf74d6412017-10-18 13:32:57 +0200249void RTCPSender::UnsetRemb() {
250 rtc::CritScope lock(&critical_section_rtcp_sender_);
251 // Stop sending REMB each report until it is reenabled and REMB data set.
252 ConsumeFlag(kRtcpRemb, /*forced=*/true);
253}
254
Erik Språng61be2a42015-04-27 13:32:52 +0200255bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700256 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200257 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000258}
259
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000260void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700261 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200262 if (enable) {
263 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
264 } else {
265 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
266 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
nisse284542b2017-01-10 08:58:32 -0800269void RTCPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
nisse6f142eb2017-02-21 07:32:47 -0800270 rtc::CritScope lock(&critical_section_rtcp_sender_);
nisse284542b2017-01-10 08:58:32 -0800271 max_packet_size_ = max_packet_size;
danilchap41befce2016-03-30 11:11:51 -0700272}
273
danilchap71fead22016-08-18 02:01:49 -0700274void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700275 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700276 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000277}
278
279void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200280 int64_t capture_time_ms,
281 int8_t payload_type) {
danilchap56036ff2016-03-22 11:14:09 -0700282 rtc::CritScope lock(&critical_section_rtcp_sender_);
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200283 // For compatibility with clients who don't set payload type correctly on all
284 // calls.
285 if (payload_type != -1) {
286 last_payload_type_ = payload_type;
287 }
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000288 last_rtp_timestamp_ = rtp_timestamp;
289 if (capture_time_ms < 0) {
290 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200291 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000292 } else {
293 last_frame_capture_time_ms_ = capture_time_ms;
294 }
295}
296
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200297void RTCPSender::SetRtpClockRate(int8_t payload_type, int rtp_clock_rate_hz) {
298 rtc::CritScope lock(&critical_section_rtcp_sender_);
299 rtp_clock_rates_khz_[payload_type] = rtp_clock_rate_hz / 1000;
300}
301
nisse14adba72017-03-20 03:52:39 -0700302uint32_t RTCPSender::SSRC() const {
303 rtc::CritScope lock(&critical_section_rtcp_sender_);
304 return ssrc_;
305}
306
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000307void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700308 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
Erik Språng242e22b2015-05-11 10:17:43 +0200310 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200311 // not first SetSSRC, probably due to a collision
312 // schedule a new RTCP report
313 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200314 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200315 }
Erik Språng242e22b2015-05-11 10:17:43 +0200316 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000317}
318
Erik Språng61be2a42015-04-27 13:32:52 +0200319void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700320 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200321 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
Peter Boström9ba52f82015-06-01 14:12:28 +0200324int32_t RTCPSender::SetCNAME(const char* c_name) {
325 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000326 return -1;
327
kwiberg352444f2016-11-28 15:58:53 -0800328 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700329 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200330 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000331 return 0;
332}
333
Erik Språng0ea42d32015-06-25 14:46:16 +0200334int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700335 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800336 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700337 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700338 // One spot is reserved for ssrc_/cname_.
339 // TODO(danilchap): Add support for more than 30 contributes by sending
340 // several sdes packets.
341 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000342 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200343
344 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000345 return 0;
346}
347
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000348int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700349 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200350 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000351
Erik Språng242e22b2015-05-11 10:17:43 +0200352 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000353 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200354
Erik Språng242e22b2015-05-11 10:17:43 +0200355 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000356 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000357}
358
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000359bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800360 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800361 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000362
Jiawei Ou3587b832018-01-31 22:08:26 -0800363 For video we use a configurable interval (default: 1 second) for a BW
364 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
365 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
367
danilchap162abd32015-12-10 02:39:40 -0800368 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000369
danilchap162abd32015-12-10 02:39:40 -0800370 MAX RTCP BW is 5% if the session BW
371 A send report is approximately 65 bytes inc CNAME
372 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
danilchap162abd32015-12-10 02:39:40 -0800374 The RECOMMENDED value for the reduced minimum in seconds is 360
375 divided by the session bandwidth in kilobits/second. This minimum
376 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000377
danilchap162abd32015-12-10 02:39:40 -0800378 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800379 initial is true), the constant Tmin is set to half of the configured
380 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000381
danilchap162abd32015-12-10 02:39:40 -0800382 The interval between RTCP packets is varied randomly over the
383 range [0.5,1.5] times the calculated interval to avoid unintended
384 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
danilchap162abd32015-12-10 02:39:40 -0800386 if we send
387 If the participant is a sender (we_sent true), the constant C is
388 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
389 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
390 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000391
danilchap162abd32015-12-10 02:39:40 -0800392 if we receive only
393 If we_sent is not true, the constant C is set
394 to the average RTCP packet size divided by 75% of the RTCP
395 bandwidth. The constant n is set to the number of receivers
396 (members - senders). If the number of senders is greater than
397 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
danilchap162abd32015-12-10 02:39:40 -0800399 reconsideration NOT required for peer-to-peer
400 "timer reconsideration" is
401 employed. This algorithm implements a simple back-off mechanism
402 which causes users to hold back RTCP packet transmission if the
403 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
danilchap162abd32015-12-10 02:39:40 -0800405 n = number of members
406 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
danilchap162abd32015-12-10 02:39:40 -0800408 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
danilchap162abd32015-12-10 02:39:40 -0800410 4. The calculated interval T is set to a number uniformly distributed
411 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
danilchap162abd32015-12-10 02:39:40 -0800413 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
414 for the fact that the timer reconsideration algorithm converges to
415 a value of the RTCP bandwidth below the intended average
416 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
Erik Språng242e22b2015-05-11 10:17:43 +0200418 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000419
danilchap56036ff2016-03-22 11:14:09 -0700420 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000421
pbosda903ea2015-10-02 02:36:56 -0700422 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000424
Erik Språng242e22b2015-05-11 10:17:43 +0200425 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200426 // for video key-frames we want to send the RTCP before the large key-frame
427 // if we have a 100 ms margin
428 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
429 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
Erik Språng242e22b2015-05-11 10:17:43 +0200431 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200432 return true;
433 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200434 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200435 // wrap
436 return true;
437 }
438 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
danilchap56036ff2016-03-22 11:14:09 -0700441std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200442 // Timestamp shouldn't be estimated before first media frame.
443 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200444 // The timestamp of this RTCP packet should be estimated as the timestamp of
445 // the frame being captured at this moment. We are calculating that
446 // timestamp as the last frame's timestamp + the time since the last frame
447 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200448 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
449 if (rtp_rate <= 0) {
450 rtp_rate =
451 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
452 1000;
453 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200454 // Round now_us_ to the closest millisecond, because Ntp time is rounded
455 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200456 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700457 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200458 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459
Erik Språngf7c57762015-12-04 10:40:35 +0100460 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700461 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200462 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700463 report->SetRtpTimestamp(rtp_timestamp);
464 report->SetPacketCount(ctx.feedback_state_.packets_sent);
465 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700466 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100467
danilchap56036ff2016-03-22 11:14:09 -0700468 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000469}
470
danilchap56036ff2016-03-22 11:14:09 -0700471std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100472 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200473 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800474 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000475
Erik Språngf7c57762015-12-04 10:40:35 +0100476 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700477 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200478
danilchap74e8df8f2017-03-16 08:04:08 -0700479 for (const auto& it : csrc_cnames_)
480 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200481
danilchap56036ff2016-03-22 11:14:09 -0700482 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000483}
484
danilchap56036ff2016-03-22 11:14:09 -0700485std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100486 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700487 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700488 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200489
danilchap56036ff2016-03-22 11:14:09 -0700490 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000491}
492
danilchap56036ff2016-03-22 11:14:09 -0700493std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100494 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700495 pli->SetSenderSsrc(ssrc_);
496 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200497
Erik Språng242e22b2015-05-11 10:17:43 +0200498 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200499
danilchap56036ff2016-03-22 11:14:09 -0700500 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200501}
502
danilchap56036ff2016-03-22 11:14:09 -0700503std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800504 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
Erik Språngf7c57762015-12-04 10:40:35 +0100506 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700507 fir->SetSenderSsrc(ssrc_);
508 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200509
Erik Språng242e22b2015-05-11 10:17:43 +0200510 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200511
danilchap56036ff2016-03-22 11:14:09 -0700512 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
danilchap56036ff2016-03-22 11:14:09 -0700515std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100516 const RtcpContext& ctx) {
517 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700518 remb->SetSenderSsrc(ssrc_);
519 remb->SetBitrateBps(remb_bitrate_);
520 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200521
danilchap56036ff2016-03-22 11:14:09 -0700522 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000523}
524
Erik Språng61be2a42015-04-27 13:32:52 +0200525void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700526 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700527 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000528}
529
danilchap56036ff2016-03-22 11:14:09 -0700530std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100531 const RtcpContext& ctx) {
532 if (ctx.feedback_state_.module == nullptr)
533 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200534 // Before sending the TMMBR check the received TMMBN, only an owner is
535 // allowed to raise the bitrate:
536 // * If the sender is an owner of the TMMBN -> send TMMBR
537 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000538
Erik Språng61be2a42015-04-27 13:32:52 +0200539 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700540 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000541
Erik Språng242e22b2015-05-11 10:17:43 +0200542 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
543 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200544 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700545 std::vector<rtcp::TmmbItem> candidates =
546 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000547
danilchap2b616392016-08-18 06:17:42 -0700548 if (!candidates.empty()) {
549 for (const auto& candidate : candidates) {
550 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
551 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100552 // Do not send the same tuple.
553 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200554 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 }
danilchap2b616392016-08-18 06:17:42 -0700556 if (!tmmbr_owner) {
557 // Use received bounding set as candidate set.
558 // Add current tuple.
559 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
danilchap2b616392016-08-18 06:17:42 -0700561 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700562 std::vector<rtcp::TmmbItem> bounding =
563 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700564 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
565 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100566 // Did not enter bounding set, no meaning to send this request.
567 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200568 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000569 }
Erik Språng61be2a42015-04-27 13:32:52 +0200570 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000571
danilchap2b616392016-08-18 06:17:42 -0700572 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100573 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700574
Erik Språngf7c57762015-12-04 10:40:35 +0100575 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700576 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800577 rtcp::TmmbItem request;
578 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700579 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800580 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700581 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100582
danilchap56036ff2016-03-22 11:14:09 -0700583 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200584}
585
danilchap56036ff2016-03-22 11:14:09 -0700586std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100587 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100588 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700589 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700590 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
591 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700592 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000593 }
Erik Språng61be2a42015-04-27 13:32:52 +0200594 }
sprangd83df502015-08-27 01:05:08 -0700595
danilchap56036ff2016-03-22 11:14:09 -0700596 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000597}
598
danilchap56036ff2016-03-22 11:14:09 -0700599std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100600 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700601 app->SetSsrc(ssrc_);
602 app->SetSubType(app_sub_type_);
603 app->SetName(app_name_);
604 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200605
danilchap56036ff2016-03-22 11:14:09 -0700606 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200607}
608
Elad Alon7d6a4c02019-02-25 13:00:51 +0100609std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
610 const RtcpContext& ctx) {
611 auto loss_notification = absl::make_unique<rtcp::LossNotification>(
612 loss_notification_state_.last_decoded_seq_num,
613 loss_notification_state_.last_received_seq_num,
614 loss_notification_state_.decodability_flag);
615 loss_notification->SetSenderSsrc(ssrc_);
616 loss_notification->SetMediaSsrc(remote_ssrc_);
617 return std::move(loss_notification);
618}
619
danilchap56036ff2016-03-22 11:14:09 -0700620std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100621 const RtcpContext& ctx) {
622 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700623 nack->SetSenderSsrc(ssrc_);
624 nack->SetMediaSsrc(remote_ssrc_);
625 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200626
627 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100628 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100629 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200630 }
Erik Språng61be2a42015-04-27 13:32:52 +0200631 packet_type_counter_.nack_requests = nack_stats_.requests();
632 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200633
Erik Språng242e22b2015-05-11 10:17:43 +0200634 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200635
danilchap56036ff2016-03-22 11:14:09 -0700636 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200637}
638
danilchap56036ff2016-03-22 11:14:09 -0700639std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100640 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700641 bye->SetSenderSsrc(ssrc_);
642 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700643
danilchap56036ff2016-03-22 11:14:09 -0700644 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000645}
646
sprang5e38c962016-12-01 05:18:09 -0800647std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100648 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800649 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700650 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000651
sprang5e38c962016-12-01 05:18:09 -0800652 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
653 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200654 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800655 xr->SetRrtr(rrtr);
656 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000657
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200658 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
659 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800660 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000661
Erik Språng8782a582018-10-04 15:36:06 +0200662 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800663 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000664
sprang5e38c962016-12-01 05:18:09 -0800665 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
666 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200667 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100668 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200669 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100670 }
sprang5e38c962016-12-01 05:18:09 -0800671 }
672 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000673
sprang5e38c962016-12-01 05:18:09 -0800674 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200675 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800676 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200677
sprang5e38c962016-12-01 05:18:09 -0800678 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000679}
680
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000681int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200682 RTCPPacketType packetType,
683 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700684 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200685 return SendCompoundRTCP(
686 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700687 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200688}
689
690int32_t RTCPSender::SendCompoundRTCP(
691 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100692 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200693 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700694 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800695 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800696 size_t max_packet_size;
697
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000698 {
danilchap56036ff2016-03-22 11:14:09 -0700699 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700700 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100701 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200702 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000703 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200704 // Add all flags as volatile. Non volatile entries will not be overwritten.
705 // All new volatile flags added will be consumed by the end of this call.
706 SetFlags(packet_types, true);
707
708 // Prevent sending streams to send SR before any media has been sent.
709 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
710 if (!can_calculate_rtp_timestamp) {
711 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
712 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
713 bool sender_report = consumed_report_flag || consumed_sr_flag;
714 if (sender_report && AllVolatileFlagsConsumed()) {
715 // This call was for Sender Report and nothing else.
716 return 0;
717 }
718 if (sending_ && method_ == RtcpMode::kCompound) {
719 // Not allowed to send any RTCP packet without sender report.
720 return -1;
721 }
722 }
723
724 if (packet_type_counter_.first_packet_time_ms == -1)
725 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100726
727 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700728 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200729 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100730
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200731 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100732
danilchap56036ff2016-03-22 11:14:09 -0700733 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800734
Erik Språngf7c57762015-12-04 10:40:35 +0100735 auto it = report_flags_.begin();
736 while (it != report_flags_.end()) {
737 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800738 RTC_DCHECK(builder_it != builders_.end())
739 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100740 if (it->is_volatile) {
741 report_flags_.erase(it++);
742 } else {
743 ++it;
744 }
745
746 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700747 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100748 if (packet.get() == nullptr)
749 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800750 // If there is a BYE, don't append now - save it and append it
751 // at the end later.
752 if (builder_it->first == kRtcpBye) {
753 packet_bye = std::move(packet);
754 } else {
755 container.Append(packet.release());
756 }
757 }
758
759 // Append the BYE now at the end
760 if (packet_bye) {
761 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100762 }
763
764 if (packet_type_counter_observer_ != nullptr) {
765 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
766 remote_ssrc_, packet_type_counter_);
767 }
768
769 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800770 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000771 }
Erik Språng61be2a42015-04-27 13:32:52 +0200772
nisse6f142eb2017-02-21 07:32:47 -0800773 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100774 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000775}
776
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200777void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200778 bool generate_report;
779 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
780 // Report type already explicitly set, don't automatically populate.
781 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700782 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200783 } else {
784 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700785 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
786 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200787 if (generate_report)
788 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000789 }
790
Erik Språng0ea42d32015-06-25 14:46:16 +0200791 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200792 SetFlag(kRtcpSdes, true);
793
Erik Språng242e22b2015-05-11 10:17:43 +0200794 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800795 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200796 !feedback_state.last_xr_rtis.empty() ||
797 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800798 SetFlag(kRtcpAnyExtendedReports, true);
799 }
Erik Språng242e22b2015-05-11 10:17:43 +0200800
801 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800802 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000803
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800804 if (!audio_ && sending_) {
805 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
806 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
807 if (send_bitrate_kbit != 0) {
808 min_interval_ms = 360000 / send_bitrate_kbit;
809 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800810 }
Erik Språng61be2a42015-04-27 13:32:52 +0200811 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800812
danilchap47a740b2015-12-15 00:30:07 -0800813 // The interval between RTCP packets is varied randomly over the
814 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800815 int time_to_next =
816 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
817
818 RTC_DCHECK_GT(time_to_next, 0);
819 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000820
danilchap96b69bd2017-07-25 09:15:14 -0700821 // RtcpSender expected to be used for sending either just sender reports
822 // or just receiver reports.
823 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000824 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000825}
826
danilchap96b69bd2017-07-25 09:15:14 -0700827std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
828 const FeedbackState& feedback_state) {
829 std::vector<rtcp::ReportBlock> result;
830 if (!receive_statistics_)
831 return result;
danilchapa72e7342015-12-22 08:07:45 -0800832
danilchapf5f793c2017-07-27 04:44:18 -0700833 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
834 // compound rtcp packet when single rtcp module is used for multiple media
835 // streams.
836 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700837
838 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
839 (feedback_state.last_rr_ntp_frac != 0))) {
840 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200841 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700842
843 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
844 receive_time <<= 16;
845 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
846
847 uint32_t delay_since_last_sr = now - receive_time;
848 // TODO(danilchap): Instead of setting same value on all report blocks,
849 // set only when media_ssrc match sender ssrc of the sender report
850 // remote times were taken from.
851 for (auto& report_block : result) {
852 report_block.SetLastSr(feedback_state.remote_sr);
853 report_block.SetDelayLastSr(delay_since_last_sr);
854 }
danilchapa72e7342015-12-22 08:07:45 -0800855 }
danilchap96b69bd2017-07-25 09:15:14 -0700856 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000857}
858
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000859void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800860 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700861 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000862 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000863}
864
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000865int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
866 uint32_t name,
867 const uint8_t* data,
868 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200869 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100870 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200871 return -1;
872 }
danilchap56036ff2016-03-22 11:14:09 -0700873 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000874
Erik Språng242e22b2015-05-11 10:17:43 +0200875 SetFlag(kRtcpApp, true);
876 app_sub_type_ = subType;
877 app_name_ = name;
878 app_data_.reset(new uint8_t[length]);
879 app_length_ = length;
880 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200881 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000882}
883
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000884void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700885 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200886 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000887}
888
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000889bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700890 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200891 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000892}
893
danilchap853ecb22016-08-22 08:26:15 -0700894void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700895 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700896 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700897 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000898}
Erik Språng61be2a42015-04-27 13:32:52 +0200899
sprang5e38c962016-12-01 05:18:09 -0800900void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
901 if (type & kRtcpAnyExtendedReports) {
902 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
903 } else {
904 report_flags_.insert(ReportFlag(type, is_volatile));
905 }
Erik Språng242e22b2015-05-11 10:17:43 +0200906}
907
908void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
909 bool is_volatile) {
910 for (RTCPPacketType type : types)
911 SetFlag(type, is_volatile);
912}
913
sprang5e38c962016-12-01 05:18:09 -0800914bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200915 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
916}
917
sprang5e38c962016-12-01 05:18:09 -0800918bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200919 auto it = report_flags_.find(ReportFlag(type, false));
920 if (it == report_flags_.end())
921 return false;
922 if (it->is_volatile || forced)
923 report_flags_.erase((it));
924 return true;
925}
926
927bool RTCPSender::AllVolatileFlagsConsumed() const {
928 for (const ReportFlag& flag : report_flags_) {
929 if (flag.is_volatile)
930 return false;
931 }
932 return true;
933}
934
Erik Språng566124a2018-04-23 12:32:22 +0200935void RTCPSender::SetVideoBitrateAllocation(
936 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800937 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200938 // Check if this allocation is first ever, or has a different set of
939 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
940 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200941 absl::optional<VideoBitrateAllocation> new_bitrate =
942 CheckAndUpdateLayerStructure(bitrate);
943 if (new_bitrate) {
944 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100945 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
946 << " with new layers enabled/disabled: "
947 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200948 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200949 } else {
950 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200951 }
952
Erik Språng8782a582018-10-04 15:36:06 +0200953 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800954 SetFlag(kRtcpAnyExtendedReports, true);
955}
956
Erik Språng1cd73912018-10-05 12:57:59 +0200957absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200958 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200959 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200960 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
961 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200962 if (!updated_bitrate &&
963 (bitrate.HasBitrate(si, ti) !=
964 video_bitrate_allocation_.HasBitrate(si, ti) ||
965 (bitrate.GetBitrate(si, ti) == 0) !=
966 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
967 updated_bitrate = bitrate;
968 }
969 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
970 bitrate.GetBitrate(si, ti) == 0) {
971 // Make sure this stream disabling is explicitly signaled.
972 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +0200973 }
974 }
975 }
976
Erik Språng1cd73912018-10-05 12:57:59 +0200977 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200978}
979
sprang233bd872015-09-08 13:25:16 -0700980bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -0800981 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -0800982 {
983 rtc::CritScope lock(&critical_section_rtcp_sender_);
984 if (method_ == RtcpMode::kOff)
985 return false;
nisse6f142eb2017-02-21 07:32:47 -0800986 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -0800987 }
988
nisse6f142eb2017-02-21 07:32:47 -0800989 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100990 bool send_failure = false;
991 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
992 if (transport_->SendRtcp(packet.data(), packet.size())) {
993 if (event_log_)
Karl Wiberg918f50c2018-07-05 11:40:33 +0200994 event_log_->Log(absl::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100995 } else {
996 send_failure = true;
997 }
998 };
999 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -07001000}
1001
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001002} // namespace webrtc