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