Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / video_engine / vie_encoder.h
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_ENCODER_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
13
14 #include <list>
15 #include <map>
16
17 #include "webrtc/common_types.h"
18 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
19 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20 #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
21 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
22 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
23 #include "webrtc/system_wrappers/interface/thread_annotations.h"
24 #include "webrtc/typedefs.h"
25 #include "webrtc/frame_callback.h"
26 #include "webrtc/video_engine/vie_defines.h"
27 #include "webrtc/video_engine/vie_frame_provider_base.h"
28
29 namespace webrtc {
30
31 class Config;
32 class CriticalSectionWrapper;
33 class EncodedImageCallback;
34 class PacedSender;
35 class ProcessThread;
36 class QMVideoSettingsCallback;
37 class RtpRtcp;
38 class ViEBitrateObserver;
39 class ViEEffectFilter;
40 class ViEEncoderObserver;
41 class VideoCodingModule;
42 class ViEPacedSenderCallback;
43
44 class ViEEncoder
45     : public RtcpIntraFrameObserver,
46       public VCMPacketizationCallback,
47       public VCMProtectionCallback,
48       public VCMSendStatisticsCallback,
49       public ViEFrameCallback {
50  public:
51   friend class ViEBitrateObserver;
52   friend class ViEPacedSenderCallback;
53
54   ViEEncoder(int32_t engine_id,
55              int32_t channel_id,
56              uint32_t number_of_cores,
57              const Config& config,
58              ProcessThread& module_process_thread,
59              BitrateController* bitrate_controller);
60   ~ViEEncoder();
61
62   bool Init();
63
64   void SetNetworkTransmissionState(bool is_transmitting);
65
66   // Returns the id of the owning channel.
67   int Owner() const;
68
69   // Drops incoming packets before they get to the encoder.
70   void Pause();
71   void Restart();
72
73   // Codec settings.
74   uint8_t NumberOfCodecs();
75   int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
76   int32_t RegisterExternalEncoder(VideoEncoder* encoder,
77                                   uint8_t pl_type,
78                                   bool internal_source);
79   int32_t DeRegisterExternalEncoder(uint8_t pl_type);
80   int32_t SetEncoder(const VideoCodec& video_codec);
81   int32_t GetEncoder(VideoCodec* video_codec);
82
83   int32_t GetCodecConfigParameters(
84     unsigned char config_parameters[kConfigParameterSize],
85     unsigned char& config_parameters_size);
86
87   PacedSender* GetPacedSender();
88
89   // Scale or crop/pad image.
90   int32_t ScaleInputImage(bool enable);
91
92   // RTP settings.
93   RtpRtcp* SendRtpRtcpModule();
94
95   // Implementing ViEFrameCallback.
96   virtual void DeliverFrame(int id,
97                             I420VideoFrame* video_frame,
98                             int num_csrcs = 0,
99                             const uint32_t CSRC[kRtpCsrcSize] = NULL);
100   virtual void DelayChanged(int id, int frame_delay);
101   virtual int GetPreferedFrameSettings(int* width,
102                                        int* height,
103                                        int* frame_rate);
104
105   virtual void ProviderDestroyed(int id) {
106     return;
107   }
108
109   int32_t SendKeyFrame();
110   int32_t SendCodecStatistics(uint32_t* num_key_frames,
111                               uint32_t* num_delta_frames);
112
113   int PacerQueuingDelayMs() const;
114   int32_t EstimatedSendBandwidth(
115         uint32_t* available_bandwidth) const;
116
117   int CodecTargetBitrate(uint32_t* bitrate) const;
118   // Loss protection.
119   int32_t UpdateProtectionMethod(bool enable_nack);
120   bool nack_enabled() const { return nack_enabled_; }
121
122   // Buffering mode.
123   void SetSenderBufferingMode(int target_delay_ms);
124
125   // Implements VCMPacketizationCallback.
126   virtual int32_t SendData(
127     FrameType frame_type,
128     uint8_t payload_type,
129     uint32_t time_stamp,
130     int64_t capture_time_ms,
131     const uint8_t* payload_data,
132     uint32_t payload_size,
133     const RTPFragmentationHeader& fragmentation_header,
134     const RTPVideoHeader* rtp_video_hdr);
135
136   // Implements VideoProtectionCallback.
137   virtual int ProtectionRequest(
138       const FecProtectionParams* delta_fec_params,
139       const FecProtectionParams* key_fec_params,
140       uint32_t* sent_video_rate_bps,
141       uint32_t* sent_nack_rate_bps,
142       uint32_t* sent_fec_rate_bps);
143
144   // Implements VideoSendStatisticsCallback.
145   virtual int32_t SendStatistics(const uint32_t bit_rate,
146                                  const uint32_t frame_rate);
147   int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
148
149   // Implements RtcpIntraFrameObserver.
150   virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
151   virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
152   virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
153   virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
154
155   // Sets SSRCs for all streams.
156   bool SetSsrcs(const std::list<unsigned int>& ssrcs);
157
158   void SetMinTransmitBitrate(int min_transmit_bitrate_kbps);
159
160   // Effect filter.
161   int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
162
163   // Enables recording of debugging information.
164   virtual int StartDebugRecording(const char* fileNameUTF8);
165
166   // Disables recording of debugging information.
167   virtual int StopDebugRecording();
168
169   // Lets the sender suspend video when the rate drops below
170   // |threshold_bps|, and turns back on when the rate goes back up above
171   // |threshold_bps| + |window_bps|.
172   virtual void SuspendBelowMinBitrate();
173
174   // New-style callbacks, used by VideoSendStream.
175   void RegisterPreEncodeCallback(I420FrameCallback* pre_encode_callback);
176   void DeRegisterPreEncodeCallback();
177   void RegisterPostEncodeImageCallback(
178         EncodedImageCallback* post_encode_callback);
179   void DeRegisterPostEncodeImageCallback();
180
181   int channel_id() const { return channel_id_; }
182
183  protected:
184   // Called by BitrateObserver.
185   void OnNetworkChanged(const uint32_t bitrate_bps,
186                         const uint8_t fraction_lost,
187                         const uint32_t round_trip_time_ms);
188
189   // Called by PacedSender.
190   bool TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
191                         int64_t capture_time_ms, bool retransmission);
192   int TimeToSendPadding(int bytes);
193  private:
194   bool EncoderPaused() const;
195
196   int32_t engine_id_;
197   const int channel_id_;
198   const uint32_t number_of_cores_;
199
200   VideoCodingModule& vcm_;
201   VideoProcessingModule& vpm_;
202   scoped_ptr<RtpRtcp> default_rtp_rtcp_;
203   scoped_ptr<CriticalSectionWrapper> callback_cs_;
204   scoped_ptr<CriticalSectionWrapper> data_cs_;
205   scoped_ptr<BitrateObserver> bitrate_observer_;
206   scoped_ptr<PacedSender> paced_sender_;
207   scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
208
209   BitrateController* bitrate_controller_;
210
211   int64_t time_of_last_incoming_frame_ms_;
212   bool send_padding_;
213   int min_transmit_bitrate_kbps_ GUARDED_BY(data_cs_);
214   int target_delay_ms_;
215   bool network_is_transmitting_;
216   bool encoder_paused_;
217   bool encoder_paused_and_dropped_frame_;
218   std::map<unsigned int, int64_t> time_last_intra_request_ms_;
219
220   bool fec_enabled_;
221   bool nack_enabled_;
222
223   ViEEncoderObserver* codec_observer_ GUARDED_BY(callback_cs_);
224   ViEEffectFilter* effect_filter_;
225   ProcessThread& module_process_thread_;
226
227   bool has_received_sli_;
228   uint8_t picture_id_sli_;
229   bool has_received_rpsi_;
230   uint64_t picture_id_rpsi_;
231   std::map<unsigned int, int> ssrc_streams_;
232
233   // Quality modes callback
234   QMVideoSettingsCallback* qm_callback_;
235   bool video_suspended_;
236   I420FrameCallback* pre_encode_callback_;
237 };
238
239 }  // namespace webrtc
240
241 #endif  // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_