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