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