glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
magjed | 6d387c0 | 2015-10-14 04:02:01 -0700 | [diff] [blame] | 11 | #include <algorithm> |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 14 | // NOTICE: androidmediadecoder_jni.h must be included before |
| 15 | // androidmediacodeccommon.h to avoid build errors. |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 16 | #include "webrtc/api/java/jni/androidmediadecoder_jni.h" |
| 17 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 18 | #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 Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 21 | #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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 25 | #include "webrtc/base/bind.h" |
| 26 | #include "webrtc/base/checks.h" |
| 27 | #include "webrtc/base/logging.h" |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 28 | #include "webrtc/base/scoped_ref_ptr.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 29 | #include "webrtc/base/thread.h" |
Magnus Jedvert | 7e31937 | 2015-10-02 15:49:38 +0200 | [diff] [blame] | 30 | #include "webrtc/base/timeutils.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 31 | #include "webrtc/common_video/include/i420_buffer_pool.h" |
perkj | 87d5845 | 2015-11-23 01:46:27 -0800 | [diff] [blame] | 32 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 33 | #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
| 34 | #include "webrtc/system_wrappers/include/tick_util.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 35 | |
| 36 | using rtc::Bind; |
| 37 | using rtc::Thread; |
| 38 | using rtc::ThreadManager; |
| 39 | using rtc::scoped_ptr; |
| 40 | |
| 41 | using webrtc::CodecSpecificInfo; |
| 42 | using webrtc::DecodedImageCallback; |
| 43 | using webrtc::EncodedImage; |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 44 | using webrtc::VideoFrame; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 45 | using webrtc::RTPFragmentationHeader; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 46 | using webrtc::TickTime; |
| 47 | using webrtc::VideoCodec; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 48 | using webrtc::VideoCodecType; |
| 49 | using webrtc::kVideoCodecH264; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 50 | using webrtc::kVideoCodecVP8; |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 51 | using webrtc::kVideoCodecVP9; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 52 | |
| 53 | namespace webrtc_jni { |
| 54 | |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 55 | // 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 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 67 | enum { kMaxWarningLogFrames = 2 }; |
| 68 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 69 | class MediaCodecVideoDecoder : public webrtc::VideoDecoder, |
| 70 | public rtc::MessageHandler { |
| 71 | public: |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 72 | explicit MediaCodecVideoDecoder( |
| 73 | JNIEnv* jni, VideoCodecType codecType, jobject render_egl_context); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 74 | virtual ~MediaCodecVideoDecoder(); |
| 75 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 76 | 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 | |
perkj | 796cfaf | 2015-12-10 09:27:38 -0800 | [diff] [blame] | 90 | bool PrefersLateDecoding() const override { return true; } |
| 91 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 92 | // rtc::MessageHandler implementation. |
| 93 | void OnMessage(rtc::Message* msg) override; |
| 94 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 95 | const char* ImplementationName() const override; |
| 96 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 97 | private: |
| 98 | // CHECK-fail if not running on |codec_thread_|. |
| 99 | void CheckOnCodecThread(); |
| 100 | |
| 101 | int32_t InitDecodeOnCodecThread(); |
| 102 | int32_t ReleaseOnCodecThread(); |
| 103 | int32_t DecodeOnCodecThread(const EncodedImage& inputImage); |
| 104 | // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
| 105 | // true on success. |
| 106 | bool DeliverPendingOutputs(JNIEnv* jni, int dequeue_timeout_us); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 107 | int32_t ProcessHWErrorOnCodecThread(); |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 108 | void EnableFrameLogOnWarning(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 109 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 110 | // Type of video codec. |
| 111 | VideoCodecType codecType_; |
| 112 | |
kjellander | 60ca31b | 2016-01-04 10:15:53 -0800 | [diff] [blame] | 113 | // Render EGL context - owned by factory, should not be allocated/destroyed |
| 114 | // by VideoDecoder. |
| 115 | jobject render_egl_context_; |
| 116 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 117 | bool key_frame_required_; |
| 118 | bool inited_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 119 | bool sw_fallback_required_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 120 | bool use_surface_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 121 | VideoCodec codec_; |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 122 | webrtc::I420BufferPool decoded_frame_pool_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 123 | rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 124 | DecodedImageCallback* callback_; |
| 125 | int frames_received_; // Number of frames received by decoder. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 126 | int frames_decoded_; // Number of frames decoded by decoder. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 127 | // Number of decoded frames for which log information is displayed. |
| 128 | int frames_decoded_logged_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 129 | int64_t start_time_ms_; // Start time for statistics. |
| 130 | int current_frames_; // Number of frames in the current statistics interval. |
| 131 | int current_bytes_; // Encoded bytes in the current statistics interval. |
| 132 | int current_decoding_time_ms_; // Overall decoding time in the current second |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 133 | int current_delay_time_ms_; // Overall delay time in the current second. |
| 134 | uint32_t max_pending_frames_; // Maximum number of pending input frames. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 135 | |
| 136 | // State that is constant for the lifetime of this object once the ctor |
| 137 | // returns. |
| 138 | scoped_ptr<Thread> codec_thread_; // Thread on which to operate MediaCodec. |
| 139 | ScopedGlobalRef<jclass> j_media_codec_video_decoder_class_; |
| 140 | ScopedGlobalRef<jobject> j_media_codec_video_decoder_; |
| 141 | jmethodID j_init_decode_method_; |
| 142 | jmethodID j_release_method_; |
| 143 | jmethodID j_dequeue_input_buffer_method_; |
| 144 | jmethodID j_queue_input_buffer_method_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 145 | jmethodID j_dequeue_byte_buffer_method_; |
| 146 | jmethodID j_dequeue_texture_buffer_method_; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 147 | jmethodID j_return_decoded_byte_buffer_method_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 148 | // MediaCodecVideoDecoder fields. |
| 149 | jfieldID j_input_buffers_field_; |
| 150 | jfieldID j_output_buffers_field_; |
| 151 | jfieldID j_color_format_field_; |
| 152 | jfieldID j_width_field_; |
| 153 | jfieldID j_height_field_; |
| 154 | jfieldID j_stride_field_; |
| 155 | jfieldID j_slice_height_field_; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 156 | // MediaCodecVideoDecoder.DecodedTextureBuffer fields. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 157 | jfieldID j_texture_id_field_; |
| 158 | jfieldID j_transform_matrix_field_; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 159 | jfieldID j_texture_presentation_timestamp_ms_field_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 160 | jfieldID j_texture_timestamp_ms_field_; |
| 161 | jfieldID j_texture_ntp_timestamp_ms_field_; |
| 162 | jfieldID j_texture_decode_time_ms_field_; |
| 163 | jfieldID j_texture_frame_delay_ms_field_; |
| 164 | // MediaCodecVideoDecoder.DecodedOutputBuffer fields. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 165 | jfieldID j_info_index_field_; |
| 166 | jfieldID j_info_offset_field_; |
| 167 | jfieldID j_info_size_field_; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 168 | jfieldID j_presentation_timestamp_ms_field_; |
| 169 | jfieldID j_timestamp_ms_field_; |
| 170 | jfieldID j_ntp_timestamp_ms_field_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 171 | jfieldID j_byte_buffer_decode_time_ms_field_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 172 | |
| 173 | // Global references; must be deleted in Release(). |
| 174 | std::vector<jobject> input_buffers_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 177 | MediaCodecVideoDecoder::MediaCodecVideoDecoder( |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 178 | JNIEnv* jni, VideoCodecType codecType, jobject render_egl_context) : |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 179 | codecType_(codecType), |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 180 | render_egl_context_(render_egl_context), |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 181 | key_frame_required_(true), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 182 | inited_(false), |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 183 | sw_fallback_required_(false), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 184 | codec_thread_(new Thread()), |
| 185 | j_media_codec_video_decoder_class_( |
| 186 | jni, |
| 187 | FindClass(jni, "org/webrtc/MediaCodecVideoDecoder")), |
| 188 | j_media_codec_video_decoder_( |
| 189 | jni, |
| 190 | jni->NewObject(*j_media_codec_video_decoder_class_, |
| 191 | GetMethodID(jni, |
| 192 | *j_media_codec_video_decoder_class_, |
| 193 | "<init>", |
| 194 | "()V"))) { |
| 195 | ScopedLocalRefFrame local_ref_frame(jni); |
| 196 | codec_thread_->SetName("MediaCodecVideoDecoder", NULL); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 197 | RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoDecoder"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 198 | |
| 199 | j_init_decode_method_ = GetMethodID( |
| 200 | jni, *j_media_codec_video_decoder_class_, "initDecode", |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 201 | "(Lorg/webrtc/MediaCodecVideoDecoder$VideoCodecType;" |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 202 | "IILorg/webrtc/SurfaceTextureHelper;)Z"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 203 | j_release_method_ = |
| 204 | GetMethodID(jni, *j_media_codec_video_decoder_class_, "release", "()V"); |
| 205 | j_dequeue_input_buffer_method_ = GetMethodID( |
| 206 | jni, *j_media_codec_video_decoder_class_, "dequeueInputBuffer", "()I"); |
| 207 | j_queue_input_buffer_method_ = GetMethodID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 208 | jni, *j_media_codec_video_decoder_class_, "queueInputBuffer", "(IIJJJ)Z"); |
| 209 | j_dequeue_byte_buffer_method_ = GetMethodID( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 210 | jni, *j_media_codec_video_decoder_class_, "dequeueOutputBuffer", |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 211 | "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer;"); |
| 212 | j_dequeue_texture_buffer_method_ = GetMethodID( |
| 213 | jni, *j_media_codec_video_decoder_class_, "dequeueTextureBuffer", |
| 214 | "(I)Lorg/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer;"); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 215 | j_return_decoded_byte_buffer_method_ = |
| 216 | GetMethodID(jni, *j_media_codec_video_decoder_class_, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 217 | "returnDecodedOutputBuffer", "(I)V"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 218 | |
| 219 | j_input_buffers_field_ = GetFieldID( |
| 220 | jni, *j_media_codec_video_decoder_class_, |
| 221 | "inputBuffers", "[Ljava/nio/ByteBuffer;"); |
| 222 | j_output_buffers_field_ = GetFieldID( |
| 223 | jni, *j_media_codec_video_decoder_class_, |
| 224 | "outputBuffers", "[Ljava/nio/ByteBuffer;"); |
| 225 | j_color_format_field_ = GetFieldID( |
| 226 | jni, *j_media_codec_video_decoder_class_, "colorFormat", "I"); |
| 227 | j_width_field_ = GetFieldID( |
| 228 | jni, *j_media_codec_video_decoder_class_, "width", "I"); |
| 229 | j_height_field_ = GetFieldID( |
| 230 | jni, *j_media_codec_video_decoder_class_, "height", "I"); |
| 231 | j_stride_field_ = GetFieldID( |
| 232 | jni, *j_media_codec_video_decoder_class_, "stride", "I"); |
| 233 | j_slice_height_field_ = GetFieldID( |
| 234 | jni, *j_media_codec_video_decoder_class_, "sliceHeight", "I"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 235 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 236 | jclass j_decoded_texture_buffer_class = FindClass(jni, |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 237 | "org/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer"); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 238 | j_texture_id_field_ = GetFieldID( |
| 239 | jni, j_decoded_texture_buffer_class, "textureID", "I"); |
| 240 | j_transform_matrix_field_ = GetFieldID( |
| 241 | jni, j_decoded_texture_buffer_class, "transformMatrix", "[F"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 242 | j_texture_presentation_timestamp_ms_field_ = GetFieldID( |
| 243 | jni, j_decoded_texture_buffer_class, "presentationTimeStampMs", "J"); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 244 | j_texture_timestamp_ms_field_ = GetFieldID( |
| 245 | jni, j_decoded_texture_buffer_class, "timeStampMs", "J"); |
| 246 | j_texture_ntp_timestamp_ms_field_ = GetFieldID( |
| 247 | jni, j_decoded_texture_buffer_class, "ntpTimeStampMs", "J"); |
| 248 | j_texture_decode_time_ms_field_ = GetFieldID( |
| 249 | jni, j_decoded_texture_buffer_class, "decodeTimeMs", "J"); |
| 250 | j_texture_frame_delay_ms_field_ = GetFieldID( |
| 251 | jni, j_decoded_texture_buffer_class, "frameDelayMs", "J"); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 252 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 253 | jclass j_decoded_output_buffer_class = FindClass(jni, |
| 254 | "org/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 255 | j_info_index_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 256 | jni, j_decoded_output_buffer_class, "index", "I"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 257 | j_info_offset_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 258 | jni, j_decoded_output_buffer_class, "offset", "I"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 259 | j_info_size_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 260 | jni, j_decoded_output_buffer_class, "size", "I"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 261 | j_presentation_timestamp_ms_field_ = GetFieldID( |
| 262 | jni, j_decoded_output_buffer_class, "presentationTimeStampMs", "J"); |
| 263 | j_timestamp_ms_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 264 | jni, j_decoded_output_buffer_class, "timeStampMs", "J"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 265 | j_ntp_timestamp_ms_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 266 | jni, j_decoded_output_buffer_class, "ntpTimeStampMs", "J"); |
| 267 | j_byte_buffer_decode_time_ms_field_ = GetFieldID( |
| 268 | jni, j_decoded_output_buffer_class, "decodeTimeMs", "J"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 269 | |
| 270 | CHECK_EXCEPTION(jni) << "MediaCodecVideoDecoder ctor failed"; |
Magnus Jedvert | 207370f | 2015-09-16 12:32:21 +0200 | [diff] [blame] | 271 | use_surface_ = (render_egl_context_ != NULL); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 272 | ALOGD << "MediaCodecVideoDecoder ctor. Use surface: " << use_surface_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 273 | memset(&codec_, 0, sizeof(codec_)); |
| 274 | AllowBlockingCalls(); |
| 275 | } |
| 276 | |
| 277 | MediaCodecVideoDecoder::~MediaCodecVideoDecoder() { |
| 278 | // Call Release() to ensure no more callbacks to us after we are deleted. |
| 279 | Release(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* inst, |
| 283 | int32_t numberOfCores) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 284 | ALOGD << "InitDecode."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 285 | if (inst == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 286 | ALOGE << "NULL VideoCodec instance"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 287 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 288 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 289 | // Factory should guard against other codecs being used with us. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 290 | RTC_CHECK(inst->codecType == codecType_) |
| 291 | << "Unsupported codec " << inst->codecType << " for " << codecType_; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 292 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 293 | if (sw_fallback_required_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 294 | ALOGE << "InitDecode() - fallback to SW decoder"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 295 | return WEBRTC_VIDEO_CODEC_OK; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 296 | } |
| 297 | // Save VideoCodec instance for later. |
| 298 | if (&codec_ != inst) { |
| 299 | codec_ = *inst; |
| 300 | } |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 301 | // If maxFramerate is not set then assume 30 fps. |
| 302 | codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 30; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 303 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 304 | // Call Java init. |
| 305 | return codec_thread_->Invoke<int32_t>( |
| 306 | Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this)); |
| 307 | } |
| 308 | |
| 309 | int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() { |
| 310 | CheckOnCodecThread(); |
| 311 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 312 | ScopedLocalRefFrame local_ref_frame(jni); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 313 | ALOGD << "InitDecodeOnCodecThread Type: " << (int)codecType_ << ". " |
| 314 | << codec_.width << " x " << codec_.height << ". Fps: " << |
| 315 | (int)codec_.maxFramerate; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 316 | |
| 317 | // Release previous codec first if it was allocated before. |
| 318 | int ret_val = ReleaseOnCodecThread(); |
| 319 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 320 | ALOGE << "Release failure: " << ret_val << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 321 | sw_fallback_required_ = true; |
| 322 | return WEBRTC_VIDEO_CODEC_ERROR; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 325 | // Always start with a complete key frame. |
| 326 | key_frame_required_ = true; |
| 327 | frames_received_ = 0; |
| 328 | frames_decoded_ = 0; |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 329 | frames_decoded_logged_ = kMaxDecodedLogFrames; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 330 | |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 331 | jobject java_surface_texture_helper_ = nullptr; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 332 | if (use_surface_) { |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 333 | java_surface_texture_helper_ = jni->CallStaticObjectMethod( |
| 334 | FindClass(jni, "org/webrtc/SurfaceTextureHelper"), |
| 335 | GetStaticMethodID(jni, |
| 336 | FindClass(jni, "org/webrtc/SurfaceTextureHelper"), |
| 337 | "create", |
| 338 | "(Lorg/webrtc/EglBase$Context;)" |
| 339 | "Lorg/webrtc/SurfaceTextureHelper;"), |
| 340 | render_egl_context_); |
| 341 | RTC_CHECK(java_surface_texture_helper_ != nullptr); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 342 | surface_texture_helper_ = new rtc::RefCountedObject<SurfaceTextureHelper>( |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 343 | jni, java_surface_texture_helper_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 346 | jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 347 | jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 348 | bool success = jni->CallBooleanMethod( |
| 349 | *j_media_codec_video_decoder_, |
| 350 | j_init_decode_method_, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 351 | j_video_codec_enum, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 352 | codec_.width, |
| 353 | codec_.height, |
perkj | 88518a2 | 2015-12-18 00:37:06 -0800 | [diff] [blame] | 354 | java_surface_texture_helper_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 355 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 356 | ALOGE << "Codec initialization error - fallback to SW codec."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 357 | sw_fallback_required_ = true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 358 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 359 | } |
| 360 | inited_ = true; |
| 361 | |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 362 | switch (codecType_) { |
| 363 | case kVideoCodecVP8: |
| 364 | max_pending_frames_ = kMaxPendingFramesVp8; |
| 365 | break; |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 366 | case kVideoCodecVP9: |
| 367 | max_pending_frames_ = kMaxPendingFramesVp9; |
| 368 | break; |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 369 | case kVideoCodecH264: |
| 370 | max_pending_frames_ = kMaxPendingFramesH264; |
| 371 | break; |
| 372 | default: |
| 373 | max_pending_frames_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 374 | } |
| 375 | start_time_ms_ = GetCurrentTimeMs(); |
| 376 | current_frames_ = 0; |
| 377 | current_bytes_ = 0; |
| 378 | current_decoding_time_ms_ = 0; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 379 | current_delay_time_ms_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 380 | |
| 381 | jobjectArray input_buffers = (jobjectArray)GetObjectField( |
| 382 | jni, *j_media_codec_video_decoder_, j_input_buffers_field_); |
| 383 | size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 384 | ALOGD << "Maximum amount of pending frames: " << max_pending_frames_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 385 | input_buffers_.resize(num_input_buffers); |
| 386 | for (size_t i = 0; i < num_input_buffers; ++i) { |
| 387 | input_buffers_[i] = |
| 388 | jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 389 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 390 | ALOGE << "NewGlobalRef error - fallback to SW codec."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 391 | sw_fallback_required_ = true; |
| 392 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 393 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 396 | codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
| 397 | |
| 398 | return WEBRTC_VIDEO_CODEC_OK; |
| 399 | } |
| 400 | |
| 401 | int32_t MediaCodecVideoDecoder::Release() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 402 | ALOGD << "DecoderRelease request"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 403 | return codec_thread_->Invoke<int32_t>( |
| 404 | Bind(&MediaCodecVideoDecoder::ReleaseOnCodecThread, this)); |
| 405 | } |
| 406 | |
| 407 | int32_t MediaCodecVideoDecoder::ReleaseOnCodecThread() { |
| 408 | if (!inited_) { |
| 409 | return WEBRTC_VIDEO_CODEC_OK; |
| 410 | } |
| 411 | CheckOnCodecThread(); |
| 412 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 413 | ALOGD << "DecoderReleaseOnCodecThread: Frames received: " << |
| 414 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 415 | ScopedLocalRefFrame local_ref_frame(jni); |
| 416 | for (size_t i = 0; i < input_buffers_.size(); i++) { |
| 417 | jni->DeleteGlobalRef(input_buffers_[i]); |
| 418 | } |
| 419 | input_buffers_.clear(); |
| 420 | jni->CallVoidMethod(*j_media_codec_video_decoder_, j_release_method_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 421 | surface_texture_helper_ = nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 422 | inited_ = false; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 423 | rtc::MessageQueueManager::Clear(this); |
| 424 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 425 | ALOGE << "Decoder release exception"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 426 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 427 | } |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 428 | ALOGD << "DecoderReleaseOnCodecThread done"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 429 | return WEBRTC_VIDEO_CODEC_OK; |
| 430 | } |
| 431 | |
| 432 | void MediaCodecVideoDecoder::CheckOnCodecThread() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 433 | RTC_CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 434 | << "Running on wrong thread!"; |
| 435 | } |
| 436 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 437 | void MediaCodecVideoDecoder::EnableFrameLogOnWarning() { |
| 438 | // Log next 2 output frames. |
| 439 | frames_decoded_logged_ = std::max( |
| 440 | frames_decoded_logged_, frames_decoded_ + kMaxWarningLogFrames); |
| 441 | } |
| 442 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 443 | int32_t MediaCodecVideoDecoder::ProcessHWErrorOnCodecThread() { |
| 444 | CheckOnCodecThread(); |
| 445 | int ret_val = ReleaseOnCodecThread(); |
| 446 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 447 | ALOGE << "ProcessHWError: Release failure"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 448 | } |
| 449 | if (codecType_ == kVideoCodecH264) { |
| 450 | // For now there is no SW H.264 which can be used as fallback codec. |
| 451 | // So try to restart hw codec for now. |
| 452 | ret_val = InitDecodeOnCodecThread(); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 453 | ALOGE << "Reset H.264 codec done. Status: " << ret_val; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 454 | if (ret_val == WEBRTC_VIDEO_CODEC_OK) { |
| 455 | // H.264 codec was succesfully reset - return regular error code. |
| 456 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 457 | } else { |
| 458 | // Fail to restart H.264 codec - return error code which should stop the |
| 459 | // call. |
| 460 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 461 | } |
| 462 | } else { |
| 463 | sw_fallback_required_ = true; |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 464 | ALOGE << "Return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 465 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 466 | } |
| 467 | } |
| 468 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 469 | int32_t MediaCodecVideoDecoder::Decode( |
| 470 | const EncodedImage& inputImage, |
| 471 | bool missingFrames, |
| 472 | const RTPFragmentationHeader* fragmentation, |
| 473 | const CodecSpecificInfo* codecSpecificInfo, |
| 474 | int64_t renderTimeMs) { |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 475 | if (sw_fallback_required_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 476 | ALOGE << "Decode() - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 477 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 478 | } |
| 479 | if (callback_ == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 480 | ALOGE << "Decode() - callback_ is NULL"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 481 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 482 | } |
| 483 | if (inputImage._buffer == NULL && inputImage._length > 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 484 | ALOGE << "Decode() - inputImage is incorrect"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 485 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 486 | } |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 487 | if (!inited_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 488 | ALOGE << "Decode() - decoder is not initialized"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 489 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 490 | } |
| 491 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 492 | // Check if encoded frame dimension has changed. |
| 493 | if ((inputImage._encodedWidth * inputImage._encodedHeight > 0) && |
| 494 | (inputImage._encodedWidth != codec_.width || |
| 495 | inputImage._encodedHeight != codec_.height)) { |
| 496 | codec_.width = inputImage._encodedWidth; |
| 497 | codec_.height = inputImage._encodedHeight; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 498 | int32_t ret = InitDecode(&codec_, 1); |
| 499 | if (ret < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 500 | ALOGE << "InitDecode failure: " << ret << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 501 | sw_fallback_required_ = true; |
| 502 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 503 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // Always start with a complete key frame. |
| 507 | if (key_frame_required_) { |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 508 | if (inputImage._frameType != webrtc::kVideoFrameKey) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 509 | ALOGE << "Decode() - key frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 510 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 511 | } |
| 512 | if (!inputImage._completeFrame) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 513 | ALOGE << "Decode() - complete frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 514 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 515 | } |
| 516 | key_frame_required_ = false; |
| 517 | } |
| 518 | if (inputImage._length == 0) { |
| 519 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 520 | } |
| 521 | |
| 522 | return codec_thread_->Invoke<int32_t>(Bind( |
| 523 | &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage)); |
| 524 | } |
| 525 | |
| 526 | int32_t MediaCodecVideoDecoder::DecodeOnCodecThread( |
| 527 | const EncodedImage& inputImage) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 528 | CheckOnCodecThread(); |
| 529 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 530 | ScopedLocalRefFrame local_ref_frame(jni); |
| 531 | |
| 532 | // Try to drain the decoder and wait until output is not too |
| 533 | // much behind the input. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 534 | if (frames_received_ > frames_decoded_ + max_pending_frames_) { |
| 535 | ALOGW << "Decoder is too far behind. Try to drain. Received: " << |
| 536 | frames_received_ << ". Decoded: " << frames_decoded_; |
| 537 | EnableFrameLogOnWarning(); |
| 538 | } |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 539 | const int64 drain_start = GetCurrentTimeMs(); |
| 540 | while ((frames_received_ > frames_decoded_ + max_pending_frames_) && |
| 541 | (GetCurrentTimeMs() - drain_start) < kMediaCodecTimeoutMs) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 542 | if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) { |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 543 | ALOGE << "DeliverPendingOutputs error. Frames received: " << |
| 544 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 545 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 546 | } |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 547 | } |
| 548 | if (frames_received_ > frames_decoded_ + max_pending_frames_) { |
| 549 | ALOGE << "Output buffer dequeue timeout. Frames received: " << |
| 550 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
| 551 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Get input buffer. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 555 | int j_input_buffer_index = jni->CallIntMethod( |
| 556 | *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 557 | if (CheckException(jni) || j_input_buffer_index < 0) { |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 558 | ALOGE << "dequeueInputBuffer error: " << j_input_buffer_index << |
| 559 | ". Retry DeliverPendingOutputs."; |
| 560 | EnableFrameLogOnWarning(); |
| 561 | // Try to drain the decoder. |
| 562 | if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) { |
| 563 | ALOGE << "DeliverPendingOutputs error. Frames received: " << |
| 564 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
| 565 | return ProcessHWErrorOnCodecThread(); |
| 566 | } |
| 567 | // Try dequeue input buffer one last time. |
| 568 | j_input_buffer_index = jni->CallIntMethod( |
| 569 | *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_); |
| 570 | if (CheckException(jni) || j_input_buffer_index < 0) { |
| 571 | ALOGE << "dequeueInputBuffer critical error: " << j_input_buffer_index; |
| 572 | return ProcessHWErrorOnCodecThread(); |
| 573 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // Copy encoded data to Java ByteBuffer. |
| 577 | jobject j_input_buffer = input_buffers_[j_input_buffer_index]; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 578 | uint8_t* buffer = |
| 579 | reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 580 | RTC_CHECK(buffer) << "Indirect buffer??"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 581 | int64_t buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 582 | if (CheckException(jni) || buffer_capacity < inputImage._length) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 583 | ALOGE << "Input frame size "<< inputImage._length << |
| 584 | " is bigger than buffer size " << buffer_capacity; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 585 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 586 | } |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 587 | jlong presentation_timestamp_us = static_cast<jlong>( |
| 588 | static_cast<int64_t>(frames_received_) * 1000000 / codec_.maxFramerate); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 589 | memcpy(buffer, inputImage._buffer, inputImage._length); |
| 590 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 591 | if (frames_decoded_ < frames_decoded_logged_) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 592 | ALOGD << "Decoder frame in # " << frames_received_ << |
| 593 | ". Type: " << inputImage._frameType << |
| 594 | ". Buffer # " << j_input_buffer_index << |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 595 | ". TS: " << presentation_timestamp_us / 1000 << |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 596 | ". Size: " << inputImage._length; |
| 597 | } |
| 598 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 599 | // Save input image timestamps for later output. |
| 600 | frames_received_++; |
| 601 | current_bytes_ += inputImage._length; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 602 | |
| 603 | // Feed input to decoder. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 604 | bool success = jni->CallBooleanMethod( |
| 605 | *j_media_codec_video_decoder_, |
| 606 | j_queue_input_buffer_method_, |
| 607 | j_input_buffer_index, |
| 608 | inputImage._length, |
| 609 | presentation_timestamp_us, |
| 610 | static_cast<int64_t> (inputImage._timeStamp), |
| 611 | inputImage.ntp_time_ms_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 612 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 613 | ALOGE << "queueInputBuffer error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 614 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Try to drain the decoder |
| 618 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 619 | ALOGE << "DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 620 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | return WEBRTC_VIDEO_CODEC_OK; |
| 624 | } |
| 625 | |
| 626 | bool MediaCodecVideoDecoder::DeliverPendingOutputs( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 627 | JNIEnv* jni, int dequeue_timeout_ms) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 628 | if (frames_received_ <= frames_decoded_) { |
| 629 | // No need to query for output buffers - decoder is drained. |
| 630 | return true; |
| 631 | } |
| 632 | // Get decoder output. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 633 | jobject j_decoder_output_buffer = |
| 634 | jni->CallObjectMethod(*j_media_codec_video_decoder_, |
| 635 | use_surface_ ? j_dequeue_texture_buffer_method_ |
| 636 | : j_dequeue_byte_buffer_method_, |
| 637 | dequeue_timeout_ms); |
| 638 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 639 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 640 | ALOGE << "dequeueOutputBuffer() error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 641 | return false; |
| 642 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 643 | if (IsNull(jni, j_decoder_output_buffer)) { |
| 644 | // No decoded frame ready. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 645 | return true; |
| 646 | } |
| 647 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 648 | // Get decoded video frame properties. |
| 649 | int color_format = GetIntField(jni, *j_media_codec_video_decoder_, |
| 650 | j_color_format_field_); |
| 651 | int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_); |
| 652 | int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_); |
| 653 | int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_); |
| 654 | int slice_height = GetIntField(jni, *j_media_codec_video_decoder_, |
| 655 | j_slice_height_field_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 656 | |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 657 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> frame_buffer; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 658 | int64_t presentation_timestamps_ms = 0; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 659 | int64_t output_timestamps_ms = 0; |
| 660 | int64_t output_ntp_timestamps_ms = 0; |
| 661 | int decode_time_ms = 0; |
| 662 | int64_t frame_delayed_ms = 0; |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 663 | if (use_surface_) { |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 664 | // Extract data from Java DecodedTextureBuffer. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 665 | presentation_timestamps_ms = GetLongField( |
| 666 | jni, j_decoder_output_buffer, |
| 667 | j_texture_presentation_timestamp_ms_field_); |
| 668 | output_timestamps_ms = GetLongField( |
| 669 | jni, j_decoder_output_buffer, j_texture_timestamp_ms_field_); |
| 670 | output_ntp_timestamps_ms = GetLongField( |
| 671 | jni, j_decoder_output_buffer, j_texture_ntp_timestamp_ms_field_); |
| 672 | decode_time_ms = GetLongField( |
| 673 | jni, j_decoder_output_buffer, j_texture_decode_time_ms_field_); |
| 674 | |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 675 | const int texture_id = |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 676 | GetIntField(jni, j_decoder_output_buffer, j_texture_id_field_); |
| 677 | if (texture_id != 0) { // |texture_id| == 0 represents a dropped frame. |
| 678 | const jfloatArray j_transform_matrix = |
| 679 | reinterpret_cast<jfloatArray>(GetObjectField( |
| 680 | jni, j_decoder_output_buffer, j_transform_matrix_field_)); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 681 | frame_delayed_ms = GetLongField( |
| 682 | jni, j_decoder_output_buffer, j_texture_frame_delay_ms_field_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 683 | |
| 684 | // Create webrtc::VideoFrameBuffer with native texture handle. |
| 685 | frame_buffer = surface_texture_helper_->CreateTextureFrame( |
| 686 | width, height, NativeHandleImpl(jni, texture_id, j_transform_matrix)); |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 687 | } else { |
| 688 | EnableFrameLogOnWarning(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 689 | } |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 690 | } else { |
| 691 | // Extract data from Java ByteBuffer and create output yuv420 frame - |
| 692 | // for non surface decoding only. |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 693 | const int output_buffer_index = GetIntField( |
| 694 | jni, j_decoder_output_buffer, j_info_index_field_); |
| 695 | const int output_buffer_offset = GetIntField( |
| 696 | jni, j_decoder_output_buffer, j_info_offset_field_); |
| 697 | const int output_buffer_size = GetIntField( |
| 698 | jni, j_decoder_output_buffer, j_info_size_field_); |
| 699 | presentation_timestamps_ms = GetLongField( |
| 700 | jni, j_decoder_output_buffer, j_presentation_timestamp_ms_field_); |
| 701 | output_timestamps_ms = GetLongField( |
| 702 | jni, j_decoder_output_buffer, j_timestamp_ms_field_); |
| 703 | output_ntp_timestamps_ms = GetLongField( |
| 704 | jni, j_decoder_output_buffer, j_ntp_timestamp_ms_field_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 705 | |
| 706 | decode_time_ms = GetLongField(jni, j_decoder_output_buffer, |
| 707 | j_byte_buffer_decode_time_ms_field_); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 708 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 709 | if (output_buffer_size < width * height * 3 / 2) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 710 | ALOGE << "Insufficient output buffer size: " << output_buffer_size; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 711 | return false; |
| 712 | } |
| 713 | jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField( |
| 714 | jni, *j_media_codec_video_decoder_, j_output_buffers_field_)); |
| 715 | jobject output_buffer = |
| 716 | jni->GetObjectArrayElement(output_buffers, output_buffer_index); |
| 717 | uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress( |
| 718 | output_buffer)); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 719 | if (CheckException(jni)) { |
| 720 | return false; |
| 721 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 722 | payload += output_buffer_offset; |
| 723 | |
| 724 | // Create yuv420 frame. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 725 | frame_buffer = decoded_frame_pool_.CreateBuffer(width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 726 | if (color_format == COLOR_FormatYUV420Planar) { |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 727 | RTC_CHECK_EQ(0, stride % 2); |
| 728 | RTC_CHECK_EQ(0, slice_height % 2); |
| 729 | const int uv_stride = stride / 2; |
| 730 | const int u_slice_height = slice_height / 2; |
| 731 | const uint8_t* y_ptr = payload; |
| 732 | const uint8_t* u_ptr = y_ptr + stride * slice_height; |
| 733 | const uint8_t* v_ptr = u_ptr + uv_stride * u_slice_height; |
| 734 | libyuv::I420Copy(y_ptr, stride, |
| 735 | u_ptr, uv_stride, |
| 736 | v_ptr, uv_stride, |
| 737 | frame_buffer->MutableData(webrtc::kYPlane), |
| 738 | frame_buffer->stride(webrtc::kYPlane), |
| 739 | frame_buffer->MutableData(webrtc::kUPlane), |
| 740 | frame_buffer->stride(webrtc::kUPlane), |
| 741 | frame_buffer->MutableData(webrtc::kVPlane), |
| 742 | frame_buffer->stride(webrtc::kVPlane), |
| 743 | width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 744 | } else { |
| 745 | // All other supported formats are nv12. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 746 | const uint8_t* y_ptr = payload; |
| 747 | const uint8_t* uv_ptr = y_ptr + stride * slice_height; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 748 | libyuv::NV12ToI420( |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 749 | y_ptr, stride, |
| 750 | uv_ptr, stride, |
| 751 | frame_buffer->MutableData(webrtc::kYPlane), |
| 752 | frame_buffer->stride(webrtc::kYPlane), |
| 753 | frame_buffer->MutableData(webrtc::kUPlane), |
| 754 | frame_buffer->stride(webrtc::kUPlane), |
| 755 | frame_buffer->MutableData(webrtc::kVPlane), |
| 756 | frame_buffer->stride(webrtc::kVPlane), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 757 | width, height); |
| 758 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 759 | // Return output byte buffer back to codec. |
| 760 | jni->CallVoidMethod( |
| 761 | *j_media_codec_video_decoder_, |
| 762 | j_return_decoded_byte_buffer_method_, |
| 763 | output_buffer_index); |
| 764 | if (CheckException(jni)) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 765 | ALOGE << "returnDecodedOutputBuffer error"; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 766 | return false; |
| 767 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 768 | } |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 769 | VideoFrame decoded_frame(frame_buffer, 0, 0, webrtc::kVideoRotation_0); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 770 | decoded_frame.set_timestamp(output_timestamps_ms); |
| 771 | decoded_frame.set_ntp_time_ms(output_ntp_timestamps_ms); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 772 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 773 | if (frames_decoded_ < frames_decoded_logged_) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 774 | ALOGD << "Decoder frame out # " << frames_decoded_ << |
| 775 | ". " << width << " x " << height << |
| 776 | ". " << stride << " x " << slice_height << |
| 777 | ". Color: " << color_format << |
| 778 | ". TS: " << presentation_timestamps_ms << |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 779 | ". DecTime: " << (int)decode_time_ms << |
| 780 | ". DelayTime: " << (int)frame_delayed_ms; |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 781 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 782 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 783 | // Calculate and print decoding statistics - every 3 seconds. |
| 784 | frames_decoded_++; |
| 785 | current_frames_++; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 786 | current_decoding_time_ms_ += decode_time_ms; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 787 | current_delay_time_ms_ += frame_delayed_ms; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 788 | int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_; |
| 789 | if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs && |
| 790 | current_frames_ > 0) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 791 | int current_bitrate = current_bytes_ * 8 / statistic_time_ms; |
| 792 | int current_fps = |
| 793 | (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 794 | ALOGD << "Frames decoded: " << frames_decoded_ << |
| 795 | ". Received: " << frames_received_ << |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 796 | ". Bitrate: " << current_bitrate << " kbps" << |
| 797 | ". Fps: " << current_fps << |
| 798 | ". DecTime: " << (current_decoding_time_ms_ / current_frames_) << |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 799 | ". DelayTime: " << (current_delay_time_ms_ / current_frames_) << |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 800 | " for last " << statistic_time_ms << " ms."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 801 | start_time_ms_ = GetCurrentTimeMs(); |
| 802 | current_frames_ = 0; |
| 803 | current_bytes_ = 0; |
| 804 | current_decoding_time_ms_ = 0; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 805 | current_delay_time_ms_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 808 | // |.IsZeroSize())| returns true when a frame has been dropped. |
| 809 | if (!decoded_frame.IsZeroSize()) { |
| 810 | // Callback - output decoded frame. |
| 811 | const int32_t callback_status = |
| 812 | callback_->Decoded(decoded_frame, decode_time_ms); |
| 813 | if (callback_status > 0) { |
| 814 | ALOGE << "callback error"; |
| 815 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 816 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 817 | return true; |
| 818 | } |
| 819 | |
| 820 | int32_t MediaCodecVideoDecoder::RegisterDecodeCompleteCallback( |
| 821 | DecodedImageCallback* callback) { |
| 822 | callback_ = callback; |
| 823 | return WEBRTC_VIDEO_CODEC_OK; |
| 824 | } |
| 825 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 826 | void MediaCodecVideoDecoder::OnMessage(rtc::Message* msg) { |
| 827 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 828 | ScopedLocalRefFrame local_ref_frame(jni); |
| 829 | if (!inited_) { |
| 830 | return; |
| 831 | } |
| 832 | // We only ever send one message to |this| directly (not through a Bind()'d |
| 833 | // functor), so expect no ID/data. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 834 | RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
| 835 | RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 836 | CheckOnCodecThread(); |
| 837 | |
| 838 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 839 | ALOGE << "OnMessage: DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 840 | ProcessHWErrorOnCodecThread(); |
| 841 | return; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 842 | } |
| 843 | codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
| 844 | } |
| 845 | |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 846 | MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 847 | ALOGD << "MediaCodecVideoDecoderFactory ctor"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 848 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 849 | ScopedLocalRefFrame local_ref_frame(jni); |
| 850 | jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 851 | supported_codec_types_.clear(); |
| 852 | |
| 853 | bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 854 | j_decoder_class, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 855 | GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 856 | if (CheckException(jni)) { |
| 857 | is_vp8_hw_supported = false; |
| 858 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 859 | if (is_vp8_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 860 | ALOGD << "VP8 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 861 | supported_codec_types_.push_back(kVideoCodecVP8); |
| 862 | } |
| 863 | |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 864 | bool is_vp9_hw_supported = jni->CallStaticBooleanMethod( |
| 865 | j_decoder_class, |
| 866 | GetStaticMethodID(jni, j_decoder_class, "isVp9HwSupported", "()Z")); |
| 867 | if (CheckException(jni)) { |
| 868 | is_vp9_hw_supported = false; |
| 869 | } |
| 870 | if (is_vp9_hw_supported) { |
| 871 | ALOGD << "VP9 HW Decoder supported."; |
| 872 | supported_codec_types_.push_back(kVideoCodecVP9); |
| 873 | } |
| 874 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 875 | bool is_h264_hw_supported = jni->CallStaticBooleanMethod( |
| 876 | j_decoder_class, |
| 877 | GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 878 | if (CheckException(jni)) { |
| 879 | is_h264_hw_supported = false; |
| 880 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 881 | if (is_h264_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 882 | ALOGD << "H264 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 883 | supported_codec_types_.push_back(kVideoCodecH264); |
| 884 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 887 | MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 888 | ALOGD << "MediaCodecVideoDecoderFactory dtor"; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void MediaCodecVideoDecoderFactory::SetEGLContext( |
| 892 | JNIEnv* jni, jobject render_egl_context) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 893 | ALOGD << "MediaCodecVideoDecoderFactory::SetEGLContext"; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 894 | if (!egl_.CreateEglBase(jni, render_egl_context)) { |
| 895 | ALOGW << "Invalid EGL context - HW surface decoding is disabled."; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 898 | |
| 899 | webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 900 | VideoCodecType type) { |
| 901 | if (supported_codec_types_.empty()) { |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 902 | ALOGW << "No HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 903 | return nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 904 | } |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 905 | for (VideoCodecType codec_type : supported_codec_types_) { |
| 906 | if (codec_type == type) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 907 | ALOGD << "Create HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 908 | return new MediaCodecVideoDecoder(AttachCurrentThreadIfNeeded(), type, |
| 909 | egl_.egl_base_context()); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 910 | } |
| 911 | } |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 912 | ALOGW << "Can not find HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 913 | return nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void MediaCodecVideoDecoderFactory::DestroyVideoDecoder( |
| 917 | webrtc::VideoDecoder* decoder) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 918 | ALOGD << "Destroy video decoder."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 919 | delete decoder; |
| 920 | } |
| 921 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 922 | const char* MediaCodecVideoDecoder::ImplementationName() const { |
| 923 | return "MediaCodec"; |
| 924 | } |
| 925 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 926 | } // namespace webrtc_jni |
| 927 | |