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