rtp_rtcp_impl.h revision 2bb1bdab8d11f5445693c028335fb3ace631f636
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_RTP_RTCP_IMPL_H_
12#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_
13
14#include <list>
15#include <vector>
16
17#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
18#include "webrtc/modules/rtp_rtcp/source/rtcp_receiver.h"
19#include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
20#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
21#include "webrtc/system_wrappers/interface/scoped_ptr.h"
22#include "webrtc/test/testsupport/gtest_prod_util.h"
23
24namespace webrtc {
25
26class ModuleRtpRtcpImpl : public RtpRtcp {
27 public:
28  explicit ModuleRtpRtcpImpl(const RtpRtcp::Configuration& configuration);
29
30  virtual ~ModuleRtpRtcpImpl();
31
32  // Returns the number of milliseconds until the module want a worker thread to
33  // call Process.
34  virtual int32_t TimeUntilNextProcess() OVERRIDE;
35
36  // Process any pending tasks such as timeouts.
37  virtual int32_t Process() OVERRIDE;
38
39  // Receiver part.
40
41  // Called when we receive an RTCP packet.
42  virtual int32_t IncomingRtcpPacket(const uint8_t* incoming_packet,
43                                     uint16_t incoming_packet_length) OVERRIDE;
44
45  virtual void SetRemoteSSRC(const uint32_t ssrc);
46
47  // Sender part.
48
49  virtual int32_t RegisterSendPayload(const CodecInst& voice_codec) OVERRIDE;
50
51  virtual int32_t RegisterSendPayload(const VideoCodec& video_codec) OVERRIDE;
52
53  virtual int32_t DeRegisterSendPayload(const int8_t payload_type) OVERRIDE;
54
55  virtual int8_t SendPayloadType() const;
56
57  // Register RTP header extension.
58  virtual int32_t RegisterSendRtpHeaderExtension(
59      const RTPExtensionType type,
60      const uint8_t id) OVERRIDE;
61
62  virtual int32_t DeregisterSendRtpHeaderExtension(
63      const RTPExtensionType type) OVERRIDE;
64
65  // Get start timestamp.
66  virtual uint32_t StartTimestamp() const OVERRIDE;
67
68  // Configure start timestamp, default is a random number.
69  virtual int32_t SetStartTimestamp(const uint32_t timestamp) OVERRIDE;
70
71  virtual uint16_t SequenceNumber() const OVERRIDE;
72
73  // Set SequenceNumber, default is a random number.
74  virtual int32_t SetSequenceNumber(const uint16_t seq) OVERRIDE;
75
76  virtual void SetRtpStateForSsrc(uint32_t ssrc,
77                                  const RtpState& rtp_state) OVERRIDE;
78  virtual bool GetRtpStateForSsrc(uint32_t ssrc, RtpState* rtp_state) OVERRIDE;
79
80  virtual uint32_t SSRC() const OVERRIDE;
81
82  // Configure SSRC, default is a random number.
83  virtual void SetSSRC(const uint32_t ssrc) OVERRIDE;
84
85  virtual int32_t CSRCs(uint32_t arr_of_csrc[kRtpCsrcSize]) const OVERRIDE;
86
87  virtual int32_t SetCSRCs(const uint32_t arr_of_csrc[kRtpCsrcSize],
88                           const uint8_t arr_length) OVERRIDE;
89
90  virtual int32_t SetCSRCStatus(const bool include) OVERRIDE;
91
92  virtual uint32_t PacketCountSent() const;
93
94  virtual int CurrentSendFrequencyHz() const;
95
96  virtual uint32_t ByteCountSent() const;
97
98  virtual void SetRTXSendStatus(const int mode) OVERRIDE;
99
100  virtual void RTXSendStatus(int* mode, uint32_t* ssrc,
101                             int* payloadType) const OVERRIDE;
102
103  virtual void SetRtxSsrc(uint32_t ssrc) OVERRIDE;
104
105  virtual void SetRtxSendPayloadType(int payload_type) OVERRIDE;
106
107  // Sends kRtcpByeCode when going from true to false.
108  virtual int32_t SetSendingStatus(const bool sending) OVERRIDE;
109
110  virtual bool Sending() const OVERRIDE;
111
112  // Drops or relays media packets.
113  virtual int32_t SetSendingMediaStatus(const bool sending) OVERRIDE;
114
115  virtual bool SendingMedia() const OVERRIDE;
116
117  // Used by the codec module to deliver a video or audio frame for
118  // packetization.
119  virtual int32_t SendOutgoingData(
120      const FrameType frame_type,
121      const int8_t payload_type,
122      const uint32_t time_stamp,
123      int64_t capture_time_ms,
124      const uint8_t* payload_data,
125      const uint32_t payload_size,
126      const RTPFragmentationHeader* fragmentation = NULL,
127      const RTPVideoHeader* rtp_video_hdr = NULL) OVERRIDE;
128
129  virtual bool TimeToSendPacket(uint32_t ssrc,
130                                uint16_t sequence_number,
131                                int64_t capture_time_ms,
132                                bool retransmission) OVERRIDE;
133  // Returns the number of padding bytes actually sent, which can be more or
134  // less than |bytes|.
135  virtual int TimeToSendPadding(int bytes) OVERRIDE;
136
137  virtual bool GetSendSideDelay(int* avg_send_delay_ms,
138                                int* max_send_delay_ms) const OVERRIDE;
139
140  // RTCP part.
141
142  // Get RTCP status.
143  virtual RTCPMethod RTCP() const OVERRIDE;
144
145  // Configure RTCP status i.e on/off.
146  virtual int32_t SetRTCPStatus(const RTCPMethod method) OVERRIDE;
147
148  // Set RTCP CName.
149  virtual int32_t SetCNAME(const char c_name[RTCP_CNAME_SIZE]) OVERRIDE;
150
151  // Get RTCP CName.
152  virtual int32_t CNAME(char c_name[RTCP_CNAME_SIZE]) OVERRIDE;
153
154  // Get remote CName.
155  virtual int32_t RemoteCNAME(const uint32_t remote_ssrc,
156                              char c_name[RTCP_CNAME_SIZE]) const OVERRIDE;
157
158  // Get remote NTP.
159  virtual int32_t RemoteNTP(uint32_t* received_ntp_secs,
160                            uint32_t* received_ntp_frac,
161                            uint32_t* rtcp_arrival_time_secs,
162                            uint32_t* rtcp_arrival_time_frac,
163                            uint32_t* rtcp_timestamp) const OVERRIDE;
164
165  virtual int32_t AddMixedCNAME(const uint32_t ssrc,
166                                const char c_name[RTCP_CNAME_SIZE]) OVERRIDE;
167
168  virtual int32_t RemoveMixedCNAME(const uint32_t ssrc) OVERRIDE;
169
170  // Get RoundTripTime.
171  virtual int32_t RTT(const uint32_t remote_ssrc,
172                      uint16_t* rtt,
173                      uint16_t* avg_rtt,
174                      uint16_t* min_rtt,
175                      uint16_t* max_rtt) const OVERRIDE;
176
177  // Reset RoundTripTime statistics.
178  virtual int32_t ResetRTT(const uint32_t remote_ssrc) OVERRIDE;
179
180  // Force a send of an RTCP packet.
181  // Normal SR and RR are triggered via the process function.
182  virtual int32_t SendRTCP(uint32_t rtcp_packet_type = kRtcpReport) OVERRIDE;
183
184  virtual int32_t ResetSendDataCountersRTP() OVERRIDE;
185
186  // Statistics of the amount of data sent and received.
187  virtual int32_t DataCountersRTP(uint32_t* bytes_sent,
188                                  uint32_t* packets_sent) const OVERRIDE;
189
190  // Get received RTCP report, sender info.
191  virtual int32_t RemoteRTCPStat(RTCPSenderInfo* sender_info) OVERRIDE;
192
193  // Get received RTCP report, report block.
194  virtual int32_t RemoteRTCPStat(
195      std::vector<RTCPReportBlock>* receive_blocks) const OVERRIDE;
196
197  // Set received RTCP report block.
198  virtual int32_t AddRTCPReportBlock(
199      const uint32_t ssrc, const RTCPReportBlock* receive_block) OVERRIDE;
200
201  virtual int32_t RemoveRTCPReportBlock(const uint32_t ssrc) OVERRIDE;
202
203  virtual void GetRtcpPacketTypeCounters(
204      RtcpPacketTypeCounter* packets_sent,
205      RtcpPacketTypeCounter* packets_received) const OVERRIDE;
206
207  // (REMB) Receiver Estimated Max Bitrate.
208  virtual bool REMB() const OVERRIDE;
209
210  virtual int32_t SetREMBStatus(const bool enable) OVERRIDE;
211
212  virtual int32_t SetREMBData(const uint32_t bitrate,
213                              const uint8_t number_of_ssrc,
214                              const uint32_t* ssrc) OVERRIDE;
215
216  // (IJ) Extended jitter report.
217  virtual bool IJ() const OVERRIDE;
218
219  virtual int32_t SetIJStatus(const bool enable) OVERRIDE;
220
221  // (TMMBR) Temporary Max Media Bit Rate.
222  virtual bool TMMBR() const OVERRIDE;
223
224  virtual int32_t SetTMMBRStatus(const bool enable) OVERRIDE;
225
226  int32_t SetTMMBN(const TMMBRSet* bounding_set);
227
228  virtual uint16_t MaxPayloadLength() const OVERRIDE;
229
230  virtual uint16_t MaxDataPayloadLength() const OVERRIDE;
231
232  virtual int32_t SetMaxTransferUnit(const uint16_t size) OVERRIDE;
233
234  virtual int32_t SetTransportOverhead(
235      const bool tcp,
236      const bool ipv6,
237      const uint8_t authentication_overhead = 0) OVERRIDE;
238
239  // (NACK) Negative acknowledgment part.
240
241  virtual int SelectiveRetransmissions() const OVERRIDE;
242
243  virtual int SetSelectiveRetransmissions(uint8_t settings) OVERRIDE;
244
245  // Send a Negative acknowledgment packet.
246  virtual int32_t SendNACK(const uint16_t* nack_list,
247                           const uint16_t size) OVERRIDE;
248
249  // Store the sent packets, needed to answer to a negative acknowledgment
250  // requests.
251  virtual int32_t SetStorePacketsStatus(
252      const bool enable, const uint16_t number_to_store) OVERRIDE;
253
254  virtual bool StorePackets() const OVERRIDE;
255
256  // Called on receipt of RTCP report block from remote side.
257  virtual void RegisterSendChannelRtcpStatisticsCallback(
258      RtcpStatisticsCallback* callback) OVERRIDE;
259  virtual RtcpStatisticsCallback*
260      GetSendChannelRtcpStatisticsCallback() OVERRIDE;
261
262  // (APP) Application specific data.
263  virtual int32_t SetRTCPApplicationSpecificData(
264      const uint8_t sub_type,
265      const uint32_t name,
266      const uint8_t* data,
267      const uint16_t length) OVERRIDE;
268
269  // (XR) VOIP metric.
270  virtual int32_t SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric) OVERRIDE;
271
272  // (XR) Receiver reference time report.
273  virtual void SetRtcpXrRrtrStatus(bool enable) OVERRIDE;
274
275  virtual bool RtcpXrRrtrStatus() const OVERRIDE;
276
277  // Audio part.
278
279  // Set audio packet size, used to determine when it's time to send a DTMF
280  // packet in silence (CNG).
281  virtual int32_t SetAudioPacketSize(
282      const uint16_t packet_size_samples) OVERRIDE;
283
284  virtual bool SendTelephoneEventActive(int8_t& telephone_event) const OVERRIDE;
285
286  // Send a TelephoneEvent tone using RFC 2833 (4733).
287  virtual int32_t SendTelephoneEventOutband(const uint8_t key,
288                                            const uint16_t time_ms,
289                                            const uint8_t level) OVERRIDE;
290
291  // Set payload type for Redundant Audio Data RFC 2198.
292  virtual int32_t SetSendREDPayloadType(const int8_t payload_type) OVERRIDE;
293
294  // Get payload type for Redundant Audio Data RFC 2198.
295  virtual int32_t SendREDPayloadType(int8_t& payload_type) const OVERRIDE;
296
297  // Store the audio level in d_bov for header-extension-for-audio-level-
298  // indication.
299  virtual int32_t SetAudioLevel(const uint8_t level_d_bov) OVERRIDE;
300
301  // Video part.
302
303  virtual RtpVideoCodecTypes SendVideoCodec() const;
304
305  virtual int32_t SendRTCPSliceLossIndication(
306      const uint8_t picture_id) OVERRIDE;
307
308  // Set method for requestion a new key frame.
309  virtual int32_t SetKeyFrameRequestMethod(
310      const KeyFrameRequestMethod method) OVERRIDE;
311
312  // Send a request for a keyframe.
313  virtual int32_t RequestKeyFrame() OVERRIDE;
314
315  virtual int32_t SetCameraDelay(const int32_t delay_ms) OVERRIDE;
316
317  virtual void SetTargetSendBitrate(
318      const std::vector<uint32_t>& stream_bitrates) OVERRIDE;
319
320  virtual int32_t SetGenericFECStatus(
321      const bool enable,
322      const uint8_t payload_type_red,
323      const uint8_t payload_type_fec) OVERRIDE;
324
325  virtual int32_t GenericFECStatus(
326      bool& enable,
327      uint8_t& payload_type_red,
328      uint8_t& payload_type_fec) OVERRIDE;
329
330  virtual int32_t SetFecParameters(
331      const FecProtectionParams* delta_params,
332      const FecProtectionParams* key_params) OVERRIDE;
333
334  virtual int32_t LastReceivedNTP(uint32_t& NTPsecs,
335                                  uint32_t& NTPfrac,
336                                  uint32_t& remote_sr);
337
338  virtual bool LastReceivedXrReferenceTimeInfo(RtcpReceiveTimeInfo* info) const;
339
340  virtual int32_t BoundingSet(bool& tmmbr_owner, TMMBRSet*& bounding_set_rec);
341
342  virtual void BitrateSent(uint32_t* total_rate,
343                           uint32_t* video_rate,
344                           uint32_t* fec_rate,
345                           uint32_t* nackRate) const OVERRIDE;
346
347  virtual void RegisterVideoBitrateObserver(BitrateStatisticsObserver* observer)
348      OVERRIDE;
349
350  virtual BitrateStatisticsObserver* GetVideoBitrateObserver() const OVERRIDE;
351
352  virtual uint32_t SendTimeOfSendReport(const uint32_t send_report);
353
354  virtual bool SendTimeOfXrRrReport(uint32_t mid_ntp, int64_t* time_ms) const;
355
356  // Good state of RTP receiver inform sender.
357  virtual int32_t SendRTCPReferencePictureSelection(
358      const uint64_t picture_id) OVERRIDE;
359
360  virtual void RegisterSendChannelRtpStatisticsCallback(
361      StreamDataCountersCallback* callback);
362  virtual StreamDataCountersCallback*
363      GetSendChannelRtpStatisticsCallback() const;
364
365  void OnReceivedTMMBR();
366
367  // Bad state of RTP receiver request a keyframe.
368  void OnRequestIntraFrame();
369
370  // Received a request for a new SLI.
371  void OnReceivedSliceLossIndication(const uint8_t picture_id);
372
373  // Received a new reference frame.
374  void OnReceivedReferencePictureSelectionIndication(
375      const uint64_t picture_id);
376
377  void OnReceivedNACK(const std::list<uint16_t>& nack_sequence_numbers);
378
379  void OnRequestSendReport();
380
381  virtual void RegisterSendFrameCountObserver(
382      FrameCountObserver* observer) OVERRIDE;
383  virtual FrameCountObserver* GetSendFrameCountObserver() const OVERRIDE;
384
385 protected:
386  void RegisterChildModule(RtpRtcp* module);
387
388  void DeRegisterChildModule(RtpRtcp* module);
389
390  bool UpdateRTCPReceiveInformationTimers();
391
392  uint32_t BitrateReceivedNow() const;
393
394  // Get remote SequenceNumber.
395  uint16_t RemoteSequenceNumber() const;
396
397  // Only for internal testing.
398  uint32_t LastSendReport(uint32_t& last_rtcptime);
399
400  RTPSender                 rtp_sender_;
401
402  RTCPSender                rtcp_sender_;
403  RTCPReceiver              rtcp_receiver_;
404
405  Clock*                    clock_;
406
407 private:
408  FRIEND_TEST_ALL_PREFIXES(RtpRtcpImplTest, Rtt);
409  FRIEND_TEST_ALL_PREFIXES(RtpRtcpImplTest, RttForReceiverOnly);
410  int64_t RtcpReportInterval();
411  void SetRtcpReceiverSsrcs(uint32_t main_ssrc);
412
413  void set_rtt_ms(uint32_t rtt_ms);
414  uint32_t rtt_ms() const;
415
416  bool IsDefaultModule() const;
417
418  int32_t             id_;
419  const bool                audio_;
420  bool                      collision_detected_;
421  int64_t             last_process_time_;
422  int64_t             last_bitrate_process_time_;
423  int64_t             last_rtt_process_time_;
424  uint16_t            packet_overhead_;
425
426  scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_;
427  scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_feedback_;
428  ModuleRtpRtcpImpl*            default_module_;
429  std::vector<ModuleRtpRtcpImpl*> child_modules_;
430  size_t padding_index_;
431
432  // Send side
433  NACKMethod            nack_method_;
434  uint32_t        nack_last_time_sent_full_;
435  uint16_t        nack_last_seq_number_sent_;
436
437  bool                  simulcast_;
438  VideoCodec            send_video_codec_;
439  KeyFrameRequestMethod key_frame_req_method_;
440
441  RemoteBitrateEstimator* remote_bitrate_;
442
443  RtcpRttStats* rtt_stats_;
444
445  // The processed RTT from RtcpRttStats.
446  scoped_ptr<CriticalSectionWrapper> critical_section_rtt_;
447  uint32_t rtt_ms_;
448};
449
450}  // namespace webrtc
451
452#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_
453