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_VIDEO_ENGINE_VIE_RECEIVER_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_RECEIVER_H_
13 
14 #include <list>
15 
16 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/engine_configurations.h"
18 #include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20 #include "webrtc/typedefs.h"
21 #include "webrtc/video_engine/include/vie_network.h"
22 #include "webrtc/video_engine/vie_defines.h"
23 
24 namespace webrtc {
25 
26 class CriticalSectionWrapper;
27 class FecReceiver;
28 class RemoteNtpTimeEstimator;
29 class ReceiveStatistics;
30 class RemoteBitrateEstimator;
31 class RtpDump;
32 class RtpHeaderParser;
33 class RTPPayloadRegistry;
34 class RtpReceiver;
35 class RtpRtcp;
36 class VideoCodingModule;
37 struct ReceiveBandwidthEstimatorStats;
38 
39 class ViEReceiver : public RtpData {
40  public:
41   ViEReceiver(const int32_t channel_id, VideoCodingModule* module_vcm,
42               RemoteBitrateEstimator* remote_bitrate_estimator,
43               RtpFeedback* rtp_feedback);
44   ~ViEReceiver();
45 
46   bool SetReceiveCodec(const VideoCodec& video_codec);
47   bool RegisterPayload(const VideoCodec& video_codec);
48 
49   void SetNackStatus(bool enable, int max_nack_reordering_threshold);
50   void SetRtxPayloadType(int payload_type);
51   void SetRtxSsrc(uint32_t ssrc);
52   bool GetRtxSsrc(uint32_t* ssrc) const;
53 
54   bool IsFecEnabled() const;
55 
56   uint32_t GetRemoteSsrc() const;
57   int GetCsrcs(uint32_t* csrcs) const;
58   void GetRID(char rid[256]) const;
59 
60   void SetRtpRtcpModule(RtpRtcp* module);
61 
62   RtpReceiver* GetRtpReceiver() const;
63 
64   void RegisterSimulcastRtpRtcpModules(const std::list<RtpRtcp*>& rtp_modules);
65 
66   bool SetReceiveTimestampOffsetStatus(bool enable, int id);
67   bool SetReceiveAbsoluteSendTimeStatus(bool enable, int id);
68   bool SetReceiveVideoRotationStatus(bool enable, int id);
69   bool SetReceiveRIDStatus(bool enable, int id);
70 
71   void StartReceive();
72   void StopReceive();
73 
74   void StartRTCPReceive();
75   void StopRTCPReceive();
76 
77   int StartRTPDump(const char file_nameUTF8[1024]);
78   int StopRTPDump();
79 
80   // Receives packets from external transport.
81   int ReceivedRTPPacket(const void* rtp_packet, size_t rtp_packet_length,
82                         const PacketTime& packet_time);
83   int ReceivedRTCPPacket(const void* rtcp_packet, size_t rtcp_packet_length);
84 
85   // Implements RtpData.
86   int32_t OnReceivedPayloadData(const uint8_t* payload_data,
87                                 const size_t payload_size,
88                                 const WebRtcRTPHeader* rtp_header) override;
89   bool OnRecoveredPacket(const uint8_t* packet, size_t packet_length) override;
90 
91   ReceiveStatistics* GetReceiveStatistics() const;
92 
93   void ReceivedBWEPacket(int64_t arrival_time_ms, size_t payload_size,
94                          const RTPHeader& header);
95  private:
96   int InsertRTPPacket(const uint8_t* rtp_packet, size_t rtp_packet_length,
97                       const PacketTime& packet_time);
98   bool ReceivePacket(const uint8_t* packet,
99                      size_t packet_length,
100                      const RTPHeader& header,
101                      bool in_order);
102   // Parses and handles for instance RTX and RED headers.
103   // This function assumes that it's being called from only one thread.
104   bool ParseAndHandleEncapsulatingHeader(const uint8_t* packet,
105                                          size_t packet_length,
106                                          const RTPHeader& header);
107   void NotifyReceiverOfFecPacket(const RTPHeader& header);
108   int InsertRTCPPacket(const uint8_t* rtcp_packet, size_t rtcp_packet_length);
109   bool IsPacketInOrder(const RTPHeader& header) const;
110   bool IsPacketRetransmitted(const RTPHeader& header, bool in_order) const;
111   void UpdateHistograms();
112 
113   rtc::scoped_ptr<CriticalSectionWrapper> receive_cs_;
114   Clock* clock_;
115   rtc::scoped_ptr<RtpHeaderParser> rtp_header_parser_;
116   rtc::scoped_ptr<RTPPayloadRegistry> rtp_payload_registry_;
117   rtc::scoped_ptr<RtpReceiver> rtp_receiver_;
118   rtc::scoped_ptr<ReceiveStatistics> rtp_receive_statistics_;
119   rtc::scoped_ptr<FecReceiver> fec_receiver_;
120   RtpRtcp* rtp_rtcp_;
121   std::list<RtpRtcp*> rtp_rtcp_simulcast_;
122   VideoCodingModule* vcm_;
123   RemoteBitrateEstimator* remote_bitrate_estimator_;
124 
125   rtc::scoped_ptr<RemoteNtpTimeEstimator> ntp_estimator_;
126 
127   RtpDump* rtp_dump_;
128   bool receiving_;
129   bool receiving_rtcp_;
130   uint8_t restored_packet_[kViEMaxMtu];
131   bool restored_packet_in_use_;
132   bool receiving_ast_enabled_;
133   bool receiving_cvo_enabled_;
134   bool receiving_rid_enabled_;
135   int64_t last_packet_log_ms_;
136 };
137 
138 }  // namespace webrt
139 
140 #endif  // WEBRTC_VIDEO_ENGINE_VIE_RECEIVER_H_
141