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