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