blob: b2c71eaa11a3755f1b4b4299e3ddc3f4445009c7 [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"
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +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
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +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.org2f446732013-04-08 11:08:41 +000036 void PushNACK(uint16_t nack);
edjee@google.com79b02892013-04-04 19:43:34 +000037 std::string GetResult();
38
39private:
40 std::ostringstream _stream;
41 int _count;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000042 uint16_t _prevNack;
edjee@google.com79b02892013-04-04 19:43:34 +000043 bool _consecutive;
44};
45
niklase@google.com470e71d2011-07-07 08:21:25 +000046class RTCPSender
47{
48public:
pbos@webrtc.org2f446732013-04-08 11:08:41 +000049 RTCPSender(const int32_t id, const bool audio,
stefan@webrtc.org717d1472013-07-10 13:39:27 +000050 Clock* clock, ModuleRtpRtcpImpl* owner,
51 ReceiveStatistics* receive_statistics);
niklase@google.com470e71d2011-07-07 08:21:25 +000052 virtual ~RTCPSender();
53
pbos@webrtc.org2f446732013-04-08 11:08:41 +000054 void ChangeUniqueId(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
pbos@webrtc.org2f446732013-04-08 11:08:41 +000056 int32_t Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000057
pbos@webrtc.org2f446732013-04-08 11:08:41 +000058 int32_t RegisterSendTransport(Transport* outgoingTransport);
niklase@google.com470e71d2011-07-07 08:21:25 +000059
60 RTCPMethod Status() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000061 int32_t SetRTCPStatus(const RTCPMethod method);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
63 bool Sending() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000064 int32_t SetSendingStatus(const bool enabled); // combine the functions
niklase@google.com470e71d2011-07-07 08:21:25 +000065
pbos@webrtc.org2f446732013-04-08 11:08:41 +000066 int32_t SetNackStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000068 void SetStartTimestamp(uint32_t start_timestamp);
69
70 void SetLastRtpTime(uint32_t rtp_timestamp,
71 int64_t capture_time_ms);
72
pbos@webrtc.org2f446732013-04-08 11:08:41 +000073 void SetSSRC( const uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +000075 void SetRemoteSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
stefan@webrtc.org7da34592013-04-09 14:56:29 +000077 int32_t SetCameraDelay(const int32_t delayMS);
78
pbos@webrtc.org2f446732013-04-08 11:08:41 +000079 int32_t CNAME(char cName[RTCP_CNAME_SIZE]);
80 int32_t SetCNAME(const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
pbos@webrtc.org2f446732013-04-08 11:08:41 +000082 int32_t AddMixedCNAME(const uint32_t SSRC,
83 const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pbos@webrtc.org2f446732013-04-08 11:08:41 +000085 int32_t RemoveMixedCNAME(const uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.org2f446732013-04-08 11:08:41 +000087 uint32_t SendTimeOfSendReport(const uint32_t sendReport);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
89 bool TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP = false) const;
90
pbos@webrtc.org2f446732013-04-08 11:08:41 +000091 uint32_t LastSendReport(uint32_t& lastRTCPTime);
niklase@google.com470e71d2011-07-07 08:21:25 +000092
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +000093 int32_t SendRTCP(
94 uint32_t rtcpPacketTypeFlags,
stefan@webrtc.org66b2e5c2013-07-05 14:30:48 +000095 int32_t nackSize = 0,
96 const uint16_t* nackList = 0,
97 bool repeat = false,
98 uint64_t pictureID = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000100 int32_t AddExternalReportBlock(
101 uint32_t SSRC,
102 const RTCPReportBlock* receiveBlock);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000104 int32_t RemoveExternalReportBlock(uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
106 /*
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000107 * REMB
108 */
109 bool REMB() const;
110
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000111 int32_t SetREMBStatus(const bool enable);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000112
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000113 int32_t SetREMBData(const uint32_t bitrate,
114 const uint8_t numberOfSSRC,
115 const uint32_t* SSRC);
mflodman@webrtc.org84dc3d12011-12-22 10:26:13 +0000116
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000117 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 * TMMBR
119 */
120 bool TMMBR() const;
121
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000122 int32_t SetTMMBRStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000124 int32_t SetTMMBN(const TMMBRSet* boundingSet,
125 const uint32_t maxBitrateKbit);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
niklase@google.com470e71d2011-07-07 08:21:25 +0000127 /*
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000128 * Extended jitter report
129 */
130 bool IJ() const;
131
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000132 int32_t SetIJStatus(const bool enable);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000133
134 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 *
136 */
137
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000138 int32_t SetApplicationSpecificData(const uint8_t subType,
139 const uint32_t name,
140 const uint8_t* data,
141 const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000143 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000145 int32_t SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
146 const uint8_t arrLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000148 int32_t SetCSRCStatus(const bool include);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000150 void SetTargetBitrate(unsigned int target_bitrate);
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000151
niklase@google.com470e71d2011-07-07 08:21:25 +0000152private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000153 int32_t SendToNetwork(const uint8_t* dataBuffer, const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
155 void UpdatePacketRate();
156
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000157 int32_t WriteAllReportBlocksToBuffer(uint8_t* rtcpbuffer,
158 int pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000159 uint8_t& numberOfReportBlocks,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000160 const uint32_t NTPsec,
161 const uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000163 int32_t WriteReportBlocksToBuffer(
164 uint8_t* rtcpbuffer,
165 int32_t position,
166 const std::map<uint32_t, RTCPReportBlock*>& report_blocks);
167
168 int32_t AddReportBlock(
169 uint32_t SSRC,
170 std::map<uint32_t, RTCPReportBlock*>* report_blocks,
171 const RTCPReportBlock* receiveBlock);
172
173 bool PrepareReport(StreamStatistician* statistician,
174 RTCPReportBlock* report_block,
175 uint32_t* ntp_secs, uint32_t* ntp_frac);
176
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000177 int32_t BuildSR(uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000178 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000179 const uint32_t NTPsec,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000180 const uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000182 int32_t BuildRR(uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000183 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000184 const uint32_t NTPsec,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000185 const uint32_t NTPfrac);
186
187 int PrepareRTCP(
188 uint32_t packetTypeFlags,
189 int32_t nackSize,
190 const uint16_t* nackList,
191 bool repeat,
192 uint64_t pictureID,
193 uint8_t* rtcp_buffer,
194 int buffer_size);
195
196 bool ShouldSendReportBlocks(uint32_t rtcp_packet_type) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000198 int32_t BuildExtendedJitterReport(
199 uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000200 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000201 const uint32_t jitterTransmissionTimeOffset);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000202
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000203 int32_t BuildSDEC(uint8_t* rtcpbuffer, int& pos);
204 int32_t BuildPLI(uint8_t* rtcpbuffer, int& pos);
205 int32_t BuildREMB(uint8_t* rtcpbuffer, int& pos);
206 int32_t BuildTMMBR(uint8_t* rtcpbuffer, int& pos);
207 int32_t BuildTMMBN(uint8_t* rtcpbuffer, int& pos);
208 int32_t BuildAPP(uint8_t* rtcpbuffer, int& pos);
209 int32_t BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos);
210 int32_t BuildBYE(uint8_t* rtcpbuffer, int& pos);
211 int32_t BuildFIR(uint8_t* rtcpbuffer, int& pos, bool repeat);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000212 int32_t BuildSLI(uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000213 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000214 const uint8_t pictureID);
215 int32_t BuildRPSI(uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000216 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000217 const uint64_t pictureID,
218 const uint8_t payloadType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000219
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000220 int32_t BuildNACK(uint8_t* rtcpbuffer,
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000221 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000222 const int32_t nackSize,
223 const uint16_t* nackList,
edjee@google.com79b02892013-04-04 19:43:34 +0000224 std::string* nackString);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225
226private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000227 int32_t _id;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000228 const bool _audio;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000229 Clock* _clock;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000230 RTCPMethod _method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000232 ModuleRtpRtcpImpl& _rtpRtcp;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000234 CriticalSectionWrapper* _criticalSectionTransport;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000235 Transport* _cbTransport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000237 CriticalSectionWrapper* _criticalSectionRTCPSender;
niklase@google.com470e71d2011-07-07 08:21:25 +0000238 bool _usingNack;
239 bool _sending;
240 bool _sendTMMBN;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000241 bool _REMB;
242 bool _sendREMB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 bool _TMMBR;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000244 bool _IJ;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000246 int64_t _nextTimeToSendRTCP;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000248 uint32_t start_timestamp_;
249 uint32_t last_rtp_timestamp_;
250 int64_t last_frame_capture_time_ms_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000251 uint32_t _SSRC;
252 uint32_t _remoteSSRC; // SSRC that we receive on our RTP channel
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000253 char _CNAME[RTCP_CNAME_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
stefan@webrtc.org717d1472013-07-10 13:39:27 +0000255
256 ReceiveStatistics* receive_statistics_;
257 std::map<uint32_t, RTCPReportBlock*> internal_report_blocks_;
258 std::map<uint32_t, RTCPReportBlock*> external_report_blocks_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000259 std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _csrcCNAMEs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000260
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000261 int32_t _cameraDelayMS;
262
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 // Sent
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000264 uint32_t _lastSendReport[RTCP_NUMBER_OF_SR]; // allow packet loss and RTT above 1 sec
265 uint32_t _lastRTCPTime[RTCP_NUMBER_OF_SR];
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
267 // send CSRCs
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000268 uint8_t _CSRCs;
269 uint32_t _CSRC[kRtpCsrcSize];
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 bool _includeCSRCs;
271
272 // Full intra request
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000273 uint8_t _sequenceNumberFIR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000275 // REMB
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000276 uint8_t _lengthRembSSRC;
277 uint8_t _sizeRembSSRC;
278 uint32_t* _rembSSRC;
279 uint32_t _rembBitrate;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000280
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 TMMBRHelp _tmmbrHelp;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000282 uint32_t _tmmbr_Send;
283 uint32_t _packetOH_Send;
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
285 // APP
286 bool _appSend;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000287 uint8_t _appSubType;
288 uint32_t _appName;
289 uint8_t* _appData;
290 uint16_t _appLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
292 // XR VoIP metric
293 bool _xrSendVoIPMetric;
294 RTCPVoIPMetric _xrVoIPMetric;
edjee@google.com79b02892013-04-04 19:43:34 +0000295
296 // Counters
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000297 uint32_t _nackCount;
298 uint32_t _pliCount;
299 uint32_t _fullIntraRequestCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000300};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000301} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000302
303#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_