Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: webrtc/api/android/jni/androidmediaencoder_jni.cc

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

Powered by Google App Engine
This is Rietveld 408576698