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