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