glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | // NOTICE: androidmediaencoder_jni.h must be included before |
| 12 | // androidmediacodeccommon.h to avoid build errors. |
sakal | d34a711 | 2016-07-01 05:10:51 -0700 | [diff] [blame] | 13 | #include "webrtc/api/android/jni/androidmediaencoder_jni.h" |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 14 | |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 15 | #include <algorithm> |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 16 | #include <memory> |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 17 | #include <list> |
| 18 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 19 | #include "third_party/libyuv/include/libyuv/convert.h" |
| 20 | #include "third_party/libyuv/include/libyuv/convert_from.h" |
| 21 | #include "third_party/libyuv/include/libyuv/video_common.h" |
sakal | d34a711 | 2016-07-01 05:10:51 -0700 | [diff] [blame] | 22 | #include "webrtc/api/android/jni/androidmediacodeccommon.h" |
| 23 | #include "webrtc/api/android/jni/classreferenceholder.h" |
| 24 | #include "webrtc/api/android/jni/native_handle_impl.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 25 | #include "webrtc/base/bind.h" |
| 26 | #include "webrtc/base/checks.h" |
| 27 | #include "webrtc/base/logging.h" |
| 28 | #include "webrtc/base/thread.h" |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 29 | #include "webrtc/base/thread_checker.h" |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 30 | #include "webrtc/base/timeutils.h" |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 31 | #include "webrtc/common_types.h" |
kthelgason | b906172 | 2016-10-26 02:48:16 -0700 | [diff] [blame] | 32 | #include "webrtc/common_video/h264/h264_bitstream_parser.h" |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 33 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 34 | #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
| 35 | #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 36 | #include "webrtc/system_wrappers/include/field_trial.h" |
| 37 | #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 38 | |
| 39 | using rtc::Bind; |
| 40 | using rtc::Thread; |
| 41 | using rtc::ThreadManager; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 42 | |
| 43 | using webrtc::CodecSpecificInfo; |
| 44 | using webrtc::EncodedImage; |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 45 | using webrtc::VideoFrame; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 46 | using webrtc::RTPFragmentationHeader; |
| 47 | using webrtc::VideoCodec; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 48 | using webrtc::VideoCodecType; |
| 49 | using webrtc::kVideoCodecH264; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 50 | using webrtc::kVideoCodecVP8; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 51 | using webrtc::kVideoCodecVP9; |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 52 | using webrtc::QualityScaler; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 53 | |
| 54 | namespace webrtc_jni { |
| 55 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 56 | // H.264 start code length. |
| 57 | #define H264_SC_LENGTH 4 |
| 58 | // Maximum allowed NALUs in one output frame. |
| 59 | #define MAX_NALUS_PERFRAME 32 |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 60 | // Maximum supported HW video encoder fps. |
| 61 | #define MAX_VIDEO_FPS 30 |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 62 | // Maximum allowed fps value in SetRates() call. |
| 63 | #define MAX_ALLOWED_VIDEO_FPS 60 |
| 64 | // Maximum allowed frames in encoder input queue. |
| 65 | #define MAX_ENCODER_Q_SIZE 2 |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 66 | // Maximum amount of dropped frames caused by full encoder queue - exceeding |
| 67 | // this threshold means that encoder probably got stuck and need to be reset. |
| 68 | #define ENCODER_STALL_FRAMEDROP_THRESHOLD 60 |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 69 | |
| 70 | // Logging macros. |
| 71 | #define TAG_ENCODER "MediaCodecVideoEncoder" |
| 72 | #ifdef TRACK_BUFFER_TIMING |
| 73 | #define ALOGV(...) |
| 74 | __android_log_print(ANDROID_LOG_VERBOSE, TAG_ENCODER, __VA_ARGS__) |
| 75 | #else |
| 76 | #define ALOGV(...) |
| 77 | #endif |
| 78 | #define ALOGD LOG_TAG(rtc::LS_INFO, TAG_ENCODER) |
| 79 | #define ALOGW LOG_TAG(rtc::LS_WARNING, TAG_ENCODER) |
| 80 | #define ALOGE LOG_TAG(rtc::LS_ERROR, TAG_ENCODER) |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 81 | |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 82 | namespace { |
| 83 | // Maximum time limit between incoming frames before requesting a key frame. |
glaznev | 489c0d4 | 2016-10-26 10:52:56 -0700 | [diff] [blame] | 84 | const size_t kFrameDiffThresholdMs = 350; |
| 85 | const int kMinKeyFrameInterval = 6; |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 86 | } // namespace |
| 87 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 88 | // MediaCodecVideoEncoder is a webrtc::VideoEncoder implementation that uses |
| 89 | // Android's MediaCodec SDK API behind the scenes to implement (hopefully) |
| 90 | // HW-backed video encode. This C++ class is implemented as a very thin shim, |
| 91 | // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. |
| 92 | // MediaCodecVideoEncoder is created, operated, and destroyed on a single |
| 93 | // thread, currently the libjingle Worker thread. |
| 94 | class MediaCodecVideoEncoder : public webrtc::VideoEncoder, |
| 95 | public rtc::MessageHandler { |
| 96 | public: |
| 97 | virtual ~MediaCodecVideoEncoder(); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 98 | MediaCodecVideoEncoder(JNIEnv* jni, |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 99 | VideoCodecType codecType, |
| 100 | jobject egl_context); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 101 | |
| 102 | // webrtc::VideoEncoder implementation. Everything trampolines to |
| 103 | // |codec_thread_| for execution. |
| 104 | int32_t InitEncode(const webrtc::VideoCodec* codec_settings, |
| 105 | int32_t /* number_of_cores */, |
| 106 | size_t /* max_payload_size */) override; |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 107 | int32_t Encode(const webrtc::VideoFrame& input_image, |
| 108 | const webrtc::CodecSpecificInfo* /* codec_specific_info */, |
| 109 | const std::vector<webrtc::FrameType>* frame_types) override; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 110 | int32_t RegisterEncodeCompleteCallback( |
| 111 | webrtc::EncodedImageCallback* callback) override; |
| 112 | int32_t Release() override; |
| 113 | int32_t SetChannelParameters(uint32_t /* packet_loss */, |
| 114 | int64_t /* rtt */) override; |
sprang | 8f46c67 | 2016-11-09 05:09:06 -0800 | [diff] [blame] | 115 | int32_t SetRateAllocation(const webrtc::BitrateAllocation& rate_allocation, |
| 116 | uint32_t frame_rate) override; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 117 | |
| 118 | // rtc::MessageHandler implementation. |
| 119 | void OnMessage(rtc::Message* msg) override; |
| 120 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 121 | void OnDroppedFrame() override; |
| 122 | |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 123 | bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 124 | const char* ImplementationName() const override; |
| 125 | |
| 126 | private: |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 127 | // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and |
| 128 | // InitEncodeOnCodecThread() in an attempt to restore the codec to an |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 129 | // operable state. Necessary after all manner of OMX-layer errors. |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 130 | // Returns true if the codec was reset successfully. |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 131 | bool ResetCodecOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 132 | |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 133 | // Fallback to a software encoder if one is supported else try to reset the |
| 134 | // encoder. Called with |reset_if_fallback_unavailable| equal to false from |
| 135 | // init/release encoder so that we don't go into infinite recursion. |
| 136 | // Returns true if the codec was reset successfully. |
| 137 | bool ProcessHWErrorOnCodecThread(bool reset_if_fallback_unavailable); |
| 138 | |
| 139 | // Calls ProcessHWErrorOnCodecThread(true). Returns |
| 140 | // WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE if sw_fallback_required_ was set or |
| 141 | // WEBRTC_VIDEO_CODEC_ERROR otherwise. |
| 142 | int32_t ProcessHWErrorOnEncodeOnCodecThread(); |
| 143 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 144 | // Implementation of webrtc::VideoEncoder methods above, all running on the |
| 145 | // codec thread exclusively. |
| 146 | // |
| 147 | // If width==0 then this is assumed to be a re-initialization and the |
| 148 | // previously-current values are reused instead of the passed parameters |
| 149 | // (makes it easier to reason about thread-safety). |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 150 | int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps, |
| 151 | bool use_surface); |
| 152 | // Reconfigure to match |frame| in width, height. Also reconfigures the |
| 153 | // encoder if |frame| is a texture/byte buffer and the encoder is initialized |
| 154 | // for byte buffer/texture. Returns false if reconfiguring fails. |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 155 | bool MaybeReconfigureEncoderOnCodecThread(const webrtc::VideoFrame& frame); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 156 | int32_t EncodeOnCodecThread( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 157 | const webrtc::VideoFrame& input_image, |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 158 | const std::vector<webrtc::FrameType>* frame_types, |
| 159 | const int64_t frame_input_time_ms); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 160 | bool EncodeByteBufferOnCodecThread(JNIEnv* jni, |
| 161 | bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 162 | bool EncodeTextureOnCodecThread(JNIEnv* jni, |
| 163 | bool key_frame, const webrtc::VideoFrame& frame); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 164 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 165 | int32_t RegisterEncodeCompleteCallbackOnCodecThread( |
| 166 | webrtc::EncodedImageCallback* callback); |
| 167 | int32_t ReleaseOnCodecThread(); |
| 168 | int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); |
Peter Boström | 53edac6 | 2016-04-27 00:08:34 +0200 | [diff] [blame] | 169 | void OnDroppedFrameOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 170 | |
| 171 | // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. |
| 172 | int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); |
| 173 | jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); |
| 174 | bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); |
| 175 | jlong GetOutputBufferInfoPresentationTimestampUs( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 176 | JNIEnv* jni, jobject j_output_buffer_info); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 177 | |
| 178 | // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
| 179 | // true on success. |
| 180 | bool DeliverPendingOutputs(JNIEnv* jni); |
| 181 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 182 | // Search for H.264 start codes. |
| 183 | int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
| 184 | |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 185 | // Displays encoder statistics. |
| 186 | void LogStatistics(bool force_log); |
| 187 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 188 | // Type of video codec. |
| 189 | VideoCodecType codecType_; |
| 190 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 191 | // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
| 192 | // |codec_thread_| synchronously. |
| 193 | webrtc::EncodedImageCallback* callback_; |
| 194 | |
| 195 | // State that is constant for the lifetime of this object once the ctor |
| 196 | // returns. |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 197 | std::unique_ptr<Thread> |
| 198 | codec_thread_; // Thread on which to operate MediaCodec. |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 199 | rtc::ThreadChecker codec_thread_checker_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 200 | ScopedGlobalRef<jclass> j_media_codec_video_encoder_class_; |
| 201 | ScopedGlobalRef<jobject> j_media_codec_video_encoder_; |
| 202 | jmethodID j_init_encode_method_; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 203 | jmethodID j_get_input_buffers_method_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 204 | jmethodID j_dequeue_input_buffer_method_; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 205 | jmethodID j_encode_buffer_method_; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 206 | jmethodID j_encode_texture_method_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 207 | jmethodID j_release_method_; |
| 208 | jmethodID j_set_rates_method_; |
| 209 | jmethodID j_dequeue_output_buffer_method_; |
| 210 | jmethodID j_release_output_buffer_method_; |
| 211 | jfieldID j_color_format_field_; |
| 212 | jfieldID j_info_index_field_; |
| 213 | jfieldID j_info_buffer_field_; |
| 214 | jfieldID j_info_is_key_frame_field_; |
| 215 | jfieldID j_info_presentation_timestamp_us_field_; |
| 216 | |
| 217 | // State that is valid only between InitEncode() and the next Release(). |
| 218 | // Touched only on codec_thread_ so no explicit synchronization necessary. |
| 219 | int width_; // Frame width in pixels. |
| 220 | int height_; // Frame height in pixels. |
| 221 | bool inited_; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 222 | bool use_surface_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 223 | uint16_t picture_id_; |
| 224 | enum libyuv::FourCC encoder_fourcc_; // Encoder color space format. |
| 225 | int last_set_bitrate_kbps_; // Last-requested bitrate in kbps. |
| 226 | int last_set_fps_; // Last-requested frame rate. |
| 227 | int64_t current_timestamp_us_; // Current frame timestamps in us. |
| 228 | int frames_received_; // Number of frames received by encoder. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 229 | int frames_encoded_; // Number of frames encoded by encoder. |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 230 | int frames_dropped_media_encoder_; // Number of frames dropped by encoder. |
| 231 | // Number of dropped frames caused by full queue. |
| 232 | int consecutive_full_queue_frame_drops_; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 233 | int64_t stat_start_time_ms_; // Start time for statistics. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 234 | int current_frames_; // Number of frames in the current statistics interval. |
| 235 | int current_bytes_; // Encoded bytes in the current statistics interval. |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 236 | int current_acc_qp_; // Accumulated QP in the current statistics interval. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 237 | int current_encoding_time_ms_; // Overall encoding time in the current second |
| 238 | int64_t last_input_timestamp_ms_; // Timestamp of last received yuv frame. |
| 239 | int64_t last_output_timestamp_ms_; // Timestamp of last encoded frame. |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 240 | |
| 241 | struct InputFrameInfo { |
| 242 | InputFrameInfo(int64_t encode_start_time, |
| 243 | int32_t frame_timestamp, |
| 244 | int64_t frame_render_time_ms, |
| 245 | webrtc::VideoRotation rotation) |
| 246 | : encode_start_time(encode_start_time), |
| 247 | frame_timestamp(frame_timestamp), |
| 248 | frame_render_time_ms(frame_render_time_ms), |
| 249 | rotation(rotation) {} |
| 250 | // Time when video frame is sent to encoder input. |
| 251 | const int64_t encode_start_time; |
| 252 | |
| 253 | // Input frame information. |
| 254 | const int32_t frame_timestamp; |
| 255 | const int64_t frame_render_time_ms; |
| 256 | const webrtc::VideoRotation rotation; |
| 257 | }; |
| 258 | std::list<InputFrameInfo> input_frame_infos_; |
| 259 | int32_t output_timestamp_; // Last output frame timestamp from |
| 260 | // |input_frame_infos_|. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 261 | int64_t output_render_time_ms_; // Last output frame render time from |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 262 | // |input_frame_infos_|. |
| 263 | webrtc::VideoRotation output_rotation_; // Last output frame rotation from |
| 264 | // |input_frame_infos_|. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 265 | // Frame size in bytes fed to MediaCodec. |
| 266 | int yuv_size_; |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 267 | // True only when between a callback_->OnEncodedImage() call return a positive |
| 268 | // value and the next Encode() call being ignored. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 269 | bool drop_next_input_frame_; |
| 270 | // Global references; must be deleted in Release(). |
| 271 | std::vector<jobject> input_buffers_; |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 272 | QualityScaler quality_scaler_; |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 273 | // Dynamic resolution change, off by default. |
| 274 | bool scale_; |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 275 | |
| 276 | // H264 bitstream parser, used to extract QP from encoded bitstreams. |
| 277 | webrtc::H264BitstreamParser h264_bitstream_parser_; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 278 | |
| 279 | // VP9 variables to populate codec specific structure. |
| 280 | webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for |
| 281 | // non-flexible VP9 mode. |
| 282 | uint8_t tl0_pic_idx_; |
| 283 | size_t gof_idx_; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 284 | |
| 285 | // EGL context - owned by factory, should not be allocated/destroyed |
| 286 | // by MediaCodecVideoEncoder. |
| 287 | jobject egl_context_; |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 288 | |
| 289 | // Temporary fix for VP8. |
| 290 | // Sends a key frame if frames are largely spaced apart (possibly |
| 291 | // corresponding to a large image change). |
| 292 | int64_t last_frame_received_ms_; |
| 293 | int frames_received_since_last_key_; |
| 294 | webrtc::VideoCodecMode codec_mode_; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 295 | |
| 296 | bool sw_fallback_required_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { |
| 300 | // Call Release() to ensure no more callbacks to us after we are deleted. |
| 301 | Release(); |
| 302 | } |
| 303 | |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 304 | MediaCodecVideoEncoder::MediaCodecVideoEncoder(JNIEnv* jni, |
| 305 | VideoCodecType codecType, |
| 306 | jobject egl_context) |
| 307 | : codecType_(codecType), |
| 308 | callback_(NULL), |
| 309 | codec_thread_(new Thread()), |
| 310 | j_media_codec_video_encoder_class_( |
| 311 | jni, |
| 312 | FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), |
| 313 | j_media_codec_video_encoder_( |
| 314 | jni, |
| 315 | jni->NewObject(*j_media_codec_video_encoder_class_, |
| 316 | GetMethodID(jni, |
| 317 | *j_media_codec_video_encoder_class_, |
| 318 | "<init>", |
| 319 | "()V"))), |
| 320 | inited_(false), |
| 321 | use_surface_(false), |
| 322 | picture_id_(0), |
| 323 | egl_context_(egl_context), |
| 324 | sw_fallback_required_(false) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 325 | ScopedLocalRefFrame local_ref_frame(jni); |
| 326 | // It would be nice to avoid spinning up a new thread per MediaCodec, and |
| 327 | // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug |
| 328 | // 2732 means that deadlocks abound. This class synchronously trampolines |
| 329 | // to |codec_thread_|, so if anything else can be coming to _us_ from |
| 330 | // |codec_thread_|, or from any thread holding the |_sendCritSect| described |
| 331 | // in the bug, we have a problem. For now work around that with a dedicated |
| 332 | // thread. |
| 333 | codec_thread_->SetName("MediaCodecVideoEncoder", NULL); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 334 | RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 335 | codec_thread_checker_.DetachFromThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 336 | jclass j_output_buffer_info_class = |
| 337 | FindClass(jni, "org/webrtc/MediaCodecVideoEncoder$OutputBufferInfo"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 338 | j_init_encode_method_ = GetMethodID( |
| 339 | jni, |
| 340 | *j_media_codec_video_encoder_class_, |
| 341 | "initEncode", |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 342 | "(Lorg/webrtc/MediaCodecVideoEncoder$VideoCodecType;" |
perkj | 48477c1 | 2015-12-18 00:34:37 -0800 | [diff] [blame] | 343 | "IIIILorg/webrtc/EglBase14$Context;)Z"); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 344 | j_get_input_buffers_method_ = GetMethodID( |
| 345 | jni, |
| 346 | *j_media_codec_video_encoder_class_, |
| 347 | "getInputBuffers", |
| 348 | "()[Ljava/nio/ByteBuffer;"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 349 | j_dequeue_input_buffer_method_ = GetMethodID( |
| 350 | jni, *j_media_codec_video_encoder_class_, "dequeueInputBuffer", "()I"); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 351 | j_encode_buffer_method_ = GetMethodID( |
| 352 | jni, *j_media_codec_video_encoder_class_, "encodeBuffer", "(ZIIJ)Z"); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 353 | j_encode_texture_method_ = GetMethodID( |
| 354 | jni, *j_media_codec_video_encoder_class_, "encodeTexture", |
| 355 | "(ZI[FJ)Z"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 356 | j_release_method_ = |
| 357 | GetMethodID(jni, *j_media_codec_video_encoder_class_, "release", "()V"); |
| 358 | j_set_rates_method_ = GetMethodID( |
| 359 | jni, *j_media_codec_video_encoder_class_, "setRates", "(II)Z"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 360 | j_dequeue_output_buffer_method_ = GetMethodID( |
| 361 | jni, |
| 362 | *j_media_codec_video_encoder_class_, |
| 363 | "dequeueOutputBuffer", |
| 364 | "()Lorg/webrtc/MediaCodecVideoEncoder$OutputBufferInfo;"); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 365 | j_release_output_buffer_method_ = GetMethodID( |
| 366 | jni, *j_media_codec_video_encoder_class_, "releaseOutputBuffer", "(I)Z"); |
| 367 | |
| 368 | j_color_format_field_ = |
| 369 | GetFieldID(jni, *j_media_codec_video_encoder_class_, "colorFormat", "I"); |
| 370 | j_info_index_field_ = |
| 371 | GetFieldID(jni, j_output_buffer_info_class, "index", "I"); |
| 372 | j_info_buffer_field_ = GetFieldID( |
| 373 | jni, j_output_buffer_info_class, "buffer", "Ljava/nio/ByteBuffer;"); |
| 374 | j_info_is_key_frame_field_ = |
| 375 | GetFieldID(jni, j_output_buffer_info_class, "isKeyFrame", "Z"); |
| 376 | j_info_presentation_timestamp_us_field_ = GetFieldID( |
| 377 | jni, j_output_buffer_info_class, "presentationTimestampUs", "J"); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 378 | if (CheckException(jni)) { |
| 379 | ALOGW << "MediaCodecVideoEncoder ctor failed."; |
| 380 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 381 | } |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 382 | srand(time(NULL)); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 383 | AllowBlockingCalls(); |
| 384 | } |
| 385 | |
| 386 | int32_t MediaCodecVideoEncoder::InitEncode( |
| 387 | const webrtc::VideoCodec* codec_settings, |
| 388 | int32_t /* number_of_cores */, |
| 389 | size_t /* max_payload_size */) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 390 | if (codec_settings == NULL) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 391 | ALOGE << "NULL VideoCodec instance"; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 392 | return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 393 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 394 | // Factory should guard against other codecs being used with us. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 395 | RTC_CHECK(codec_settings->codecType == codecType_) |
| 396 | << "Unsupported codec " << codec_settings->codecType << " for " |
| 397 | << codecType_; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 398 | if (sw_fallback_required_) { |
| 399 | return WEBRTC_VIDEO_CODEC_OK; |
| 400 | } |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 401 | codec_mode_ = codec_settings->mode; |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 402 | int init_width = codec_settings->width; |
| 403 | int init_height = codec_settings->height; |
Peter Boström | c5ad0c8 | 2016-06-30 16:11:34 +0200 | [diff] [blame] | 404 | // Scaling is disabled for VP9, but optionally enabled for VP8. |
| 405 | // TODO(pbos): Extract automaticResizeOn out of VP8 settings. |
| 406 | scale_ = false; |
| 407 | if (codecType_ == kVideoCodecVP8) { |
| 408 | scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn; |
| 409 | } else if (codecType_ != kVideoCodecVP9) { |
| 410 | scale_ = true; |
| 411 | } |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 412 | |
| 413 | ALOGD << "InitEncode request: " << init_width << " x " << init_height; |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 414 | ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 415 | |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 416 | if (scale_) { |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 417 | if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecH264) { |
| 418 | quality_scaler_.Init(codecType_, codec_settings->startBitrate, |
| 419 | codec_settings->width, codec_settings->height, |
pbos | cbac40d | 2016-04-13 02:51:02 -0700 | [diff] [blame] | 420 | codec_settings->maxFramerate); |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 421 | } else { |
| 422 | // When adding codec support to additional hardware codecs, also configure |
| 423 | // their QP thresholds for scaling. |
| 424 | RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 425 | scale_ = false; |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 426 | } |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 427 | QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); |
Peter Boström | 926dfcd | 2016-04-14 14:48:10 +0200 | [diff] [blame] | 428 | init_width = res.width; |
| 429 | init_height = res.height; |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 430 | ALOGD << "Scaled resolution: " << init_width << " x " << init_height; |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 431 | } |
Alex Glaznev | a9d0892 | 2016-02-19 15:24:06 -0800 | [diff] [blame] | 432 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 433 | return codec_thread_->Invoke<int32_t>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 434 | RTC_FROM_HERE, |
| 435 | Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, |
| 436 | init_height, codec_settings->startBitrate, |
skvlad | 3abb764 | 2016-06-16 12:08:03 -0700 | [diff] [blame] | 437 | codec_settings->maxFramerate, |
| 438 | codec_settings->expect_encode_from_texture)); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | int32_t MediaCodecVideoEncoder::Encode( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 442 | const webrtc::VideoFrame& frame, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 443 | const webrtc::CodecSpecificInfo* /* codec_specific_info */, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 444 | const std::vector<webrtc::FrameType>* frame_types) { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 445 | return codec_thread_->Invoke<int32_t>( |
| 446 | RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::EncodeOnCodecThread, this, |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 447 | frame, frame_types, rtc::TimeMillis())); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallback( |
| 451 | webrtc::EncodedImageCallback* callback) { |
| 452 | return codec_thread_->Invoke<int32_t>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 453 | RTC_FROM_HERE, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 454 | Bind(&MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread, |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 455 | this, callback)); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | int32_t MediaCodecVideoEncoder::Release() { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 459 | ALOGD << "EncoderRelease request"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 460 | return codec_thread_->Invoke<int32_t>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 461 | RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::ReleaseOnCodecThread, this)); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | int32_t MediaCodecVideoEncoder::SetChannelParameters(uint32_t /* packet_loss */, |
| 465 | int64_t /* rtt */) { |
| 466 | return WEBRTC_VIDEO_CODEC_OK; |
| 467 | } |
| 468 | |
sprang | 8f46c67 | 2016-11-09 05:09:06 -0800 | [diff] [blame] | 469 | int32_t MediaCodecVideoEncoder::SetRateAllocation( |
| 470 | const webrtc::BitrateAllocation& rate_allocation, |
| 471 | uint32_t frame_rate) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 472 | return codec_thread_->Invoke<int32_t>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 473 | RTC_FROM_HERE, Bind(&MediaCodecVideoEncoder::SetRatesOnCodecThread, this, |
sprang | 8f46c67 | 2016-11-09 05:09:06 -0800 | [diff] [blame] | 474 | rate_allocation.get_sum_kbps(), frame_rate)); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void MediaCodecVideoEncoder::OnMessage(rtc::Message* msg) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 478 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 479 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 480 | ScopedLocalRefFrame local_ref_frame(jni); |
| 481 | |
| 482 | // We only ever send one message to |this| directly (not through a Bind()'d |
| 483 | // functor), so expect no ID/data. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 484 | RTC_CHECK(!msg->message_id) << "Unexpected message!"; |
| 485 | RTC_CHECK(!msg->pdata) << "Unexpected message!"; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 486 | if (!inited_) { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | // It would be nice to recover from a failure here if one happened, but it's |
| 491 | // unclear how to signal such a failure to the app, so instead we stay silent |
| 492 | // about it and let the next app-called API method reveal the borkedness. |
| 493 | DeliverPendingOutputs(jni); |
sakal | 4745680 | 2016-05-24 00:16:58 -0700 | [diff] [blame] | 494 | |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 495 | // If there aren't more frames to deliver, we can start polling at lower rate. |
| 496 | if (input_frame_infos_.empty()) { |
| 497 | codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollNoFramesMs, this); |
sakal | 4745680 | 2016-05-24 00:16:58 -0700 | [diff] [blame] | 498 | } else { |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 499 | codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
sakal | 4745680 | 2016-05-24 00:16:58 -0700 | [diff] [blame] | 500 | } |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 501 | |
| 502 | // Call log statistics here so it's called even if no frames are being |
| 503 | // delivered. |
| 504 | LogStatistics(false); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 505 | } |
| 506 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 507 | bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { |
| 508 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 509 | ALOGE << "ResetOnCodecThread"; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 510 | if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK) { |
| 511 | ALOGE << "Releasing codec failed during reset."; |
| 512 | return false; |
| 513 | } |
| 514 | if (InitEncodeOnCodecThread(width_, height_, 0, 0, false) != |
| 515 | WEBRTC_VIDEO_CODEC_OK) { |
| 516 | ALOGE << "Initializing encoder failed during reset."; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 517 | return false; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 518 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 519 | return true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 520 | } |
| 521 | |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 522 | bool MediaCodecVideoEncoder::ProcessHWErrorOnCodecThread( |
| 523 | bool reset_if_fallback_unavailable) { |
| 524 | ALOGE << "ProcessHWErrorOnCodecThread"; |
| 525 | if (VideoEncoder::IsSupportedSoftware( |
| 526 | VideoEncoder::CodecToEncoderType(codecType_))) { |
| 527 | ALOGE << "Fallback to SW encoder."; |
| 528 | sw_fallback_required_ = true; |
| 529 | return false; |
| 530 | } else if (reset_if_fallback_unavailable) { |
| 531 | ALOGE << "Reset encoder."; |
| 532 | return ResetCodecOnCodecThread(); |
| 533 | } |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | int32_t MediaCodecVideoEncoder::ProcessHWErrorOnEncodeOnCodecThread() { |
| 538 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 539 | return sw_fallback_required_ ? WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE |
| 540 | : WEBRTC_VIDEO_CODEC_ERROR; |
| 541 | } |
| 542 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 543 | int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 544 | int width, int height, int kbps, int fps, bool use_surface) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 545 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 546 | if (sw_fallback_required_) { |
| 547 | return WEBRTC_VIDEO_CODEC_OK; |
| 548 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 549 | RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 550 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 551 | ScopedLocalRefFrame local_ref_frame(jni); |
| 552 | |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 553 | ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << |
| 554 | width << " x " << height << ". Bitrate: " << kbps << |
| 555 | " kbps. Fps: " << fps; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 556 | if (kbps == 0) { |
| 557 | kbps = last_set_bitrate_kbps_; |
| 558 | } |
| 559 | if (fps == 0) { |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 560 | fps = MAX_VIDEO_FPS; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | width_ = width; |
| 564 | height_ = height; |
| 565 | last_set_bitrate_kbps_ = kbps; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 566 | last_set_fps_ = (fps < MAX_VIDEO_FPS) ? fps : MAX_VIDEO_FPS; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 567 | yuv_size_ = width_ * height_ * 3 / 2; |
| 568 | frames_received_ = 0; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 569 | frames_encoded_ = 0; |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 570 | frames_dropped_media_encoder_ = 0; |
| 571 | consecutive_full_queue_frame_drops_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 572 | current_timestamp_us_ = 0; |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 573 | stat_start_time_ms_ = rtc::TimeMillis(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 574 | current_frames_ = 0; |
| 575 | current_bytes_ = 0; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 576 | current_acc_qp_ = 0; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 577 | current_encoding_time_ms_ = 0; |
| 578 | last_input_timestamp_ms_ = -1; |
| 579 | last_output_timestamp_ms_ = -1; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 580 | output_timestamp_ = 0; |
| 581 | output_render_time_ms_ = 0; |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 582 | input_frame_infos_.clear(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 583 | drop_next_input_frame_ = false; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 584 | use_surface_ = use_surface; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 585 | picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 586 | gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); |
| 587 | tl0_pic_idx_ = static_cast<uint8_t>(rand()); |
| 588 | gof_idx_ = 0; |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 589 | last_frame_received_ms_ = -1; |
| 590 | frames_received_since_last_key_ = kMinKeyFrameInterval; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 591 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 592 | // We enforce no extra stride/padding in the format creation step. |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 593 | jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 594 | jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 595 | const bool encode_status = jni->CallBooleanMethod( |
| 596 | *j_media_codec_video_encoder_, j_init_encode_method_, |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 597 | j_video_codec_enum, width, height, kbps, fps, |
| 598 | (use_surface ? egl_context_ : nullptr)); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 599 | if (!encode_status) { |
| 600 | ALOGE << "Failed to configure encoder."; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 601 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 602 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 603 | } |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 604 | if (CheckException(jni)) { |
| 605 | ALOGE << "Exception in init encode."; |
| 606 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
| 607 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 608 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 609 | |
Per | 598242a | 2015-11-26 14:28:55 +0100 | [diff] [blame] | 610 | if (!use_surface) { |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 611 | jobjectArray input_buffers = reinterpret_cast<jobjectArray>( |
| 612 | jni->CallObjectMethod(*j_media_codec_video_encoder_, |
| 613 | j_get_input_buffers_method_)); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 614 | if (CheckException(jni)) { |
| 615 | ALOGE << "Exception in get input buffers."; |
| 616 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
| 617 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 618 | } |
| 619 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 620 | if (IsNull(jni, input_buffers)) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 621 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 622 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 623 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 624 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 625 | switch (GetIntField(jni, *j_media_codec_video_encoder_, |
| 626 | j_color_format_field_)) { |
| 627 | case COLOR_FormatYUV420Planar: |
| 628 | encoder_fourcc_ = libyuv::FOURCC_YU12; |
| 629 | break; |
| 630 | case COLOR_FormatYUV420SemiPlanar: |
| 631 | case COLOR_QCOM_FormatYUV420SemiPlanar: |
| 632 | case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: |
| 633 | encoder_fourcc_ = libyuv::FOURCC_NV12; |
| 634 | break; |
| 635 | default: |
| 636 | LOG(LS_ERROR) << "Wrong color format."; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 637 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 638 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 639 | } |
| 640 | size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
| 641 | RTC_CHECK(input_buffers_.empty()) |
| 642 | << "Unexpected double InitEncode without Release"; |
| 643 | input_buffers_.resize(num_input_buffers); |
| 644 | for (size_t i = 0; i < num_input_buffers; ++i) { |
| 645 | input_buffers_[i] = |
| 646 | jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
| 647 | int64_t yuv_buffer_capacity = |
| 648 | jni->GetDirectBufferCapacity(input_buffers_[i]); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 649 | if (CheckException(jni)) { |
| 650 | ALOGE << "Exception in get direct buffer capacity."; |
| 651 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
| 652 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 653 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 654 | RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; |
| 655 | } |
| 656 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 657 | |
| 658 | inited_ = true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 659 | return WEBRTC_VIDEO_CODEC_OK; |
| 660 | } |
| 661 | |
| 662 | int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 663 | const webrtc::VideoFrame& frame, |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 664 | const std::vector<webrtc::FrameType>* frame_types, |
| 665 | const int64_t frame_input_time_ms) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 666 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 667 | if (sw_fallback_required_) |
| 668 | return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 669 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 670 | ScopedLocalRefFrame local_ref_frame(jni); |
| 671 | |
| 672 | if (!inited_) { |
| 673 | return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 674 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 675 | |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 676 | bool send_key_frame = false; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 677 | if (codec_mode_ == webrtc::kRealtimeVideo) { |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 678 | ++frames_received_since_last_key_; |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 679 | int64_t now_ms = rtc::TimeMillis(); |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 680 | if (last_frame_received_ms_ != -1 && |
| 681 | (now_ms - last_frame_received_ms_) > kFrameDiffThresholdMs) { |
| 682 | // Add limit to prevent triggering a key for every frame for very low |
| 683 | // framerates (e.g. if frame diff > kFrameDiffThresholdMs). |
| 684 | if (frames_received_since_last_key_ > kMinKeyFrameInterval) { |
| 685 | ALOGD << "Send key, frame diff: " << (now_ms - last_frame_received_ms_); |
| 686 | send_key_frame = true; |
| 687 | } |
| 688 | frames_received_since_last_key_ = 0; |
| 689 | } |
| 690 | last_frame_received_ms_ = now_ms; |
| 691 | } |
| 692 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 693 | frames_received_++; |
| 694 | if (!DeliverPendingOutputs(jni)) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 695 | if (!ProcessHWErrorOnCodecThread( |
| 696 | true /* reset_if_fallback_unavailable */)) { |
| 697 | return sw_fallback_required_ ? WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE |
| 698 | : WEBRTC_VIDEO_CODEC_ERROR; |
| 699 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 700 | } |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 701 | if (frames_encoded_ < kMaxEncodedLogFrames) { |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 702 | ALOGD << "Encoder frame in # " << (frames_received_ - 1) |
| 703 | << ". TS: " << (int)(current_timestamp_us_ / 1000) |
| 704 | << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ |
| 705 | << ". Kbps: " << last_set_bitrate_kbps_; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 706 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 707 | |
| 708 | if (drop_next_input_frame_) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 709 | ALOGW << "Encoder drop frame - failed callback."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 710 | drop_next_input_frame_ = false; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 711 | current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 712 | frames_dropped_media_encoder_++; |
Peter Boström | 53edac6 | 2016-04-27 00:08:34 +0200 | [diff] [blame] | 713 | OnDroppedFrameOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 714 | return WEBRTC_VIDEO_CODEC_OK; |
| 715 | } |
| 716 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 717 | RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 718 | |
Peter Boström | f7704d1 | 2016-04-11 16:42:40 +0200 | [diff] [blame] | 719 | // Check if we accumulated too many frames in encoder input buffers and drop |
| 720 | // frame if so. |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 721 | if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { |
| 722 | ALOGD << "Already " << input_frame_infos_.size() |
| 723 | << " frames in the queue, dropping" |
Peter Boström | f7704d1 | 2016-04-11 16:42:40 +0200 | [diff] [blame] | 724 | << ". TS: " << (int)(current_timestamp_us_ / 1000) |
| 725 | << ". Fps: " << last_set_fps_ |
| 726 | << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; |
| 727 | current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 728 | consecutive_full_queue_frame_drops_++; |
| 729 | if (consecutive_full_queue_frame_drops_ >= |
| 730 | ENCODER_STALL_FRAMEDROP_THRESHOLD) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 731 | ALOGE << "Encoder got stuck."; |
| 732 | return ProcessHWErrorOnEncodeOnCodecThread(); |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 733 | } |
Peter Boström | f7704d1 | 2016-04-11 16:42:40 +0200 | [diff] [blame] | 734 | frames_dropped_media_encoder_++; |
Peter Boström | 53edac6 | 2016-04-27 00:08:34 +0200 | [diff] [blame] | 735 | OnDroppedFrameOnCodecThread(); |
Peter Boström | f7704d1 | 2016-04-11 16:42:40 +0200 | [diff] [blame] | 736 | return WEBRTC_VIDEO_CODEC_OK; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 737 | } |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 738 | consecutive_full_queue_frame_drops_ = 0; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 739 | |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 740 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( |
| 741 | frame.video_frame_buffer()); |
Per | 598242a | 2015-11-26 14:28:55 +0100 | [diff] [blame] | 742 | if (scale_) { |
| 743 | // Check framerate before spatial resolution change. |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 744 | quality_scaler_.OnEncodeFrame(frame.width(), frame.height()); |
Per | 598242a | 2015-11-26 14:28:55 +0100 | [diff] [blame] | 745 | const webrtc::QualityScaler::Resolution scaled_resolution = |
| 746 | quality_scaler_.GetScaledResolution(); |
| 747 | if (scaled_resolution.width != frame.width() || |
| 748 | scaled_resolution.height != frame.height()) { |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 749 | if (input_buffer->native_handle() != nullptr) { |
| 750 | input_buffer = static_cast<AndroidTextureBuffer*>(input_buffer.get()) |
| 751 | ->CropScaleAndRotate(frame.width(), frame.height(), |
| 752 | 0, 0, |
| 753 | scaled_resolution.width, |
| 754 | scaled_resolution.height, |
| 755 | webrtc::kVideoRotation_0); |
Per | 598242a | 2015-11-26 14:28:55 +0100 | [diff] [blame] | 756 | } else { |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 757 | input_buffer = quality_scaler_.GetScaledBuffer(input_buffer); |
Per | 598242a | 2015-11-26 14:28:55 +0100 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | } |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 761 | |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 762 | VideoFrame input_frame(input_buffer, frame.timestamp(), |
| 763 | frame.render_time_ms(), frame.rotation()); |
| 764 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 765 | if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { |
| 766 | ALOGE << "Failed to reconfigure encoder."; |
| 767 | return WEBRTC_VIDEO_CODEC_ERROR; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 768 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 769 | |
asapersson | 1d61a51 | 2016-01-20 01:13:46 -0800 | [diff] [blame] | 770 | const bool key_frame = |
| 771 | frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 772 | bool encode_status = true; |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 773 | if (!input_frame.video_frame_buffer()->native_handle()) { |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 774 | int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
| 775 | j_dequeue_input_buffer_method_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 776 | if (CheckException(jni)) { |
| 777 | ALOGE << "Exception in dequeu input buffer."; |
| 778 | return ProcessHWErrorOnEncodeOnCodecThread(); |
| 779 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 780 | if (j_input_buffer_index == -1) { |
| 781 | // Video codec falls behind - no input buffer available. |
| 782 | ALOGW << "Encoder drop frame - no input buffers available"; |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 783 | if (frames_received_ > 1) { |
| 784 | current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 785 | frames_dropped_media_encoder_++; |
| 786 | OnDroppedFrameOnCodecThread(); |
| 787 | } else { |
| 788 | // Input buffers are not ready after codec initialization, HW is still |
| 789 | // allocating thme - this is expected and should not result in drop |
| 790 | // frame report. |
| 791 | frames_received_ = 0; |
| 792 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 793 | return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 794 | } else if (j_input_buffer_index == -2) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 795 | return ProcessHWErrorOnEncodeOnCodecThread(); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 796 | } |
| 797 | encode_status = EncodeByteBufferOnCodecThread(jni, key_frame, input_frame, |
| 798 | j_input_buffer_index); |
| 799 | } else { |
| 800 | encode_status = EncodeTextureOnCodecThread(jni, key_frame, input_frame); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 801 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 802 | |
| 803 | if (!encode_status) { |
| 804 | ALOGE << "Failed encode frame with timestamp: " << input_frame.timestamp(); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 805 | return ProcessHWErrorOnEncodeOnCodecThread(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 808 | // Save input image timestamps for later output. |
| 809 | input_frame_infos_.emplace_back( |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 810 | frame_input_time_ms, input_frame.timestamp(), |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 811 | input_frame.render_time_ms(), input_frame.rotation()); |
| 812 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 813 | last_input_timestamp_ms_ = |
| 814 | current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 815 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 816 | current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 817 | |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 818 | codec_thread_->Clear(this); |
| 819 | codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
sakal | 4745680 | 2016-05-24 00:16:58 -0700 | [diff] [blame] | 820 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 821 | if (!DeliverPendingOutputs(jni)) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 822 | return ProcessHWErrorOnEncodeOnCodecThread(); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 823 | } |
| 824 | return WEBRTC_VIDEO_CODEC_OK; |
| 825 | } |
| 826 | |
| 827 | bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread( |
| 828 | const webrtc::VideoFrame& frame) { |
| 829 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 830 | |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 831 | const bool is_texture_frame = |
| 832 | frame.video_frame_buffer()->native_handle() != nullptr; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 833 | const bool reconfigure_due_to_format = is_texture_frame != use_surface_; |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 834 | const bool reconfigure_due_to_size = |
| 835 | frame.width() != width_ || frame.height() != height_; |
| 836 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 837 | if (reconfigure_due_to_format) { |
| 838 | ALOGD << "Reconfigure encoder due to format change. " |
| 839 | << (use_surface_ ? |
| 840 | "Reconfiguring to encode from byte buffer." : |
| 841 | "Reconfiguring to encode from texture."); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 842 | LogStatistics(true); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 843 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 844 | if (reconfigure_due_to_size) { |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 845 | ALOGW << "Reconfigure encoder due to frame resolution change from " |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 846 | << width_ << " x " << height_ << " to " << frame.width() << " x " |
| 847 | << frame.height(); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 848 | LogStatistics(true); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 849 | width_ = frame.width(); |
| 850 | height_ = frame.height(); |
| 851 | } |
| 852 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 853 | if (!reconfigure_due_to_format && !reconfigure_due_to_size) |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 854 | return true; |
| 855 | |
| 856 | ReleaseOnCodecThread(); |
| 857 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 858 | return InitEncodeOnCodecThread(width_, height_, 0, 0 , is_texture_frame) == |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 859 | WEBRTC_VIDEO_CODEC_OK; |
| 860 | } |
| 861 | |
| 862 | bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni, |
| 863 | bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index) { |
| 864 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 865 | RTC_CHECK(!use_surface_); |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 866 | |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 867 | jobject j_input_buffer = input_buffers_[input_buffer_index]; |
| 868 | uint8_t* yuv_buffer = |
| 869 | reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 870 | if (CheckException(jni)) { |
| 871 | ALOGE << "Exception in get direct buffer address."; |
| 872 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 873 | return false; |
| 874 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 875 | RTC_CHECK(yuv_buffer) << "Indirect buffer??"; |
| 876 | RTC_CHECK(!libyuv::ConvertFromI420( |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 877 | frame.video_frame_buffer()->DataY(), |
| 878 | frame.video_frame_buffer()->StrideY(), |
| 879 | frame.video_frame_buffer()->DataU(), |
| 880 | frame.video_frame_buffer()->StrideU(), |
| 881 | frame.video_frame_buffer()->DataV(), |
| 882 | frame.video_frame_buffer()->StrideV(), |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 883 | yuv_buffer, width_, width_, height_, encoder_fourcc_)) |
| 884 | << "ConvertFromI420 failed"; |
| 885 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 886 | bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 887 | j_encode_buffer_method_, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 888 | key_frame, |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 889 | input_buffer_index, |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 890 | yuv_size_, |
| 891 | current_timestamp_us_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 892 | if (CheckException(jni)) { |
| 893 | ALOGE << "Exception in encode buffer."; |
| 894 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 895 | return false; |
| 896 | } |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 897 | return encode_status; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 898 | } |
| 899 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 900 | bool MediaCodecVideoEncoder::EncodeTextureOnCodecThread(JNIEnv* jni, |
| 901 | bool key_frame, const webrtc::VideoFrame& frame) { |
| 902 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 903 | RTC_CHECK(use_surface_); |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 904 | NativeHandleImpl* handle = static_cast<NativeHandleImpl*>( |
| 905 | frame.video_frame_buffer()->native_handle()); |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 906 | jfloatArray sampling_matrix = handle->sampling_matrix.ToJava(jni); |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 907 | bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 908 | j_encode_texture_method_, |
| 909 | key_frame, |
| 910 | handle->oes_texture_id, |
| 911 | sampling_matrix, |
| 912 | current_timestamp_us_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 913 | if (CheckException(jni)) { |
| 914 | ALOGE << "Exception in encode texture."; |
| 915 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 916 | return false; |
| 917 | } |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 918 | return encode_status; |
| 919 | } |
| 920 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 921 | int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( |
| 922 | webrtc::EncodedImageCallback* callback) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 923 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 924 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 925 | ScopedLocalRefFrame local_ref_frame(jni); |
| 926 | callback_ = callback; |
| 927 | return WEBRTC_VIDEO_CODEC_OK; |
| 928 | } |
| 929 | |
| 930 | int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 931 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 932 | if (!inited_) { |
| 933 | return WEBRTC_VIDEO_CODEC_OK; |
| 934 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 935 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 936 | ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << |
| 937 | frames_received_ << ". Encoded: " << frames_encoded_ << |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 938 | ". Dropped: " << frames_dropped_media_encoder_; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 939 | ScopedLocalRefFrame local_ref_frame(jni); |
| 940 | for (size_t i = 0; i < input_buffers_.size(); ++i) |
| 941 | jni->DeleteGlobalRef(input_buffers_[i]); |
| 942 | input_buffers_.clear(); |
| 943 | jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 944 | if (CheckException(jni)) { |
| 945 | ALOGE << "Exception in release."; |
| 946 | ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
| 947 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 948 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 949 | rtc::MessageQueueManager::Clear(this); |
| 950 | inited_ = false; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 951 | use_surface_ = false; |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 952 | ALOGD << "EncoderReleaseOnCodecThread done."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 953 | return WEBRTC_VIDEO_CODEC_OK; |
| 954 | } |
| 955 | |
| 956 | int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, |
| 957 | uint32_t frame_rate) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 958 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 959 | if (sw_fallback_required_) |
| 960 | return WEBRTC_VIDEO_CODEC_OK; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 961 | frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? |
| 962 | frame_rate : MAX_ALLOWED_VIDEO_FPS; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 963 | if (last_set_bitrate_kbps_ == new_bit_rate && |
| 964 | last_set_fps_ == frame_rate) { |
| 965 | return WEBRTC_VIDEO_CODEC_OK; |
| 966 | } |
glaznev | 919ff75 | 2016-01-27 15:01:03 -0800 | [diff] [blame] | 967 | if (scale_) { |
| 968 | quality_scaler_.ReportFramerate(frame_rate); |
| 969 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 970 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 971 | ScopedLocalRefFrame local_ref_frame(jni); |
| 972 | if (new_bit_rate > 0) { |
| 973 | last_set_bitrate_kbps_ = new_bit_rate; |
| 974 | } |
| 975 | if (frame_rate > 0) { |
| 976 | last_set_fps_ = frame_rate; |
| 977 | } |
| 978 | bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 979 | j_set_rates_method_, |
| 980 | last_set_bitrate_kbps_, |
| 981 | last_set_fps_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 982 | if (CheckException(jni) || !ret) { |
| 983 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 984 | return sw_fallback_required_ ? WEBRTC_VIDEO_CODEC_OK |
| 985 | : WEBRTC_VIDEO_CODEC_ERROR; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 986 | } |
| 987 | return WEBRTC_VIDEO_CODEC_OK; |
| 988 | } |
| 989 | |
| 990 | int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( |
| 991 | JNIEnv* jni, |
| 992 | jobject j_output_buffer_info) { |
| 993 | return GetIntField(jni, j_output_buffer_info, j_info_index_field_); |
| 994 | } |
| 995 | |
| 996 | jobject MediaCodecVideoEncoder::GetOutputBufferInfoBuffer( |
| 997 | JNIEnv* jni, |
| 998 | jobject j_output_buffer_info) { |
| 999 | return GetObjectField(jni, j_output_buffer_info, j_info_buffer_field_); |
| 1000 | } |
| 1001 | |
| 1002 | bool MediaCodecVideoEncoder::GetOutputBufferInfoIsKeyFrame( |
| 1003 | JNIEnv* jni, |
| 1004 | jobject j_output_buffer_info) { |
| 1005 | return GetBooleanField(jni, j_output_buffer_info, j_info_is_key_frame_field_); |
| 1006 | } |
| 1007 | |
| 1008 | jlong MediaCodecVideoEncoder::GetOutputBufferInfoPresentationTimestampUs( |
| 1009 | JNIEnv* jni, |
| 1010 | jobject j_output_buffer_info) { |
| 1011 | return GetLongField( |
| 1012 | jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); |
| 1013 | } |
| 1014 | |
| 1015 | bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { |
perkj | 9576e54 | 2015-11-12 06:43:16 -0800 | [diff] [blame] | 1016 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 1017 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1018 | while (true) { |
| 1019 | jobject j_output_buffer_info = jni->CallObjectMethod( |
| 1020 | *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 1021 | if (CheckException(jni)) { |
| 1022 | ALOGE << "Exception in set dequeue output buffer."; |
| 1023 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 1024 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 1025 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1026 | if (IsNull(jni, j_output_buffer_info)) { |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | int output_buffer_index = |
| 1031 | GetOutputBufferInfoIndex(jni, j_output_buffer_info); |
| 1032 | if (output_buffer_index == -1) { |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 1033 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1037 | // Get key and config frame flags. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1038 | jobject j_output_buffer = |
| 1039 | GetOutputBufferInfoBuffer(jni, j_output_buffer_info); |
| 1040 | bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Get frame timestamps from a queue - for non config frames only. |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 1043 | int64_t encoding_start_time_ms = 0; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1044 | int64_t frame_encoding_time_ms = 0; |
| 1045 | last_output_timestamp_ms_ = |
| 1046 | GetOutputBufferInfoPresentationTimestampUs(jni, j_output_buffer_info) / |
sakal | 4745680 | 2016-05-24 00:16:58 -0700 | [diff] [blame] | 1047 | rtc::kNumMicrosecsPerMillisec; |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 1048 | if (!input_frame_infos_.empty()) { |
| 1049 | const InputFrameInfo& frame_info = input_frame_infos_.front(); |
| 1050 | output_timestamp_ = frame_info.frame_timestamp; |
| 1051 | output_render_time_ms_ = frame_info.frame_render_time_ms; |
| 1052 | output_rotation_ = frame_info.rotation; |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 1053 | encoding_start_time_ms = frame_info.encode_start_time; |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 1054 | input_frame_infos_.pop_front(); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Extract payload. |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1058 | size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer); |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1059 | uint8_t* payload = reinterpret_cast<uint8_t*>( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1060 | jni->GetDirectBufferAddress(j_output_buffer)); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 1061 | if (CheckException(jni)) { |
| 1062 | ALOGE << "Exception in get direct buffer address."; |
| 1063 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
| 1064 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 1065 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1066 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1067 | // Callback - return encoded frame. |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 1068 | webrtc::EncodedImageCallback::Result callback_result( |
| 1069 | webrtc::EncodedImageCallback::Result::OK); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1070 | if (callback_) { |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1071 | std::unique_ptr<webrtc::EncodedImage> image( |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1072 | new webrtc::EncodedImage(payload, payload_size, payload_size)); |
| 1073 | image->_encodedWidth = width_; |
| 1074 | image->_encodedHeight = height_; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1075 | image->_timeStamp = output_timestamp_; |
| 1076 | image->capture_time_ms_ = output_render_time_ms_; |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 1077 | image->rotation_ = output_rotation_; |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 1078 | image->_frameType = |
| 1079 | (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1080 | image->_completeFrame = true; |
asapersson | 075fb4b | 2015-10-29 08:49:14 -0700 | [diff] [blame] | 1081 | image->adapt_reason_.quality_resolution_downscales = |
| 1082 | scale_ ? quality_scaler_.downscale_shift() : -1; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1083 | |
| 1084 | webrtc::CodecSpecificInfo info; |
| 1085 | memset(&info, 0, sizeof(info)); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1086 | info.codecType = codecType_; |
| 1087 | if (codecType_ == kVideoCodecVP8) { |
| 1088 | info.codecSpecific.VP8.pictureId = picture_id_; |
| 1089 | info.codecSpecific.VP8.nonReference = false; |
| 1090 | info.codecSpecific.VP8.simulcastIdx = 0; |
| 1091 | info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; |
| 1092 | info.codecSpecific.VP8.layerSync = false; |
| 1093 | info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; |
| 1094 | info.codecSpecific.VP8.keyIdx = webrtc::kNoKeyIdx; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1095 | } else if (codecType_ == kVideoCodecVP9) { |
| 1096 | if (key_frame) { |
| 1097 | gof_idx_ = 0; |
| 1098 | } |
| 1099 | info.codecSpecific.VP9.picture_id = picture_id_; |
| 1100 | info.codecSpecific.VP9.inter_pic_predicted = key_frame ? false : true; |
| 1101 | info.codecSpecific.VP9.flexible_mode = false; |
| 1102 | info.codecSpecific.VP9.ss_data_available = key_frame ? true : false; |
| 1103 | info.codecSpecific.VP9.tl0_pic_idx = tl0_pic_idx_++; |
| 1104 | info.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx; |
| 1105 | info.codecSpecific.VP9.spatial_idx = webrtc::kNoSpatialIdx; |
| 1106 | info.codecSpecific.VP9.temporal_up_switch = true; |
| 1107 | info.codecSpecific.VP9.inter_layer_predicted = false; |
| 1108 | info.codecSpecific.VP9.gof_idx = |
| 1109 | static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof); |
| 1110 | info.codecSpecific.VP9.num_spatial_layers = 1; |
| 1111 | info.codecSpecific.VP9.spatial_layer_resolution_present = false; |
| 1112 | if (info.codecSpecific.VP9.ss_data_available) { |
| 1113 | info.codecSpecific.VP9.spatial_layer_resolution_present = true; |
| 1114 | info.codecSpecific.VP9.width[0] = width_; |
| 1115 | info.codecSpecific.VP9.height[0] = height_; |
| 1116 | info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_); |
| 1117 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1118 | } |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1119 | picture_id_ = (picture_id_ + 1) & 0x7FFF; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Generate a header describing a single fragment. |
| 1122 | webrtc::RTPFragmentationHeader header; |
| 1123 | memset(&header, 0, sizeof(header)); |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1124 | if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1125 | header.VerifyAndAllocateFragmentationHeader(1); |
| 1126 | header.fragmentationOffset[0] = 0; |
| 1127 | header.fragmentationLength[0] = image->_length; |
| 1128 | header.fragmentationPlType[0] = 0; |
| 1129 | header.fragmentationTimeDiff[0] = 0; |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1130 | if (codecType_ == kVideoCodecVP8 && scale_) { |
asapersson | 86b0160 | 2015-10-20 23:55:26 -0700 | [diff] [blame] | 1131 | int qp; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1132 | if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { |
| 1133 | current_acc_qp_ += qp; |
asapersson | 86b0160 | 2015-10-20 23:55:26 -0700 | [diff] [blame] | 1134 | quality_scaler_.ReportQP(qp); |
asapersson | 24ebc44 | 2016-04-19 23:48:21 -0700 | [diff] [blame] | 1135 | image->qp_ = qp; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1136 | } |
asapersson | 86b0160 | 2015-10-20 23:55:26 -0700 | [diff] [blame] | 1137 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1138 | } else if (codecType_ == kVideoCodecH264) { |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 1139 | if (scale_) { |
| 1140 | h264_bitstream_parser_.ParseBitstream(payload, payload_size); |
| 1141 | int qp; |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1142 | if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { |
| 1143 | current_acc_qp_ += qp; |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 1144 | quality_scaler_.ReportQP(qp); |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1145 | } |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 1146 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1147 | // For H.264 search for start codes. |
| 1148 | int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; |
| 1149 | int32_t scPositionsLength = 0; |
| 1150 | int32_t scPosition = 0; |
| 1151 | while (scPositionsLength < MAX_NALUS_PERFRAME) { |
| 1152 | int32_t naluPosition = NextNaluPosition( |
| 1153 | payload + scPosition, payload_size - scPosition); |
| 1154 | if (naluPosition < 0) { |
| 1155 | break; |
| 1156 | } |
| 1157 | scPosition += naluPosition; |
| 1158 | scPositions[scPositionsLength++] = scPosition; |
| 1159 | scPosition += H264_SC_LENGTH; |
| 1160 | } |
| 1161 | if (scPositionsLength == 0) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 1162 | ALOGE << "Start code is not found!"; |
| 1163 | ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] |
| 1164 | << " " << image->_buffer[2] << " " << image->_buffer[3] |
| 1165 | << " " << image->_buffer[4] << " " << image->_buffer[5]; |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 1166 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | scPositions[scPositionsLength] = payload_size; |
| 1170 | header.VerifyAndAllocateFragmentationHeader(scPositionsLength); |
| 1171 | for (size_t i = 0; i < scPositionsLength; i++) { |
| 1172 | header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; |
| 1173 | header.fragmentationLength[i] = |
| 1174 | scPositions[i + 1] - header.fragmentationOffset[i]; |
| 1175 | header.fragmentationPlType[i] = 0; |
| 1176 | header.fragmentationTimeDiff[i] = 0; |
| 1177 | } |
| 1178 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1179 | |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 1180 | callback_result = callback_->OnEncodedImage(*image, &info, &header); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | // Return output buffer back to the encoder. |
| 1184 | bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 1185 | j_release_output_buffer_method_, |
| 1186 | output_buffer_index); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 1187 | if (CheckException(jni) || !success) { |
| 1188 | ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1189 | return false; |
| 1190 | } |
| 1191 | |
glaznev | ce5a874 | 2016-06-18 19:12:57 -0700 | [diff] [blame] | 1192 | // Print per frame statistics. |
| 1193 | if (encoding_start_time_ms > 0) { |
| 1194 | frame_encoding_time_ms = rtc::TimeMillis() - encoding_start_time_ms; |
| 1195 | } |
| 1196 | if (frames_encoded_ < kMaxEncodedLogFrames) { |
| 1197 | int current_latency = |
| 1198 | (int)(last_input_timestamp_ms_ - last_output_timestamp_ms_); |
| 1199 | ALOGD << "Encoder frame out # " << frames_encoded_ << |
| 1200 | ". Key: " << key_frame << |
| 1201 | ". Size: " << payload_size << |
| 1202 | ". TS: " << (int)last_output_timestamp_ms_ << |
| 1203 | ". Latency: " << current_latency << |
| 1204 | ". EncTime: " << frame_encoding_time_ms; |
| 1205 | } |
| 1206 | |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1207 | // Calculate and print encoding statistics - every 3 seconds. |
| 1208 | frames_encoded_++; |
| 1209 | current_frames_++; |
| 1210 | current_bytes_ += payload_size; |
| 1211 | current_encoding_time_ms_ += frame_encoding_time_ms; |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 1212 | LogStatistics(false); |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1213 | |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 1214 | // Errors in callback_result are currently ignored. |
| 1215 | if (callback_result.drop_next_frame) |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1216 | drop_next_input_frame_ = true; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1217 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1218 | return true; |
| 1219 | } |
| 1220 | |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 1221 | void MediaCodecVideoEncoder::LogStatistics(bool force_log) { |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 1222 | int statistic_time_ms = rtc::TimeMillis() - stat_start_time_ms_; |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 1223 | if ((statistic_time_ms >= kMediaCodecStatisticsIntervalMs || force_log) |
| 1224 | && statistic_time_ms > 0) { |
| 1225 | // Prevent division by zero. |
| 1226 | int current_frames_divider = current_frames_ != 0 ? current_frames_ : 1; |
| 1227 | |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 1228 | int current_bitrate = current_bytes_ * 8 / statistic_time_ms; |
| 1229 | int current_fps = |
| 1230 | (current_frames_ * 1000 + statistic_time_ms / 2) / statistic_time_ms; |
| 1231 | ALOGD << "Encoded frames: " << frames_encoded_ << |
| 1232 | ". Bitrate: " << current_bitrate << |
| 1233 | ", target: " << last_set_bitrate_kbps_ << " kbps" << |
| 1234 | ", fps: " << current_fps << |
sakal | 1fc4810 | 2016-06-14 01:53:39 -0700 | [diff] [blame] | 1235 | ", encTime: " << (current_encoding_time_ms_ / current_frames_divider) << |
| 1236 | ". QP: " << (current_acc_qp_ / current_frames_divider) << |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 1237 | " for last " << statistic_time_ms << " ms."; |
Niels Möller | d28db7f | 2016-05-10 16:31:47 +0200 | [diff] [blame] | 1238 | stat_start_time_ms_ = rtc::TimeMillis(); |
glaznev | 9429148 | 2016-02-01 13:17:18 -0800 | [diff] [blame] | 1239 | current_frames_ = 0; |
| 1240 | current_bytes_ = 0; |
| 1241 | current_acc_qp_ = 0; |
| 1242 | current_encoding_time_ms_ = 0; |
| 1243 | } |
| 1244 | } |
| 1245 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1246 | int32_t MediaCodecVideoEncoder::NextNaluPosition( |
| 1247 | uint8_t *buffer, size_t buffer_size) { |
| 1248 | if (buffer_size < H264_SC_LENGTH) { |
| 1249 | return -1; |
| 1250 | } |
| 1251 | uint8_t *head = buffer; |
| 1252 | // Set end buffer pointer to 4 bytes before actual buffer end so we can |
| 1253 | // access head[1], head[2] and head[3] in a loop without buffer overrun. |
| 1254 | uint8_t *end = buffer + buffer_size - H264_SC_LENGTH; |
| 1255 | |
| 1256 | while (head < end) { |
| 1257 | if (head[0]) { |
| 1258 | head++; |
| 1259 | continue; |
| 1260 | } |
| 1261 | if (head[1]) { // got 00xx |
| 1262 | head += 2; |
| 1263 | continue; |
| 1264 | } |
| 1265 | if (head[2]) { // got 0000xx |
| 1266 | head += 3; |
| 1267 | continue; |
| 1268 | } |
| 1269 | if (head[3] != 0x01) { // got 000000xx |
glaznev@webrtc.org | dc08a23 | 2015-03-06 23:32:20 +0000 | [diff] [blame] | 1270 | head++; // xx != 1, continue searching. |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1271 | continue; |
| 1272 | } |
| 1273 | return (int32_t)(head - buffer); |
| 1274 | } |
| 1275 | return -1; |
| 1276 | } |
| 1277 | |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 1278 | void MediaCodecVideoEncoder::OnDroppedFrame() { |
Peter Boström | 53edac6 | 2016-04-27 00:08:34 +0200 | [diff] [blame] | 1279 | // Methods running on the codec thread should call OnDroppedFrameOnCodecThread |
| 1280 | // directly. |
| 1281 | RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); |
| 1282 | codec_thread_->Invoke<void>( |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1283 | RTC_FROM_HERE, |
Peter Boström | 53edac6 | 2016-04-27 00:08:34 +0200 | [diff] [blame] | 1284 | Bind(&MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread, this)); |
| 1285 | } |
| 1286 | |
| 1287 | void MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread() { |
| 1288 | RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
glaznev | f4decb5 | 2016-01-15 13:49:22 -0800 | [diff] [blame] | 1289 | // Report dropped frame to quality_scaler_. |
Peter Boström | 2bc68c7 | 2015-09-24 16:22:28 +0200 | [diff] [blame] | 1290 | if (scale_) |
| 1291 | quality_scaler_.ReportDroppedFrame(); |
jackychen | 61b4d51 | 2015-04-21 15:30:11 -0700 | [diff] [blame] | 1292 | } |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1293 | |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 1294 | const char* MediaCodecVideoEncoder::ImplementationName() const { |
| 1295 | return "MediaCodec"; |
| 1296 | } |
| 1297 | |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 1298 | MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() |
| 1299 | : egl_context_(nullptr) { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1300 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 1301 | ScopedLocalRefFrame local_ref_frame(jni); |
| 1302 | jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder"); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1303 | supported_codecs_.clear(); |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1304 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1305 | bool is_vp8_hw_supported = jni->CallStaticBooleanMethod( |
| 1306 | j_encoder_class, |
| 1307 | GetStaticMethodID(jni, j_encoder_class, "isVp8HwSupported", "()Z")); |
| 1308 | CHECK_EXCEPTION(jni); |
| 1309 | if (is_vp8_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 1310 | ALOGD << "VP8 HW Encoder supported."; |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1311 | supported_codecs_.push_back(cricket::VideoCodec("VP8")); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1314 | bool is_vp9_hw_supported = jni->CallStaticBooleanMethod( |
| 1315 | j_encoder_class, |
| 1316 | GetStaticMethodID(jni, j_encoder_class, "isVp9HwSupported", "()Z")); |
| 1317 | CHECK_EXCEPTION(jni); |
| 1318 | if (is_vp9_hw_supported) { |
| 1319 | ALOGD << "VP9 HW Encoder supported."; |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1320 | supported_codecs_.push_back(cricket::VideoCodec("VP9")); |
Alex Glaznev | ad948c4 | 2015-11-18 13:06:42 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1323 | bool is_h264_hw_supported = jni->CallStaticBooleanMethod( |
| 1324 | j_encoder_class, |
| 1325 | GetStaticMethodID(jni, j_encoder_class, "isH264HwSupported", "()Z")); |
| 1326 | CHECK_EXCEPTION(jni); |
| 1327 | if (is_h264_hw_supported) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 1328 | ALOGD << "H.264 HW Encoder supported."; |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1329 | supported_codecs_.push_back(cricket::VideoCodec("H264")); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1330 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 1333 | MediaCodecVideoEncoderFactory::~MediaCodecVideoEncoderFactory() { |
| 1334 | ALOGD << "MediaCodecVideoEncoderFactory dtor"; |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 1335 | if (egl_context_) { |
| 1336 | JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 1337 | jni->DeleteGlobalRef(egl_context_); |
| 1338 | } |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 1339 | } |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1340 | |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 1341 | void MediaCodecVideoEncoderFactory::SetEGLContext( |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 1342 | JNIEnv* jni, jobject egl_context) { |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 1343 | ALOGD << "MediaCodecVideoEncoderFactory::SetEGLContext"; |
Per | fd22e6c | 2016-02-18 11:35:48 +0100 | [diff] [blame] | 1344 | if (egl_context_) { |
| 1345 | jni->DeleteGlobalRef(egl_context_); |
| 1346 | egl_context_ = nullptr; |
| 1347 | } |
perkj | 461121c | 2016-02-15 06:28:36 -0800 | [diff] [blame] | 1348 | egl_context_ = jni->NewGlobalRef(egl_context); |
| 1349 | if (CheckException(jni)) { |
| 1350 | ALOGE << "error calling NewGlobalRef for EGL Context."; |
perkj | 30e9182 | 2015-11-20 01:31:25 -0800 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1354 | webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder( |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1355 | const cricket::VideoCodec& codec) { |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1356 | if (supported_codecs_.empty()) { |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1357 | ALOGW << "No HW video encoder for codec " << codec.name; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 1358 | return nullptr; |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1359 | } |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1360 | if (IsCodecSupported(supported_codecs_, codec)) { |
| 1361 | ALOGD << "Create HW video encoder for " << codec.name; |
| 1362 | const VideoCodecType type = cricket::CodecTypeFromName(codec.name); |
| 1363 | return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), type, |
| 1364 | egl_context_); |
glaznev@webrtc.org | b28474c | 2015-02-23 17:44:27 +0000 | [diff] [blame] | 1365 | } |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1366 | ALOGW << "Can not find HW video encoder for type " << codec.name; |
Per | ec2922f | 2016-01-27 15:25:46 +0100 | [diff] [blame] | 1367 | return nullptr; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
magjed | 1e45cc6 | 2016-10-28 07:43:45 -0700 | [diff] [blame] | 1370 | const std::vector<cricket::VideoCodec>& |
| 1371 | MediaCodecVideoEncoderFactory::supported_codecs() const { |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1372 | return supported_codecs_; |
| 1373 | } |
| 1374 | |
| 1375 | void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
| 1376 | webrtc::VideoEncoder* encoder) { |
Alex Glaznev | fddf6e5 | 2015-10-07 16:51:02 -0700 | [diff] [blame] | 1377 | ALOGD << "Destroy video encoder."; |
glaznev@webrtc.org | 18c9247 | 2015-02-18 18:42:55 +0000 | [diff] [blame] | 1378 | delete encoder; |
| 1379 | } |
| 1380 | |
| 1381 | } // namespace webrtc_jni |