blob: 06c14ce0e30a5e7d2b2bdf598ea461e2a491a479 [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
11#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
13
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000014#include <map>
edjee@google.com79b02892013-04-04 19:43:34 +000015#include <sstream>
16#include <string>
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +000017
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000018#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
19#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000020#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
pbos@webrtc.orga048d7c2013-05-29 14:27:38 +000021#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
22#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
23#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
24#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
25#include "webrtc/system_wrappers/interface/scoped_ptr.h"
26#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
pwestin@webrtc.org741da942011-09-20 13:52:04 +000029
wu@webrtc.org822fbd82013-08-15 23:38:54 +000030class ModuleRtpRtcpImpl;
pwestin@webrtc.org741da942011-09-20 13:52:04 +000031
edjee@google.com79b02892013-04-04 19:43:34 +000032class NACKStringBuilder
33{
34public:
35 NACKStringBuilder();
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000036 ~NACKStringBuilder();
37
pbos@webrtc.org2f446732013-04-08 11:08:41 +000038 void PushNACK(uint16_t nack);
edjee@google.com79b02892013-04-04 19:43:34 +000039 std::string GetResult();
40
41private:
42 std::ostringstream _stream;
43 int _count;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000044 uint16_t _prevNack;
edjee@google.com79b02892013-04-04 19:43:34 +000045 bool _consecutive;
46};
47
niklase@google.com470e71d2011-07-07 08:21:25 +000048class RTCPSender
49{
50public:
pbos@webrtc.org2f446732013-04-08 11:08:41 +000051 RTCPSender(const int32_t id, const bool audio,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000052 Clock* clock, ModuleRtpRtcpImpl* owner,
53 ReceiveStatistics* receive_statistics);
niklase@google.com470e71d2011-07-07 08:21:25 +000054 virtual ~RTCPSender();
55
pbos@webrtc.org2f446732013-04-08 11:08:41 +000056 void ChangeUniqueId(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
pbos@webrtc.org2f446732013-04-08 11:08:41 +000058 int32_t Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000059
pbos@webrtc.org2f446732013-04-08 11:08:41 +000060 int32_t RegisterSendTransport(Transport* outgoingTransport);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
62 RTCPMethod Status() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000063 int32_t SetRTCPStatus(const RTCPMethod method);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
65 bool Sending() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 int32_t SetSendingStatus(const bool enabled); // combine the functions
niklase@google.com470e71d2011-07-07 08:21:25 +000067
pbos@webrtc.org2f446732013-04-08 11:08:41 +000068 int32_t SetNackStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000069
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000070 void SetStartTimestamp(uint32_t start_timestamp);
71
72 void SetLastRtpTime(uint32_t rtp_timestamp,
73 int64_t capture_time_ms);
74
pbos@webrtc.org2f446732013-04-08 11:08:41 +000075 void SetSSRC( const uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
wu@webrtc.org822fbd82013-08-15 23:38:54 +000077 void SetRemoteSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
stefan@webrtc.org7da34592013-04-09 14:56:29 +000079 int32_t SetCameraDelay(const int32_t delayMS);
80
pbos@webrtc.org2f446732013-04-08 11:08:41 +000081 int32_t CNAME(char cName[RTCP_CNAME_SIZE]);
82 int32_t SetCNAME(const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
pbos@webrtc.org2f446732013-04-08 11:08:41 +000084 int32_t AddMixedCNAME(const uint32_t SSRC,
85 const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.org2f446732013-04-08 11:08:41 +000087 int32_t RemoveMixedCNAME(const uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pbos@webrtc.org2f446732013-04-08 11:08:41 +000089 uint32_t SendTimeOfSendReport(const uint32_t sendReport);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
91 bool TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP = false) const;
92
pbos@webrtc.org2f446732013-04-08 11:08:41 +000093 uint32_t LastSendReport(uint32_t& lastRTCPTime);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
wu@webrtc.org822fbd82013-08-15 23:38:54 +000095 int32_t SendRTCP(
96 uint32_t rtcpPacketTypeFlags,
wu@webrtc.org822fbd82013-08-15 23:38:54 +000097 int32_t nackSize = 0,
98 const uint16_t* nackList = 0,
99 bool repeat = false,
100 uint64_t pictureID = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000102 int32_t AddExternalReportBlock(
103 uint32_t SSRC,
104 const RTCPReportBlock* receiveBlock);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000106 int32_t RemoveExternalReportBlock(uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
108 /*
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000109 * REMB
110 */
111 bool REMB() const;
112
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000113 int32_t SetREMBStatus(const bool enable);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000114
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000115 int32_t SetREMBData(const uint32_t bitrate,
116 const uint8_t numberOfSSRC,
117 const uint32_t* SSRC);
mflodman@webrtc.org84dc3d12011-12-22 10:26:13 +0000118
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000119 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 * TMMBR
121 */
122 bool TMMBR() const;
123
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000124 int32_t SetTMMBRStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000126 int32_t SetTMMBN(const TMMBRSet* boundingSet,
127 const uint32_t maxBitrateKbit);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 /*
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000130 * Extended jitter report
131 */
132 bool IJ() const;
133
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000134 int32_t SetIJStatus(const bool enable);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000135
136 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 *
138 */
139
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000140 int32_t SetApplicationSpecificData(const uint8_t subType,
141 const uint32_t name,
142 const uint8_t* data,
143 const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000147 int32_t SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
148 const uint8_t arrLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000150 int32_t SetCSRCStatus(const bool include);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000152 void SetTargetBitrate(unsigned int target_bitrate);
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000153
niklase@google.com470e71d2011-07-07 08:21:25 +0000154private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000155 int32_t SendToNetwork(const uint8_t* dataBuffer, const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156
157 void UpdatePacketRate();
158
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000159 int32_t WriteAllReportBlocksToBuffer(uint8_t* rtcpbuffer,
160 int pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000161 uint8_t& numberOfReportBlocks,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000162 const uint32_t NTPsec,
163 const uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000165 int32_t WriteReportBlocksToBuffer(
166 uint8_t* rtcpbuffer,
167 int32_t position,
168 const std::map<uint32_t, RTCPReportBlock*>& report_blocks);
169
170 int32_t AddReportBlock(
171 uint32_t SSRC,
172 std::map<uint32_t, RTCPReportBlock*>* report_blocks,
173 const RTCPReportBlock* receiveBlock);
174
175 bool PrepareReport(StreamStatistician* statistician,
176 RTCPReportBlock* report_block,
177 uint32_t* ntp_secs, uint32_t* ntp_frac);
178
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000179 int32_t BuildSR(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000180 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000181 const uint32_t NTPsec,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000182 const uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000184 int32_t BuildRR(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000185 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000186 const uint32_t NTPsec,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000187 const uint32_t NTPfrac);
188
189 int PrepareRTCP(
190 uint32_t packetTypeFlags,
191 int32_t nackSize,
192 const uint16_t* nackList,
193 bool repeat,
194 uint64_t pictureID,
195 uint8_t* rtcp_buffer,
196 int buffer_size);
197
198 bool ShouldSendReportBlocks(uint32_t rtcp_packet_type) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000200 int32_t BuildExtendedJitterReport(
201 uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000202 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000203 const uint32_t jitterTransmissionTimeOffset);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000204
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000205 int32_t BuildSDEC(uint8_t* rtcpbuffer, int& pos);
206 int32_t BuildPLI(uint8_t* rtcpbuffer, int& pos);
207 int32_t BuildREMB(uint8_t* rtcpbuffer, int& pos);
208 int32_t BuildTMMBR(uint8_t* rtcpbuffer, int& pos);
209 int32_t BuildTMMBN(uint8_t* rtcpbuffer, int& pos);
210 int32_t BuildAPP(uint8_t* rtcpbuffer, int& pos);
211 int32_t BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos);
212 int32_t BuildBYE(uint8_t* rtcpbuffer, int& pos);
213 int32_t BuildFIR(uint8_t* rtcpbuffer, int& pos, bool repeat);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214 int32_t BuildSLI(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000215 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000216 const uint8_t pictureID);
217 int32_t BuildRPSI(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000218 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000219 const uint64_t pictureID,
220 const uint8_t payloadType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222 int32_t BuildNACK(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000223 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000224 const int32_t nackSize,
225 const uint16_t* nackList,
edjee@google.com79b02892013-04-04 19:43:34 +0000226 std::string* nackString);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227
228private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000229 int32_t _id;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000230 const bool _audio;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000231 Clock* _clock;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000232 RTCPMethod _method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000234 ModuleRtpRtcpImpl& _rtpRtcp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000236 CriticalSectionWrapper* _criticalSectionTransport;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000237 Transport* _cbTransport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000239 CriticalSectionWrapper* _criticalSectionRTCPSender;
niklase@google.com470e71d2011-07-07 08:21:25 +0000240 bool _usingNack;
241 bool _sending;
242 bool _sendTMMBN;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000243 bool _REMB;
244 bool _sendREMB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 bool _TMMBR;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000246 bool _IJ;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000248 int64_t _nextTimeToSendRTCP;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000250 uint32_t start_timestamp_;
251 uint32_t last_rtp_timestamp_;
252 int64_t last_frame_capture_time_ms_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000253 uint32_t _SSRC;
254 uint32_t _remoteSSRC; // SSRC that we receive on our RTP channel
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000255 char _CNAME[RTCP_CNAME_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +0000256
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000257
258 ReceiveStatistics* receive_statistics_;
259 std::map<uint32_t, RTCPReportBlock*> internal_report_blocks_;
260 std::map<uint32_t, RTCPReportBlock*> external_report_blocks_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000261 std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _csrcCNAMEs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000263 int32_t _cameraDelayMS;
264
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 // Sent
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000266 uint32_t _lastSendReport[RTCP_NUMBER_OF_SR]; // allow packet loss and RTT above 1 sec
267 uint32_t _lastRTCPTime[RTCP_NUMBER_OF_SR];
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
269 // send CSRCs
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000270 uint8_t _CSRCs;
271 uint32_t _CSRC[kRtpCsrcSize];
niklase@google.com470e71d2011-07-07 08:21:25 +0000272 bool _includeCSRCs;
273
274 // Full intra request
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000275 uint8_t _sequenceNumberFIR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000277 // REMB
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000278 uint8_t _lengthRembSSRC;
279 uint8_t _sizeRembSSRC;
280 uint32_t* _rembSSRC;
281 uint32_t _rembBitrate;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000282
niklase@google.com470e71d2011-07-07 08:21:25 +0000283 TMMBRHelp _tmmbrHelp;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284 uint32_t _tmmbr_Send;
285 uint32_t _packetOH_Send;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
287 // APP
288 bool _appSend;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000289 uint8_t _appSubType;
290 uint32_t _appName;
291 uint8_t* _appData;
292 uint16_t _appLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
294 // XR VoIP metric
295 bool _xrSendVoIPMetric;
296 RTCPVoIPMetric _xrVoIPMetric;
edjee@google.com79b02892013-04-04 19:43:34 +0000297
298 // Counters
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000299 uint32_t _nackCount;
300 uint32_t _pliCount;
301 uint32_t _fullIntraRequestCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000303} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000304
305#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_