Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 | 119 |
| 120 // rtc::MessageHandler implementation. | 120 // rtc::MessageHandler implementation. |
| 121 void OnMessage(rtc::Message* msg) override; | 121 void OnMessage(rtc::Message* msg) override; |
| 122 | 122 |
| 123 void OnDroppedFrame() override; | 123 void OnDroppedFrame() override; |
| 124 | 124 |
| 125 bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } | 125 bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } |
| 126 const char* ImplementationName() const override; | 126 const char* ImplementationName() const override; |
| 127 | 127 |
| 128 private: | 128 private: |
| 129 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and | |
| 130 // InitEncodeOnCodecThread() in an attempt to restore the codec to an | |
| 131 // operable state. Necessary after all manner of OMX-layer errors. | |
| 132 bool ResetCodecOnCodecThread(); | |
| 133 | |
| 134 // Implementation of webrtc::VideoEncoder methods above, all running on the | 129 // Implementation of webrtc::VideoEncoder methods above, all running on the |
| 135 // codec thread exclusively. | 130 // codec thread exclusively. |
| 136 // | 131 // |
| 137 // If width==0 then this is assumed to be a re-initialization and the | 132 // If width==0 then this is assumed to be a re-initialization and the |
| 138 // previously-current values are reused instead of the passed parameters | 133 // previously-current values are reused instead of the passed parameters |
| 139 // (makes it easier to reason about thread-safety). | 134 // (makes it easier to reason about thread-safety). |
| 140 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps, | 135 int32_t InitEncodeOnCodecThread(int width, int height, int kbps, int fps, |
| 141 bool use_surface); | 136 bool use_surface); |
| 142 // Reconfigure to match |frame| in width, height. Also reconfigures the | 137 // Reconfigure to match |frame| in width, height. Also reconfigures the |
| 143 // encoder if |frame| is a texture/byte buffer and the encoder is initialized | 138 // encoder if |frame| is a texture/byte buffer and the encoder is initialized |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 // EGL context - owned by factory, should not be allocated/destroyed | 270 // EGL context - owned by factory, should not be allocated/destroyed |
| 276 // by MediaCodecVideoEncoder. | 271 // by MediaCodecVideoEncoder. |
| 277 jobject egl_context_; | 272 jobject egl_context_; |
| 278 | 273 |
| 279 // Temporary fix for VP8. | 274 // Temporary fix for VP8. |
| 280 // Sends a key frame if frames are largely spaced apart (possibly | 275 // Sends a key frame if frames are largely spaced apart (possibly |
| 281 // corresponding to a large image change). | 276 // corresponding to a large image change). |
| 282 int64_t last_frame_received_ms_; | 277 int64_t last_frame_received_ms_; |
| 283 int frames_received_since_last_key_; | 278 int frames_received_since_last_key_; |
| 284 webrtc::VideoCodecMode codec_mode_; | 279 webrtc::VideoCodecMode codec_mode_; |
| 280 | |
| 281 bool sw_fallback_required_; | |
| 285 }; | 282 }; |
| 286 | 283 |
| 287 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { | 284 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { |
| 288 // Call Release() to ensure no more callbacks to us after we are deleted. | 285 // Call Release() to ensure no more callbacks to us after we are deleted. |
| 289 Release(); | 286 Release(); |
| 290 } | 287 } |
| 291 | 288 |
| 292 MediaCodecVideoEncoder::MediaCodecVideoEncoder( | 289 MediaCodecVideoEncoder::MediaCodecVideoEncoder(JNIEnv* jni, |
| 293 JNIEnv* jni, VideoCodecType codecType, jobject egl_context) : | 290 VideoCodecType codecType, |
| 294 codecType_(codecType), | 291 jobject egl_context) |
| 295 callback_(NULL), | 292 : codecType_(codecType), |
| 296 codec_thread_(new Thread()), | 293 callback_(NULL), |
| 297 j_media_codec_video_encoder_class_( | 294 codec_thread_(new Thread()), |
| 298 jni, | 295 j_media_codec_video_encoder_class_( |
| 299 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), | 296 jni, |
| 300 j_media_codec_video_encoder_( | 297 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), |
| 301 jni, | 298 j_media_codec_video_encoder_( |
| 302 jni->NewObject(*j_media_codec_video_encoder_class_, | 299 jni, |
| 303 GetMethodID(jni, | 300 jni->NewObject(*j_media_codec_video_encoder_class_, |
| 304 *j_media_codec_video_encoder_class_, | 301 GetMethodID(jni, |
| 305 "<init>", | 302 *j_media_codec_video_encoder_class_, |
| 306 "()V"))), | 303 "<init>", |
| 307 inited_(false), | 304 "()V"))), |
| 308 use_surface_(false), | 305 inited_(false), |
| 309 picture_id_(0), | 306 use_surface_(false), |
| 310 egl_context_(egl_context) { | 307 picture_id_(0), |
| 308 egl_context_(egl_context), | |
| 309 sw_fallback_required_(false) { | |
| 311 ScopedLocalRefFrame local_ref_frame(jni); | 310 ScopedLocalRefFrame local_ref_frame(jni); |
| 312 // It would be nice to avoid spinning up a new thread per MediaCodec, and | 311 // It would be nice to avoid spinning up a new thread per MediaCodec, and |
| 313 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug | 312 // instead re-use e.g. the PeerConnectionFactory's |worker_thread_|, but bug |
| 314 // 2732 means that deadlocks abound. This class synchronously trampolines | 313 // 2732 means that deadlocks abound. This class synchronously trampolines |
| 315 // to |codec_thread_|, so if anything else can be coming to _us_ from | 314 // to |codec_thread_|, so if anything else can be coming to _us_ from |
| 316 // |codec_thread_|, or from any thread holding the |_sendCritSect| described | 315 // |codec_thread_|, or from any thread holding the |_sendCritSect| described |
| 317 // in the bug, we have a problem. For now work around that with a dedicated | 316 // in the bug, we have a problem. For now work around that with a dedicated |
| 318 // thread. | 317 // thread. |
| 319 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); | 318 codec_thread_->SetName("MediaCodecVideoEncoder", NULL); |
| 320 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; | 319 RTC_CHECK(codec_thread_->Start()) << "Failed to start MediaCodecVideoEncoder"; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 j_color_format_field_ = | 353 j_color_format_field_ = |
| 355 GetFieldID(jni, *j_media_codec_video_encoder_class_, "colorFormat", "I"); | 354 GetFieldID(jni, *j_media_codec_video_encoder_class_, "colorFormat", "I"); |
| 356 j_info_index_field_ = | 355 j_info_index_field_ = |
| 357 GetFieldID(jni, j_output_buffer_info_class, "index", "I"); | 356 GetFieldID(jni, j_output_buffer_info_class, "index", "I"); |
| 358 j_info_buffer_field_ = GetFieldID( | 357 j_info_buffer_field_ = GetFieldID( |
| 359 jni, j_output_buffer_info_class, "buffer", "Ljava/nio/ByteBuffer;"); | 358 jni, j_output_buffer_info_class, "buffer", "Ljava/nio/ByteBuffer;"); |
| 360 j_info_is_key_frame_field_ = | 359 j_info_is_key_frame_field_ = |
| 361 GetFieldID(jni, j_output_buffer_info_class, "isKeyFrame", "Z"); | 360 GetFieldID(jni, j_output_buffer_info_class, "isKeyFrame", "Z"); |
| 362 j_info_presentation_timestamp_us_field_ = GetFieldID( | 361 j_info_presentation_timestamp_us_field_ = GetFieldID( |
| 363 jni, j_output_buffer_info_class, "presentationTimestampUs", "J"); | 362 jni, j_output_buffer_info_class, "presentationTimestampUs", "J"); |
| 364 CHECK_EXCEPTION(jni) << "MediaCodecVideoEncoder ctor failed"; | 363 if (CheckException(jni)) { |
| 364 ALOGW << "MediaCodecVideoEncoder ctor failed. Falling back to SW encoder."; | |
| 365 sw_fallback_required_ = true; | |
| 366 } | |
| 365 srand(time(NULL)); | 367 srand(time(NULL)); |
| 366 AllowBlockingCalls(); | 368 AllowBlockingCalls(); |
| 367 } | 369 } |
| 368 | 370 |
| 369 int32_t MediaCodecVideoEncoder::InitEncode( | 371 int32_t MediaCodecVideoEncoder::InitEncode( |
| 370 const webrtc::VideoCodec* codec_settings, | 372 const webrtc::VideoCodec* codec_settings, |
| 371 int32_t /* number_of_cores */, | 373 int32_t /* number_of_cores */, |
| 372 size_t /* max_payload_size */) { | 374 size_t /* max_payload_size */) { |
| 373 if (codec_settings == NULL) { | 375 if (codec_settings == NULL) { |
| 374 ALOGE << "NULL VideoCodec instance"; | 376 ALOGE << "NULL VideoCodec instance"; |
| 375 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 377 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 376 } | 378 } |
| 377 // Factory should guard against other codecs being used with us. | 379 // Factory should guard against other codecs being used with us. |
| 378 RTC_CHECK(codec_settings->codecType == codecType_) | 380 RTC_CHECK(codec_settings->codecType == codecType_) |
| 379 << "Unsupported codec " << codec_settings->codecType << " for " | 381 << "Unsupported codec " << codec_settings->codecType << " for " |
| 380 << codecType_; | 382 << codecType_; |
| 381 | 383 |
|
AlexG
2016/08/31 00:25:57
check sw_fallback_required_ flag here
sakal
2016/09/02 08:08:00
Done.
| |
| 382 codec_mode_ = codec_settings->mode; | 384 codec_mode_ = codec_settings->mode; |
| 383 int init_width = codec_settings->width; | 385 int init_width = codec_settings->width; |
| 384 int init_height = codec_settings->height; | 386 int init_height = codec_settings->height; |
| 385 // Scaling is disabled for VP9, but optionally enabled for VP8. | 387 // Scaling is disabled for VP9, but optionally enabled for VP8. |
| 386 // TODO(pbos): Extract automaticResizeOn out of VP8 settings. | 388 // TODO(pbos): Extract automaticResizeOn out of VP8 settings. |
| 387 scale_ = false; | 389 scale_ = false; |
| 388 if (codecType_ == kVideoCodecVP8) { | 390 if (codecType_ == kVideoCodecVP8) { |
| 389 scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn; | 391 scale_ = codec_settings->codecSpecific.VP8.automaticResizeOn; |
| 390 } else if (codecType_ != kVideoCodecVP9) { | 392 } else if (codecType_ != kVideoCodecVP9) { |
| 391 scale_ = true; | 393 scale_ = true; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollNoFramesMs, this); | 486 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollNoFramesMs, this); |
| 485 } else { | 487 } else { |
| 486 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); | 488 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
| 487 } | 489 } |
| 488 | 490 |
| 489 // Call log statistics here so it's called even if no frames are being | 491 // Call log statistics here so it's called even if no frames are being |
| 490 // delivered. | 492 // delivered. |
| 491 LogStatistics(false); | 493 LogStatistics(false); |
| 492 } | 494 } |
| 493 | 495 |
| 494 bool MediaCodecVideoEncoder::ResetCodecOnCodecThread() { | |
| 495 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
| 496 ALOGE << "ResetOnCodecThread"; | |
| 497 if (ReleaseOnCodecThread() != WEBRTC_VIDEO_CODEC_OK || | |
| 498 InitEncodeOnCodecThread(width_, height_, 0, 0, false) != | |
| 499 WEBRTC_VIDEO_CODEC_OK) { | |
| 500 // TODO(fischman): wouldn't it be nice if there was a way to gracefully | |
| 501 // degrade to a SW encoder at this point? There isn't one AFAICT :( | |
| 502 // https://code.google.com/p/webrtc/issues/detail?id=2920 | |
| 503 return false; | |
| 504 } | |
| 505 return true; | |
| 506 } | |
| 507 | |
| 508 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( | 496 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( |
| 509 int width, int height, int kbps, int fps, bool use_surface) { | 497 int width, int height, int kbps, int fps, bool use_surface) { |
| 510 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 498 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 499 if (sw_fallback_required_) { | |
| 500 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 501 } | |
| 511 RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set."; | 502 RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set."; |
| 512 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 503 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 513 ScopedLocalRefFrame local_ref_frame(jni); | 504 ScopedLocalRefFrame local_ref_frame(jni); |
| 514 | 505 |
| 515 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << | 506 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << |
| 516 width << " x " << height << ". Bitrate: " << kbps << | 507 width << " x " << height << ". Bitrate: " << kbps << |
| 517 " kbps. Fps: " << fps; | 508 " kbps. Fps: " << fps; |
| 518 if (kbps == 0) { | 509 if (kbps == 0) { |
| 519 kbps = last_set_bitrate_kbps_; | 510 kbps = last_set_bitrate_kbps_; |
| 520 } | 511 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 | 544 |
| 554 // We enforce no extra stride/padding in the format creation step. | 545 // We enforce no extra stride/padding in the format creation step. |
| 555 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( | 546 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
| 556 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); | 547 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
| 557 const bool encode_status = jni->CallBooleanMethod( | 548 const bool encode_status = jni->CallBooleanMethod( |
| 558 *j_media_codec_video_encoder_, j_init_encode_method_, | 549 *j_media_codec_video_encoder_, j_init_encode_method_, |
| 559 j_video_codec_enum, width, height, kbps, fps, | 550 j_video_codec_enum, width, height, kbps, fps, |
| 560 (use_surface ? egl_context_ : nullptr)); | 551 (use_surface ? egl_context_ : nullptr)); |
| 561 if (!encode_status) { | 552 if (!encode_status) { |
| 562 ALOGE << "Failed to configure encoder."; | 553 ALOGE << "Failed to configure encoder."; |
| 563 return WEBRTC_VIDEO_CODEC_ERROR; | 554 return WEBRTC_VIDEO_CODEC_ERROR; |
|
AlexG
2016/08/31 00:25:57
fallback to sw encoder here. Still return error, b
sakal
2016/09/02 08:08:00
Done.
| |
| 564 } | 555 } |
| 565 CHECK_EXCEPTION(jni); | 556 if (CheckException(jni)) { |
| 557 ALOGE << "Exception in init encode. Falling back to SW encoder."; | |
| 558 sw_fallback_required_ = true; | |
| 559 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
|
AlexG
2016/08/31 00:25:58
return WEBRTC_VIDEO_CODEC_ERROR? In decoder WEBRT
sakal
2016/09/02 08:08:00
Done.
| |
| 560 } | |
| 566 | 561 |
| 567 if (!use_surface) { | 562 if (!use_surface) { |
| 568 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( | 563 jobjectArray input_buffers = reinterpret_cast<jobjectArray>( |
| 569 jni->CallObjectMethod(*j_media_codec_video_encoder_, | 564 jni->CallObjectMethod(*j_media_codec_video_encoder_, |
| 570 j_get_input_buffers_method_)); | 565 j_get_input_buffers_method_)); |
| 571 CHECK_EXCEPTION(jni); | 566 if (CheckException(jni)) { |
| 567 ALOGE << "Exception in get input buffers. Falling back to SW encoder."; | |
| 568 sw_fallback_required_ = true; | |
| 569 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 570 } | |
| 571 | |
| 572 if (IsNull(jni, input_buffers)) { | 572 if (IsNull(jni, input_buffers)) { |
| 573 return WEBRTC_VIDEO_CODEC_ERROR; | 573 return WEBRTC_VIDEO_CODEC_ERROR; |
|
AlexG
2016/08/31 00:25:57
set sw_fallback_required_ = true
sakal
2016/09/02 08:08:01
Done.
| |
| 574 } | 574 } |
| 575 | 575 |
| 576 switch (GetIntField(jni, *j_media_codec_video_encoder_, | 576 switch (GetIntField(jni, *j_media_codec_video_encoder_, |
| 577 j_color_format_field_)) { | 577 j_color_format_field_)) { |
| 578 case COLOR_FormatYUV420Planar: | 578 case COLOR_FormatYUV420Planar: |
| 579 encoder_fourcc_ = libyuv::FOURCC_YU12; | 579 encoder_fourcc_ = libyuv::FOURCC_YU12; |
| 580 break; | 580 break; |
| 581 case COLOR_FormatYUV420SemiPlanar: | 581 case COLOR_FormatYUV420SemiPlanar: |
| 582 case COLOR_QCOM_FormatYUV420SemiPlanar: | 582 case COLOR_QCOM_FormatYUV420SemiPlanar: |
| 583 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: | 583 case COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m: |
| 584 encoder_fourcc_ = libyuv::FOURCC_NV12; | 584 encoder_fourcc_ = libyuv::FOURCC_NV12; |
| 585 break; | 585 break; |
| 586 default: | 586 default: |
| 587 LOG(LS_ERROR) << "Wrong color format."; | 587 LOG(LS_ERROR) << "Wrong color format."; |
|
AlexG
2016/08/31 00:25:57
set sw_fallback_required_ = true
sakal
2016/09/02 08:08:01
Done.
| |
| 588 return WEBRTC_VIDEO_CODEC_ERROR; | 588 return WEBRTC_VIDEO_CODEC_ERROR; |
| 589 } | 589 } |
| 590 size_t num_input_buffers = jni->GetArrayLength(input_buffers); | 590 size_t num_input_buffers = jni->GetArrayLength(input_buffers); |
| 591 RTC_CHECK(input_buffers_.empty()) | 591 RTC_CHECK(input_buffers_.empty()) |
| 592 << "Unexpected double InitEncode without Release"; | 592 << "Unexpected double InitEncode without Release"; |
| 593 input_buffers_.resize(num_input_buffers); | 593 input_buffers_.resize(num_input_buffers); |
| 594 for (size_t i = 0; i < num_input_buffers; ++i) { | 594 for (size_t i = 0; i < num_input_buffers; ++i) { |
| 595 input_buffers_[i] = | 595 input_buffers_[i] = |
| 596 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); | 596 jni->NewGlobalRef(jni->GetObjectArrayElement(input_buffers, i)); |
| 597 int64_t yuv_buffer_capacity = | 597 int64_t yuv_buffer_capacity = |
| 598 jni->GetDirectBufferCapacity(input_buffers_[i]); | 598 jni->GetDirectBufferCapacity(input_buffers_[i]); |
| 599 CHECK_EXCEPTION(jni); | 599 if (CheckException(jni)) { |
| 600 ALOGE << "Exception in get direct buffer capacity. Falling back to SW " | |
| 601 "encoder."; | |
| 602 sw_fallback_required_ = true; | |
| 603 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 604 } | |
| 600 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; | 605 RTC_CHECK(yuv_buffer_capacity >= yuv_size_) << "Insufficient capacity"; |
| 601 } | 606 } |
| 602 } | 607 } |
| 603 | 608 |
| 604 inited_ = true; | 609 inited_ = true; |
| 605 return WEBRTC_VIDEO_CODEC_OK; | 610 return WEBRTC_VIDEO_CODEC_OK; |
| 606 } | 611 } |
| 607 | 612 |
| 608 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( | 613 int32_t MediaCodecVideoEncoder::EncodeOnCodecThread( |
| 609 const webrtc::VideoFrame& frame, | 614 const webrtc::VideoFrame& frame, |
| 610 const std::vector<webrtc::FrameType>* frame_types, | 615 const std::vector<webrtc::FrameType>* frame_types, |
| 611 const int64_t frame_input_time_ms) { | 616 const int64_t frame_input_time_ms) { |
| 612 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 617 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 618 if (sw_fallback_required_) { | |
| 619 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 620 } | |
| 613 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 621 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 614 ScopedLocalRefFrame local_ref_frame(jni); | 622 ScopedLocalRefFrame local_ref_frame(jni); |
| 615 | 623 |
| 616 if (!inited_) { | 624 if (!inited_) { |
| 617 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 625 return WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 618 } | 626 } |
| 619 | 627 |
| 620 bool send_key_frame = false; | 628 bool send_key_frame = false; |
| 621 if (codec_mode_ == webrtc::kRealtimeVideo) { | 629 if (codec_mode_ == webrtc::kRealtimeVideo) { |
| 622 ++frames_received_since_last_key_; | 630 ++frames_received_since_last_key_; |
| 623 int64_t now_ms = rtc::TimeMillis(); | 631 int64_t now_ms = rtc::TimeMillis(); |
| 624 if (last_frame_received_ms_ != -1 && | 632 if (last_frame_received_ms_ != -1 && |
| 625 (now_ms - last_frame_received_ms_) > kFrameDiffThresholdMs) { | 633 (now_ms - last_frame_received_ms_) > kFrameDiffThresholdMs) { |
| 626 // Add limit to prevent triggering a key for every frame for very low | 634 // Add limit to prevent triggering a key for every frame for very low |
| 627 // framerates (e.g. if frame diff > kFrameDiffThresholdMs). | 635 // framerates (e.g. if frame diff > kFrameDiffThresholdMs). |
| 628 if (frames_received_since_last_key_ > kMinKeyFrameInterval) { | 636 if (frames_received_since_last_key_ > kMinKeyFrameInterval) { |
| 629 ALOGD << "Send key, frame diff: " << (now_ms - last_frame_received_ms_); | 637 ALOGD << "Send key, frame diff: " << (now_ms - last_frame_received_ms_); |
| 630 send_key_frame = true; | 638 send_key_frame = true; |
| 631 } | 639 } |
| 632 frames_received_since_last_key_ = 0; | 640 frames_received_since_last_key_ = 0; |
| 633 } | 641 } |
| 634 last_frame_received_ms_ = now_ms; | 642 last_frame_received_ms_ = now_ms; |
| 635 } | 643 } |
| 636 | 644 |
| 637 frames_received_++; | 645 frames_received_++; |
| 638 if (!DeliverPendingOutputs(jni)) { | 646 if (!DeliverPendingOutputs(jni)) { |
| 639 if (!ResetCodecOnCodecThread()) | 647 sw_fallback_required_ = true; |
| 640 return WEBRTC_VIDEO_CODEC_ERROR; | 648 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 641 } | 649 } |
| 642 if (frames_encoded_ < kMaxEncodedLogFrames) { | 650 if (frames_encoded_ < kMaxEncodedLogFrames) { |
| 643 ALOGD << "Encoder frame in # " << (frames_received_ - 1) | 651 ALOGD << "Encoder frame in # " << (frames_received_ - 1) |
| 644 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 652 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
| 645 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ | 653 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ |
| 646 << ". Kbps: " << last_set_bitrate_kbps_; | 654 << ". Kbps: " << last_set_bitrate_kbps_; |
| 647 } | 655 } |
| 648 | 656 |
| 649 if (drop_next_input_frame_) { | 657 if (drop_next_input_frame_) { |
| 650 ALOGW << "Encoder drop frame - failed callback."; | 658 ALOGW << "Encoder drop frame - failed callback."; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 663 ALOGD << "Already " << input_frame_infos_.size() | 671 ALOGD << "Already " << input_frame_infos_.size() |
| 664 << " frames in the queue, dropping" | 672 << " frames in the queue, dropping" |
| 665 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 673 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
| 666 << ". Fps: " << last_set_fps_ | 674 << ". Fps: " << last_set_fps_ |
| 667 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; | 675 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; |
| 668 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 676 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 669 consecutive_full_queue_frame_drops_++; | 677 consecutive_full_queue_frame_drops_++; |
| 670 if (consecutive_full_queue_frame_drops_ >= | 678 if (consecutive_full_queue_frame_drops_ >= |
| 671 ENCODER_STALL_FRAMEDROP_THRESHOLD) { | 679 ENCODER_STALL_FRAMEDROP_THRESHOLD) { |
| 672 ALOGE << "Encoder got stuck. Reset."; | 680 ALOGE << "Encoder got stuck. Reset."; |
| 673 ResetCodecOnCodecThread(); | 681 sw_fallback_required_ = true; |
| 674 return WEBRTC_VIDEO_CODEC_ERROR; | 682 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 675 } | 683 } |
| 676 frames_dropped_media_encoder_++; | 684 frames_dropped_media_encoder_++; |
| 677 OnDroppedFrameOnCodecThread(); | 685 OnDroppedFrameOnCodecThread(); |
| 678 return WEBRTC_VIDEO_CODEC_OK; | 686 return WEBRTC_VIDEO_CODEC_OK; |
| 679 } | 687 } |
| 680 consecutive_full_queue_frame_drops_ = 0; | 688 consecutive_full_queue_frame_drops_ = 0; |
| 681 | 689 |
| 682 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( | 690 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( |
| 683 frame.video_frame_buffer()); | 691 frame.video_frame_buffer()); |
| 684 if (scale_) { | 692 if (scale_) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 708 ALOGE << "Failed to reconfigure encoder."; | 716 ALOGE << "Failed to reconfigure encoder."; |
| 709 return WEBRTC_VIDEO_CODEC_ERROR; | 717 return WEBRTC_VIDEO_CODEC_ERROR; |
| 710 } | 718 } |
| 711 | 719 |
| 712 const bool key_frame = | 720 const bool key_frame = |
| 713 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; | 721 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; |
| 714 bool encode_status = true; | 722 bool encode_status = true; |
| 715 if (!input_frame.video_frame_buffer()->native_handle()) { | 723 if (!input_frame.video_frame_buffer()->native_handle()) { |
| 716 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, | 724 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
| 717 j_dequeue_input_buffer_method_); | 725 j_dequeue_input_buffer_method_); |
| 718 CHECK_EXCEPTION(jni); | 726 if (CheckException(jni)) { |
| 727 ALOGE << "Exception in dequeu input buffer. Falling back to SW encoder."; | |
| 728 sw_fallback_required_ = true; | |
| 729 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 730 } | |
| 719 if (j_input_buffer_index == -1) { | 731 if (j_input_buffer_index == -1) { |
| 720 // Video codec falls behind - no input buffer available. | 732 // Video codec falls behind - no input buffer available. |
| 721 ALOGW << "Encoder drop frame - no input buffers available"; | 733 ALOGW << "Encoder drop frame - no input buffers available"; |
| 722 if (frames_received_ > 1) { | 734 if (frames_received_ > 1) { |
| 723 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 735 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 724 frames_dropped_media_encoder_++; | 736 frames_dropped_media_encoder_++; |
| 725 OnDroppedFrameOnCodecThread(); | 737 OnDroppedFrameOnCodecThread(); |
| 726 } else { | 738 } else { |
| 727 // Input buffers are not ready after codec initialization, HW is still | 739 // Input buffers are not ready after codec initialization, HW is still |
| 728 // allocating thme - this is expected and should not result in drop | 740 // allocating thme - this is expected and should not result in drop |
| 729 // frame report. | 741 // frame report. |
| 730 frames_received_ = 0; | 742 frames_received_ = 0; |
| 731 } | 743 } |
| 732 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. | 744 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. |
| 733 } else if (j_input_buffer_index == -2) { | 745 } else if (j_input_buffer_index == -2) { |
| 734 ResetCodecOnCodecThread(); | 746 sw_fallback_required_ = true; |
| 735 return WEBRTC_VIDEO_CODEC_ERROR; | 747 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 736 } | 748 } |
| 737 encode_status = EncodeByteBufferOnCodecThread(jni, key_frame, input_frame, | 749 encode_status = EncodeByteBufferOnCodecThread(jni, key_frame, input_frame, |
| 738 j_input_buffer_index); | 750 j_input_buffer_index); |
| 739 } else { | 751 } else { |
| 740 encode_status = EncodeTextureOnCodecThread(jni, key_frame, input_frame); | 752 encode_status = EncodeTextureOnCodecThread(jni, key_frame, input_frame); |
| 741 } | 753 } |
| 742 | 754 |
| 743 if (!encode_status) { | 755 if (!encode_status) { |
| 744 ALOGE << "Failed encode frame with timestamp: " << input_frame.timestamp(); | 756 ALOGE << "Failed encode frame with timestamp: " << input_frame.timestamp(); |
| 745 ResetCodecOnCodecThread(); | 757 sw_fallback_required_ = true; |
| 746 return WEBRTC_VIDEO_CODEC_ERROR; | 758 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 747 } | 759 } |
| 748 | 760 |
| 749 // Save input image timestamps for later output. | 761 // Save input image timestamps for later output. |
| 750 input_frame_infos_.emplace_back( | 762 input_frame_infos_.emplace_back( |
| 751 frame_input_time_ms, input_frame.timestamp(), | 763 frame_input_time_ms, input_frame.timestamp(), |
| 752 input_frame.render_time_ms(), input_frame.rotation()); | 764 input_frame.render_time_ms(), input_frame.rotation()); |
| 753 | 765 |
| 754 last_input_timestamp_ms_ = | 766 last_input_timestamp_ms_ = |
| 755 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; | 767 current_timestamp_us_ / rtc::kNumMicrosecsPerMillisec; |
| 756 | 768 |
| 757 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 769 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
| 758 | 770 |
| 759 codec_thread_->Clear(this); | 771 codec_thread_->Clear(this); |
| 760 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); | 772 codec_thread_->PostDelayed(RTC_FROM_HERE, kMediaCodecPollMs, this); |
| 761 | 773 |
| 762 if (!DeliverPendingOutputs(jni)) { | 774 if (!DeliverPendingOutputs(jni)) { |
| 763 ALOGE << "Failed deliver pending outputs."; | 775 ALOGE << "Failed deliver pending outputs."; |
| 764 ResetCodecOnCodecThread(); | 776 sw_fallback_required_ = true; |
| 765 return WEBRTC_VIDEO_CODEC_ERROR; | 777 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 766 } | 778 } |
| 767 return WEBRTC_VIDEO_CODEC_OK; | 779 return WEBRTC_VIDEO_CODEC_OK; |
| 768 } | 780 } |
| 769 | 781 |
| 770 bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread( | 782 bool MediaCodecVideoEncoder::MaybeReconfigureEncoderOnCodecThread( |
| 771 const webrtc::VideoFrame& frame) { | 783 const webrtc::VideoFrame& frame) { |
| 772 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 784 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 773 | 785 |
| 774 const bool is_texture_frame = | 786 const bool is_texture_frame = |
| 775 frame.video_frame_buffer()->native_handle() != nullptr; | 787 frame.video_frame_buffer()->native_handle() != nullptr; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 803 } | 815 } |
| 804 | 816 |
| 805 bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni, | 817 bool MediaCodecVideoEncoder::EncodeByteBufferOnCodecThread(JNIEnv* jni, |
| 806 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index) { | 818 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index) { |
| 807 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 819 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 808 RTC_CHECK(!use_surface_); | 820 RTC_CHECK(!use_surface_); |
| 809 | 821 |
| 810 jobject j_input_buffer = input_buffers_[input_buffer_index]; | 822 jobject j_input_buffer = input_buffers_[input_buffer_index]; |
| 811 uint8_t* yuv_buffer = | 823 uint8_t* yuv_buffer = |
| 812 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); | 824 reinterpret_cast<uint8_t*>(jni->GetDirectBufferAddress(j_input_buffer)); |
| 813 CHECK_EXCEPTION(jni); | 825 if (CheckException(jni)) { |
| 826 ALOGE << "Exception in get direct buffer address. Falling back to SW " | |
| 827 "encoder."; | |
| 828 sw_fallback_required_ = true; | |
|
AlexG
2016/08/31 00:25:57
We can not fallback to SW encoder for H.264 codec
sakal
2016/09/02 08:08:00
Done.
| |
| 829 return false; | |
| 830 } | |
| 814 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; | 831 RTC_CHECK(yuv_buffer) << "Indirect buffer??"; |
| 815 RTC_CHECK(!libyuv::ConvertFromI420( | 832 RTC_CHECK(!libyuv::ConvertFromI420( |
| 816 frame.video_frame_buffer()->DataY(), | 833 frame.video_frame_buffer()->DataY(), |
| 817 frame.video_frame_buffer()->StrideY(), | 834 frame.video_frame_buffer()->StrideY(), |
| 818 frame.video_frame_buffer()->DataU(), | 835 frame.video_frame_buffer()->DataU(), |
| 819 frame.video_frame_buffer()->StrideU(), | 836 frame.video_frame_buffer()->StrideU(), |
| 820 frame.video_frame_buffer()->DataV(), | 837 frame.video_frame_buffer()->DataV(), |
| 821 frame.video_frame_buffer()->StrideV(), | 838 frame.video_frame_buffer()->StrideV(), |
| 822 yuv_buffer, width_, width_, height_, encoder_fourcc_)) | 839 yuv_buffer, width_, width_, height_, encoder_fourcc_)) |
| 823 << "ConvertFromI420 failed"; | 840 << "ConvertFromI420 failed"; |
| 824 | 841 |
| 825 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 842 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 826 j_encode_buffer_method_, | 843 j_encode_buffer_method_, |
| 827 key_frame, | 844 key_frame, |
| 828 input_buffer_index, | 845 input_buffer_index, |
| 829 yuv_size_, | 846 yuv_size_, |
| 830 current_timestamp_us_); | 847 current_timestamp_us_); |
| 831 CHECK_EXCEPTION(jni); | 848 if (CheckException(jni)) { |
| 849 ALOGE << "Exception in encode buffer. Falling back to SW encoder."; | |
| 850 sw_fallback_required_ = true; | |
| 851 return false; | |
| 852 } | |
| 832 return encode_status; | 853 return encode_status; |
| 833 } | 854 } |
| 834 | 855 |
| 835 bool MediaCodecVideoEncoder::EncodeTextureOnCodecThread(JNIEnv* jni, | 856 bool MediaCodecVideoEncoder::EncodeTextureOnCodecThread(JNIEnv* jni, |
| 836 bool key_frame, const webrtc::VideoFrame& frame) { | 857 bool key_frame, const webrtc::VideoFrame& frame) { |
| 837 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 858 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 838 RTC_CHECK(use_surface_); | 859 RTC_CHECK(use_surface_); |
| 839 NativeHandleImpl* handle = static_cast<NativeHandleImpl*>( | 860 NativeHandleImpl* handle = static_cast<NativeHandleImpl*>( |
| 840 frame.video_frame_buffer()->native_handle()); | 861 frame.video_frame_buffer()->native_handle()); |
| 841 jfloatArray sampling_matrix = handle->sampling_matrix.ToJava(jni); | 862 jfloatArray sampling_matrix = handle->sampling_matrix.ToJava(jni); |
| 842 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 863 bool encode_status = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 843 j_encode_texture_method_, | 864 j_encode_texture_method_, |
| 844 key_frame, | 865 key_frame, |
| 845 handle->oes_texture_id, | 866 handle->oes_texture_id, |
| 846 sampling_matrix, | 867 sampling_matrix, |
| 847 current_timestamp_us_); | 868 current_timestamp_us_); |
| 848 CHECK_EXCEPTION(jni); | 869 if (CheckException(jni)) { |
| 870 ALOGE << "Exception in encode texture. Falling back to SW encoder."; | |
| 871 sw_fallback_required_ = true; | |
| 872 return false; | |
| 873 } | |
| 849 return encode_status; | 874 return encode_status; |
| 850 } | 875 } |
| 851 | 876 |
| 852 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( | 877 int32_t MediaCodecVideoEncoder::RegisterEncodeCompleteCallbackOnCodecThread( |
| 853 webrtc::EncodedImageCallback* callback) { | 878 webrtc::EncodedImageCallback* callback) { |
| 854 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 879 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 855 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 880 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 856 ScopedLocalRefFrame local_ref_frame(jni); | 881 ScopedLocalRefFrame local_ref_frame(jni); |
| 857 callback_ = callback; | 882 callback_ = callback; |
| 858 return WEBRTC_VIDEO_CODEC_OK; | 883 return WEBRTC_VIDEO_CODEC_OK; |
| 859 } | 884 } |
| 860 | 885 |
| 861 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { | 886 int32_t MediaCodecVideoEncoder::ReleaseOnCodecThread() { |
| 862 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 887 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 863 if (!inited_) { | 888 if (!inited_) { |
| 864 return WEBRTC_VIDEO_CODEC_OK; | 889 return WEBRTC_VIDEO_CODEC_OK; |
| 865 } | 890 } |
| 866 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 891 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 867 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << | 892 ALOGD << "EncoderReleaseOnCodecThread: Frames received: " << |
| 868 frames_received_ << ". Encoded: " << frames_encoded_ << | 893 frames_received_ << ". Encoded: " << frames_encoded_ << |
| 869 ". Dropped: " << frames_dropped_media_encoder_; | 894 ". Dropped: " << frames_dropped_media_encoder_; |
| 870 ScopedLocalRefFrame local_ref_frame(jni); | 895 ScopedLocalRefFrame local_ref_frame(jni); |
| 871 for (size_t i = 0; i < input_buffers_.size(); ++i) | 896 for (size_t i = 0; i < input_buffers_.size(); ++i) |
| 872 jni->DeleteGlobalRef(input_buffers_[i]); | 897 jni->DeleteGlobalRef(input_buffers_[i]); |
| 873 input_buffers_.clear(); | 898 input_buffers_.clear(); |
| 874 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); | 899 jni->CallVoidMethod(*j_media_codec_video_encoder_, j_release_method_); |
| 875 CHECK_EXCEPTION(jni); | 900 if (CheckException(jni)) { |
| 901 ALOGE << "Exception in release. Falling back to SW encoder."; | |
| 902 sw_fallback_required_ = true; | |
| 903 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
|
AlexG
2016/08/31 00:25:58
Return error here?
sakal
2016/09/02 08:08:01
Done.
| |
| 904 } | |
| 876 rtc::MessageQueueManager::Clear(this); | 905 rtc::MessageQueueManager::Clear(this); |
| 877 inited_ = false; | 906 inited_ = false; |
| 878 use_surface_ = false; | 907 use_surface_ = false; |
| 879 ALOGD << "EncoderReleaseOnCodecThread done."; | 908 ALOGD << "EncoderReleaseOnCodecThread done."; |
| 880 return WEBRTC_VIDEO_CODEC_OK; | 909 return WEBRTC_VIDEO_CODEC_OK; |
| 881 } | 910 } |
| 882 | 911 |
| 883 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, | 912 int32_t MediaCodecVideoEncoder::SetRatesOnCodecThread(uint32_t new_bit_rate, |
| 884 uint32_t frame_rate) { | 913 uint32_t frame_rate) { |
| 885 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 914 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 915 if (sw_fallback_required_) { | |
| 916 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
|
AlexG
2016/08/31 00:25:58
Return WEBRTC_VIDEO_CODEC_OK here? WEBRTC_VIDEO_CO
sakal
2016/09/02 08:08:00
Done.
| |
| 917 } | |
| 886 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? | 918 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? |
| 887 frame_rate : MAX_ALLOWED_VIDEO_FPS; | 919 frame_rate : MAX_ALLOWED_VIDEO_FPS; |
| 888 if (last_set_bitrate_kbps_ == new_bit_rate && | 920 if (last_set_bitrate_kbps_ == new_bit_rate && |
| 889 last_set_fps_ == frame_rate) { | 921 last_set_fps_ == frame_rate) { |
| 890 return WEBRTC_VIDEO_CODEC_OK; | 922 return WEBRTC_VIDEO_CODEC_OK; |
| 891 } | 923 } |
| 892 if (scale_) { | 924 if (scale_) { |
| 893 quality_scaler_.ReportFramerate(frame_rate); | 925 quality_scaler_.ReportFramerate(frame_rate); |
| 894 } | 926 } |
| 895 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 927 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
| 896 ScopedLocalRefFrame local_ref_frame(jni); | 928 ScopedLocalRefFrame local_ref_frame(jni); |
| 897 if (new_bit_rate > 0) { | 929 if (new_bit_rate > 0) { |
| 898 last_set_bitrate_kbps_ = new_bit_rate; | 930 last_set_bitrate_kbps_ = new_bit_rate; |
| 899 } | 931 } |
| 900 if (frame_rate > 0) { | 932 if (frame_rate > 0) { |
| 901 last_set_fps_ = frame_rate; | 933 last_set_fps_ = frame_rate; |
| 902 } | 934 } |
| 903 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 935 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 904 j_set_rates_method_, | 936 j_set_rates_method_, |
| 905 last_set_bitrate_kbps_, | 937 last_set_bitrate_kbps_, |
| 906 last_set_fps_); | 938 last_set_fps_); |
| 907 CHECK_EXCEPTION(jni); | 939 if (CheckException(jni) || !ret) { |
| 908 if (!ret) { | 940 sw_fallback_required_ = true; |
|
AlexG
2016/08/31 00:25:58
ditto: Return WEBRTC_VIDEO_CODEC_OK here, but keep
sakal
2016/09/02 08:08:00
Done.
| |
| 909 ResetCodecOnCodecThread(); | 941 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; |
| 910 return WEBRTC_VIDEO_CODEC_ERROR; | |
| 911 } | 942 } |
| 912 return WEBRTC_VIDEO_CODEC_OK; | 943 return WEBRTC_VIDEO_CODEC_OK; |
| 913 } | 944 } |
| 914 | 945 |
| 915 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( | 946 int MediaCodecVideoEncoder::GetOutputBufferInfoIndex( |
| 916 JNIEnv* jni, | 947 JNIEnv* jni, |
| 917 jobject j_output_buffer_info) { | 948 jobject j_output_buffer_info) { |
| 918 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); | 949 return GetIntField(jni, j_output_buffer_info, j_info_index_field_); |
| 919 } | 950 } |
| 920 | 951 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 936 return GetLongField( | 967 return GetLongField( |
| 937 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); | 968 jni, j_output_buffer_info, j_info_presentation_timestamp_us_field_); |
| 938 } | 969 } |
| 939 | 970 |
| 940 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { | 971 bool MediaCodecVideoEncoder::DeliverPendingOutputs(JNIEnv* jni) { |
| 941 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 972 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
| 942 | 973 |
| 943 while (true) { | 974 while (true) { |
| 944 jobject j_output_buffer_info = jni->CallObjectMethod( | 975 jobject j_output_buffer_info = jni->CallObjectMethod( |
| 945 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); | 976 *j_media_codec_video_encoder_, j_dequeue_output_buffer_method_); |
| 946 CHECK_EXCEPTION(jni); | 977 if (CheckException(jni)) { |
| 978 ALOGE << "Exception in set dequeue output buffer. Falling back to SW " | |
| 979 "encoder."; | |
| 980 sw_fallback_required_ = true; | |
| 981 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 982 } | |
| 947 if (IsNull(jni, j_output_buffer_info)) { | 983 if (IsNull(jni, j_output_buffer_info)) { |
| 948 break; | 984 break; |
| 949 } | 985 } |
| 950 | 986 |
| 951 int output_buffer_index = | 987 int output_buffer_index = |
| 952 GetOutputBufferInfoIndex(jni, j_output_buffer_info); | 988 GetOutputBufferInfoIndex(jni, j_output_buffer_info); |
| 953 if (output_buffer_index == -1) { | 989 if (output_buffer_index == -1) { |
| 954 ResetCodecOnCodecThread(); | 990 sw_fallback_required_ = true; |
| 955 return false; | 991 return false; |
| 956 } | 992 } |
| 957 | 993 |
| 958 // Get key and config frame flags. | 994 // Get key and config frame flags. |
| 959 jobject j_output_buffer = | 995 jobject j_output_buffer = |
| 960 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); | 996 GetOutputBufferInfoBuffer(jni, j_output_buffer_info); |
| 961 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); | 997 bool key_frame = GetOutputBufferInfoIsKeyFrame(jni, j_output_buffer_info); |
| 962 | 998 |
| 963 // Get frame timestamps from a queue - for non config frames only. | 999 // Get frame timestamps from a queue - for non config frames only. |
| 964 int64_t encoding_start_time_ms = 0; | 1000 int64_t encoding_start_time_ms = 0; |
| 965 int64_t frame_encoding_time_ms = 0; | 1001 int64_t frame_encoding_time_ms = 0; |
| 966 last_output_timestamp_ms_ = | 1002 last_output_timestamp_ms_ = |
| 967 GetOutputBufferInfoPresentationTimestampUs(jni, j_output_buffer_info) / | 1003 GetOutputBufferInfoPresentationTimestampUs(jni, j_output_buffer_info) / |
| 968 rtc::kNumMicrosecsPerMillisec; | 1004 rtc::kNumMicrosecsPerMillisec; |
| 969 if (!input_frame_infos_.empty()) { | 1005 if (!input_frame_infos_.empty()) { |
| 970 const InputFrameInfo& frame_info = input_frame_infos_.front(); | 1006 const InputFrameInfo& frame_info = input_frame_infos_.front(); |
| 971 output_timestamp_ = frame_info.frame_timestamp; | 1007 output_timestamp_ = frame_info.frame_timestamp; |
| 972 output_render_time_ms_ = frame_info.frame_render_time_ms; | 1008 output_render_time_ms_ = frame_info.frame_render_time_ms; |
| 973 output_rotation_ = frame_info.rotation; | 1009 output_rotation_ = frame_info.rotation; |
| 974 encoding_start_time_ms = frame_info.encode_start_time; | 1010 encoding_start_time_ms = frame_info.encode_start_time; |
| 975 input_frame_infos_.pop_front(); | 1011 input_frame_infos_.pop_front(); |
| 976 } | 1012 } |
| 977 | 1013 |
| 978 // Extract payload. | 1014 // Extract payload. |
| 979 size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer); | 1015 size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer); |
| 980 uint8_t* payload = reinterpret_cast<uint8_t*>( | 1016 uint8_t* payload = reinterpret_cast<uint8_t*>( |
| 981 jni->GetDirectBufferAddress(j_output_buffer)); | 1017 jni->GetDirectBufferAddress(j_output_buffer)); |
| 982 CHECK_EXCEPTION(jni); | 1018 if (CheckException(jni)) { |
| 1019 ALOGE << "Exception in get direct buffer address. Falling back to SW " | |
| 1020 "encoder."; | |
| 1021 sw_fallback_required_ = true; | |
| 1022 return WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE; | |
| 1023 } | |
| 983 | 1024 |
| 984 // Callback - return encoded frame. | 1025 // Callback - return encoded frame. |
| 985 int32_t callback_status = 0; | 1026 int32_t callback_status = 0; |
| 986 if (callback_) { | 1027 if (callback_) { |
| 987 std::unique_ptr<webrtc::EncodedImage> image( | 1028 std::unique_ptr<webrtc::EncodedImage> image( |
| 988 new webrtc::EncodedImage(payload, payload_size, payload_size)); | 1029 new webrtc::EncodedImage(payload, payload_size, payload_size)); |
| 989 image->_encodedWidth = width_; | 1030 image->_encodedWidth = width_; |
| 990 image->_encodedHeight = height_; | 1031 image->_encodedHeight = height_; |
| 991 image->_timeStamp = output_timestamp_; | 1032 image->_timeStamp = output_timestamp_; |
| 992 image->capture_time_ms_ = output_render_time_ms_; | 1033 image->capture_time_ms_ = output_render_time_ms_; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1072 } | 1113 } |
| 1073 scPosition += naluPosition; | 1114 scPosition += naluPosition; |
| 1074 scPositions[scPositionsLength++] = scPosition; | 1115 scPositions[scPositionsLength++] = scPosition; |
| 1075 scPosition += H264_SC_LENGTH; | 1116 scPosition += H264_SC_LENGTH; |
| 1076 } | 1117 } |
| 1077 if (scPositionsLength == 0) { | 1118 if (scPositionsLength == 0) { |
| 1078 ALOGE << "Start code is not found!"; | 1119 ALOGE << "Start code is not found!"; |
| 1079 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] | 1120 ALOGE << "Data:" << image->_buffer[0] << " " << image->_buffer[1] |
| 1080 << " " << image->_buffer[2] << " " << image->_buffer[3] | 1121 << " " << image->_buffer[2] << " " << image->_buffer[3] |
| 1081 << " " << image->_buffer[4] << " " << image->_buffer[5]; | 1122 << " " << image->_buffer[4] << " " << image->_buffer[5]; |
| 1082 ResetCodecOnCodecThread(); | 1123 sw_fallback_required_ = true; |
|
AlexG
2016/08/31 00:25:58
You can not fallback to SW for H.264 encoder - we
sakal
2016/09/02 08:08:01
Done.
| |
| 1083 return false; | 1124 return false; |
| 1084 } | 1125 } |
| 1085 scPositions[scPositionsLength] = payload_size; | 1126 scPositions[scPositionsLength] = payload_size; |
| 1086 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); | 1127 header.VerifyAndAllocateFragmentationHeader(scPositionsLength); |
| 1087 for (size_t i = 0; i < scPositionsLength; i++) { | 1128 for (size_t i = 0; i < scPositionsLength; i++) { |
| 1088 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; | 1129 header.fragmentationOffset[i] = scPositions[i] + H264_SC_LENGTH; |
| 1089 header.fragmentationLength[i] = | 1130 header.fragmentationLength[i] = |
| 1090 scPositions[i + 1] - header.fragmentationOffset[i]; | 1131 scPositions[i + 1] - header.fragmentationOffset[i]; |
| 1091 header.fragmentationPlType[i] = 0; | 1132 header.fragmentationPlType[i] = 0; |
| 1092 header.fragmentationTimeDiff[i] = 0; | 1133 header.fragmentationTimeDiff[i] = 0; |
| 1093 } | 1134 } |
| 1094 } | 1135 } |
| 1095 | 1136 |
| 1096 callback_status = callback_->Encoded(*image, &info, &header); | 1137 callback_status = callback_->Encoded(*image, &info, &header); |
| 1097 } | 1138 } |
| 1098 | 1139 |
| 1099 // Return output buffer back to the encoder. | 1140 // Return output buffer back to the encoder. |
| 1100 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 1141 bool success = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
| 1101 j_release_output_buffer_method_, | 1142 j_release_output_buffer_method_, |
| 1102 output_buffer_index); | 1143 output_buffer_index); |
| 1103 CHECK_EXCEPTION(jni); | 1144 if (CheckException(jni) || !success) { |
| 1104 if (!success) { | 1145 sw_fallback_required_ = true; |
| 1105 ResetCodecOnCodecThread(); | |
| 1106 return false; | 1146 return false; |
| 1107 } | 1147 } |
| 1108 | 1148 |
| 1109 // Print per frame statistics. | 1149 // Print per frame statistics. |
| 1110 if (encoding_start_time_ms > 0) { | 1150 if (encoding_start_time_ms > 0) { |
| 1111 frame_encoding_time_ms = rtc::TimeMillis() - encoding_start_time_ms; | 1151 frame_encoding_time_ms = rtc::TimeMillis() - encoding_start_time_ms; |
| 1112 } | 1152 } |
| 1113 if (frames_encoded_ < kMaxEncodedLogFrames) { | 1153 if (frames_encoded_ < kMaxEncodedLogFrames) { |
| 1114 int current_latency = | 1154 int current_latency = |
| 1115 (int)(last_input_timestamp_ms_ - last_output_timestamp_ms_); | 1155 (int)(last_input_timestamp_ms_ - last_output_timestamp_ms_); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1297 return supported_codecs_; | 1337 return supported_codecs_; |
| 1298 } | 1338 } |
| 1299 | 1339 |
| 1300 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1340 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
| 1301 webrtc::VideoEncoder* encoder) { | 1341 webrtc::VideoEncoder* encoder) { |
| 1302 ALOGD << "Destroy video encoder."; | 1342 ALOGD << "Destroy video encoder."; |
| 1303 delete encoder; | 1343 delete encoder; |
| 1304 } | 1344 } |
| 1305 | 1345 |
| 1306 } // namespace webrtc_jni | 1346 } // namespace webrtc_jni |
| OLD | NEW |