2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
17 #include "webrtc/base/thread_annotations.h"
18 #include "webrtc/common_types.h"
19 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
20 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
21 #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
22 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
23 #include "webrtc/system_wrappers/interface/scoped_ptr.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"
32 class CriticalSectionWrapper;
33 class EncodedImageCallback;
36 class QMVideoSettingsCallback;
38 class ViEBitrateObserver;
39 class ViEEffectFilter;
40 class ViEEncoderObserver;
41 class VideoCodingModule;
42 class ViEPacedSenderCallback;
45 : public RtcpIntraFrameObserver,
46 public VCMPacketizationCallback,
47 public VCMProtectionCallback,
48 public VCMSendStatisticsCallback,
49 public ViEFrameCallback {
51 friend class ViEBitrateObserver;
52 friend class ViEPacedSenderCallback;
54 ViEEncoder(int32_t engine_id,
56 uint32_t number_of_cores,
58 ProcessThread& module_process_thread,
59 BitrateController* bitrate_controller);
64 void SetNetworkTransmissionState(bool is_transmitting);
66 // Returns the id of the owning channel.
69 // Drops incoming packets before they get to the encoder.
74 uint8_t NumberOfCodecs();
75 int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
76 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
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);
83 int32_t GetCodecConfigParameters(
84 unsigned char config_parameters[kConfigParameterSize],
85 unsigned char& config_parameters_size);
87 PacedSender* GetPacedSender();
89 // Scale or crop/pad image.
90 int32_t ScaleInputImage(bool enable);
93 RtpRtcp* SendRtpRtcpModule();
95 // Implementing ViEFrameCallback.
96 virtual void DeliverFrame(int id,
97 I420VideoFrame* video_frame,
99 const uint32_t CSRC[kRtpCsrcSize] = NULL) OVERRIDE;
100 virtual void DelayChanged(int id, int frame_delay) OVERRIDE;
101 virtual int GetPreferedFrameSettings(int* width,
103 int* frame_rate) OVERRIDE;
105 virtual void ProviderDestroyed(int id) OVERRIDE {
109 int32_t SendKeyFrame();
110 int32_t SendCodecStatistics(uint32_t* num_key_frames,
111 uint32_t* num_delta_frames);
113 int PacerQueuingDelayMs() const;
115 int CodecTargetBitrate(uint32_t* bitrate) const;
117 int32_t UpdateProtectionMethod(bool enable_nack);
118 bool nack_enabled() const { return nack_enabled_; }
121 void SetSenderBufferingMode(int target_delay_ms);
123 // Implements VCMPacketizationCallback.
124 virtual int32_t SendData(
125 FrameType frame_type,
126 uint8_t payload_type,
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) OVERRIDE;
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) OVERRIDE;
142 // Implements VideoSendStatisticsCallback.
143 virtual int32_t SendStatistics(const uint32_t bit_rate,
144 const uint32_t frame_rate) OVERRIDE;
146 int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
148 // Implements RtcpIntraFrameObserver.
149 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc) OVERRIDE;
150 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) OVERRIDE;
151 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) OVERRIDE;
152 virtual void OnLocalSsrcChanged(uint32_t old_ssrc,
153 uint32_t new_ssrc) OVERRIDE;
155 // Sets SSRCs for all streams.
156 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
158 void SetMinTransmitBitrate(int min_transmit_bitrate_kbps);
161 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
163 // Enables recording of debugging information.
164 int StartDebugRecording(const char* fileNameUTF8);
166 // Disables recording of debugging information.
167 int StopDebugRecording();
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 void SuspendBelowMinBitrate();
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();
181 int channel_id() const { return channel_id_; }
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);
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);
194 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
195 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
196 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
198 void UpdateHistograms();
201 const int channel_id_;
202 const uint32_t number_of_cores_;
204 VideoCodingModule& vcm_;
205 VideoProcessingModule& vpm_;
206 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
207 scoped_ptr<CriticalSectionWrapper> callback_cs_;
208 scoped_ptr<CriticalSectionWrapper> data_cs_;
209 scoped_ptr<BitrateObserver> bitrate_observer_;
210 scoped_ptr<PacedSender> paced_sender_;
211 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
213 BitrateController* bitrate_controller_;
215 int64_t time_of_last_incoming_frame_ms_ GUARDED_BY(data_cs_);
216 bool send_padding_ GUARDED_BY(data_cs_);
217 int min_transmit_bitrate_kbps_ GUARDED_BY(data_cs_);
218 int target_delay_ms_ GUARDED_BY(data_cs_);
219 bool network_is_transmitting_ GUARDED_BY(data_cs_);
220 bool encoder_paused_ GUARDED_BY(data_cs_);
221 bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_);
222 std::map<unsigned int, int64_t> time_last_intra_request_ms_
223 GUARDED_BY(data_cs_);
228 ViEEncoderObserver* codec_observer_ GUARDED_BY(callback_cs_);
229 ViEEffectFilter* effect_filter_ GUARDED_BY(callback_cs_);
230 ProcessThread& module_process_thread_;
232 bool has_received_sli_ GUARDED_BY(data_cs_);
233 uint8_t picture_id_sli_ GUARDED_BY(data_cs_);
234 bool has_received_rpsi_ GUARDED_BY(data_cs_);
235 uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_);
236 std::map<unsigned int, int> ssrc_streams_ GUARDED_BY(data_cs_);
238 // Quality modes callback
239 QMVideoSettingsCallback* qm_callback_;
240 bool video_suspended_ GUARDED_BY(data_cs_);
241 I420FrameCallback* pre_encode_callback_ GUARDED_BY(callback_cs_);
242 const int64_t start_ms_;
245 } // namespace webrtc
247 #endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_