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