blob: 8fe3334789d91ea368f1d16cd77743d0e4f621ee [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
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000011#include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
stefan@webrtc.org9354cc92012-06-07 08:10:14 +000013#include <string.h> // memcpy
niklase@google.com470e71d2011-07-07 08:21:25 +000014
Danil Chapovalov70ffead2016-07-20 15:26:59 +020015#include <utility>
16
Erik Språng61be2a42015-04-27 13:32:52 +020017#include "webrtc/base/checks.h"
kwiberg4485ffb2016-04-26 08:14:39 -070018#include "webrtc/base/constructormagic.h"
Peter Boströmebc0b4e2015-10-28 16:39:33 +010019#include "webrtc/base/logging.h"
tommie4f96502015-10-20 23:00:48 -070020#include "webrtc/base/trace_event.h"
terelius429c3452016-01-21 05:42:04 -080021#include "webrtc/call.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000022#include "webrtc/common_types.h"
skvladcc91d282016-10-03 18:31:22 -070023#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
danilchap0219c9b2015-11-18 05:56:53 -080024#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/app.h"
danilchap50c51362015-11-22 09:03:11 -080025#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/bye.h"
danilchap2f7dea12016-01-13 02:03:04 -080026#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/compound_packet.h"
Danil Chapovalov256e5b22016-01-15 14:16:24 +010027#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/extended_reports.h"
Danil Chapovalov5679da12016-01-15 13:19:53 +010028#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/fir.h"
danilchapa8890a52015-12-22 03:43:04 -080029#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
danilchapf8385ad2015-11-27 05:36:09 -080030#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/pli.h"
Danil Chapovalov97f7e132015-12-04 16:13:30 +010031#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/receiver_report.h"
Danil Chapovalova5eba6c2016-01-15 12:40:15 +010032#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/remb.h"
Danil Chapovalov2c132972016-01-15 15:21:21 +010033#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/rpsi.h"
Danil Chapovalov1567d0b2016-01-15 17:34:27 +010034#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sdes.h"
danilchap34ed2b92016-01-18 02:43:32 -080035#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sender_report.h"
danilchap92e677a2016-01-12 10:04:52 -080036#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/sli.h"
danilchapef3d8052016-01-11 03:31:08 -080037#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbn.h"
danilchap7e8145f2016-01-11 11:49:19 -080038#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/tmmbr.h"
sprang233bd872015-09-08 13:25:16 -070039#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
tommie4f96502015-10-20 23:00:48 -070040#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
danilchap13deaad2016-05-24 13:25:27 -070041#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000042
niklase@google.com470e71d2011-07-07 08:21:25 +000043namespace webrtc {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000044
Erik Språng61be2a42015-04-27 13:32:52 +020045NACKStringBuilder::NACKStringBuilder()
danilchap162abd32015-12-10 02:39:40 -080046 : stream_(""), count_(0), prevNack_(0), consecutive_(false) {}
edjee@google.com79b02892013-04-04 19:43:34 +000047
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000048NACKStringBuilder::~NACKStringBuilder() {}
49
danilchap162abd32015-12-10 02:39:40 -080050void NACKStringBuilder::PushNACK(uint16_t nack) {
Erik Språng242e22b2015-05-11 10:17:43 +020051 if (count_ == 0) {
52 stream_ << nack;
53 } else if (nack == prevNack_ + 1) {
54 consecutive_ = true;
Erik Språng61be2a42015-04-27 13:32:52 +020055 } else {
Erik Språng242e22b2015-05-11 10:17:43 +020056 if (consecutive_) {
57 stream_ << "-" << prevNack_;
58 consecutive_ = false;
edjee@google.com79b02892013-04-04 19:43:34 +000059 }
Erik Språng242e22b2015-05-11 10:17:43 +020060 stream_ << "," << nack;
Erik Språng61be2a42015-04-27 13:32:52 +020061 }
Erik Språng242e22b2015-05-11 10:17:43 +020062 count_++;
63 prevNack_ = nack;
edjee@google.com79b02892013-04-04 19:43:34 +000064}
65
Erik Språng61be2a42015-04-27 13:32:52 +020066std::string NACKStringBuilder::GetResult() {
Erik Språng242e22b2015-05-11 10:17:43 +020067 if (consecutive_) {
68 stream_ << "-" << prevNack_;
69 consecutive_ = false;
Erik Språng61be2a42015-04-27 13:32:52 +020070 }
Erik Språng242e22b2015-05-11 10:17:43 +020071 return stream_.str();
edjee@google.com79b02892013-04-04 19:43:34 +000072}
73
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000074RTCPSender::FeedbackState::FeedbackState()
75 : send_payload_type(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000076 packets_sent(0),
77 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000078 send_bitrate(0),
79 last_rr_ntp_secs(0),
80 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000081 remote_sr(0),
Erik Språng61be2a42015-04-27 13:32:52 +020082 has_last_xr_rr(false),
danilchap162abd32015-12-10 02:39:40 -080083 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000084
danilchap2f7dea12016-01-13 02:03:04 -080085class PacketContainer : public rtcp::CompoundPacket,
Erik Språngf7c57762015-12-04 10:40:35 +010086 public rtcp::RtcpPacket::PacketReadyCallback {
87 public:
terelius429c3452016-01-21 05:42:04 -080088 PacketContainer(Transport* transport, RtcEventLog* event_log)
89 : transport_(transport), event_log_(event_log), bytes_sent_(0) {}
Erik Språngf7c57762015-12-04 10:40:35 +010090 virtual ~PacketContainer() {
91 for (RtcpPacket* packet : appended_packets_)
92 delete packet;
93 }
94
95 void OnPacketReady(uint8_t* data, size_t length) override {
terelius429c3452016-01-21 05:42:04 -080096 if (transport_->SendRtcp(data, length)) {
Erik Språngf7c57762015-12-04 10:40:35 +010097 bytes_sent_ += length;
terelius429c3452016-01-21 05:42:04 -080098 if (event_log_) {
99 event_log_->LogRtcpPacket(kOutgoingPacket, MediaType::ANY, data,
100 length);
101 }
102 }
Erik Språngf7c57762015-12-04 10:40:35 +0100103 }
104
danilchap41befce2016-03-30 11:11:51 -0700105 size_t SendPackets(size_t max_payload_length) {
106 RTC_DCHECK_LE(max_payload_length, static_cast<size_t>(IP_PACKET_SIZE));
107 uint8_t buffer[IP_PACKET_SIZE];
108 BuildExternalBuffer(buffer, max_payload_length, this);
Erik Språngf7c57762015-12-04 10:40:35 +0100109 return bytes_sent_;
110 }
111
112 private:
113 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -0800114 RtcEventLog* const event_log_;
Erik Språngf7c57762015-12-04 10:40:35 +0100115 size_t bytes_sent_;
terelius429c3452016-01-21 05:42:04 -0800116
117 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +0100118};
119
120class RTCPSender::RtcpContext {
121 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200122 RtcpContext(const FeedbackState& feedback_state,
123 int32_t nack_size,
124 const uint16_t* nack_list,
125 bool repeat,
126 uint64_t picture_id,
Erik Språngf7c57762015-12-04 10:40:35 +0100127 uint32_t ntp_sec,
danilchap56036ff2016-03-22 11:14:09 -0700128 uint32_t ntp_frac)
Erik Språngf7c57762015-12-04 10:40:35 +0100129 : feedback_state_(feedback_state),
130 nack_size_(nack_size),
131 nack_list_(nack_list),
132 repeat_(repeat),
133 picture_id_(picture_id),
134 ntp_sec_(ntp_sec),
danilchap56036ff2016-03-22 11:14:09 -0700135 ntp_frac_(ntp_frac) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200136
Erik Språngf7c57762015-12-04 10:40:35 +0100137 const FeedbackState& feedback_state_;
138 const int32_t nack_size_;
139 const uint16_t* nack_list_;
140 const bool repeat_;
141 const uint64_t picture_id_;
142 const uint32_t ntp_sec_;
143 const uint32_t ntp_frac_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200144};
145
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000146RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000147 bool audio,
148 Clock* clock,
149 ReceiveStatistics* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700150 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800151 RtcEventLog* event_log,
sprang86fd9ed2015-09-29 04:45:43 -0700152 Transport* outgoing_transport)
Peter Boströmac547a62015-09-17 23:03:57 +0200153 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200154 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800155 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700156 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800157 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700158 transport_(outgoing_transport),
Erik Språng242e22b2015-05-11 10:17:43 +0200159 using_nack_(false),
160 sending_(false),
161 remb_enabled_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200162 next_time_to_send_rtcp_(0),
danilchap71fead22016-08-18 02:01:49 -0700163 timestamp_offset_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000164 last_rtp_timestamp_(0),
165 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200166 ssrc_(0),
167 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000168 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
Erik Språng242e22b2015-05-11 10:17:43 +0200170 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000171
Erik Språng242e22b2015-05-11 10:17:43 +0200172 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
danilchap2b616392016-08-18 06:17:42 -0700174 tmmbr_send_bps_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200175 packet_oh_send_(0),
danilchap41befce2016-03-30 11:11:51 -0700176 max_payload_length_(IP_PACKET_SIZE - 28), // IPv4 + UDP by default.
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000177
Erik Språng242e22b2015-05-11 10:17:43 +0200178 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200179 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200180 app_data_(nullptr),
181 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
Erik Språng242e22b2015-05-11 10:17:43 +0200183 xr_send_receiver_reference_time_enabled_(false),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000184 packet_type_counter_observer_(packet_type_counter_observer) {
sprang86fd9ed2015-09-29 04:45:43 -0700185 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200186
187 builders_[kRtcpSr] = &RTCPSender::BuildSR;
188 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200189 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200190 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
191 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
192 builders_[kRtcpSli] = &RTCPSender::BuildSLI;
193 builders_[kRtcpRpsi] = &RTCPSender::BuildRPSI;
194 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
195 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
196 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
197 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
198 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
199 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
200 builders_[kRtcpXrVoipMetric] = &RTCPSender::BuildVoIPMetric;
201 builders_[kRtcpXrReceiverReferenceTime] =
202 &RTCPSender::BuildReceiverReferenceTime;
203 builders_[kRtcpXrDlrrReportBlock] = &RTCPSender::BuildDlrr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
danilchap162abd32015-12-10 02:39:40 -0800206RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000207
pbosda903ea2015-10-02 02:36:56 -0700208RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700209 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200210 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000211}
212
skvlad1c392cc2016-04-01 14:46:44 -0700213void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700214 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000215
skvlad1c392cc2016-04-01 14:46:44 -0700216 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
217 // When switching on, reschedule the next packet
218 next_time_to_send_rtcp_ =
Erik Språng242e22b2015-05-11 10:17:43 +0200219 clock_->TimeInMilliseconds() +
220 (audio_ ? RTCP_INTERVAL_AUDIO_MS / 2 : RTCP_INTERVAL_VIDEO_MS / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700221 }
222 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
Erik Språng61be2a42015-04-27 13:32:52 +0200225bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700226 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200227 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228}
229
Erik Språng61be2a42015-04-27 13:32:52 +0200230int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
231 bool sending) {
232 bool sendRTCPBye = false;
233 {
danilchap56036ff2016-03-22 11:14:09 -0700234 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
pbosda903ea2015-10-02 02:36:56 -0700236 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200237 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200238 // Trigger RTCP bye
239 sendRTCPBye = true;
240 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 }
Erik Språng242e22b2015-05-11 10:17:43 +0200242 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200243 }
244 if (sendRTCPBye)
245 return SendRTCP(feedback_state, kRtcpBye);
246 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
Erik Språng61be2a42015-04-27 13:32:52 +0200249bool RTCPSender::REMB() const {
danilchap56036ff2016-03-22 11:14:09 -0700250 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200251 return remb_enabled_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000252}
253
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000254void RTCPSender::SetREMBStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700255 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200256 remb_enabled_ = enable;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000257}
258
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000259void RTCPSender::SetREMBData(uint32_t bitrate,
260 const std::vector<uint32_t>& ssrcs) {
danilchap56036ff2016-03-22 11:14:09 -0700261 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200262 remb_bitrate_ = bitrate;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000263 remb_ssrcs_ = ssrcs;
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000264
Erik Språng242e22b2015-05-11 10:17:43 +0200265 if (remb_enabled_)
266 SetFlag(kRtcpRemb, false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000267 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
268 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200269 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000270}
271
Erik Språng61be2a42015-04-27 13:32:52 +0200272bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700273 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200274 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000275}
276
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000277void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700278 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200279 if (enable) {
280 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
281 } else {
282 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
283 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000284}
285
danilchap41befce2016-03-30 11:11:51 -0700286void RTCPSender::SetMaxPayloadLength(size_t max_payload_length) {
287 max_payload_length_ = max_payload_length;
288}
289
danilchap71fead22016-08-18 02:01:49 -0700290void RTCPSender::SetTimestampOffset(uint32_t timestamp_offset) {
danilchap56036ff2016-03-22 11:14:09 -0700291 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap71fead22016-08-18 02:01:49 -0700292 timestamp_offset_ = timestamp_offset;
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000293}
294
295void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
296 int64_t capture_time_ms) {
danilchap56036ff2016-03-22 11:14:09 -0700297 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000298 last_rtp_timestamp_ = rtp_timestamp;
299 if (capture_time_ms < 0) {
300 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200301 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000302 } else {
303 last_frame_capture_time_ms_ = capture_time_ms;
304 }
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
henrikg91d6ede2015-09-17 00:24:34 -0700328 RTC_DCHECK_LT(strlen(c_name), static_cast<size_t>(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);
henrikg91d6ede2015-09-17 00:24:34 -0700336 RTC_DCHECK_LT(strlen(c_name), static_cast<size_t>(RTCP_CNAME_SIZE));
danilchap56036ff2016-03-22 11:14:09 -0700337 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200338 if (csrc_cnames_.size() >= kRtpCsrcSize)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000339 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200340
341 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000342 return 0;
343}
344
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000345int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700346 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200347 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000348
Erik Språng242e22b2015-05-11 10:17:43 +0200349 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000350 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200351
Erik Språng242e22b2015-05-11 10:17:43 +0200352 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000353 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000354}
355
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000356bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800357 /*
358 For audio we use a fix 5 sec interval
niklase@google.com470e71d2011-07-07 08:21:25 +0000359
danilchap162abd32015-12-10 02:39:40 -0800360 For video we use 1 sec interval fo a BW smaller than 360 kbit/s,
361 technicaly we break the max 5% RTCP BW for video below 10 kbit/s but
362 that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
364
danilchap162abd32015-12-10 02:39:40 -0800365 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
danilchap162abd32015-12-10 02:39:40 -0800367 MAX RTCP BW is 5% if the session BW
368 A send report is approximately 65 bytes inc CNAME
369 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000370
danilchap162abd32015-12-10 02:39:40 -0800371 The RECOMMENDED value for the reduced minimum in seconds is 360
372 divided by the session bandwidth in kilobits/second. This minimum
373 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
danilchap162abd32015-12-10 02:39:40 -0800375 If the participant has not yet sent an RTCP packet (the variable
376 initial is true), the constant Tmin is set to 2.5 seconds, else it
377 is set to 5 seconds.
niklase@google.com470e71d2011-07-07 08:21:25 +0000378
danilchap162abd32015-12-10 02:39:40 -0800379 The interval between RTCP packets is varied randomly over the
380 range [0.5,1.5] times the calculated interval to avoid unintended
381 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
danilchap162abd32015-12-10 02:39:40 -0800383 if we send
384 If the participant is a sender (we_sent true), the constant C is
385 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
386 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
387 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
danilchap162abd32015-12-10 02:39:40 -0800389 if we receive only
390 If we_sent is not true, the constant C is set
391 to the average RTCP packet size divided by 75% of the RTCP
392 bandwidth. The constant n is set to the number of receivers
393 (members - senders). If the number of senders is greater than
394 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000395
danilchap162abd32015-12-10 02:39:40 -0800396 reconsideration NOT required for peer-to-peer
397 "timer reconsideration" is
398 employed. This algorithm implements a simple back-off mechanism
399 which causes users to hold back RTCP packet transmission if the
400 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
danilchap162abd32015-12-10 02:39:40 -0800402 n = number of members
403 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
danilchap162abd32015-12-10 02:39:40 -0800405 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
danilchap162abd32015-12-10 02:39:40 -0800407 4. The calculated interval T is set to a number uniformly distributed
408 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
danilchap162abd32015-12-10 02:39:40 -0800410 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
411 for the fact that the timer reconsideration algorithm converges to
412 a value of the RTCP bandwidth below the intended average
413 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
Erik Språng242e22b2015-05-11 10:17:43 +0200415 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000416
danilchap56036ff2016-03-22 11:14:09 -0700417 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000418
pbosda903ea2015-10-02 02:36:56 -0700419 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
Erik Språng242e22b2015-05-11 10:17:43 +0200422 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200423 // for video key-frames we want to send the RTCP before the large key-frame
424 // if we have a 100 ms margin
425 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
426 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000427
Erik Språng242e22b2015-05-11 10:17:43 +0200428 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200429 return true;
430 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200431 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200432 // wrap
433 return true;
434 }
435 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000436}
437
danilchap56036ff2016-03-22 11:14:09 -0700438std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200439 // Timestamp shouldn't be estimated before first media frame.
440 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200441 // The timestamp of this RTCP packet should be estimated as the timestamp of
442 // the frame being captured at this moment. We are calculating that
443 // timestamp as the last frame's timestamp + the time since the last frame
444 // was captured.
solenbergb19d2882016-10-03 06:22:25 -0700445 uint32_t rtp_rate =
446 (audio_ ? kBogusRtpRateForAudioRtcp : kVideoPayloadTypeFrequency) / 1000;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200447 uint32_t rtp_timestamp =
danilchap71fead22016-08-18 02:01:49 -0700448 timestamp_offset_ + last_rtp_timestamp_ +
solenbergb19d2882016-10-03 06:22:25 -0700449 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) * rtp_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000450
Erik Språngf7c57762015-12-04 10:40:35 +0100451 rtcp::SenderReport* report = new rtcp::SenderReport();
danilchap822a16f2016-09-27 09:27:47 -0700452 report->SetSenderSsrc(ssrc_);
453 report->SetNtp(NtpTime(ctx.ntp_sec_, ctx.ntp_frac_));
454 report->SetRtpTimestamp(rtp_timestamp);
455 report->SetPacketCount(ctx.feedback_state_.packets_sent);
456 report->SetOctetCount(ctx.feedback_state_.media_bytes_sent);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200458 for (auto it : report_blocks_)
danilchap822a16f2016-09-27 09:27:47 -0700459 report->AddReportBlock(it.second);
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200461 report_blocks_.clear();
Erik Språngf7c57762015-12-04 10:40:35 +0100462
danilchap56036ff2016-03-22 11:14:09 -0700463 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000464}
465
danilchap56036ff2016-03-22 11:14:09 -0700466std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSDES(
Erik Språngf7c57762015-12-04 10:40:35 +0100467 const RtcpContext& ctx) {
Erik Språng0ea42d32015-06-25 14:46:16 +0200468 size_t length_cname = cname_.length();
henrikg91d6ede2015-09-17 00:24:34 -0700469 RTC_CHECK_LT(length_cname, static_cast<size_t>(RTCP_CNAME_SIZE));
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
Erik Språngf7c57762015-12-04 10:40:35 +0100471 rtcp::Sdes* sdes = new rtcp::Sdes();
danilchap822a16f2016-09-27 09:27:47 -0700472 sdes->AddCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200473
474 for (const auto it : csrc_cnames_)
danilchap822a16f2016-09-27 09:27:47 -0700475 sdes->AddCName(it.first, it.second);
Erik Språng0ea42d32015-06-25 14:46:16 +0200476
danilchap56036ff2016-03-22 11:14:09 -0700477 return std::unique_ptr<rtcp::RtcpPacket>(sdes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000478}
479
danilchap56036ff2016-03-22 11:14:09 -0700480std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100481 rtcp::ReceiverReport* report = new rtcp::ReceiverReport();
danilchap822a16f2016-09-27 09:27:47 -0700482 report->SetSenderSsrc(ssrc_);
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200483 for (auto it : report_blocks_)
danilchap822a16f2016-09-27 09:27:47 -0700484 report->AddReportBlock(it.second);
Erik Språng61be2a42015-04-27 13:32:52 +0200485
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200486 report_blocks_.clear();
danilchap56036ff2016-03-22 11:14:09 -0700487 return std::unique_ptr<rtcp::RtcpPacket>(report);
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
danilchap56036ff2016-03-22 11:14:09 -0700490std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildPLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100491 rtcp::Pli* pli = new rtcp::Pli();
danilchap822a16f2016-09-27 09:27:47 -0700492 pli->SetSenderSsrc(ssrc_);
493 pli->SetMediaSsrc(remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200494
Erik Språng242e22b2015-05-11 10:17:43 +0200495 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
496 "RTCPSender::PLI");
497 ++packet_type_counter_.pli_packets;
498 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_PLICount",
499 ssrc_, packet_type_counter_.pli_packets);
500
danilchap56036ff2016-03-22 11:14:09 -0700501 return std::unique_ptr<rtcp::RtcpPacket>(pli);
Erik Språng61be2a42015-04-27 13:32:52 +0200502}
503
danilchap56036ff2016-03-22 11:14:09 -0700504std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildFIR(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100505 if (!ctx.repeat_)
sprang62dae192015-08-05 02:35:35 -0700506 ++sequence_number_fir_; // Do not increase if repetition.
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
Erik Språngf7c57762015-12-04 10:40:35 +0100508 rtcp::Fir* fir = new rtcp::Fir();
danilchap822a16f2016-09-27 09:27:47 -0700509 fir->SetSenderSsrc(ssrc_);
510 fir->AddRequestTo(remote_ssrc_, sequence_number_fir_);
Erik Språng242e22b2015-05-11 10:17:43 +0200511
512 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
513 "RTCPSender::FIR");
514 ++packet_type_counter_.fir_packets;
515 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_FIRCount",
516 ssrc_, packet_type_counter_.fir_packets);
517
danilchap56036ff2016-03-22 11:14:09 -0700518 return std::unique_ptr<rtcp::RtcpPacket>(fir);
niklase@google.com470e71d2011-07-07 08:21:25 +0000519}
520
521/*
522 0 1 2 3
523 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
524 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
525 | First | Number | PictureID |
526 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
527*/
danilchap56036ff2016-03-22 11:14:09 -0700528std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSLI(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100529 rtcp::Sli* sli = new rtcp::Sli();
danilchap822a16f2016-09-27 09:27:47 -0700530 sli->SetSenderSsrc(ssrc_);
531 sli->SetMediaSsrc(remote_ssrc_);
sprang0365a272015-08-11 01:02:37 -0700532 // Crop picture id to 6 least significant bits.
danilchap822a16f2016-09-27 09:27:47 -0700533 sli->AddPictureId(ctx.picture_id_ & 0x3F);
sprang0365a272015-08-11 01:02:37 -0700534
danilchap56036ff2016-03-22 11:14:09 -0700535 return std::unique_ptr<rtcp::RtcpPacket>(sli);
niklase@google.com470e71d2011-07-07 08:21:25 +0000536}
537
538/*
539 0 1 2 3
540 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
541 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
542 | PB |0| Payload Type| Native RPSI bit string |
543 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
544 | defined per codec ... | Padding (0) |
545 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
546*/
547/*
548* Note: not generic made for VP8
549*/
danilchap56036ff2016-03-22 11:14:09 -0700550std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildRPSI(
Erik Språngf7c57762015-12-04 10:40:35 +0100551 const RtcpContext& ctx) {
552 if (ctx.feedback_state_.send_payload_type == 0xFF)
553 return nullptr;
Erik Språng242e22b2015-05-11 10:17:43 +0200554
Erik Språngf7c57762015-12-04 10:40:35 +0100555 rtcp::Rpsi* rpsi = new rtcp::Rpsi();
danilchap822a16f2016-09-27 09:27:47 -0700556 rpsi->SetSenderSsrc(ssrc_);
557 rpsi->SetMediaSsrc(remote_ssrc_);
558 rpsi->SetPayloadType(ctx.feedback_state_.send_payload_type);
559 rpsi->SetPictureId(ctx.picture_id_);
sprangcf7f54d2015-08-13 04:37:42 -0700560
danilchap56036ff2016-03-22 11:14:09 -0700561 return std::unique_ptr<rtcp::RtcpPacket>(rpsi);
niklase@google.com470e71d2011-07-07 08:21:25 +0000562}
563
danilchap56036ff2016-03-22 11:14:09 -0700564std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildREMB(
Erik Språngf7c57762015-12-04 10:40:35 +0100565 const RtcpContext& ctx) {
566 rtcp::Remb* remb = new rtcp::Remb();
danilchap822a16f2016-09-27 09:27:47 -0700567 remb->SetSenderSsrc(ssrc_);
568 remb->SetBitrateBps(remb_bitrate_);
569 remb->SetSsrcs(remb_ssrcs_);
Erik Språng61be2a42015-04-27 13:32:52 +0200570
Erik Språng242e22b2015-05-11 10:17:43 +0200571 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
572 "RTCPSender::REMB");
573
danilchap56036ff2016-03-22 11:14:09 -0700574 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000575}
576
Erik Språng61be2a42015-04-27 13:32:52 +0200577void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700578 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap2b616392016-08-18 06:17:42 -0700579 tmmbr_send_bps_ = target_bitrate;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000580}
581
danilchap56036ff2016-03-22 11:14:09 -0700582std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100583 const RtcpContext& ctx) {
584 if (ctx.feedback_state_.module == nullptr)
585 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200586 // Before sending the TMMBR check the received TMMBN, only an owner is
587 // allowed to raise the bitrate:
588 // * If the sender is an owner of the TMMBN -> send TMMBR
589 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
Erik Språng61be2a42015-04-27 13:32:52 +0200591 // get current bounding set from RTCP receiver
danilchap2b616392016-08-18 06:17:42 -0700592 bool tmmbr_owner = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
Erik Språng242e22b2015-05-11 10:17:43 +0200594 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
595 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200596 // since RTCPreceiver is not doing the reverse we should be fine
danilchap2b616392016-08-18 06:17:42 -0700597 std::vector<rtcp::TmmbItem> candidates =
598 ctx.feedback_state_.module->BoundingSet(&tmmbr_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +0000599
danilchap2b616392016-08-18 06:17:42 -0700600 if (!candidates.empty()) {
601 for (const auto& candidate : candidates) {
602 if (candidate.bitrate_bps() == tmmbr_send_bps_ &&
603 candidate.packet_overhead() == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100604 // Do not send the same tuple.
605 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200606 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000607 }
danilchap2b616392016-08-18 06:17:42 -0700608 if (!tmmbr_owner) {
609 // Use received bounding set as candidate set.
610 // Add current tuple.
611 candidates.emplace_back(ssrc_, tmmbr_send_bps_, packet_oh_send_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000612
danilchap2b616392016-08-18 06:17:42 -0700613 // Find bounding set.
danilchap2f69ce92016-08-16 03:21:38 -0700614 std::vector<rtcp::TmmbItem> bounding =
615 TMMBRHelp::FindBoundingSet(std::move(candidates));
danilchap2b616392016-08-18 06:17:42 -0700616 tmmbr_owner = TMMBRHelp::IsOwner(bounding, ssrc_);
617 if (!tmmbr_owner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100618 // Did not enter bounding set, no meaning to send this request.
619 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200620 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000621 }
Erik Språng61be2a42015-04-27 13:32:52 +0200622 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000623
danilchap2b616392016-08-18 06:17:42 -0700624 if (!tmmbr_send_bps_)
Erik Språngf7c57762015-12-04 10:40:35 +0100625 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700626
Erik Språngf7c57762015-12-04 10:40:35 +0100627 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
danilchap822a16f2016-09-27 09:27:47 -0700628 tmmbr->SetSenderSsrc(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800629 rtcp::TmmbItem request;
630 request.set_ssrc(remote_ssrc_);
danilchap2b616392016-08-18 06:17:42 -0700631 request.set_bitrate_bps(tmmbr_send_bps_);
danilchapf174e3a2016-02-05 04:56:36 -0800632 request.set_packet_overhead(packet_oh_send_);
danilchap822a16f2016-09-27 09:27:47 -0700633 tmmbr->AddTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100634
danilchap56036ff2016-03-22 11:14:09 -0700635 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200636}
637
danilchap56036ff2016-03-22 11:14:09 -0700638std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100639 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100640 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
danilchap822a16f2016-09-27 09:27:47 -0700641 tmmbn->SetSenderSsrc(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700642 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
643 if (tmmbr.bitrate_bps() > 0) {
danilchap822a16f2016-09-27 09:27:47 -0700644 tmmbn->AddTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000645 }
Erik Språng61be2a42015-04-27 13:32:52 +0200646 }
sprangd83df502015-08-27 01:05:08 -0700647
danilchap56036ff2016-03-22 11:14:09 -0700648 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000649}
650
danilchap56036ff2016-03-22 11:14:09 -0700651std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100652 rtcp::App* app = new rtcp::App();
danilchap822a16f2016-09-27 09:27:47 -0700653 app->SetSsrc(ssrc_);
654 app->SetSubType(app_sub_type_);
655 app->SetName(app_name_);
656 app->SetData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200657
danilchap56036ff2016-03-22 11:14:09 -0700658 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200659}
660
danilchap56036ff2016-03-22 11:14:09 -0700661std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100662 const RtcpContext& ctx) {
663 rtcp::Nack* nack = new rtcp::Nack();
danilchap822a16f2016-09-27 09:27:47 -0700664 nack->SetSenderSsrc(ssrc_);
665 nack->SetMediaSsrc(remote_ssrc_);
666 nack->SetPacketIds(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200667
668 // Report stats.
669 NACKStringBuilder stringBuilder;
Erik Språngf7c57762015-12-04 10:40:35 +0100670 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
671 stringBuilder.PushNACK(ctx.nack_list_[idx]);
672 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200673 }
Erik Språng61be2a42015-04-27 13:32:52 +0200674 packet_type_counter_.nack_requests = nack_stats_.requests();
675 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200676
677 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
678 "RTCPSender::NACK", "nacks",
679 TRACE_STR_COPY(stringBuilder.GetResult().c_str()));
680 ++packet_type_counter_.nack_packets;
681 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_NACKCount",
682 ssrc_, packet_type_counter_.nack_packets);
683
danilchap56036ff2016-03-22 11:14:09 -0700684 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200685}
686
danilchap56036ff2016-03-22 11:14:09 -0700687std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100688 rtcp::Bye* bye = new rtcp::Bye();
danilchap822a16f2016-09-27 09:27:47 -0700689 bye->SetSenderSsrc(ssrc_);
690 bye->SetCsrcs(csrcs_);
sprangd8ee4f92015-08-24 03:25:19 -0700691
danilchap56036ff2016-03-22 11:14:09 -0700692 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000693}
694
danilchap56036ff2016-03-22 11:14:09 -0700695std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildReceiverReferenceTime(
Erik Språngf7c57762015-12-04 10:40:35 +0100696 const RtcpContext& ctx) {
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000697
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100698 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
danilchap822a16f2016-09-27 09:27:47 -0700699 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000700
Erik Språngca28fdc2015-08-31 14:00:50 +0200701 rtcp::Rrtr rrtr;
danilchap822a16f2016-09-27 09:27:47 -0700702 rrtr.SetNtp(NtpTime(ctx.ntp_sec_, ctx.ntp_frac_));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000703
danilchap822a16f2016-09-27 09:27:47 -0700704 xr->AddRrtr(rrtr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000705
Erik Språngca28fdc2015-08-31 14:00:50 +0200706 // TODO(sprang): Merge XR report sending to contain all of RRTR, DLRR, VOIP?
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000707
danilchap56036ff2016-03-22 11:14:09 -0700708 return std::unique_ptr<rtcp::RtcpPacket>(xr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000709}
710
danilchap56036ff2016-03-22 11:14:09 -0700711std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildDlrr(
Erik Språngf7c57762015-12-04 10:40:35 +0100712 const RtcpContext& ctx) {
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100713 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
danilchap822a16f2016-09-27 09:27:47 -0700714 xr->SetSenderSsrc(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000715
Erik Språngca28fdc2015-08-31 14:00:50 +0200716 rtcp::Dlrr dlrr;
danilchap798896a2016-09-28 02:54:25 -0700717 RTC_DCHECK(ctx.feedback_state_.has_last_xr_rr);
718 dlrr.AddDlrrItem(ctx.feedback_state_.last_xr_rr);
Erik Språngca28fdc2015-08-31 14:00:50 +0200719
danilchap822a16f2016-09-27 09:27:47 -0700720 xr->AddDlrr(dlrr);
Erik Språngca28fdc2015-08-31 14:00:50 +0200721
danilchap56036ff2016-03-22 11:14:09 -0700722 return std::unique_ptr<rtcp::RtcpPacket>(xr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000723}
724
Erik Språng242e22b2015-05-11 10:17:43 +0200725// TODO(sprang): Add a unit test for this, or remove if the code isn't used.
danilchap56036ff2016-03-22 11:14:09 -0700726std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildVoIPMetric(
Erik Språngf7c57762015-12-04 10:40:35 +0100727 const RtcpContext& context) {
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100728 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
danilchap822a16f2016-09-27 09:27:47 -0700729 xr->SetSenderSsrc(ssrc_);
Erik Språngca28fdc2015-08-31 14:00:50 +0200730
731 rtcp::VoipMetric voip;
danilchap822a16f2016-09-27 09:27:47 -0700732 voip.SetMediaSsrc(remote_ssrc_);
733 voip.SetVoipMetric(xr_voip_metric_);
Erik Språngca28fdc2015-08-31 14:00:50 +0200734
danilchap822a16f2016-09-27 09:27:47 -0700735 xr->AddVoipMetric(voip);
Erik Språngca28fdc2015-08-31 14:00:50 +0200736
danilchap56036ff2016-03-22 11:14:09 -0700737 return std::unique_ptr<rtcp::RtcpPacket>(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000738}
739
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000740int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200741 RTCPPacketType packetType,
742 int32_t nack_size,
743 const uint16_t* nack_list,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000744 bool repeat,
745 uint64_t pictureID) {
Erik Språng242e22b2015-05-11 10:17:43 +0200746 return SendCompoundRTCP(
747 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
748 nack_size, nack_list, repeat, pictureID);
749}
750
751int32_t RTCPSender::SendCompoundRTCP(
752 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100753 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200754 int32_t nack_size,
755 const uint16_t* nack_list,
756 bool repeat,
757 uint64_t pictureID) {
terelius429c3452016-01-21 05:42:04 -0800758 PacketContainer container(transport_, event_log_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000759 {
danilchap56036ff2016-03-22 11:14:09 -0700760 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700761 if (method_ == RtcpMode::kOff) {
Erik Språng61be2a42015-04-27 13:32:52 +0200762 LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
763 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000764 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200765 // Add all flags as volatile. Non volatile entries will not be overwritten.
766 // All new volatile flags added will be consumed by the end of this call.
767 SetFlags(packet_types, true);
768
769 // Prevent sending streams to send SR before any media has been sent.
770 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
771 if (!can_calculate_rtp_timestamp) {
772 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
773 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
774 bool sender_report = consumed_report_flag || consumed_sr_flag;
775 if (sender_report && AllVolatileFlagsConsumed()) {
776 // This call was for Sender Report and nothing else.
777 return 0;
778 }
779 if (sending_ && method_ == RtcpMode::kCompound) {
780 // Not allowed to send any RTCP packet without sender report.
781 return -1;
782 }
783 }
784
785 if (packet_type_counter_.first_packet_time_ms == -1)
786 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100787
788 // We need to send our NTP even if we haven't received any reports.
789 uint32_t ntp_sec;
790 uint32_t ntp_frac;
791 clock_->CurrentNtp(ntp_sec, ntp_frac);
792 RtcpContext context(feedback_state, nack_size, nack_list, repeat, pictureID,
danilchap56036ff2016-03-22 11:14:09 -0700793 ntp_sec, ntp_frac);
Erik Språngf7c57762015-12-04 10:40:35 +0100794
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200795 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100796
danilchap56036ff2016-03-22 11:14:09 -0700797 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800798
Erik Språngf7c57762015-12-04 10:40:35 +0100799 auto it = report_flags_.begin();
800 while (it != report_flags_.end()) {
801 auto builder_it = builders_.find(it->type);
802 RTC_DCHECK(builder_it != builders_.end());
803 if (it->is_volatile) {
804 report_flags_.erase(it++);
805 } else {
806 ++it;
807 }
808
809 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700810 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100811 if (packet.get() == nullptr)
812 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800813 // If there is a BYE, don't append now - save it and append it
814 // at the end later.
815 if (builder_it->first == kRtcpBye) {
816 packet_bye = std::move(packet);
817 } else {
818 container.Append(packet.release());
819 }
820 }
821
822 // Append the BYE now at the end
823 if (packet_bye) {
824 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100825 }
826
827 if (packet_type_counter_observer_ != nullptr) {
828 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
829 remote_ssrc_, packet_type_counter_);
830 }
831
832 RTC_DCHECK(AllVolatileFlagsConsumed());
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000833 }
Erik Språng61be2a42015-04-27 13:32:52 +0200834
danilchap41befce2016-03-30 11:11:51 -0700835 size_t bytes_sent = container.SendPackets(max_payload_length_);
Erik Språngf7c57762015-12-04 10:40:35 +0100836 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000837}
838
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200839void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200840 bool generate_report;
841 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
842 // Report type already explicitly set, don't automatically populate.
843 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700844 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200845 } else {
846 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700847 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
848 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200849 if (generate_report)
850 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000851 }
852
Erik Språng0ea42d32015-06-25 14:46:16 +0200853 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200854 SetFlag(kRtcpSdes, true);
855
Erik Språng242e22b2015-05-11 10:17:43 +0200856 if (generate_report) {
857 if (!sending_ && xr_send_receiver_reference_time_enabled_)
858 SetFlag(kRtcpXrReceiverReferenceTime, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200859 if (feedback_state.has_last_xr_rr)
Erik Språng242e22b2015-05-11 10:17:43 +0200860 SetFlag(kRtcpXrDlrrReportBlock, true);
861
862 // generate next time to send an RTCP report
danilchap47a740b2015-12-15 00:30:07 -0800863 uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000864
danilchap47a740b2015-12-15 00:30:07 -0800865 if (!audio_) {
Erik Språng242e22b2015-05-11 10:17:43 +0200866 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200867 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
868 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
869 if (send_bitrate_kbit != 0)
870 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000871 }
Erik Språng61be2a42015-04-27 13:32:52 +0200872 if (minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
873 minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
Erik Språng61be2a42015-04-27 13:32:52 +0200874 }
danilchap47a740b2015-12-15 00:30:07 -0800875 // The interval between RTCP packets is varied randomly over the
876 // range [1/2,3/2] times the calculated interval.
877 uint32_t timeToNext =
878 random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
Erik Språng242e22b2015-05-11 10:17:43 +0200879 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000880
Per83d09102016-04-15 14:59:13 +0200881 if (receive_statistics_) {
882 StatisticianMap statisticians =
883 receive_statistics_->GetActiveStatisticians();
884 RTC_DCHECK(report_blocks_.empty());
885 for (auto& it : statisticians) {
886 AddReportBlock(feedback_state, it.first, it.second);
887 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000888 }
889 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000890}
891
danilchapa72e7342015-12-22 08:07:45 -0800892bool RTCPSender::AddReportBlock(const FeedbackState& feedback_state,
893 uint32_t ssrc,
894 StreamStatistician* statistician) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000895 // Do we have receive statistics to send?
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000896 RtcpStatistics stats;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000897 if (!statistician->GetStatistics(&stats, true))
898 return false;
danilchapa72e7342015-12-22 08:07:45 -0800899
900 if (report_blocks_.size() >= RTCP_MAX_REPORT_BLOCKS) {
901 LOG(LS_WARNING) << "Too many report blocks.";
902 return false;
903 }
904 RTC_DCHECK(report_blocks_.find(ssrc) == report_blocks_.end());
905 rtcp::ReportBlock* block = &report_blocks_[ssrc];
danilchap822a16f2016-09-27 09:27:47 -0700906 block->SetMediaSsrc(ssrc);
907 block->SetFractionLost(stats.fraction_lost);
908 if (!block->SetCumulativeLost(stats.cumulative_lost)) {
danilchapa72e7342015-12-22 08:07:45 -0800909 report_blocks_.erase(ssrc);
910 LOG(LS_WARNING) << "Cumulative lost is oversized.";
911 return false;
912 }
danilchap822a16f2016-09-27 09:27:47 -0700913 block->SetExtHighestSeqNum(stats.extended_max_sequence_number);
914 block->SetJitter(stats.jitter);
915 block->SetLastSr(feedback_state.remote_sr);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000916
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200917 // TODO(sprang): Do we really need separate time stamps for each report?
918 // Get our NTP as late as possible to avoid a race.
919 uint32_t ntp_secs;
920 uint32_t ntp_frac;
921 clock_->CurrentNtp(ntp_secs, ntp_frac);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000922
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200923 // Delay since last received report.
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000924 if ((feedback_state.last_rr_ntp_secs != 0) ||
925 (feedback_state.last_rr_ntp_frac != 0)) {
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200926 // Get the 16 lowest bits of seconds and the 16 highest bits of fractions.
927 uint32_t now = ntp_secs & 0x0000FFFF;
Erik Språng61be2a42015-04-27 13:32:52 +0200928 now <<= 16;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200929 now += (ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000930
Erik Språng61be2a42015-04-27 13:32:52 +0200931 uint32_t receiveTime = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
932 receiveTime <<= 16;
933 receiveTime += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000934
danilchap822a16f2016-09-27 09:27:47 -0700935 block->SetDelayLastSr(now - receiveTime);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000936 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000937 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000938}
939
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000940void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
danilchap56036ff2016-03-22 11:14:09 -0700941 RTC_DCHECK_LE(csrcs.size(), static_cast<size_t>(kRtpCsrcSize));
942 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000943 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000944}
945
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000946int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
947 uint32_t name,
948 const uint8_t* data,
949 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200950 if (length % 4 != 0) {
951 LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
952 return -1;
953 }
danilchap56036ff2016-03-22 11:14:09 -0700954 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
Erik Språng242e22b2015-05-11 10:17:43 +0200956 SetFlag(kRtcpApp, true);
957 app_sub_type_ = subType;
958 app_name_ = name;
959 app_data_.reset(new uint8_t[length]);
960 app_length_ = length;
961 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200962 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000963}
964
Erik Språng61be2a42015-04-27 13:32:52 +0200965int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
danilchap56036ff2016-03-22 11:14:09 -0700966 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200967 memcpy(&xr_voip_metric_, VoIPMetric, sizeof(RTCPVoIPMetric));
niklase@google.com470e71d2011-07-07 08:21:25 +0000968
Erik Språng242e22b2015-05-11 10:17:43 +0200969 SetFlag(kRtcpXrVoipMetric, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200970 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000971}
972
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000973void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700974 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200975 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000976}
977
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000978bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700979 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200980 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000981}
982
danilchap853ecb22016-08-22 08:26:15 -0700983void RTCPSender::SetTmmbn(std::vector<rtcp::TmmbItem> bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700984 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap853ecb22016-08-22 08:26:15 -0700985 tmmbn_to_send_ = std::move(bounding_set);
danilchap6eaa3a42016-05-09 10:59:50 -0700986 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000987}
Erik Språng61be2a42015-04-27 13:32:52 +0200988
Erik Språng242e22b2015-05-11 10:17:43 +0200989void RTCPSender::SetFlag(RTCPPacketType type, bool is_volatile) {
990 report_flags_.insert(ReportFlag(type, is_volatile));
991}
992
993void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
994 bool is_volatile) {
995 for (RTCPPacketType type : types)
996 SetFlag(type, is_volatile);
997}
998
999bool RTCPSender::IsFlagPresent(RTCPPacketType type) const {
1000 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
1001}
1002
1003bool RTCPSender::ConsumeFlag(RTCPPacketType type, bool forced) {
1004 auto it = report_flags_.find(ReportFlag(type, false));
1005 if (it == report_flags_.end())
1006 return false;
1007 if (it->is_volatile || forced)
1008 report_flags_.erase((it));
1009 return true;
1010}
1011
1012bool RTCPSender::AllVolatileFlagsConsumed() const {
1013 for (const ReportFlag& flag : report_flags_) {
1014 if (flag.is_volatile)
1015 return false;
1016 }
1017 return true;
1018}
1019
sprang233bd872015-09-08 13:25:16 -07001020bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
sprang233bd872015-09-08 13:25:16 -07001021 class Sender : public rtcp::RtcpPacket::PacketReadyCallback {
1022 public:
terelius429c3452016-01-21 05:42:04 -08001023 Sender(Transport* transport, RtcEventLog* event_log)
1024 : transport_(transport), event_log_(event_log), send_failure_(false) {}
sprang233bd872015-09-08 13:25:16 -07001025
1026 void OnPacketReady(uint8_t* data, size_t length) override {
terelius429c3452016-01-21 05:42:04 -08001027 if (transport_->SendRtcp(data, length)) {
1028 if (event_log_) {
1029 event_log_->LogRtcpPacket(kOutgoingPacket, MediaType::ANY, data,
1030 length);
1031 }
1032 } else {
sprang233bd872015-09-08 13:25:16 -07001033 send_failure_ = true;
terelius429c3452016-01-21 05:42:04 -08001034 }
sprang233bd872015-09-08 13:25:16 -07001035 }
1036
1037 Transport* const transport_;
terelius429c3452016-01-21 05:42:04 -08001038 RtcEventLog* const event_log_;
sprang233bd872015-09-08 13:25:16 -07001039 bool send_failure_;
terelius429c3452016-01-21 05:42:04 -08001040 // TODO(terelius): We would like to
1041 // RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sender);
1042 // but we can't because of an incorrect warning (C4822) in MVS 2013.
1043 } sender(transport_, event_log_);
sprang233bd872015-09-08 13:25:16 -07001044
danilchap41befce2016-03-30 11:11:51 -07001045 RTC_DCHECK_LE(max_payload_length_, static_cast<size_t>(IP_PACKET_SIZE));
sprang233bd872015-09-08 13:25:16 -07001046 uint8_t buffer[IP_PACKET_SIZE];
danilchap41befce2016-03-30 11:11:51 -07001047 return packet.BuildExternalBuffer(buffer, max_payload_length_, &sender) &&
sprang233bd872015-09-08 13:25:16 -07001048 !sender.send_failure_;
1049}
1050
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001051} // namespace webrtc