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