blob: 64831c3174e18e908e4272f2573f15c1fd52f41f [file] [log] [blame]
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include "talk/app/webrtc/java/jni/androidmediaencoder_jni.h"
30#include "talk/app/webrtc/java/jni/classreferenceholder.h"
31#include "talk/app/webrtc/java/jni/androidmediacodeccommon.h"
perkj30e91822015-11-20 01:31:25 -080032#include "talk/app/webrtc/java/jni/native_handle_impl.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000033#include "webrtc/base/bind.h"
34#include "webrtc/base/checks.h"
35#include "webrtc/base/logging.h"
36#include "webrtc/base/thread.h"
perkj9576e542015-11-12 06:43:16 -080037#include "webrtc/base/thread_checker.h"
Peter Boström2bc68c72015-09-24 16:22:28 +020038#include "webrtc/modules/rtp_rtcp/source/h264_bitstream_parser.h"
perkj30e91822015-11-20 01:31:25 -080039#include "webrtc/modules/video_coding/include/video_codec_interface.h"
kjellander@webrtc.orgb7ce9642015-11-18 23:04:10 +010040#include "webrtc/modules/video_coding/utility/quality_scaler.h"
41#include "webrtc/modules/video_coding/utility/vp8_header_parser.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010042#include "webrtc/system_wrappers/include/field_trial.h"
43#include "webrtc/system_wrappers/include/logcat_trace_context.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000044#include "third_party/libyuv/include/libyuv/convert.h"
45#include "third_party/libyuv/include/libyuv/convert_from.h"
46#include "third_party/libyuv/include/libyuv/video_common.h"
47
48using rtc::Bind;
49using rtc::Thread;
50using rtc::ThreadManager;
51using rtc::scoped_ptr;
52
53using webrtc::CodecSpecificInfo;
54using webrtc::EncodedImage;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070055using webrtc::VideoFrame;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000056using webrtc::RTPFragmentationHeader;
57using webrtc::VideoCodec;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000058using webrtc::VideoCodecType;
59using webrtc::kVideoCodecH264;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000060using webrtc::kVideoCodecVP8;
Alex Glaznevad948c42015-11-18 13:06:42 -080061using webrtc::kVideoCodecVP9;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000062
63namespace webrtc_jni {
64
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000065// H.264 start code length.
66#define H264_SC_LENGTH 4
67// Maximum allowed NALUs in one output frame.
68#define MAX_NALUS_PERFRAME 32
69// Maximum supported HW video encoder resolution.
70#define MAX_VIDEO_WIDTH 1280
71#define MAX_VIDEO_HEIGHT 1280
72// Maximum supported HW video encoder fps.
73#define MAX_VIDEO_FPS 30
74
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000075// MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses
76// Android's MediaCodec SDK API behind the scenes to implement (hopefully)
77// HW-backed video encode. This C++ class is implemented as a very thin shim,
78// delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder.
79// MediaCodecVideoEncoder is created, operated, and destroyed on a single
80// thread, currently the libjingle Worker thread.
81class MediaCodecVideoEncoder : public webrtc::VideoEncoder,
82 public rtc::MessageHandler {
83 public:
84 virtual ~MediaCodecVideoEncoder();
perkj9576e542015-11-12 06:43:16 -080085 MediaCodecVideoEncoder(JNIEnv* jni,
perkj30e91822015-11-20 01:31:25 -080086 VideoCodecType codecType,
87 jobject egl_context);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000088
89 // webrtc::VideoEncoder implementation. Everything trampolines to
90 // |codec_thread_| for execution.
91 int32_t InitEncode(const webrtc::VideoCodec* codec_settings,
92 int32_t /* number_of_cores */,
93 size_t /* max_payload_size */) override;
pbos22993e12015-10-19 02:39:06 -070094 int32_t Encode(const webrtc::VideoFrame& input_image,
95 const webrtc::CodecSpecificInfo* /* codec_specific_info */,
96 const std::vector<webrtc::FrameType>* frame_types) override;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000097 int32_t RegisterEncodeCompleteCallback(
98 webrtc::EncodedImageCallback* callback) override;
99 int32_t Release() override;
100 int32_t SetChannelParameters(uint32_t /* packet_loss */,
101 int64_t /* rtt */) override;
102 int32_t SetRates(uint32_t new_bit_rate, uint32_t frame_rate) override;
103
104 // rtc::MessageHandler implementation.
105 void OnMessage(rtc::Message* msg) override;
106
jackychen61b4d512015-04-21 15:30:11 -0700107 void OnDroppedFrame() override;
108
jackychen6e2ce6e2015-07-13 16:26:33 -0700109 int GetTargetFramerate() override;
110
perkj30e91822015-11-20 01:31:25 -0800111 bool SupportsNativeHandle() const override { return true; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100112 const char* ImplementationName() const override;
113
114 private:
115 // CHECK-fail if not running on |codec_thread_|.
116 void CheckOnCodecThread();
perkj30e91822015-11-20 01:31:25 -0800117
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000118 private:
perkj9576e542015-11-12 06:43:16 -0800119 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and
120 // InitEncodeOnCodecThread() in an attempt to restore the codec to an
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000121 // operable state. Necessary after all manner of OMX-layer errors.
perkj9576e542015-11-12 06:43:16 -0800122 bool ResetCodecOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000123
124 // Implementation of webrtc::VideoEncoder methods above, all running on the
125 // codec thread exclusively.
126 //
127 // If width==0 then this is assumed to be a re-initialization and the
128 // previously-current values are reused instead of the passed parameters
129 // (makes it easier to reason about thread-safety).
perkj30e91822015-11-20 01:31:25 -0800130 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps,
131 bool use_surface);
132 // Reconfigure to match |frame| in width, height. Also reconfigures the
133 // encoder if |frame| is a texture/byte buffer and the encoder is initialized
134 // for byte buffer/texture. Returns false if reconfiguring fails.
perkj9576e542015-11-12 06:43:16 -0800135 bool MaybeReconfigureEncoderOnCodecThread(const webrtc::VideoFrame& frame);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000136 int32_t EncodeOnCodecThread(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700137 const webrtc::VideoFrame& input_image,
pbos22993e12015-10-19 02:39:06 -0700138 const std::vector<webrtc::FrameType>* frame_types);
perkj9576e542015-11-12 06:43:16 -0800139 bool EncodeByteBufferOnCodecThread(JNIEnv* jni,
140 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index);
perkj30e91822015-11-20 01:31:25 -0800141 bool EncodeTextureOnCodecThread(JNIEnv* jni,
142 bool key_frame, const webrtc::VideoFrame& frame);
perkj9576e542015-11-12 06:43:16 -0800143
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000144 int32_t RegisterEncodeCompleteCallbackOnCodecThread(
145 webrtc::EncodedImageCallback* callback);
146 int32_t ReleaseOnCodecThread();
147 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate);
148
149 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members.
150 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info);
151 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info);
152 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info);
153 jlong GetOutputBufferInfoPresentationTimestampUs(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000154 JNIEnv* jni, jobject j_output_buffer_info);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000155
156 // Deliver any outputs pending in the MediaCodec to our |callback_| and return
157 // true on success.
158 bool DeliverPendingOutputs(JNIEnv* jni);
159
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000160 // Search for H.264 start codes.
161 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size);
162
163 // Type of video codec.
164 VideoCodecType codecType_;
165
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000166 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to
167 // |codec_thread_| synchronously.
168 webrtc::EncodedImageCallback* callback_;
169
170 // State that is constant for the lifetime of this object once the ctor
171 // returns.
172 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec.
perkj9576e542015-11-12 06:43:16 -0800173 rtc::ThreadChecker codec_thread_checker_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000174 ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_;
175 ScopedGlobalRef<jobject> j_media_codec_video_encoder_;
176 jmethodID j_init_encode_method_;
perkj9576e542015-11-12 06:43:16 -0800177 jmethodID j_get_input_buffers_method_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000178 jmethodID j_dequeue_input_buffer_method_;
perkj9576e542015-11-12 06:43:16 -0800179 jmethodID j_encode_buffer_method_;
perkj30e91822015-11-20 01:31:25 -0800180 jmethodID j_encode_texture_method_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000181 jmethodID j_release_method_;
182 jmethodID j_set_rates_method_;
183 jmethodID j_dequeue_output_buffer_method_;
184 jmethodID j_release_output_buffer_method_;
185 jfieldID j_color_format_field_;
186 jfieldID j_info_index_field_;
187 jfieldID j_info_buffer_field_;
188 jfieldID j_info_is_key_frame_field_;
189 jfieldID j_info_presentation_timestamp_us_field_;
190
191 // State that is valid only between InitEncode() and the next Release().
192 // Touched only on codec_thread_ so no explicit synchronization necessary.
193 int width_; // Frame width in pixels.
194 int height_; // Frame height in pixels.
195 bool inited_;
perkj30e91822015-11-20 01:31:25 -0800196 bool use_surface_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000197 uint16_t picture_id_;
198 enum libyuv::FourCC encoder_fourcc_; // Encoder color space format.
199 int last_set_bitrate_kbps_; // Last-requested bitrate in kbps.
200 int last_set_fps_; // Last-requested frame rate.
201 int64_t current_timestamp_us_; // Current frame timestamps in us.
202 int frames_received_; // Number of frames received by encoder.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000203 int frames_encoded_; // Number of frames encoded by encoder.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000204 int frames_dropped_; // Number of frames dropped by encoder.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000205 int frames_in_queue_; // Number of frames in encoder queue.
206 int64_t start_time_ms_; // Start time for statistics.
207 int current_frames_; // Number of frames in the current statistics interval.
208 int current_bytes_; // Encoded bytes in the current statistics interval.
209 int current_encoding_time_ms_; // Overall encoding time in the current second
210 int64_t last_input_timestamp_ms_; // Timestamp of last received yuv frame.
211 int64_t last_output_timestamp_ms_; // Timestamp of last encoded frame.
212 std::vector<int32_t> timestamps_; // Video frames timestamp queue.
213 std::vector<int64_t> render_times_ms_; // Video frames render time queue.
214 std::vector<int64_t> frame_rtc_times_ms_; // Time when video frame is sent to
215 // encoder input.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000216 int32_t output_timestamp_; // Last output frame timestamp from timestamps_ Q.
217 int64_t output_render_time_ms_; // Last output frame render time from
218 // render_times_ms_ queue.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000219 // Frame size in bytes fed to MediaCodec.
220 int yuv_size_;
221 // True only when between a callback_->Encoded() call return a positive value
222 // and the next Encode() call being ignored.
223 bool drop_next_input_frame_;
224 // Global references; must be deleted in Release().
225 std::vector<jobject> input_buffers_;
Peter Boström2bc68c72015-09-24 16:22:28 +0200226 webrtc::QualityScaler quality_scaler_;
jackychen61b4d512015-04-21 15:30:11 -0700227 // Dynamic resolution change, off by default.
228 bool scale_;
Peter Boström2bc68c72015-09-24 16:22:28 +0200229
230 // H264 bitstream parser, used to extract QP from encoded bitstreams.
231 webrtc::H264BitstreamParser h264_bitstream_parser_;
Alex Glaznevad948c42015-11-18 13:06:42 -0800232
233 // VP9 variables to populate codec specific structure.
234 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for
235 // non-flexible VP9 mode.
236 uint8_t tl0_pic_idx_;
237 size_t gof_idx_;
perkj30e91822015-11-20 01:31:25 -0800238
239 // EGL context - owned by factory, should not be allocated/destroyed
240 // by MediaCodecVideoEncoder.
241 jobject egl_context_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000242};
243
244MediaCodecVideoEncoder::~MediaCodecVideoEncoder() {
245 // Call Release() to ensure no more callbacks to us after we are deleted.
246 Release();
247}
248
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000249MediaCodecVideoEncoder::MediaCodecVideoEncoder(
perkj30e91822015-11-20 01:31:25 -0800250 JNIEnv* jni, VideoCodecType codecType, jobject egl_context) :
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000251 codecType_(codecType),
252 callback_(NULL),
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000253 codec_thread_(new Thread()),
254 j_media_codec_video_encoder_class_(
255 jni,
256 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")),
257 j_media_codec_video_encoder_(
258 jni,
259 jni->NewObject(*j_media_codec_video_encoder_class_,
260 GetMethodID(jni,
261 *j_media_codec_video_encoder_class_,
262 "<init>",
perkj30e91822015-11-20 01:31:25 -0800263 "()V"))),
kjellander60ca31b2016-01-04 10:15:53 -0800264 inited_(false),
265 use_surface_(false),
266 picture_id_(0),
perkj30e91822015-11-20 01:31:25 -0800267 egl_context_(egl_context) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000268 ScopedLocalRefFrame local_ref_frame(jni);
269 // It would be nice to avoid spinning up a new thread per MediaCodec, and
270 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug
271 // 2732 means that deadlocks abound. This class synchronously trampolines
272 // to |codec_thread_|, so if anything else can be coming to _us_ from
273 // |codec_thread_|, or from any thread holding the |_sendCritSect| described
274 // in the bug, we have a problem. For now work around that with a dedicated
275 // thread.
276 codec_thread_->SetName("MediaCodecVideoEncoder", NULL);
henrikg91d6ede2015-09-17 00:24:34 -0700277 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder";
perkj9576e542015-11-12 06:43:16 -0800278 codec_thread_checker_.DetachFromThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000279 jclass j_output_buffer_info_class =
280 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo");
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000281 j_init_encode_method_ = GetMethodID(
282 jni,
283 *j_media_codec_video_encoder_class_,
284 "initEncode",
perkj30e91822015-11-20 01:31:25 -0800285 "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;"
perkj48477c12015-12-18 00:34:37 -0800286 "IIIILorg/webrtc/EglBase14$Context;)Z");
perkj9576e542015-11-12 06:43:16 -0800287 j_get_input_buffers_method_ = GetMethodID(
288 jni,
289 *j_media_codec_video_encoder_class_,
290 "getInputBuffers",
291 "()[Ljava/nio/ByteBuffer;");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000292 j_dequeue_input_buffer_method_ = GetMethodID(
293 jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I");
perkj9576e542015-11-12 06:43:16 -0800294 j_encode_buffer_method_ = GetMethodID(
295 jni, *j_media_codec_video_encoder_class_, "encodeBuffer", "(ZIIJ)Z");
perkj30e91822015-11-20 01:31:25 -0800296 j_encode_texture_method_ = GetMethodID(
297 jni, *j_media_codec_video_encoder_class_, "encodeTexture",
298 "(ZI[FJ)Z");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000299 j_release_method_ =
300 GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V");
301 j_set_rates_method_ = GetMethodID(
302 jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z");
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000303 j_dequeue_output_buffer_method_ = GetMethodID(
304 jni,
305 *j_media_codec_video_encoder_class_,
306 "dequeueOutputBuffer",
307 "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000308 j_release_output_buffer_method_ = GetMethodID(
309 jni, *j_media_codec_video_encoder_class_, "releaseOutputBuffer", "(I)Z");
310
311 j_color_format_field_ =
312 GetFieldID(jni, *j_media_codec_video_encoder_class_, "colorFormat", "I");
313 j_info_index_field_ =
314 GetFieldID(jni, j_output_buffer_info_class, "index", "I");
315 j_info_buffer_field_ = GetFieldID(
316 jni, j_output_buffer_info_class, "buffer", "Ljava/nio/ByteBuffer;");
317 j_info_is_key_frame_field_ =
318 GetFieldID(jni, j_output_buffer_info_class, "isKeyFrame", "Z");
319 j_info_presentation_timestamp_us_field_ = GetFieldID(
320 jni, j_output_buffer_info_class, "presentationTimestampUs", "J");
321 CHECK_EXCEPTION(jni) << "MediaCodecVideoEncoder ctor failed";
Alex Glaznevad948c42015-11-18 13:06:42 -0800322 srand(time(NULL));
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000323 AllowBlockingCalls();
324}
325
326int32_t MediaCodecVideoEncoder::InitEncode(
327 const webrtc::VideoCodec* codec_settings,
328 int32_t /* number_of_cores */,
329 size_t /* max_payload_size */) {
jackychen61b4d512015-04-21 15:30:11 -0700330 const int kMinWidth = 320;
331 const int kMinHeight = 180;
jackychen98d8cf52015-05-21 11:12:02 -0700332 const int kLowQpThresholdDenominator = 3;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000333 if (codec_settings == NULL) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700334 ALOGE << "NULL VideoCodec instance";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000335 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
336 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000337 // Factory should guard against other codecs being used with us.
henrikg91d6ede2015-09-17 00:24:34 -0700338 RTC_CHECK(codec_settings->codecType == codecType_)
339 << "Unsupported codec " << codec_settings->codecType << " for "
340 << codecType_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000341
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700342 ALOGD << "InitEncode request";
Alex Glaznevad948c42015-11-18 13:06:42 -0800343 scale_ = (codecType_ != kVideoCodecVP9) && (webrtc::field_trial::FindFullName(
344 "WebRTC-MediaCodecVideoEncoder-AutomaticResize") == "Enabled");
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700345 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled");
Peter Boström2bc68c72015-09-24 16:22:28 +0200346 if (scale_) {
347 if (codecType_ == kVideoCodecVP8) {
348 // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the
349 // (internal) range: [0, 127]. And we cannot change QP_max in HW, so it is
350 // always = 127. Note that in SW, QP is that of the user-level range [0,
351 // 63].
352 const int kMaxQp = 127;
Peter Boström17417702015-09-25 17:03:26 +0200353 // TODO(pbos): Investigate whether high-QP thresholds make sense for VP8.
354 // This effectively disables high QP as VP8 QP can't go above this
355 // threshold.
356 const int kDisabledBadQpThreshold = kMaxQp + 1;
357 quality_scaler_.Init(kMaxQp / kLowQpThresholdDenominator,
358 kDisabledBadQpThreshold, true);
Peter Boström2bc68c72015-09-24 16:22:28 +0200359 } else if (codecType_ == kVideoCodecH264) {
360 // H264 QP is in the range [0, 51].
361 const int kMaxQp = 51;
Peter Boström17417702015-09-25 17:03:26 +0200362 const int kBadQpThreshold = 40;
363 quality_scaler_.Init(kMaxQp / kLowQpThresholdDenominator, kBadQpThreshold,
364 false);
Peter Boström2bc68c72015-09-24 16:22:28 +0200365 } else {
366 // When adding codec support to additional hardware codecs, also configure
367 // their QP thresholds for scaling.
368 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds.";
369 }
370 quality_scaler_.SetMinResolution(kMinWidth, kMinHeight);
371 quality_scaler_.ReportFramerate(codec_settings->maxFramerate);
jackychen61b4d512015-04-21 15:30:11 -0700372 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000373 return codec_thread_->Invoke<int32_t>(
374 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread,
375 this,
376 codec_settings->width,
377 codec_settings->height,
378 codec_settings->startBitrate,
perkj30e91822015-11-20 01:31:25 -0800379 codec_settings->maxFramerate,
380 false /* use_surface */));
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000381}
382
383int32_t MediaCodecVideoEncoder::Encode(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700384 const webrtc::VideoFrame& frame,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000385 const webrtc::CodecSpecificInfo* /* codec_specific_info */,
pbos22993e12015-10-19 02:39:06 -0700386 const std::vector<webrtc::FrameType>* frame_types) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000387 return codec_thread_->Invoke<int32_t>(Bind(
388 &MediaCodecVideoEncoder::EncodeOnCodecThread, this, frame, frame_types));
389}
390
391int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallback(
392 webrtc::EncodedImageCallback* callback) {
393 return codec_thread_->Invoke<int32_t>(
394 Bind(&MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread,
395 this,
396 callback));
397}
398
399int32_t MediaCodecVideoEncoder::Release() {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700400 ALOGD << "EncoderRelease request";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000401 return codec_thread_->Invoke<int32_t>(
402 Bind(&MediaCodecVideoEncoder::ReleaseOnCodecThread, this));
403}
404
405int32_t MediaCodecVideoEncoder::SetChannelParameters(uint32_t /* packet_loss */,
406 int64_t /* rtt */) {
407 return WEBRTC_VIDEO_CODEC_OK;
408}
409
410int32_t MediaCodecVideoEncoder::SetRates(uint32_t new_bit_rate,
411 uint32_t frame_rate) {
Peter Boström2bc68c72015-09-24 16:22:28 +0200412 if (scale_)
413 quality_scaler_.ReportFramerate(frame_rate);
414
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000415 return codec_thread_->Invoke<int32_t>(
416 Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread,
417 this,
418 new_bit_rate,
419 frame_rate));
420}
421
422void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) {
perkj9576e542015-11-12 06:43:16 -0800423 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000424 JNIEnv* jni = AttachCurrentThreadIfNeeded();
425 ScopedLocalRefFrame local_ref_frame(jni);
426
427 // We only ever send one message to |this| directly (not through a Bind()'d
428 // functor), so expect no ID/data.
henrikg91d6ede2015-09-17 00:24:34 -0700429 RTC_CHECK(!msg->message_id) << "Unexpected message!";
430 RTC_CHECK(!msg->pdata) << "Unexpected message!";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000431 if (!inited_) {
432 return;
433 }
434
435 // It would be nice to recover from a failure here if one happened, but it's
436 // unclear how to signal such a failure to the app, so instead we stay silent
437 // about it and let the next app-called API method reveal the borkedness.
438 DeliverPendingOutputs(jni);
439 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
440}
441
perkj9576e542015-11-12 06:43:16 -0800442bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() {
443 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
444 ALOGE << "ResetOnCodecThread";
445 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK ||
perkj30e91822015-11-20 01:31:25 -0800446 InitEncodeOnCodecThread(width_, height_, 0, 0, false) !=
447 WEBRTC_VIDEO_CODEC_OK) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000448 // TODO(fischman): wouldn't it be nice if there was a way to gracefully
449 // degrade to a SW encoder at this point? There isn't one AFAICT :(
450 // https://code.google.com/p/webrtc/issues/detail?id=2920
perkj9576e542015-11-12 06:43:16 -0800451 return false;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000452 }
perkj9576e542015-11-12 06:43:16 -0800453 return true;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000454}
455
456int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread(
perkj30e91822015-11-20 01:31:25 -0800457 int width, int height, int kbps, int fps, bool use_surface) {
perkj9576e542015-11-12 06:43:16 -0800458 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
perkj30e91822015-11-20 01:31:25 -0800459 RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000460 JNIEnv* jni = AttachCurrentThreadIfNeeded();
461 ScopedLocalRefFrame local_ref_frame(jni);
462
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700463 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " <<
464 width << " x " << height << ". Bitrate: " << kbps <<
465 " kbps. Fps: " << fps;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000466 if (kbps == 0) {
467 kbps = last_set_bitrate_kbps_;
468 }
469 if (fps == 0) {
470 fps = last_set_fps_;
471 }
472
473 width_ = width;
474 height_ = height;
475 last_set_bitrate_kbps_ = kbps;
476 last_set_fps_ = fps;
477 yuv_size_ = width_ * height_ * 3 / 2;
478 frames_received_ = 0;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000479 frames_encoded_ = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000480 frames_dropped_ = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000481 frames_in_queue_ = 0;
482 current_timestamp_us_ = 0;
483 start_time_ms_ = GetCurrentTimeMs();
484 current_frames_ = 0;
485 current_bytes_ = 0;
486 current_encoding_time_ms_ = 0;
487 last_input_timestamp_ms_ = -1;
488 last_output_timestamp_ms_ = -1;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000489 output_timestamp_ = 0;
490 output_render_time_ms_ = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000491 timestamps_.clear();
492 render_times_ms_.clear();
493 frame_rtc_times_ms_.clear();
494 drop_next_input_frame_ = false;
perkj30e91822015-11-20 01:31:25 -0800495 use_surface_ = use_surface;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000496 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF;
Alex Glaznevad948c42015-11-18 13:06:42 -0800497 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1);
498 tl0_pic_idx_ = static_cast<uint8_t>(rand());
499 gof_idx_ = 0;
perkj9576e542015-11-12 06:43:16 -0800500
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000501 // We enforce no extra stride/padding in the format creation step.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000502 jobject j_video_codec_enum = JavaEnumFromIndex(
503 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_);
perkj9576e542015-11-12 06:43:16 -0800504 const bool encode_status = jni->CallBooleanMethod(
505 *j_media_codec_video_encoder_, j_init_encode_method_,
perkj30e91822015-11-20 01:31:25 -0800506 j_video_codec_enum, width, height, kbps, fps,
507 (use_surface ? egl_context_ : nullptr));
perkj9576e542015-11-12 06:43:16 -0800508 if (!encode_status) {
509 ALOGE << "Failed to configure encoder.";
510 return WEBRTC_VIDEO_CODEC_ERROR;
511 }
512 CHECK_EXCEPTION(jni);
513
Per598242a2015-11-26 14:28:55 +0100514 if (!use_surface) {
perkj30e91822015-11-20 01:31:25 -0800515 jobjectArray input_buffers = reinterpret_cast<jobjectArray>(
516 jni->CallObjectMethod(*j_media_codec_video_encoder_,
517 j_get_input_buffers_method_));
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000518 CHECK_EXCEPTION(jni);
perkj30e91822015-11-20 01:31:25 -0800519 if (IsNull(jni, input_buffers)) {
520 return WEBRTC_VIDEO_CODEC_ERROR;
521 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000522
perkj30e91822015-11-20 01:31:25 -0800523 switch (GetIntField(jni, *j_media_codec_video_encoder_,
524 j_color_format_field_)) {
525 case COLOR_FormatYUV420Planar:
526 encoder_fourcc_ = libyuv::FOURCC_YU12;
527 break;
528 case COLOR_FormatYUV420SemiPlanar:
529 case COLOR_QCOM_FormatYUV420SemiPlanar:
530 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m:
531 encoder_fourcc_ = libyuv::FOURCC_NV12;
532 break;
533 default:
534 LOG(LS_ERROR) << "Wrong color format.";
535 return WEBRTC_VIDEO_CODEC_ERROR;
536 }
537 size_t num_input_buffers = jni->GetArrayLength(input_buffers);
538 RTC_CHECK(input_buffers_.empty())
539 << "Unexpected double InitEncode without Release";
540 input_buffers_.resize(num_input_buffers);
541 for (size_t i = 0; i < num_input_buffers; ++i) {
542 input_buffers_[i] =
543 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i));
544 int64_t yuv_buffer_capacity =
545 jni->GetDirectBufferCapacity(input_buffers_[i]);
546 CHECK_EXCEPTION(jni);
547 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity";
548 }
549 }
perkj9576e542015-11-12 06:43:16 -0800550
551 inited_ = true;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000552 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
553 return WEBRTC_VIDEO_CODEC_OK;
554}
555
556int32_t MediaCodecVideoEncoder::EncodeOnCodecThread(
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700557 const webrtc::VideoFrame& frame,
pbos22993e12015-10-19 02:39:06 -0700558 const std::vector<webrtc::FrameType>* frame_types) {
perkj9576e542015-11-12 06:43:16 -0800559 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000560 JNIEnv* jni = AttachCurrentThreadIfNeeded();
561 ScopedLocalRefFrame local_ref_frame(jni);
562
563 if (!inited_) {
564 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
565 }
perkj9576e542015-11-12 06:43:16 -0800566
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000567 frames_received_++;
568 if (!DeliverPendingOutputs(jni)) {
perkj9576e542015-11-12 06:43:16 -0800569 if (!ResetCodecOnCodecThread())
570 return WEBRTC_VIDEO_CODEC_ERROR;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000571 }
572
573 if (drop_next_input_frame_) {
perkj9576e542015-11-12 06:43:16 -0800574 ALOGW << "Encoder drop frame - failed callback.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000575 drop_next_input_frame_ = false;
576 return WEBRTC_VIDEO_CODEC_OK;
577 }
578
henrikg91d6ede2015-09-17 00:24:34 -0700579 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count";
Peter Boström2bc68c72015-09-24 16:22:28 +0200580
Per598242a2015-11-26 14:28:55 +0100581 VideoFrame input_frame = frame;
582 if (scale_) {
583 // Check framerate before spatial resolution change.
584 quality_scaler_.OnEncodeFrame(frame);
585 const webrtc::QualityScaler::Resolution scaled_resolution =
586 quality_scaler_.GetScaledResolution();
587 if (scaled_resolution.width != frame.width() ||
588 scaled_resolution.height != frame.height()) {
589 if (frame.native_handle() != nullptr) {
590 rtc::scoped_refptr<webrtc::VideoFrameBuffer> scaled_buffer(
591 static_cast<AndroidTextureBuffer*>(
Per71f5a9a2015-12-11 09:32:37 +0100592 frame.video_frame_buffer().get())->ScaleAndRotate(
Per598242a2015-11-26 14:28:55 +0100593 scaled_resolution.width,
Per71f5a9a2015-12-11 09:32:37 +0100594 scaled_resolution.height,
595 webrtc::kVideoRotation_0));
Per598242a2015-11-26 14:28:55 +0100596 input_frame.set_video_frame_buffer(scaled_buffer);
597 } else {
598 input_frame = quality_scaler_.GetScaledFrame(frame);
599 }
600 }
601 }
jackychen61b4d512015-04-21 15:30:11 -0700602
perkj9576e542015-11-12 06:43:16 -0800603 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) {
604 ALOGE << "Failed to reconfigure encoder.";
605 return WEBRTC_VIDEO_CODEC_ERROR;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000606 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000607
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000608 // Check if we accumulated too many frames in encoder input buffers
609 // or the encoder latency exceeds 70 ms and drop frame if so.
610 if (frames_in_queue_ > 0 && last_input_timestamp_ms_ >= 0) {
611 int encoder_latency_ms = last_input_timestamp_ms_ -
612 last_output_timestamp_ms_;
613 if (frames_in_queue_ > 2 || encoder_latency_ms > 70) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700614 ALOGD << "Drop frame - encoder is behind by " << encoder_latency_ms <<
615 " ms. Q size: " << frames_in_queue_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000616 frames_dropped_++;
jackychen61b4d512015-04-21 15:30:11 -0700617 // Report dropped frame to quality_scaler_.
618 OnDroppedFrame();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000619 return WEBRTC_VIDEO_CODEC_OK;
620 }
621 }
622
perkj30e91822015-11-20 01:31:25 -0800623 const bool key_frame = frame_types->front() != webrtc::kVideoFrameDelta;
624 bool encode_status = true;
625 if (!input_frame.native_handle()) {
626 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_,
627 j_dequeue_input_buffer_method_);
628 CHECK_EXCEPTION(jni);
629 if (j_input_buffer_index == -1) {
630 // Video codec falls behind - no input buffer available.
631 ALOGW << "Encoder drop frame - no input buffers available";
632 frames_dropped_++;
633 // Report dropped frame to quality_scaler_.
634 OnDroppedFrame();
635 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887.
636 }
637 if (j_input_buffer_index == -2) {
638 ResetCodecOnCodecThread();
639 return WEBRTC_VIDEO_CODEC_ERROR;
640 }
641 encode_status = EncodeByteBufferOnCodecThread(jni, key_frame, input_frame,
642 j_input_buffer_index);
643 } else {
644 encode_status = EncodeTextureOnCodecThread(jni, key_frame, input_frame);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000645 }
perkj30e91822015-11-20 01:31:25 -0800646
647 if (!encode_status) {
648 ALOGE << "Failed encode frame with timestamp: " << input_frame.timestamp();
perkj9576e542015-11-12 06:43:16 -0800649 ResetCodecOnCodecThread();
perkj12f68022015-10-16 13:31:45 +0200650 return WEBRTC_VIDEO_CODEC_ERROR;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000651 }
652
perkj9576e542015-11-12 06:43:16 -0800653 last_input_timestamp_ms_ =
654 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec;
perkj12f68022015-10-16 13:31:45 +0200655 frames_in_queue_++;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000656
perkj12f68022015-10-16 13:31:45 +0200657 // Save input image timestamps for later output
658 timestamps_.push_back(input_frame.timestamp());
659 render_times_ms_.push_back(input_frame.render_time_ms());
660 frame_rtc_times_ms_.push_back(GetCurrentTimeMs());
perkj9576e542015-11-12 06:43:16 -0800661 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_;
662
perkj30e91822015-11-20 01:31:25 -0800663 if (!DeliverPendingOutputs(jni)) {
perkj9576e542015-11-12 06:43:16 -0800664 ALOGE << "Failed deliver pending outputs.";
665 ResetCodecOnCodecThread();
666 return WEBRTC_VIDEO_CODEC_ERROR;
667 }
668 return WEBRTC_VIDEO_CODEC_OK;
669}
670
671bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread(
672 const webrtc::VideoFrame& frame) {
673 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
674
perkj30e91822015-11-20 01:31:25 -0800675 const bool is_texture_frame = frame.native_handle() != nullptr;
676 const bool reconfigure_due_to_format = is_texture_frame != use_surface_;
perkj9576e542015-11-12 06:43:16 -0800677 const bool reconfigure_due_to_size =
678 frame.width() != width_ || frame.height() != height_;
679
perkj30e91822015-11-20 01:31:25 -0800680 if (reconfigure_due_to_format) {
681 ALOGD << "Reconfigure encoder due to format change. "
682 << (use_surface_ ?
683 "Reconfiguring to encode from byte buffer." :
684 "Reconfiguring to encode from texture.");
685 }
perkj9576e542015-11-12 06:43:16 -0800686 if (reconfigure_due_to_size) {
687 ALOGD << "Reconfigure encoder due to frame resolution change from "
688 << width_ << " x " << height_ << " to " << frame.width() << " x "
689 << frame.height();
690 width_ = frame.width();
691 height_ = frame.height();
692 }
693
perkj30e91822015-11-20 01:31:25 -0800694 if (!reconfigure_due_to_format && !reconfigure_due_to_size)
perkj9576e542015-11-12 06:43:16 -0800695 return true;
696
697 ReleaseOnCodecThread();
698
perkj30e91822015-11-20 01:31:25 -0800699 return InitEncodeOnCodecThread(width_, height_, 0, 0 , is_texture_frame) ==
perkj9576e542015-11-12 06:43:16 -0800700 WEBRTC_VIDEO_CODEC_OK;
701}
702
703bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni,
704 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index) {
705 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
perkj30e91822015-11-20 01:31:25 -0800706 RTC_CHECK(!use_surface_);
perkj9576e542015-11-12 06:43:16 -0800707
708 ALOGV("Encoder frame in # %d. TS: %lld. Q: %d",
709 frames_received_ - 1, current_timestamp_us_ / 1000, frames_in_queue_);
710
711 jobject j_input_buffer = input_buffers_[input_buffer_index];
712 uint8_t* yuv_buffer =
713 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer));
714 CHECK_EXCEPTION(jni);
715 RTC_CHECK(yuv_buffer) << "Indirect buffer??";
716 RTC_CHECK(!libyuv::ConvertFromI420(
717 frame.buffer(webrtc::kYPlane), frame.stride(webrtc::kYPlane),
718 frame.buffer(webrtc::kUPlane), frame.stride(webrtc::kUPlane),
719 frame.buffer(webrtc::kVPlane), frame.stride(webrtc::kVPlane),
720 yuv_buffer, width_, width_, height_, encoder_fourcc_))
721 << "ConvertFromI420 failed";
722
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000723 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
perkj9576e542015-11-12 06:43:16 -0800724 j_encode_buffer_method_,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000725 key_frame,
perkj9576e542015-11-12 06:43:16 -0800726 input_buffer_index,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000727 yuv_size_,
728 current_timestamp_us_);
729 CHECK_EXCEPTION(jni);
perkj9576e542015-11-12 06:43:16 -0800730 return encode_status;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000731}
732
perkj30e91822015-11-20 01:31:25 -0800733bool MediaCodecVideoEncoder::EncodeTextureOnCodecThread(JNIEnv* jni,
734 bool key_frame, const webrtc::VideoFrame& frame) {
735 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
736 RTC_CHECK(use_surface_);
737 NativeHandleImpl* handle =
738 static_cast<NativeHandleImpl*>(frame.native_handle());
739 jfloatArray sampling_matrix = jni->NewFloatArray(16);
740 jni->SetFloatArrayRegion(sampling_matrix, 0, 16, handle->sampling_matrix);
741
742 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
743 j_encode_texture_method_,
744 key_frame,
745 handle->oes_texture_id,
746 sampling_matrix,
747 current_timestamp_us_);
748 CHECK_EXCEPTION(jni);
749 return encode_status;
750}
751
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000752int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread(
753 webrtc::EncodedImageCallback* callback) {
perkj9576e542015-11-12 06:43:16 -0800754 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000755 JNIEnv* jni = AttachCurrentThreadIfNeeded();
756 ScopedLocalRefFrame local_ref_frame(jni);
757 callback_ = callback;
758 return WEBRTC_VIDEO_CODEC_OK;
759}
760
761int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() {
perkj9576e542015-11-12 06:43:16 -0800762 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000763 if (!inited_) {
764 return WEBRTC_VIDEO_CODEC_OK;
765 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000766 JNIEnv* jni = AttachCurrentThreadIfNeeded();
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700767 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " <<
768 frames_received_ << ". Encoded: " << frames_encoded_ <<
769 ". Dropped: " << frames_dropped_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000770 ScopedLocalRefFrame local_ref_frame(jni);
771 for (size_t i = 0; i < input_buffers_.size(); ++i)
772 jni->DeleteGlobalRef(input_buffers_[i]);
773 input_buffers_.clear();
774 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_);
775 CHECK_EXCEPTION(jni);
776 rtc::MessageQueueManager::Clear(this);
777 inited_ = false;
perkj30e91822015-11-20 01:31:25 -0800778 use_surface_ = false;
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700779 ALOGD << "EncoderReleaseOnCodecThread done.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000780 return WEBRTC_VIDEO_CODEC_OK;
781}
782
783int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate,
784 uint32_t frame_rate) {
perkj9576e542015-11-12 06:43:16 -0800785 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000786 if (last_set_bitrate_kbps_ == new_bit_rate &&
787 last_set_fps_ == frame_rate) {
788 return WEBRTC_VIDEO_CODEC_OK;
789 }
790 JNIEnv* jni = AttachCurrentThreadIfNeeded();
791 ScopedLocalRefFrame local_ref_frame(jni);
792 if (new_bit_rate > 0) {
793 last_set_bitrate_kbps_ = new_bit_rate;
794 }
795 if (frame_rate > 0) {
796 last_set_fps_ = frame_rate;
797 }
798 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
799 j_set_rates_method_,
800 last_set_bitrate_kbps_,
801 last_set_fps_);
802 CHECK_EXCEPTION(jni);
803 if (!ret) {
perkj9576e542015-11-12 06:43:16 -0800804 ResetCodecOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000805 return WEBRTC_VIDEO_CODEC_ERROR;
806 }
807 return WEBRTC_VIDEO_CODEC_OK;
808}
809
810int MediaCodecVideoEncoder::GetOutputBufferInfoIndex(
811 JNIEnv* jni,
812 jobject j_output_buffer_info) {
813 return GetIntField(jni, j_output_buffer_info, j_info_index_field_);
814}
815
816jobject MediaCodecVideoEncoder::GetOutputBufferInfoBuffer(
817 JNIEnv* jni,
818 jobject j_output_buffer_info) {
819 return GetObjectField(jni, j_output_buffer_info, j_info_buffer_field_);
820}
821
822bool MediaCodecVideoEncoder::GetOutputBufferInfoIsKeyFrame(
823 JNIEnv* jni,
824 jobject j_output_buffer_info) {
825 return GetBooleanField(jni, j_output_buffer_info, j_info_is_key_frame_field_);
826}
827
828jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs(
829 JNIEnv* jni,
830 jobject j_output_buffer_info) {
831 return GetLongField(
832 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_);
833}
834
835bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) {
perkj9576e542015-11-12 06:43:16 -0800836 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread());
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000837 while (true) {
838 jobject j_output_buffer_info = jni->CallObjectMethod(
839 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_);
840 CHECK_EXCEPTION(jni);
841 if (IsNull(jni, j_output_buffer_info)) {
842 break;
843 }
844
845 int output_buffer_index =
846 GetOutputBufferInfoIndex(jni, j_output_buffer_info);
847 if (output_buffer_index == -1) {
perkj9576e542015-11-12 06:43:16 -0800848 ResetCodecOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000849 return false;
850 }
851
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000852 // Get key and config frame flags.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000853 jobject j_output_buffer =
854 GetOutputBufferInfoBuffer(jni, j_output_buffer_info);
855 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000856
857 // Get frame timestamps from a queue - for non config frames only.
858 int64_t frame_encoding_time_ms = 0;
859 last_output_timestamp_ms_ =
860 GetOutputBufferInfoPresentationTimestampUs(jni, j_output_buffer_info) /
861 1000;
862 if (frames_in_queue_ > 0) {
863 output_timestamp_ = timestamps_.front();
864 timestamps_.erase(timestamps_.begin());
865 output_render_time_ms_ = render_times_ms_.front();
866 render_times_ms_.erase(render_times_ms_.begin());
867 frame_encoding_time_ms = GetCurrentTimeMs() - frame_rtc_times_ms_.front();
868 frame_rtc_times_ms_.erase(frame_rtc_times_ms_.begin());
869 frames_in_queue_--;
870 }
871
872 // Extract payload.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000873 size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer);
Peter Boström0c4e06b2015-10-07 12:23:21 +0200874 uint8_t* payload = reinterpret_cast<uint8_t*>(
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000875 jni->GetDirectBufferAddress(j_output_buffer));
876 CHECK_EXCEPTION(jni);
877
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000878 ALOGV("Encoder frame out # %d. Key: %d. Size: %d. TS: %lld."
879 " Latency: %lld. EncTime: %lld",
880 frames_encoded_, key_frame, payload_size,
881 last_output_timestamp_ms_,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000882 last_input_timestamp_ms_ - last_output_timestamp_ms_,
883 frame_encoding_time_ms);
884
885 // Calculate and print encoding statistics - every 3 seconds.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000886 frames_encoded_++;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000887 current_frames_++;
888 current_bytes_ += payload_size;
889 current_encoding_time_ms_ += frame_encoding_time_ms;
890 int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_;
891 if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs &&
892 current_frames_ > 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700893 ALOGD << "Encoded frames: " << frames_encoded_ << ". Bitrate: " <<
894 (current_bytes_ * 8 / statistic_time_ms) <<
895 ", target: " << last_set_bitrate_kbps_ << " kbps, fps: " <<
896 ((current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms)
897 << ", encTime: " <<
898 (current_encoding_time_ms_ / current_frames_) << " for last " <<
899 statistic_time_ms << " ms.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000900 start_time_ms_ = GetCurrentTimeMs();
901 current_frames_ = 0;
902 current_bytes_ = 0;
903 current_encoding_time_ms_ = 0;
904 }
905
906 // Callback - return encoded frame.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000907 int32_t callback_status = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000908 if (callback_) {
909 scoped_ptr<webrtc::EncodedImage> image(
910 new webrtc::EncodedImage(payload, payload_size, payload_size));
911 image->_encodedWidth = width_;
912 image->_encodedHeight = height_;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000913 image->_timeStamp = output_timestamp_;
914 image->capture_time_ms_ = output_render_time_ms_;
Peter Boström49e196a2015-10-23 15:58:18 +0200915 image->_frameType =
916 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000917 image->_completeFrame = true;
asapersson075fb4b2015-10-29 08:49:14 -0700918 image->adapt_reason_.quality_resolution_downscales =
919 scale_ ? quality_scaler_.downscale_shift() : -1;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000920
921 webrtc::CodecSpecificInfo info;
922 memset(&info, 0, sizeof(info));
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000923 info.codecType = codecType_;
924 if (codecType_ == kVideoCodecVP8) {
925 info.codecSpecific.VP8.pictureId = picture_id_;
926 info.codecSpecific.VP8.nonReference = false;
927 info.codecSpecific.VP8.simulcastIdx = 0;
928 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx;
929 info.codecSpecific.VP8.layerSync = false;
930 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx;
931 info.codecSpecific.VP8.keyIdx = webrtc::kNoKeyIdx;
Alex Glaznevad948c42015-11-18 13:06:42 -0800932 } else if (codecType_ == kVideoCodecVP9) {
933 if (key_frame) {
934 gof_idx_ = 0;
935 }
936 info.codecSpecific.VP9.picture_id = picture_id_;
937 info.codecSpecific.VP9.inter_pic_predicted = key_frame ? false : true;
938 info.codecSpecific.VP9.flexible_mode = false;
939 info.codecSpecific.VP9.ss_data_available = key_frame ? true : false;
940 info.codecSpecific.VP9.tl0_pic_idx = tl0_pic_idx_++;
941 info.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx;
942 info.codecSpecific.VP9.spatial_idx = webrtc::kNoSpatialIdx;
943 info.codecSpecific.VP9.temporal_up_switch = true;
944 info.codecSpecific.VP9.inter_layer_predicted = false;
945 info.codecSpecific.VP9.gof_idx =
946 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof);
947 info.codecSpecific.VP9.num_spatial_layers = 1;
948 info.codecSpecific.VP9.spatial_layer_resolution_present = false;
949 if (info.codecSpecific.VP9.ss_data_available) {
950 info.codecSpecific.VP9.spatial_layer_resolution_present = true;
951 info.codecSpecific.VP9.width[0] = width_;
952 info.codecSpecific.VP9.height[0] = height_;
953 info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_);
954 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000955 }
Alex Glaznevad948c42015-11-18 13:06:42 -0800956 picture_id_ = (picture_id_ + 1) & 0x7FFF;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000957
958 // Generate a header describing a single fragment.
959 webrtc::RTPFragmentationHeader header;
960 memset(&header, 0, sizeof(header));
Alex Glaznevad948c42015-11-18 13:06:42 -0800961 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000962 header.VerifyAndAllocateFragmentationHeader(1);
963 header.fragmentationOffset[0] = 0;
964 header.fragmentationLength[0] = image->_length;
965 header.fragmentationPlType[0] = 0;
966 header.fragmentationTimeDiff[0] = 0;
Alex Glaznevad948c42015-11-18 13:06:42 -0800967 if (codecType_ == kVideoCodecVP8 && scale_) {
asapersson86b01602015-10-20 23:55:26 -0700968 int qp;
969 if (webrtc::vp8::GetQp(payload, payload_size, &qp))
970 quality_scaler_.ReportQP(qp);
971 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000972 } else if (codecType_ == kVideoCodecH264) {
Peter Boström2bc68c72015-09-24 16:22:28 +0200973 if (scale_) {
974 h264_bitstream_parser_.ParseBitstream(payload, payload_size);
975 int qp;
976 if (h264_bitstream_parser_.GetLastSliceQp(&qp))
977 quality_scaler_.ReportQP(qp);
978 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000979 // For H.264 search for start codes.
980 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {};
981 int32_t scPositionsLength = 0;
982 int32_t scPosition = 0;
983 while (scPositionsLength < MAX_NALUS_PERFRAME) {
984 int32_t naluPosition = NextNaluPosition(
985 payload + scPosition, payload_size - scPosition);
986 if (naluPosition < 0) {
987 break;
988 }
989 scPosition += naluPosition;
990 scPositions[scPositionsLength++] = scPosition;
991 scPosition += H264_SC_LENGTH;
992 }
993 if (scPositionsLength == 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700994 ALOGE << "Start code is not found!";
995 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1]
996 << " " << image->_buffer[2] << " " << image->_buffer[3]
997 << " " << image->_buffer[4] << " " << image->_buffer[5];
perkj9576e542015-11-12 06:43:16 -0800998 ResetCodecOnCodecThread();
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000999 return false;
1000 }
1001 scPositions[scPositionsLength] = payload_size;
1002 header.VerifyAndAllocateFragmentationHeader(scPositionsLength);
1003 for (size_t i = 0; i < scPositionsLength; i++) {
1004 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH;
1005 header.fragmentationLength[i] =
1006 scPositions[i + 1] - header.fragmentationOffset[i];
1007 header.fragmentationPlType[i] = 0;
1008 header.fragmentationTimeDiff[i] = 0;
1009 }
1010 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001011
1012 callback_status = callback_->Encoded(*image, &info, &header);
1013 }
1014
1015 // Return output buffer back to the encoder.
1016 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_,
1017 j_release_output_buffer_method_,
1018 output_buffer_index);
1019 CHECK_EXCEPTION(jni);
1020 if (!success) {
perkj9576e542015-11-12 06:43:16 -08001021 ResetCodecOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001022 return false;
1023 }
1024
1025 if (callback_status > 0) {
1026 drop_next_input_frame_ = true;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001027 // Theoretically could handle callback_status<0 here, but unclear what
1028 // that would mean for us.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001029 }
1030 }
1031
1032 return true;
1033}
1034
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001035int32_t MediaCodecVideoEncoder::NextNaluPosition(
1036 uint8_t *buffer, size_t buffer_size) {
1037 if (buffer_size < H264_SC_LENGTH) {
1038 return -1;
1039 }
1040 uint8_t *head = buffer;
1041 // Set end buffer pointer to 4 bytes before actual buffer end so we can
1042 // access head[1], head[2] and head[3] in a loop without buffer overrun.
1043 uint8_t *end = buffer + buffer_size - H264_SC_LENGTH;
1044
1045 while (head < end) {
1046 if (head[0]) {
1047 head++;
1048 continue;
1049 }
1050 if (head[1]) { // got 00xx
1051 head += 2;
1052 continue;
1053 }
1054 if (head[2]) { // got 0000xx
1055 head += 3;
1056 continue;
1057 }
1058 if (head[3] != 0x01) { // got 000000xx
glaznev@webrtc.orgdc08a232015-03-06 23:32:20 +00001059 head++; // xx != 1, continue searching.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001060 continue;
1061 }
1062 return (int32_t)(head - buffer);
1063 }
1064 return -1;
1065}
1066
jackychen61b4d512015-04-21 15:30:11 -07001067void MediaCodecVideoEncoder::OnDroppedFrame() {
Peter Boström2bc68c72015-09-24 16:22:28 +02001068 if (scale_)
1069 quality_scaler_.ReportDroppedFrame();
jackychen61b4d512015-04-21 15:30:11 -07001070}
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001071
jackychen6e2ce6e2015-07-13 16:26:33 -07001072int MediaCodecVideoEncoder::GetTargetFramerate() {
Peter Boström2bc68c72015-09-24 16:22:28 +02001073 return scale_ ? quality_scaler_.GetTargetFramerate() : -1;
jackychen6e2ce6e2015-07-13 16:26:33 -07001074}
1075
Peter Boströmb7d9a972015-12-18 16:01:11 +01001076const char* MediaCodecVideoEncoder::ImplementationName() const {
1077 return "MediaCodec";
1078}
1079
perkj30e91822015-11-20 01:31:25 -08001080MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory()
Peter Boströmb7d9a972015-12-18 16:01:11 +01001081 : egl_context_(nullptr) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001082 JNIEnv* jni = AttachCurrentThreadIfNeeded();
1083 ScopedLocalRefFrame local_ref_frame(jni);
1084 jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder");
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001085 supported_codecs_.clear();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001086
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001087 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod(
1088 j_encoder_class,
1089 GetStaticMethodID(jni, j_encoder_class, "isVp8HwSupported", "()Z"));
1090 CHECK_EXCEPTION(jni);
1091 if (is_vp8_hw_supported) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -07001092 ALOGD << "VP8 HW Encoder supported.";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001093 supported_codecs_.push_back(VideoCodec(kVideoCodecVP8, "VP8",
1094 MAX_VIDEO_WIDTH, MAX_VIDEO_HEIGHT, MAX_VIDEO_FPS));
1095 }
1096
Alex Glaznevad948c42015-11-18 13:06:42 -08001097 bool is_vp9_hw_supported = jni->CallStaticBooleanMethod(
1098 j_encoder_class,
1099 GetStaticMethodID(jni, j_encoder_class, "isVp9HwSupported", "()Z"));
1100 CHECK_EXCEPTION(jni);
1101 if (is_vp9_hw_supported) {
1102 ALOGD << "VP9 HW Encoder supported.";
1103 supported_codecs_.push_back(VideoCodec(kVideoCodecVP9, "VP9",
1104 MAX_VIDEO_WIDTH, MAX_VIDEO_HEIGHT, MAX_VIDEO_FPS));
1105 }
1106
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001107 bool is_h264_hw_supported = jni->CallStaticBooleanMethod(
1108 j_encoder_class,
1109 GetStaticMethodID(jni, j_encoder_class, "isH264HwSupported", "()Z"));
1110 CHECK_EXCEPTION(jni);
1111 if (is_h264_hw_supported) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -07001112 ALOGD << "H.264 HW Encoder supported.";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001113 supported_codecs_.push_back(VideoCodec(kVideoCodecH264, "H264",
1114 MAX_VIDEO_WIDTH, MAX_VIDEO_HEIGHT, MAX_VIDEO_FPS));
1115 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001116}
1117
1118MediaCodecVideoEncoderFactory::~MediaCodecVideoEncoderFactory() {}
1119
perkj30e91822015-11-20 01:31:25 -08001120void MediaCodecVideoEncoderFactory::SetEGLContext(
1121 JNIEnv* jni, jobject render_egl_context) {
1122 ALOGD << "MediaCodecVideoEncoderFactory::SetEGLContext";
1123 if (egl_context_) {
1124 jni->DeleteGlobalRef(egl_context_);
1125 egl_context_ = NULL;
1126 }
1127 if (!IsNull(jni, render_egl_context)) {
1128 egl_context_ = jni->NewGlobalRef(render_egl_context);
1129 if (CheckException(jni)) {
1130 ALOGE << "error calling NewGlobalRef for EGL Context.";
1131 egl_context_ = NULL;
1132 } else {
1133 jclass j_egl_context_class =
perkj48477c12015-12-18 00:34:37 -08001134 FindClass(jni, "org/webrtc/EglBase14$Context");
perkj30e91822015-11-20 01:31:25 -08001135 if (!jni->IsInstanceOf(egl_context_, j_egl_context_class)) {
1136 ALOGE << "Wrong EGL Context.";
1137 jni->DeleteGlobalRef(egl_context_);
1138 egl_context_ = NULL;
1139 }
1140 }
1141 }
1142 if (egl_context_ == NULL) {
1143 ALOGW << "NULL VideoDecoder EGL context - HW surface encoding is disabled.";
1144 }
1145}
1146
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001147webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001148 VideoCodecType type) {
1149 if (supported_codecs_.empty()) {
Alex Glaznevad948c42015-11-18 13:06:42 -08001150 ALOGW << "No HW video encoder for type " << (int)type;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001151 return NULL;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001152 }
1153 for (std::vector<VideoCodec>::const_iterator it = supported_codecs_.begin();
1154 it != supported_codecs_.end(); ++it) {
1155 if (it->type == type) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -07001156 ALOGD << "Create HW video encoder for type " << (int)type <<
1157 " (" << it->name << ").";
perkj30e91822015-11-20 01:31:25 -08001158 return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), type,
1159 egl_context_);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001160 }
1161 }
Alex Glaznevad948c42015-11-18 13:06:42 -08001162 ALOGW << "Can not find HW video encoder for type " << (int)type;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +00001163 return NULL;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001164}
1165
1166const std::vector<MediaCodecVideoEncoderFactory::VideoCodec>&
1167MediaCodecVideoEncoderFactory::codecs() const {
1168 return supported_codecs_;
1169}
1170
1171void MediaCodecVideoEncoderFactory::DestroyVideoEncoder(
1172 webrtc::VideoEncoder* encoder) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -07001173 ALOGD << "Destroy video encoder.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001174 delete encoder;
1175}
1176
1177} // namespace webrtc_jni
1178