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" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 44 | #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
| 45 | #include "webrtc/system_wrappers/include/tick_util.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 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 | 8c425aa | 2015-10-22 16:52:39 -0700 | [diff] [blame] | 194 | "IILjavax/microedition/khronos/egl/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 | } |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 286 | // If maxFramerate is not set then assume 30 fps. |
| 287 | codec_.maxFramerate = (codec_.maxFramerate >= 1) ? codec_.maxFramerate : 30; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 288 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 289 | // Call Java init. |
| 290 | return codec_thread_->Invoke<int32_t>( |
| 291 | Bind(&MediaCodecVideoDecoder::InitDecodeOnCodecThread, this)); |
| 292 | } |
| 293 | |
| 294 | int32_t MediaCodecVideoDecoder::InitDecodeOnCodecThread() { |
| 295 | CheckOnCodecThread(); |
| 296 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 297 | ScopedLocalRefFrame local_ref_frame(jni); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 298 | ALOGD << "InitDecodeOnCodecThread Type: " << (int)codecType_ << ". " |
| 299 | << codec_.width << " x " << codec_.height << ". Fps: " << |
| 300 | (int)codec_.maxFramerate; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 301 | |
| 302 | // Release previous codec first if it was allocated before. |
| 303 | int ret_val = ReleaseOnCodecThread(); |
| 304 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 305 | ALOGE << "Release failure: " << ret_val << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 306 | sw_fallback_required_ = true; |
| 307 | return WEBRTC_VIDEO_CODEC_ERROR; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 310 | // Always start with a complete key frame. |
| 311 | key_frame_required_ = true; |
| 312 | frames_received_ = 0; |
| 313 | frames_decoded_ = 0; |
| 314 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 315 | jobject j_video_codec_enum = JavaEnumFromIndex( |
| 316 | jni, "MediaCodecVideoDecoder$VideoCodecType", codecType_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 317 | bool success = jni->CallBooleanMethod( |
| 318 | *j_media_codec_video_decoder_, |
| 319 | j_init_decode_method_, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 320 | j_video_codec_enum, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 321 | codec_.width, |
| 322 | codec_.height, |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 323 | use_surface_ ? render_egl_context_ : nullptr); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 324 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 325 | ALOGE << "Codec initialization error - fallback to SW codec."; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 326 | sw_fallback_required_ = true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 327 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 328 | } |
| 329 | inited_ = true; |
| 330 | |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 331 | switch (codecType_) { |
| 332 | case kVideoCodecVP8: |
| 333 | max_pending_frames_ = kMaxPendingFramesVp8; |
| 334 | break; |
| 335 | case kVideoCodecH264: |
| 336 | max_pending_frames_ = kMaxPendingFramesH264; |
| 337 | break; |
| 338 | default: |
| 339 | max_pending_frames_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 340 | } |
| 341 | start_time_ms_ = GetCurrentTimeMs(); |
| 342 | current_frames_ = 0; |
| 343 | current_bytes_ = 0; |
| 344 | current_decoding_time_ms_ = 0; |
| 345 | timestamps_.clear(); |
| 346 | ntp_times_ms_.clear(); |
| 347 | frame_rtc_times_ms_.clear(); |
| 348 | |
| 349 | jobjectArray input_buffers = (jobjectArray)GetObjectField( |
| 350 | jni, *j_media_codec_video_decoder_, j_input_buffers_field_); |
| 351 | size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 352 | ALOGD << "Maximum amount of pending frames: " << max_pending_frames_; |
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(); |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 390 | ALOGD << "DecoderReleaseOnCodecThread: Frames received: " << |
| 391 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 392 | ScopedLocalRefFrame local_ref_frame(jni); |
| 393 | for (size_t i = 0; i < input_buffers_.size(); i++) { |
| 394 | jni->DeleteGlobalRef(input_buffers_[i]); |
| 395 | } |
| 396 | input_buffers_.clear(); |
| 397 | jni->CallVoidMethod(*j_media_codec_video_decoder_, j_release_method_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 398 | inited_ = false; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 399 | rtc::MessageQueueManager::Clear(this); |
| 400 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 401 | ALOGE << "Decoder release exception"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 402 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 403 | } |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 404 | ALOGD << "DecoderReleaseOnCodecThread done"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 405 | return WEBRTC_VIDEO_CODEC_OK; |
| 406 | } |
| 407 | |
| 408 | void MediaCodecVideoDecoder::CheckOnCodecThread() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 409 | RTC_CHECK(codec_thread_ == ThreadManager::Instance()->CurrentThread()) |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 410 | << "Running on wrong thread!"; |
| 411 | } |
| 412 | |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 413 | int32_t MediaCodecVideoDecoder::ProcessHWErrorOnCodecThread() { |
| 414 | CheckOnCodecThread(); |
| 415 | int ret_val = ReleaseOnCodecThread(); |
| 416 | if (ret_val < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 417 | ALOGE << "ProcessHWError: Release failure"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 418 | } |
| 419 | if (codecType_ == kVideoCodecH264) { |
| 420 | // For now there is no SW H.264 which can be used as fallback codec. |
| 421 | // So try to restart hw codec for now. |
| 422 | ret_val = InitDecodeOnCodecThread(); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 423 | ALOGE << "Reset H.264 codec done. Status: " << ret_val; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 424 | if (ret_val == WEBRTC_VIDEO_CODEC_OK) { |
| 425 | // H.264 codec was succesfully reset - return regular error code. |
| 426 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 427 | } else { |
| 428 | // Fail to restart H.264 codec - return error code which should stop the |
| 429 | // call. |
| 430 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 431 | } |
| 432 | } else { |
| 433 | sw_fallback_required_ = true; |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 434 | ALOGE << "Return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 435 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 436 | } |
| 437 | } |
| 438 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 439 | int32_t MediaCodecVideoDecoder::Decode( |
| 440 | const EncodedImage& inputImage, |
| 441 | bool missingFrames, |
| 442 | const RTPFragmentationHeader* fragmentation, |
| 443 | const CodecSpecificInfo* codecSpecificInfo, |
| 444 | int64_t renderTimeMs) { |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 445 | if (sw_fallback_required_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 446 | ALOGE << "Decode() - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 447 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 448 | } |
| 449 | if (callback_ == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 450 | ALOGE << "Decode() - callback_ is NULL"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 451 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 452 | } |
| 453 | if (inputImage._buffer == NULL && inputImage._length > 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 454 | ALOGE << "Decode() - inputImage is incorrect"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 455 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 456 | } |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 457 | if (!inited_) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 458 | ALOGE << "Decode() - decoder is not initialized"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 459 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 460 | } |
| 461 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 462 | // Check if encoded frame dimension has changed. |
| 463 | if ((inputImage._encodedWidth * inputImage._encodedHeight > 0) && |
| 464 | (inputImage._encodedWidth != codec_.width || |
| 465 | inputImage._encodedHeight != codec_.height)) { |
| 466 | codec_.width = inputImage._encodedWidth; |
| 467 | codec_.height = inputImage._encodedHeight; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 468 | int32_t ret = InitDecode(&codec_, 1); |
| 469 | if (ret < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 470 | ALOGE << "InitDecode failure: " << ret << " - fallback to SW codec"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 471 | sw_fallback_required_ = true; |
| 472 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 473 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Always start with a complete key frame. |
| 477 | if (key_frame_required_) { |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 478 | if (inputImage._frameType != webrtc::kVideoFrameKey) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 479 | ALOGE << "Decode() - key frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 480 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 481 | } |
| 482 | if (!inputImage._completeFrame) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 483 | ALOGE << "Decode() - complete frame is required"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 484 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 485 | } |
| 486 | key_frame_required_ = false; |
| 487 | } |
| 488 | if (inputImage._length == 0) { |
| 489 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 490 | } |
| 491 | |
| 492 | return codec_thread_->Invoke<int32_t>(Bind( |
| 493 | &MediaCodecVideoDecoder::DecodeOnCodecThread, this, inputImage)); |
| 494 | } |
| 495 | |
| 496 | int32_t MediaCodecVideoDecoder::DecodeOnCodecThread( |
| 497 | const EncodedImage& inputImage) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 498 | CheckOnCodecThread(); |
| 499 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 500 | ScopedLocalRefFrame local_ref_frame(jni); |
| 501 | |
| 502 | // Try to drain the decoder and wait until output is not too |
| 503 | // much behind the input. |
| 504 | if (frames_received_ > frames_decoded_ + max_pending_frames_) { |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 505 | ALOGV("Received: %d. Decoded: %d. Wait for output...", |
| 506 | frames_received_, frames_decoded_); |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 507 | if (!DeliverPendingOutputs(jni, kMediaCodecTimeoutMs * 1000)) { |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 508 | ALOGE << "DeliverPendingOutputs error. Frames received: " << |
| 509 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 510 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 511 | } |
| 512 | if (frames_received_ > frames_decoded_ + max_pending_frames_) { |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 513 | ALOGE << "Output buffer dequeue timeout. Frames received: " << |
| 514 | frames_received_ << ". Frames decoded: " << frames_decoded_; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 515 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | // Get input buffer. |
| 520 | int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_decoder_, |
| 521 | j_dequeue_input_buffer_method_); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 522 | if (CheckException(jni) || j_input_buffer_index < 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 523 | ALOGE << "dequeueInputBuffer error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 524 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // Copy encoded data to Java ByteBuffer. |
| 528 | jobject j_input_buffer = input_buffers_[j_input_buffer_index]; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 529 | uint8_t* buffer = |
| 530 | reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 531 | RTC_CHECK(buffer) << "Indirect buffer??"; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 532 | int64_t buffer_capacity = jni->GetDirectBufferCapacity(j_input_buffer); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 533 | if (CheckException(jni) || buffer_capacity < inputImage._length) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 534 | ALOGE << "Input frame size "<< inputImage._length << |
| 535 | " is bigger than buffer size " << buffer_capacity; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 536 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 537 | } |
glaznev@webrtc.org | a4623d2 | 2015-02-25 00:02:50 +0000 | [diff] [blame] | 538 | jlong timestamp_us = (frames_received_ * 1000000) / codec_.maxFramerate; |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 539 | if (frames_decoded_ < kMaxDecodedLogFrames) { |
| 540 | ALOGD << "Decoder frame in # " << frames_received_ << ". Type: " |
| 541 | << inputImage._frameType << ". Buffer # " << |
| 542 | j_input_buffer_index << ". TS: " << (int)(timestamp_us / 1000) |
| 543 | << ". Size: " << inputImage._length; |
| 544 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 545 | memcpy(buffer, inputImage._buffer, inputImage._length); |
| 546 | |
| 547 | // Save input image timestamps for later output. |
| 548 | frames_received_++; |
| 549 | current_bytes_ += inputImage._length; |
| 550 | timestamps_.push_back(inputImage._timeStamp); |
| 551 | ntp_times_ms_.push_back(inputImage.ntp_time_ms_); |
| 552 | frame_rtc_times_ms_.push_back(GetCurrentTimeMs()); |
| 553 | |
| 554 | // Feed input to decoder. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 555 | bool success = jni->CallBooleanMethod(*j_media_codec_video_decoder_, |
| 556 | j_queue_input_buffer_method_, |
| 557 | j_input_buffer_index, |
| 558 | inputImage._length, |
| 559 | timestamp_us); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 560 | if (CheckException(jni) || !success) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 561 | ALOGE << "queueInputBuffer 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 | // Try to drain the decoder |
| 566 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 567 | ALOGE << "DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 568 | return ProcessHWErrorOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | return WEBRTC_VIDEO_CODEC_OK; |
| 572 | } |
| 573 | |
| 574 | bool MediaCodecVideoDecoder::DeliverPendingOutputs( |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 575 | JNIEnv* jni, int dequeue_timeout_us) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 576 | if (frames_received_ <= frames_decoded_) { |
| 577 | // No need to query for output buffers - decoder is drained. |
| 578 | return true; |
| 579 | } |
| 580 | // Get decoder output. |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 581 | jobject j_decoder_output_buffer = jni->CallObjectMethod( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 582 | *j_media_codec_video_decoder_, |
| 583 | j_dequeue_output_buffer_method_, |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 584 | dequeue_timeout_us); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 585 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 586 | ALOGE << "dequeueOutputBuffer() error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 587 | return false; |
| 588 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 589 | if (IsNull(jni, j_decoder_output_buffer)) { |
| 590 | // No decoded frame ready. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 594 | // Get decoded video frame properties. |
| 595 | int color_format = GetIntField(jni, *j_media_codec_video_decoder_, |
| 596 | j_color_format_field_); |
| 597 | int width = GetIntField(jni, *j_media_codec_video_decoder_, j_width_field_); |
| 598 | int height = GetIntField(jni, *j_media_codec_video_decoder_, j_height_field_); |
| 599 | int stride = GetIntField(jni, *j_media_codec_video_decoder_, j_stride_field_); |
| 600 | int slice_height = GetIntField(jni, *j_media_codec_video_decoder_, |
| 601 | j_slice_height_field_); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 602 | |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 603 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> frame_buffer; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 604 | long output_timestamps_ms = 0; |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 605 | if (use_surface_) { |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 606 | // Extract data from Java DecodedTextureBuffer. |
| 607 | const int texture_id = |
| 608 | GetIntField(jni, j_decoder_output_buffer, j_textureID_field_); |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 609 | const int64_t timestamp_us = |
| 610 | GetLongField(jni, j_decoder_output_buffer, |
| 611 | j_texture_presentation_timestamp_us_field_); |
| 612 | output_timestamps_ms = timestamp_us / rtc::kNumMicrosecsPerMillisec; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 613 | // Create webrtc::VideoFrameBuffer with native texture handle. |
magjed | 543b6ca | 2015-10-15 05:45:07 -0700 | [diff] [blame] | 614 | native_handle_.SetTextureObject(surface_texture_, texture_id); |
| 615 | frame_buffer = new rtc::RefCountedObject<JniNativeHandleBuffer>( |
| 616 | &native_handle_, width, height); |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 617 | } else { |
| 618 | // Extract data from Java ByteBuffer and create output yuv420 frame - |
| 619 | // for non surface decoding only. |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 620 | const int output_buffer_index = |
| 621 | GetIntField(jni, j_decoder_output_buffer, j_info_index_field_); |
| 622 | const int output_buffer_offset = |
| 623 | GetIntField(jni, j_decoder_output_buffer, j_info_offset_field_); |
| 624 | const int output_buffer_size = |
| 625 | GetIntField(jni, j_decoder_output_buffer, j_info_size_field_); |
| 626 | const int64_t timestamp_us = GetLongField( |
| 627 | jni, j_decoder_output_buffer, j_info_presentation_timestamp_us_field_); |
| 628 | output_timestamps_ms = timestamp_us / rtc::kNumMicrosecsPerMillisec; |
| 629 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 630 | if (output_buffer_size < width * height * 3 / 2) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 631 | ALOGE << "Insufficient output buffer size: " << output_buffer_size; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | jobjectArray output_buffers = reinterpret_cast<jobjectArray>(GetObjectField( |
| 635 | jni, *j_media_codec_video_decoder_, j_output_buffers_field_)); |
| 636 | jobject output_buffer = |
| 637 | jni->GetObjectArrayElement(output_buffers, output_buffer_index); |
| 638 | uint8_t* payload = reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress( |
| 639 | output_buffer)); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 640 | if (CheckException(jni)) { |
| 641 | return false; |
| 642 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 643 | payload += output_buffer_offset; |
| 644 | |
| 645 | // Create yuv420 frame. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 646 | frame_buffer = decoded_frame_pool_.CreateBuffer(width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 647 | if (color_format == COLOR_FormatYUV420Planar) { |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 648 | RTC_CHECK_EQ(0, stride % 2); |
| 649 | RTC_CHECK_EQ(0, slice_height % 2); |
| 650 | const int uv_stride = stride / 2; |
| 651 | const int u_slice_height = slice_height / 2; |
| 652 | const uint8_t* y_ptr = payload; |
| 653 | const uint8_t* u_ptr = y_ptr + stride * slice_height; |
| 654 | const uint8_t* v_ptr = u_ptr + uv_stride * u_slice_height; |
| 655 | libyuv::I420Copy(y_ptr, stride, |
| 656 | u_ptr, uv_stride, |
| 657 | v_ptr, uv_stride, |
| 658 | frame_buffer->MutableData(webrtc::kYPlane), |
| 659 | frame_buffer->stride(webrtc::kYPlane), |
| 660 | frame_buffer->MutableData(webrtc::kUPlane), |
| 661 | frame_buffer->stride(webrtc::kUPlane), |
| 662 | frame_buffer->MutableData(webrtc::kVPlane), |
| 663 | frame_buffer->stride(webrtc::kVPlane), |
| 664 | width, height); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 665 | } else { |
| 666 | // All other supported formats are nv12. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 667 | const uint8_t* y_ptr = payload; |
| 668 | const uint8_t* uv_ptr = y_ptr + stride * slice_height; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 669 | libyuv::NV12ToI420( |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 670 | y_ptr, stride, |
| 671 | uv_ptr, stride, |
| 672 | frame_buffer->MutableData(webrtc::kYPlane), |
| 673 | frame_buffer->stride(webrtc::kYPlane), |
| 674 | frame_buffer->MutableData(webrtc::kUPlane), |
| 675 | frame_buffer->stride(webrtc::kUPlane), |
| 676 | frame_buffer->MutableData(webrtc::kVPlane), |
| 677 | frame_buffer->stride(webrtc::kVPlane), |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 678 | width, height); |
| 679 | } |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 680 | // Return output byte buffer back to codec. |
| 681 | jni->CallVoidMethod( |
| 682 | *j_media_codec_video_decoder_, |
| 683 | j_return_decoded_byte_buffer_method_, |
| 684 | output_buffer_index); |
| 685 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 686 | ALOGE << "returnDecodedByteBuffer error"; |
magjed | 44bf6f5 | 2015-10-03 02:08:00 -0700 | [diff] [blame] | 687 | return false; |
| 688 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 689 | } |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 690 | VideoFrame decoded_frame(frame_buffer, 0, 0, webrtc::kVideoRotation_0); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 691 | |
| 692 | // Get frame timestamps from a queue. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 693 | if (timestamps_.size() > 0) { |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 694 | decoded_frame.set_timestamp(timestamps_.front()); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 695 | timestamps_.erase(timestamps_.begin()); |
| 696 | } |
| 697 | if (ntp_times_ms_.size() > 0) { |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 698 | decoded_frame.set_ntp_time_ms(ntp_times_ms_.front()); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 699 | ntp_times_ms_.erase(ntp_times_ms_.begin()); |
| 700 | } |
| 701 | int64_t frame_decoding_time_ms = 0; |
| 702 | if (frame_rtc_times_ms_.size() > 0) { |
| 703 | frame_decoding_time_ms = GetCurrentTimeMs() - frame_rtc_times_ms_.front(); |
| 704 | frame_rtc_times_ms_.erase(frame_rtc_times_ms_.begin()); |
| 705 | } |
glaznev | e55c42c | 2015-10-28 10:30:32 -0700 | [diff] [blame] | 706 | if (frames_decoded_ < kMaxDecodedLogFrames) { |
| 707 | ALOGD << "Decoder frame out # " << frames_decoded_ << ". " << width << |
| 708 | " x " << height << ". " << stride << " x " << slice_height << |
| 709 | ". Color: " << color_format << ". TS:" << (int)output_timestamps_ms << |
| 710 | ". DecTime: " << (int)frame_decoding_time_ms; |
| 711 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 712 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 713 | // Calculate and print decoding statistics - every 3 seconds. |
| 714 | frames_decoded_++; |
| 715 | current_frames_++; |
| 716 | current_decoding_time_ms_ += frame_decoding_time_ms; |
| 717 | int statistic_time_ms = GetCurrentTimeMs() - start_time_ms_; |
| 718 | if (statistic_time_ms >= kMediaCodecStatisticsIntervalMs && |
| 719 | current_frames_ > 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 720 | ALOGD << "Decoded frames: " << frames_decoded_ << ". Bitrate: " << |
| 721 | (current_bytes_ * 8 / statistic_time_ms) << " kbps, fps: " << |
| 722 | ((current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms) |
| 723 | << ". decTime: " << (current_decoding_time_ms_ / current_frames_) << |
| 724 | " for last " << statistic_time_ms << " ms."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 725 | start_time_ms_ = GetCurrentTimeMs(); |
| 726 | current_frames_ = 0; |
| 727 | current_bytes_ = 0; |
| 728 | current_decoding_time_ms_ = 0; |
| 729 | } |
| 730 | |
| 731 | // Callback - output decoded frame. |
Magnus Jedvert | bbda54e | 2015-09-30 16:06:37 +0200 | [diff] [blame] | 732 | const int32_t callback_status = callback_->Decoded(decoded_frame); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 733 | if (callback_status > 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 734 | ALOGE << "callback error"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | return true; |
| 738 | } |
| 739 | |
| 740 | int32_t MediaCodecVideoDecoder::RegisterDecodeCompleteCallback( |
| 741 | DecodedImageCallback* callback) { |
| 742 | callback_ = callback; |
| 743 | return WEBRTC_VIDEO_CODEC_OK; |
| 744 | } |
| 745 | |
| 746 | int32_t MediaCodecVideoDecoder::Reset() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 747 | ALOGD << "DecoderReset"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 748 | if (!inited_) { |
| 749 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 750 | } |
| 751 | return InitDecode(&codec_, 1); |
| 752 | } |
| 753 | |
| 754 | void MediaCodecVideoDecoder::OnMessage(rtc::Message* msg) { |
| 755 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 756 | ScopedLocalRefFrame local_ref_frame(jni); |
| 757 | if (!inited_) { |
| 758 | return; |
| 759 | } |
| 760 | // We only ever send one message to |this| directly (not through a Bind()'d |
| 761 | // functor), so expect no ID/data. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 762 | RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
| 763 | RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 764 | CheckOnCodecThread(); |
| 765 | |
| 766 | if (!DeliverPendingOutputs(jni, 0)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 767 | ALOGE << "OnMessage: DeliverPendingOutputs error"; |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 768 | ProcessHWErrorOnCodecThread(); |
| 769 | return; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 770 | } |
| 771 | codec_thread_->PostDelayed(kMediaCodecPollMs, this); |
| 772 | } |
| 773 | |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 774 | MediaCodecVideoDecoderFactory::MediaCodecVideoDecoderFactory() : |
| 775 | render_egl_context_(NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 776 | ALOGD << "MediaCodecVideoDecoderFactory ctor"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 777 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 778 | ScopedLocalRefFrame local_ref_frame(jni); |
| 779 | jclass j_decoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoDecoder"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 780 | supported_codec_types_.clear(); |
| 781 | |
| 782 | bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 783 | j_decoder_class, |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 784 | GetStaticMethodID(jni, j_decoder_class, "isVp8HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 785 | if (CheckException(jni)) { |
| 786 | is_vp8_hw_supported = false; |
| 787 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 788 | if (is_vp8_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 789 | ALOGD << "VP8 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 790 | supported_codec_types_.push_back(kVideoCodecVP8); |
| 791 | } |
| 792 | |
| 793 | bool is_h264_hw_supported = jni->CallStaticBooleanMethod( |
| 794 | j_decoder_class, |
| 795 | GetStaticMethodID(jni, j_decoder_class, "isH264HwSupported", "()Z")); |
Alex Glaznev | 782671f | 2015-06-12 16:40:44 -0700 | [diff] [blame] | 796 | if (CheckException(jni)) { |
| 797 | is_h264_hw_supported = false; |
| 798 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 799 | if (is_h264_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 800 | ALOGD << "H264 HW Decoder supported."; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 801 | supported_codec_types_.push_back(kVideoCodecH264); |
| 802 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 805 | MediaCodecVideoDecoderFactory::~MediaCodecVideoDecoderFactory() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 806 | ALOGD << "MediaCodecVideoDecoderFactory dtor"; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 807 | if (render_egl_context_) { |
| 808 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 809 | jni->DeleteGlobalRef(render_egl_context_); |
| 810 | render_egl_context_ = NULL; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | void MediaCodecVideoDecoderFactory::SetEGLContext( |
| 815 | JNIEnv* jni, jobject render_egl_context) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 816 | ALOGD << "MediaCodecVideoDecoderFactory::SetEGLContext"; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 817 | if (render_egl_context_) { |
| 818 | jni->DeleteGlobalRef(render_egl_context_); |
| 819 | render_egl_context_ = NULL; |
| 820 | } |
| 821 | if (!IsNull(jni, render_egl_context)) { |
| 822 | render_egl_context_ = jni->NewGlobalRef(render_egl_context); |
| 823 | if (CheckException(jni)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 824 | ALOGE << "error calling NewGlobalRef for EGL Context."; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 825 | render_egl_context_ = NULL; |
| 826 | } else { |
magjed | 8c425aa | 2015-10-22 16:52:39 -0700 | [diff] [blame] | 827 | jclass j_egl_context_class = |
| 828 | FindClass(jni, "javax/microedition/khronos/egl/EGLContext"); |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 829 | if (!jni->IsInstanceOf(render_egl_context_, j_egl_context_class)) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 830 | ALOGE << "Wrong EGL Context."; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 831 | jni->DeleteGlobalRef(render_egl_context_); |
| 832 | render_egl_context_ = NULL; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | if (render_egl_context_ == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 837 | ALOGW << "NULL VideoDecoder EGL context - HW surface decoding is disabled."; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 840 | |
| 841 | webrtc::VideoDecoder* MediaCodecVideoDecoderFactory::CreateVideoDecoder( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 842 | VideoCodecType type) { |
| 843 | if (supported_codec_types_.empty()) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 844 | ALOGE << "No HW video decoder for type " << (int)type; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 845 | return NULL; |
| 846 | } |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 847 | for (VideoCodecType codec_type : supported_codec_types_) { |
| 848 | if (codec_type == type) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 849 | ALOGD << "Create HW video decoder for type " << (int)type; |
Alex Glaznev | 4d2f4d1 | 2015-09-01 15:04:13 -0700 | [diff] [blame] | 850 | return new MediaCodecVideoDecoder( |
| 851 | AttachCurrentThreadIfNeeded(), type, render_egl_context_); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 854 | ALOGE << "Can not find HW video decoder for type " << (int)type; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 855 | return NULL; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | void MediaCodecVideoDecoderFactory::DestroyVideoDecoder( |
| 859 | webrtc::VideoDecoder* decoder) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 860 | ALOGD << "Destroy video decoder."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 861 | delete decoder; |
| 862 | } |
| 863 | |
| 864 | } // namespace webrtc_jni |
| 865 | |