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