• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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 
14 #include <map>
15 #include <sstream>
16 #include <string>
17 
18 #include "webrtc/base/thread_annotations.h"
19 #include "webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h"
20 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
21 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
22 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
23 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
24 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
25 #include "webrtc/modules/rtp_rtcp/source/tmmbr_help.h"
26 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
27 #include "webrtc/typedefs.h"
28 
29 namespace webrtc {
30 
31 class ModuleRtpRtcpImpl;
32 class RTCPReceiver;
33 
34 class NACKStringBuilder
35 {
36 public:
37     NACKStringBuilder();
38     ~NACKStringBuilder();
39 
40     void PushNACK(uint16_t nack);
41     std::string GetResult();
42 
43 private:
44     std::ostringstream _stream;
45     int _count;
46     uint16_t _prevNack;
47     bool _consecutive;
48 };
49 
50 class RTCPSender
51 {
52 public:
53  struct FeedbackState {
54    FeedbackState();
55 
56    uint8_t send_payload_type;
57    uint32_t frequency_hz;
58    uint32_t packets_sent;
59    uint32_t media_bytes_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    bool has_last_xr_rr;
67    RtcpReceiveTimeInfo last_xr_rr;
68 
69    // Used when generating TMMBR.
70    ModuleRtpRtcpImpl* module;
71  };
72     RTCPSender(const int32_t id, const bool audio,
73                Clock* clock,
74                ReceiveStatistics* receive_statistics);
75     virtual ~RTCPSender();
76 
77     int32_t RegisterSendTransport(Transport* outgoingTransport);
78 
79     RTCPMethod Status() const;
80     int32_t SetRTCPStatus(const RTCPMethod method);
81 
82     bool Sending() const;
83     int32_t SetSendingStatus(const FeedbackState& feedback_state,
84                              bool enabled);  // combine the functions
85 
86     int32_t SetNackStatus(const bool enable);
87 
88     void SetStartTimestamp(uint32_t start_timestamp);
89 
90     void SetLastRtpTime(uint32_t rtp_timestamp,
91                         int64_t capture_time_ms);
92 
93     void SetSSRC( const uint32_t ssrc);
94 
95     void SetRemoteSSRC(uint32_t ssrc);
96 
97     int32_t SetCameraDelay(const int32_t delayMS);
98 
99     int32_t SetCNAME(const char cName[RTCP_CNAME_SIZE]);
100 
101     int32_t AddMixedCNAME(const uint32_t SSRC,
102                           const char cName[RTCP_CNAME_SIZE]);
103 
104     int32_t RemoveMixedCNAME(const uint32_t SSRC);
105 
106     uint32_t SendTimeOfSendReport(const uint32_t sendReport);
107 
108     bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const;
109 
110     bool TimeToSendRTCPReport(const bool sendKeyframeBeforeRTP = false) const;
111 
112     uint32_t LastSendReport(uint32_t& lastRTCPTime);
113 
114     int32_t SendRTCP(
115         const FeedbackState& feedback_state,
116         uint32_t rtcpPacketTypeFlags,
117         int32_t nackSize = 0,
118         const uint16_t* nackList = 0,
119         bool repeat = false,
120         uint64_t pictureID = 0);
121 
122     int32_t AddExternalReportBlock(
123             uint32_t SSRC,
124             const RTCPReportBlock* receiveBlock);
125 
126     int32_t RemoveExternalReportBlock(uint32_t SSRC);
127 
128     /*
129     *  REMB
130     */
131     bool REMB() const;
132 
133     int32_t SetREMBStatus(const bool enable);
134 
135     int32_t SetREMBData(const uint32_t bitrate,
136                         const uint8_t numberOfSSRC,
137                         const uint32_t* SSRC);
138 
139     /*
140     *   TMMBR
141     */
142     bool TMMBR() const;
143 
144     int32_t SetTMMBRStatus(const bool enable);
145 
146     int32_t SetTMMBN(const TMMBRSet* boundingSet,
147                      const uint32_t maxBitrateKbit);
148 
149     /*
150     *   Extended jitter report
151     */
152     bool IJ() const;
153 
154     int32_t SetIJStatus(const bool enable);
155 
156     /*
157     *
158     */
159 
160     int32_t SetApplicationSpecificData(const uint8_t subType,
161                                        const uint32_t name,
162                                        const uint8_t* data,
163                                        const uint16_t length);
164 
165     int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
166 
167     void SendRtcpXrReceiverReferenceTime(bool enable);
168 
169     bool RtcpXrReceiverReferenceTime() const;
170 
171     int32_t SetCSRCs(const uint32_t arrOfCSRC[kRtpCsrcSize],
172                      const uint8_t arrLength);
173 
174     int32_t SetCSRCStatus(const bool include);
175 
176     void SetTargetBitrate(unsigned int target_bitrate);
177 
178     void GetPacketTypeCounter(RtcpPacketTypeCounter* packet_counter) const;
179 
180 private:
181     int32_t SendToNetwork(const uint8_t* dataBuffer, const uint16_t length);
182 
183     int32_t WriteAllReportBlocksToBuffer(uint8_t* rtcpbuffer,
184                             int pos,
185                             uint8_t& numberOfReportBlocks,
186                             const uint32_t NTPsec,
187                             const uint32_t NTPfrac)
188         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
189 
190     int32_t WriteReportBlocksToBuffer(
191         uint8_t* rtcpbuffer,
192         int32_t position,
193         const std::map<uint32_t, RTCPReportBlock*>& report_blocks);
194 
195     int32_t AddReportBlock(
196         uint32_t SSRC,
197         std::map<uint32_t, RTCPReportBlock*>* report_blocks,
198         const RTCPReportBlock* receiveBlock);
199 
200     bool PrepareReport(const FeedbackState& feedback_state,
201                        StreamStatistician* statistician,
202                        RTCPReportBlock* report_block,
203                        uint32_t* ntp_secs, uint32_t* ntp_frac);
204 
205     int32_t BuildSR(const FeedbackState& feedback_state,
206                     uint8_t* rtcpbuffer,
207                     int& pos,
208                     uint32_t NTPsec,
209                     uint32_t NTPfrac)
210         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
211 
212     int32_t BuildRR(uint8_t* rtcpbuffer,
213                     int& pos,
214                     const uint32_t NTPsec,
215                     const uint32_t NTPfrac)
216         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
217 
218     int PrepareRTCP(
219         const FeedbackState& feedback_state,
220         uint32_t packetTypeFlags,
221         int32_t nackSize,
222         const uint16_t* nackList,
223         bool repeat,
224         uint64_t pictureID,
225         uint8_t* rtcp_buffer,
226         int buffer_size);
227 
228     bool ShouldSendReportBlocks(uint32_t rtcp_packet_type) const;
229 
230     int32_t BuildExtendedJitterReport(
231         uint8_t* rtcpbuffer,
232         int& pos,
233         const uint32_t jitterTransmissionTimeOffset)
234         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
235 
236     int32_t BuildSDEC(uint8_t* rtcpbuffer, int& pos)
237         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
238     int32_t BuildPLI(uint8_t* rtcpbuffer, int& pos)
239         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
240     int32_t BuildREMB(uint8_t* rtcpbuffer, int& pos)
241         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
242     int32_t BuildTMMBR(ModuleRtpRtcpImpl* module, uint8_t* rtcpbuffer, int& pos)
243         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
244     int32_t BuildTMMBN(uint8_t* rtcpbuffer, int& pos)
245         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
246     int32_t BuildAPP(uint8_t* rtcpbuffer, int& pos)
247         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
248     int32_t BuildVoIPMetric(uint8_t* rtcpbuffer, int& pos)
249         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
250     int32_t BuildBYE(uint8_t* rtcpbuffer, int& pos)
251         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
252     int32_t BuildFIR(uint8_t* rtcpbuffer, int& pos, bool repeat)
253         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
254     int32_t BuildSLI(uint8_t* rtcpbuffer, int& pos, const uint8_t pictureID)
255         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
256     int32_t BuildRPSI(uint8_t* rtcpbuffer,
257                       int& pos,
258                       const uint64_t pictureID,
259                       const uint8_t payloadType)
260         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
261 
262     int32_t BuildNACK(uint8_t* rtcpbuffer,
263                       int& pos,
264                       const int32_t nackSize,
265                       const uint16_t* nackList,
266                       std::string* nackString)
267         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
268     int32_t BuildReceiverReferenceTime(uint8_t* buffer,
269                                        int& pos,
270                                        uint32_t ntp_sec,
271                                        uint32_t ntp_frac)
272         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
273     int32_t BuildDlrr(uint8_t* buffer,
274                       int& pos,
275                       const RtcpReceiveTimeInfo& info)
276         EXCLUSIVE_LOCKS_REQUIRED(_criticalSectionRTCPSender);
277 
278 private:
279     const int32_t            _id;
280     const bool               _audio;
281     Clock* const             _clock;
282     RTCPMethod               _method GUARDED_BY(_criticalSectionRTCPSender);
283 
284     CriticalSectionWrapper* _criticalSectionTransport;
285     Transport*              _cbTransport GUARDED_BY(_criticalSectionTransport);
286 
287     CriticalSectionWrapper* _criticalSectionRTCPSender;
288     bool                    _usingNack GUARDED_BY(_criticalSectionRTCPSender);
289     bool                    _sending GUARDED_BY(_criticalSectionRTCPSender);
290     bool                    _sendTMMBN GUARDED_BY(_criticalSectionRTCPSender);
291     bool                    _REMB GUARDED_BY(_criticalSectionRTCPSender);
292     bool                    _sendREMB GUARDED_BY(_criticalSectionRTCPSender);
293     bool                    _TMMBR GUARDED_BY(_criticalSectionRTCPSender);
294     bool                    _IJ GUARDED_BY(_criticalSectionRTCPSender);
295 
296     int64_t        _nextTimeToSendRTCP GUARDED_BY(_criticalSectionRTCPSender);
297 
298     uint32_t start_timestamp_ GUARDED_BY(_criticalSectionRTCPSender);
299     uint32_t last_rtp_timestamp_ GUARDED_BY(_criticalSectionRTCPSender);
300     int64_t last_frame_capture_time_ms_ GUARDED_BY(_criticalSectionRTCPSender);
301     uint32_t _SSRC GUARDED_BY(_criticalSectionRTCPSender);
302     // SSRC that we receive on our RTP channel
303     uint32_t _remoteSSRC GUARDED_BY(_criticalSectionRTCPSender);
304     char _CNAME[RTCP_CNAME_SIZE] GUARDED_BY(_criticalSectionRTCPSender);
305 
306     ReceiveStatistics* receive_statistics_
307         GUARDED_BY(_criticalSectionRTCPSender);
308     std::map<uint32_t, RTCPReportBlock*> internal_report_blocks_
309         GUARDED_BY(_criticalSectionRTCPSender);
310     std::map<uint32_t, RTCPReportBlock*> external_report_blocks_
311         GUARDED_BY(_criticalSectionRTCPSender);
312     std::map<uint32_t, RTCPUtility::RTCPCnameInformation*> _csrcCNAMEs
313         GUARDED_BY(_criticalSectionRTCPSender);
314 
315     int32_t         _cameraDelayMS GUARDED_BY(_criticalSectionRTCPSender);
316 
317     // Sent
318     uint32_t _lastSendReport[RTCP_NUMBER_OF_SR] GUARDED_BY(
319         _criticalSectionRTCPSender);  // allow packet loss and RTT above 1 sec
320     uint32_t _lastRTCPTime[RTCP_NUMBER_OF_SR] GUARDED_BY(
321         _criticalSectionRTCPSender);
322 
323     // Sent XR receiver reference time report.
324     // <mid ntp (mid 32 bits of the 64 bits NTP timestamp), send time in ms>.
325     std::map<uint32_t, int64_t> last_xr_rr_
326         GUARDED_BY(_criticalSectionRTCPSender);
327 
328     // send CSRCs
329     uint8_t         _CSRCs GUARDED_BY(_criticalSectionRTCPSender);
330     uint32_t        _CSRC[kRtpCsrcSize] GUARDED_BY(_criticalSectionRTCPSender);
331     bool                _includeCSRCs GUARDED_BY(_criticalSectionRTCPSender);
332 
333     // Full intra request
334     uint8_t         _sequenceNumberFIR GUARDED_BY(_criticalSectionRTCPSender);
335 
336     // REMB
337     uint8_t       _lengthRembSSRC GUARDED_BY(_criticalSectionRTCPSender);
338     uint8_t       _sizeRembSSRC GUARDED_BY(_criticalSectionRTCPSender);
339     uint32_t*     _rembSSRC GUARDED_BY(_criticalSectionRTCPSender);
340     uint32_t      _rembBitrate GUARDED_BY(_criticalSectionRTCPSender);
341 
342     TMMBRHelp           _tmmbrHelp GUARDED_BY(_criticalSectionRTCPSender);
343     uint32_t      _tmmbr_Send GUARDED_BY(_criticalSectionRTCPSender);
344     uint32_t      _packetOH_Send GUARDED_BY(_criticalSectionRTCPSender);
345 
346     // APP
347     bool                 _appSend GUARDED_BY(_criticalSectionRTCPSender);
348     uint8_t        _appSubType GUARDED_BY(_criticalSectionRTCPSender);
349     uint32_t       _appName GUARDED_BY(_criticalSectionRTCPSender);
350     uint8_t*       _appData GUARDED_BY(_criticalSectionRTCPSender);
351     uint16_t       _appLength GUARDED_BY(_criticalSectionRTCPSender);
352 
353     // True if sending of XR Receiver reference time report is enabled.
354     bool xrSendReceiverReferenceTimeEnabled_
355         GUARDED_BY(_criticalSectionRTCPSender);
356 
357     // XR VoIP metric
358     bool _xrSendVoIPMetric GUARDED_BY(_criticalSectionRTCPSender);
359     RTCPVoIPMetric _xrVoIPMetric GUARDED_BY(_criticalSectionRTCPSender);
360 
361     RtcpPacketTypeCounter packet_type_counter_
362         GUARDED_BY(_criticalSectionRTCPSender);
363 };
364 }  // namespace webrtc
365 
366 #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_SENDER_H_
367