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