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