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 12 matching lines...) Expand all Loading... |
23 #include "webrtc/api/android/jni/classreferenceholder.h" | 23 #include "webrtc/api/android/jni/classreferenceholder.h" |
24 #include "webrtc/api/android/jni/native_handle_impl.h" | 24 #include "webrtc/api/android/jni/native_handle_impl.h" |
25 #include "webrtc/base/bind.h" | 25 #include "webrtc/base/bind.h" |
26 #include "webrtc/base/checks.h" | 26 #include "webrtc/base/checks.h" |
27 #include "webrtc/base/logging.h" | 27 #include "webrtc/base/logging.h" |
28 #include "webrtc/base/thread.h" | 28 #include "webrtc/base/thread.h" |
29 #include "webrtc/base/thread_checker.h" | 29 #include "webrtc/base/thread_checker.h" |
30 #include "webrtc/base/timeutils.h" | 30 #include "webrtc/base/timeutils.h" |
31 #include "webrtc/common_types.h" | 31 #include "webrtc/common_types.h" |
32 #include "webrtc/common_video/h264/h264_bitstream_parser.h" | 32 #include "webrtc/common_video/h264/h264_bitstream_parser.h" |
33 #include "webrtc/media/engine/internalencoderfactory.h" | |
34 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 33 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
35 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 34 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
36 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" | 35 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" |
37 #include "webrtc/system_wrappers/include/field_trial.h" | 36 #include "webrtc/system_wrappers/include/field_trial.h" |
38 #include "webrtc/system_wrappers/include/logcat_trace_context.h" | 37 #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
39 | 38 |
40 using rtc::Bind; | 39 using rtc::Bind; |
41 using rtc::Thread; | 40 using rtc::Thread; |
42 using rtc::ThreadManager; | 41 using rtc::ThreadManager; |
43 | 42 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // Android's MediaCodec SDK API behind the scenes to implement (hopefully) | 89 // Android's MediaCodec SDK API behind the scenes to implement (hopefully) |
91 // HW-backed video encode. This C++ class is implemented as a very thin shim, | 90 // HW-backed video encode. This C++ class is implemented as a very thin shim, |
92 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. | 91 // delegating all of the interesting work to org.webrtc.MediaCodecVideoEncoder. |
93 // MediaCodecVideoEncoder is created, operated, and destroyed on a single | 92 // MediaCodecVideoEncoder is created, operated, and destroyed on a single |
94 // thread, currently the libjingle Worker thread. | 93 // thread, currently the libjingle Worker thread. |
95 class MediaCodecVideoEncoder : public webrtc::VideoEncoder, | 94 class MediaCodecVideoEncoder : public webrtc::VideoEncoder, |
96 public rtc::MessageHandler { | 95 public rtc::MessageHandler { |
97 public: | 96 public: |
98 virtual ~MediaCodecVideoEncoder(); | 97 virtual ~MediaCodecVideoEncoder(); |
99 MediaCodecVideoEncoder(JNIEnv* jni, | 98 MediaCodecVideoEncoder(JNIEnv* jni, |
100 const cricket::VideoCodec& codec, | 99 VideoCodecType codecType, |
101 jobject egl_context); | 100 jobject egl_context); |
102 | 101 |
103 // webrtc::VideoEncoder implementation. Everything trampolines to | 102 // webrtc::VideoEncoder implementation. Everything trampolines to |
104 // |codec_thread_| for execution. | 103 // |codec_thread_| for execution. |
105 int32_t InitEncode(const webrtc::VideoCodec* codec_settings, | 104 int32_t InitEncode(const webrtc::VideoCodec* codec_settings, |
106 int32_t /* number_of_cores */, | 105 int32_t /* number_of_cores */, |
107 size_t /* max_payload_size */) override; | 106 size_t /* max_payload_size */) override; |
108 int32_t Encode(const webrtc::VideoFrame& input_image, | 107 int32_t Encode(const webrtc::VideoFrame& input_image, |
109 const webrtc::CodecSpecificInfo* /* codec_specific_info */, | 108 const webrtc::CodecSpecificInfo* /* codec_specific_info */, |
110 const std::vector<webrtc::FrameType>* frame_types) override; | 109 const std::vector<webrtc::FrameType>* frame_types) override; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 // true on success. | 179 // true on success. |
181 bool DeliverPendingOutputs(JNIEnv* jni); | 180 bool DeliverPendingOutputs(JNIEnv* jni); |
182 | 181 |
183 // Search for H.264 start codes. | 182 // Search for H.264 start codes. |
184 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); | 183 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
185 | 184 |
186 // Displays encoder statistics. | 185 // Displays encoder statistics. |
187 void LogStatistics(bool force_log); | 186 void LogStatistics(bool force_log); |
188 | 187 |
189 // Type of video codec. | 188 // Type of video codec. |
190 const cricket::VideoCodec codec_; | 189 VideoCodecType codecType_; |
191 | 190 |
192 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to | 191 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
193 // |codec_thread_| synchronously. | 192 // |codec_thread_| synchronously. |
194 webrtc::EncodedImageCallback* callback_; | 193 webrtc::EncodedImageCallback* callback_; |
195 | 194 |
196 // State that is constant for the lifetime of this object once the ctor | 195 // State that is constant for the lifetime of this object once the ctor |
197 // returns. | 196 // returns. |
198 std::unique_ptr<Thread> | 197 std::unique_ptr<Thread> |
199 codec_thread_; // Thread on which to operate MediaCodec. | 198 codec_thread_; // Thread on which to operate MediaCodec. |
200 rtc::ThreadChecker codec_thread_checker_; | 199 rtc::ThreadChecker codec_thread_checker_; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 | 295 |
297 bool sw_fallback_required_; | 296 bool sw_fallback_required_; |
298 }; | 297 }; |
299 | 298 |
300 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { | 299 MediaCodecVideoEncoder::~MediaCodecVideoEncoder() { |
301 // Call Release() to ensure no more callbacks to us after we are deleted. | 300 // Call Release() to ensure no more callbacks to us after we are deleted. |
302 Release(); | 301 Release(); |
303 } | 302 } |
304 | 303 |
305 MediaCodecVideoEncoder::MediaCodecVideoEncoder(JNIEnv* jni, | 304 MediaCodecVideoEncoder::MediaCodecVideoEncoder(JNIEnv* jni, |
306 const cricket::VideoCodec& codec, | 305 VideoCodecType codecType, |
307 jobject egl_context) | 306 jobject egl_context) |
308 : codec_(codec), | 307 : codecType_(codecType), |
309 callback_(NULL), | 308 callback_(NULL), |
310 codec_thread_(new Thread()), | 309 codec_thread_(new Thread()), |
311 j_media_codec_video_encoder_class_( | 310 j_media_codec_video_encoder_class_( |
312 jni, | 311 jni, |
313 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), | 312 FindClass(jni, "org/webrtc/MediaCodecVideoEncoder")), |
314 j_media_codec_video_encoder_( | 313 j_media_codec_video_encoder_( |
315 jni, | 314 jni, |
316 jni->NewObject(*j_media_codec_video_encoder_class_, | 315 jni->NewObject(*j_media_codec_video_encoder_class_, |
317 GetMethodID(jni, | 316 GetMethodID(jni, |
318 *j_media_codec_video_encoder_class_, | 317 *j_media_codec_video_encoder_class_, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 | 385 |
387 int32_t MediaCodecVideoEncoder::InitEncode( | 386 int32_t MediaCodecVideoEncoder::InitEncode( |
388 const webrtc::VideoCodec* codec_settings, | 387 const webrtc::VideoCodec* codec_settings, |
389 int32_t /* number_of_cores */, | 388 int32_t /* number_of_cores */, |
390 size_t /* max_payload_size */) { | 389 size_t /* max_payload_size */) { |
391 if (codec_settings == NULL) { | 390 if (codec_settings == NULL) { |
392 ALOGE << "NULL VideoCodec instance"; | 391 ALOGE << "NULL VideoCodec instance"; |
393 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; | 392 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
394 } | 393 } |
395 // Factory should guard against other codecs being used with us. | 394 // Factory should guard against other codecs being used with us. |
396 const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); | 395 RTC_CHECK(codec_settings->codecType == codecType_) |
397 RTC_CHECK(codec_settings->codecType == codec_type) | |
398 << "Unsupported codec " << codec_settings->codecType << " for " | 396 << "Unsupported codec " << codec_settings->codecType << " for " |
399 << codec_type; | 397 << codecType_; |
400 if (sw_fallback_required_) { | 398 if (sw_fallback_required_) { |
401 return WEBRTC_VIDEO_CODEC_OK; | 399 return WEBRTC_VIDEO_CODEC_OK; |
402 } | 400 } |
403 codec_mode_ = codec_settings->mode; | 401 codec_mode_ = codec_settings->mode; |
404 int init_width = codec_settings->width; | 402 int init_width = codec_settings->width; |
405 int init_height = codec_settings->height; | 403 int init_height = codec_settings->height; |
406 // Scaling is disabled for VP9, but optionally enabled for VP8. | 404 // Scaling is disabled for VP9, but optionally enabled for VP8. |
407 // TODO(pbos): Extract automaticResizeOn out of VP8 settings. | 405 // TODO(pbos): Extract automaticResizeOn out of VP8 settings. |
408 scale_ = false; | 406 scale_ = false; |
409 if (codec_type == kVideoCodecVP8) { | 407 if (codecType_ == kVideoCodecVP8) { |
410 scale_ = codec_settings->VP8().automaticResizeOn; | 408 scale_ = codec_settings->VP8().automaticResizeOn; |
411 } else if (codec_type != kVideoCodecVP9) { | 409 } else if (codecType_ != kVideoCodecVP9) { |
412 scale_ = true; | 410 scale_ = true; |
413 } | 411 } |
414 | 412 |
415 ALOGD << "InitEncode request: " << init_width << " x " << init_height; | 413 ALOGD << "InitEncode request: " << init_width << " x " << init_height; |
416 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); | 414 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); |
417 | 415 |
418 if (scale_) { | 416 if (scale_) { |
419 if (codec_type == kVideoCodecVP8 || codec_type == kVideoCodecH264) { | 417 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecH264) { |
420 quality_scaler_.Init(codec_type, codec_settings->startBitrate, | 418 quality_scaler_.Init(codecType_, codec_settings->startBitrate, |
421 codec_settings->width, codec_settings->height, | 419 codec_settings->width, codec_settings->height, |
422 codec_settings->maxFramerate); | 420 codec_settings->maxFramerate); |
423 } else { | 421 } else { |
424 // When adding codec support to additional hardware codecs, also configure | 422 // When adding codec support to additional hardware codecs, also configure |
425 // their QP thresholds for scaling. | 423 // their QP thresholds for scaling. |
426 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; | 424 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; |
427 scale_ = false; | 425 scale_ = false; |
428 } | 426 } |
429 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); | 427 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); |
430 init_width = res.width; | 428 init_width = res.width; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 WEBRTC_VIDEO_CODEC_OK) { | 515 WEBRTC_VIDEO_CODEC_OK) { |
518 ALOGE << "Initializing encoder failed during reset."; | 516 ALOGE << "Initializing encoder failed during reset."; |
519 return false; | 517 return false; |
520 } | 518 } |
521 return true; | 519 return true; |
522 } | 520 } |
523 | 521 |
524 bool MediaCodecVideoEncoder::ProcessHWErrorOnCodecThread( | 522 bool MediaCodecVideoEncoder::ProcessHWErrorOnCodecThread( |
525 bool reset_if_fallback_unavailable) { | 523 bool reset_if_fallback_unavailable) { |
526 ALOGE << "ProcessHWErrorOnCodecThread"; | 524 ALOGE << "ProcessHWErrorOnCodecThread"; |
527 if (FindMatchingCodec( | 525 if (VideoEncoder::IsSupportedSoftware( |
528 cricket::InternalEncoderFactory::GetInstance().supported_codecs(), | 526 VideoEncoder::CodecToEncoderType(codecType_))) { |
529 codec_)) { | |
530 ALOGE << "Fallback to SW encoder."; | 527 ALOGE << "Fallback to SW encoder."; |
531 sw_fallback_required_ = true; | 528 sw_fallback_required_ = true; |
532 return false; | 529 return false; |
533 } else if (reset_if_fallback_unavailable) { | 530 } else if (reset_if_fallback_unavailable) { |
534 ALOGE << "Reset encoder."; | 531 ALOGE << "Reset encoder."; |
535 return ResetCodecOnCodecThread(); | 532 return ResetCodecOnCodecThread(); |
536 } | 533 } |
537 return false; | 534 return false; |
538 } | 535 } |
539 | 536 |
540 int32_t MediaCodecVideoEncoder::ProcessHWErrorOnEncodeOnCodecThread() { | 537 int32_t MediaCodecVideoEncoder::ProcessHWErrorOnEncodeOnCodecThread() { |
541 ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); | 538 ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
542 return sw_fallback_required_ ? WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE | 539 return sw_fallback_required_ ? WEBRTC_VIDEO_CODEC_FALLBACK_SOFTWARE |
543 : WEBRTC_VIDEO_CODEC_ERROR; | 540 : WEBRTC_VIDEO_CODEC_ERROR; |
544 } | 541 } |
545 | 542 |
546 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( | 543 int32_t MediaCodecVideoEncoder::InitEncodeOnCodecThread( |
547 int width, int height, int kbps, int fps, bool use_surface) { | 544 int width, int height, int kbps, int fps, bool use_surface) { |
548 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 545 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
549 if (sw_fallback_required_) { | 546 if (sw_fallback_required_) { |
550 return WEBRTC_VIDEO_CODEC_OK; | 547 return WEBRTC_VIDEO_CODEC_OK; |
551 } | 548 } |
552 RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set."; | 549 RTC_CHECK(!use_surface || egl_context_ != nullptr) << "EGL context not set."; |
553 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 550 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
554 ScopedLocalRefFrame local_ref_frame(jni); | 551 ScopedLocalRefFrame local_ref_frame(jni); |
555 | 552 |
556 const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); | 553 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codecType_ << ", " << |
557 ALOGD << "InitEncodeOnCodecThread Type: " << (int)codec_type << ", " << width | 554 width << " x " << height << ". Bitrate: " << kbps << |
558 << " x " << height << ". Bitrate: " << kbps << " kbps. Fps: " << fps; | 555 " kbps. Fps: " << fps; |
559 if (kbps == 0) { | 556 if (kbps == 0) { |
560 kbps = last_set_bitrate_kbps_; | 557 kbps = last_set_bitrate_kbps_; |
561 } | 558 } |
562 if (fps == 0) { | 559 if (fps == 0) { |
563 fps = MAX_VIDEO_FPS; | 560 fps = MAX_VIDEO_FPS; |
564 } | 561 } |
565 | 562 |
566 width_ = width; | 563 width_ = width; |
567 height_ = height; | 564 height_ = height; |
568 last_set_bitrate_kbps_ = kbps; | 565 last_set_bitrate_kbps_ = kbps; |
(...skipping 18 matching lines...) Expand all Loading... |
587 use_surface_ = use_surface; | 584 use_surface_ = use_surface; |
588 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; | 585 picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF; |
589 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); | 586 gof_.SetGofInfoVP9(webrtc::TemporalStructureMode::kTemporalStructureMode1); |
590 tl0_pic_idx_ = static_cast<uint8_t>(rand()); | 587 tl0_pic_idx_ = static_cast<uint8_t>(rand()); |
591 gof_idx_ = 0; | 588 gof_idx_ = 0; |
592 last_frame_received_ms_ = -1; | 589 last_frame_received_ms_ = -1; |
593 frames_received_since_last_key_ = kMinKeyFrameInterval; | 590 frames_received_since_last_key_ = kMinKeyFrameInterval; |
594 | 591 |
595 // We enforce no extra stride/padding in the format creation step. | 592 // We enforce no extra stride/padding in the format creation step. |
596 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( | 593 jobject j_video_codec_enum = JavaEnumFromIndexAndClassName( |
597 jni, "MediaCodecVideoEncoder$VideoCodecType", codec_type); | 594 jni, "MediaCodecVideoEncoder$VideoCodecType", codecType_); |
598 const bool encode_status = jni->CallBooleanMethod( | 595 const bool encode_status = jni->CallBooleanMethod( |
599 *j_media_codec_video_encoder_, j_init_encode_method_, | 596 *j_media_codec_video_encoder_, j_init_encode_method_, |
600 j_video_codec_enum, width, height, kbps, fps, | 597 j_video_codec_enum, width, height, kbps, fps, |
601 (use_surface ? egl_context_ : nullptr)); | 598 (use_surface ? egl_context_ : nullptr)); |
602 if (!encode_status) { | 599 if (!encode_status) { |
603 ALOGE << "Failed to configure encoder."; | 600 ALOGE << "Failed to configure encoder."; |
604 ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); | 601 ProcessHWErrorOnCodecThread(false /* reset_if_fallback_unavailable */); |
605 return WEBRTC_VIDEO_CODEC_ERROR; | 602 return WEBRTC_VIDEO_CODEC_ERROR; |
606 } | 603 } |
607 if (CheckException(jni)) { | 604 if (CheckException(jni)) { |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer); | 1058 size_t payload_size = jni->GetDirectBufferCapacity(j_output_buffer); |
1062 uint8_t* payload = reinterpret_cast<uint8_t*>( | 1059 uint8_t* payload = reinterpret_cast<uint8_t*>( |
1063 jni->GetDirectBufferAddress(j_output_buffer)); | 1060 jni->GetDirectBufferAddress(j_output_buffer)); |
1064 if (CheckException(jni)) { | 1061 if (CheckException(jni)) { |
1065 ALOGE << "Exception in get direct buffer address."; | 1062 ALOGE << "Exception in get direct buffer address."; |
1066 ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); | 1063 ProcessHWErrorOnCodecThread(true /* reset_if_fallback_unavailable */); |
1067 return WEBRTC_VIDEO_CODEC_ERROR; | 1064 return WEBRTC_VIDEO_CODEC_ERROR; |
1068 } | 1065 } |
1069 | 1066 |
1070 // Callback - return encoded frame. | 1067 // Callback - return encoded frame. |
1071 const VideoCodecType codec_type = cricket::CodecTypeFromName(codec_.name); | |
1072 webrtc::EncodedImageCallback::Result callback_result( | 1068 webrtc::EncodedImageCallback::Result callback_result( |
1073 webrtc::EncodedImageCallback::Result::OK); | 1069 webrtc::EncodedImageCallback::Result::OK); |
1074 if (callback_) { | 1070 if (callback_) { |
1075 std::unique_ptr<webrtc::EncodedImage> image( | 1071 std::unique_ptr<webrtc::EncodedImage> image( |
1076 new webrtc::EncodedImage(payload, payload_size, payload_size)); | 1072 new webrtc::EncodedImage(payload, payload_size, payload_size)); |
1077 image->_encodedWidth = width_; | 1073 image->_encodedWidth = width_; |
1078 image->_encodedHeight = height_; | 1074 image->_encodedHeight = height_; |
1079 image->_timeStamp = output_timestamp_; | 1075 image->_timeStamp = output_timestamp_; |
1080 image->capture_time_ms_ = output_render_time_ms_; | 1076 image->capture_time_ms_ = output_render_time_ms_; |
1081 image->rotation_ = output_rotation_; | 1077 image->rotation_ = output_rotation_; |
1082 image->_frameType = | 1078 image->_frameType = |
1083 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); | 1079 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); |
1084 image->_completeFrame = true; | 1080 image->_completeFrame = true; |
1085 image->adapt_reason_.quality_resolution_downscales = | 1081 image->adapt_reason_.quality_resolution_downscales = |
1086 scale_ ? quality_scaler_.downscale_shift() : -1; | 1082 scale_ ? quality_scaler_.downscale_shift() : -1; |
1087 | 1083 |
1088 webrtc::CodecSpecificInfo info; | 1084 webrtc::CodecSpecificInfo info; |
1089 memset(&info, 0, sizeof(info)); | 1085 memset(&info, 0, sizeof(info)); |
1090 info.codecType = codec_type; | 1086 info.codecType = codecType_; |
1091 if (codec_type == kVideoCodecVP8) { | 1087 if (codecType_ == kVideoCodecVP8) { |
1092 info.codecSpecific.VP8.pictureId = picture_id_; | 1088 info.codecSpecific.VP8.pictureId = picture_id_; |
1093 info.codecSpecific.VP8.nonReference = false; | 1089 info.codecSpecific.VP8.nonReference = false; |
1094 info.codecSpecific.VP8.simulcastIdx = 0; | 1090 info.codecSpecific.VP8.simulcastIdx = 0; |
1095 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; | 1091 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; |
1096 info.codecSpecific.VP8.layerSync = false; | 1092 info.codecSpecific.VP8.layerSync = false; |
1097 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; | 1093 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; |
1098 info.codecSpecific.VP8.keyIdx = webrtc::kNoKeyIdx; | 1094 info.codecSpecific.VP8.keyIdx = webrtc::kNoKeyIdx; |
1099 } else if (codec_type == kVideoCodecVP9) { | 1095 } else if (codecType_ == kVideoCodecVP9) { |
1100 if (key_frame) { | 1096 if (key_frame) { |
1101 gof_idx_ = 0; | 1097 gof_idx_ = 0; |
1102 } | 1098 } |
1103 info.codecSpecific.VP9.picture_id = picture_id_; | 1099 info.codecSpecific.VP9.picture_id = picture_id_; |
1104 info.codecSpecific.VP9.inter_pic_predicted = key_frame ? false : true; | 1100 info.codecSpecific.VP9.inter_pic_predicted = key_frame ? false : true; |
1105 info.codecSpecific.VP9.flexible_mode = false; | 1101 info.codecSpecific.VP9.flexible_mode = false; |
1106 info.codecSpecific.VP9.ss_data_available = key_frame ? true : false; | 1102 info.codecSpecific.VP9.ss_data_available = key_frame ? true : false; |
1107 info.codecSpecific.VP9.tl0_pic_idx = tl0_pic_idx_++; | 1103 info.codecSpecific.VP9.tl0_pic_idx = tl0_pic_idx_++; |
1108 info.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx; | 1104 info.codecSpecific.VP9.temporal_idx = webrtc::kNoTemporalIdx; |
1109 info.codecSpecific.VP9.spatial_idx = webrtc::kNoSpatialIdx; | 1105 info.codecSpecific.VP9.spatial_idx = webrtc::kNoSpatialIdx; |
1110 info.codecSpecific.VP9.temporal_up_switch = true; | 1106 info.codecSpecific.VP9.temporal_up_switch = true; |
1111 info.codecSpecific.VP9.inter_layer_predicted = false; | 1107 info.codecSpecific.VP9.inter_layer_predicted = false; |
1112 info.codecSpecific.VP9.gof_idx = | 1108 info.codecSpecific.VP9.gof_idx = |
1113 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof); | 1109 static_cast<uint8_t>(gof_idx_++ % gof_.num_frames_in_gof); |
1114 info.codecSpecific.VP9.num_spatial_layers = 1; | 1110 info.codecSpecific.VP9.num_spatial_layers = 1; |
1115 info.codecSpecific.VP9.spatial_layer_resolution_present = false; | 1111 info.codecSpecific.VP9.spatial_layer_resolution_present = false; |
1116 if (info.codecSpecific.VP9.ss_data_available) { | 1112 if (info.codecSpecific.VP9.ss_data_available) { |
1117 info.codecSpecific.VP9.spatial_layer_resolution_present = true; | 1113 info.codecSpecific.VP9.spatial_layer_resolution_present = true; |
1118 info.codecSpecific.VP9.width[0] = width_; | 1114 info.codecSpecific.VP9.width[0] = width_; |
1119 info.codecSpecific.VP9.height[0] = height_; | 1115 info.codecSpecific.VP9.height[0] = height_; |
1120 info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_); | 1116 info.codecSpecific.VP9.gof.CopyGofInfoVP9(gof_); |
1121 } | 1117 } |
1122 } | 1118 } |
1123 picture_id_ = (picture_id_ + 1) & 0x7FFF; | 1119 picture_id_ = (picture_id_ + 1) & 0x7FFF; |
1124 | 1120 |
1125 // Generate a header describing a single fragment. | 1121 // Generate a header describing a single fragment. |
1126 webrtc::RTPFragmentationHeader header; | 1122 webrtc::RTPFragmentationHeader header; |
1127 memset(&header, 0, sizeof(header)); | 1123 memset(&header, 0, sizeof(header)); |
1128 if (codec_type == kVideoCodecVP8 || codec_type == kVideoCodecVP9) { | 1124 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { |
1129 header.VerifyAndAllocateFragmentationHeader(1); | 1125 header.VerifyAndAllocateFragmentationHeader(1); |
1130 header.fragmentationOffset[0] = 0; | 1126 header.fragmentationOffset[0] = 0; |
1131 header.fragmentationLength[0] = image->_length; | 1127 header.fragmentationLength[0] = image->_length; |
1132 header.fragmentationPlType[0] = 0; | 1128 header.fragmentationPlType[0] = 0; |
1133 header.fragmentationTimeDiff[0] = 0; | 1129 header.fragmentationTimeDiff[0] = 0; |
1134 if (codec_type == kVideoCodecVP8 && scale_) { | 1130 if (codecType_ == kVideoCodecVP8 && scale_) { |
1135 int qp; | 1131 int qp; |
1136 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { | 1132 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { |
1137 current_acc_qp_ += qp; | 1133 current_acc_qp_ += qp; |
1138 quality_scaler_.ReportQP(qp); | 1134 quality_scaler_.ReportQP(qp); |
1139 image->qp_ = qp; | 1135 image->qp_ = qp; |
1140 } | 1136 } |
1141 } | 1137 } |
1142 } else if (codec_type == kVideoCodecH264) { | 1138 } else if (codecType_ == kVideoCodecH264) { |
1143 if (scale_) { | 1139 if (scale_) { |
1144 h264_bitstream_parser_.ParseBitstream(payload, payload_size); | 1140 h264_bitstream_parser_.ParseBitstream(payload, payload_size); |
1145 int qp; | 1141 int qp; |
1146 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { | 1142 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { |
1147 current_acc_qp_ += qp; | 1143 current_acc_qp_ += qp; |
1148 quality_scaler_.ReportQP(qp); | 1144 quality_scaler_.ReportQP(qp); |
1149 } | 1145 } |
1150 } | 1146 } |
1151 // For H.264 search for start codes. | 1147 // For H.264 search for start codes. |
1152 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; | 1148 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1356 } | 1352 } |
1357 | 1353 |
1358 webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder( | 1354 webrtc::VideoEncoder* MediaCodecVideoEncoderFactory::CreateVideoEncoder( |
1359 const cricket::VideoCodec& codec) { | 1355 const cricket::VideoCodec& codec) { |
1360 if (supported_codecs_.empty()) { | 1356 if (supported_codecs_.empty()) { |
1361 ALOGW << "No HW video encoder for codec " << codec.name; | 1357 ALOGW << "No HW video encoder for codec " << codec.name; |
1362 return nullptr; | 1358 return nullptr; |
1363 } | 1359 } |
1364 if (FindMatchingCodec(supported_codecs_, codec)) { | 1360 if (FindMatchingCodec(supported_codecs_, codec)) { |
1365 ALOGD << "Create HW video encoder for " << codec.name; | 1361 ALOGD << "Create HW video encoder for " << codec.name; |
1366 return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), codec, | 1362 const VideoCodecType type = cricket::CodecTypeFromName(codec.name); |
| 1363 return new MediaCodecVideoEncoder(AttachCurrentThreadIfNeeded(), type, |
1367 egl_context_); | 1364 egl_context_); |
1368 } | 1365 } |
1369 ALOGW << "Can not find HW video encoder for type " << codec.name; | 1366 ALOGW << "Can not find HW video encoder for type " << codec.name; |
1370 return nullptr; | 1367 return nullptr; |
1371 } | 1368 } |
1372 | 1369 |
1373 const std::vector<cricket::VideoCodec>& | 1370 const std::vector<cricket::VideoCodec>& |
1374 MediaCodecVideoEncoderFactory::supported_codecs() const { | 1371 MediaCodecVideoEncoderFactory::supported_codecs() const { |
1375 return supported_codecs_; | 1372 return supported_codecs_; |
1376 } | 1373 } |
1377 | 1374 |
1378 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1375 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
1379 webrtc::VideoEncoder* encoder) { | 1376 webrtc::VideoEncoder* encoder) { |
1380 ALOGD << "Destroy video encoder."; | 1377 ALOGD << "Destroy video encoder."; |
1381 delete encoder; | 1378 delete encoder; |
1382 } | 1379 } |
1383 | 1380 |
1384 } // namespace webrtc_jni | 1381 } // namespace webrtc_jni |
OLD | NEW |