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(); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 102 | int32_t ResetDecodeOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 103 | 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 Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 108 | int32_t ProcessHWErrorOnCodecThread(); |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 109 | void EnableFrameLogOnWarning(); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 110 | void ResetVariables(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 111 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 112 | // Type of video codec. |
| 113 | VideoCodecType codecType_; |
| 114 | |
kjellander | 60ca31b | 2016-01-04 10:15:53 -0800 | [diff] [blame] | 115 | // Render EGL context - owned by factory, should not be allocated/destroyed |
| 116 | // by VideoDecoder. |
| 117 | jobject render_egl_context_; |
| 118 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 119 | bool key_frame_required_; |
| 120 | bool inited_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 121 | bool sw_fallback_required_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 122 | bool use_surface_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 123 | VideoCodec codec_; |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 124 | webrtc::I420BufferPool decoded_frame_pool_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 125 | rtc::scoped_refptr<SurfaceTextureHelper> surface_texture_helper_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 126 | DecodedImageCallback* callback_; |
| 127 | int frames_received_; // Number of frames received by decoder. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 128 | int frames_decoded_; // Number of frames decoded by decoder. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 129 | // Number of decoded frames for which log information is displayed. |
| 130 | int frames_decoded_logged_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 131 | 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 |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 135 | 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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 137 | |
| 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 Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 144 | jmethodID j_reset_method_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 145 | jmethodID j_release_method_; |
| 146 | jmethodID j_dequeue_input_buffer_method_; |
| 147 | jmethodID j_queue_input_buffer_method_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 148 | jmethodID j_dequeue_byte_buffer_method_; |
| 149 | jmethodID j_dequeue_texture_buffer_method_; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 150 | jmethodID j_return_decoded_byte_buffer_method_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 151 | // 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_; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 159 | // MediaCodecVideoDecoder.DecodedTextureBuffer fields. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 160 | jfieldID j_texture_id_field_; |
| 161 | jfieldID j_transform_matrix_field_; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 162 | jfieldID j_texture_presentation_timestamp_ms_field_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 163 | 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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 168 | jfieldID j_info_index_field_; |
| 169 | jfieldID j_info_offset_field_; |
| 170 | jfieldID j_info_size_field_; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 171 | jfieldID j_presentation_timestamp_ms_field_; |
| 172 | jfieldID j_timestamp_ms_field_; |
| 173 | jfieldID j_ntp_timestamp_ms_field_; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 174 | jfieldID j_byte_buffer_decode_time_ms_field_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 175 | |
| 176 | // Global references; must be deleted in Release(). |
| 177 | std::vector<jobject> input_buffers_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 180 | MediaCodecVideoDecoder::MediaCodecVideoDecoder( |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 181 | JNIEnv* jni, VideoCodecType codecType, jobject render_egl_context) : |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 182 | codecType_(codecType), |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 183 | render_egl_context_(render_egl_context), |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 184 | key_frame_required_(true), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 185 | inited_(false), |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 186 | sw_fallback_required_(false), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 187 | 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); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 200 | RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoDecoder"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 201 | |
| 202 | j_init_decode_method_ = GetMethodID( |
| 203 | jni, *j_media_codec_video_decoder_class_, "initDecode", |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 204 | "(Lorg/webrtc/MediaCodecVideoDecoder$VideoCodecType;" |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 205 | "IILorg/webrtc/SurfaceTextureHelper;)Z"); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 206 | j_reset_method_ = |
| 207 | GetMethodID(jni, *j_media_codec_video_decoder_class_, "reset", "(II)V"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 208 | 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( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 213 | jni, *j_media_codec_video_decoder_class_, "queueInputBuffer", "(IIJJJ)Z"); |
| 214 | j_dequeue_byte_buffer_method_ = GetMethodID( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 215 | jni, *j_media_codec_video_decoder_class_, "dequeueOutputBuffer", |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 216 | "(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;"); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 220 | j_return_decoded_byte_buffer_method_ = |
| 221 | GetMethodID(jni, *j_media_codec_video_decoder_class_, |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 222 | "returnDecodedOutputBuffer", "(I)V"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 223 | |
| 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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 240 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 241 | jclass j_decoded_texture_buffer_class = FindClass(jni, |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 242 | "org/webrtc/MediaCodecVideoDecoder$DecodedTextureBuffer"); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 243 | 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"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 247 | j_texture_presentation_timestamp_ms_field_ = GetFieldID( |
| 248 | jni, j_decoded_texture_buffer_class, "presentationTimeStampMs", "J"); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 249 | 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"); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 257 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 258 | jclass j_decoded_output_buffer_class = FindClass(jni, |
| 259 | "org/webrtc/MediaCodecVideoDecoder$DecodedOutputBuffer"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 260 | j_info_index_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 261 | jni, j_decoded_output_buffer_class, "index", "I"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 262 | j_info_offset_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 263 | jni, j_decoded_output_buffer_class, "offset", "I"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 264 | j_info_size_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 265 | jni, j_decoded_output_buffer_class, "size", "I"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 266 | j_presentation_timestamp_ms_field_ = GetFieldID( |
| 267 | jni, j_decoded_output_buffer_class, "presentationTimeStampMs", "J"); |
| 268 | j_timestamp_ms_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 269 | jni, j_decoded_output_buffer_class, "timeStampMs", "J"); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 270 | j_ntp_timestamp_ms_field_ = GetFieldID( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 271 | 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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 274 | |
| 275 | CHECK_EXCEPTION(jni) << "MediaCodecVideoDecoder ctor failed"; |
Magnus Jedvert | 207370f | 2015-09-16 12:32:21 +0200 | [diff] [blame] | 276 | use_surface_ = (render_egl_context_ != NULL); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 277 | ALOGD << "MediaCodecVideoDecoder ctor. Use surface: " << use_surface_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 278 | memset(&codec_, 0, sizeof(codec_)); |
| 279 | AllowBlockingCalls(); |
| 280 | } |
| 281 | |
| 282 | MediaCodecVideoDecoder::~MediaCodecVideoDecoder() { |
| 283 | // Call Release() to ensure no more callbacks to us after we are deleted. |
| 284 | Release(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int32_t MediaCodecVideoDecoder::InitDecode(const VideoCodec* inst, |
| 288 | int32_t numberOfCores) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 289 | ALOGD << "InitDecode."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 290 | if (inst == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 291 | ALOGE << "NULL VideoCodec instance"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 292 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 293 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 294 | // Factory should guard against other codecs being used with us. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 295 | RTC_CHECK(inst->codecType == codecType_) |
| 296 | << "Unsupported codec " << inst->codecType << " for " << codecType_; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 297 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 298 | if (sw_fallback_required_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 299 | ALOGE << "InitDecode() - fallback to SW decoder"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 300 | return WEBRTC_VIDEO_CODEC_OK; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 301 | } |
| 302 | // Save VideoCodec instance for later. |
| 303 | if (&codec_ != inst) { |
| 304 | codec_ = *inst; |
| 305 | } |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 306 | // If maxFramerate is not set then assume 30 fps. |
| 307 | codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 30; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 308 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 309 | // Call Java init. |
| 310 | return codec_thread_->Invoke<int32_t>( |
| 311 | Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this)); |
| 312 | } |
| 313 | |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 314 | void 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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 328 | int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() { |
| 329 | CheckOnCodecThread(); |
| 330 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 331 | ScopedLocalRefFrame local_ref_frame(jni); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 332 | ALOGD << "InitDecodeOnCodecThread Type: " << (int)codecType_ << ". " |
| 333 | << codec_.width << " x " << codec_.height << ". Fps: " << |
| 334 | (int)codec_.maxFramerate; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 335 | |
| 336 | // Release previous codec first if it was allocated before. |
| 337 | int ret_val = ReleaseOnCodecThread(); |
| 338 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 339 | ALOGE << "Release failure: " << ret_val << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 340 | sw_fallback_required_ = true; |
| 341 | return WEBRTC_VIDEO_CODEC_ERROR; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 344 | ResetVariables(); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 345 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 346 | if (use_surface_) { |
| 347 | surface_texture_helper_ = new rtc::RefCountedObject<SurfaceTextureHelper>( |
magjed | 82b750b | 2016-03-31 00:54:15 -0700 | [diff] [blame] | 348 | jni, "Decoder SurfaceTextureHelper", render_egl_context_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 351 | jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 352 | jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 353 | bool success = jni->CallBooleanMethod( |
| 354 | *j_media_codec_video_decoder_, |
| 355 | j_init_decode_method_, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 356 | j_video_codec_enum, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 357 | codec_.width, |
| 358 | codec_.height, |
magjed | 0dc2316 | 2016-03-14 03:59:38 -0700 | [diff] [blame] | 359 | use_surface_ ? surface_texture_helper_->GetJavaSurfaceTextureHelper() |
| 360 | : nullptr); |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 361 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 362 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 363 | ALOGE << "Codec initialization error - fallback to SW codec."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 364 | sw_fallback_required_ = true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 365 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 366 | } |
| 367 | inited_ = true; |
| 368 | |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 369 | switch (codecType_) { |
| 370 | case kVideoCodecVP8: |
| 371 | max_pending_frames_ = kMaxPendingFramesVp8; |
| 372 | break; |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 373 | case kVideoCodecVP9: |
| 374 | max_pending_frames_ = kMaxPendingFramesVp9; |
| 375 | break; |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 376 | case kVideoCodecH264: |
| 377 | max_pending_frames_ = kMaxPendingFramesH264; |
| 378 | break; |
| 379 | default: |
| 380 | max_pending_frames_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 381 | } |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 382 | ALOGD << "Maximum amount of pending frames: " << max_pending_frames_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 383 | |
| 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 Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 391 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 392 | ALOGE << "NewGlobalRef error - fallback to SW codec."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 393 | sw_fallback_required_ = true; |
| 394 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 395 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 396 | } |
| 397 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 398 | codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
| 399 | |
| 400 | return WEBRTC_VIDEO_CODEC_OK; |
| 401 | } |
| 402 | |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 403 | int32_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.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 434 | int32_t MediaCodecVideoDecoder::Release() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 435 | ALOGD << "DecoderRelease request"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 436 | return codec_thread_->Invoke<int32_t>( |
| 437 | Bind(&MediaCodecVideoDecoder::ReleaseOnCodecThread, this)); |
| 438 | } |
| 439 | |
| 440 | int32_t MediaCodecVideoDecoder::ReleaseOnCodecThread() { |
| 441 | if (!inited_) { |
| 442 | return WEBRTC_VIDEO_CODEC_OK; |
| 443 | } |
| 444 | CheckOnCodecThread(); |
| 445 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 446 | ALOGD << "DecoderReleaseOnCodecThread: Frames received: " << |
| 447 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 448 | 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_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 454 | surface_texture_helper_ = nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 455 | inited_ = false; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 456 | rtc::MessageQueueManager::Clear(this); |
| 457 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 458 | ALOGE << "Decoder release exception"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 459 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 460 | } |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 461 | ALOGD << "DecoderReleaseOnCodecThread done"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 462 | return WEBRTC_VIDEO_CODEC_OK; |
| 463 | } |
| 464 | |
| 465 | void MediaCodecVideoDecoder::CheckOnCodecThread() { |
kwiberg | 9708e9c | 2016-03-29 10:17:42 -0700 | [diff] [blame] | 466 | RTC_CHECK(codec_thread_.get() == ThreadManager::Instance()->CurrentThread()) |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 467 | << "Running on wrong thread!"; |
| 468 | } |
| 469 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 470 | void MediaCodecVideoDecoder::EnableFrameLogOnWarning() { |
| 471 | // Log next 2 output frames. |
| 472 | frames_decoded_logged_ = std::max( |
| 473 | frames_decoded_logged_, frames_decoded_ + kMaxWarningLogFrames); |
| 474 | } |
| 475 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 476 | int32_t MediaCodecVideoDecoder::ProcessHWErrorOnCodecThread() { |
| 477 | CheckOnCodecThread(); |
| 478 | int ret_val = ReleaseOnCodecThread(); |
| 479 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 480 | ALOGE << "ProcessHWError: Release failure"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 481 | } |
| 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 Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 486 | ALOGE << "Reset H.264 codec done. Status: " << ret_val; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 487 | 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 Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 497 | ALOGE << "Return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 498 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 499 | } |
| 500 | } |
| 501 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 502 | int32_t MediaCodecVideoDecoder::Decode( |
| 503 | const EncodedImage& inputImage, |
| 504 | bool missingFrames, |
| 505 | const RTPFragmentationHeader* fragmentation, |
| 506 | const CodecSpecificInfo* codecSpecificInfo, |
| 507 | int64_t renderTimeMs) { |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 508 | if (sw_fallback_required_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 509 | ALOGE << "Decode() - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 510 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 511 | } |
| 512 | if (callback_ == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 513 | ALOGE << "Decode() - callback_ is NULL"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 514 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 515 | } |
| 516 | if (inputImage._buffer == NULL && inputImage._length > 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 517 | ALOGE << "Decode() - inputImage is incorrect"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 518 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 519 | } |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 520 | if (!inited_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 521 | ALOGE << "Decode() - decoder is not initialized"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 522 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 523 | } |
| 524 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 525 | // 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 Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 529 | ALOGW << "Input resolution changed from " << |
| 530 | codec_.width << " x " << codec_.height << " to " << |
| 531 | inputImage._encodedWidth << " x " << inputImage._encodedHeight; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 532 | codec_.width = inputImage._encodedWidth; |
| 533 | codec_.height = inputImage._encodedHeight; |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 534 | int32_t ret; |
| 535 | if (use_surface_ && codecType_ == kVideoCodecVP8) { |
| 536 | // Soft codec reset - only for VP8 and surface decoding. |
| 537 | // TODO(glaznev): try to use similar approach for H.264 |
| 538 | // and buffer decoding. |
| 539 | ret = codec_thread_->Invoke<int32_t>(Bind( |
| 540 | &MediaCodecVideoDecoder::ResetDecodeOnCodecThread, this)); |
| 541 | } else { |
| 542 | // Hard codec reset. |
| 543 | ret = InitDecode(&codec_, 1); |
| 544 | } |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 545 | if (ret < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 546 | ALOGE << "InitDecode failure: " << ret << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 547 | sw_fallback_required_ = true; |
| 548 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 549 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // Always start with a complete key frame. |
| 553 | if (key_frame_required_) { |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 554 | if (inputImage._frameType != webrtc::kVideoFrameKey) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 555 | ALOGE << "Decode() - key frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 556 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 557 | } |
| 558 | if (!inputImage._completeFrame) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 559 | ALOGE << "Decode() - complete frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 560 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 561 | } |
| 562 | key_frame_required_ = false; |
| 563 | } |
| 564 | if (inputImage._length == 0) { |
| 565 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 566 | } |
| 567 | |
| 568 | return codec_thread_->Invoke<int32_t>(Bind( |
| 569 | &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage)); |
| 570 | } |
| 571 | |
| 572 | int32_t MediaCodecVideoDecoder::DecodeOnCodecThread( |
| 573 | const EncodedImage& inputImage) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 574 | CheckOnCodecThread(); |
| 575 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 576 | ScopedLocalRefFrame local_ref_frame(jni); |
| 577 | |
| 578 | // Try to drain the decoder and wait until output is not too |
| 579 | // much behind the input. |
Alex Glaznev | 6a4a03c | 2016-03-04 14:10:50 -0800 | [diff] [blame] | 580 | if (codecType_ == kVideoCodecH264 && |
| 581 | frames_received_ > frames_decoded_ + max_pending_frames_) { |
| 582 | // Print warning for H.264 only - for VP8/VP9 one frame delay is ok. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 583 | ALOGW << "Decoder is too far behind. Try to drain. Received: " << |
| 584 | frames_received_ << ". Decoded: " << frames_decoded_; |
| 585 | EnableFrameLogOnWarning(); |
| 586 | } |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 587 | const int64 drain_start = GetCurrentTimeMs(); |
| 588 | while ((frames_received_ > frames_decoded_ + max_pending_frames_) && |
| 589 | (GetCurrentTimeMs() - drain_start) < kMediaCodecTimeoutMs) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 590 | if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) { |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 591 | ALOGE << "DeliverPendingOutputs error. Frames received: " << |
| 592 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 593 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 594 | } |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 595 | } |
| 596 | if (frames_received_ > frames_decoded_ + max_pending_frames_) { |
| 597 | ALOGE << "Output buffer dequeue timeout. Frames received: " << |
| 598 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
| 599 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Get input buffer. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 603 | int j_input_buffer_index = jni->CallIntMethod( |
| 604 | *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 605 | if (CheckException(jni) || j_input_buffer_index < 0) { |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 606 | ALOGE << "dequeueInputBuffer error: " << j_input_buffer_index << |
| 607 | ". Retry DeliverPendingOutputs."; |
| 608 | EnableFrameLogOnWarning(); |
| 609 | // Try to drain the decoder. |
| 610 | if (!DeliverPendingOutputs(jni, kMediaCodecPollMs)) { |
| 611 | ALOGE << "DeliverPendingOutputs error. Frames received: " << |
| 612 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
| 613 | return ProcessHWErrorOnCodecThread(); |
| 614 | } |
| 615 | // Try dequeue input buffer one last time. |
| 616 | j_input_buffer_index = jni->CallIntMethod( |
| 617 | *j_media_codec_video_decoder_, j_dequeue_input_buffer_method_); |
| 618 | if (CheckException(jni) || j_input_buffer_index < 0) { |
| 619 | ALOGE << "dequeueInputBuffer critical error: " << j_input_buffer_index; |
| 620 | return ProcessHWErrorOnCodecThread(); |
| 621 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // Copy encoded data to Java ByteBuffer. |
| 625 | jobject j_input_buffer = input_buffers_[j_input_buffer_index]; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 626 | uint8_t* buffer = |
| 627 | reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 628 | RTC_CHECK(buffer) << "Indirect buffer??"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 629 | int64_t buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 630 | if (CheckException(jni) || buffer_capacity < inputImage._length) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 631 | ALOGE << "Input frame size "<< inputImage._length << |
| 632 | " is bigger than buffer size " << buffer_capacity; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 633 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 634 | } |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 635 | jlong presentation_timestamp_us = static_cast<jlong>( |
| 636 | static_cast<int64_t>(frames_received_) * 1000000 / codec_.maxFramerate); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 637 | memcpy(buffer, inputImage._buffer, inputImage._length); |
| 638 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 639 | if (frames_decoded_ < frames_decoded_logged_) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 640 | ALOGD << "Decoder frame in # " << frames_received_ << |
| 641 | ". Type: " << inputImage._frameType << |
| 642 | ". Buffer # " << j_input_buffer_index << |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 643 | ". TS: " << presentation_timestamp_us / 1000 << |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 644 | ". Size: " << inputImage._length; |
| 645 | } |
| 646 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 647 | // Save input image timestamps for later output. |
| 648 | frames_received_++; |
| 649 | current_bytes_ += inputImage._length; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 650 | |
| 651 | // Feed input to decoder. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 652 | bool success = jni->CallBooleanMethod( |
| 653 | *j_media_codec_video_decoder_, |
| 654 | j_queue_input_buffer_method_, |
| 655 | j_input_buffer_index, |
| 656 | inputImage._length, |
| 657 | presentation_timestamp_us, |
| 658 | static_cast<int64_t> (inputImage._timeStamp), |
| 659 | inputImage.ntp_time_ms_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 660 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 661 | ALOGE << "queueInputBuffer error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 662 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // Try to drain the decoder |
| 666 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 667 | ALOGE << "DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 668 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | return WEBRTC_VIDEO_CODEC_OK; |
| 672 | } |
| 673 | |
| 674 | bool MediaCodecVideoDecoder::DeliverPendingOutputs( |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 675 | JNIEnv* jni, int dequeue_timeout_ms) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 676 | if (frames_received_ <= frames_decoded_) { |
| 677 | // No need to query for output buffers - decoder is drained. |
| 678 | return true; |
| 679 | } |
| 680 | // Get decoder output. |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 681 | jobject j_decoder_output_buffer = |
| 682 | jni->CallObjectMethod(*j_media_codec_video_decoder_, |
| 683 | use_surface_ ? j_dequeue_texture_buffer_method_ |
| 684 | : j_dequeue_byte_buffer_method_, |
| 685 | dequeue_timeout_ms); |
| 686 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 687 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 688 | ALOGE << "dequeueOutputBuffer() error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 689 | return false; |
| 690 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 691 | if (IsNull(jni, j_decoder_output_buffer)) { |
| 692 | // No decoded frame ready. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 693 | return true; |
| 694 | } |
| 695 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 696 | // Get decoded video frame properties. |
| 697 | int color_format = GetIntField(jni, *j_media_codec_video_decoder_, |
| 698 | j_color_format_field_); |
| 699 | int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_); |
| 700 | int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_); |
| 701 | int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_); |
| 702 | int slice_height = GetIntField(jni, *j_media_codec_video_decoder_, |
| 703 | j_slice_height_field_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 704 | |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 705 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> frame_buffer; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 706 | int64_t presentation_timestamps_ms = 0; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 707 | int64_t output_timestamps_ms = 0; |
| 708 | int64_t output_ntp_timestamps_ms = 0; |
| 709 | int decode_time_ms = 0; |
| 710 | int64_t frame_delayed_ms = 0; |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 711 | if (use_surface_) { |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 712 | // Extract data from Java DecodedTextureBuffer. |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 713 | presentation_timestamps_ms = GetLongField( |
| 714 | jni, j_decoder_output_buffer, |
| 715 | j_texture_presentation_timestamp_ms_field_); |
| 716 | output_timestamps_ms = GetLongField( |
| 717 | jni, j_decoder_output_buffer, j_texture_timestamp_ms_field_); |
| 718 | output_ntp_timestamps_ms = GetLongField( |
| 719 | jni, j_decoder_output_buffer, j_texture_ntp_timestamp_ms_field_); |
| 720 | decode_time_ms = GetLongField( |
| 721 | jni, j_decoder_output_buffer, j_texture_decode_time_ms_field_); |
| 722 | |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 723 | const int texture_id = |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 724 | GetIntField(jni, j_decoder_output_buffer, j_texture_id_field_); |
| 725 | if (texture_id != 0) { // |texture_id| == 0 represents a dropped frame. |
| 726 | const jfloatArray j_transform_matrix = |
| 727 | reinterpret_cast<jfloatArray>(GetObjectField( |
| 728 | jni, j_decoder_output_buffer, j_transform_matrix_field_)); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 729 | frame_delayed_ms = GetLongField( |
| 730 | jni, j_decoder_output_buffer, j_texture_frame_delay_ms_field_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 731 | |
| 732 | // Create webrtc::VideoFrameBuffer with native texture handle. |
| 733 | frame_buffer = surface_texture_helper_->CreateTextureFrame( |
| 734 | width, height, NativeHandleImpl(jni, texture_id, j_transform_matrix)); |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 735 | } else { |
| 736 | EnableFrameLogOnWarning(); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 737 | } |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 738 | } else { |
| 739 | // Extract data from Java ByteBuffer and create output yuv420 frame - |
| 740 | // for non surface decoding only. |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 741 | const int output_buffer_index = GetIntField( |
| 742 | jni, j_decoder_output_buffer, j_info_index_field_); |
| 743 | const int output_buffer_offset = GetIntField( |
| 744 | jni, j_decoder_output_buffer, j_info_offset_field_); |
| 745 | const int output_buffer_size = GetIntField( |
| 746 | jni, j_decoder_output_buffer, j_info_size_field_); |
| 747 | presentation_timestamps_ms = GetLongField( |
| 748 | jni, j_decoder_output_buffer, j_presentation_timestamp_ms_field_); |
| 749 | output_timestamps_ms = GetLongField( |
| 750 | jni, j_decoder_output_buffer, j_timestamp_ms_field_); |
| 751 | output_ntp_timestamps_ms = GetLongField( |
| 752 | jni, j_decoder_output_buffer, j_ntp_timestamp_ms_field_); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 753 | |
| 754 | decode_time_ms = GetLongField(jni, j_decoder_output_buffer, |
| 755 | j_byte_buffer_decode_time_ms_field_); |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 756 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 757 | if (output_buffer_size < width * height * 3 / 2) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 758 | ALOGE << "Insufficient output buffer size: " << output_buffer_size; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 759 | return false; |
| 760 | } |
glaznev | 3816bfd | 2016-03-08 10:35:33 -0800 | [diff] [blame] | 761 | if (output_buffer_size < stride * height * 3 / 2 && |
| 762 | slice_height == height && stride > width) { |
| 763 | // Some codecs (Exynos) incorrectly report stride information for |
| 764 | // output byte buffer, so actual stride value need to be corrected. |
| 765 | stride = output_buffer_size * 2 / (height * 3); |
| 766 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 767 | jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField( |
| 768 | jni, *j_media_codec_video_decoder_, j_output_buffers_field_)); |
| 769 | jobject output_buffer = |
| 770 | jni->GetObjectArrayElement(output_buffers, output_buffer_index); |
| 771 | uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress( |
| 772 | output_buffer)); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 773 | if (CheckException(jni)) { |
| 774 | return false; |
| 775 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 776 | payload += output_buffer_offset; |
| 777 | |
| 778 | // Create yuv420 frame. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 779 | frame_buffer = decoded_frame_pool_.CreateBuffer(width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 780 | if (color_format == COLOR_FormatYUV420Planar) { |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 781 | RTC_CHECK_EQ(0, stride % 2); |
| 782 | RTC_CHECK_EQ(0, slice_height % 2); |
| 783 | const int uv_stride = stride / 2; |
| 784 | const int u_slice_height = slice_height / 2; |
| 785 | const uint8_t* y_ptr = payload; |
| 786 | const uint8_t* u_ptr = y_ptr + stride * slice_height; |
| 787 | const uint8_t* v_ptr = u_ptr + uv_stride * u_slice_height; |
| 788 | libyuv::I420Copy(y_ptr, stride, |
| 789 | u_ptr, uv_stride, |
| 790 | v_ptr, uv_stride, |
| 791 | frame_buffer->MutableData(webrtc::kYPlane), |
| 792 | frame_buffer->stride(webrtc::kYPlane), |
| 793 | frame_buffer->MutableData(webrtc::kUPlane), |
| 794 | frame_buffer->stride(webrtc::kUPlane), |
| 795 | frame_buffer->MutableData(webrtc::kVPlane), |
| 796 | frame_buffer->stride(webrtc::kVPlane), |
| 797 | width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 798 | } else { |
| 799 | // All other supported formats are nv12. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 800 | const uint8_t* y_ptr = payload; |
| 801 | const uint8_t* uv_ptr = y_ptr + stride * slice_height; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 802 | libyuv::NV12ToI420( |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 803 | y_ptr, stride, |
| 804 | uv_ptr, stride, |
| 805 | frame_buffer->MutableData(webrtc::kYPlane), |
| 806 | frame_buffer->stride(webrtc::kYPlane), |
| 807 | frame_buffer->MutableData(webrtc::kUPlane), |
| 808 | frame_buffer->stride(webrtc::kUPlane), |
| 809 | frame_buffer->MutableData(webrtc::kVPlane), |
| 810 | frame_buffer->stride(webrtc::kVPlane), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 811 | width, height); |
| 812 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 813 | // Return output byte buffer back to codec. |
| 814 | jni->CallVoidMethod( |
| 815 | *j_media_codec_video_decoder_, |
| 816 | j_return_decoded_byte_buffer_method_, |
| 817 | output_buffer_index); |
| 818 | if (CheckException(jni)) { |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 819 | ALOGE << "returnDecodedOutputBuffer error"; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 820 | return false; |
| 821 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 822 | } |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 823 | VideoFrame decoded_frame(frame_buffer, 0, 0, webrtc::kVideoRotation_0); |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 824 | decoded_frame.set_timestamp(output_timestamps_ms); |
| 825 | decoded_frame.set_ntp_time_ms(output_ntp_timestamps_ms); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 826 | |
glaznev | ae95ff3 | 2016-02-04 11:47:12 -0800 | [diff] [blame] | 827 | if (frames_decoded_ < frames_decoded_logged_) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 828 | ALOGD << "Decoder frame out # " << frames_decoded_ << |
| 829 | ". " << width << " x " << height << |
| 830 | ". " << stride << " x " << slice_height << |
| 831 | ". Color: " << color_format << |
| 832 | ". TS: " << presentation_timestamps_ms << |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 833 | ". DecTime: " << (int)decode_time_ms << |
| 834 | ". DelayTime: " << (int)frame_delayed_ms; |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 835 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 836 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 837 | // Calculate and print decoding statistics - every 3 seconds. |
| 838 | frames_decoded_++; |
| 839 | current_frames_++; |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 840 | current_decoding_time_ms_ += decode_time_ms; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 841 | current_delay_time_ms_ += frame_delayed_ms; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 842 | int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_; |
| 843 | if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs && |
| 844 | current_frames_ > 0) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 845 | int current_bitrate = current_bytes_ * 8 / statistic_time_ms; |
| 846 | int current_fps = |
| 847 | (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 848 | ALOGD << "Frames decoded: " << frames_decoded_ << |
| 849 | ". Received: " << frames_received_ << |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 850 | ". Bitrate: " << current_bitrate << " kbps" << |
| 851 | ". Fps: " << current_fps << |
| 852 | ". DecTime: " << (current_decoding_time_ms_ / current_frames_) << |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 853 | ". DelayTime: " << (current_delay_time_ms_ / current_frames_) << |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 854 | " for last " << statistic_time_ms << " ms."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 855 | start_time_ms_ = GetCurrentTimeMs(); |
| 856 | current_frames_ = 0; |
| 857 | current_bytes_ = 0; |
| 858 | current_decoding_time_ms_ = 0; |
glaznev | fd6706a | 2016-02-05 14:05:08 -0800 | [diff] [blame] | 859 | current_delay_time_ms_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Per | 488e75f | 2015-11-19 10:43:36 +0100 | [diff] [blame] | 862 | // |.IsZeroSize())| returns true when a frame has been dropped. |
| 863 | if (!decoded_frame.IsZeroSize()) { |
| 864 | // Callback - output decoded frame. |
| 865 | const int32_t callback_status = |
| 866 | callback_->Decoded(decoded_frame, decode_time_ms); |
| 867 | if (callback_status > 0) { |
| 868 | ALOGE << "callback error"; |
| 869 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 870 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 871 | return true; |
| 872 | } |
| 873 | |
| 874 | int32_t MediaCodecVideoDecoder::RegisterDecodeCompleteCallback( |
| 875 | DecodedImageCallback* callback) { |
| 876 | callback_ = callback; |
| 877 | return WEBRTC_VIDEO_CODEC_OK; |
| 878 | } |
| 879 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 880 | void MediaCodecVideoDecoder::OnMessage(rtc::Message* msg) { |
| 881 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 882 | ScopedLocalRefFrame local_ref_frame(jni); |
| 883 | if (!inited_) { |
| 884 | return; |
| 885 | } |
| 886 | // We only ever send one message to |this| directly (not through a Bind()'d |
| 887 | // functor), so expect no ID/data. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 888 | RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
| 889 | RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 890 | CheckOnCodecThread(); |
| 891 | |
| 892 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 893 | ALOGE << "OnMessage: DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 894 | ProcessHWErrorOnCodecThread(); |
| 895 | return; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 896 | } |
| 897 | codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
| 898 | } |
| 899 | |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 900 | MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory() |
| 901 | : egl_context_(nullptr) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 902 | ALOGD << "MediaCodecVideoDecoderFactory ctor"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 903 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 904 | ScopedLocalRefFrame local_ref_frame(jni); |
| 905 | jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 906 | supported_codec_types_.clear(); |
| 907 | |
| 908 | bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 909 | j_decoder_class, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 910 | GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 911 | if (CheckException(jni)) { |
| 912 | is_vp8_hw_supported = false; |
| 913 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 914 | if (is_vp8_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 915 | ALOGD << "VP8 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 916 | supported_codec_types_.push_back(kVideoCodecVP8); |
| 917 | } |
| 918 | |
Alex Glaznev | 69a7fd5 | 2015-11-10 10:25:40 -0800 | [diff] [blame] | 919 | bool is_vp9_hw_supported = jni->CallStaticBooleanMethod( |
| 920 | j_decoder_class, |
| 921 | GetStaticMethodID(jni, j_decoder_class, "isVp9HwSupported", "()Z")); |
| 922 | if (CheckException(jni)) { |
| 923 | is_vp9_hw_supported = false; |
| 924 | } |
| 925 | if (is_vp9_hw_supported) { |
| 926 | ALOGD << "VP9 HW Decoder supported."; |
| 927 | supported_codec_types_.push_back(kVideoCodecVP9); |
| 928 | } |
| 929 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 930 | bool is_h264_hw_supported = jni->CallStaticBooleanMethod( |
| 931 | j_decoder_class, |
| 932 | GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 933 | if (CheckException(jni)) { |
| 934 | is_h264_hw_supported = false; |
| 935 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 936 | if (is_h264_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 937 | ALOGD << "H264 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 938 | supported_codec_types_.push_back(kVideoCodecH264); |
| 939 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 942 | MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 943 | ALOGD << "MediaCodecVideoDecoderFactory dtor"; |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 944 | if (egl_context_) { |
| 945 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 946 | jni->DeleteGlobalRef(egl_context_); |
| 947 | } |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void MediaCodecVideoDecoderFactory::SetEGLContext( |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 951 | JNIEnv* jni, jobject egl_context) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 952 | ALOGD << "MediaCodecVideoDecoderFactory::SetEGLContext"; |
Per | fd22e6c | 2016-02-18 11:35:48 +0100 | [diff] [blame] | 953 | if (egl_context_) { |
| 954 | jni->DeleteGlobalRef(egl_context_); |
| 955 | egl_context_ = nullptr; |
| 956 | } |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 957 | egl_context_ = jni->NewGlobalRef(egl_context); |
| 958 | if (CheckException(jni)) { |
| 959 | ALOGE << "error calling NewGlobalRef for EGL Context."; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 962 | |
| 963 | webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 964 | VideoCodecType type) { |
| 965 | if (supported_codec_types_.empty()) { |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 966 | ALOGW << "No HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 967 | return nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 968 | } |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 969 | for (VideoCodecType codec_type : supported_codec_types_) { |
| 970 | if (codec_type == type) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 971 | ALOGD << "Create HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 972 | return new MediaCodecVideoDecoder(AttachCurrentThreadIfNeeded(), type, |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 973 | egl_context_); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 974 | } |
| 975 | } |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 976 | ALOGW << "Can not find HW video decoder for type " << (int)type; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 977 | return nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void MediaCodecVideoDecoderFactory::DestroyVideoDecoder( |
| 981 | webrtc::VideoDecoder* decoder) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 982 | ALOGD << "Destroy video decoder."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 983 | delete decoder; |
| 984 | } |
| 985 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 986 | const char* MediaCodecVideoDecoder::ImplementationName() const { |
| 987 | return "MediaCodec"; |
| 988 | } |
| 989 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 990 | } // namespace webrtc_jni |
| 991 | |