summaryrefslogtreecommitdiff
path: root/modules/rtp_rtcp/source/rtcp_sender.h
blob: fad3b5e3329da66841f0d533f8b66ba66892c12c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_

#include <map>
#include <sstream>
#include <string>

#include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
#include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
#include "webrtc/system_wrappers/interface/scoped_ptr.h"
#include "webrtc/system_wrappers/interface/thread_annotations.h"
#include "webrtc/typedefs.h"

namespace webrtc {

class ModuleRtpRtcpImpl;
class RTCPReceiver;

class NACKStringBuilder
{
public:
    NACKStringBuilder();
    ~NACKStringBuilder();

    void PushNACK(uint16_t nack);
    std::string GetResult();

private:
    std::ostringstream _stream;
    int _count;
    uint16_t _prevNack;
    bool _consecutive;
};

class RTCPSender
{
public:
 struct FeedbackState {
   explicit FeedbackState(ModuleRtpRtcpImpl* module);
   FeedbackState();

   uint8_t send_payload_type;
   uint32_t frequency_hz;
   uint32_t packet_count_sent;
   uint32_t byte_count_sent;
   uint32_t send_bitrate;

   uint32_t last_rr_ntp_secs;
   uint32_t last_rr_ntp_frac;
   uint32_t remote_sr;

   bool has_last_xr_rr;
   RtcpReceiveTimeInfo last_xr_rr;

   // Used when generating TMMBR.
   ModuleRtpRtcpImpl* module;
 };
    RTCPSender(const int32_t id, const bool audio,
               Clock* clock,
               ReceiveStatistics* receive_statistics);
    virtual ~RTCPSender();

    int32_t RegisterSendTransport(Transport* outgoingTransport);

    RTCPMethod Status() const;
    int32_t SetRTCPStatus(const RTCPMethod method);

    bool Sending() const;
    int32_t SetSendingStatus(const FeedbackState& feedback_state,
                             bool enabled);  // combine the functions

    int32_t SetNackStatus(const bool enable);

    void SetStartTimestamp(uint32_t start_timestamp);

    void SetLastRtpTime(uint32_t rtp_timestamp,
                        int64_t capture_time_ms);

    void SetSSRC( const uint32_t ssrc);

    void SetRemoteSSRC(uint32_t ssrc);

    int32_t SetCameraDelay(const int32_t delayMS);

    int32_t SetCNAME(const char cName[RTCP_CNAME_SIZE]);

    int32_t AddMixedCNAME(const uint32_t SSRC,
                          const char cName[RTCP_CNAME_SIZE]);

    int32_t RemoveMixedCNAME(const uint32_t SSRC);

    uint32_t SendTimeOfSendReport(const uint32_t sendReport);

    bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const;

    bool TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP = false) const;

    uint32_t LastSendReport(uint32_t& lastRTCPTime);

    int32_t SendRTCP(
        const FeedbackState& feedback_state,
        uint32_t rtcpPacketTypeFlags,
        int32_t nackSize = 0,
        const uint16_t* nackList = 0,
        bool repeat = false,
        uint64_t pictureID = 0);

    int32_t AddExternalReportBlock(
            uint32_t SSRC,
            const RTCPReportBlock* receiveBlock);

    int32_t RemoveExternalReportBlock(uint32_t SSRC);

    /*
    *  REMB
    */
    bool REMB() const;

    int32_t SetREMBStatus(const bool enable);

    int32_t SetREMBData(const uint32_t bitrate,
                        const uint8_t numberOfSSRC,
                        const uint32_t* SSRC);

    /*
    *   TMMBR
    */
    bool TMMBR() const;

    int32_t SetTMMBRStatus(const bool enable);

    int32_t SetTMMBN(const TMMBRSet* boundingSet,
                     const uint32_t maxBitrateKbit);

    /*
    *   Extended jitter report
    */
    bool IJ() const;

    int32_t SetIJStatus(const bool enable);

    /*
    *
    */

    int32_t SetApplicationSpecificData(const uint8_t subType,
                                       const uint32_t name,
                                       const uint8_t* data,
                                       const uint16_t length);

    int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);

    void SendRtcpXrReceiverReferenceTime(bool enable);

    bool RtcpXrReceiverReferenceTime() const;

    int32_t SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
                     const uint8_t arrLength);

    int32_t SetCSRCStatus(const bool include);

    void SetTargetBitrate(unsigned int target_bitrate);

    void GetPacketTypeCounter(RtcpPacketTypeCounter* packet_counter) const;

private:
    int32_t SendToNetwork(const uint8_t* dataBuffer, const uint16_t length);

    int32_t WriteAllReportBlocksToBuffer(uint8_t* rtcpbuffer,
                            int pos,
                            uint8_t& numberOfReportBlocks,
                            const uint32_t NTPsec,
                            const uint32_t NTPfrac)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

    int32_t WriteReportBlocksToBuffer(
        uint8_t* rtcpbuffer,
        int32_t position,
        const std::map<uint32_t, RTCPReportBlock*>& report_blocks);

    int32_t AddReportBlock(
        uint32_t SSRC,
        std::map<uint32_t, RTCPReportBlock*>* report_blocks,
        const RTCPReportBlock* receiveBlock);

    bool PrepareReport(const FeedbackState& feedback_state,
                       StreamStatistician* statistician,
                       RTCPReportBlock* report_block,
                       uint32_t* ntp_secs, uint32_t* ntp_frac);

    int32_t BuildSR(const FeedbackState& feedback_state,
                    uint8_t* rtcpbuffer,
                    int& pos,
                    uint32_t NTPsec,
                    uint32_t NTPfrac)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

    int32_t BuildRR(uint8_t* rtcpbuffer,
                    int& pos,
                    const uint32_t NTPsec,
                    const uint32_t NTPfrac)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

    int PrepareRTCP(
        const FeedbackState& feedback_state,
        uint32_t packetTypeFlags,
        int32_t nackSize,
        const uint16_t* nackList,
        bool repeat,
        uint64_t pictureID,
        uint8_t* rtcp_buffer,
        int buffer_size);

    bool ShouldSendReportBlocks(uint32_t rtcp_packet_type) const;

    int32_t BuildExtendedJitterReport(
        uint8_t* rtcpbuffer,
        int& pos,
        const uint32_t jitterTransmissionTimeOffset)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

    int32_t BuildSDEC(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildPLI(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildREMB(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildTMMBR(ModuleRtpRtcpImpl* module, uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildTMMBN(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildAPP(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildBYE(uint8_t* rtcpbuffer, int& pos)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildFIR(uint8_t* rtcpbuffer, int& pos, bool repeat)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildSLI(uint8_t* rtcpbuffer, int& pos, const uint8_t pictureID)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildRPSI(uint8_t* rtcpbuffer,
                      int& pos,
                      const uint64_t pictureID,
                      const uint8_t payloadType)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

    int32_t BuildNACK(uint8_t* rtcpbuffer,
                      int& pos,
                      const int32_t nackSize,
                      const uint16_t* nackList,
                      std::string* nackString)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildReceiverReferenceTime(uint8_t* buffer,
                                       int& pos,
                                       uint32_t ntp_sec,
                                       uint32_t ntp_frac)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
    int32_t BuildDlrr(uint8_t* buffer,
                      int& pos,
                      const RtcpReceiveTimeInfo& info)
        EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);

private:
    const int32_t            _id;
    const bool               _audio;
    Clock* const             _clock;
    RTCPMethod               _method GUARDED_BY(_criticalSectionRTCPSender);

    CriticalSectionWrapper* _criticalSectionTransport;
    Transport*              _cbTransport GUARDED_BY(_criticalSectionTransport);

    CriticalSectionWrapper* _criticalSectionRTCPSender;
    bool                    _usingNack GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _sending GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _sendTMMBN GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _REMB GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _sendREMB GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _TMMBR GUARDED_BY(_criticalSectionRTCPSender);
    bool                    _IJ GUARDED_BY(_criticalSectionRTCPSender);

    int64_t        _nextTimeToSendRTCP GUARDED_BY(_criticalSectionRTCPSender);

    uint32_t start_timestamp_ GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t last_rtp_timestamp_ GUARDED_BY(_criticalSectionRTCPSender);
    int64_t last_frame_capture_time_ms_ GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t _SSRC GUARDED_BY(_criticalSectionRTCPSender);
    // SSRC that we receive on our RTP channel
    uint32_t _remoteSSRC GUARDED_BY(_criticalSectionRTCPSender);
    char _CNAME[RTCP_CNAME_SIZE] GUARDED_BY(_criticalSectionRTCPSender);

    ReceiveStatistics* receive_statistics_
        GUARDED_BY(_criticalSectionRTCPSender);
    std::map<uint32_t, RTCPReportBlock*> internal_report_blocks_
        GUARDED_BY(_criticalSectionRTCPSender);
    std::map<uint32_t, RTCPReportBlock*> external_report_blocks_
        GUARDED_BY(_criticalSectionRTCPSender);
    std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _csrcCNAMEs
        GUARDED_BY(_criticalSectionRTCPSender);

    int32_t         _cameraDelayMS GUARDED_BY(_criticalSectionRTCPSender);

    // Sent
    uint32_t _lastSendReport[RTCP_NUMBER_OF_SR] GUARDED_BY(
        _criticalSectionRTCPSender);  // allow packet loss and RTT above 1 sec
    uint32_t _lastRTCPTime[RTCP_NUMBER_OF_SR] GUARDED_BY(
        _criticalSectionRTCPSender);

    // Sent XR receiver reference time report.
    // <mid ntp (mid 32 bits of the 64 bits NTP timestamp), send time in ms>.
    std::map<uint32_t, int64_t> last_xr_rr_
        GUARDED_BY(_criticalSectionRTCPSender);

    // send CSRCs
    uint8_t         _CSRCs GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t        _CSRC[kRtpCsrcSize] GUARDED_BY(_criticalSectionRTCPSender);
    bool                _includeCSRCs GUARDED_BY(_criticalSectionRTCPSender);

    // Full intra request
    uint8_t         _sequenceNumberFIR GUARDED_BY(_criticalSectionRTCPSender);

    // REMB
    uint8_t       _lengthRembSSRC GUARDED_BY(_criticalSectionRTCPSender);
    uint8_t       _sizeRembSSRC GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t*     _rembSSRC GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t      _rembBitrate GUARDED_BY(_criticalSectionRTCPSender);

    TMMBRHelp           _tmmbrHelp GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t      _tmmbr_Send GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t      _packetOH_Send GUARDED_BY(_criticalSectionRTCPSender);

    // APP
    bool                 _appSend GUARDED_BY(_criticalSectionRTCPSender);
    uint8_t        _appSubType GUARDED_BY(_criticalSectionRTCPSender);
    uint32_t       _appName GUARDED_BY(_criticalSectionRTCPSender);
    uint8_t*       _appData GUARDED_BY(_criticalSectionRTCPSender);
    uint16_t       _appLength GUARDED_BY(_criticalSectionRTCPSender);

    // True if sending of XR Receiver reference time report is enabled.
    bool xrSendReceiverReferenceTimeEnabled_
        GUARDED_BY(_criticalSectionRTCPSender);

    // XR VoIP metric
    bool _xrSendVoIPMetric GUARDED_BY(_criticalSectionRTCPSender);
    RTCPVoIPMetric _xrVoIPMetric GUARDED_BY(_criticalSectionRTCPSender);

    RtcpPacketTypeCounter packet_type_counter_
        GUARDED_BY(_criticalSectionRTCPSender);
};
}  // namespace webrtc

#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_