blob: 15325d1592ff7d7223d2811f9d7aed6b659eb87d [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),
153 clock_(config.clock),
danilchap47a740b2015-12-15 00:30:07 -0800154 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700155 method_(RtcpMode::kOff),
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000156 event_log_(config.event_log),
157 transport_(config.outgoing_transport),
158 report_interval_ms_(config.rtcp_report_interval_ms > 0
159 ? config.rtcp_report_interval_ms
160 : (config.audio ? kDefaultAudioReportInterval
161 : kDefaultVideoReportInterval)),
Erik Språng242e22b2015-05-11 10:17:43 +0200162 sending_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200163 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700164 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000165 last_rtp_timestamp_(0),
166 last_frame_capture_time_ms_(-1),
Erik Språngc15f92a2019-08-21 15:54:16 +0200167 ssrc_(config.local_media_ssrc.value_or(0)),
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
nisse14adba72017-03-20 03:52:39 -0700334uint32_t RTCPSender::SSRC() const {
335 rtc::CritScope lock(&critical_section_rtcp_sender_);
336 return ssrc_;
337}
338
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000339void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700340 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000341
Mirko Bonadei9b1f24d2019-07-12 17:32:28 +0000342 if (ssrc_ != 0 && ssrc != ssrc_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200343 // not first SetSSRC, probably due to a collision
344 // schedule a new RTCP report
345 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200346 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200347 }
Erik Språng242e22b2015-05-11 10:17:43 +0200348 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000349}
350
Erik Språng61be2a42015-04-27 13:32:52 +0200351void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700352 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200353 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
Peter Boström9ba52f82015-06-01 14:12:28 +0200356int32_t RTCPSender::SetCNAME(const char* c_name) {
357 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000358 return -1;
359
kwiberg352444f2016-11-28 15:58:53 -0800360 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700361 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200362 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000363 return 0;
364}
365
Erik Språng0ea42d32015-06-25 14:46:16 +0200366int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700367 RTC_DCHECK(c_name);
kwiberg352444f2016-11-28 15:58:53 -0800368 RTC_DCHECK_LT(strlen(c_name), RTCP_CNAME_SIZE);
danilchap56036ff2016-03-22 11:14:09 -0700369 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap74e8df8f2017-03-16 08:04:08 -0700370 // One spot is reserved for ssrc_/cname_.
371 // TODO(danilchap): Add support for more than 30 contributes by sending
372 // several sdes packets.
373 if (csrc_cnames_.size() >= rtcp::Sdes::kMaxNumberOfChunks - 1)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000374 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200375
376 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000377 return 0;
378}
379
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000380int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700381 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200382 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000383
Erik Språng242e22b2015-05-11 10:17:43 +0200384 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000385 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200386
Erik Språng242e22b2015-05-11 10:17:43 +0200387 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000388 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000391bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800392 /*
Jiawei Ou3587b832018-01-31 22:08:26 -0800393 For audio we use a configurable interval (default: 5 seconds)
niklase@google.com470e71d2011-07-07 08:21:25 +0000394
Jiawei Ou3587b832018-01-31 22:08:26 -0800395 For video we use a configurable interval (default: 1 second) for a BW
396 smaller than 360 kbit/s, technicaly we break the max 5% RTCP BW for
397 video below 10 kbit/s but that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000398
399
danilchap162abd32015-12-10 02:39:40 -0800400 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
danilchap162abd32015-12-10 02:39:40 -0800402 MAX RTCP BW is 5% if the session BW
403 A send report is approximately 65 bytes inc CNAME
404 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
danilchap162abd32015-12-10 02:39:40 -0800406 The RECOMMENDED value for the reduced minimum in seconds is 360
407 divided by the session bandwidth in kilobits/second. This minimum
408 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
danilchap162abd32015-12-10 02:39:40 -0800410 If the participant has not yet sent an RTCP packet (the variable
Jiawei Ou3587b832018-01-31 22:08:26 -0800411 initial is true), the constant Tmin is set to half of the configured
412 interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000413
danilchap162abd32015-12-10 02:39:40 -0800414 The interval between RTCP packets is varied randomly over the
415 range [0.5,1.5] times the calculated interval to avoid unintended
416 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000417
danilchap162abd32015-12-10 02:39:40 -0800418 if we send
419 If the participant is a sender (we_sent true), the constant C is
420 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
421 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
422 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000423
danilchap162abd32015-12-10 02:39:40 -0800424 if we receive only
425 If we_sent is not true, the constant C is set
426 to the average RTCP packet size divided by 75% of the RTCP
427 bandwidth. The constant n is set to the number of receivers
428 (members - senders). If the number of senders is greater than
429 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000430
danilchap162abd32015-12-10 02:39:40 -0800431 reconsideration NOT required for peer-to-peer
432 "timer reconsideration" is
433 employed. This algorithm implements a simple back-off mechanism
434 which causes users to hold back RTCP packet transmission if the
435 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000436
danilchap162abd32015-12-10 02:39:40 -0800437 n = number of members
438 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000439
danilchap162abd32015-12-10 02:39:40 -0800440 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000441
danilchap162abd32015-12-10 02:39:40 -0800442 4. The calculated interval T is set to a number uniformly distributed
443 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000444
danilchap162abd32015-12-10 02:39:40 -0800445 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
446 for the fact that the timer reconsideration algorithm converges to
447 a value of the RTCP bandwidth below the intended average
448 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000449
Erik Språng242e22b2015-05-11 10:17:43 +0200450 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000451
danilchap56036ff2016-03-22 11:14:09 -0700452 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000453
pbosda903ea2015-10-02 02:36:56 -0700454 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000455 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000456
Erik Språng242e22b2015-05-11 10:17:43 +0200457 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200458 // for video key-frames we want to send the RTCP before the large key-frame
459 // if we have a 100 ms margin
460 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
461 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
Erik Språng242e22b2015-05-11 10:17:43 +0200463 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200464 return true;
465 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200466 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200467 // wrap
468 return true;
469 }
470 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000471}
472
danilchap56036ff2016-03-22 11:14:09 -0700473std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200474 // Timestamp shouldn't be estimated before first media frame.
475 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200476 // The timestamp of this RTCP packet should be estimated as the timestamp of
477 // the frame being captured at this moment. We are calculating that
478 // timestamp as the last frame's timestamp + the time since the last frame
479 // was captured.
Ilya Nikolaevskiy5e58bcb2018-10-24 13:34:32 +0200480 int rtp_rate = rtp_clock_rates_khz_[last_payload_type_];
481 if (rtp_rate <= 0) {
482 rtp_rate =
483 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) /
484 1000;
485 }
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200486 // Round now_us_ to the closest millisecond, because Ntp time is rounded
487 // when converted to milliseconds,
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200488 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700489 timestamp_offset_ + last_rtp_timestamp_ +
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200490 ((ctx.now_us_ + 500) / 1000 - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000491
Erik Språngf7c57762015-12-04 10:40:35 +0100492 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700493 report->SetSenderSsrc(ssrc_);
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200494 report->SetNtp(TimeMicrosToNtp(ctx.now_us_));
danilchap822a16f2016-09-27 09:27:47 -0700495 report->SetRtpTimestamp(rtp_timestamp);
496 report->SetPacketCount(ctx.feedback_state_.packets_sent);
497 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
danilchap96b69bd2017-07-25 09:15:14 -0700498 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språngf7c57762015-12-04 10:40:35 +0100499
danilchap56036ff2016-03-22 11:14:09 -0700500 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000501}
502
danilchap56036ff2016-03-22 11:14:09 -0700503std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100504 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200505 size_t length_cname = cname_.length();
kwiberg352444f2016-11-28 15:58:53 -0800506 RTC_CHECK_LT(length_cname, RTCP_CNAME_SIZE);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
Erik Språngf7c57762015-12-04 10:40:35 +0100508 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700509 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200510
danilchap74e8df8f2017-03-16 08:04:08 -0700511 for (const auto& it : csrc_cnames_)
512 RTC_CHECK(sdes->AddCName(it.first, it.second));
Erik Språng0ea42d32015-06-25 14:46:16 +0200513
danilchap56036ff2016-03-22 11:14:09 -0700514 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000515}
516
danilchap56036ff2016-03-22 11:14:09 -0700517std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100518 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700519 report->SetSenderSsrc(ssrc_);
danilchap96b69bd2017-07-25 09:15:14 -0700520 report->SetReportBlocks(CreateReportBlocks(ctx.feedback_state_));
Erik Språng61be2a42015-04-27 13:32:52 +0200521
danilchap56036ff2016-03-22 11:14:09 -0700522 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000523}
524
danilchap56036ff2016-03-22 11:14:09 -0700525std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100526 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700527 pli->SetSenderSsrc(ssrc_);
528 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200529
Erik Språng242e22b2015-05-11 10:17:43 +0200530 ++packet_type_counter_.pli_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200531
danilchap56036ff2016-03-22 11:14:09 -0700532 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200533}
534
danilchap56036ff2016-03-22 11:14:09 -0700535std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
danilchap498ee8e2017-02-08 05:24:31 -0800536 ++sequence_number_fir_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000537
Erik Språngf7c57762015-12-04 10:40:35 +0100538 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700539 fir->SetSenderSsrc(ssrc_);
540 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200541
Erik Språng242e22b2015-05-11 10:17:43 +0200542 ++packet_type_counter_.fir_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200543
danilchap56036ff2016-03-22 11:14:09 -0700544 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000545}
546
danilchap56036ff2016-03-22 11:14:09 -0700547std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100548 const RtcpContext& ctx) {
549 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700550 remb->SetSenderSsrc(ssrc_);
551 remb->SetBitrateBps(remb_bitrate_);
552 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200553
danilchap56036ff2016-03-22 11:14:09 -0700554 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000555}
556
Erik Språng61be2a42015-04-27 13:32:52 +0200557void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700558 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700559 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000560}
561
danilchap56036ff2016-03-22 11:14:09 -0700562std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100563 const RtcpContext& ctx) {
564 if (ctx.feedback_state_.module == nullptr)
565 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200566 // Before sending the TMMBR check the received TMMBN, only an owner is
567 // allowed to raise the bitrate:
568 // * If the sender is an owner of the TMMBN -> send TMMBR
569 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000570
Erik Språng61be2a42015-04-27 13:32:52 +0200571 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700572 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000573
Erik Språng242e22b2015-05-11 10:17:43 +0200574 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
575 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200576 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700577 std::vector<rtcp::TmmbItem> candidates =
578 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
danilchap2b616392016-08-18 06:17:42 -0700580 if (!candidates.empty()) {
581 for (const auto& candidate : candidates) {
582 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
583 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100584 // Do not send the same tuple.
585 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200586 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000587 }
danilchap2b616392016-08-18 06:17:42 -0700588 if (!tmmbr_owner) {
589 // Use received bounding set as candidate set.
590 // Add current tuple.
591 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000592
danilchap2b616392016-08-18 06:17:42 -0700593 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700594 std::vector<rtcp::TmmbItem> bounding =
595 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700596 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
597 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100598 // Did not enter bounding set, no meaning to send this request.
599 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200600 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000601 }
Erik Språng61be2a42015-04-27 13:32:52 +0200602 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000603
danilchap2b616392016-08-18 06:17:42 -0700604 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100605 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700606
Erik Språngf7c57762015-12-04 10:40:35 +0100607 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700608 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800609 rtcp::TmmbItem request;
610 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700611 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800612 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700613 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100614
danilchap56036ff2016-03-22 11:14:09 -0700615 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200616}
617
danilchap56036ff2016-03-22 11:14:09 -0700618std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100619 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100620 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700621 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700622 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
623 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700624 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000625 }
Erik Språng61be2a42015-04-27 13:32:52 +0200626 }
sprangd83df502015-08-27 01:05:08 -0700627
danilchap56036ff2016-03-22 11:14:09 -0700628 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000629}
630
danilchap56036ff2016-03-22 11:14:09 -0700631std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100632 rtcp::App* app = new rtcp::App();
Per Kjellander955f8fd2019-10-09 13:30:36 +0200633 app->SetSenderSsrc(ssrc_);
danilchap822a16f2016-09-27 09:27:47 -0700634 app->SetSubType(app_sub_type_);
635 app->SetName(app_name_);
636 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200637
danilchap56036ff2016-03-22 11:14:09 -0700638 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200639}
640
Elad Alon7d6a4c02019-02-25 13:00:51 +0100641std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildLossNotification(
642 const RtcpContext& ctx) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200643 auto loss_notification = std::make_unique<rtcp::LossNotification>(
Elad Alon7d6a4c02019-02-25 13:00:51 +0100644 loss_notification_state_.last_decoded_seq_num,
645 loss_notification_state_.last_received_seq_num,
646 loss_notification_state_.decodability_flag);
647 loss_notification->SetSenderSsrc(ssrc_);
648 loss_notification->SetMediaSsrc(remote_ssrc_);
649 return std::move(loss_notification);
650}
651
danilchap56036ff2016-03-22 11:14:09 -0700652std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100653 const RtcpContext& ctx) {
654 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700655 nack->SetSenderSsrc(ssrc_);
656 nack->SetMediaSsrc(remote_ssrc_);
657 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200658
659 // Report stats.
Erik Språngf7c57762015-12-04 10:40:35 +0100660 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100661 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200662 }
Erik Språng61be2a42015-04-27 13:32:52 +0200663 packet_type_counter_.nack_requests = nack_stats_.requests();
664 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200665
Erik Språng242e22b2015-05-11 10:17:43 +0200666 ++packet_type_counter_.nack_packets;
Erik Språng242e22b2015-05-11 10:17:43 +0200667
danilchap56036ff2016-03-22 11:14:09 -0700668 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200669}
670
danilchap56036ff2016-03-22 11:14:09 -0700671std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100672 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700673 bye->SetSenderSsrc(ssrc_);
674 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700675
danilchap56036ff2016-03-22 11:14:09 -0700676 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000677}
678
sprang5e38c962016-12-01 05:18:09 -0800679std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildExtendedReports(
Erik Språngf7c57762015-12-04 10:40:35 +0100680 const RtcpContext& ctx) {
sprang5e38c962016-12-01 05:18:09 -0800681 std::unique_ptr<rtcp::ExtendedReports> xr(new rtcp::ExtendedReports());
danilchap822a16f2016-09-27 09:27:47 -0700682 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000683
sprang5e38c962016-12-01 05:18:09 -0800684 if (!sending_ && xr_send_receiver_reference_time_enabled_) {
685 rtcp::Rrtr rrtr;
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200686 rrtr.SetNtp(TimeMicrosToNtp(ctx.now_us_));
sprang5e38c962016-12-01 05:18:09 -0800687 xr->SetRrtr(rrtr);
688 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000689
Mirta Dvornicicb1f063d2018-04-16 11:16:21 +0200690 for (const rtcp::ReceiveTimeInfo& rti : ctx.feedback_state_.last_xr_rtis) {
691 xr->AddDlrrItem(rti);
sprang5e38c962016-12-01 05:18:09 -0800692 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000693
Erik Språng8782a582018-10-04 15:36:06 +0200694 if (send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800695 rtcp::TargetBitrate target_bitrate;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000696
sprang5e38c962016-12-01 05:18:09 -0800697 for (int sl = 0; sl < kMaxSpatialLayers; ++sl) {
698 for (int tl = 0; tl < kMaxTemporalStreams; ++tl) {
Erik Språng8782a582018-10-04 15:36:06 +0200699 if (video_bitrate_allocation_.HasBitrate(sl, tl)) {
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100700 target_bitrate.AddTargetBitrate(
Erik Språng8782a582018-10-04 15:36:06 +0200701 sl, tl, video_bitrate_allocation_.GetBitrate(sl, tl) / 1000);
erikvarga@webrtc.org01f2ec32017-11-15 14:58:23 +0100702 }
sprang5e38c962016-12-01 05:18:09 -0800703 }
704 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000705
sprang5e38c962016-12-01 05:18:09 -0800706 xr->SetTargetBitrate(target_bitrate);
Erik Språng8782a582018-10-04 15:36:06 +0200707 send_video_bitrate_allocation_ = false;
sprang5e38c962016-12-01 05:18:09 -0800708 }
Erik Språngca28fdc2015-08-31 14:00:50 +0200709
sprang5e38c962016-12-01 05:18:09 -0800710 return std::move(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000711}
712
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000713int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200714 RTCPPacketType packetType,
715 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700716 const uint16_t* nack_list) {
Erik Språng242e22b2015-05-11 10:17:43 +0200717 return SendCompoundRTCP(
718 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
nissecd386eb2017-03-14 08:54:43 -0700719 nack_size, nack_list);
Erik Språng242e22b2015-05-11 10:17:43 +0200720}
721
722int32_t RTCPSender::SendCompoundRTCP(
723 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100724 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200725 int32_t nack_size,
nissecd386eb2017-03-14 08:54:43 -0700726 const uint16_t* nack_list) {
terelius429c3452016-01-21 05:42:04 -0800727 PacketContainer container(transport_, event_log_);
nisse6f142eb2017-02-21 07:32:47 -0800728 size_t max_packet_size;
729
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000730 {
danilchap56036ff2016-03-22 11:14:09 -0700731 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700732 if (method_ == RtcpMode::kOff) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100733 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
Erik Språng61be2a42015-04-27 13:32:52 +0200734 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000735 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200736 // Add all flags as volatile. Non volatile entries will not be overwritten.
737 // All new volatile flags added will be consumed by the end of this call.
738 SetFlags(packet_types, true);
739
740 // Prevent sending streams to send SR before any media has been sent.
741 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
742 if (!can_calculate_rtp_timestamp) {
743 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
744 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
745 bool sender_report = consumed_report_flag || consumed_sr_flag;
746 if (sender_report && AllVolatileFlagsConsumed()) {
747 // This call was for Sender Report and nothing else.
748 return 0;
749 }
750 if (sending_ && method_ == RtcpMode::kCompound) {
751 // Not allowed to send any RTCP packet without sender report.
752 return -1;
753 }
754 }
755
756 if (packet_type_counter_.first_packet_time_ms == -1)
757 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100758
759 // We need to send our NTP even if we haven't received any reports.
nissecd386eb2017-03-14 08:54:43 -0700760 RtcpContext context(feedback_state, nack_size, nack_list,
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200761 clock_->TimeInMicroseconds());
Erik Språngf7c57762015-12-04 10:40:35 +0100762
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200763 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100764
danilchap56036ff2016-03-22 11:14:09 -0700765 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800766
Erik Språngf7c57762015-12-04 10:40:35 +0100767 auto it = report_flags_.begin();
768 while (it != report_flags_.end()) {
769 auto builder_it = builders_.find(it->type);
sprang5e38c962016-12-01 05:18:09 -0800770 RTC_DCHECK(builder_it != builders_.end())
771 << "Could not find builder for packet type " << it->type;
Erik Språngf7c57762015-12-04 10:40:35 +0100772 if (it->is_volatile) {
773 report_flags_.erase(it++);
774 } else {
775 ++it;
776 }
777
778 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700779 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Danil Chapovalov59e14642019-09-03 12:41:41 +0200780 if (packet == nullptr)
Erik Språngf7c57762015-12-04 10:40:35 +0100781 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800782 // If there is a BYE, don't append now - save it and append it
783 // at the end later.
784 if (builder_it->first == kRtcpBye) {
785 packet_bye = std::move(packet);
786 } else {
787 container.Append(packet.release());
788 }
789 }
790
791 // Append the BYE now at the end
792 if (packet_bye) {
793 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100794 }
795
796 if (packet_type_counter_observer_ != nullptr) {
797 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
798 remote_ssrc_, packet_type_counter_);
799 }
800
801 RTC_DCHECK(AllVolatileFlagsConsumed());
nisse6f142eb2017-02-21 07:32:47 -0800802 max_packet_size = max_packet_size_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000803 }
Erik Språng61be2a42015-04-27 13:32:52 +0200804
nisse6f142eb2017-02-21 07:32:47 -0800805 size_t bytes_sent = container.SendPackets(max_packet_size);
Erik Språngf7c57762015-12-04 10:40:35 +0100806 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000807}
808
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200809void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200810 bool generate_report;
811 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
812 // Report type already explicitly set, don't automatically populate.
813 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700814 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200815 } else {
816 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700817 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
818 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200819 if (generate_report)
820 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000821 }
822
Erik Språng0ea42d32015-06-25 14:46:16 +0200823 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200824 SetFlag(kRtcpSdes, true);
825
Erik Språng242e22b2015-05-11 10:17:43 +0200826 if (generate_report) {
sprang5e38c962016-12-01 05:18:09 -0800827 if ((!sending_ && xr_send_receiver_reference_time_enabled_) ||
Erik Språng8782a582018-10-04 15:36:06 +0200828 !feedback_state.last_xr_rtis.empty() ||
829 send_video_bitrate_allocation_) {
sprang5e38c962016-12-01 05:18:09 -0800830 SetFlag(kRtcpAnyExtendedReports, true);
831 }
Erik Språng242e22b2015-05-11 10:17:43 +0200832
833 // generate next time to send an RTCP report
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800834 int min_interval_ms = report_interval_ms_;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000835
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800836 if (!audio_ && sending_) {
837 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
838 int send_bitrate_kbit = feedback_state.send_bitrate / 1000;
839 if (send_bitrate_kbit != 0) {
840 min_interval_ms = 360000 / send_bitrate_kbit;
841 min_interval_ms = std::min(min_interval_ms, report_interval_ms_);
Jiawei Ou3587b832018-01-31 22:08:26 -0800842 }
Erik Språng61be2a42015-04-27 13:32:52 +0200843 }
Jiawei Ou3587b832018-01-31 22:08:26 -0800844
danilchap47a740b2015-12-15 00:30:07 -0800845 // The interval between RTCP packets is varied randomly over the
846 // range [1/2,3/2] times the calculated interval.
Jiawei Ou8b5d9d82018-11-15 16:44:37 -0800847 int time_to_next =
848 random_.Rand(min_interval_ms * 1 / 2, min_interval_ms * 3 / 2);
849
850 RTC_DCHECK_GT(time_to_next, 0);
851 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + time_to_next;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000852
danilchap96b69bd2017-07-25 09:15:14 -0700853 // RtcpSender expected to be used for sending either just sender reports
854 // or just receiver reports.
855 RTC_DCHECK(!(IsFlagPresent(kRtcpSr) && IsFlagPresent(kRtcpRr)));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000856 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000857}
858
danilchap96b69bd2017-07-25 09:15:14 -0700859std::vector<rtcp::ReportBlock> RTCPSender::CreateReportBlocks(
860 const FeedbackState& feedback_state) {
861 std::vector<rtcp::ReportBlock> result;
862 if (!receive_statistics_)
863 return result;
danilchapa72e7342015-12-22 08:07:45 -0800864
danilchapf5f793c2017-07-27 04:44:18 -0700865 // TODO(danilchap): Support sending more than |RTCP_MAX_REPORT_BLOCKS| per
866 // compound rtcp packet when single rtcp module is used for multiple media
867 // streams.
868 result = receive_statistics_->RtcpReportBlocks(RTCP_MAX_REPORT_BLOCKS);
danilchap96b69bd2017-07-25 09:15:14 -0700869
870 if (!result.empty() && ((feedback_state.last_rr_ntp_secs != 0) ||
871 (feedback_state.last_rr_ntp_frac != 0))) {
872 // Get our NTP as late as possible to avoid a race.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +0200873 uint32_t now = CompactNtp(TimeMicrosToNtp(clock_->TimeInMicroseconds()));
danilchap96b69bd2017-07-25 09:15:14 -0700874
875 uint32_t receive_time = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
876 receive_time <<= 16;
877 receive_time += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
878
879 uint32_t delay_since_last_sr = now - receive_time;
880 // TODO(danilchap): Instead of setting same value on all report blocks,
881 // set only when media_ssrc match sender ssrc of the sender report
882 // remote times were taken from.
883 for (auto& report_block : result) {
884 report_block.SetLastSr(feedback_state.remote_sr);
885 report_block.SetDelayLastSr(delay_since_last_sr);
886 }
danilchapa72e7342015-12-22 08:07:45 -0800887 }
danilchap96b69bd2017-07-25 09:15:14 -0700888 return result;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889}
890
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000891void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
kwiberg352444f2016-11-28 15:58:53 -0800892 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
danilchap56036ff2016-03-22 11:14:09 -0700893 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000894 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000895}
896
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000897int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
898 uint32_t name,
899 const uint8_t* data,
900 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200901 if (length % 4 != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100902 RTC_LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
Erik Språng61be2a42015-04-27 13:32:52 +0200903 return -1;
904 }
danilchap56036ff2016-03-22 11:14:09 -0700905 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000906
Erik Språng242e22b2015-05-11 10:17:43 +0200907 SetFlag(kRtcpApp, true);
908 app_sub_type_ = subType;
909 app_name_ = name;
910 app_data_.reset(new uint8_t[length]);
911 app_length_ = length;
912 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200913 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000914}
915
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000916void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700917 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200918 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000919}
920
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000921bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700922 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200923 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000924}
925
danilchap853ecb22016-08-22 08:26:15 -0700926void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700927 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700928 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700929 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000930}
Erik Språng61be2a42015-04-27 13:32:52 +0200931
sprang5e38c962016-12-01 05:18:09 -0800932void RTCPSender::SetFlag(uint32_t type, bool is_volatile) {
933 if (type & kRtcpAnyExtendedReports) {
934 report_flags_.insert(ReportFlag(kRtcpAnyExtendedReports, is_volatile));
935 } else {
936 report_flags_.insert(ReportFlag(type, is_volatile));
937 }
Erik Språng242e22b2015-05-11 10:17:43 +0200938}
939
940void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
941 bool is_volatile) {
942 for (RTCPPacketType type : types)
943 SetFlag(type, is_volatile);
944}
945
sprang5e38c962016-12-01 05:18:09 -0800946bool RTCPSender::IsFlagPresent(uint32_t type) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200947 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
948}
949
sprang5e38c962016-12-01 05:18:09 -0800950bool RTCPSender::ConsumeFlag(uint32_t type, bool forced) {
Erik Språng242e22b2015-05-11 10:17:43 +0200951 auto it = report_flags_.find(ReportFlag(type, false));
952 if (it == report_flags_.end())
953 return false;
954 if (it->is_volatile || forced)
955 report_flags_.erase((it));
956 return true;
957}
958
959bool RTCPSender::AllVolatileFlagsConsumed() const {
960 for (const ReportFlag& flag : report_flags_) {
961 if (flag.is_volatile)
962 return false;
963 }
964 return true;
965}
966
Erik Språng566124a2018-04-23 12:32:22 +0200967void RTCPSender::SetVideoBitrateAllocation(
968 const VideoBitrateAllocation& bitrate) {
sprang5e38c962016-12-01 05:18:09 -0800969 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng8782a582018-10-04 15:36:06 +0200970 // Check if this allocation is first ever, or has a different set of
971 // spatial/temporal layers signaled and enabled, if so trigger an rtcp report
972 // as soon as possible.
Erik Språng1cd73912018-10-05 12:57:59 +0200973 absl::optional<VideoBitrateAllocation> new_bitrate =
974 CheckAndUpdateLayerStructure(bitrate);
975 if (new_bitrate) {
976 video_bitrate_allocation_ = *new_bitrate;
Rasmus Brandt71f76b52019-03-27 16:06:30 +0100977 RTC_LOG(LS_INFO) << "Emitting TargetBitrate XR for SSRC " << ssrc_
978 << " with new layers enabled/disabled: "
979 << video_bitrate_allocation_.ToString();
Erik Språng8782a582018-10-04 15:36:06 +0200980 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
Erik Språng1cd73912018-10-05 12:57:59 +0200981 } else {
982 video_bitrate_allocation_ = bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200983 }
984
Erik Språng8782a582018-10-04 15:36:06 +0200985 send_video_bitrate_allocation_ = true;
sprang5e38c962016-12-01 05:18:09 -0800986 SetFlag(kRtcpAnyExtendedReports, true);
987}
988
Erik Språng1cd73912018-10-05 12:57:59 +0200989absl::optional<VideoBitrateAllocation> RTCPSender::CheckAndUpdateLayerStructure(
Erik Språng8782a582018-10-04 15:36:06 +0200990 const VideoBitrateAllocation& bitrate) const {
Erik Språng1cd73912018-10-05 12:57:59 +0200991 absl::optional<VideoBitrateAllocation> updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +0200992 for (size_t si = 0; si < kMaxSpatialLayers; ++si) {
993 for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) {
Erik Språng1cd73912018-10-05 12:57:59 +0200994 if (!updated_bitrate &&
995 (bitrate.HasBitrate(si, ti) !=
996 video_bitrate_allocation_.HasBitrate(si, ti) ||
997 (bitrate.GetBitrate(si, ti) == 0) !=
998 (video_bitrate_allocation_.GetBitrate(si, ti) == 0))) {
999 updated_bitrate = bitrate;
1000 }
1001 if (video_bitrate_allocation_.GetBitrate(si, ti) > 0 &&
1002 bitrate.GetBitrate(si, ti) == 0) {
1003 // Make sure this stream disabling is explicitly signaled.
1004 updated_bitrate->SetBitrate(si, ti, 0);
Erik Språng8782a582018-10-04 15:36:06 +02001005 }
1006 }
1007 }
1008
Erik Språng1cd73912018-10-05 12:57:59 +02001009 return updated_bitrate;
Erik Språng8782a582018-10-04 15:36:06 +02001010}
1011
sprang233bd872015-09-08 13:25:16 -07001012bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
nisse6f142eb2017-02-21 07:32:47 -08001013 size_t max_packet_size;
stefanb77c7162017-02-06 06:29:38 -08001014 {
1015 rtc::CritScope lock(&critical_section_rtcp_sender_);
1016 if (method_ == RtcpMode::kOff)
1017 return false;
nisse6f142eb2017-02-21 07:32:47 -08001018 max_packet_size = max_packet_size_;
stefanb77c7162017-02-06 06:29:38 -08001019 }
1020
nisse6f142eb2017-02-21 07:32:47 -08001021 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
Danil Chapovalov5c3cc412017-12-07 10:15:53 +01001022 bool send_failure = false;
1023 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1024 if (transport_->SendRtcp(packet.data(), packet.size())) {
1025 if (event_log_)
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001026 event_log_->Log(std::make_unique<RtcEventRtcpPacketOutgoing>(packet));
Danil Chapovalov5c3cc412017-12-07 10:15:53 +01001027 } else {
1028 send_failure = true;
1029 }
1030 };
1031 return packet.Build(max_packet_size, callback) && !send_failure;
sprang233bd872015-09-08 13:25:16 -07001032}
1033
Sebastian Janssone1795f42019-07-24 11:38:03 +02001034bool RTCPSender::SendNetworkStateEstimatePacket(
1035 const rtcp::RemoteEstimate& packet) {
1036 size_t max_packet_size;
1037 {
1038 rtc::CritScope lock(&critical_section_rtcp_sender_);
1039 if (method_ == RtcpMode::kOff)
1040 return false;
1041 max_packet_size = max_packet_size_;
1042 }
1043
1044 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1045 bool send_success = false;
1046 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1047 send_success = transport_->SendRtcp(packet.data(), packet.size());
1048 };
1049 return packet.Build(max_packet_size, callback) && send_success;
1050}
1051
Per Kjellander16999812019-10-10 12:57:28 +02001052void RTCPSender::SendCombinedRtcpPacket(
1053 std::vector<std::unique_ptr<rtcp::RtcpPacket>> rtcp_packets) {
1054 size_t max_packet_size;
1055 uint32_t ssrc;
1056 {
1057 rtc::CritScope lock(&critical_section_rtcp_sender_);
1058 if (method_ == RtcpMode::kOff) {
1059 RTC_LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
1060 return;
1061 }
1062
1063 max_packet_size = max_packet_size_;
1064 ssrc = ssrc_;
1065 }
1066 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
1067 auto callback = [&](rtc::ArrayView<const uint8_t> packet) {
1068 if (transport_->SendRtcp(packet.data(), packet.size())) {
1069 if (event_log_)
1070 event_log_->Log(std::make_unique<RtcEventRtcpPacketOutgoing>(packet));
1071 }
1072 };
1073 PacketSender sender(callback, max_packet_size);
1074 for (auto& rtcp_packet : rtcp_packets) {
1075 rtcp_packet->SetSenderSsrc(ssrc);
1076 sender.AppendPacket(*rtcp_packet);
1077 }
1078 sender.Send();
1079}
1080
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001081} // namespace webrtc