blob: c83be1818f32ef3e56c7e28abba94be488ab72f4 [file] [log] [blame]
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +00009 */
10
magjed6d387c02015-10-14 04:02:01 -070011#include <algorithm>
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000012#include <vector>
13
kjellandera96e2d72016-02-04 23:52:28 -080014// NOTICE: androidmediadecoder_jni.h must be included before
15// androidmediacodeccommon.h to avoid build errors.
Henrik Kjellander15583c12016-02-10 10:53:12 +010016#include "webrtc/api/java/jni/androidmediadecoder_jni.h"
17
kjellandera96e2d72016-02-04 23:52:28 -080018#include "third_party/libyuv/include/libyuv/convert.h"
19#include "third_party/libyuv/include/libyuv/convert_from.h"
20#include "third_party/libyuv/include/libyuv/video_common.h"
Henrik Kjellander15583c12016-02-10 10:53:12 +010021#include "webrtc/api/java/jni/androidmediacodeccommon.h"
22#include "webrtc/api/java/jni/classreferenceholder.h"
23#include "webrtc/api/java/jni/native_handle_impl.h"
24#include "webrtc/api/java/jni/surfacetexturehelper_jni.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000025#include "webrtc/base/bind.h"
26#include "webrtc/base/checks.h"
27#include "webrtc/base/logging.h"
Magnus Jedvertbbda54e2015-09-30 16:06:37 +020028#include "webrtc/base/scoped_ref_ptr.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000029#include "webrtc/base/thread.h"
Magnus Jedvert7e319372015-10-02 15:49:38 +020030#include "webrtc/base/timeutils.h"
kjellander6f8ce062015-11-16 13:52:24 -080031#include "webrtc/common_video/include/i420_buffer_pool.h"
perkj87d58452015-11-23 01:46:27 -080032#include "webrtc/modules/video_coding/include/video_codec_interface.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010033#include "webrtc/system_wrappers/include/logcat_trace_context.h"
34#include "webrtc/system_wrappers/include/tick_util.h"
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000035
36using rtc::Bind;
37using rtc::Thread;
38using rtc::ThreadManager;
39using rtc::scoped_ptr;
40
41using webrtc::CodecSpecificInfo;
42using webrtc::DecodedImageCallback;
43using webrtc::EncodedImage;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070044using webrtc::VideoFrame;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000045using webrtc::RTPFragmentationHeader;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000046using webrtc::TickTime;
47using webrtc::VideoCodec;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000048using webrtc::VideoCodecType;
49using webrtc::kVideoCodecH264;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000050using webrtc::kVideoCodecVP8;
Alex Glaznev69a7fd52015-11-10 10:25:40 -080051using webrtc::kVideoCodecVP9;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000052
53namespace webrtc_jni {
54
glaznevf4decb52016-01-15 13:49:22 -080055// Logging macros.
56#define TAG_DECODER "MediaCodecVideoDecoder"
57#ifdef TRACK_BUFFER_TIMING
58#define ALOGV(...)
59 __android_log_print(ANDROID_LOG_VERBOSE, TAG_DECODER, __VA_ARGS__)
60#else
61#define ALOGV(...)
62#endif
63#define ALOGD LOG_TAG(rtc::LS_INFO, TAG_DECODER)
64#define ALOGW LOG_TAG(rtc::LS_WARNING, TAG_DECODER)
65#define ALOGE LOG_TAG(rtc::LS_ERROR, TAG_DECODER)
66
glaznevae95ff32016-02-04 11:47:12 -080067enum { kMaxWarningLogFrames = 2 };
68
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000069class MediaCodecVideoDecoder : public webrtc::VideoDecoder,
70 public rtc::MessageHandler {
71 public:
Alex Glaznev4d2f4d12015-09-01 15:04:13 -070072 explicit MediaCodecVideoDecoder(
73 JNIEnv* jni, VideoCodecType codecType, jobject render_egl_context);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000074 virtual ~MediaCodecVideoDecoder();
75
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000076 int32_t InitDecode(const VideoCodec* codecSettings, int32_t numberOfCores)
77 override;
78
79 int32_t Decode(
80 const EncodedImage& inputImage, bool missingFrames,
81 const RTPFragmentationHeader* fragmentation,
82 const CodecSpecificInfo* codecSpecificInfo = NULL,
83 int64_t renderTimeMs = -1) override;
84
85 int32_t RegisterDecodeCompleteCallback(DecodedImageCallback* callback)
86 override;
87
88 int32_t Release() override;
89
perkj796cfaf2015-12-10 09:27:38 -080090 bool PrefersLateDecoding() const override { return true; }
91
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000092 // rtc::MessageHandler implementation.
93 void OnMessage(rtc::Message* msg) override;
94
Peter Boströmb7d9a972015-12-18 16:01:11 +010095 const char* ImplementationName() const override;
96
glaznev@webrtc.org18c92472015-02-18 18:42:55 +000097 private:
98 // CHECK-fail if not running on |codec_thread_|.
99 void CheckOnCodecThread();
100
101 int32_t InitDecodeOnCodecThread();
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800102 int32_t ResetDecodeOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000103 int32_t ReleaseOnCodecThread();
104 int32_t DecodeOnCodecThread(const EncodedImage& inputImage);
105 // Deliver any outputs pending in the MediaCodec to our |callback_| and return
106 // true on success.
107 bool DeliverPendingOutputs(JNIEnv* jni, int dequeue_timeout_us);
Alex Glaznev782671f2015-06-12 16:40:44 -0700108 int32_t ProcessHWErrorOnCodecThread();
glaznevae95ff32016-02-04 11:47:12 -0800109 void EnableFrameLogOnWarning();
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800110 void ResetVariables();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000111
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000112 // Type of video codec.
113 VideoCodecType codecType_;
114
kjellander60ca31b2016-01-04 10:15:53 -0800115 // Render EGL context - owned by factory, should not be allocated/destroyed
116 // by VideoDecoder.
117 jobject render_egl_context_;
118
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000119 bool key_frame_required_;
120 bool inited_;
Alex Glaznev782671f2015-06-12 16:40:44 -0700121 bool sw_fallback_required_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000122 bool use_surface_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000123 VideoCodec codec_;
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200124 webrtc::I420BufferPool decoded_frame_pool_;
Per488e75f2015-11-19 10:43:36 +0100125 rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000126 DecodedImageCallback* callback_;
127 int frames_received_; // Number of frames received by decoder.
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000128 int frames_decoded_; // Number of frames decoded by decoder.
glaznevae95ff32016-02-04 11:47:12 -0800129 // Number of decoded frames for which log information is displayed.
130 int frames_decoded_logged_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000131 int64_t start_time_ms_; // Start time for statistics.
132 int current_frames_; // Number of frames in the current statistics interval.
133 int current_bytes_; // Encoded bytes in the current statistics interval.
134 int current_decoding_time_ms_; // Overall decoding time in the current second
glaznevfd6706a2016-02-05 14:05:08 -0800135 int current_delay_time_ms_; // Overall delay time in the current second.
136 uint32_t max_pending_frames_; // Maximum number of pending input frames.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000137
138 // State that is constant for the lifetime of this object once the ctor
139 // returns.
140 scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec.
141 ScopedGlobalRef<jclass> j_media_codec_video_decoder_class_;
142 ScopedGlobalRef<jobject> j_media_codec_video_decoder_;
143 jmethodID j_init_decode_method_;
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800144 jmethodID j_reset_method_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000145 jmethodID j_release_method_;
146 jmethodID j_dequeue_input_buffer_method_;
147 jmethodID j_queue_input_buffer_method_;
Per488e75f2015-11-19 10:43:36 +0100148 jmethodID j_dequeue_byte_buffer_method_;
149 jmethodID j_dequeue_texture_buffer_method_;
magjed44bf6f52015-10-03 02:08:00 -0700150 jmethodID j_return_decoded_byte_buffer_method_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000151 // MediaCodecVideoDecoder fields.
152 jfieldID j_input_buffers_field_;
153 jfieldID j_output_buffers_field_;
154 jfieldID j_color_format_field_;
155 jfieldID j_width_field_;
156 jfieldID j_height_field_;
157 jfieldID j_stride_field_;
158 jfieldID j_slice_height_field_;
magjed44bf6f52015-10-03 02:08:00 -0700159 // MediaCodecVideoDecoder.DecodedTextureBuffer fields.
Per488e75f2015-11-19 10:43:36 +0100160 jfieldID j_texture_id_field_;
161 jfieldID j_transform_matrix_field_;
glaznev94291482016-02-01 13:17:18 -0800162 jfieldID j_texture_presentation_timestamp_ms_field_;
Per488e75f2015-11-19 10:43:36 +0100163 jfieldID j_texture_timestamp_ms_field_;
164 jfieldID j_texture_ntp_timestamp_ms_field_;
165 jfieldID j_texture_decode_time_ms_field_;
166 jfieldID j_texture_frame_delay_ms_field_;
167 // MediaCodecVideoDecoder.DecodedOutputBuffer fields.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000168 jfieldID j_info_index_field_;
169 jfieldID j_info_offset_field_;
170 jfieldID j_info_size_field_;
glaznev94291482016-02-01 13:17:18 -0800171 jfieldID j_presentation_timestamp_ms_field_;
172 jfieldID j_timestamp_ms_field_;
173 jfieldID j_ntp_timestamp_ms_field_;
Per488e75f2015-11-19 10:43:36 +0100174 jfieldID j_byte_buffer_decode_time_ms_field_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000175
176 // Global references; must be deleted in Release().
177 std::vector<jobject> input_buffers_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000178};
179
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000180MediaCodecVideoDecoder::MediaCodecVideoDecoder(
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700181 JNIEnv* jni, VideoCodecType codecType, jobject render_egl_context) :
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000182 codecType_(codecType),
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700183 render_egl_context_(render_egl_context),
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000184 key_frame_required_(true),
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000185 inited_(false),
Alex Glaznev782671f2015-06-12 16:40:44 -0700186 sw_fallback_required_(false),
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000187 codec_thread_(new Thread()),
188 j_media_codec_video_decoder_class_(
189 jni,
190 FindClass(jni, "org/webrtc/MediaCodecVideoDecoder")),
191 j_media_codec_video_decoder_(
192 jni,
193 jni->NewObject(*j_media_codec_video_decoder_class_,
194 GetMethodID(jni,
195 *j_media_codec_video_decoder_class_,
196 "<init>",
197 "()V"))) {
198 ScopedLocalRefFrame local_ref_frame(jni);
199 codec_thread_->SetName("MediaCodecVideoDecoder", NULL);
henrikg91d6ede2015-09-17 00:24:34 -0700200 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoDecoder";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000201
202 j_init_decode_method_ = GetMethodID(
203 jni, *j_media_codec_video_decoder_class_, "initDecode",
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000204 "(Lorg/webrtc/MediaCodecVideoDecoder$VideoCodecType;"
Per488e75f2015-11-19 10:43:36 +0100205 "IILorg/webrtc/SurfaceTextureHelper;)Z");
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800206 j_reset_method_ =
207 GetMethodID(jni, *j_media_codec_video_decoder_class_, "reset", "(II)V");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000208 j_release_method_ =
209 GetMethodID(jni, *j_media_codec_video_decoder_class_, "release", "()V");
210 j_dequeue_input_buffer_method_ = GetMethodID(
211 jni, *j_media_codec_video_decoder_class_, "dequeueInputBuffer", "()I");
212 j_queue_input_buffer_method_ = GetMethodID(
Per488e75f2015-11-19 10:43:36 +0100213 jni, *j_media_codec_video_decoder_class_, "queueInputBuffer", "(IIJJJ)Z");
214 j_dequeue_byte_buffer_method_ = GetMethodID(
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000215 jni, *j_media_codec_video_decoder_class_, "dequeueOutputBuffer",
Per488e75f2015-11-19 10:43:36 +0100216 "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer;");
217 j_dequeue_texture_buffer_method_ = GetMethodID(
218 jni, *j_media_codec_video_decoder_class_, "dequeueTextureBuffer",
219 "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer;");
magjed44bf6f52015-10-03 02:08:00 -0700220 j_return_decoded_byte_buffer_method_ =
221 GetMethodID(jni, *j_media_codec_video_decoder_class_,
Per488e75f2015-11-19 10:43:36 +0100222 "returnDecodedOutputBuffer", "(I)V");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000223
224 j_input_buffers_field_ = GetFieldID(
225 jni, *j_media_codec_video_decoder_class_,
226 "inputBuffers", "[Ljava/nio/ByteBuffer;");
227 j_output_buffers_field_ = GetFieldID(
228 jni, *j_media_codec_video_decoder_class_,
229 "outputBuffers", "[Ljava/nio/ByteBuffer;");
230 j_color_format_field_ = GetFieldID(
231 jni, *j_media_codec_video_decoder_class_, "colorFormat", "I");
232 j_width_field_ = GetFieldID(
233 jni, *j_media_codec_video_decoder_class_, "width", "I");
234 j_height_field_ = GetFieldID(
235 jni, *j_media_codec_video_decoder_class_, "height", "I");
236 j_stride_field_ = GetFieldID(
237 jni, *j_media_codec_video_decoder_class_, "stride", "I");
238 j_slice_height_field_ = GetFieldID(
239 jni, *j_media_codec_video_decoder_class_, "sliceHeight", "I");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000240
Per488e75f2015-11-19 10:43:36 +0100241 jclass j_decoded_texture_buffer_class = FindClass(jni,
magjed44bf6f52015-10-03 02:08:00 -0700242 "org/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer");
Per488e75f2015-11-19 10:43:36 +0100243 j_texture_id_field_ = GetFieldID(
244 jni, j_decoded_texture_buffer_class, "textureID", "I");
245 j_transform_matrix_field_ = GetFieldID(
246 jni, j_decoded_texture_buffer_class, "transformMatrix", "[F");
glaznev94291482016-02-01 13:17:18 -0800247 j_texture_presentation_timestamp_ms_field_ = GetFieldID(
248 jni, j_decoded_texture_buffer_class, "presentationTimeStampMs", "J");
Per488e75f2015-11-19 10:43:36 +0100249 j_texture_timestamp_ms_field_ = GetFieldID(
250 jni, j_decoded_texture_buffer_class, "timeStampMs", "J");
251 j_texture_ntp_timestamp_ms_field_ = GetFieldID(
252 jni, j_decoded_texture_buffer_class, "ntpTimeStampMs", "J");
253 j_texture_decode_time_ms_field_ = GetFieldID(
254 jni, j_decoded_texture_buffer_class, "decodeTimeMs", "J");
255 j_texture_frame_delay_ms_field_ = GetFieldID(
256 jni, j_decoded_texture_buffer_class, "frameDelayMs", "J");
magjed44bf6f52015-10-03 02:08:00 -0700257
Per488e75f2015-11-19 10:43:36 +0100258 jclass j_decoded_output_buffer_class = FindClass(jni,
259 "org/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000260 j_info_index_field_ = GetFieldID(
Per488e75f2015-11-19 10:43:36 +0100261 jni, j_decoded_output_buffer_class, "index", "I");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000262 j_info_offset_field_ = GetFieldID(
Per488e75f2015-11-19 10:43:36 +0100263 jni, j_decoded_output_buffer_class, "offset", "I");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000264 j_info_size_field_ = GetFieldID(
Per488e75f2015-11-19 10:43:36 +0100265 jni, j_decoded_output_buffer_class, "size", "I");
glaznev94291482016-02-01 13:17:18 -0800266 j_presentation_timestamp_ms_field_ = GetFieldID(
267 jni, j_decoded_output_buffer_class, "presentationTimeStampMs", "J");
268 j_timestamp_ms_field_ = GetFieldID(
Per488e75f2015-11-19 10:43:36 +0100269 jni, j_decoded_output_buffer_class, "timeStampMs", "J");
glaznev94291482016-02-01 13:17:18 -0800270 j_ntp_timestamp_ms_field_ = GetFieldID(
Per488e75f2015-11-19 10:43:36 +0100271 jni, j_decoded_output_buffer_class, "ntpTimeStampMs", "J");
272 j_byte_buffer_decode_time_ms_field_ = GetFieldID(
273 jni, j_decoded_output_buffer_class, "decodeTimeMs", "J");
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000274
275 CHECK_EXCEPTION(jni) << "MediaCodecVideoDecoder ctor failed";
Magnus Jedvert207370f2015-09-16 12:32:21 +0200276 use_surface_ = (render_egl_context_ != NULL);
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700277 ALOGD << "MediaCodecVideoDecoder ctor. Use surface: " << use_surface_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000278 memset(&codec_, 0, sizeof(codec_));
279 AllowBlockingCalls();
280}
281
282MediaCodecVideoDecoder::~MediaCodecVideoDecoder() {
283 // Call Release() to ensure no more callbacks to us after we are deleted.
284 Release();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000285}
286
287int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* inst,
288 int32_t numberOfCores) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700289 ALOGD << "InitDecode.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000290 if (inst == NULL) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700291 ALOGE << "NULL VideoCodec instance";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000292 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
293 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000294 // Factory should guard against other codecs being used with us.
henrikg91d6ede2015-09-17 00:24:34 -0700295 RTC_CHECK(inst->codecType == codecType_)
296 << "Unsupported codec " << inst->codecType << " for " << codecType_;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000297
Alex Glaznev782671f2015-06-12 16:40:44 -0700298 if (sw_fallback_required_) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700299 ALOGE << "InitDecode() - fallback to SW decoder";
Alex Glaznev782671f2015-06-12 16:40:44 -0700300 return WEBRTC_VIDEO_CODEC_OK;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000301 }
302 // Save VideoCodec instance for later.
303 if (&codec_ != inst) {
304 codec_ = *inst;
305 }
glazneve55c42c2015-10-28 10:30:32 -0700306 // If maxFramerate is not set then assume 30 fps.
307 codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 30;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000308
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000309 // Call Java init.
310 return codec_thread_->Invoke<int32_t>(
311 Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this));
312}
313
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800314void MediaCodecVideoDecoder::ResetVariables() {
315 CheckOnCodecThread();
316
317 key_frame_required_ = true;
318 frames_received_ = 0;
319 frames_decoded_ = 0;
320 frames_decoded_logged_ = kMaxDecodedLogFrames;
321 start_time_ms_ = GetCurrentTimeMs();
322 current_frames_ = 0;
323 current_bytes_ = 0;
324 current_decoding_time_ms_ = 0;
325 current_delay_time_ms_ = 0;
326}
327
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000328int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() {
329 CheckOnCodecThread();
330 JNIEnv* jni = AttachCurrentThreadIfNeeded();
331 ScopedLocalRefFrame local_ref_frame(jni);
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700332 ALOGD << "InitDecodeOnCodecThread Type: " << (int)codecType_ << ". "
333 << codec_.width << " x " << codec_.height << ". Fps: " <<
334 (int)codec_.maxFramerate;
Alex Glaznev782671f2015-06-12 16:40:44 -0700335
336 // Release previous codec first if it was allocated before.
337 int ret_val = ReleaseOnCodecThread();
338 if (ret_val < 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700339 ALOGE << "Release failure: " << ret_val << " - fallback to SW codec";
Alex Glaznev782671f2015-06-12 16:40:44 -0700340 sw_fallback_required_ = true;
341 return WEBRTC_VIDEO_CODEC_ERROR;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000342 }
343
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800344 ResetVariables();
Alex Glaznev782671f2015-06-12 16:40:44 -0700345
Per488e75f2015-11-19 10:43:36 +0100346 if (use_surface_) {
347 surface_texture_helper_ = new rtc::RefCountedObject<SurfaceTextureHelper>(
magjed82b750b2016-03-31 00:54:15 -0700348 jni, "Decoder SurfaceTextureHelper", render_egl_context_);
Per488e75f2015-11-19 10:43:36 +0100349 }
350
Perec2922f2016-01-27 15:25:46 +0100351 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000352 jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000353 bool success = jni->CallBooleanMethod(
354 *j_media_codec_video_decoder_,
355 j_init_decode_method_,
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000356 j_video_codec_enum,
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000357 codec_.width,
358 codec_.height,
magjed0dc23162016-03-14 03:59:38 -0700359 use_surface_ ? surface_texture_helper_->GetJavaSurfaceTextureHelper()
360 : nullptr);
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800361
Alex Glaznev782671f2015-06-12 16:40:44 -0700362 if (CheckException(jni) || !success) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700363 ALOGE << "Codec initialization error - fallback to SW codec.";
Alex Glaznev782671f2015-06-12 16:40:44 -0700364 sw_fallback_required_ = true;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000365 return WEBRTC_VIDEO_CODEC_ERROR;
366 }
367 inited_ = true;
368
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +0000369 switch (codecType_) {
370 case kVideoCodecVP8:
371 max_pending_frames_ = kMaxPendingFramesVp8;
372 break;
Alex Glaznev69a7fd52015-11-10 10:25:40 -0800373 case kVideoCodecVP9:
374 max_pending_frames_ = kMaxPendingFramesVp9;
375 break;
glaznev@webrtc.orga4623d22015-02-25 00:02:50 +0000376 case kVideoCodecH264:
377 max_pending_frames_ = kMaxPendingFramesH264;
378 break;
379 default:
380 max_pending_frames_ = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000381 }
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800382 ALOGD << "Maximum amount of pending frames: " << max_pending_frames_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000383
384 jobjectArray input_buffers = (jobjectArray)GetObjectField(
385 jni, *j_media_codec_video_decoder_, j_input_buffers_field_);
386 size_t num_input_buffers = jni->GetArrayLength(input_buffers);
387 input_buffers_.resize(num_input_buffers);
388 for (size_t i = 0; i < num_input_buffers; ++i) {
389 input_buffers_[i] =
390 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i));
Alex Glaznev782671f2015-06-12 16:40:44 -0700391 if (CheckException(jni)) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700392 ALOGE << "NewGlobalRef error - fallback to SW codec.";
Alex Glaznev782671f2015-06-12 16:40:44 -0700393 sw_fallback_required_ = true;
394 return WEBRTC_VIDEO_CODEC_ERROR;
395 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000396 }
397
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000398 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
399
400 return WEBRTC_VIDEO_CODEC_OK;
401}
402
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800403int32_t MediaCodecVideoDecoder::ResetDecodeOnCodecThread() {
404 CheckOnCodecThread();
405 JNIEnv* jni = AttachCurrentThreadIfNeeded();
406 ScopedLocalRefFrame local_ref_frame(jni);
407 ALOGD << "ResetDecodeOnCodecThread Type: " << (int)codecType_ << ". "
408 << codec_.width << " x " << codec_.height;
409 ALOGD << " Frames received: " << frames_received_ <<
410 ". Frames decoded: " << frames_decoded_;
411
412 inited_ = false;
413 rtc::MessageQueueManager::Clear(this);
414 ResetVariables();
415
416 jni->CallVoidMethod(
417 *j_media_codec_video_decoder_,
418 j_reset_method_,
419 codec_.width,
420 codec_.height);
421
422 if (CheckException(jni)) {
423 ALOGE << "Soft reset error - fallback to SW codec.";
424 sw_fallback_required_ = true;
425 return WEBRTC_VIDEO_CODEC_ERROR;
426 }
427 inited_ = true;
428
429 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
430
431 return WEBRTC_VIDEO_CODEC_OK;
432}
433
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000434int32_t MediaCodecVideoDecoder::Release() {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700435 ALOGD << "DecoderRelease request";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000436 return codec_thread_->Invoke<int32_t>(
437 Bind(&MediaCodecVideoDecoder::ReleaseOnCodecThread, this));
438}
439
440int32_t MediaCodecVideoDecoder::ReleaseOnCodecThread() {
441 if (!inited_) {
442 return WEBRTC_VIDEO_CODEC_OK;
443 }
444 CheckOnCodecThread();
445 JNIEnv* jni = AttachCurrentThreadIfNeeded();
glazneve55c42c2015-10-28 10:30:32 -0700446 ALOGD << "DecoderReleaseOnCodecThread: Frames received: " <<
447 frames_received_ << ". Frames decoded: " << frames_decoded_;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000448 ScopedLocalRefFrame local_ref_frame(jni);
449 for (size_t i = 0; i < input_buffers_.size(); i++) {
450 jni->DeleteGlobalRef(input_buffers_[i]);
451 }
452 input_buffers_.clear();
453 jni->CallVoidMethod(*j_media_codec_video_decoder_, j_release_method_);
Per488e75f2015-11-19 10:43:36 +0100454 surface_texture_helper_ = nullptr;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000455 inited_ = false;
Alex Glaznev782671f2015-06-12 16:40:44 -0700456 rtc::MessageQueueManager::Clear(this);
457 if (CheckException(jni)) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700458 ALOGE << "Decoder release exception";
Alex Glaznev782671f2015-06-12 16:40:44 -0700459 return WEBRTC_VIDEO_CODEC_ERROR;
460 }
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700461 ALOGD << "DecoderReleaseOnCodecThread done";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000462 return WEBRTC_VIDEO_CODEC_OK;
463}
464
465void MediaCodecVideoDecoder::CheckOnCodecThread() {
kwiberg9708e9c2016-03-29 10:17:42 -0700466 RTC_CHECK(codec_thread_.get() == ThreadManager::Instance()->CurrentThread())
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000467 << "Running on wrong thread!";
468}
469
glaznevae95ff32016-02-04 11:47:12 -0800470void MediaCodecVideoDecoder::EnableFrameLogOnWarning() {
471 // Log next 2 output frames.
472 frames_decoded_logged_ = std::max(
473 frames_decoded_logged_, frames_decoded_ + kMaxWarningLogFrames);
474}
475
Alex Glaznev782671f2015-06-12 16:40:44 -0700476int32_t MediaCodecVideoDecoder::ProcessHWErrorOnCodecThread() {
477 CheckOnCodecThread();
478 int ret_val = ReleaseOnCodecThread();
479 if (ret_val < 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700480 ALOGE << "ProcessHWError: Release failure";
Alex Glaznev782671f2015-06-12 16:40:44 -0700481 }
482 if (codecType_ == kVideoCodecH264) {
483 // For now there is no SW H.264 which can be used as fallback codec.
484 // So try to restart hw codec for now.
485 ret_val = InitDecodeOnCodecThread();
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700486 ALOGE << "Reset H.264 codec done. Status: " << ret_val;
Alex Glaznev782671f2015-06-12 16:40:44 -0700487 if (ret_val == WEBRTC_VIDEO_CODEC_OK) {
488 // H.264 codec was succesfully reset - return regular error code.
489 return WEBRTC_VIDEO_CODEC_ERROR;
490 } else {
491 // Fail to restart H.264 codec - return error code which should stop the
492 // call.
493 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
494 }
495 } else {
496 sw_fallback_required_ = true;
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700497 ALOGE << "Return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE";
Alex Glaznev782671f2015-06-12 16:40:44 -0700498 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
499 }
500}
501
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000502int32_t MediaCodecVideoDecoder::Decode(
503 const EncodedImage& inputImage,
504 bool missingFrames,
505 const RTPFragmentationHeader* fragmentation,
506 const CodecSpecificInfo* codecSpecificInfo,
507 int64_t renderTimeMs) {
Alex Glaznev782671f2015-06-12 16:40:44 -0700508 if (sw_fallback_required_) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700509 ALOGE << "Decode() - fallback to SW codec";
Alex Glaznev782671f2015-06-12 16:40:44 -0700510 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000511 }
512 if (callback_ == NULL) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700513 ALOGE << "Decode() - callback_ is NULL";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000514 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
515 }
516 if (inputImage._buffer == NULL && inputImage._length > 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700517 ALOGE << "Decode() - inputImage is incorrect";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000518 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
519 }
Alex Glaznev782671f2015-06-12 16:40:44 -0700520 if (!inited_) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700521 ALOGE << "Decode() - decoder is not initialized";
Alex Glaznev782671f2015-06-12 16:40:44 -0700522 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
523 }
524
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000525 // Check if encoded frame dimension has changed.
526 if ((inputImage._encodedWidth * inputImage._encodedHeight > 0) &&
527 (inputImage._encodedWidth != codec_.width ||
528 inputImage._encodedHeight != codec_.height)) {
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800529 ALOGW << "Input resolution changed from " <<
530 codec_.width << " x " << codec_.height << " to " <<
531 inputImage._encodedWidth << " x " << inputImage._encodedHeight;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000532 codec_.width = inputImage._encodedWidth;
533 codec_.height = inputImage._encodedHeight;
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800534 int32_t ret;
Alex Glaznev79299af2016-04-12 16:39:39 -0700535 if (use_surface_ &&
536 (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecH264)) {
537 // Soft codec reset - only for surface decoding.
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800538 ret = codec_thread_->Invoke<int32_t>(Bind(
539 &MediaCodecVideoDecoder::ResetDecodeOnCodecThread, this));
540 } else {
541 // Hard codec reset.
542 ret = InitDecode(&codec_, 1);
543 }
Alex Glaznev782671f2015-06-12 16:40:44 -0700544 if (ret < 0) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700545 ALOGE << "InitDecode failure: " << ret << " - fallback to SW codec";
Alex Glaznev782671f2015-06-12 16:40:44 -0700546 sw_fallback_required_ = true;
547 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE;
548 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000549 }
550
551 // Always start with a complete key frame.
552 if (key_frame_required_) {
Peter Boström49e196a2015-10-23 15:58:18 +0200553 if (inputImage._frameType != webrtc::kVideoFrameKey) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700554 ALOGE << "Decode() - key frame is required";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000555 return WEBRTC_VIDEO_CODEC_ERROR;
556 }
557 if (!inputImage._completeFrame) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700558 ALOGE << "Decode() - complete frame is required";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000559 return WEBRTC_VIDEO_CODEC_ERROR;
560 }
561 key_frame_required_ = false;
562 }
563 if (inputImage._length == 0) {
564 return WEBRTC_VIDEO_CODEC_ERROR;
565 }
566
567 return codec_thread_->Invoke<int32_t>(Bind(
568 &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage));
569}
570
571int32_t MediaCodecVideoDecoder::DecodeOnCodecThread(
572 const EncodedImage& inputImage) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000573 CheckOnCodecThread();
574 JNIEnv* jni = AttachCurrentThreadIfNeeded();
575 ScopedLocalRefFrame local_ref_frame(jni);
576
577 // Try to drain the decoder and wait until output is not too
578 // much behind the input.
Alex Glaznev6a4a03c2016-03-04 14:10:50 -0800579 if (codecType_ == kVideoCodecH264 &&
580 frames_received_ > frames_decoded_ + max_pending_frames_) {
581 // Print warning for H.264 only - for VP8/VP9 one frame delay is ok.
glaznevae95ff32016-02-04 11:47:12 -0800582 ALOGW << "Decoder is too far behind. Try to drain. Received: " <<
583 frames_received_ << ". Decoded: " << frames_decoded_;
584 EnableFrameLogOnWarning();
585 }
Per488e75f2015-11-19 10:43:36 +0100586 const int64 drain_start = GetCurrentTimeMs();
587 while ((frames_received_ > frames_decoded_ + max_pending_frames_) &&
588 (GetCurrentTimeMs() - drain_start) < kMediaCodecTimeoutMs) {
Per488e75f2015-11-19 10:43:36 +0100589 if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) {
glazneve55c42c2015-10-28 10:30:32 -0700590 ALOGE << "DeliverPendingOutputs error. Frames received: " <<
591 frames_received_ << ". Frames decoded: " << frames_decoded_;
Alex Glaznev782671f2015-06-12 16:40:44 -0700592 return ProcessHWErrorOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000593 }
Per488e75f2015-11-19 10:43:36 +0100594 }
595 if (frames_received_ > frames_decoded_ + max_pending_frames_) {
596 ALOGE << "Output buffer dequeue timeout. Frames received: " <<
597 frames_received_ << ". Frames decoded: " << frames_decoded_;
598 return ProcessHWErrorOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000599 }
600
601 // Get input buffer.
glaznevae95ff32016-02-04 11:47:12 -0800602 int j_input_buffer_index = jni->CallIntMethod(
603 *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_);
Alex Glaznev782671f2015-06-12 16:40:44 -0700604 if (CheckException(jni) || j_input_buffer_index < 0) {
glaznevae95ff32016-02-04 11:47:12 -0800605 ALOGE << "dequeueInputBuffer error: " << j_input_buffer_index <<
606 ". Retry DeliverPendingOutputs.";
607 EnableFrameLogOnWarning();
608 // Try to drain the decoder.
609 if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) {
610 ALOGE << "DeliverPendingOutputs error. Frames received: " <<
611 frames_received_ << ". Frames decoded: " << frames_decoded_;
612 return ProcessHWErrorOnCodecThread();
613 }
614 // Try dequeue input buffer one last time.
615 j_input_buffer_index = jni->CallIntMethod(
616 *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_);
617 if (CheckException(jni) || j_input_buffer_index < 0) {
618 ALOGE << "dequeueInputBuffer critical error: " << j_input_buffer_index;
619 return ProcessHWErrorOnCodecThread();
620 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000621 }
622
623 // Copy encoded data to Java ByteBuffer.
624 jobject j_input_buffer = input_buffers_[j_input_buffer_index];
Peter Boström0c4e06b2015-10-07 12:23:21 +0200625 uint8_t* buffer =
626 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer));
henrikg91d6ede2015-09-17 00:24:34 -0700627 RTC_CHECK(buffer) << "Indirect buffer??";
Peter Boström0c4e06b2015-10-07 12:23:21 +0200628 int64_t buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer);
Alex Glaznev782671f2015-06-12 16:40:44 -0700629 if (CheckException(jni) || buffer_capacity < inputImage._length) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700630 ALOGE << "Input frame size "<< inputImage._length <<
631 " is bigger than buffer size " << buffer_capacity;
Alex Glaznev782671f2015-06-12 16:40:44 -0700632 return ProcessHWErrorOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000633 }
glaznevae95ff32016-02-04 11:47:12 -0800634 jlong presentation_timestamp_us = static_cast<jlong>(
635 static_cast<int64_t>(frames_received_) * 1000000 / codec_.maxFramerate);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000636 memcpy(buffer, inputImage._buffer, inputImage._length);
637
glaznevae95ff32016-02-04 11:47:12 -0800638 if (frames_decoded_ < frames_decoded_logged_) {
glaznev94291482016-02-01 13:17:18 -0800639 ALOGD << "Decoder frame in # " << frames_received_ <<
640 ". Type: " << inputImage._frameType <<
641 ". Buffer # " << j_input_buffer_index <<
glaznevae95ff32016-02-04 11:47:12 -0800642 ". TS: " << presentation_timestamp_us / 1000 <<
glaznev94291482016-02-01 13:17:18 -0800643 ". Size: " << inputImage._length;
644 }
645
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000646 // Save input image timestamps for later output.
647 frames_received_++;
648 current_bytes_ += inputImage._length;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000649
650 // Feed input to decoder.
Per488e75f2015-11-19 10:43:36 +0100651 bool success = jni->CallBooleanMethod(
652 *j_media_codec_video_decoder_,
653 j_queue_input_buffer_method_,
654 j_input_buffer_index,
655 inputImage._length,
656 presentation_timestamp_us,
657 static_cast<int64_t> (inputImage._timeStamp),
658 inputImage.ntp_time_ms_);
Alex Glaznev782671f2015-06-12 16:40:44 -0700659 if (CheckException(jni) || !success) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700660 ALOGE << "queueInputBuffer error";
Alex Glaznev782671f2015-06-12 16:40:44 -0700661 return ProcessHWErrorOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000662 }
663
664 // Try to drain the decoder
665 if (!DeliverPendingOutputs(jni, 0)) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700666 ALOGE << "DeliverPendingOutputs error";
Alex Glaznev782671f2015-06-12 16:40:44 -0700667 return ProcessHWErrorOnCodecThread();
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000668 }
669
670 return WEBRTC_VIDEO_CODEC_OK;
671}
672
673bool MediaCodecVideoDecoder::DeliverPendingOutputs(
Per488e75f2015-11-19 10:43:36 +0100674 JNIEnv* jni, int dequeue_timeout_ms) {
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000675 if (frames_received_ <= frames_decoded_) {
676 // No need to query for output buffers - decoder is drained.
677 return true;
678 }
679 // Get decoder output.
Per488e75f2015-11-19 10:43:36 +0100680 jobject j_decoder_output_buffer =
681 jni->CallObjectMethod(*j_media_codec_video_decoder_,
682 use_surface_ ? j_dequeue_texture_buffer_method_
683 : j_dequeue_byte_buffer_method_,
684 dequeue_timeout_ms);
685
Alex Glaznev782671f2015-06-12 16:40:44 -0700686 if (CheckException(jni)) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700687 ALOGE << "dequeueOutputBuffer() error";
Alex Glaznev782671f2015-06-12 16:40:44 -0700688 return false;
689 }
magjed44bf6f52015-10-03 02:08:00 -0700690 if (IsNull(jni, j_decoder_output_buffer)) {
691 // No decoded frame ready.
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000692 return true;
693 }
694
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000695 // Get decoded video frame properties.
696 int color_format = GetIntField(jni, *j_media_codec_video_decoder_,
697 j_color_format_field_);
698 int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_);
699 int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_);
700 int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_);
701 int slice_height = GetIntField(jni, *j_media_codec_video_decoder_,
702 j_slice_height_field_);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000703
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200704 rtc::scoped_refptr<webrtc::VideoFrameBuffer> frame_buffer;
glaznev94291482016-02-01 13:17:18 -0800705 int64_t presentation_timestamps_ms = 0;
Per488e75f2015-11-19 10:43:36 +0100706 int64_t output_timestamps_ms = 0;
707 int64_t output_ntp_timestamps_ms = 0;
708 int decode_time_ms = 0;
709 int64_t frame_delayed_ms = 0;
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200710 if (use_surface_) {
magjed44bf6f52015-10-03 02:08:00 -0700711 // Extract data from Java DecodedTextureBuffer.
glaznevae95ff32016-02-04 11:47:12 -0800712 presentation_timestamps_ms = GetLongField(
713 jni, j_decoder_output_buffer,
714 j_texture_presentation_timestamp_ms_field_);
715 output_timestamps_ms = GetLongField(
716 jni, j_decoder_output_buffer, j_texture_timestamp_ms_field_);
717 output_ntp_timestamps_ms = GetLongField(
718 jni, j_decoder_output_buffer, j_texture_ntp_timestamp_ms_field_);
719 decode_time_ms = GetLongField(
720 jni, j_decoder_output_buffer, j_texture_decode_time_ms_field_);
721
magjed44bf6f52015-10-03 02:08:00 -0700722 const int texture_id =
Per488e75f2015-11-19 10:43:36 +0100723 GetIntField(jni, j_decoder_output_buffer, j_texture_id_field_);
724 if (texture_id != 0) { // |texture_id| == 0 represents a dropped frame.
725 const jfloatArray j_transform_matrix =
726 reinterpret_cast<jfloatArray>(GetObjectField(
727 jni, j_decoder_output_buffer, j_transform_matrix_field_));
glaznev94291482016-02-01 13:17:18 -0800728 frame_delayed_ms = GetLongField(
729 jni, j_decoder_output_buffer, j_texture_frame_delay_ms_field_);
Per488e75f2015-11-19 10:43:36 +0100730
731 // Create webrtc::VideoFrameBuffer with native texture handle.
732 frame_buffer = surface_texture_helper_->CreateTextureFrame(
733 width, height, NativeHandleImpl(jni, texture_id, j_transform_matrix));
glaznevae95ff32016-02-04 11:47:12 -0800734 } else {
735 EnableFrameLogOnWarning();
Per488e75f2015-11-19 10:43:36 +0100736 }
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200737 } else {
738 // Extract data from Java ByteBuffer and create output yuv420 frame -
739 // for non surface decoding only.
glaznev94291482016-02-01 13:17:18 -0800740 const int output_buffer_index = GetIntField(
741 jni, j_decoder_output_buffer, j_info_index_field_);
742 const int output_buffer_offset = GetIntField(
743 jni, j_decoder_output_buffer, j_info_offset_field_);
744 const int output_buffer_size = GetIntField(
745 jni, j_decoder_output_buffer, j_info_size_field_);
746 presentation_timestamps_ms = GetLongField(
747 jni, j_decoder_output_buffer, j_presentation_timestamp_ms_field_);
748 output_timestamps_ms = GetLongField(
749 jni, j_decoder_output_buffer, j_timestamp_ms_field_);
750 output_ntp_timestamps_ms = GetLongField(
751 jni, j_decoder_output_buffer, j_ntp_timestamp_ms_field_);
Per488e75f2015-11-19 10:43:36 +0100752
753 decode_time_ms = GetLongField(jni, j_decoder_output_buffer,
754 j_byte_buffer_decode_time_ms_field_);
magjed44bf6f52015-10-03 02:08:00 -0700755
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000756 if (output_buffer_size < width * height * 3 / 2) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700757 ALOGE << "Insufficient output buffer size: " << output_buffer_size;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000758 return false;
759 }
glaznev3816bfd2016-03-08 10:35:33 -0800760 if (output_buffer_size < stride * height * 3 / 2 &&
761 slice_height == height && stride > width) {
762 // Some codecs (Exynos) incorrectly report stride information for
763 // output byte buffer, so actual stride value need to be corrected.
764 stride = output_buffer_size * 2 / (height * 3);
765 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000766 jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField(
767 jni, *j_media_codec_video_decoder_, j_output_buffers_field_));
768 jobject output_buffer =
769 jni->GetObjectArrayElement(output_buffers, output_buffer_index);
770 uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(
771 output_buffer));
Alex Glaznev782671f2015-06-12 16:40:44 -0700772 if (CheckException(jni)) {
773 return false;
774 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000775 payload += output_buffer_offset;
776
777 // Create yuv420 frame.
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200778 frame_buffer = decoded_frame_pool_.CreateBuffer(width, height);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000779 if (color_format == COLOR_FormatYUV420Planar) {
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200780 RTC_CHECK_EQ(0, stride % 2);
781 RTC_CHECK_EQ(0, slice_height % 2);
782 const int uv_stride = stride / 2;
783 const int u_slice_height = slice_height / 2;
784 const uint8_t* y_ptr = payload;
785 const uint8_t* u_ptr = y_ptr + stride * slice_height;
786 const uint8_t* v_ptr = u_ptr + uv_stride * u_slice_height;
787 libyuv::I420Copy(y_ptr, stride,
788 u_ptr, uv_stride,
789 v_ptr, uv_stride,
790 frame_buffer->MutableData(webrtc::kYPlane),
791 frame_buffer->stride(webrtc::kYPlane),
792 frame_buffer->MutableData(webrtc::kUPlane),
793 frame_buffer->stride(webrtc::kUPlane),
794 frame_buffer->MutableData(webrtc::kVPlane),
795 frame_buffer->stride(webrtc::kVPlane),
796 width, height);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000797 } else {
798 // All other supported formats are nv12.
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200799 const uint8_t* y_ptr = payload;
800 const uint8_t* uv_ptr = y_ptr + stride * slice_height;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000801 libyuv::NV12ToI420(
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200802 y_ptr, stride,
803 uv_ptr, stride,
804 frame_buffer->MutableData(webrtc::kYPlane),
805 frame_buffer->stride(webrtc::kYPlane),
806 frame_buffer->MutableData(webrtc::kUPlane),
807 frame_buffer->stride(webrtc::kUPlane),
808 frame_buffer->MutableData(webrtc::kVPlane),
809 frame_buffer->stride(webrtc::kVPlane),
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000810 width, height);
811 }
magjed44bf6f52015-10-03 02:08:00 -0700812 // Return output byte buffer back to codec.
813 jni->CallVoidMethod(
814 *j_media_codec_video_decoder_,
815 j_return_decoded_byte_buffer_method_,
816 output_buffer_index);
817 if (CheckException(jni)) {
Per488e75f2015-11-19 10:43:36 +0100818 ALOGE << "returnDecodedOutputBuffer error";
magjed44bf6f52015-10-03 02:08:00 -0700819 return false;
820 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000821 }
Magnus Jedvertbbda54e2015-09-30 16:06:37 +0200822 VideoFrame decoded_frame(frame_buffer, 0, 0, webrtc::kVideoRotation_0);
Per488e75f2015-11-19 10:43:36 +0100823 decoded_frame.set_timestamp(output_timestamps_ms);
824 decoded_frame.set_ntp_time_ms(output_ntp_timestamps_ms);
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000825
glaznevae95ff32016-02-04 11:47:12 -0800826 if (frames_decoded_ < frames_decoded_logged_) {
glaznev94291482016-02-01 13:17:18 -0800827 ALOGD << "Decoder frame out # " << frames_decoded_ <<
828 ". " << width << " x " << height <<
829 ". " << stride << " x " << slice_height <<
830 ". Color: " << color_format <<
831 ". TS: " << presentation_timestamps_ms <<
Per488e75f2015-11-19 10:43:36 +0100832 ". DecTime: " << (int)decode_time_ms <<
833 ". DelayTime: " << (int)frame_delayed_ms;
glazneve55c42c2015-10-28 10:30:32 -0700834 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000835
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000836 // Calculate and print decoding statistics - every 3 seconds.
837 frames_decoded_++;
838 current_frames_++;
Per488e75f2015-11-19 10:43:36 +0100839 current_decoding_time_ms_ += decode_time_ms;
glaznevfd6706a2016-02-05 14:05:08 -0800840 current_delay_time_ms_ += frame_delayed_ms;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000841 int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_;
842 if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs &&
843 current_frames_ > 0) {
glaznev94291482016-02-01 13:17:18 -0800844 int current_bitrate = current_bytes_ * 8 / statistic_time_ms;
845 int current_fps =
846 (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms;
glaznevfd6706a2016-02-05 14:05:08 -0800847 ALOGD << "Frames decoded: " << frames_decoded_ <<
848 ". Received: " << frames_received_ <<
glaznev94291482016-02-01 13:17:18 -0800849 ". Bitrate: " << current_bitrate << " kbps" <<
850 ". Fps: " << current_fps <<
851 ". DecTime: " << (current_decoding_time_ms_ / current_frames_) <<
glaznevfd6706a2016-02-05 14:05:08 -0800852 ". DelayTime: " << (current_delay_time_ms_ / current_frames_) <<
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700853 " for last " << statistic_time_ms << " ms.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000854 start_time_ms_ = GetCurrentTimeMs();
855 current_frames_ = 0;
856 current_bytes_ = 0;
857 current_decoding_time_ms_ = 0;
glaznevfd6706a2016-02-05 14:05:08 -0800858 current_delay_time_ms_ = 0;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000859 }
860
Per488e75f2015-11-19 10:43:36 +0100861 // |.IsZeroSize())| returns true when a frame has been dropped.
862 if (!decoded_frame.IsZeroSize()) {
863 // Callback - output decoded frame.
864 const int32_t callback_status =
865 callback_->Decoded(decoded_frame, decode_time_ms);
866 if (callback_status > 0) {
867 ALOGE << "callback error";
868 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000869 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000870 return true;
871}
872
873int32_t MediaCodecVideoDecoder::RegisterDecodeCompleteCallback(
874 DecodedImageCallback* callback) {
875 callback_ = callback;
876 return WEBRTC_VIDEO_CODEC_OK;
877}
878
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000879void MediaCodecVideoDecoder::OnMessage(rtc::Message* msg) {
880 JNIEnv* jni = AttachCurrentThreadIfNeeded();
881 ScopedLocalRefFrame local_ref_frame(jni);
882 if (!inited_) {
883 return;
884 }
885 // We only ever send one message to |this| directly (not through a Bind()'d
886 // functor), so expect no ID/data.
henrikg91d6ede2015-09-17 00:24:34 -0700887 RTC_CHECK(!msg->message_id) << "Unexpected message!";
888 RTC_CHECK(!msg->pdata) << "Unexpected message!";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000889 CheckOnCodecThread();
890
891 if (!DeliverPendingOutputs(jni, 0)) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700892 ALOGE << "OnMessage: DeliverPendingOutputs error";
Alex Glaznev782671f2015-06-12 16:40:44 -0700893 ProcessHWErrorOnCodecThread();
894 return;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000895 }
896 codec_thread_->PostDelayed(kMediaCodecPollMs, this);
897}
898
perkj461121c2016-02-15 06:28:36 -0800899MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory()
900 : egl_context_(nullptr) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700901 ALOGD << "MediaCodecVideoDecoderFactory ctor";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000902 JNIEnv* jni = AttachCurrentThreadIfNeeded();
903 ScopedLocalRefFrame local_ref_frame(jni);
904 jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder");
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000905 supported_codec_types_.clear();
906
907 bool is_vp8_hw_supported = jni->CallStaticBooleanMethod(
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000908 j_decoder_class,
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000909 GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z"));
Alex Glaznev782671f2015-06-12 16:40:44 -0700910 if (CheckException(jni)) {
911 is_vp8_hw_supported = false;
912 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000913 if (is_vp8_hw_supported) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700914 ALOGD << "VP8 HW Decoder supported.";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000915 supported_codec_types_.push_back(kVideoCodecVP8);
916 }
917
Alex Glaznev69a7fd52015-11-10 10:25:40 -0800918 bool is_vp9_hw_supported = jni->CallStaticBooleanMethod(
919 j_decoder_class,
920 GetStaticMethodID(jni, j_decoder_class, "isVp9HwSupported", "()Z"));
921 if (CheckException(jni)) {
922 is_vp9_hw_supported = false;
923 }
924 if (is_vp9_hw_supported) {
925 ALOGD << "VP9 HW Decoder supported.";
926 supported_codec_types_.push_back(kVideoCodecVP9);
927 }
928
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000929 bool is_h264_hw_supported = jni->CallStaticBooleanMethod(
930 j_decoder_class,
931 GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z"));
Alex Glaznev782671f2015-06-12 16:40:44 -0700932 if (CheckException(jni)) {
933 is_h264_hw_supported = false;
934 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000935 if (is_h264_hw_supported) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700936 ALOGD << "H264 HW Decoder supported.";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000937 supported_codec_types_.push_back(kVideoCodecH264);
938 }
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000939}
940
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700941MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700942 ALOGD << "MediaCodecVideoDecoderFactory dtor";
perkj461121c2016-02-15 06:28:36 -0800943 if (egl_context_) {
944 JNIEnv* jni = AttachCurrentThreadIfNeeded();
945 jni->DeleteGlobalRef(egl_context_);
946 }
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700947}
948
949void MediaCodecVideoDecoderFactory::SetEGLContext(
perkj461121c2016-02-15 06:28:36 -0800950 JNIEnv* jni, jobject egl_context) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700951 ALOGD << "MediaCodecVideoDecoderFactory::SetEGLContext";
Perfd22e6c2016-02-18 11:35:48 +0100952 if (egl_context_) {
953 jni->DeleteGlobalRef(egl_context_);
954 egl_context_ = nullptr;
955 }
perkj461121c2016-02-15 06:28:36 -0800956 egl_context_ = jni->NewGlobalRef(egl_context);
957 if (CheckException(jni)) {
958 ALOGE << "error calling NewGlobalRef for EGL Context.";
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700959 }
960}
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000961
962webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder(
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000963 VideoCodecType type) {
964 if (supported_codec_types_.empty()) {
Alex Glaznevad948c42015-11-18 13:06:42 -0800965 ALOGW << "No HW video decoder for type " << (int)type;
Perec2922f2016-01-27 15:25:46 +0100966 return nullptr;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000967 }
Alex Glaznev4d2f4d12015-09-01 15:04:13 -0700968 for (VideoCodecType codec_type : supported_codec_types_) {
969 if (codec_type == type) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700970 ALOGD << "Create HW video decoder for type " << (int)type;
Perec2922f2016-01-27 15:25:46 +0100971 return new MediaCodecVideoDecoder(AttachCurrentThreadIfNeeded(), type,
perkj461121c2016-02-15 06:28:36 -0800972 egl_context_);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000973 }
974 }
Alex Glaznevad948c42015-11-18 13:06:42 -0800975 ALOGW << "Can not find HW video decoder for type " << (int)type;
Perec2922f2016-01-27 15:25:46 +0100976 return nullptr;
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000977}
978
979void MediaCodecVideoDecoderFactory::DestroyVideoDecoder(
980 webrtc::VideoDecoder* decoder) {
Alex Glaznevfddf6e52015-10-07 16:51:02 -0700981 ALOGD << "Destroy video decoder.";
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000982 delete decoder;
983}
984
Peter Boströmb7d9a972015-12-18 16:01:11 +0100985const char* MediaCodecVideoDecoder::ImplementationName() const {
986 return "MediaCodec";
987}
988
glaznev@webrtc.org18c92472015-02-18 18:42:55 +0000989} // namespace webrtc_jni
990