blob: 909c15d231c254dd5b9eb78f1e98bfb60532955c [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;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000031class RTCPReceiver;
pwestin@webrtc.org741da942011-09-20 13:52:04 +000032
edjee@google.com79b02892013-04-04 19:43:34 +000033class NACKStringBuilder
34{
35public:
36 NACKStringBuilder();
pbos@webrtc.orgf3e4cee2013-07-31 15:17:19 +000037 ~NACKStringBuilder();
38
pbos@webrtc.org2f446732013-04-08 11:08:41 +000039 void PushNACK(uint16_t nack);
edjee@google.com79b02892013-04-04 19:43:34 +000040 std::string GetResult();
41
42private:
43 std::ostringstream _stream;
44 int _count;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000045 uint16_t _prevNack;
edjee@google.com79b02892013-04-04 19:43:34 +000046 bool _consecutive;
47};
48
niklase@google.com470e71d2011-07-07 08:21:25 +000049class RTCPSender
50{
51public:
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000052 struct FeedbackState {
53 explicit FeedbackState(ModuleRtpRtcpImpl* module);
54 FeedbackState();
55
56 uint8_t send_payload_type;
57 uint32_t frequency_hz;
58 uint32_t packet_count_sent;
59 uint32_t byte_count_sent;
60 uint32_t send_bitrate;
61
62 uint32_t last_rr_ntp_secs;
63 uint32_t last_rr_ntp_frac;
64 uint32_t remote_sr;
65
66 // Used when generating TMMBR.
67 ModuleRtpRtcpImpl* module;
68 };
pbos@webrtc.org2f446732013-04-08 11:08:41 +000069 RTCPSender(const int32_t id, const bool audio,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000070 Clock* clock,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +000071 ReceiveStatistics* receive_statistics);
niklase@google.com470e71d2011-07-07 08:21:25 +000072 virtual ~RTCPSender();
73
pbos@webrtc.org2f446732013-04-08 11:08:41 +000074 void ChangeUniqueId(const int32_t id);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pbos@webrtc.org2f446732013-04-08 11:08:41 +000076 int32_t Init();
niklase@google.com470e71d2011-07-07 08:21:25 +000077
pbos@webrtc.org2f446732013-04-08 11:08:41 +000078 int32_t RegisterSendTransport(Transport* outgoingTransport);
niklase@google.com470e71d2011-07-07 08:21:25 +000079
80 RTCPMethod Status() const;
pbos@webrtc.org2f446732013-04-08 11:08:41 +000081 int32_t SetRTCPStatus(const RTCPMethod method);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
83 bool Sending() const;
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +000084 int32_t SetSendingStatus(const FeedbackState& feedback_state,
85 bool enabled); // combine the functions
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.org2f446732013-04-08 11:08:41 +000087 int32_t SetNackStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +000089 void SetStartTimestamp(uint32_t start_timestamp);
90
91 void SetLastRtpTime(uint32_t rtp_timestamp,
92 int64_t capture_time_ms);
93
pbos@webrtc.org2f446732013-04-08 11:08:41 +000094 void SetSSRC( const uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000095
wu@webrtc.org822fbd82013-08-15 23:38:54 +000096 void SetRemoteSSRC(uint32_t ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
stefan@webrtc.org7da34592013-04-09 14:56:29 +000098 int32_t SetCameraDelay(const int32_t delayMS);
99
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000100 int32_t CNAME(char cName[RTCP_CNAME_SIZE]);
101 int32_t SetCNAME(const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000103 int32_t AddMixedCNAME(const uint32_t SSRC,
104 const char cName[RTCP_CNAME_SIZE]);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000106 int32_t RemoveMixedCNAME(const uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000108 uint32_t SendTimeOfSendReport(const uint32_t sendReport);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
110 bool TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP = false) const;
111
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000112 uint32_t LastSendReport(uint32_t& lastRTCPTime);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000114 int32_t SendRTCP(
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000115 const FeedbackState& feedback_state,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000116 uint32_t rtcpPacketTypeFlags,
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117 int32_t nackSize = 0,
118 const uint16_t* nackList = 0,
119 bool repeat = false,
120 uint64_t pictureID = 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000122 int32_t AddExternalReportBlock(
123 uint32_t SSRC,
124 const RTCPReportBlock* receiveBlock);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000126 int32_t RemoveExternalReportBlock(uint32_t SSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
128 /*
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000129 * REMB
130 */
131 bool REMB() const;
132
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000133 int32_t SetREMBStatus(const bool enable);
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000134
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000135 int32_t SetREMBData(const uint32_t bitrate,
136 const uint8_t numberOfSSRC,
137 const uint32_t* SSRC);
mflodman@webrtc.org84dc3d12011-12-22 10:26:13 +0000138
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000139 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000140 * TMMBR
141 */
142 bool TMMBR() const;
143
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000144 int32_t SetTMMBRStatus(const bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000146 int32_t SetTMMBN(const TMMBRSet* boundingSet,
147 const uint32_t maxBitrateKbit);
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 /*
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000150 * Extended jitter report
151 */
152 bool IJ() const;
153
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000154 int32_t SetIJStatus(const bool enable);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000155
156 /*
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 *
158 */
159
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000160 int32_t SetApplicationSpecificData(const uint8_t subType,
161 const uint32_t name,
162 const uint8_t* data,
163 const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000165 int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000167 int32_t SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
168 const uint8_t arrLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000170 int32_t SetCSRCStatus(const bool include);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
stefan@webrtc.org9354cc92012-06-07 08:10:14 +0000172 void SetTargetBitrate(unsigned int target_bitrate);
mflodman@webrtc.org117c1192012-01-13 08:52:58 +0000173
niklase@google.com470e71d2011-07-07 08:21:25 +0000174private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000175 int32_t SendToNetwork(const uint8_t* dataBuffer, const uint16_t length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
177 void UpdatePacketRate();
178
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000179 int32_t WriteAllReportBlocksToBuffer(uint8_t* rtcpbuffer,
180 int pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000181 uint8_t& numberOfReportBlocks,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000182 const uint32_t NTPsec,
183 const uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000185 int32_t WriteReportBlocksToBuffer(
186 uint8_t* rtcpbuffer,
187 int32_t position,
188 const std::map<uint32_t, RTCPReportBlock*>& report_blocks);
189
190 int32_t AddReportBlock(
191 uint32_t SSRC,
192 std::map<uint32_t, RTCPReportBlock*>* report_blocks,
193 const RTCPReportBlock* receiveBlock);
194
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000195 bool PrepareReport(const FeedbackState& feedback_state,
196 StreamStatistician* statistician,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000197 RTCPReportBlock* report_block,
198 uint32_t* ntp_secs, uint32_t* ntp_frac);
199
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000200 int32_t BuildSR(const FeedbackState& feedback_state,
201 uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000202 int& pos,
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000203 uint32_t NTPsec,
204 uint32_t NTPfrac);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000206 int32_t BuildRR(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000207 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000208 const uint32_t NTPsec,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000209 const uint32_t NTPfrac);
210
211 int PrepareRTCP(
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000212 const FeedbackState& feedback_state,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000213 uint32_t packetTypeFlags,
214 int32_t nackSize,
215 const uint16_t* nackList,
216 bool repeat,
217 uint64_t pictureID,
218 uint8_t* rtcp_buffer,
219 int buffer_size);
220
221 bool ShouldSendReportBlocks(uint32_t rtcp_packet_type) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000223 int32_t BuildExtendedJitterReport(
224 uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000225 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000226 const uint32_t jitterTransmissionTimeOffset);
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000227
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000228 int32_t BuildSDEC(uint8_t* rtcpbuffer, int& pos);
229 int32_t BuildPLI(uint8_t* rtcpbuffer, int& pos);
230 int32_t BuildREMB(uint8_t* rtcpbuffer, int& pos);
pbos@webrtc.org59f20bb2013-09-09 16:02:19 +0000231 int32_t BuildTMMBR(ModuleRtpRtcpImpl* module,
232 uint8_t* rtcpbuffer,
233 int& pos);
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000234 int32_t BuildTMMBN(uint8_t* rtcpbuffer, int& pos);
235 int32_t BuildAPP(uint8_t* rtcpbuffer, int& pos);
236 int32_t BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos);
237 int32_t BuildBYE(uint8_t* rtcpbuffer, int& pos);
238 int32_t BuildFIR(uint8_t* rtcpbuffer, int& pos, bool repeat);
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000239 int32_t BuildSLI(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000240 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000241 const uint8_t pictureID);
242 int32_t BuildRPSI(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000243 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000244 const uint64_t pictureID,
245 const uint8_t payloadType);
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000247 int32_t BuildNACK(uint8_t* rtcpbuffer,
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000248 int& pos,
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000249 const int32_t nackSize,
250 const uint16_t* nackList,
edjee@google.com79b02892013-04-04 19:43:34 +0000251 std::string* nackString);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
253private:
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000254 int32_t _id;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000255 const bool _audio;
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000256 Clock* _clock;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000257 RTCPMethod _method;
niklase@google.com470e71d2011-07-07 08:21:25 +0000258
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000259 CriticalSectionWrapper* _criticalSectionTransport;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000260 Transport* _cbTransport;
niklase@google.com470e71d2011-07-07 08:21:25 +0000261
henrike@webrtc.org65573f22011-12-13 19:17:27 +0000262 CriticalSectionWrapper* _criticalSectionRTCPSender;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263 bool _usingNack;
264 bool _sending;
265 bool _sendTMMBN;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000266 bool _REMB;
267 bool _sendREMB;
niklase@google.com470e71d2011-07-07 08:21:25 +0000268 bool _TMMBR;
asapersson@webrtc.org5249cc82011-12-16 14:31:37 +0000269 bool _IJ;
niklase@google.com470e71d2011-07-07 08:21:25 +0000270
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000271 int64_t _nextTimeToSendRTCP;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272
stefan@webrtc.org7c3523c2012-09-11 07:00:42 +0000273 uint32_t start_timestamp_;
274 uint32_t last_rtp_timestamp_;
275 int64_t last_frame_capture_time_ms_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000276 uint32_t _SSRC;
277 uint32_t _remoteSSRC; // SSRC that we receive on our RTP channel
pwestin@webrtc.org26f8d9c2012-01-19 15:53:09 +0000278 char _CNAME[RTCP_CNAME_SIZE];
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000280
281 ReceiveStatistics* receive_statistics_;
282 std::map<uint32_t, RTCPReportBlock*> internal_report_blocks_;
283 std::map<uint32_t, RTCPReportBlock*> external_report_blocks_;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000284 std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _csrcCNAMEs;
niklase@google.com470e71d2011-07-07 08:21:25 +0000285
stefan@webrtc.org7da34592013-04-09 14:56:29 +0000286 int32_t _cameraDelayMS;
287
niklase@google.com470e71d2011-07-07 08:21:25 +0000288 // Sent
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000289 uint32_t _lastSendReport[RTCP_NUMBER_OF_SR]; // allow packet loss and RTT above 1 sec
290 uint32_t _lastRTCPTime[RTCP_NUMBER_OF_SR];
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
292 // send CSRCs
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000293 uint8_t _CSRCs;
294 uint32_t _CSRC[kRtpCsrcSize];
niklase@google.com470e71d2011-07-07 08:21:25 +0000295 bool _includeCSRCs;
296
297 // Full intra request
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000298 uint8_t _sequenceNumberFIR;
niklase@google.com470e71d2011-07-07 08:21:25 +0000299
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000300 // REMB
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000301 uint8_t _lengthRembSSRC;
302 uint8_t _sizeRembSSRC;
303 uint32_t* _rembSSRC;
304 uint32_t _rembBitrate;
pwestin@webrtc.org741da942011-09-20 13:52:04 +0000305
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 TMMBRHelp _tmmbrHelp;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000307 uint32_t _tmmbr_Send;
308 uint32_t _packetOH_Send;
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
310 // APP
311 bool _appSend;
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000312 uint8_t _appSubType;
313 uint32_t _appName;
314 uint8_t* _appData;
315 uint16_t _appLength;
niklase@google.com470e71d2011-07-07 08:21:25 +0000316
317 // XR VoIP metric
318 bool _xrSendVoIPMetric;
319 RTCPVoIPMetric _xrVoIPMetric;
edjee@google.com79b02892013-04-04 19:43:34 +0000320
321 // Counters
pbos@webrtc.org2f446732013-04-08 11:08:41 +0000322 uint32_t _nackCount;
323 uint32_t _pliCount;
324 uint32_t _fullIntraRequestCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000325};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000326} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
328#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_