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