blob: 5365d19abb7627d606368b5d6018798b982fc93e [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"
22#include "webrtc/call/rtc_event_log.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000023#include "webrtc/common_types.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),
76 frequency_hz(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000077 packets_sent(0),
78 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000079 send_bitrate(0),
80 last_rr_ntp_secs(0),
81 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000082 remote_sr(0),
Erik Språng61be2a42015-04-27 13:32:52 +020083 has_last_xr_rr(false),
danilchap162abd32015-12-10 02:39:40 -080084 module(nullptr) {}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000085
danilchap2f7dea12016-01-13 02:03:04 -080086class PacketContainer : public rtcp::CompoundPacket,
Erik Språngf7c57762015-12-04 10:40:35 +010087 public rtcp::RtcpPacket::PacketReadyCallback {
88 public:
terelius429c3452016-01-21 05:42:04 -080089 PacketContainer(Transport* transport, RtcEventLog* event_log)
90 : transport_(transport), event_log_(event_log), bytes_sent_(0) {}
Erik Språngf7c57762015-12-04 10:40:35 +010091 virtual ~PacketContainer() {
92 for (RtcpPacket* packet : appended_packets_)
93 delete packet;
94 }
95
96 void OnPacketReady(uint8_t* data, size_t length) override {
terelius429c3452016-01-21 05:42:04 -080097 if (transport_->SendRtcp(data, length)) {
Erik Språngf7c57762015-12-04 10:40:35 +010098 bytes_sent_ += length;
terelius429c3452016-01-21 05:42:04 -080099 if (event_log_) {
100 event_log_->LogRtcpPacket(kOutgoingPacket, MediaType::ANY, data,
101 length);
102 }
103 }
Erik Språngf7c57762015-12-04 10:40:35 +0100104 }
105
danilchap41befce2016-03-30 11:11:51 -0700106 size_t SendPackets(size_t max_payload_length) {
107 RTC_DCHECK_LE(max_payload_length, static_cast<size_t>(IP_PACKET_SIZE));
108 uint8_t buffer[IP_PACKET_SIZE];
109 BuildExternalBuffer(buffer, max_payload_length, this);
Erik Språngf7c57762015-12-04 10:40:35 +0100110 return bytes_sent_;
111 }
112
113 private:
114 Transport* transport_;
terelius429c3452016-01-21 05:42:04 -0800115 RtcEventLog* const event_log_;
Erik Språngf7c57762015-12-04 10:40:35 +0100116 size_t bytes_sent_;
terelius429c3452016-01-21 05:42:04 -0800117
118 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(PacketContainer);
Erik Språngf7c57762015-12-04 10:40:35 +0100119};
120
121class RTCPSender::RtcpContext {
122 public:
Erik Språng242e22b2015-05-11 10:17:43 +0200123 RtcpContext(const FeedbackState& feedback_state,
124 int32_t nack_size,
125 const uint16_t* nack_list,
126 bool repeat,
127 uint64_t picture_id,
Erik Språngf7c57762015-12-04 10:40:35 +0100128 uint32_t ntp_sec,
danilchap56036ff2016-03-22 11:14:09 -0700129 uint32_t ntp_frac)
Erik Språngf7c57762015-12-04 10:40:35 +0100130 : feedback_state_(feedback_state),
131 nack_size_(nack_size),
132 nack_list_(nack_list),
133 repeat_(repeat),
134 picture_id_(picture_id),
135 ntp_sec_(ntp_sec),
danilchap56036ff2016-03-22 11:14:09 -0700136 ntp_frac_(ntp_frac) {}
Erik Språng242e22b2015-05-11 10:17:43 +0200137
Erik Språngf7c57762015-12-04 10:40:35 +0100138 const FeedbackState& feedback_state_;
139 const int32_t nack_size_;
140 const uint16_t* nack_list_;
141 const bool repeat_;
142 const uint64_t picture_id_;
143 const uint32_t ntp_sec_;
144 const uint32_t ntp_frac_;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200145};
146
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000147RTCPSender::RTCPSender(
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000148 bool audio,
149 Clock* clock,
150 ReceiveStatistics* receive_statistics,
sprang86fd9ed2015-09-29 04:45:43 -0700151 RtcpPacketTypeCounterObserver* packet_type_counter_observer,
terelius429c3452016-01-21 05:42:04 -0800152 RtcEventLog* event_log,
sprang86fd9ed2015-09-29 04:45:43 -0700153 Transport* outgoing_transport)
Peter Boströmac547a62015-09-17 23:03:57 +0200154 : audio_(audio),
Erik Språng242e22b2015-05-11 10:17:43 +0200155 clock_(clock),
danilchap47a740b2015-12-15 00:30:07 -0800156 random_(clock_->TimeInMicroseconds()),
pbosda903ea2015-10-02 02:36:56 -0700157 method_(RtcpMode::kOff),
terelius429c3452016-01-21 05:42:04 -0800158 event_log_(event_log),
sprang86fd9ed2015-09-29 04:45:43 -0700159 transport_(outgoing_transport),
Erik Språng242e22b2015-05-11 10:17:43 +0200160 using_nack_(false),
161 sending_(false),
162 remb_enabled_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200163 next_time_to_send_rtcp_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000164 start_timestamp_(0),
165 last_rtp_timestamp_(0),
166 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200167 ssrc_(0),
168 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000169 receive_statistics_(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
Erik Språng242e22b2015-05-11 10:17:43 +0200175 tmmbr_send_(0),
176 packet_oh_send_(0),
danilchap41befce2016-03-30 11:11:51 -0700177 max_payload_length_(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),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000185 packet_type_counter_observer_(packet_type_counter_observer) {
sprang86fd9ed2015-09-29 04:45:43 -0700186 RTC_DCHECK(transport_ != nullptr);
Erik Språng242e22b2015-05-11 10:17:43 +0200187
188 builders_[kRtcpSr] = &RTCPSender::BuildSR;
189 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200190 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200191 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
192 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
193 builders_[kRtcpSli] = &RTCPSender::BuildSLI;
194 builders_[kRtcpRpsi] = &RTCPSender::BuildRPSI;
195 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
196 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
197 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
198 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
199 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
200 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
201 builders_[kRtcpXrVoipMetric] = &RTCPSender::BuildVoIPMetric;
202 builders_[kRtcpXrReceiverReferenceTime] =
203 &RTCPSender::BuildReceiverReferenceTime;
204 builders_[kRtcpXrDlrrReportBlock] = &RTCPSender::BuildDlrr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205}
206
danilchap162abd32015-12-10 02:39:40 -0800207RTCPSender::~RTCPSender() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
pbosda903ea2015-10-02 02:36:56 -0700209RtcpMode RTCPSender::Status() const {
danilchap56036ff2016-03-22 11:14:09 -0700210 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200211 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212}
213
skvlad1c392cc2016-04-01 14:46:44 -0700214void RTCPSender::SetRTCPStatus(RtcpMode new_method) {
danilchap56036ff2016-03-22 11:14:09 -0700215 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000216
skvlad1c392cc2016-04-01 14:46:44 -0700217 if (method_ == RtcpMode::kOff && new_method != RtcpMode::kOff) {
218 // When switching on, reschedule the next packet
219 next_time_to_send_rtcp_ =
Erik Språng242e22b2015-05-11 10:17:43 +0200220 clock_->TimeInMilliseconds() +
221 (audio_ ? RTCP_INTERVAL_AUDIO_MS / 2 : RTCP_INTERVAL_VIDEO_MS / 2);
skvlad1c392cc2016-04-01 14:46:44 -0700222 }
223 method_ = new_method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
Erik Språng61be2a42015-04-27 13:32:52 +0200226bool RTCPSender::Sending() const {
danilchap56036ff2016-03-22 11:14:09 -0700227 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200228 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
Erik Språng61be2a42015-04-27 13:32:52 +0200231int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
232 bool sending) {
233 bool sendRTCPBye = false;
234 {
danilchap56036ff2016-03-22 11:14:09 -0700235 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
pbosda903ea2015-10-02 02:36:56 -0700237 if (method_ != RtcpMode::kOff) {
Erik Språng242e22b2015-05-11 10:17:43 +0200238 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200239 // Trigger RTCP bye
240 sendRTCPBye = true;
241 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 }
Erik Språng242e22b2015-05-11 10:17:43 +0200243 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200244 }
245 if (sendRTCPBye)
246 return SendRTCP(feedback_state, kRtcpBye);
247 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000248}
249
Erik Språng61be2a42015-04-27 13:32:52 +0200250bool RTCPSender::REMB() const {
danilchap56036ff2016-03-22 11:14:09 -0700251 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200252 return remb_enabled_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000253}
254
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000255void RTCPSender::SetREMBStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700256 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200257 remb_enabled_ = enable;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000258}
259
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000260void RTCPSender::SetREMBData(uint32_t bitrate,
261 const std::vector<uint32_t>& ssrcs) {
danilchap56036ff2016-03-22 11:14:09 -0700262 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200263 remb_bitrate_ = bitrate;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000264 remb_ssrcs_ = ssrcs;
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000265
Erik Språng242e22b2015-05-11 10:17:43 +0200266 if (remb_enabled_)
267 SetFlag(kRtcpRemb, false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000268 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
269 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200270 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000271}
272
Erik Språng61be2a42015-04-27 13:32:52 +0200273bool RTCPSender::TMMBR() const {
danilchap56036ff2016-03-22 11:14:09 -0700274 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200275 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276}
277
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000278void RTCPSender::SetTMMBRStatus(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700279 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200280 if (enable) {
281 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
282 } else {
283 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
284 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000285}
286
danilchap41befce2016-03-30 11:11:51 -0700287void RTCPSender::SetMaxPayloadLength(size_t max_payload_length) {
288 max_payload_length_ = max_payload_length;
289}
290
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000291void RTCPSender::SetStartTimestamp(uint32_t start_timestamp) {
danilchap56036ff2016-03-22 11:14:09 -0700292 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000293 start_timestamp_ = start_timestamp;
294}
295
296void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
297 int64_t capture_time_ms) {
danilchap56036ff2016-03-22 11:14:09 -0700298 rtc::CritScope lock(&critical_section_rtcp_sender_);
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000299 last_rtp_timestamp_ = rtp_timestamp;
300 if (capture_time_ms < 0) {
301 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200302 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000303 } else {
304 last_frame_capture_time_ms_ = capture_time_ms;
305 }
306}
307
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000308void RTCPSender::SetSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700309 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000310
Erik Språng242e22b2015-05-11 10:17:43 +0200311 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200312 // not first SetSSRC, probably due to a collision
313 // schedule a new RTCP report
314 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200315 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200316 }
Erik Språng242e22b2015-05-11 10:17:43 +0200317 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000318}
319
Erik Språng61be2a42015-04-27 13:32:52 +0200320void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
danilchap56036ff2016-03-22 11:14:09 -0700321 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200322 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323}
324
Peter Boström9ba52f82015-06-01 14:12:28 +0200325int32_t RTCPSender::SetCNAME(const char* c_name) {
326 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000327 return -1;
328
henrikg91d6ede2015-09-17 00:24:34 -0700329 RTC_DCHECK_LT(strlen(c_name), static_cast<size_t>(RTCP_CNAME_SIZE));
danilchap56036ff2016-03-22 11:14:09 -0700330 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200331 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000332 return 0;
333}
334
Erik Språng0ea42d32015-06-25 14:46:16 +0200335int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
danilchap56036ff2016-03-22 11:14:09 -0700336 RTC_DCHECK(c_name);
henrikg91d6ede2015-09-17 00:24:34 -0700337 RTC_DCHECK_LT(strlen(c_name), static_cast<size_t>(RTCP_CNAME_SIZE));
danilchap56036ff2016-03-22 11:14:09 -0700338 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200339 if (csrc_cnames_.size() >= kRtpCsrcSize)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000340 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200341
342 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000343 return 0;
344}
345
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000346int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
danilchap56036ff2016-03-22 11:14:09 -0700347 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200348 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000349
Erik Språng242e22b2015-05-11 10:17:43 +0200350 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000351 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200352
Erik Språng242e22b2015-05-11 10:17:43 +0200353 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000354 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000355}
356
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000357bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
danilchap162abd32015-12-10 02:39:40 -0800358 /*
359 For audio we use a fix 5 sec interval
niklase@google.com470e71d2011-07-07 08:21:25 +0000360
danilchap162abd32015-12-10 02:39:40 -0800361 For video we use 1 sec interval fo a BW smaller than 360 kbit/s,
362 technicaly we break the max 5% RTCP BW for video below 10 kbit/s but
363 that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000364
365
danilchap162abd32015-12-10 02:39:40 -0800366 From RFC 3550
niklase@google.com470e71d2011-07-07 08:21:25 +0000367
danilchap162abd32015-12-10 02:39:40 -0800368 MAX RTCP BW is 5% if the session BW
369 A send report is approximately 65 bytes inc CNAME
370 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
danilchap162abd32015-12-10 02:39:40 -0800372 The RECOMMENDED value for the reduced minimum in seconds is 360
373 divided by the session bandwidth in kilobits/second. This minimum
374 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
niklase@google.com470e71d2011-07-07 08:21:25 +0000375
danilchap162abd32015-12-10 02:39:40 -0800376 If the participant has not yet sent an RTCP packet (the variable
377 initial is true), the constant Tmin is set to 2.5 seconds, else it
378 is set to 5 seconds.
niklase@google.com470e71d2011-07-07 08:21:25 +0000379
danilchap162abd32015-12-10 02:39:40 -0800380 The interval between RTCP packets is varied randomly over the
381 range [0.5,1.5] times the calculated interval to avoid unintended
382 synchronization of all participants
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
danilchap162abd32015-12-10 02:39:40 -0800384 if we send
385 If the participant is a sender (we_sent true), the constant C is
386 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
387 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
388 number of senders.
niklase@google.com470e71d2011-07-07 08:21:25 +0000389
danilchap162abd32015-12-10 02:39:40 -0800390 if we receive only
391 If we_sent is not true, the constant C is set
392 to the average RTCP packet size divided by 75% of the RTCP
393 bandwidth. The constant n is set to the number of receivers
394 (members - senders). If the number of senders is greater than
395 25%, senders and receivers are treated together.
niklase@google.com470e71d2011-07-07 08:21:25 +0000396
danilchap162abd32015-12-10 02:39:40 -0800397 reconsideration NOT required for peer-to-peer
398 "timer reconsideration" is
399 employed. This algorithm implements a simple back-off mechanism
400 which causes users to hold back RTCP packet transmission if the
401 group sizes are increasing.
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
danilchap162abd32015-12-10 02:39:40 -0800403 n = number of members
404 C = avg_size/(rtcpBW/4)
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
danilchap162abd32015-12-10 02:39:40 -0800406 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
niklase@google.com470e71d2011-07-07 08:21:25 +0000407
danilchap162abd32015-12-10 02:39:40 -0800408 4. The calculated interval T is set to a number uniformly distributed
409 between 0.5 and 1.5 times the deterministic calculated interval.
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
danilchap162abd32015-12-10 02:39:40 -0800411 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
412 for the fact that the timer reconsideration algorithm converges to
413 a value of the RTCP bandwidth below the intended average
414 */
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
Erik Språng242e22b2015-05-11 10:17:43 +0200416 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000417
danilchap56036ff2016-03-22 11:14:09 -0700418 rtc::CritScope lock(&critical_section_rtcp_sender_);
xians@webrtc.org8738d272011-11-25 13:43:53 +0000419
pbosda903ea2015-10-02 02:36:56 -0700420 if (method_ == RtcpMode::kOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000421 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422
Erik Språng242e22b2015-05-11 10:17:43 +0200423 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200424 // for video key-frames we want to send the RTCP before the large key-frame
425 // if we have a 100 ms margin
426 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
427 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000428
Erik Språng242e22b2015-05-11 10:17:43 +0200429 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200430 return true;
431 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200432 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200433 // wrap
434 return true;
435 }
436 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000437}
438
danilchap56036ff2016-03-22 11:14:09 -0700439std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildSR(const RtcpContext& ctx) {
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200440 // Timestamp shouldn't be estimated before first media frame.
441 RTC_DCHECK_GE(last_frame_capture_time_ms_, 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200442 // The timestamp of this RTCP packet should be estimated as the timestamp of
443 // the frame being captured at this moment. We are calculating that
444 // timestamp as the last frame's timestamp + the time since the last frame
445 // was captured.
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200446 uint32_t rtp_timestamp =
447 start_timestamp_ + last_rtp_timestamp_ +
448 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) *
Erik Språngf7c57762015-12-04 10:40:35 +0100449 (ctx.feedback_state_.frequency_hz / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450
Erik Språngf7c57762015-12-04 10:40:35 +0100451 rtcp::SenderReport* report = new rtcp::SenderReport();
452 report->From(ssrc_);
danilchap34ed2b92016-01-18 02:43:32 -0800453 report->WithNtp(NtpTime(ctx.ntp_sec_, ctx.ntp_frac_));
Erik Språngf7c57762015-12-04 10:40:35 +0100454 report->WithRtpTimestamp(rtp_timestamp);
455 report->WithPacketCount(ctx.feedback_state_.packets_sent);
456 report->WithOctetCount(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_)
Erik Språngf7c57762015-12-04 10:40:35 +0100459 report->WithReportBlock(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();
472 sdes->WithCName(ssrc_, cname_);
Erik Språng0ea42d32015-06-25 14:46:16 +0200473
474 for (const auto it : csrc_cnames_)
Erik Språngf7c57762015-12-04 10:40:35 +0100475 sdes->WithCName(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();
482 report->From(ssrc_);
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200483 for (auto it : report_blocks_)
Erik Språngf7c57762015-12-04 10:40:35 +0100484 report->WithReportBlock(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();
492 pli->From(ssrc_);
493 pli->To(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();
509 fir->From(ssrc_);
Danil Chapovalov32e590e2016-01-22 11:04:56 +0100510 fir->WithRequestTo(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();
530 sli->From(ssrc_);
531 sli->To(remote_ssrc_);
sprang0365a272015-08-11 01:02:37 -0700532 // Crop picture id to 6 least significant bits.
Erik Språngf7c57762015-12-04 10:40:35 +0100533 sli->WithPictureId(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();
556 rpsi->From(ssrc_);
557 rpsi->To(remote_ssrc_);
558 rpsi->WithPayloadType(ctx.feedback_state_.send_payload_type);
559 rpsi->WithPictureId(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();
567 remb->From(ssrc_);
sprangdd4edc52015-08-21 04:21:51 -0700568 for (uint32_t ssrc : remb_ssrcs_)
Erik Språngf7c57762015-12-04 10:40:35 +0100569 remb->AppliesTo(ssrc);
570 remb->WithBitrateBps(remb_bitrate_);
Erik Språng61be2a42015-04-27 13:32:52 +0200571
Erik Språng242e22b2015-05-11 10:17:43 +0200572 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
573 "RTCPSender::REMB");
574
danilchap56036ff2016-03-22 11:14:09 -0700575 return std::unique_ptr<rtcp::RtcpPacket>(remb);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000576}
577
Erik Språng61be2a42015-04-27 13:32:52 +0200578void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
danilchap56036ff2016-03-22 11:14:09 -0700579 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200580 tmmbr_send_ = target_bitrate / 1000;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000581}
582
danilchap56036ff2016-03-22 11:14:09 -0700583std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBR(
Erik Språngf7c57762015-12-04 10:40:35 +0100584 const RtcpContext& ctx) {
585 if (ctx.feedback_state_.module == nullptr)
586 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200587 // Before sending the TMMBR check the received TMMBN, only an owner is
588 // allowed to raise the bitrate:
589 // * If the sender is an owner of the TMMBN -> send TMMBR
590 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000591
Erik Språng61be2a42015-04-27 13:32:52 +0200592 // get current bounding set from RTCP receiver
593 bool tmmbrOwner = false;
danilchap2f69ce92016-08-16 03:21:38 -0700594 TMMBRSet candidates;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
Erik Språng242e22b2015-05-11 10:17:43 +0200596 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
597 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200598 // since RTCPreceiver is not doing the reverse we should be fine
599 int32_t lengthOfBoundingSet =
danilchap2f69ce92016-08-16 03:21:38 -0700600 ctx.feedback_state_.module->BoundingSet(&tmmbrOwner, &candidates);
niklase@google.com470e71d2011-07-07 08:21:25 +0000601
Erik Språng61be2a42015-04-27 13:32:52 +0200602 if (lengthOfBoundingSet > 0) {
603 for (int32_t i = 0; i < lengthOfBoundingSet; i++) {
danilchap2f69ce92016-08-16 03:21:38 -0700604 if (candidates.Tmmbr(i) == tmmbr_send_ &&
605 candidates.PacketOH(i) == packet_oh_send_) {
Erik Språngf7c57762015-12-04 10:40:35 +0100606 // Do not send the same tuple.
607 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200608 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000609 }
Erik Språng61be2a42015-04-27 13:32:52 +0200610 if (!tmmbrOwner) {
611 // use received bounding set as candidate set
612 // add current tuple
danilchap2f69ce92016-08-16 03:21:38 -0700613 candidates.SetEntry(lengthOfBoundingSet, tmmbr_send_, packet_oh_send_,
614 ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000615
Erik Språng61be2a42015-04-27 13:32:52 +0200616 // find bounding set
danilchap2f69ce92016-08-16 03:21:38 -0700617 std::vector<rtcp::TmmbItem> bounding =
618 TMMBRHelp::FindBoundingSet(std::move(candidates));
Danil Chapovalovdaa90a72016-08-10 11:29:50 +0200619 tmmbrOwner = TMMBRHelp::IsOwner(bounding, ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200620 if (!tmmbrOwner) {
Erik Språngf7c57762015-12-04 10:40:35 +0100621 // Did not enter bounding set, no meaning to send this request.
622 return nullptr;
Erik Språng61be2a42015-04-27 13:32:52 +0200623 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000624 }
Erik Språng61be2a42015-04-27 13:32:52 +0200625 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000626
Erik Språngf7c57762015-12-04 10:40:35 +0100627 if (!tmmbr_send_)
628 return nullptr;
sprang81a3e602015-08-21 05:30:11 -0700629
Erik Språngf7c57762015-12-04 10:40:35 +0100630 rtcp::Tmmbr* tmmbr = new rtcp::Tmmbr();
631 tmmbr->From(ssrc_);
danilchapf174e3a2016-02-05 04:56:36 -0800632 rtcp::TmmbItem request;
633 request.set_ssrc(remote_ssrc_);
634 request.set_bitrate_bps(tmmbr_send_ * 1000);
635 request.set_packet_overhead(packet_oh_send_);
636 tmmbr->WithTmmbr(request);
Erik Språngf7c57762015-12-04 10:40:35 +0100637
danilchap56036ff2016-03-22 11:14:09 -0700638 return std::unique_ptr<rtcp::RtcpPacket>(tmmbr);
Erik Språng61be2a42015-04-27 13:32:52 +0200639}
640
danilchap56036ff2016-03-22 11:14:09 -0700641std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildTMMBN(
Erik Språngf7c57762015-12-04 10:40:35 +0100642 const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100643 rtcp::Tmmbn* tmmbn = new rtcp::Tmmbn();
644 tmmbn->From(ssrc_);
danilchap6eaa3a42016-05-09 10:59:50 -0700645 for (const rtcp::TmmbItem& tmmbr : tmmbn_to_send_) {
646 if (tmmbr.bitrate_bps() > 0) {
647 tmmbn->WithTmmbr(tmmbr);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000648 }
Erik Språng61be2a42015-04-27 13:32:52 +0200649 }
sprangd83df502015-08-27 01:05:08 -0700650
danilchap56036ff2016-03-22 11:14:09 -0700651 return std::unique_ptr<rtcp::RtcpPacket>(tmmbn);
niklase@google.com470e71d2011-07-07 08:21:25 +0000652}
653
danilchap56036ff2016-03-22 11:14:09 -0700654std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildAPP(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100655 rtcp::App* app = new rtcp::App();
656 app->From(ssrc_);
657 app->WithSubType(app_sub_type_);
658 app->WithName(app_name_);
659 app->WithData(app_data_.get(), app_length_);
Erik Språng521875a2015-09-01 10:11:16 +0200660
danilchap56036ff2016-03-22 11:14:09 -0700661 return std::unique_ptr<rtcp::RtcpPacket>(app);
Erik Språng61be2a42015-04-27 13:32:52 +0200662}
663
danilchap56036ff2016-03-22 11:14:09 -0700664std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildNACK(
Erik Språngf7c57762015-12-04 10:40:35 +0100665 const RtcpContext& ctx) {
666 rtcp::Nack* nack = new rtcp::Nack();
667 nack->From(ssrc_);
668 nack->To(remote_ssrc_);
669 nack->WithList(ctx.nack_list_, ctx.nack_size_);
Erik Språng61be2a42015-04-27 13:32:52 +0200670
671 // Report stats.
672 NACKStringBuilder stringBuilder;
Erik Språngf7c57762015-12-04 10:40:35 +0100673 for (int idx = 0; idx < ctx.nack_size_; ++idx) {
674 stringBuilder.PushNACK(ctx.nack_list_[idx]);
675 nack_stats_.ReportRequest(ctx.nack_list_[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200676 }
Erik Språng61be2a42015-04-27 13:32:52 +0200677 packet_type_counter_.nack_requests = nack_stats_.requests();
678 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200679
680 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
681 "RTCPSender::NACK", "nacks",
682 TRACE_STR_COPY(stringBuilder.GetResult().c_str()));
683 ++packet_type_counter_.nack_packets;
684 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_NACKCount",
685 ssrc_, packet_type_counter_.nack_packets);
686
danilchap56036ff2016-03-22 11:14:09 -0700687 return std::unique_ptr<rtcp::RtcpPacket>(nack);
Erik Språng61be2a42015-04-27 13:32:52 +0200688}
689
danilchap56036ff2016-03-22 11:14:09 -0700690std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildBYE(const RtcpContext& ctx) {
Erik Språngf7c57762015-12-04 10:40:35 +0100691 rtcp::Bye* bye = new rtcp::Bye();
692 bye->From(ssrc_);
sprangd8ee4f92015-08-24 03:25:19 -0700693 for (uint32_t csrc : csrcs_)
Erik Språngf7c57762015-12-04 10:40:35 +0100694 bye->WithCsrc(csrc);
sprangd8ee4f92015-08-24 03:25:19 -0700695
danilchap56036ff2016-03-22 11:14:09 -0700696 return std::unique_ptr<rtcp::RtcpPacket>(bye);
niklase@google.com470e71d2011-07-07 08:21:25 +0000697}
698
danilchap56036ff2016-03-22 11:14:09 -0700699std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildReceiverReferenceTime(
Erik Språngf7c57762015-12-04 10:40:35 +0100700 const RtcpContext& ctx) {
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000701
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100702 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
Erik Språngf7c57762015-12-04 10:40:35 +0100703 xr->From(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000704
Erik Språngca28fdc2015-08-31 14:00:50 +0200705 rtcp::Rrtr rrtr;
Danil Chapovalovfc47ed62015-12-07 14:46:35 +0100706 rrtr.WithNtp(NtpTime(ctx.ntp_sec_, ctx.ntp_frac_));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000707
danilchapde138822016-01-29 11:26:14 -0800708 xr->WithRrtr(rrtr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000709
Erik Språngca28fdc2015-08-31 14:00:50 +0200710 // TODO(sprang): Merge XR report sending to contain all of RRTR, DLRR, VOIP?
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000711
danilchap56036ff2016-03-22 11:14:09 -0700712 return std::unique_ptr<rtcp::RtcpPacket>(xr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000713}
714
danilchap56036ff2016-03-22 11:14:09 -0700715std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildDlrr(
Erik Språngf7c57762015-12-04 10:40:35 +0100716 const RtcpContext& ctx) {
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100717 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
Erik Språngf7c57762015-12-04 10:40:35 +0100718 xr->From(ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000719
Erik Språngca28fdc2015-08-31 14:00:50 +0200720 rtcp::Dlrr dlrr;
Erik Språngf7c57762015-12-04 10:40:35 +0100721 const RtcpReceiveTimeInfo& info = ctx.feedback_state_.last_xr_rr;
Erik Språngca28fdc2015-08-31 14:00:50 +0200722 dlrr.WithDlrrItem(info.sourceSSRC, info.lastRR, info.delaySinceLastRR);
723
danilchapde138822016-01-29 11:26:14 -0800724 xr->WithDlrr(dlrr);
Erik Språngca28fdc2015-08-31 14:00:50 +0200725
danilchap56036ff2016-03-22 11:14:09 -0700726 return std::unique_ptr<rtcp::RtcpPacket>(xr);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000727}
728
Erik Språng242e22b2015-05-11 10:17:43 +0200729// TODO(sprang): Add a unit test for this, or remove if the code isn't used.
danilchap56036ff2016-03-22 11:14:09 -0700730std::unique_ptr<rtcp::RtcpPacket> RTCPSender::BuildVoIPMetric(
Erik Språngf7c57762015-12-04 10:40:35 +0100731 const RtcpContext& context) {
Danil Chapovalov256e5b22016-01-15 14:16:24 +0100732 rtcp::ExtendedReports* xr = new rtcp::ExtendedReports();
Erik Språngf7c57762015-12-04 10:40:35 +0100733 xr->From(ssrc_);
Erik Språngca28fdc2015-08-31 14:00:50 +0200734
735 rtcp::VoipMetric voip;
736 voip.To(remote_ssrc_);
danilchap91941ae2015-12-15 07:06:36 -0800737 voip.WithVoipMetric(xr_voip_metric_);
Erik Språngca28fdc2015-08-31 14:00:50 +0200738
danilchapde138822016-01-29 11:26:14 -0800739 xr->WithVoipMetric(voip);
Erik Språngca28fdc2015-08-31 14:00:50 +0200740
danilchap56036ff2016-03-22 11:14:09 -0700741 return std::unique_ptr<rtcp::RtcpPacket>(xr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000742}
743
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000744int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +0200745 RTCPPacketType packetType,
746 int32_t nack_size,
747 const uint16_t* nack_list,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000748 bool repeat,
749 uint64_t pictureID) {
Erik Språng242e22b2015-05-11 10:17:43 +0200750 return SendCompoundRTCP(
751 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
752 nack_size, nack_list, repeat, pictureID);
753}
754
755int32_t RTCPSender::SendCompoundRTCP(
756 const FeedbackState& feedback_state,
Erik Språngf7c57762015-12-04 10:40:35 +0100757 const std::set<RTCPPacketType>& packet_types,
Erik Språng242e22b2015-05-11 10:17:43 +0200758 int32_t nack_size,
759 const uint16_t* nack_list,
760 bool repeat,
761 uint64_t pictureID) {
terelius429c3452016-01-21 05:42:04 -0800762 PacketContainer container(transport_, event_log_);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000763 {
danilchap56036ff2016-03-22 11:14:09 -0700764 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbosda903ea2015-10-02 02:36:56 -0700765 if (method_ == RtcpMode::kOff) {
Erik Språng61be2a42015-04-27 13:32:52 +0200766 LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
767 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +0000768 }
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200769 // Add all flags as volatile. Non volatile entries will not be overwritten.
770 // All new volatile flags added will be consumed by the end of this call.
771 SetFlags(packet_types, true);
772
773 // Prevent sending streams to send SR before any media has been sent.
774 const bool can_calculate_rtp_timestamp = (last_frame_capture_time_ms_ >= 0);
775 if (!can_calculate_rtp_timestamp) {
776 bool consumed_sr_flag = ConsumeFlag(kRtcpSr);
777 bool consumed_report_flag = sending_ && ConsumeFlag(kRtcpReport);
778 bool sender_report = consumed_report_flag || consumed_sr_flag;
779 if (sender_report && AllVolatileFlagsConsumed()) {
780 // This call was for Sender Report and nothing else.
781 return 0;
782 }
783 if (sending_ && method_ == RtcpMode::kCompound) {
784 // Not allowed to send any RTCP packet without sender report.
785 return -1;
786 }
787 }
788
789 if (packet_type_counter_.first_packet_time_ms == -1)
790 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språngf7c57762015-12-04 10:40:35 +0100791
792 // We need to send our NTP even if we haven't received any reports.
793 uint32_t ntp_sec;
794 uint32_t ntp_frac;
795 clock_->CurrentNtp(ntp_sec, ntp_frac);
796 RtcpContext context(feedback_state, nack_size, nack_list, repeat, pictureID,
danilchap56036ff2016-03-22 11:14:09 -0700797 ntp_sec, ntp_frac);
Erik Språngf7c57762015-12-04 10:40:35 +0100798
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200799 PrepareReport(feedback_state);
Erik Språngf7c57762015-12-04 10:40:35 +0100800
danilchap56036ff2016-03-22 11:14:09 -0700801 std::unique_ptr<rtcp::RtcpPacket> packet_bye;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800802
Erik Språngf7c57762015-12-04 10:40:35 +0100803 auto it = report_flags_.begin();
804 while (it != report_flags_.end()) {
805 auto builder_it = builders_.find(it->type);
806 RTC_DCHECK(builder_it != builders_.end());
807 if (it->is_volatile) {
808 report_flags_.erase(it++);
809 } else {
810 ++it;
811 }
812
813 BuilderFunc func = builder_it->second;
danilchap56036ff2016-03-22 11:14:09 -0700814 std::unique_ptr<rtcp::RtcpPacket> packet = (this->*func)(context);
Erik Språngf7c57762015-12-04 10:40:35 +0100815 if (packet.get() == nullptr)
816 return -1;
aleungbroadsoft0e2e50c2016-02-18 08:33:26 -0800817 // If there is a BYE, don't append now - save it and append it
818 // at the end later.
819 if (builder_it->first == kRtcpBye) {
820 packet_bye = std::move(packet);
821 } else {
822 container.Append(packet.release());
823 }
824 }
825
826 // Append the BYE now at the end
827 if (packet_bye) {
828 container.Append(packet_bye.release());
Erik Språngf7c57762015-12-04 10:40:35 +0100829 }
830
831 if (packet_type_counter_observer_ != nullptr) {
832 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
833 remote_ssrc_, packet_type_counter_);
834 }
835
836 RTC_DCHECK(AllVolatileFlagsConsumed());
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000837 }
Erik Språng61be2a42015-04-27 13:32:52 +0200838
danilchap41befce2016-03-30 11:11:51 -0700839 size_t bytes_sent = container.SendPackets(max_payload_length_);
Erik Språngf7c57762015-12-04 10:40:35 +0100840 return bytes_sent == 0 ? -1 : 0;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000841}
842
Danil Chapovalov70ffead2016-07-20 15:26:59 +0200843void RTCPSender::PrepareReport(const FeedbackState& feedback_state) {
Erik Språng242e22b2015-05-11 10:17:43 +0200844 bool generate_report;
845 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
846 // Report type already explicitly set, don't automatically populate.
847 generate_report = true;
henrikg91d6ede2015-09-17 00:24:34 -0700848 RTC_DCHECK(ConsumeFlag(kRtcpReport) == false);
Erik Språng242e22b2015-05-11 10:17:43 +0200849 } else {
850 generate_report =
pbosda903ea2015-10-02 02:36:56 -0700851 (ConsumeFlag(kRtcpReport) && method_ == RtcpMode::kReducedSize) ||
852 method_ == RtcpMode::kCompound;
Erik Språng242e22b2015-05-11 10:17:43 +0200853 if (generate_report)
854 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +0000855 }
856
Erik Språng0ea42d32015-06-25 14:46:16 +0200857 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +0200858 SetFlag(kRtcpSdes, true);
859
Erik Språng242e22b2015-05-11 10:17:43 +0200860 if (generate_report) {
861 if (!sending_ && xr_send_receiver_reference_time_enabled_)
862 SetFlag(kRtcpXrReceiverReferenceTime, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200863 if (feedback_state.has_last_xr_rr)
Erik Språng242e22b2015-05-11 10:17:43 +0200864 SetFlag(kRtcpXrDlrrReportBlock, true);
865
866 // generate next time to send an RTCP report
danilchap47a740b2015-12-15 00:30:07 -0800867 uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000868
danilchap47a740b2015-12-15 00:30:07 -0800869 if (!audio_) {
Erik Språng242e22b2015-05-11 10:17:43 +0200870 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200871 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
872 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
873 if (send_bitrate_kbit != 0)
874 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000875 }
Erik Språng61be2a42015-04-27 13:32:52 +0200876 if (minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
877 minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
Erik Språng61be2a42015-04-27 13:32:52 +0200878 }
danilchap47a740b2015-12-15 00:30:07 -0800879 // The interval between RTCP packets is varied randomly over the
880 // range [1/2,3/2] times the calculated interval.
881 uint32_t timeToNext =
882 random_.Rand(minIntervalMs * 1 / 2, minIntervalMs * 3 / 2);
Erik Språng242e22b2015-05-11 10:17:43 +0200883 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000884
Per83d09102016-04-15 14:59:13 +0200885 if (receive_statistics_) {
886 StatisticianMap statisticians =
887 receive_statistics_->GetActiveStatisticians();
888 RTC_DCHECK(report_blocks_.empty());
889 for (auto& it : statisticians) {
890 AddReportBlock(feedback_state, it.first, it.second);
891 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000892 }
893 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000894}
895
danilchapa72e7342015-12-22 08:07:45 -0800896bool RTCPSender::AddReportBlock(const FeedbackState& feedback_state,
897 uint32_t ssrc,
898 StreamStatistician* statistician) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000899 // Do we have receive statistics to send?
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000900 RtcpStatistics stats;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000901 if (!statistician->GetStatistics(&stats, true))
902 return false;
danilchapa72e7342015-12-22 08:07:45 -0800903
904 if (report_blocks_.size() >= RTCP_MAX_REPORT_BLOCKS) {
905 LOG(LS_WARNING) << "Too many report blocks.";
906 return false;
907 }
908 RTC_DCHECK(report_blocks_.find(ssrc) == report_blocks_.end());
909 rtcp::ReportBlock* block = &report_blocks_[ssrc];
910 block->To(ssrc);
911 block->WithFractionLost(stats.fraction_lost);
912 if (!block->WithCumulativeLost(stats.cumulative_lost)) {
913 report_blocks_.erase(ssrc);
914 LOG(LS_WARNING) << "Cumulative lost is oversized.";
915 return false;
916 }
917 block->WithExtHighestSeqNum(stats.extended_max_sequence_number);
918 block->WithJitter(stats.jitter);
919 block->WithLastSr(feedback_state.remote_sr);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000920
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200921 // TODO(sprang): Do we really need separate time stamps for each report?
922 // Get our NTP as late as possible to avoid a race.
923 uint32_t ntp_secs;
924 uint32_t ntp_frac;
925 clock_->CurrentNtp(ntp_secs, ntp_frac);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000926
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200927 // Delay since last received report.
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000928 if ((feedback_state.last_rr_ntp_secs != 0) ||
929 (feedback_state.last_rr_ntp_frac != 0)) {
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200930 // Get the 16 lowest bits of seconds and the 16 highest bits of fractions.
931 uint32_t now = ntp_secs & 0x0000FFFF;
Erik Språng61be2a42015-04-27 13:32:52 +0200932 now <<= 16;
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200933 now += (ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000934
Erik Språng61be2a42015-04-27 13:32:52 +0200935 uint32_t receiveTime = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
936 receiveTime <<= 16;
937 receiveTime += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000938
danilchapa72e7342015-12-22 08:07:45 -0800939 block->WithDelayLastSr(now - receiveTime);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000940 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000941 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000942}
943
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000944void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
danilchap56036ff2016-03-22 11:14:09 -0700945 RTC_DCHECK_LE(csrcs.size(), static_cast<size_t>(kRtpCsrcSize));
946 rtc::CritScope lock(&critical_section_rtcp_sender_);
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000947 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000948}
949
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000950int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
951 uint32_t name,
952 const uint8_t* data,
953 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +0200954 if (length % 4 != 0) {
955 LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
956 return -1;
957 }
danilchap56036ff2016-03-22 11:14:09 -0700958 rtc::CritScope lock(&critical_section_rtcp_sender_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000959
Erik Språng242e22b2015-05-11 10:17:43 +0200960 SetFlag(kRtcpApp, true);
961 app_sub_type_ = subType;
962 app_name_ = name;
963 app_data_.reset(new uint8_t[length]);
964 app_length_ = length;
965 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +0200966 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000967}
968
Erik Språng61be2a42015-04-27 13:32:52 +0200969int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
danilchap56036ff2016-03-22 11:14:09 -0700970 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200971 memcpy(&xr_voip_metric_, VoIPMetric, sizeof(RTCPVoIPMetric));
niklase@google.com470e71d2011-07-07 08:21:25 +0000972
Erik Språng242e22b2015-05-11 10:17:43 +0200973 SetFlag(kRtcpXrVoipMetric, true);
Erik Språng61be2a42015-04-27 13:32:52 +0200974 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000975}
976
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000977void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
danilchap56036ff2016-03-22 11:14:09 -0700978 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200979 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000980}
981
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000982bool RTCPSender::RtcpXrReceiverReferenceTime() const {
danilchap56036ff2016-03-22 11:14:09 -0700983 rtc::CritScope lock(&critical_section_rtcp_sender_);
Erik Språng242e22b2015-05-11 10:17:43 +0200984 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +0000985}
986
niklase@google.com470e71d2011-07-07 08:21:25 +0000987// no callbacks allowed inside this function
danilchap6eaa3a42016-05-09 10:59:50 -0700988void RTCPSender::SetTMMBN(const std::vector<rtcp::TmmbItem>* bounding_set) {
danilchap56036ff2016-03-22 11:14:09 -0700989 rtc::CritScope lock(&critical_section_rtcp_sender_);
danilchap6eaa3a42016-05-09 10:59:50 -0700990 if (bounding_set) {
991 tmmbn_to_send_ = *bounding_set;
992 } else {
993 tmmbn_to_send_.clear();
Erik Språng61be2a42015-04-27 13:32:52 +0200994 }
danilchap6eaa3a42016-05-09 10:59:50 -0700995 SetFlag(kRtcpTmmbn, true);
niklase@google.com470e71d2011-07-07 08:21:25 +0000996}
Erik Språng61be2a42015-04-27 13:32:52 +0200997
Erik Språng242e22b2015-05-11 10:17:43 +0200998void RTCPSender::SetFlag(RTCPPacketType type, bool is_volatile) {
999 report_flags_.insert(ReportFlag(type, is_volatile));
1000}
1001
1002void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
1003 bool is_volatile) {
1004 for (RTCPPacketType type : types)
1005 SetFlag(type, is_volatile);
1006}
1007
1008bool RTCPSender::IsFlagPresent(RTCPPacketType type) const {
1009 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
1010}
1011
1012bool RTCPSender::ConsumeFlag(RTCPPacketType type, bool forced) {
1013 auto it = report_flags_.find(ReportFlag(type, false));
1014 if (it == report_flags_.end())
1015 return false;
1016 if (it->is_volatile || forced)
1017 report_flags_.erase((it));
1018 return true;
1019}
1020
1021bool RTCPSender::AllVolatileFlagsConsumed() const {
1022 for (const ReportFlag& flag : report_flags_) {
1023 if (flag.is_volatile)
1024 return false;
1025 }
1026 return true;
1027}
1028
sprang233bd872015-09-08 13:25:16 -07001029bool RTCPSender::SendFeedbackPacket(const rtcp::TransportFeedback& packet) {
sprang233bd872015-09-08 13:25:16 -07001030 class Sender : public rtcp::RtcpPacket::PacketReadyCallback {
1031 public:
terelius429c3452016-01-21 05:42:04 -08001032 Sender(Transport* transport, RtcEventLog* event_log)
1033 : transport_(transport), event_log_(event_log), send_failure_(false) {}
sprang233bd872015-09-08 13:25:16 -07001034
1035 void OnPacketReady(uint8_t* data, size_t length) override {
terelius429c3452016-01-21 05:42:04 -08001036 if (transport_->SendRtcp(data, length)) {
1037 if (event_log_) {
1038 event_log_->LogRtcpPacket(kOutgoingPacket, MediaType::ANY, data,
1039 length);
1040 }
1041 } else {
sprang233bd872015-09-08 13:25:16 -07001042 send_failure_ = true;
terelius429c3452016-01-21 05:42:04 -08001043 }
sprang233bd872015-09-08 13:25:16 -07001044 }
1045
1046 Transport* const transport_;
terelius429c3452016-01-21 05:42:04 -08001047 RtcEventLog* const event_log_;
sprang233bd872015-09-08 13:25:16 -07001048 bool send_failure_;
terelius429c3452016-01-21 05:42:04 -08001049 // TODO(terelius): We would like to
1050 // RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Sender);
1051 // but we can't because of an incorrect warning (C4822) in MVS 2013.
1052 } sender(transport_, event_log_);
sprang233bd872015-09-08 13:25:16 -07001053
danilchap41befce2016-03-30 11:11:51 -07001054 RTC_DCHECK_LE(max_payload_length_, static_cast<size_t>(IP_PACKET_SIZE));
sprang233bd872015-09-08 13:25:16 -07001055 uint8_t buffer[IP_PACKET_SIZE];
danilchap41befce2016-03-30 11:11:51 -07001056 return packet.BuildExternalBuffer(buffer, max_payload_length_, &sender) &&
sprang233bd872015-09-08 13:25:16 -07001057 !sender.send_failure_;
1058}
1059
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001060} // namespace webrtc