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