rtp_rtcp_impl.h revision 20ed36dada62ad56ec01263fc0eef0ed198f6476
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_receiver.h"
21#include "webrtc/modules/rtp_rtcp/source/rtp_sender.h"
22#include "webrtc/system_wrappers/interface/scoped_ptr.h"
23
24#ifdef MATLAB
25class MatlabPlot;
26#endif
27
28namespace webrtc {
29
30class ModuleRtpRtcpImpl : public RtpRtcp {
31 public:
32  explicit ModuleRtpRtcpImpl(const RtpRtcp::Configuration& configuration);
33
34  virtual ~ModuleRtpRtcpImpl();
35
36  // Returns the number of milliseconds until the module want a worker thread to
37  // call Process.
38  virtual WebRtc_Word32 TimeUntilNextProcess();
39
40  // Process any pending tasks such as timeouts.
41  virtual WebRtc_Word32 Process();
42
43  // Receiver part.
44
45  // Configure a timeout value.
46  virtual WebRtc_Word32 SetPacketTimeout(const WebRtc_UWord32 rtp_timeout_ms,
47                                         const WebRtc_UWord32 rtcp_timeout_ms);
48
49  // Set periodic dead or alive notification.
50  virtual WebRtc_Word32 SetPeriodicDeadOrAliveStatus(
51      const bool enable,
52      const WebRtc_UWord8 sample_time_seconds);
53
54  // Get periodic dead or alive notification status.
55  virtual WebRtc_Word32 PeriodicDeadOrAliveStatus(
56      bool& enable,
57      WebRtc_UWord8& sample_time_seconds);
58
59  virtual WebRtc_Word32 RegisterReceivePayload(const CodecInst& voice_codec);
60
61  virtual WebRtc_Word32 RegisterReceivePayload(const VideoCodec& video_codec);
62
63  virtual WebRtc_Word32 ReceivePayloadType(const CodecInst& voice_codec,
64                                           WebRtc_Word8* pl_type);
65
66  virtual WebRtc_Word32 ReceivePayloadType(const VideoCodec& video_codec,
67                                           WebRtc_Word8* pl_type);
68
69  virtual WebRtc_Word32 DeRegisterReceivePayload(
70      const WebRtc_Word8 payload_type);
71
72  // Register RTP header extension.
73  virtual WebRtc_Word32 RegisterReceiveRtpHeaderExtension(
74      const RTPExtensionType type,
75      const WebRtc_UWord8 id);
76
77  virtual WebRtc_Word32 DeregisterReceiveRtpHeaderExtension(
78      const RTPExtensionType type);
79
80  // Get the currently configured SSRC filter.
81  virtual WebRtc_Word32 SSRCFilter(WebRtc_UWord32& allowed_ssrc) const;
82
83  // Set a SSRC to be used as a filter for incoming RTP streams.
84  virtual WebRtc_Word32 SetSSRCFilter(const bool enable,
85                                      const WebRtc_UWord32 allowed_ssrc);
86
87  // Get last received remote timestamp.
88  virtual WebRtc_UWord32 RemoteTimestamp() const;
89
90  // Get the local time of the last received remote timestamp.
91  virtual int64_t LocalTimeOfRemoteTimeStamp() const;
92
93  // Get the current estimated remote timestamp.
94  virtual WebRtc_Word32 EstimatedRemoteTimeStamp(
95      WebRtc_UWord32& timestamp) const;
96
97  virtual WebRtc_UWord32 RemoteSSRC() const;
98
99  virtual WebRtc_Word32 RemoteCSRCs(
100      WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize]) const;
101
102  virtual WebRtc_Word32 SetRTXReceiveStatus(const bool enable,
103                                            const WebRtc_UWord32 ssrc);
104
105  virtual WebRtc_Word32 RTXReceiveStatus(bool* enable,
106                                         WebRtc_UWord32* ssrc) const;
107
108  // Called by the network module when we receive a packet.
109  virtual WebRtc_Word32 IncomingPacket(const WebRtc_UWord8* incoming_packet,
110                                       const WebRtc_UWord16 packet_length);
111
112  // Sender part.
113
114  virtual WebRtc_Word32 RegisterSendPayload(const CodecInst& voice_codec);
115
116  virtual WebRtc_Word32 RegisterSendPayload(const VideoCodec& video_codec);
117
118  virtual WebRtc_Word32 DeRegisterSendPayload(const WebRtc_Word8 payload_type);
119
120  virtual WebRtc_Word8 SendPayloadType() const;
121
122  // Register RTP header extension.
123  virtual WebRtc_Word32 RegisterSendRtpHeaderExtension(
124      const RTPExtensionType type,
125      const WebRtc_UWord8 id);
126
127  virtual WebRtc_Word32 DeregisterSendRtpHeaderExtension(
128      const RTPExtensionType type);
129
130  // Get start timestamp.
131  virtual WebRtc_UWord32 StartTimestamp() const;
132
133  // Configure start timestamp, default is a random number.
134  virtual WebRtc_Word32 SetStartTimestamp(const WebRtc_UWord32 timestamp);
135
136  virtual WebRtc_UWord16 SequenceNumber() const;
137
138  // Set SequenceNumber, default is a random number.
139  virtual WebRtc_Word32 SetSequenceNumber(const WebRtc_UWord16 seq);
140
141  virtual WebRtc_UWord32 SSRC() const;
142
143  // Configure SSRC, default is a random number.
144  virtual WebRtc_Word32 SetSSRC(const WebRtc_UWord32 ssrc);
145
146  virtual WebRtc_Word32 CSRCs(WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize]) const;
147
148  virtual WebRtc_Word32 SetCSRCs(const WebRtc_UWord32 arr_of_csrc[kRtpCsrcSize],
149                                 const WebRtc_UWord8 arr_length);
150
151  virtual WebRtc_Word32 SetCSRCStatus(const bool include);
152
153  virtual WebRtc_UWord32 PacketCountSent() const;
154
155  virtual int CurrentSendFrequencyHz() const;
156
157  virtual WebRtc_UWord32 ByteCountSent() const;
158
159  virtual WebRtc_Word32 SetRTXSendStatus(const bool enable,
160                                         const bool set_ssrc,
161                                         const WebRtc_UWord32 ssrc);
162
163  virtual WebRtc_Word32 RTXSendStatus(bool* enable,
164                                      WebRtc_UWord32* ssrc) const;
165
166  // Sends kRtcpByeCode when going from true to false.
167  virtual WebRtc_Word32 SetSendingStatus(const bool sending);
168
169  virtual bool Sending() const;
170
171  // Drops or relays media packets.
172  virtual WebRtc_Word32 SetSendingMediaStatus(const bool sending);
173
174  virtual bool SendingMedia() const;
175
176  // Used by the codec module to deliver a video or audio frame for
177  // packetization.
178  virtual WebRtc_Word32 SendOutgoingData(
179      const FrameType frame_type,
180      const WebRtc_Word8 payload_type,
181      const WebRtc_UWord32 time_stamp,
182      int64_t capture_time_ms,
183      const WebRtc_UWord8* payload_data,
184      const WebRtc_UWord32 payload_size,
185      const RTPFragmentationHeader* fragmentation = NULL,
186      const RTPVideoHeader* rtp_video_hdr = NULL);
187
188  virtual void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
189                                int64_t capture_time_ms);
190  // RTCP part.
191
192  // Get RTCP status.
193  virtual RTCPMethod RTCP() const;
194
195  // Configure RTCP status i.e on/off.
196  virtual WebRtc_Word32 SetRTCPStatus(const RTCPMethod method);
197
198  // Set RTCP CName.
199  virtual WebRtc_Word32 SetCNAME(const char c_name[RTCP_CNAME_SIZE]);
200
201  // Get RTCP CName.
202  virtual WebRtc_Word32 CNAME(char c_name[RTCP_CNAME_SIZE]);
203
204  // Get remote CName.
205  virtual WebRtc_Word32 RemoteCNAME(const WebRtc_UWord32 remote_ssrc,
206                                    char c_name[RTCP_CNAME_SIZE]) const;
207
208  // Get remote NTP.
209  virtual WebRtc_Word32 RemoteNTP(WebRtc_UWord32* received_ntp_secs,
210                                  WebRtc_UWord32* received_ntp_frac,
211                                  WebRtc_UWord32* rtcp_arrival_time_secs,
212                                  WebRtc_UWord32* rtcp_arrival_time_frac,
213                                  WebRtc_UWord32* rtcp_timestamp) const;
214
215  virtual WebRtc_Word32 AddMixedCNAME(const WebRtc_UWord32 ssrc,
216                                      const char c_name[RTCP_CNAME_SIZE]);
217
218  virtual WebRtc_Word32 RemoveMixedCNAME(const WebRtc_UWord32 ssrc);
219
220  // Get RoundTripTime.
221  virtual WebRtc_Word32 RTT(const WebRtc_UWord32 remote_ssrc,
222                            WebRtc_UWord16* rtt,
223                            WebRtc_UWord16* avg_rtt,
224                            WebRtc_UWord16* min_rtt,
225                            WebRtc_UWord16* max_rtt) const;
226
227  // Reset RoundTripTime statistics.
228  virtual WebRtc_Word32 ResetRTT(const WebRtc_UWord32 remote_ssrc);
229
230  virtual void SetRtt(uint32_t rtt);
231
232  // Force a send of an RTCP packet.
233  // Normal SR and RR are triggered via the process function.
234  virtual WebRtc_Word32 SendRTCP(WebRtc_UWord32 rtcp_packet_type = kRtcpReport);
235
236  // Statistics of our locally created statistics of the received RTP stream.
237  virtual WebRtc_Word32 StatisticsRTP(WebRtc_UWord8*  fraction_lost,
238                                      WebRtc_UWord32* cum_lost,
239                                      WebRtc_UWord32* ext_max,
240                                      WebRtc_UWord32* jitter,
241                                      WebRtc_UWord32* max_jitter = NULL) const;
242
243  // Reset RTP statistics.
244  virtual WebRtc_Word32 ResetStatisticsRTP();
245
246  virtual WebRtc_Word32 ResetReceiveDataCountersRTP();
247
248  virtual WebRtc_Word32 ResetSendDataCountersRTP();
249
250  // Statistics of the amount of data sent and received.
251  virtual WebRtc_Word32 DataCountersRTP(WebRtc_UWord32* bytes_sent,
252                                        WebRtc_UWord32* packets_sent,
253                                        WebRtc_UWord32* bytes_received,
254                                        WebRtc_UWord32* packets_received) const;
255
256  virtual WebRtc_Word32 ReportBlockStatistics(
257      WebRtc_UWord8* fraction_lost,
258      WebRtc_UWord32* cum_lost,
259      WebRtc_UWord32* ext_max,
260      WebRtc_UWord32* jitter,
261      WebRtc_UWord32* jitter_transmission_time_offset);
262
263  // Get received RTCP report, sender info.
264  virtual WebRtc_Word32 RemoteRTCPStat(RTCPSenderInfo* sender_info);
265
266  // Get received RTCP report, report block.
267  virtual WebRtc_Word32 RemoteRTCPStat(
268      std::vector<RTCPReportBlock>* receive_blocks) const;
269
270  // Set received RTCP report block.
271  virtual WebRtc_Word32 AddRTCPReportBlock(
272    const WebRtc_UWord32 ssrc, const RTCPReportBlock* receive_block);
273
274  virtual WebRtc_Word32 RemoveRTCPReportBlock(const WebRtc_UWord32 ssrc);
275
276  // (REMB) Receiver Estimated Max Bitrate.
277  virtual bool REMB() const;
278
279  virtual WebRtc_Word32 SetREMBStatus(const bool enable);
280
281  virtual WebRtc_Word32 SetREMBData(const WebRtc_UWord32 bitrate,
282                                    const WebRtc_UWord8 number_of_ssrc,
283                                    const WebRtc_UWord32* ssrc);
284
285  // (IJ) Extended jitter report.
286  virtual bool IJ() const;
287
288  virtual WebRtc_Word32 SetIJStatus(const bool enable);
289
290  // (TMMBR) Temporary Max Media Bit Rate.
291  virtual bool TMMBR() const;
292
293  virtual WebRtc_Word32 SetTMMBRStatus(const bool enable);
294
295  WebRtc_Word32 SetTMMBN(const TMMBRSet* bounding_set);
296
297  virtual WebRtc_UWord16 MaxPayloadLength() const;
298
299  virtual WebRtc_UWord16 MaxDataPayloadLength() const;
300
301  virtual WebRtc_Word32 SetMaxTransferUnit(const WebRtc_UWord16 size);
302
303  virtual WebRtc_Word32 SetTransportOverhead(
304      const bool tcp,
305      const bool ipv6,
306      const WebRtc_UWord8 authentication_overhead = 0);
307
308  // (NACK) Negative acknowledgment part.
309
310  // Is Negative acknowledgment requests on/off?
311  virtual NACKMethod NACK() const;
312
313  // Turn negative acknowledgment requests on/off.
314  virtual WebRtc_Word32 SetNACKStatus(const NACKMethod method);
315
316  virtual int SelectiveRetransmissions() const;
317
318  virtual int SetSelectiveRetransmissions(uint8_t settings);
319
320  // Send a Negative acknowledgment packet.
321  virtual WebRtc_Word32 SendNACK(const WebRtc_UWord16* nack_list,
322                                 const WebRtc_UWord16 size);
323
324  // Store the sent packets, needed to answer to a negative acknowledgment
325  // requests.
326  virtual WebRtc_Word32 SetStorePacketsStatus(
327      const bool enable, const WebRtc_UWord16 number_to_store = 200);
328
329  // (APP) Application specific data.
330  virtual WebRtc_Word32 SetRTCPApplicationSpecificData(
331      const WebRtc_UWord8 sub_type,
332      const WebRtc_UWord32 name,
333      const WebRtc_UWord8* data,
334      const WebRtc_UWord16 length);
335
336  // (XR) VOIP metric.
337  virtual WebRtc_Word32 SetRTCPVoIPMetrics(const RTCPVoIPMetric* VoIPMetric);
338
339  // Audio part.
340
341  // Set audio packet size, used to determine when it's time to send a DTMF
342  // packet in silence (CNG).
343  virtual WebRtc_Word32 SetAudioPacketSize(
344      const WebRtc_UWord16 packet_size_samples);
345
346  // Outband DTMF detection.
347  virtual WebRtc_Word32 SetTelephoneEventStatus(
348      const bool enable,
349      const bool forward_to_decoder,
350      const bool detect_end_of_tone = false);
351
352  // Is outband DTMF turned on/off?
353  virtual bool TelephoneEvent() const;
354
355  // Is forwarding of outband telephone events turned on/off?
356  virtual bool TelephoneEventForwardToDecoder() const;
357
358  virtual bool SendTelephoneEventActive(WebRtc_Word8& telephone_event) const;
359
360  // Send a TelephoneEvent tone using RFC 2833 (4733).
361  virtual WebRtc_Word32 SendTelephoneEventOutband(const WebRtc_UWord8 key,
362                                                  const WebRtc_UWord16 time_ms,
363                                                  const WebRtc_UWord8 level);
364
365  // Set payload type for Redundant Audio Data RFC 2198.
366  virtual WebRtc_Word32 SetSendREDPayloadType(const WebRtc_Word8 payload_type);
367
368  // Get payload type for Redundant Audio Data RFC 2198.
369  virtual WebRtc_Word32 SendREDPayloadType(WebRtc_Word8& payload_type) const;
370
371  // Set status and id for header-extension-for-audio-level-indication.
372  virtual WebRtc_Word32 SetRTPAudioLevelIndicationStatus(
373      const bool enable, const WebRtc_UWord8 id);
374
375  // Get status and id for header-extension-for-audio-level-indication.
376  virtual WebRtc_Word32 GetRTPAudioLevelIndicationStatus(
377      bool& enable, WebRtc_UWord8& id) const;
378
379  // Store the audio level in d_bov for header-extension-for-audio-level-
380  // indication.
381  virtual WebRtc_Word32 SetAudioLevel(const WebRtc_UWord8 level_d_bov);
382
383  // Video part.
384
385  virtual RtpVideoCodecTypes ReceivedVideoCodec() const;
386
387  virtual RtpVideoCodecTypes SendVideoCodec() const;
388
389  virtual WebRtc_Word32 SendRTCPSliceLossIndication(
390      const WebRtc_UWord8 picture_id);
391
392  // Set method for requestion a new key frame.
393  virtual WebRtc_Word32 SetKeyFrameRequestMethod(
394      const KeyFrameRequestMethod method);
395
396  // Send a request for a keyframe.
397  virtual WebRtc_Word32 RequestKeyFrame();
398
399  virtual WebRtc_Word32 SetCameraDelay(const WebRtc_Word32 delay_ms);
400
401  virtual void SetTargetSendBitrate(const WebRtc_UWord32 bitrate);
402
403  virtual WebRtc_Word32 SetGenericFECStatus(
404      const bool enable,
405      const WebRtc_UWord8 payload_type_red,
406      const WebRtc_UWord8 payload_type_fec);
407
408  virtual WebRtc_Word32 GenericFECStatus(
409      bool& enable,
410      WebRtc_UWord8& payload_type_red,
411      WebRtc_UWord8& payload_type_fec);
412
413  virtual WebRtc_Word32 SetFecParameters(
414      const FecProtectionParams* delta_params,
415      const FecProtectionParams* key_params);
416
417  virtual WebRtc_Word32 LastReceivedNTP(WebRtc_UWord32& NTPsecs,
418                                        WebRtc_UWord32& NTPfrac,
419                                        WebRtc_UWord32& remote_sr);
420
421  virtual WebRtc_Word32 BoundingSet(bool& tmmbr_owner,
422                                    TMMBRSet*& bounding_set_rec);
423
424  virtual void BitrateSent(WebRtc_UWord32* total_rate,
425                           WebRtc_UWord32* video_rate,
426                           WebRtc_UWord32* fec_rate,
427                           WebRtc_UWord32* nackRate) const;
428
429  virtual int EstimatedReceiveBandwidth(
430      WebRtc_UWord32* available_bandwidth) const;
431
432  virtual void SetRemoteSSRC(const WebRtc_UWord32 ssrc);
433
434  virtual WebRtc_UWord32 SendTimeOfSendReport(const WebRtc_UWord32 send_report);
435
436  // Good state of RTP receiver inform sender.
437  virtual WebRtc_Word32 SendRTCPReferencePictureSelection(
438      const WebRtc_UWord64 picture_id);
439
440  void OnReceivedTMMBR();
441
442  // Bad state of RTP receiver request a keyframe.
443  void OnRequestIntraFrame();
444
445  // Received a request for a new SLI.
446  void OnReceivedSliceLossIndication(const WebRtc_UWord8 picture_id);
447
448  // Received a new reference frame.
449  void OnReceivedReferencePictureSelectionIndication(
450      const WebRtc_UWord64 picture_id);
451
452  void OnReceivedNACK(const WebRtc_UWord16 nack_sequence_numbers_length,
453                      const WebRtc_UWord16* nack_sequence_numbers);
454
455  void OnRequestSendReport();
456
457  // Following function is only called when constructing the object so no
458  // need to worry about data race.
459  void OwnsClock() {
460    owns_clock_ = true;
461  }
462
463 protected:
464  void RegisterChildModule(RtpRtcp* module);
465
466  void DeRegisterChildModule(RtpRtcp* module);
467
468  bool UpdateRTCPReceiveInformationTimers();
469
470  void ProcessDeadOrAliveTimer();
471
472  WebRtc_UWord32 BitrateReceivedNow() const;
473
474  // Get remote SequenceNumber.
475  WebRtc_UWord16 RemoteSequenceNumber() const;
476
477  // Only for internal testing.
478  WebRtc_UWord32 LastSendReport(WebRtc_UWord32& last_rtcptime);
479
480  RTPSender                 rtp_sender_;
481  RTPReceiver               rtp_receiver_;
482
483  RTCPSender                rtcp_sender_;
484  RTCPReceiver              rtcp_receiver_;
485
486  bool                      owns_clock_;
487  Clock&                    clock_;
488
489 private:
490  int64_t RtcpReportInterval();
491
492  WebRtc_Word32             id_;
493  const bool                audio_;
494  bool                      collision_detected_;
495  WebRtc_Word64             last_process_time_;
496  WebRtc_Word64             last_bitrate_process_time_;
497  WebRtc_Word64             last_packet_timeout_process_time_;
498  WebRtc_UWord16            packet_overhead_;
499
500  scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_;
501  scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_feedback_;
502  ModuleRtpRtcpImpl*            default_module_;
503  std::list<ModuleRtpRtcpImpl*> child_modules_;
504
505  // Dead or alive.
506  bool                  dead_or_alive_active_;
507  WebRtc_UWord32        dead_or_alive_timeout_ms_;
508  WebRtc_Word64         dead_or_alive_last_timer_;
509  // Send side
510  NACKMethod            nack_method_;
511  WebRtc_UWord32        nack_last_time_sent_;
512  WebRtc_UWord16        nack_last_seq_number_sent_;
513
514  bool                  simulcast_;
515  VideoCodec            send_video_codec_;
516  KeyFrameRequestMethod key_frame_req_method_;
517
518  RemoteBitrateEstimator* remote_bitrate_;
519
520  RtcpRttObserver* rtt_observer_;
521
522#ifdef MATLAB
523  MatlabPlot*           plot1_;
524#endif
525};
526
527}  // namespace webrtc
528
529#endif  // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_RTCP_IMPL_H_
530