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