Home | History | Annotate | Download | only in video_engine
      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_CHANNEL_H_
     12 #define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
     13 
     14 #include <list>
     15 
     16 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
     17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
     18 #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
     19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     20 #include "webrtc/system_wrappers/interface/tick_util.h"
     21 #include "webrtc/typedefs.h"
     22 #include "webrtc/video_engine/include/vie_network.h"
     23 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
     24 #include "webrtc/video_engine/vie_defines.h"
     25 #include "webrtc/video_engine/vie_frame_provider_base.h"
     26 #include "webrtc/video_engine/vie_receiver.h"
     27 #include "webrtc/video_engine/vie_sender.h"
     28 #include "webrtc/video_engine/vie_sync_module.h"
     29 
     30 namespace webrtc {
     31 
     32 class CallStatsObserver;
     33 class ChannelStatsObserver;
     34 class Config;
     35 class CriticalSectionWrapper;
     36 class EncodedImageCallback;
     37 class I420FrameCallback;
     38 class PacedSender;
     39 class ProcessThread;
     40 class RtcpRttStats;
     41 class RtpRtcp;
     42 class ThreadWrapper;
     43 class ViEDecoderObserver;
     44 class ViEEffectFilter;
     45 class ViERTCPObserver;
     46 class ViERTPObserver;
     47 class VideoCodingModule;
     48 class VideoDecoder;
     49 class VideoRenderCallback;
     50 class VoEVideoSync;
     51 
     52 class ViEChannel
     53     : public VCMFrameTypeCallback,
     54       public VCMReceiveCallback,
     55       public VCMReceiveStatisticsCallback,
     56       public VCMDecoderTimingCallback,
     57       public VCMPacketRequestCallback,
     58       public RtcpFeedback,
     59       public RtpFeedback,
     60       public ViEFrameProviderBase {
     61  public:
     62   friend class ChannelStatsObserver;
     63 
     64   ViEChannel(int32_t channel_id,
     65              int32_t engine_id,
     66              uint32_t number_of_cores,
     67              const Config& config,
     68              ProcessThread& module_process_thread,
     69              RtcpIntraFrameObserver* intra_frame_observer,
     70              RtcpBandwidthObserver* bandwidth_observer,
     71              RemoteBitrateEstimator* remote_bitrate_estimator,
     72              RtcpRttStats* rtt_stats,
     73              PacedSender* paced_sender,
     74              RtpRtcp* default_rtp_rtcp,
     75              bool sender);
     76   ~ViEChannel();
     77 
     78   int32_t Init();
     79 
     80   // Sets the encoder to use for the channel. |new_stream| indicates the encoder
     81   // type has changed and we should start a new RTP stream.
     82   int32_t SetSendCodec(const VideoCodec& video_codec, bool new_stream = true);
     83   int32_t SetReceiveCodec(const VideoCodec& video_codec);
     84   int32_t GetReceiveCodec(VideoCodec* video_codec);
     85   int32_t RegisterCodecObserver(ViEDecoderObserver* observer);
     86   // Registers an external decoder. |buffered_rendering| means that the decoder
     87   // will render frames after decoding according to the render timestamp
     88   // provided by the video coding module. |render_delay| indicates the time
     89   // needed to decode and render a frame.
     90   int32_t RegisterExternalDecoder(const uint8_t pl_type,
     91                                   VideoDecoder* decoder,
     92                                   bool buffered_rendering,
     93                                   int32_t render_delay);
     94   int32_t DeRegisterExternalDecoder(const uint8_t pl_type);
     95   int32_t ReceiveCodecStatistics(uint32_t* num_key_frames,
     96                                  uint32_t* num_delta_frames);
     97   uint32_t DiscardedPackets() const;
     98 
     99   // Returns the estimated delay in milliseconds.
    100   int ReceiveDelay() const;
    101 
    102   // Only affects calls to SetReceiveCodec done after this call.
    103   int32_t WaitForKeyFrame(bool wait);
    104 
    105   // If enabled, a key frame request will be sent as soon as there are lost
    106   // packets. If |only_key_frames| are set, requests are only sent for loss in
    107   // key frames.
    108   int32_t SetSignalPacketLossStatus(bool enable, bool only_key_frames);
    109 
    110   int32_t SetRTCPMode(const RTCPMethod rtcp_mode);
    111   int32_t GetRTCPMode(RTCPMethod* rtcp_mode);
    112   int32_t SetNACKStatus(const bool enable);
    113   int32_t SetFECStatus(const bool enable,
    114                        const unsigned char payload_typeRED,
    115                        const unsigned char payload_typeFEC);
    116   int32_t SetHybridNACKFECStatus(const bool enable,
    117                                  const unsigned char payload_typeRED,
    118                                  const unsigned char payload_typeFEC);
    119   int SetSenderBufferingMode(int target_delay_ms);
    120   int SetReceiverBufferingMode(int target_delay_ms);
    121   int32_t SetKeyFrameRequestMethod(const KeyFrameRequestMethod method);
    122   bool EnableRemb(bool enable);
    123   int SetSendTimestampOffsetStatus(bool enable, int id);
    124   int SetReceiveTimestampOffsetStatus(bool enable, int id);
    125   int SetSendAbsoluteSendTimeStatus(bool enable, int id);
    126   int SetReceiveAbsoluteSendTimeStatus(bool enable, int id);
    127   bool GetReceiveAbsoluteSendTimeStatus() const;
    128   void SetRtcpXrRrtrStatus(bool enable);
    129   void SetTransmissionSmoothingStatus(bool enable);
    130   int32_t EnableTMMBR(const bool enable);
    131   int32_t EnableKeyFrameRequestCallback(const bool enable);
    132 
    133   // Sets SSRC for outgoing stream.
    134   int32_t SetSSRC(const uint32_t SSRC,
    135                   const StreamType usage,
    136                   const unsigned char simulcast_idx);
    137 
    138   // Gets SSRC for outgoing stream number |idx|.
    139   int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
    140 
    141   // Gets SSRC for the incoming stream.
    142   int32_t GetRemoteSSRC(uint32_t* ssrc);
    143 
    144   // Gets the CSRC for the incoming stream.
    145   int32_t GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]);
    146 
    147   int SetRtxSendPayloadType(int payload_type);
    148   // Only has an effect once RTX is enabled.
    149   void SetPadWithRedundantPayloads(bool enable);
    150   void SetRtxReceivePayloadType(int payload_type);
    151 
    152   // Sets the starting sequence number, must be called before StartSend.
    153   int32_t SetStartSequenceNumber(uint16_t sequence_number);
    154 
    155   // Sets the CName for the outgoing stream on the channel.
    156   int32_t SetRTCPCName(const char rtcp_cname[]);
    157 
    158   // Gets the CName for the outgoing stream on the channel.
    159   int32_t GetRTCPCName(char rtcp_cname[]);
    160 
    161   // Gets the CName of the incoming stream.
    162   int32_t GetRemoteRTCPCName(char rtcp_cname[]);
    163   int32_t RegisterRtpObserver(ViERTPObserver* observer);
    164   int32_t RegisterRtcpObserver(ViERTCPObserver* observer);
    165   int32_t SendApplicationDefinedRTCPPacket(
    166       const uint8_t sub_type,
    167       uint32_t name,
    168       const uint8_t* data,
    169       uint16_t data_length_in_bytes);
    170 
    171   // Returns statistics reported by the remote client in an RTCP packet.
    172   int32_t GetSendRtcpStatistics(uint16_t* fraction_lost,
    173                                 uint32_t* cumulative_lost,
    174                                 uint32_t* extended_max,
    175                                 uint32_t* jitter_samples,
    176                                 int32_t* rtt_ms);
    177 
    178   // Called on receipt of RTCP report block from remote side.
    179   void RegisterSendChannelRtcpStatisticsCallback(
    180       RtcpStatisticsCallback* callback);
    181 
    182   // Returns our localy created statistics of the received RTP stream.
    183   int32_t GetReceivedRtcpStatistics(uint16_t* fraction_lost,
    184                                     uint32_t* cumulative_lost,
    185                                     uint32_t* extended_max,
    186                                     uint32_t* jitter_samples,
    187                                     int32_t* rtt_ms);
    188 
    189   // Called on generation of RTCP stats
    190   void RegisterReceiveChannelRtcpStatisticsCallback(
    191       RtcpStatisticsCallback* callback);
    192 
    193   // Gets sent/received packets statistics.
    194   int32_t GetRtpStatistics(uint32_t* bytes_sent,
    195                            uint32_t* packets_sent,
    196                            uint32_t* bytes_received,
    197                            uint32_t* packets_received) const;
    198 
    199   // Called on update of RTP statistics.
    200   void RegisterSendChannelRtpStatisticsCallback(
    201       StreamDataCountersCallback* callback);
    202 
    203   // Called on update of RTP statistics.
    204   void RegisterReceiveChannelRtpStatisticsCallback(
    205       StreamDataCountersCallback* callback);
    206 
    207   void GetRtcpPacketTypeCounters(RtcpPacketTypeCounter* packets_sent,
    208                                  RtcpPacketTypeCounter* packets_received) const;
    209 
    210   void GetBandwidthUsage(uint32_t* total_bitrate_sent,
    211                          uint32_t* video_bitrate_sent,
    212                          uint32_t* fec_bitrate_sent,
    213                          uint32_t* nackBitrateSent) const;
    214   bool GetSendSideDelay(int* avg_send_delay, int* max_send_delay) const;
    215   void GetReceiveBandwidthEstimatorStats(
    216       ReceiveBandwidthEstimatorStats* output) const;
    217 
    218   // Called on any new send bitrate estimate.
    219   void RegisterSendBitrateObserver(BitrateStatisticsObserver* observer);
    220 
    221   int32_t StartRTPDump(const char file_nameUTF8[1024],
    222                        RTPDirections direction);
    223   int32_t StopRTPDump(RTPDirections direction);
    224 
    225   // Implements RtcpFeedback.
    226   // TODO(pwestin) Depricate this functionality.
    227   virtual void OnApplicationDataReceived(const int32_t id,
    228                                          const uint8_t sub_type,
    229                                          const uint32_t name,
    230                                          const uint16_t length,
    231                                          const uint8_t* data);
    232   // Implements RtpFeedback.
    233   virtual int32_t OnInitializeDecoder(
    234       const int32_t id,
    235       const int8_t payload_type,
    236       const char payload_name[RTP_PAYLOAD_NAME_SIZE],
    237       const int frequency,
    238       const uint8_t channels,
    239       const uint32_t rate);
    240   virtual void OnIncomingSSRCChanged(const int32_t id,
    241                                      const uint32_t ssrc);
    242   virtual void OnIncomingCSRCChanged(const int32_t id,
    243                                      const uint32_t CSRC,
    244                                      const bool added);
    245   virtual void ResetStatistics(uint32_t);
    246 
    247   int32_t SetLocalReceiver(const uint16_t rtp_port,
    248                            const uint16_t rtcp_port,
    249                            const char* ip_address);
    250   int32_t GetLocalReceiver(uint16_t* rtp_port,
    251                            uint16_t* rtcp_port,
    252                            char* ip_address) const;
    253   int32_t SetSendDestination(const char* ip_address,
    254                              const uint16_t rtp_port,
    255                              const uint16_t rtcp_port,
    256                              const uint16_t source_rtp_port,
    257                              const uint16_t source_rtcp_port);
    258   int32_t GetSendDestination(char* ip_address,
    259                              uint16_t* rtp_port,
    260                              uint16_t* rtcp_port,
    261                              uint16_t* source_rtp_port,
    262                              uint16_t* source_rtcp_port) const;
    263   int32_t GetSourceInfo(uint16_t* rtp_port,
    264                         uint16_t* rtcp_port,
    265                         char* ip_address,
    266                         uint32_t ip_address_length);
    267 
    268   int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC);
    269 
    270   int32_t StartSend();
    271   int32_t StopSend();
    272   bool Sending();
    273   int32_t StartReceive();
    274   int32_t StopReceive();
    275 
    276   int32_t RegisterSendTransport(Transport* transport);
    277   int32_t DeregisterSendTransport();
    278 
    279   // Incoming packet from external transport.
    280   int32_t ReceivedRTPPacket(const void* rtp_packet,
    281                             const int32_t rtp_packet_length,
    282                             const PacketTime& packet_time);
    283 
    284   // Incoming packet from external transport.
    285   int32_t ReceivedRTCPPacket(const void* rtcp_packet,
    286                              const int32_t rtcp_packet_length);
    287 
    288   // Sets the maximum transfer unit size for the network link, i.e. including
    289   // IP, UDP and RTP headers.
    290   int32_t SetMTU(uint16_t mtu);
    291 
    292   // Returns maximum allowed payload size, i.e. the maximum allowed size of
    293   // encoded data in each packet.
    294   uint16_t MaxDataPayloadLength() const;
    295   int32_t SetMaxPacketBurstSize(uint16_t max_number_of_packets);
    296   int32_t SetPacketBurstSpreadState(bool enable, const uint16_t frame_periodMS);
    297 
    298   int32_t EnableColorEnhancement(bool enable);
    299 
    300   // Gets the modules used by the channel.
    301   RtpRtcp* rtp_rtcp();
    302 
    303   CallStatsObserver* GetStatsObserver();
    304 
    305   // Implements VCMReceiveCallback.
    306   virtual int32_t FrameToRender(I420VideoFrame& video_frame);  // NOLINT
    307 
    308   // Implements VCMReceiveCallback.
    309   virtual int32_t ReceivedDecodedReferenceFrame(
    310       const uint64_t picture_id);
    311 
    312   // Implements VCMReceiveCallback.
    313   virtual void IncomingCodecChanged(const VideoCodec& codec);
    314 
    315   // Implements VCMReceiveStatisticsCallback.
    316   virtual int32_t OnReceiveStatisticsUpdate(const uint32_t bit_rate,
    317                                     const uint32_t frame_rate);
    318 
    319   // Implements VCMDecoderTimingCallback.
    320   virtual void OnDecoderTiming(int decode_ms,
    321                                int max_decode_ms,
    322                                int current_delay_ms,
    323                                int target_delay_ms,
    324                                int jitter_buffer_ms,
    325                                int min_playout_delay_ms,
    326                                int render_delay_ms);
    327 
    328   // Implements VideoFrameTypeCallback.
    329   virtual int32_t RequestKeyFrame();
    330 
    331   // Implements VideoFrameTypeCallback.
    332   virtual int32_t SliceLossIndicationRequest(
    333       const uint64_t picture_id);
    334 
    335   // Implements VideoPacketRequestCallback.
    336   virtual int32_t ResendPackets(const uint16_t* sequence_numbers,
    337                                 uint16_t length);
    338 
    339   int32_t SetVoiceChannel(int32_t ve_channel_id,
    340                           VoEVideoSync* ve_sync_interface);
    341   int32_t VoiceChannel();
    342 
    343   // Implements ViEFrameProviderBase.
    344   virtual int FrameCallbackChanged() {return -1;}
    345 
    346   int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
    347 
    348   // New-style callbacks, used by VideoReceiveStream.
    349   void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback);
    350   void RegisterPreDecodeImageCallback(
    351       EncodedImageCallback* pre_decode_callback);
    352 
    353   void RegisterSendFrameCountObserver(FrameCountObserver* observer);
    354 
    355   void ReceivedBWEPacket(int64_t arrival_time_ms, int payload_size,
    356                          const RTPHeader& header);
    357 
    358  protected:
    359   static bool ChannelDecodeThreadFunction(void* obj);
    360   bool ChannelDecodeProcess();
    361 
    362   void OnRttUpdate(uint32_t rtt);
    363 
    364  private:
    365   // Assumed to be protected.
    366   int32_t StartDecodeThread();
    367   int32_t StopDecodeThread();
    368 
    369   int32_t ProcessNACKRequest(const bool enable);
    370   int32_t ProcessFECRequest(const bool enable,
    371                             const unsigned char payload_typeRED,
    372                             const unsigned char payload_typeFEC);
    373   // Compute NACK list parameters for the buffering mode.
    374   int GetRequiredNackListSize(int target_delay_ms);
    375   void SetRtxSendStatus(bool enable);
    376 
    377   int32_t channel_id_;
    378   int32_t engine_id_;
    379   uint32_t number_of_cores_;
    380   uint8_t num_socket_threads_;
    381 
    382   // Used for all registered callbacks except rendering.
    383   scoped_ptr<CriticalSectionWrapper> callback_cs_;
    384   scoped_ptr<CriticalSectionWrapper> rtp_rtcp_cs_;
    385 
    386   RtpRtcp* default_rtp_rtcp_;
    387 
    388   // Owned modules/classes.
    389   scoped_ptr<RtpRtcp> rtp_rtcp_;
    390   std::list<RtpRtcp*> simulcast_rtp_rtcp_;
    391   std::list<RtpRtcp*> removed_rtp_rtcp_;
    392   VideoCodingModule* const vcm_;
    393   ViEReceiver vie_receiver_;
    394   ViESender vie_sender_;
    395   ViESyncModule vie_sync_;
    396 
    397   // Helper to report call statistics.
    398   scoped_ptr<ChannelStatsObserver> stats_observer_;
    399 
    400   // Not owned.
    401   ProcessThread& module_process_thread_;
    402   ViEDecoderObserver* codec_observer_;
    403   bool do_key_frame_callbackRequest_;
    404   ViERTPObserver* rtp_observer_;
    405   ViERTCPObserver* rtcp_observer_;
    406   RtcpIntraFrameObserver* intra_frame_observer_;
    407   RtcpRttStats* rtt_stats_;
    408   PacedSender* paced_sender_;
    409   bool pad_with_redundant_payloads_;
    410 
    411   scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_;
    412   int send_timestamp_extension_id_;
    413   int absolute_send_time_extension_id_;
    414 
    415   Transport* external_transport_;
    416 
    417   bool decoder_reset_;
    418   // Current receive codec used for codec change callback.
    419   VideoCodec receive_codec_;
    420   bool wait_for_key_frame_;
    421   ThreadWrapper* decode_thread_;
    422 
    423   ViEEffectFilter* effect_filter_;
    424   bool color_enhancement_;
    425 
    426   // User set MTU, -1 if not set.
    427   uint16_t mtu_;
    428   const bool sender_;
    429 
    430   int nack_history_size_sender_;
    431   int max_nack_reordering_threshold_;
    432   I420FrameCallback* pre_render_callback_;
    433 };
    434 
    435 }  // namespace webrtc
    436 
    437 #endif  // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
    438