blob: 066b179c6d469e84423d5043e9c834146f14a80b [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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h> // assert
14#include <stdlib.h> // rand
stefan@webrtc.org9354cc92012-06-07 08:10:14 +000015#include <string.h> // memcpy
niklase@google.com470e71d2011-07-07 08:21:25 +000016
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000017#include <algorithm> // min
Erik Språng61be2a42015-04-27 13:32:52 +020018#include <limits> // max
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000019
Erik Språng61be2a42015-04-27 13:32:52 +020020#include "webrtc/base/checks.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000021#include "webrtc/common_types.h"
sprang@webrtc.org779c3d12015-03-17 16:42:49 +000022#include "webrtc/modules/rtp_rtcp/source/byte_io.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000023#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
24#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +000025#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000026#include "webrtc/system_wrappers/interface/trace_event.h"
pwestin@webrtc.org741da942011-09-20 13:52:04 +000027
niklase@google.com470e71d2011-07-07 08:21:25 +000028namespace webrtc {
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000029
30using RTCPUtility::RTCPCnameInformation;
31
Erik Språng61be2a42015-04-27 13:32:52 +020032NACKStringBuilder::NACKStringBuilder()
Erik Språng242e22b2015-05-11 10:17:43 +020033 : stream_(""), count_(0), prevNack_(0), consecutive_(false) {
edjee@google.com79b02892013-04-04 19:43:34 +000034}
35
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000036NACKStringBuilder::~NACKStringBuilder() {}
37
pbos@webrtc.org2f446732013-04-08 11:08:41 +000038void NACKStringBuilder::PushNACK(uint16_t nack)
edjee@google.com79b02892013-04-04 19:43:34 +000039{
Erik Språng242e22b2015-05-11 10:17:43 +020040 if (count_ == 0) {
41 stream_ << nack;
42 } else if (nack == prevNack_ + 1) {
43 consecutive_ = true;
Erik Språng61be2a42015-04-27 13:32:52 +020044 } else {
Erik Språng242e22b2015-05-11 10:17:43 +020045 if (consecutive_) {
46 stream_ << "-" << prevNack_;
47 consecutive_ = false;
edjee@google.com79b02892013-04-04 19:43:34 +000048 }
Erik Språng242e22b2015-05-11 10:17:43 +020049 stream_ << "," << nack;
Erik Språng61be2a42015-04-27 13:32:52 +020050 }
Erik Språng242e22b2015-05-11 10:17:43 +020051 count_++;
52 prevNack_ = nack;
edjee@google.com79b02892013-04-04 19:43:34 +000053}
54
Erik Språng61be2a42015-04-27 13:32:52 +020055std::string NACKStringBuilder::GetResult() {
Erik Språng242e22b2015-05-11 10:17:43 +020056 if (consecutive_) {
57 stream_ << "-" << prevNack_;
58 consecutive_ = false;
Erik Språng61be2a42015-04-27 13:32:52 +020059 }
Erik Språng242e22b2015-05-11 10:17:43 +020060 return stream_.str();
edjee@google.com79b02892013-04-04 19:43:34 +000061}
62
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000063RTCPSender::FeedbackState::FeedbackState()
64 : send_payload_type(0),
65 frequency_hz(0),
pbos@webrtc.org2f4b14e2014-07-15 15:25:39 +000066 packets_sent(0),
67 media_bytes_sent(0),
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000068 send_bitrate(0),
69 last_rr_ntp_secs(0),
70 last_rr_ntp_frac(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +000071 remote_sr(0),
Erik Språng61be2a42015-04-27 13:32:52 +020072 has_last_xr_rr(false),
73 module(nullptr) {
74}
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000075
Erik Språng242e22b2015-05-11 10:17:43 +020076struct RTCPSender::RtcpContext {
77 RtcpContext(const FeedbackState& feedback_state,
78 int32_t nack_size,
79 const uint16_t* nack_list,
80 bool repeat,
81 uint64_t picture_id,
82 uint8_t* buffer,
83 uint32_t buffer_size)
84 : feedback_state(feedback_state),
85 nack_size(nack_size),
86 nack_list(nack_list),
87 repeat(repeat),
88 picture_id(picture_id),
89 buffer(buffer),
90 buffer_size(buffer_size),
91 ntp_sec(0),
92 ntp_frac(0),
Erik Språng242e22b2015-05-11 10:17:43 +020093 position(0) {}
94
95 uint8_t* AllocateData(uint32_t bytes) {
96 DCHECK_LE(position + bytes, buffer_size);
97 uint8_t* ptr = &buffer[position];
98 position += bytes;
99 return ptr;
100 }
101
102 const FeedbackState& feedback_state;
103 int32_t nack_size;
104 const uint16_t* nack_list;
105 bool repeat;
106 uint64_t picture_id;
107 uint8_t* buffer;
108 uint32_t buffer_size;
109 uint32_t ntp_sec;
110 uint32_t ntp_frac;
Erik Språng242e22b2015-05-11 10:17:43 +0200111 uint32_t position;
112};
113
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200114// TODO(sprang): Once all builders use RtcpPacket, call SendToNetwork() here.
115class RTCPSender::PacketBuiltCallback
116 : public rtcp::RtcpPacket::PacketReadyCallback {
117 public:
118 PacketBuiltCallback(RtcpContext* context) : context_(context) {}
119 virtual ~PacketBuiltCallback() {}
120 void OnPacketReady(uint8_t* data, size_t length) override {
121 context_->position += length;
122 }
Erik Språng72aa9a62015-07-31 16:16:02 +0200123 bool BuildPacket(const rtcp::RtcpPacket& packet) {
124 return packet.BuildExternalBuffer(
125 &context_->buffer[context_->position],
126 context_->buffer_size - context_->position, this);
127 }
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200128
129 private:
130 RtcpContext* const context_;
131};
132
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000133RTCPSender::RTCPSender(
134 int32_t id,
135 bool audio,
136 Clock* clock,
137 ReceiveStatistics* receive_statistics,
138 RtcpPacketTypeCounterObserver* packet_type_counter_observer)
Erik Språng242e22b2015-05-11 10:17:43 +0200139 : id_(id),
140 audio_(audio),
141 clock_(clock),
142 method_(kRtcpOff),
143 critical_section_transport_(
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000144 CriticalSectionWrapper::CreateCriticalSection()),
Erik Språng242e22b2015-05-11 10:17:43 +0200145 cbTransport_(nullptr),
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
Erik Språng242e22b2015-05-11 10:17:43 +0200147 critical_section_rtcp_sender_(
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000148 CriticalSectionWrapper::CreateCriticalSection()),
Erik Språng242e22b2015-05-11 10:17:43 +0200149 using_nack_(false),
150 sending_(false),
151 remb_enabled_(false),
Erik Språng242e22b2015-05-11 10:17:43 +0200152 next_time_to_send_rtcp_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000153 start_timestamp_(0),
154 last_rtp_timestamp_(0),
155 last_frame_capture_time_ms_(-1),
Erik Språng242e22b2015-05-11 10:17:43 +0200156 ssrc_(0),
157 remote_ssrc_(0),
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000158 receive_statistics_(receive_statistics),
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
Erik Språng242e22b2015-05-11 10:17:43 +0200160 sequence_number_fir_(0),
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000161
Erik Språng242e22b2015-05-11 10:17:43 +0200162 remb_bitrate_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000163
Erik Språng242e22b2015-05-11 10:17:43 +0200164 tmmbr_help_(),
165 tmmbr_send_(0),
166 packet_oh_send_(0),
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000167
Erik Språng242e22b2015-05-11 10:17:43 +0200168 app_sub_type_(0),
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200169 app_name_(0),
Erik Språng242e22b2015-05-11 10:17:43 +0200170 app_data_(nullptr),
171 app_length_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
Erik Språng242e22b2015-05-11 10:17:43 +0200173 xr_send_receiver_reference_time_enabled_(false),
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +0000174 packet_type_counter_observer_(packet_type_counter_observer) {
Erik Språng242e22b2015-05-11 10:17:43 +0200175 memset(last_send_report_, 0, sizeof(last_send_report_));
176 memset(last_rtcp_time_, 0, sizeof(last_rtcp_time_));
177
178 builders_[kRtcpSr] = &RTCPSender::BuildSR;
179 builders_[kRtcpRr] = &RTCPSender::BuildRR;
Erik Språng0ea42d32015-06-25 14:46:16 +0200180 builders_[kRtcpSdes] = &RTCPSender::BuildSDES;
Erik Språng242e22b2015-05-11 10:17:43 +0200181 builders_[kRtcpPli] = &RTCPSender::BuildPLI;
182 builders_[kRtcpFir] = &RTCPSender::BuildFIR;
183 builders_[kRtcpSli] = &RTCPSender::BuildSLI;
184 builders_[kRtcpRpsi] = &RTCPSender::BuildRPSI;
185 builders_[kRtcpRemb] = &RTCPSender::BuildREMB;
186 builders_[kRtcpBye] = &RTCPSender::BuildBYE;
187 builders_[kRtcpApp] = &RTCPSender::BuildAPP;
188 builders_[kRtcpTmmbr] = &RTCPSender::BuildTMMBR;
189 builders_[kRtcpTmmbn] = &RTCPSender::BuildTMMBN;
190 builders_[kRtcpNack] = &RTCPSender::BuildNACK;
191 builders_[kRtcpXrVoipMetric] = &RTCPSender::BuildVoIPMetric;
192 builders_[kRtcpXrReceiverReferenceTime] =
193 &RTCPSender::BuildReceiverReferenceTime;
194 builders_[kRtcpXrDlrrReportBlock] = &RTCPSender::BuildDlrr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000197RTCPSender::~RTCPSender() {
niklase@google.com470e71d2011-07-07 08:21:25 +0000198}
199
Erik Språng61be2a42015-04-27 13:32:52 +0200200int32_t RTCPSender::RegisterSendTransport(Transport* outgoingTransport) {
Erik Språng242e22b2015-05-11 10:17:43 +0200201 CriticalSectionScoped lock(critical_section_transport_.get());
202 cbTransport_ = outgoingTransport;
Erik Språng61be2a42015-04-27 13:32:52 +0200203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000204}
205
Erik Språng61be2a42015-04-27 13:32:52 +0200206RTCPMethod RTCPSender::Status() const {
Erik Språng242e22b2015-05-11 10:17:43 +0200207 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
208 return method_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000211void RTCPSender::SetRTCPStatus(RTCPMethod method) {
Erik Språng242e22b2015-05-11 10:17:43 +0200212 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
213 method_ = method;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000214
215 if (method == kRtcpOff)
216 return;
Erik Språng242e22b2015-05-11 10:17:43 +0200217 next_time_to_send_rtcp_ =
218 clock_->TimeInMilliseconds() +
219 (audio_ ? RTCP_INTERVAL_AUDIO_MS / 2 : RTCP_INTERVAL_VIDEO_MS / 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220}
221
Erik Språng61be2a42015-04-27 13:32:52 +0200222bool RTCPSender::Sending() const {
Erik Språng242e22b2015-05-11 10:17:43 +0200223 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
224 return sending_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000225}
226
Erik Språng61be2a42015-04-27 13:32:52 +0200227int32_t RTCPSender::SetSendingStatus(const FeedbackState& feedback_state,
228 bool sending) {
229 bool sendRTCPBye = false;
230 {
Erik Språng242e22b2015-05-11 10:17:43 +0200231 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
Erik Språng242e22b2015-05-11 10:17:43 +0200233 if (method_ != kRtcpOff) {
234 if (sending == false && sending_ == true) {
Erik Språng61be2a42015-04-27 13:32:52 +0200235 // Trigger RTCP bye
236 sendRTCPBye = true;
237 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000238 }
Erik Språng242e22b2015-05-11 10:17:43 +0200239 sending_ = sending;
Erik Språng61be2a42015-04-27 13:32:52 +0200240 }
241 if (sendRTCPBye)
242 return SendRTCP(feedback_state, kRtcpBye);
243 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244}
245
Erik Språng61be2a42015-04-27 13:32:52 +0200246bool RTCPSender::REMB() const {
Erik Språng242e22b2015-05-11 10:17:43 +0200247 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
248 return remb_enabled_;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000249}
250
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000251void RTCPSender::SetREMBStatus(bool enable) {
Erik Språng242e22b2015-05-11 10:17:43 +0200252 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
253 remb_enabled_ = enable;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000254}
255
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000256void RTCPSender::SetREMBData(uint32_t bitrate,
257 const std::vector<uint32_t>& ssrcs) {
Erik Språng242e22b2015-05-11 10:17:43 +0200258 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
259 remb_bitrate_ = bitrate;
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000260 remb_ssrcs_ = ssrcs;
stefan@webrtc.org4ef438e2014-07-11 09:55:30 +0000261
Erik Språng242e22b2015-05-11 10:17:43 +0200262 if (remb_enabled_)
263 SetFlag(kRtcpRemb, false);
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000264 // Send a REMB immediately if we have a new REMB. The frequency of REMBs is
265 // throttled by the caller.
Erik Språng242e22b2015-05-11 10:17:43 +0200266 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds();
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000267}
268
Erik Språng61be2a42015-04-27 13:32:52 +0200269bool RTCPSender::TMMBR() const {
Erik Språng242e22b2015-05-11 10:17:43 +0200270 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
271 return IsFlagPresent(RTCPPacketType::kRtcpTmmbr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000274void RTCPSender::SetTMMBRStatus(bool enable) {
Erik Språng242e22b2015-05-11 10:17:43 +0200275 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
276 if (enable) {
277 SetFlag(RTCPPacketType::kRtcpTmmbr, false);
278 } else {
279 ConsumeFlag(RTCPPacketType::kRtcpTmmbr, true);
280 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000281}
282
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000283void RTCPSender::SetStartTimestamp(uint32_t start_timestamp) {
Erik Språng242e22b2015-05-11 10:17:43 +0200284 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000285 start_timestamp_ = start_timestamp;
286}
287
288void RTCPSender::SetLastRtpTime(uint32_t rtp_timestamp,
289 int64_t capture_time_ms) {
Erik Språng242e22b2015-05-11 10:17:43 +0200290 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000291 last_rtp_timestamp_ = rtp_timestamp;
292 if (capture_time_ms < 0) {
293 // We don't currently get a capture time from VoiceEngine.
Erik Språng242e22b2015-05-11 10:17:43 +0200294 last_frame_capture_time_ms_ = clock_->TimeInMilliseconds();
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000295 } else {
296 last_frame_capture_time_ms_ = capture_time_ms;
297 }
298}
299
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000300void RTCPSender::SetSSRC(uint32_t ssrc) {
Erik Språng242e22b2015-05-11 10:17:43 +0200301 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
Erik Språng242e22b2015-05-11 10:17:43 +0200303 if (ssrc_ != 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200304 // not first SetSSRC, probably due to a collision
305 // schedule a new RTCP report
306 // make sure that we send a RTP packet
Erik Språng242e22b2015-05-11 10:17:43 +0200307 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + 100;
Erik Språng61be2a42015-04-27 13:32:52 +0200308 }
Erik Språng242e22b2015-05-11 10:17:43 +0200309 ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000310}
311
Erik Språng61be2a42015-04-27 13:32:52 +0200312void RTCPSender::SetRemoteSSRC(uint32_t ssrc) {
Erik Språng242e22b2015-05-11 10:17:43 +0200313 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
314 remote_ssrc_ = ssrc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
Peter Boström9ba52f82015-06-01 14:12:28 +0200317int32_t RTCPSender::SetCNAME(const char* c_name) {
318 if (!c_name)
tommi@webrtc.orga990e122012-04-26 15:28:22 +0000319 return -1;
320
Peter Boström9ba52f82015-06-01 14:12:28 +0200321 DCHECK_LT(strlen(c_name), static_cast<size_t>(RTCP_CNAME_SIZE));
Erik Språng242e22b2015-05-11 10:17:43 +0200322 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
Erik Språng0ea42d32015-06-25 14:46:16 +0200323 cname_ = c_name;
pwestin@webrtc.orgf6bb77a2012-01-24 17:16:59 +0000324 return 0;
325}
326
Erik Språng0ea42d32015-06-25 14:46:16 +0200327int32_t RTCPSender::AddMixedCNAME(uint32_t SSRC, const char* c_name) {
328 assert(c_name);
329 DCHECK_LT(strlen(c_name), static_cast<size_t>(RTCP_CNAME_SIZE));
Erik Språng242e22b2015-05-11 10:17:43 +0200330 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
Erik Språng0ea42d32015-06-25 14:46:16 +0200331 if (csrc_cnames_.size() >= kRtpCsrcSize)
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000332 return -1;
Erik Språng0ea42d32015-06-25 14:46:16 +0200333
334 csrc_cnames_[SSRC] = c_name;
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000335 return 0;
336}
337
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000338int32_t RTCPSender::RemoveMixedCNAME(uint32_t SSRC) {
Erik Språng242e22b2015-05-11 10:17:43 +0200339 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
Erik Språng0ea42d32015-06-25 14:46:16 +0200340 auto it = csrc_cnames_.find(SSRC);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000341
Erik Språng242e22b2015-05-11 10:17:43 +0200342 if (it == csrc_cnames_.end())
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000343 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +0200344
Erik Språng242e22b2015-05-11 10:17:43 +0200345 csrc_cnames_.erase(it);
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000346 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000349bool RTCPSender::TimeToSendRTCPReport(bool sendKeyframeBeforeRTP) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000350/*
351 For audio we use a fix 5 sec interval
352
353 For video we use 1 sec interval fo a BW smaller than 360 kbit/s,
stefan@webrtc.org9d4762e2014-03-24 17:13:00 +0000354 technicaly we break the max 5% RTCP BW for video below 10 kbit/s but
355 that should be extremely rare
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
357
358From RFC 3550
359
360 MAX RTCP BW is 5% if the session BW
361 A send report is approximately 65 bytes inc CNAME
stefan@webrtc.org9d4762e2014-03-24 17:13:00 +0000362 A receiver report is approximately 28 bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000363
364 The RECOMMENDED value for the reduced minimum in seconds is 360
365 divided by the session bandwidth in kilobits/second. This minimum
366 is smaller than 5 seconds for bandwidths greater than 72 kb/s.
367
368 If the participant has not yet sent an RTCP packet (the variable
369 initial is true), the constant Tmin is set to 2.5 seconds, else it
370 is set to 5 seconds.
371
372 The interval between RTCP packets is varied randomly over the
373 range [0.5,1.5] times the calculated interval to avoid unintended
374 synchronization of all participants
375
376 if we send
377 If the participant is a sender (we_sent true), the constant C is
378 set to the average RTCP packet size (avg_rtcp_size) divided by 25%
379 of the RTCP bandwidth (rtcp_bw), and the constant n is set to the
380 number of senders.
381
382 if we receive only
383 If we_sent is not true, the constant C is set
384 to the average RTCP packet size divided by 75% of the RTCP
385 bandwidth. The constant n is set to the number of receivers
386 (members - senders). If the number of senders is greater than
387 25%, senders and receivers are treated together.
388
389 reconsideration NOT required for peer-to-peer
390 "timer reconsideration" is
391 employed. This algorithm implements a simple back-off mechanism
392 which causes users to hold back RTCP packet transmission if the
393 group sizes are increasing.
394
395 n = number of members
396 C = avg_size/(rtcpBW/4)
397
398 3. The deterministic calculated interval Td is set to max(Tmin, n*C).
399
400 4. The calculated interval T is set to a number uniformly distributed
401 between 0.5 and 1.5 times the deterministic calculated interval.
402
403 5. The resulting value of T is divided by e-3/2=1.21828 to compensate
404 for the fact that the timer reconsideration algorithm converges to
405 a value of the RTCP bandwidth below the intended average
406*/
407
Erik Språng242e22b2015-05-11 10:17:43 +0200408 int64_t now = clock_->TimeInMilliseconds();
xians@webrtc.org8738d272011-11-25 13:43:53 +0000409
Erik Språng242e22b2015-05-11 10:17:43 +0200410 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
xians@webrtc.org8738d272011-11-25 13:43:53 +0000411
Erik Språng242e22b2015-05-11 10:17:43 +0200412 if (method_ == kRtcpOff)
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000414
Erik Språng242e22b2015-05-11 10:17:43 +0200415 if (!audio_ && sendKeyframeBeforeRTP) {
Erik Språng61be2a42015-04-27 13:32:52 +0200416 // for video key-frames we want to send the RTCP before the large key-frame
417 // if we have a 100 ms margin
418 now += RTCP_SEND_BEFORE_KEY_FRAME_MS;
419 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
Erik Språng242e22b2015-05-11 10:17:43 +0200421 if (now >= next_time_to_send_rtcp_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200422 return true;
423 } else if (now < 0x0000ffff &&
Erik Språng242e22b2015-05-11 10:17:43 +0200424 next_time_to_send_rtcp_ > 0xffff0000) { // 65 sec margin
Erik Språng61be2a42015-04-27 13:32:52 +0200425 // wrap
426 return true;
427 }
428 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000429}
430
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000431int64_t RTCPSender::SendTimeOfSendReport(uint32_t sendReport) {
Erik Språng242e22b2015-05-11 10:17:43 +0200432 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +0000433
Erik Språng61be2a42015-04-27 13:32:52 +0200434 // This is only saved when we are the sender
Erik Språng242e22b2015-05-11 10:17:43 +0200435 if ((last_send_report_[0] == 0) || (sendReport == 0)) {
Erik Språng61be2a42015-04-27 13:32:52 +0200436 return 0; // will be ignored
437 } else {
438 for (int i = 0; i < RTCP_NUMBER_OF_SR; ++i) {
Erik Språng242e22b2015-05-11 10:17:43 +0200439 if (last_send_report_[i] == sendReport)
440 return last_rtcp_time_[i];
niklase@google.com470e71d2011-07-07 08:21:25 +0000441 }
Erik Språng61be2a42015-04-27 13:32:52 +0200442 }
443 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000446bool RTCPSender::SendTimeOfXrRrReport(uint32_t mid_ntp,
447 int64_t* time_ms) const {
Erik Språng242e22b2015-05-11 10:17:43 +0200448 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +0000449
450 if (last_xr_rr_.empty()) {
451 return false;
452 }
453 std::map<uint32_t, int64_t>::const_iterator it = last_xr_rr_.find(mid_ntp);
454 if (it == last_xr_rr_.end()) {
455 return false;
456 }
457 *time_ms = it->second;
458 return true;
459}
460
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200461int32_t RTCPSender::AddReportBlock(const RTCPReportBlock& report_block) {
462 if (report_blocks_.size() >= RTCP_MAX_REPORT_BLOCKS) {
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000463 LOG(LS_WARNING) << "Too many report blocks.";
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000464 return -1;
465 }
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200466 rtcp::ReportBlock* block = &report_blocks_[report_block.remoteSSRC];
467 block->To(report_block.remoteSSRC);
468 block->WithFractionLost(report_block.fractionLost);
469 block->WithCumulativeLost(report_block.cumulativeLost);
470 block->WithExtHighestSeqNum(report_block.extendedHighSeqNum);
471 block->WithJitter(report_block.jitter);
472 block->WithLastSr(report_block.lastSR);
473 block->WithDelayLastSr(report_block.delaySinceLastSR);
474
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000475 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
Erik Språng242e22b2015-05-11 10:17:43 +0200478RTCPSender::BuildResult RTCPSender::BuildSR(RtcpContext* ctx) {
Erik Språng61be2a42015-04-27 13:32:52 +0200479 for (int i = (RTCP_NUMBER_OF_SR - 2); i >= 0; i--) {
480 // shift old
Erik Språng242e22b2015-05-11 10:17:43 +0200481 last_send_report_[i + 1] = last_send_report_[i];
482 last_rtcp_time_[i + 1] = last_rtcp_time_[i];
Erik Språng61be2a42015-04-27 13:32:52 +0200483 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000484
Erik Språng242e22b2015-05-11 10:17:43 +0200485 last_rtcp_time_[0] = Clock::NtpToMs(ctx->ntp_sec, ctx->ntp_frac);
486 last_send_report_[0] = (ctx->ntp_sec << 16) + (ctx->ntp_frac >> 16);
niklase@google.com470e71d2011-07-07 08:21:25 +0000487
Erik Språng61be2a42015-04-27 13:32:52 +0200488 // The timestamp of this RTCP packet should be estimated as the timestamp of
489 // the frame being captured at this moment. We are calculating that
490 // timestamp as the last frame's timestamp + the time since the last frame
491 // was captured.
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200492 uint32_t rtp_timestamp =
493 start_timestamp_ + last_rtp_timestamp_ +
494 (clock_->TimeInMilliseconds() - last_frame_capture_time_ms_) *
495 (ctx->feedback_state.frequency_hz / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000496
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200497 rtcp::SenderReport report;
498 report.From(ssrc_);
499 report.WithNtpSec(ctx->ntp_sec);
500 report.WithNtpFrac(ctx->ntp_frac);
501 report.WithRtpTimestamp(rtp_timestamp);
502 report.WithPacketCount(ctx->feedback_state.packets_sent);
503 report.WithOctetCount(ctx->feedback_state.media_bytes_sent);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200505 for (auto it : report_blocks_)
506 report.WithReportBlock(it.second);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200508 PacketBuiltCallback callback(ctx);
Erik Språng72aa9a62015-07-31 16:16:02 +0200509 if (!callback.BuildPacket(report))
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200510 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000511
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200512 report_blocks_.clear();
Erik Språng242e22b2015-05-11 10:17:43 +0200513 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000514}
515
Erik Språng0ea42d32015-06-25 14:46:16 +0200516RTCPSender::BuildResult RTCPSender::BuildSDES(RtcpContext* ctx) {
517 size_t length_cname = cname_.length();
518 CHECK_LT(length_cname, static_cast<size_t>(RTCP_CNAME_SIZE));
niklase@google.com470e71d2011-07-07 08:21:25 +0000519
Erik Språng0ea42d32015-06-25 14:46:16 +0200520 rtcp::Sdes sdes;
521 sdes.WithCName(ssrc_, cname_);
522
523 for (const auto it : csrc_cnames_)
524 sdes.WithCName(it.first, it.second);
525
526 PacketBuiltCallback callback(ctx);
Erik Språng72aa9a62015-07-31 16:16:02 +0200527 if (!callback.BuildPacket(sdes))
Erik Språng242e22b2015-05-11 10:17:43 +0200528 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000529
Erik Språng242e22b2015-05-11 10:17:43 +0200530 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000531}
532
Erik Språng242e22b2015-05-11 10:17:43 +0200533RTCPSender::BuildResult RTCPSender::BuildRR(RtcpContext* ctx) {
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200534 rtcp::ReceiverReport report;
535 report.From(ssrc_);
536 for (auto it : report_blocks_)
537 report.WithReportBlock(it.second);
538
539 PacketBuiltCallback callback(ctx);
Erik Språng72aa9a62015-07-31 16:16:02 +0200540 if (!callback.BuildPacket(report))
Erik Språng242e22b2015-05-11 10:17:43 +0200541 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200542
Erik Språngbdc0b0d2015-06-22 15:21:24 +0200543 report_blocks_.clear();
Erik Språng242e22b2015-05-11 10:17:43 +0200544
545 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000546}
547
Erik Språng242e22b2015-05-11 10:17:43 +0200548RTCPSender::BuildResult RTCPSender::BuildPLI(RtcpContext* ctx) {
Erik Språng72aa9a62015-07-31 16:16:02 +0200549 rtcp::Pli pli;
550 pli.From(ssrc_);
551 pli.To(remote_ssrc_);
552
553 PacketBuiltCallback callback(ctx);
554 if (!callback.BuildPacket(pli))
Erik Språng242e22b2015-05-11 10:17:43 +0200555 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200556
Erik Språng242e22b2015-05-11 10:17:43 +0200557 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
558 "RTCPSender::PLI");
559 ++packet_type_counter_.pli_packets;
560 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_PLICount",
561 ssrc_, packet_type_counter_.pli_packets);
562
563 return BuildResult::kSuccess;
Erik Språng61be2a42015-04-27 13:32:52 +0200564}
565
Erik Språng242e22b2015-05-11 10:17:43 +0200566RTCPSender::BuildResult RTCPSender::BuildFIR(RtcpContext* ctx) {
Erik Språng242e22b2015-05-11 10:17:43 +0200567 if (!ctx->repeat)
sprang62dae192015-08-05 02:35:35 -0700568 ++sequence_number_fir_; // Do not increase if repetition.
niklase@google.com470e71d2011-07-07 08:21:25 +0000569
sprang62dae192015-08-05 02:35:35 -0700570 rtcp::Fir fir;
571 fir.From(ssrc_);
572 fir.To(remote_ssrc_);
573 fir.WithCommandSeqNum(sequence_number_fir_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000574
sprang62dae192015-08-05 02:35:35 -0700575 PacketBuiltCallback callback(ctx);
576 if (!callback.BuildPacket(fir))
577 return BuildResult::kTruncated;
Erik Språng242e22b2015-05-11 10:17:43 +0200578
579 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
580 "RTCPSender::FIR");
581 ++packet_type_counter_.fir_packets;
582 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_FIRCount",
583 ssrc_, packet_type_counter_.fir_packets);
584
585 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000586}
587
588/*
589 0 1 2 3
590 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
591 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 | First | Number | PictureID |
593 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
594*/
Erik Språng242e22b2015-05-11 10:17:43 +0200595RTCPSender::BuildResult RTCPSender::BuildSLI(RtcpContext* ctx) {
sprang0365a272015-08-11 01:02:37 -0700596 rtcp::Sli sli;
597 sli.From(ssrc_);
598 sli.To(remote_ssrc_);
599 // Crop picture id to 6 least significant bits.
600 sli.WithPictureId(ctx->picture_id & 0x3F);
601 sli.WithFirstMb(0);
602 sli.WithNumberOfMb(0x1FFF); // 13 bits, only ones for now.
603
604 PacketBuiltCallback callback(ctx);
605 if (!callback.BuildPacket(sli))
Erik Språng242e22b2015-05-11 10:17:43 +0200606 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000607
Erik Språng242e22b2015-05-11 10:17:43 +0200608 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000609}
610
611/*
612 0 1 2 3
613 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
614 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
615 | PB |0| Payload Type| Native RPSI bit string |
616 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
617 | defined per codec ... | Padding (0) |
618 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
619*/
620/*
621* Note: not generic made for VP8
622*/
Erik Språng242e22b2015-05-11 10:17:43 +0200623RTCPSender::BuildResult RTCPSender::BuildRPSI(RtcpContext* ctx) {
624 if (ctx->feedback_state.send_payload_type == 0xFF)
625 return BuildResult::kError;
626
Erik Språng61be2a42015-04-27 13:32:52 +0200627 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200628 if (ctx->position + 24 >= IP_PACKET_SIZE)
629 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000630
Erik Språng61be2a42015-04-27 13:32:52 +0200631 // add Reference Picture Selection Indication
632 uint8_t FMT = 3;
Erik Språng242e22b2015-05-11 10:17:43 +0200633 *ctx->AllocateData(1) = 0x80 + FMT;
634 *ctx->AllocateData(1) = 206;
niklase@google.com470e71d2011-07-07 08:21:25 +0000635
Erik Språng61be2a42015-04-27 13:32:52 +0200636 // calc length
637 uint32_t bitsRequired = 7;
638 uint8_t bytesRequired = 1;
Erik Språng242e22b2015-05-11 10:17:43 +0200639 while ((ctx->picture_id >> bitsRequired) > 0) {
Erik Språng61be2a42015-04-27 13:32:52 +0200640 bitsRequired += 7;
641 bytesRequired++;
642 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000643
Erik Språng61be2a42015-04-27 13:32:52 +0200644 uint8_t size = 3;
645 if (bytesRequired > 6) {
646 size = 5;
647 } else if (bytesRequired > 2) {
648 size = 4;
649 }
Erik Språng242e22b2015-05-11 10:17:43 +0200650 *ctx->AllocateData(1) = 0;
651 *ctx->AllocateData(1) = size;
niklase@google.com470e71d2011-07-07 08:21:25 +0000652
Erik Språng61be2a42015-04-27 13:32:52 +0200653 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200654 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000655
Erik Språng61be2a42015-04-27 13:32:52 +0200656 // Add the remote SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200657 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200658
659 // calc padding length
660 uint8_t paddingBytes = 4 - ((2 + bytesRequired) % 4);
661 if (paddingBytes == 4)
662 paddingBytes = 0;
663 // add padding length in bits
Erik Språng242e22b2015-05-11 10:17:43 +0200664 *ctx->AllocateData(1) = paddingBytes * 8; // padding can be 0, 8, 16 or 24
Erik Språng61be2a42015-04-27 13:32:52 +0200665
666 // add payload type
Erik Språng242e22b2015-05-11 10:17:43 +0200667 *ctx->AllocateData(1) = ctx->feedback_state.send_payload_type;
Erik Språng61be2a42015-04-27 13:32:52 +0200668
669 // add picture ID
670 for (int i = bytesRequired - 1; i > 0; --i) {
Erik Språng242e22b2015-05-11 10:17:43 +0200671 *ctx->AllocateData(1) =
672 0x80 | static_cast<uint8_t>(ctx->picture_id >> (i * 7));
Erik Språng61be2a42015-04-27 13:32:52 +0200673 }
674 // add last byte of picture ID
Erik Språng242e22b2015-05-11 10:17:43 +0200675 *ctx->AllocateData(1) = static_cast<uint8_t>(ctx->picture_id & 0x7f);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
Erik Språng61be2a42015-04-27 13:32:52 +0200677 // add padding
678 for (int j = 0; j < paddingBytes; j++) {
Erik Språng242e22b2015-05-11 10:17:43 +0200679 *ctx->AllocateData(1) = 0;
Erik Språng61be2a42015-04-27 13:32:52 +0200680 }
Erik Språng242e22b2015-05-11 10:17:43 +0200681
682 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000683}
684
Erik Språng242e22b2015-05-11 10:17:43 +0200685RTCPSender::BuildResult RTCPSender::BuildREMB(RtcpContext* ctx) {
Erik Språng61be2a42015-04-27 13:32:52 +0200686 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200687 if (ctx->position + 20 + 4 * remb_ssrcs_.size() >= IP_PACKET_SIZE)
688 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200689
690 // add application layer feedback
691 uint8_t FMT = 15;
Erik Språng242e22b2015-05-11 10:17:43 +0200692 *ctx->AllocateData(1) = 0x80 + FMT;
693 *ctx->AllocateData(1) = 206;
Erik Språng61be2a42015-04-27 13:32:52 +0200694
Erik Språng242e22b2015-05-11 10:17:43 +0200695 *ctx->AllocateData(1) = 0;
696 *ctx->AllocateData(1) = static_cast<uint8_t>(remb_ssrcs_.size() + 4);
Erik Språng61be2a42015-04-27 13:32:52 +0200697
698 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200699 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200700
701 // Remote SSRC must be 0
Erik Språng242e22b2015-05-11 10:17:43 +0200702 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200703
Erik Språng242e22b2015-05-11 10:17:43 +0200704 *ctx->AllocateData(1) = 'R';
705 *ctx->AllocateData(1) = 'E';
706 *ctx->AllocateData(1) = 'M';
707 *ctx->AllocateData(1) = 'B';
Erik Språng61be2a42015-04-27 13:32:52 +0200708
Erik Språng242e22b2015-05-11 10:17:43 +0200709 *ctx->AllocateData(1) = remb_ssrcs_.size();
Erik Språng61be2a42015-04-27 13:32:52 +0200710 // 6 bit Exp
711 // 18 bit mantissa
712 uint8_t brExp = 0;
713 for (uint32_t i = 0; i < 64; i++) {
Erik Språng242e22b2015-05-11 10:17:43 +0200714 if (remb_bitrate_ <= (0x3FFFFu << i)) {
Erik Språng61be2a42015-04-27 13:32:52 +0200715 brExp = i;
716 break;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000717 }
Erik Språng61be2a42015-04-27 13:32:52 +0200718 }
Erik Språng242e22b2015-05-11 10:17:43 +0200719 const uint32_t brMantissa = (remb_bitrate_ >> brExp);
720 *ctx->AllocateData(1) =
721 static_cast<uint8_t>((brExp << 2) + ((brMantissa >> 16) & 0x03));
722 *ctx->AllocateData(1) = static_cast<uint8_t>(brMantissa >> 8);
723 *ctx->AllocateData(1) = static_cast<uint8_t>(brMantissa);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000724
Erik Språng242e22b2015-05-11 10:17:43 +0200725 for (size_t i = 0; i < remb_ssrcs_.size(); i++)
726 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), remb_ssrcs_[i]);
727
728 TRACE_EVENT_INSTANT0(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
729 "RTCPSender::REMB");
730
731 return BuildResult::kSuccess;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000732}
733
Erik Språng61be2a42015-04-27 13:32:52 +0200734void RTCPSender::SetTargetBitrate(unsigned int target_bitrate) {
Erik Språng242e22b2015-05-11 10:17:43 +0200735 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
736 tmmbr_send_ = target_bitrate / 1000;
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000737}
738
Erik Språng242e22b2015-05-11 10:17:43 +0200739RTCPSender::BuildResult RTCPSender::BuildTMMBR(RtcpContext* ctx) {
740 if (ctx->feedback_state.module == NULL)
741 return BuildResult::kError;
Erik Språng61be2a42015-04-27 13:32:52 +0200742 // Before sending the TMMBR check the received TMMBN, only an owner is
743 // allowed to raise the bitrate:
744 // * If the sender is an owner of the TMMBN -> send TMMBR
745 // * If not an owner but the TMMBR would enter the TMMBN -> send TMMBR
niklase@google.com470e71d2011-07-07 08:21:25 +0000746
Erik Språng61be2a42015-04-27 13:32:52 +0200747 // get current bounding set from RTCP receiver
748 bool tmmbrOwner = false;
749 // store in candidateSet, allocates one extra slot
Erik Språng242e22b2015-05-11 10:17:43 +0200750 TMMBRSet* candidateSet = tmmbr_help_.CandidateSet();
niklase@google.com470e71d2011-07-07 08:21:25 +0000751
Erik Språng242e22b2015-05-11 10:17:43 +0200752 // holding critical_section_rtcp_sender_ while calling RTCPreceiver which
753 // will accuire criticalSectionRTCPReceiver_ is a potental deadlock but
Erik Språng61be2a42015-04-27 13:32:52 +0200754 // since RTCPreceiver is not doing the reverse we should be fine
755 int32_t lengthOfBoundingSet =
Erik Språng242e22b2015-05-11 10:17:43 +0200756 ctx->feedback_state.module->BoundingSet(tmmbrOwner, candidateSet);
niklase@google.com470e71d2011-07-07 08:21:25 +0000757
Erik Språng61be2a42015-04-27 13:32:52 +0200758 if (lengthOfBoundingSet > 0) {
759 for (int32_t i = 0; i < lengthOfBoundingSet; i++) {
Erik Språng242e22b2015-05-11 10:17:43 +0200760 if (candidateSet->Tmmbr(i) == tmmbr_send_ &&
761 candidateSet->PacketOH(i) == packet_oh_send_) {
Erik Språng61be2a42015-04-27 13:32:52 +0200762 // do not send the same tuple
Erik Språng242e22b2015-05-11 10:17:43 +0200763 return BuildResult::kAborted;
Erik Språng61be2a42015-04-27 13:32:52 +0200764 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 }
Erik Språng61be2a42015-04-27 13:32:52 +0200766 if (!tmmbrOwner) {
767 // use received bounding set as candidate set
768 // add current tuple
Erik Språng242e22b2015-05-11 10:17:43 +0200769 candidateSet->SetEntry(lengthOfBoundingSet, tmmbr_send_, packet_oh_send_,
770 ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200771 int numCandidates = lengthOfBoundingSet + 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772
Erik Språng61be2a42015-04-27 13:32:52 +0200773 // find bounding set
774 TMMBRSet* boundingSet = NULL;
Erik Språng242e22b2015-05-11 10:17:43 +0200775 int numBoundingSet = tmmbr_help_.FindTMMBRBoundingSet(boundingSet);
Erik Språng61be2a42015-04-27 13:32:52 +0200776 if (numBoundingSet > 0 || numBoundingSet <= numCandidates)
Erik Språng242e22b2015-05-11 10:17:43 +0200777 tmmbrOwner = tmmbr_help_.IsOwner(ssrc_, numBoundingSet);
Erik Språng61be2a42015-04-27 13:32:52 +0200778 if (!tmmbrOwner) {
779 // did not enter bounding set, no meaning to send this request
Erik Språng242e22b2015-05-11 10:17:43 +0200780 return BuildResult::kAborted;
Erik Språng61be2a42015-04-27 13:32:52 +0200781 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 }
Erik Språng61be2a42015-04-27 13:32:52 +0200783 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000784
Erik Språng242e22b2015-05-11 10:17:43 +0200785 if (tmmbr_send_) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200787 if (ctx->position + 20 >= IP_PACKET_SIZE)
788 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
Erik Språng61be2a42015-04-27 13:32:52 +0200790 // add TMMBR indicator
791 uint8_t FMT = 3;
Erik Språng242e22b2015-05-11 10:17:43 +0200792 *ctx->AllocateData(1) = 0x80 + FMT;
793 *ctx->AllocateData(1) = 205;
Erik Språng61be2a42015-04-27 13:32:52 +0200794
795 // Length of 4
Erik Språng242e22b2015-05-11 10:17:43 +0200796 *ctx->AllocateData(1) = 0;
797 *ctx->AllocateData(1) = 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000798
799 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200800 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000801
Erik Språng61be2a42015-04-27 13:32:52 +0200802 // RFC 5104 4.2.1.2. Semantics
niklase@google.com470e71d2011-07-07 08:21:25 +0000803
804 // SSRC of media source
Erik Språng242e22b2015-05-11 10:17:43 +0200805 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000806
807 // Additional Feedback Control Information (FCI)
Erik Språng242e22b2015-05-11 10:17:43 +0200808 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), remote_ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000809
Erik Språng242e22b2015-05-11 10:17:43 +0200810 uint32_t bitRate = tmmbr_send_ * 1000;
Erik Språng61be2a42015-04-27 13:32:52 +0200811 uint32_t mmbrExp = 0;
812 for (uint32_t i = 0; i < 64; i++) {
813 if (bitRate <= (0x1FFFFu << i)) {
814 mmbrExp = i;
815 break;
816 }
817 }
818 uint32_t mmbrMantissa = (bitRate >> mmbrExp);
819
Erik Språng242e22b2015-05-11 10:17:43 +0200820 *ctx->AllocateData(1) =
Erik Språng61be2a42015-04-27 13:32:52 +0200821 static_cast<uint8_t>((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03));
Erik Språng242e22b2015-05-11 10:17:43 +0200822 *ctx->AllocateData(1) = static_cast<uint8_t>(mmbrMantissa >> 7);
823 *ctx->AllocateData(1) = static_cast<uint8_t>(
824 (mmbrMantissa << 1) + ((packet_oh_send_ >> 8) & 0x01));
825 *ctx->AllocateData(1) = static_cast<uint8_t>(packet_oh_send_);
Erik Språng61be2a42015-04-27 13:32:52 +0200826 }
Erik Språng242e22b2015-05-11 10:17:43 +0200827 return BuildResult::kSuccess;
Erik Språng61be2a42015-04-27 13:32:52 +0200828}
829
Erik Språng242e22b2015-05-11 10:17:43 +0200830RTCPSender::BuildResult RTCPSender::BuildTMMBN(RtcpContext* ctx) {
831 TMMBRSet* boundingSet = tmmbr_help_.BoundingSetToSend();
Erik Språng61be2a42015-04-27 13:32:52 +0200832 if (boundingSet == NULL)
Erik Språng242e22b2015-05-11 10:17:43 +0200833 return BuildResult::kError;
Erik Språng61be2a42015-04-27 13:32:52 +0200834
835 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200836 if (ctx->position + 12 + boundingSet->lengthOfSet() * 8 >= IP_PACKET_SIZE) {
Erik Språng61be2a42015-04-27 13:32:52 +0200837 LOG(LS_WARNING) << "Failed to build TMMBN.";
Erik Språng242e22b2015-05-11 10:17:43 +0200838 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200839 }
840
841 uint8_t FMT = 4;
842 // add TMMBN indicator
Erik Språng242e22b2015-05-11 10:17:43 +0200843 *ctx->AllocateData(1) = 0x80 + FMT;
844 *ctx->AllocateData(1) = 205;
Erik Språng61be2a42015-04-27 13:32:52 +0200845
846 // Add length later
Erik Språng242e22b2015-05-11 10:17:43 +0200847 int posLength = ctx->position;
848 ctx->AllocateData(2);
Erik Språng61be2a42015-04-27 13:32:52 +0200849
850 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200851 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200852
853 // RFC 5104 4.2.2.2. Semantics
854
855 // SSRC of media source
Erik Språng242e22b2015-05-11 10:17:43 +0200856 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), 0);
Erik Språng61be2a42015-04-27 13:32:52 +0200857
858 // Additional Feedback Control Information (FCI)
859 int numBoundingSet = 0;
860 for (uint32_t n = 0; n < boundingSet->lengthOfSet(); n++) {
861 if (boundingSet->Tmmbr(n) > 0) {
862 uint32_t tmmbrSSRC = boundingSet->Ssrc(n);
Erik Språng242e22b2015-05-11 10:17:43 +0200863 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), tmmbrSSRC);
Erik Språng61be2a42015-04-27 13:32:52 +0200864
865 uint32_t bitRate = boundingSet->Tmmbr(n) * 1000;
866 uint32_t mmbrExp = 0;
867 for (int i = 0; i < 64; i++) {
868 if (bitRate <= (0x1FFFFu << i)) {
869 mmbrExp = i;
andresp@webrtc.org523f9372013-04-11 11:30:39 +0000870 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 }
andresp@webrtc.org523f9372013-04-11 11:30:39 +0000872 }
Erik Språng61be2a42015-04-27 13:32:52 +0200873 uint32_t mmbrMantissa = (bitRate >> mmbrExp);
874 uint32_t measuredOH = boundingSet->PacketOH(n);
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000875
Erik Språng242e22b2015-05-11 10:17:43 +0200876 *ctx->AllocateData(1) =
Erik Språng61be2a42015-04-27 13:32:52 +0200877 static_cast<uint8_t>((mmbrExp << 2) + ((mmbrMantissa >> 15) & 0x03));
Erik Språng242e22b2015-05-11 10:17:43 +0200878 *ctx->AllocateData(1) = static_cast<uint8_t>(mmbrMantissa >> 7);
879 *ctx->AllocateData(1) = static_cast<uint8_t>((mmbrMantissa << 1) +
880 ((measuredOH >> 8) & 0x01));
881 *ctx->AllocateData(1) = static_cast<uint8_t>(measuredOH);
Erik Språng61be2a42015-04-27 13:32:52 +0200882 numBoundingSet++;
asapersson@webrtc.org2dd31342014-10-29 12:42:30 +0000883 }
Erik Språng61be2a42015-04-27 13:32:52 +0200884 }
885 uint16_t length = static_cast<uint16_t>(2 + 2 * numBoundingSet);
Erik Språng242e22b2015-05-11 10:17:43 +0200886 ctx->buffer[posLength++] = static_cast<uint8_t>(length >> 8);
887 ctx->buffer[posLength] = static_cast<uint8_t>(length);
888
889 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +0000890}
891
Erik Språng242e22b2015-05-11 10:17:43 +0200892RTCPSender::BuildResult RTCPSender::BuildAPP(RtcpContext* ctx) {
Erik Språng61be2a42015-04-27 13:32:52 +0200893 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200894 if (app_data_ == NULL) {
Erik Språng61be2a42015-04-27 13:32:52 +0200895 LOG(LS_WARNING) << "Failed to build app specific.";
Erik Språng242e22b2015-05-11 10:17:43 +0200896 return BuildResult::kError;
Erik Språng61be2a42015-04-27 13:32:52 +0200897 }
Erik Språng242e22b2015-05-11 10:17:43 +0200898 if (ctx->position + 12 + app_length_ >= IP_PACKET_SIZE) {
Erik Språng61be2a42015-04-27 13:32:52 +0200899 LOG(LS_WARNING) << "Failed to build app specific.";
Erik Språng242e22b2015-05-11 10:17:43 +0200900 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200901 }
Erik Språng242e22b2015-05-11 10:17:43 +0200902 *ctx->AllocateData(1) = 0x80 + app_sub_type_;
Erik Språng61be2a42015-04-27 13:32:52 +0200903
904 // Add APP ID
Erik Språng242e22b2015-05-11 10:17:43 +0200905 *ctx->AllocateData(1) = 204;
Erik Språng61be2a42015-04-27 13:32:52 +0200906
Erik Språng242e22b2015-05-11 10:17:43 +0200907 uint16_t length = (app_length_ >> 2) + 2; // include SSRC and name
908 *ctx->AllocateData(1) = static_cast<uint8_t>(length >> 8);
909 *ctx->AllocateData(1) = static_cast<uint8_t>(length);
Erik Språng61be2a42015-04-27 13:32:52 +0200910
911 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200912 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200913
914 // Add our application name
Erik Språng242e22b2015-05-11 10:17:43 +0200915 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), app_name_);
Erik Språng61be2a42015-04-27 13:32:52 +0200916
917 // Add the data
Erik Språng242e22b2015-05-11 10:17:43 +0200918 memcpy(ctx->AllocateData(app_length_), app_data_.get(), app_length_);
919
920 return BuildResult::kSuccess;
Erik Språng61be2a42015-04-27 13:32:52 +0200921}
922
Erik Språng242e22b2015-05-11 10:17:43 +0200923RTCPSender::BuildResult RTCPSender::BuildNACK(RtcpContext* ctx) {
Erik Språng61be2a42015-04-27 13:32:52 +0200924 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200925 if (ctx->position + 16 >= IP_PACKET_SIZE) {
Erik Språng61be2a42015-04-27 13:32:52 +0200926 LOG(LS_WARNING) << "Failed to build NACK.";
Erik Språng242e22b2015-05-11 10:17:43 +0200927 return BuildResult::kTruncated;
Erik Språng61be2a42015-04-27 13:32:52 +0200928 }
929
Erik Språng242e22b2015-05-11 10:17:43 +0200930 // int size, uint16_t* nack_list
Erik Språng61be2a42015-04-27 13:32:52 +0200931 // add nack list
932 uint8_t FMT = 1;
Erik Språng242e22b2015-05-11 10:17:43 +0200933 *ctx->AllocateData(1) = 0x80 + FMT;
934 *ctx->AllocateData(1) = 205;
Erik Språng61be2a42015-04-27 13:32:52 +0200935
Erik Språng242e22b2015-05-11 10:17:43 +0200936 *ctx->AllocateData(1) = 0;
937 int nack_size_pos_ = ctx->position;
938 *ctx->AllocateData(1) = 3; // setting it to one kNACK signal as default
Erik Språng61be2a42015-04-27 13:32:52 +0200939
940 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200941 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200942
943 // Add the remote SSRC
Erik Språng242e22b2015-05-11 10:17:43 +0200944 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), remote_ssrc_);
Erik Språng61be2a42015-04-27 13:32:52 +0200945
946 // Build NACK bitmasks and write them to the RTCP message.
947 // The nack list should be sorted and not contain duplicates if one
948 // wants to build the smallest rtcp nack packet.
949 int numOfNackFields = 0;
950 int maxNackFields =
Erik Språng242e22b2015-05-11 10:17:43 +0200951 std::min<int>(kRtcpMaxNackFields, (IP_PACKET_SIZE - ctx->position) / 4);
Erik Språng61be2a42015-04-27 13:32:52 +0200952 int i = 0;
Erik Språng242e22b2015-05-11 10:17:43 +0200953 while (i < ctx->nack_size && numOfNackFields < maxNackFields) {
954 uint16_t nack = ctx->nack_list[i++];
Erik Språng61be2a42015-04-27 13:32:52 +0200955 uint16_t bitmask = 0;
Erik Språng242e22b2015-05-11 10:17:43 +0200956 while (i < ctx->nack_size) {
957 int shift = static_cast<uint16_t>(ctx->nack_list[i] - nack) - 1;
Erik Språng61be2a42015-04-27 13:32:52 +0200958 if (shift >= 0 && shift <= 15) {
959 bitmask |= (1 << shift);
960 ++i;
961 } else {
962 break;
963 }
964 }
965 // Write the sequence number and the bitmask to the packet.
Erik Språng242e22b2015-05-11 10:17:43 +0200966 assert(ctx->position + 4 < IP_PACKET_SIZE);
967 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2), nack);
968 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2), bitmask);
Erik Språng61be2a42015-04-27 13:32:52 +0200969 numOfNackFields++;
970 }
Erik Språng242e22b2015-05-11 10:17:43 +0200971 ctx->buffer[nack_size_pos_] = static_cast<uint8_t>(2 + numOfNackFields);
Erik Språng61be2a42015-04-27 13:32:52 +0200972
Erik Språng242e22b2015-05-11 10:17:43 +0200973 if (i != ctx->nack_size)
Erik Språng61be2a42015-04-27 13:32:52 +0200974 LOG(LS_WARNING) << "Nack list too large for one packet.";
975
976 // Report stats.
977 NACKStringBuilder stringBuilder;
978 for (int idx = 0; idx < i; ++idx) {
Erik Språng242e22b2015-05-11 10:17:43 +0200979 stringBuilder.PushNACK(ctx->nack_list[idx]);
980 nack_stats_.ReportRequest(ctx->nack_list[idx]);
Erik Språng61be2a42015-04-27 13:32:52 +0200981 }
Erik Språng61be2a42015-04-27 13:32:52 +0200982 packet_type_counter_.nack_requests = nack_stats_.requests();
983 packet_type_counter_.unique_nack_requests = nack_stats_.unique_requests();
Erik Språng242e22b2015-05-11 10:17:43 +0200984
985 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"),
986 "RTCPSender::NACK", "nacks",
987 TRACE_STR_COPY(stringBuilder.GetResult().c_str()));
988 ++packet_type_counter_.nack_packets;
989 TRACE_COUNTER_ID1(TRACE_DISABLED_BY_DEFAULT("webrtc_rtp"), "RTCP_NACKCount",
990 ssrc_, packet_type_counter_.nack_packets);
991
992 return BuildResult::kSuccess;
Erik Språng61be2a42015-04-27 13:32:52 +0200993}
994
Erik Språng242e22b2015-05-11 10:17:43 +0200995RTCPSender::BuildResult RTCPSender::BuildBYE(RtcpContext* ctx) {
pbos@webrtc.org9334ac22014-11-24 08:25:50 +0000996 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +0200997 if (ctx->position + 8 >= IP_PACKET_SIZE)
998 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +0000999
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001000 // Add a bye packet
1001 // Number of SSRC + CSRCs.
Erik Språng242e22b2015-05-11 10:17:43 +02001002 *ctx->AllocateData(1) = static_cast<uint8_t>(0x80 + 1 + csrcs_.size());
1003 *ctx->AllocateData(1) = 203;
niklase@google.com470e71d2011-07-07 08:21:25 +00001004
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001005 // length
Erik Språng242e22b2015-05-11 10:17:43 +02001006 *ctx->AllocateData(1) = 0;
1007 *ctx->AllocateData(1) = static_cast<uint8_t>(1 + csrcs_.size());
niklase@google.com470e71d2011-07-07 08:21:25 +00001008
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001009 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +02001010 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001011
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001012 // add CSRCs
Erik Språng242e22b2015-05-11 10:17:43 +02001013 for (size_t i = 0; i < csrcs_.size(); i++)
1014 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), csrcs_[i]);
niklase@google.com470e71d2011-07-07 08:21:25 +00001015
Erik Språng242e22b2015-05-11 10:17:43 +02001016 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +00001017}
1018
Erik Språng242e22b2015-05-11 10:17:43 +02001019RTCPSender::BuildResult RTCPSender::BuildReceiverReferenceTime(
1020 RtcpContext* ctx) {
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001021 const int kRrTimeBlockLength = 20;
Erik Språng242e22b2015-05-11 10:17:43 +02001022 if (ctx->position + kRrTimeBlockLength >= IP_PACKET_SIZE)
1023 return BuildResult::kTruncated;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001024
Erik Språng61be2a42015-04-27 13:32:52 +02001025 if (last_xr_rr_.size() >= RTCP_NUMBER_OF_SR)
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001026 last_xr_rr_.erase(last_xr_rr_.begin());
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001027 last_xr_rr_.insert(std::pair<uint32_t, int64_t>(
Erik Språng242e22b2015-05-11 10:17:43 +02001028 RTCPUtility::MidNtp(ctx->ntp_sec, ctx->ntp_frac),
1029 Clock::NtpToMs(ctx->ntp_sec, ctx->ntp_frac)));
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001030
1031 // Add XR header.
Erik Språng242e22b2015-05-11 10:17:43 +02001032 *ctx->AllocateData(1) = 0x80;
1033 *ctx->AllocateData(1) = 207;
1034 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1035 4); // XR packet length.
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001036
1037 // Add our own SSRC.
Erik Språng242e22b2015-05-11 10:17:43 +02001038 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001039
1040 // 0 1 2 3
1041 // 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
1042 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1043 // | BT=4 | reserved | block length = 2 |
1044 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1045 // | NTP timestamp, most significant word |
1046 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1047 // | NTP timestamp, least significant word |
1048 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1049
1050 // Add Receiver Reference Time Report block.
Erik Språng242e22b2015-05-11 10:17:43 +02001051 *ctx->AllocateData(1) = 4; // BT.
1052 *ctx->AllocateData(1) = 0; // Reserved.
1053 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1054 2); // Block length.
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001055
1056 // NTP timestamp.
Erik Språng242e22b2015-05-11 10:17:43 +02001057 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ctx->ntp_sec);
1058 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ctx->ntp_frac);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001059
Erik Språng242e22b2015-05-11 10:17:43 +02001060 return BuildResult::kSuccess;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001061}
1062
Erik Språng242e22b2015-05-11 10:17:43 +02001063RTCPSender::BuildResult RTCPSender::BuildDlrr(RtcpContext* ctx) {
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001064 const int kDlrrBlockLength = 24;
Erik Språng242e22b2015-05-11 10:17:43 +02001065 if (ctx->position + kDlrrBlockLength >= IP_PACKET_SIZE)
1066 return BuildResult::kTruncated;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001067
1068 // Add XR header.
Erik Språng242e22b2015-05-11 10:17:43 +02001069 *ctx->AllocateData(1) = 0x80;
1070 *ctx->AllocateData(1) = 207;
1071 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1072 5); // XR packet length.
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001073
1074 // Add our own SSRC.
Erik Språng242e22b2015-05-11 10:17:43 +02001075 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001076
1077 // 0 1 2 3
1078 // 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
1079 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1080 // | BT=5 | reserved | block length |
1081 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
1082 // | SSRC_1 (SSRC of first receiver) | sub-
1083 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
1084 // | last RR (LRR) | 1
1085 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1086 // | delay since last RR (DLRR) |
1087 // +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
1088 // | SSRC_2 (SSRC of second receiver) | sub-
1089 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
1090 // : ... : 2
1091
1092 // Add DLRR sub block.
Erik Språng242e22b2015-05-11 10:17:43 +02001093 *ctx->AllocateData(1) = 5; // BT.
1094 *ctx->AllocateData(1) = 0; // Reserved.
1095 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1096 3); // Block length.
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001097
1098 // NTP timestamp.
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001099
Erik Språng242e22b2015-05-11 10:17:43 +02001100 const RtcpReceiveTimeInfo& info = ctx->feedback_state.last_xr_rr;
1101 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), info.sourceSSRC);
1102 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), info.lastRR);
1103 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4),
1104 info.delaySinceLastRR);
1105
1106 return BuildResult::kSuccess;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001107}
1108
Erik Språng242e22b2015-05-11 10:17:43 +02001109// TODO(sprang): Add a unit test for this, or remove if the code isn't used.
1110RTCPSender::BuildResult RTCPSender::BuildVoIPMetric(RtcpContext* ctx) {
Erik Språng61be2a42015-04-27 13:32:52 +02001111 // sanity
Erik Språng242e22b2015-05-11 10:17:43 +02001112 if (ctx->position + 44 >= IP_PACKET_SIZE)
1113 return BuildResult::kTruncated;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114
Erik Språng61be2a42015-04-27 13:32:52 +02001115 // Add XR header
Erik Språng242e22b2015-05-11 10:17:43 +02001116 *ctx->AllocateData(1) = 0x80;
1117 *ctx->AllocateData(1) = 207;
niklase@google.com470e71d2011-07-07 08:21:25 +00001118
Erik Språng242e22b2015-05-11 10:17:43 +02001119 uint32_t XRLengthPos = ctx->position;
niklase@google.com470e71d2011-07-07 08:21:25 +00001120
Erik Språng61be2a42015-04-27 13:32:52 +02001121 // handle length later on
Erik Språng242e22b2015-05-11 10:17:43 +02001122 ctx->AllocateData(2);
niklase@google.com470e71d2011-07-07 08:21:25 +00001123
Erik Språng61be2a42015-04-27 13:32:52 +02001124 // Add our own SSRC
Erik Språng242e22b2015-05-11 10:17:43 +02001125 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001126
Erik Språng61be2a42015-04-27 13:32:52 +02001127 // Add a VoIP metrics block
Erik Språng242e22b2015-05-11 10:17:43 +02001128 *ctx->AllocateData(1) = 7;
1129 *ctx->AllocateData(1) = 0;
1130 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2), 8);
niklase@google.com470e71d2011-07-07 08:21:25 +00001131
Erik Språng61be2a42015-04-27 13:32:52 +02001132 // Add the remote SSRC
Erik Språng242e22b2015-05-11 10:17:43 +02001133 ByteWriter<uint32_t>::WriteBigEndian(ctx->AllocateData(4), remote_ssrc_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001134
Erik Språng242e22b2015-05-11 10:17:43 +02001135 *ctx->AllocateData(1) = xr_voip_metric_.lossRate;
1136 *ctx->AllocateData(1) = xr_voip_metric_.discardRate;
1137 *ctx->AllocateData(1) = xr_voip_metric_.burstDensity;
1138 *ctx->AllocateData(1) = xr_voip_metric_.gapDensity;
niklase@google.com470e71d2011-07-07 08:21:25 +00001139
Erik Språng242e22b2015-05-11 10:17:43 +02001140 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1141 xr_voip_metric_.burstDuration);
1142 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1143 xr_voip_metric_.gapDuration);
niklase@google.com470e71d2011-07-07 08:21:25 +00001144
Erik Språng242e22b2015-05-11 10:17:43 +02001145 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1146 xr_voip_metric_.roundTripDelay);
1147 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1148 xr_voip_metric_.endSystemDelay);
niklase@google.com470e71d2011-07-07 08:21:25 +00001149
Erik Språng242e22b2015-05-11 10:17:43 +02001150 *ctx->AllocateData(1) = xr_voip_metric_.signalLevel;
1151 *ctx->AllocateData(1) = xr_voip_metric_.noiseLevel;
1152 *ctx->AllocateData(1) = xr_voip_metric_.RERL;
1153 *ctx->AllocateData(1) = xr_voip_metric_.Gmin;
niklase@google.com470e71d2011-07-07 08:21:25 +00001154
Erik Språng242e22b2015-05-11 10:17:43 +02001155 *ctx->AllocateData(1) = xr_voip_metric_.Rfactor;
1156 *ctx->AllocateData(1) = xr_voip_metric_.extRfactor;
1157 *ctx->AllocateData(1) = xr_voip_metric_.MOSLQ;
1158 *ctx->AllocateData(1) = xr_voip_metric_.MOSCQ;
niklase@google.com470e71d2011-07-07 08:21:25 +00001159
Erik Språng242e22b2015-05-11 10:17:43 +02001160 *ctx->AllocateData(1) = xr_voip_metric_.RXconfig;
1161 *ctx->AllocateData(1) = 0; // reserved
niklase@google.com470e71d2011-07-07 08:21:25 +00001162
Erik Språng242e22b2015-05-11 10:17:43 +02001163 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1164 xr_voip_metric_.JBnominal);
1165 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1166 xr_voip_metric_.JBmax);
1167 ByteWriter<uint16_t>::WriteBigEndian(ctx->AllocateData(2),
1168 xr_voip_metric_.JBabsMax);
niklase@google.com470e71d2011-07-07 08:21:25 +00001169
Erik Språng242e22b2015-05-11 10:17:43 +02001170 ByteWriter<uint16_t>::WriteBigEndian(&ctx->buffer[XRLengthPos], 10);
1171
1172 return BuildResult::kSuccess;
niklase@google.com470e71d2011-07-07 08:21:25 +00001173}
1174
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001175int32_t RTCPSender::SendRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +02001176 RTCPPacketType packetType,
1177 int32_t nack_size,
1178 const uint16_t* nack_list,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001179 bool repeat,
1180 uint64_t pictureID) {
Erik Språng242e22b2015-05-11 10:17:43 +02001181 return SendCompoundRTCP(
1182 feedback_state, std::set<RTCPPacketType>(&packetType, &packetType + 1),
1183 nack_size, nack_list, repeat, pictureID);
1184}
1185
1186int32_t RTCPSender::SendCompoundRTCP(
1187 const FeedbackState& feedback_state,
1188 const std::set<RTCPPacketType>& packetTypes,
1189 int32_t nack_size,
1190 const uint16_t* nack_list,
1191 bool repeat,
1192 uint64_t pictureID) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001193 {
Erik Språng242e22b2015-05-11 10:17:43 +02001194 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
1195 if (method_ == kRtcpOff) {
Erik Språng61be2a42015-04-27 13:32:52 +02001196 LOG(LS_WARNING) << "Can't send rtcp if it is disabled.";
1197 return -1;
pwestin@webrtc.org8edb39d2011-12-22 07:40:33 +00001198 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001199 }
1200 uint8_t rtcp_buffer[IP_PACKET_SIZE];
Erik Språng242e22b2015-05-11 10:17:43 +02001201 int rtcp_length =
1202 PrepareRTCP(feedback_state, packetTypes, nack_size, nack_list, repeat,
1203 pictureID, rtcp_buffer, IP_PACKET_SIZE);
Erik Språng61be2a42015-04-27 13:32:52 +02001204
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001205 // Sanity don't send empty packets.
Erik Språng242e22b2015-05-11 10:17:43 +02001206 if (rtcp_length <= 0)
1207 return -1;
Erik Språng61be2a42015-04-27 13:32:52 +02001208
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001209 return SendToNetwork(rtcp_buffer, static_cast<size_t>(rtcp_length));
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001210}
1211
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001212int RTCPSender::PrepareRTCP(const FeedbackState& feedback_state,
Erik Språng242e22b2015-05-11 10:17:43 +02001213 const std::set<RTCPPacketType>& packetTypes,
1214 int32_t nack_size,
1215 const uint16_t* nack_list,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001216 bool repeat,
1217 uint64_t pictureID,
1218 uint8_t* rtcp_buffer,
1219 int buffer_size) {
Erik Språng242e22b2015-05-11 10:17:43 +02001220 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001221
Erik Språng242e22b2015-05-11 10:17:43 +02001222 RtcpContext context(feedback_state, nack_size, nack_list, repeat, pictureID,
1223 rtcp_buffer, buffer_size);
1224
1225 // Add all flags as volatile. Non volatile entries will not be overwritten
1226 // and all new volatile flags added will be consumed by the end of this call.
1227 SetFlags(packetTypes, true);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001228
Erik Språng61be2a42015-04-27 13:32:52 +02001229 if (packet_type_counter_.first_packet_time_ms == -1)
Erik Språng242e22b2015-05-11 10:17:43 +02001230 packet_type_counter_.first_packet_time_ms = clock_->TimeInMilliseconds();
Erik Språng61be2a42015-04-27 13:32:52 +02001231
Erik Språng242e22b2015-05-11 10:17:43 +02001232 bool generate_report;
1233 if (IsFlagPresent(kRtcpSr) || IsFlagPresent(kRtcpRr)) {
1234 // Report type already explicitly set, don't automatically populate.
1235 generate_report = true;
1236 DCHECK(ConsumeFlag(kRtcpReport) == false);
1237 } else {
1238 generate_report =
1239 (ConsumeFlag(kRtcpReport) && method_ == kRtcpNonCompound) ||
1240 method_ == kRtcpCompound;
1241 if (generate_report)
1242 SetFlag(sending_ ? kRtcpSr : kRtcpRr, true);
asapersson@webrtc.orgd08d3892014-12-16 12:03:11 +00001243 }
1244
Erik Språng0ea42d32015-06-25 14:46:16 +02001245 if (IsFlagPresent(kRtcpSr) || (IsFlagPresent(kRtcpRr) && !cname_.empty()))
Erik Språng242e22b2015-05-11 10:17:43 +02001246 SetFlag(kRtcpSdes, true);
1247
1248 // We need to send our NTP even if we haven't received any reports.
1249 clock_->CurrentNtp(context.ntp_sec, context.ntp_frac);
1250
1251 if (generate_report) {
1252 if (!sending_ && xr_send_receiver_reference_time_enabled_)
1253 SetFlag(kRtcpXrReceiverReferenceTime, true);
Erik Språng61be2a42015-04-27 13:32:52 +02001254 if (feedback_state.has_last_xr_rr)
Erik Språng242e22b2015-05-11 10:17:43 +02001255 SetFlag(kRtcpXrDlrrReportBlock, true);
1256
1257 // generate next time to send an RTCP report
Erik Språng61be2a42015-04-27 13:32:52 +02001258 // seeded from RTP constructor
1259 int32_t random = rand() % 1000;
1260 int32_t timeToNext = RTCP_INTERVAL_AUDIO_MS;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001261
Erik Språng242e22b2015-05-11 10:17:43 +02001262 if (audio_) {
Erik Språng61be2a42015-04-27 13:32:52 +02001263 timeToNext = (RTCP_INTERVAL_AUDIO_MS / 2) +
1264 (RTCP_INTERVAL_AUDIO_MS * random / 1000);
1265 } else {
1266 uint32_t minIntervalMs = RTCP_INTERVAL_AUDIO_MS;
Erik Språng242e22b2015-05-11 10:17:43 +02001267 if (sending_) {
Erik Språng61be2a42015-04-27 13:32:52 +02001268 // Calculate bandwidth for video; 360 / send bandwidth in kbit/s.
1269 uint32_t send_bitrate_kbit = feedback_state.send_bitrate / 1000;
1270 if (send_bitrate_kbit != 0)
1271 minIntervalMs = 360000 / send_bitrate_kbit;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001272 }
Erik Språng61be2a42015-04-27 13:32:52 +02001273 if (minIntervalMs > RTCP_INTERVAL_VIDEO_MS)
1274 minIntervalMs = RTCP_INTERVAL_VIDEO_MS;
1275 timeToNext = (minIntervalMs / 2) + (minIntervalMs * random / 1000);
1276 }
Erik Språng242e22b2015-05-11 10:17:43 +02001277 next_time_to_send_rtcp_ = clock_->TimeInMilliseconds() + timeToNext;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001278
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001279 StatisticianMap statisticians =
1280 receive_statistics_->GetActiveStatisticians();
1281 if (!statisticians.empty()) {
Erik Språng61be2a42015-04-27 13:32:52 +02001282 for (auto it = statisticians.begin(); it != statisticians.end(); ++it) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001283 RTCPReportBlock report_block;
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001284 if (PrepareReport(feedback_state, it->first, it->second,
1285 &report_block)) {
1286 AddReportBlock(report_block);
Erik Språng61be2a42015-04-27 13:32:52 +02001287 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001288 }
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001289 }
1290 }
1291
Erik Språng242e22b2015-05-11 10:17:43 +02001292 auto it = report_flags_.begin();
1293 while (it != report_flags_.end()) {
1294 auto builder = builders_.find(it->type);
1295 DCHECK(builder != builders_.end());
1296 if (it->is_volatile) {
1297 report_flags_.erase(it++);
1298 } else {
1299 ++it;
Erik Språng61be2a42015-04-27 13:32:52 +02001300 }
Erik Språng242e22b2015-05-11 10:17:43 +02001301
1302 uint32_t start_position = context.position;
Erik Språng0ea42d32015-06-25 14:46:16 +02001303 BuildResult result = (this->*(builder->second))(&context);
Erik Språng242e22b2015-05-11 10:17:43 +02001304 switch (result) {
1305 case BuildResult::kError:
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001306 return -1;
Erik Språng242e22b2015-05-11 10:17:43 +02001307 case BuildResult::kTruncated:
1308 return context.position;
1309 case BuildResult::kAborted:
1310 context.position = start_position;
1311 FALLTHROUGH();
1312 case BuildResult::kSuccess:
1313 continue;
Erik Språng61be2a42015-04-27 13:32:52 +02001314 default:
Erik Språng242e22b2015-05-11 10:17:43 +02001315 abort();
Erik Språng61be2a42015-04-27 13:32:52 +02001316 }
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001317 }
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001318
1319 if (packet_type_counter_observer_ != NULL) {
1320 packet_type_counter_observer_->RtcpPacketTypesCounterUpdated(
Erik Språng242e22b2015-05-11 10:17:43 +02001321 remote_ssrc_, packet_type_counter_);
pbos@webrtc.org1d0fa5d2015-02-19 12:47:00 +00001322 }
1323
Erik Språng242e22b2015-05-11 10:17:43 +02001324 DCHECK(AllVolatileFlagsConsumed());
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001325
Erik Språng242e22b2015-05-11 10:17:43 +02001326 return context.position;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001327}
1328
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001329bool RTCPSender::PrepareReport(const FeedbackState& feedback_state,
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001330 uint32_t ssrc,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001331 StreamStatistician* statistician,
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001332 RTCPReportBlock* report_block) {
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001333 // Do we have receive statistics to send?
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00001334 RtcpStatistics stats;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001335 if (!statistician->GetStatistics(&stats, true))
1336 return false;
1337 report_block->fractionLost = stats.fraction_lost;
1338 report_block->cumulativeLost = stats.cumulative_lost;
1339 report_block->extendedHighSeqNum =
1340 stats.extended_max_sequence_number;
1341 report_block->jitter = stats.jitter;
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001342 report_block->remoteSSRC = ssrc;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001343
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001344 // TODO(sprang): Do we really need separate time stamps for each report?
1345 // Get our NTP as late as possible to avoid a race.
1346 uint32_t ntp_secs;
1347 uint32_t ntp_frac;
1348 clock_->CurrentNtp(ntp_secs, ntp_frac);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001349
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001350 // Delay since last received report.
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001351 uint32_t delaySinceLastReceivedSR = 0;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001352 if ((feedback_state.last_rr_ntp_secs != 0) ||
1353 (feedback_state.last_rr_ntp_frac != 0)) {
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001354 // Get the 16 lowest bits of seconds and the 16 highest bits of fractions.
1355 uint32_t now = ntp_secs & 0x0000FFFF;
Erik Språng61be2a42015-04-27 13:32:52 +02001356 now <<= 16;
Erik Språngbdc0b0d2015-06-22 15:21:24 +02001357 now += (ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001358
Erik Språng61be2a42015-04-27 13:32:52 +02001359 uint32_t receiveTime = feedback_state.last_rr_ntp_secs & 0x0000FFFF;
1360 receiveTime <<= 16;
1361 receiveTime += (feedback_state.last_rr_ntp_frac & 0xffff0000) >> 16;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001362
1363 delaySinceLastReceivedSR = now-receiveTime;
1364 }
1365 report_block->delaySinceLastSR = delaySinceLastReceivedSR;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +00001366 report_block->lastSR = feedback_state.remote_sr;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00001367 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00001368}
1369
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001370int32_t RTCPSender::SendToNetwork(const uint8_t* dataBuffer, size_t length) {
Erik Språng242e22b2015-05-11 10:17:43 +02001371 CriticalSectionScoped lock(critical_section_transport_.get());
1372 if (cbTransport_) {
1373 if (cbTransport_->SendRTCPPacket(id_, dataBuffer, length) > 0)
Erik Språng61be2a42015-04-27 13:32:52 +02001374 return 0;
1375 }
1376 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001377}
1378
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001379void RTCPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
1380 assert(csrcs.size() <= kRtpCsrcSize);
Erik Språng242e22b2015-05-11 10:17:43 +02001381 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
pbos@webrtc.org9334ac22014-11-24 08:25:50 +00001382 csrcs_ = csrcs;
niklase@google.com470e71d2011-07-07 08:21:25 +00001383}
1384
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001385int32_t RTCPSender::SetApplicationSpecificData(uint8_t subType,
1386 uint32_t name,
1387 const uint8_t* data,
1388 uint16_t length) {
Erik Språng61be2a42015-04-27 13:32:52 +02001389 if (length % 4 != 0) {
1390 LOG(LS_ERROR) << "Failed to SetApplicationSpecificData.";
1391 return -1;
1392 }
Erik Språng242e22b2015-05-11 10:17:43 +02001393 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001394
Erik Språng242e22b2015-05-11 10:17:43 +02001395 SetFlag(kRtcpApp, true);
1396 app_sub_type_ = subType;
1397 app_name_ = name;
1398 app_data_.reset(new uint8_t[length]);
1399 app_length_ = length;
1400 memcpy(app_data_.get(), data, length);
Erik Språng61be2a42015-04-27 13:32:52 +02001401 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001402}
1403
Erik Språng61be2a42015-04-27 13:32:52 +02001404int32_t RTCPSender::SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) {
Erik Språng242e22b2015-05-11 10:17:43 +02001405 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
1406 memcpy(&xr_voip_metric_, VoIPMetric, sizeof(RTCPVoIPMetric));
niklase@google.com470e71d2011-07-07 08:21:25 +00001407
Erik Språng242e22b2015-05-11 10:17:43 +02001408 SetFlag(kRtcpXrVoipMetric, true);
Erik Språng61be2a42015-04-27 13:32:52 +02001409 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001410}
1411
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001412void RTCPSender::SendRtcpXrReceiverReferenceTime(bool enable) {
Erik Språng242e22b2015-05-11 10:17:43 +02001413 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
1414 xr_send_receiver_reference_time_enabled_ = enable;
asapersson@webrtc.org8469f7b2013-10-02 13:15:34 +00001415}
1416
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +00001417bool RTCPSender::RtcpXrReceiverReferenceTime() const {
Erik Språng242e22b2015-05-11 10:17:43 +02001418 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
1419 return xr_send_receiver_reference_time_enabled_;
asapersson@webrtc.org8d02f5d2013-11-21 08:57:04 +00001420}
1421
niklase@google.com470e71d2011-07-07 08:21:25 +00001422// no callbacks allowed inside this function
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00001423int32_t RTCPSender::SetTMMBN(const TMMBRSet* boundingSet,
1424 uint32_t maxBitrateKbit) {
Erik Språng242e22b2015-05-11 10:17:43 +02001425 CriticalSectionScoped lock(critical_section_rtcp_sender_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00001426
Erik Språng242e22b2015-05-11 10:17:43 +02001427 if (0 == tmmbr_help_.SetTMMBRBoundingSetToSend(boundingSet, maxBitrateKbit)) {
1428 SetFlag(kRtcpTmmbn, true);
Erik Språng61be2a42015-04-27 13:32:52 +02001429 return 0;
1430 }
1431 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001432}
Erik Språng61be2a42015-04-27 13:32:52 +02001433
Erik Språng242e22b2015-05-11 10:17:43 +02001434void RTCPSender::SetFlag(RTCPPacketType type, bool is_volatile) {
1435 report_flags_.insert(ReportFlag(type, is_volatile));
1436}
1437
1438void RTCPSender::SetFlags(const std::set<RTCPPacketType>& types,
1439 bool is_volatile) {
1440 for (RTCPPacketType type : types)
1441 SetFlag(type, is_volatile);
1442}
1443
1444bool RTCPSender::IsFlagPresent(RTCPPacketType type) const {
1445 return report_flags_.find(ReportFlag(type, false)) != report_flags_.end();
1446}
1447
1448bool RTCPSender::ConsumeFlag(RTCPPacketType type, bool forced) {
1449 auto it = report_flags_.find(ReportFlag(type, false));
1450 if (it == report_flags_.end())
1451 return false;
1452 if (it->is_volatile || forced)
1453 report_flags_.erase((it));
1454 return true;
1455}
1456
1457bool RTCPSender::AllVolatileFlagsConsumed() const {
1458 for (const ReportFlag& flag : report_flags_) {
1459 if (flag.is_volatile)
1460 return false;
1461 }
1462 return true;
1463}
1464
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00001465} // namespace webrtc