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 17 matching lines...) Expand all Loading... |
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/modules/video_coding/include/video_codec_interface.h" | 33 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
34 #include "webrtc/modules/video_coding/utility/quality_scaler.h" | 34 #include "webrtc/modules/video_coding/utility/quality_scaler.h" |
35 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" | 35 #include "webrtc/modules/video_coding/utility/vp8_header_parser.h" |
36 #include "webrtc/system_wrappers/include/field_trial.h" | 36 #include "webrtc/system_wrappers/include/field_trial.h" |
37 #include "webrtc/system_wrappers/include/logcat_trace_context.h" | 37 #include "webrtc/system_wrappers/include/logcat_trace_context.h" |
| 38 #include "webrtc/video_encoder.h" |
38 | 39 |
39 using rtc::Bind; | 40 using rtc::Bind; |
40 using rtc::Thread; | 41 using rtc::Thread; |
41 using rtc::ThreadManager; | 42 using rtc::ThreadManager; |
42 | 43 |
43 using webrtc::CodecSpecificInfo; | 44 using webrtc::CodecSpecificInfo; |
44 using webrtc::EncodedImage; | 45 using webrtc::EncodedImage; |
45 using webrtc::VideoFrame; | 46 using webrtc::VideoFrame; |
46 using webrtc::RTPFragmentationHeader; | 47 using webrtc::RTPFragmentationHeader; |
47 using webrtc::VideoCodec; | 48 using webrtc::VideoCodec; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 webrtc::EncodedImageCallback* callback) override; | 112 webrtc::EncodedImageCallback* callback) override; |
112 int32_t Release() override; | 113 int32_t Release() override; |
113 int32_t SetChannelParameters(uint32_t /* packet_loss */, | 114 int32_t SetChannelParameters(uint32_t /* packet_loss */, |
114 int64_t /* rtt */) override; | 115 int64_t /* rtt */) override; |
115 int32_t SetRateAllocation(const webrtc::BitrateAllocation& rate_allocation, | 116 int32_t SetRateAllocation(const webrtc::BitrateAllocation& rate_allocation, |
116 uint32_t frame_rate) override; | 117 uint32_t frame_rate) override; |
117 | 118 |
118 // rtc::MessageHandler implementation. | 119 // rtc::MessageHandler implementation. |
119 void OnMessage(rtc::Message* msg) override; | 120 void OnMessage(rtc::Message* msg) override; |
120 | 121 |
121 void OnDroppedFrame() override; | |
122 | |
123 bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } | 122 bool SupportsNativeHandle() const override { return egl_context_ != nullptr; } |
124 const char* ImplementationName() const override; | 123 const char* ImplementationName() const override; |
125 | 124 |
126 private: | 125 private: |
127 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and | 126 // ResetCodecOnCodecThread() calls ReleaseOnCodecThread() and |
128 // InitEncodeOnCodecThread() in an attempt to restore the codec to an | 127 // InitEncodeOnCodecThread() in an attempt to restore the codec to an |
129 // operable state. Necessary after all manner of OMX-layer errors. | 128 // operable state. Necessary after all manner of OMX-layer errors. |
130 // Returns true if the codec was reset successfully. | 129 // Returns true if the codec was reset successfully. |
131 bool ResetCodecOnCodecThread(); | 130 bool ResetCodecOnCodecThread(); |
132 | 131 |
(...skipping 26 matching lines...) Expand all Loading... |
159 const int64_t frame_input_time_ms); | 158 const int64_t frame_input_time_ms); |
160 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, | 159 bool EncodeByteBufferOnCodecThread(JNIEnv* jni, |
161 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); | 160 bool key_frame, const webrtc::VideoFrame& frame, int input_buffer_index); |
162 bool EncodeTextureOnCodecThread(JNIEnv* jni, | 161 bool EncodeTextureOnCodecThread(JNIEnv* jni, |
163 bool key_frame, const webrtc::VideoFrame& frame); | 162 bool key_frame, const webrtc::VideoFrame& frame); |
164 | 163 |
165 int32_t RegisterEncodeCompleteCallbackOnCodecThread( | 164 int32_t RegisterEncodeCompleteCallbackOnCodecThread( |
166 webrtc::EncodedImageCallback* callback); | 165 webrtc::EncodedImageCallback* callback); |
167 int32_t ReleaseOnCodecThread(); | 166 int32_t ReleaseOnCodecThread(); |
168 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); | 167 int32_t SetRatesOnCodecThread(uint32_t new_bit_rate, uint32_t frame_rate); |
169 void OnDroppedFrameOnCodecThread(); | |
170 | 168 |
171 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. | 169 // Helper accessors for MediaCodecVideoEncoder$OutputBufferInfo members. |
172 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); | 170 int GetOutputBufferInfoIndex(JNIEnv* jni, jobject j_output_buffer_info); |
173 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); | 171 jobject GetOutputBufferInfoBuffer(JNIEnv* jni, jobject j_output_buffer_info); |
174 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); | 172 bool GetOutputBufferInfoIsKeyFrame(JNIEnv* jni, jobject j_output_buffer_info); |
175 jlong GetOutputBufferInfoPresentationTimestampUs( | 173 jlong GetOutputBufferInfoPresentationTimestampUs( |
176 JNIEnv* jni, jobject j_output_buffer_info); | 174 JNIEnv* jni, jobject j_output_buffer_info); |
177 | 175 |
178 // Deliver any outputs pending in the MediaCodec to our |callback_| and return | 176 // Deliver any outputs pending in the MediaCodec to our |callback_| and return |
179 // true on success. | 177 // true on success. |
180 bool DeliverPendingOutputs(JNIEnv* jni); | 178 bool DeliverPendingOutputs(JNIEnv* jni); |
181 | 179 |
182 // Search for H.264 start codes. | 180 // Search for H.264 start codes. |
183 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); | 181 int32_t NextNaluPosition(uint8_t *buffer, size_t buffer_size); |
184 | 182 |
| 183 VideoEncoder::ScalingSettings GetScalingSettings() const override; |
| 184 |
185 // Displays encoder statistics. | 185 // Displays encoder statistics. |
186 void LogStatistics(bool force_log); | 186 void LogStatistics(bool force_log); |
187 | 187 |
188 // Type of video codec. | 188 // Type of video codec. |
189 VideoCodecType codecType_; | 189 VideoCodecType codecType_; |
190 | 190 |
191 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to | 191 // Valid all the time since RegisterEncodeCompleteCallback() Invoke()s to |
192 // |codec_thread_| synchronously. | 192 // |codec_thread_| synchronously. |
193 webrtc::EncodedImageCallback* callback_; | 193 webrtc::EncodedImageCallback* callback_; |
194 | 194 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 // |input_frame_infos_|. | 260 // |input_frame_infos_|. |
261 int64_t output_render_time_ms_; // Last output frame render time from | 261 int64_t output_render_time_ms_; // Last output frame render time from |
262 // |input_frame_infos_|. | 262 // |input_frame_infos_|. |
263 webrtc::VideoRotation output_rotation_; // Last output frame rotation from | 263 webrtc::VideoRotation output_rotation_; // Last output frame rotation from |
264 // |input_frame_infos_|. | 264 // |input_frame_infos_|. |
265 // Frame size in bytes fed to MediaCodec. | 265 // Frame size in bytes fed to MediaCodec. |
266 int yuv_size_; | 266 int yuv_size_; |
267 // True only when between a callback_->OnEncodedImage() call return a positive | 267 // True only when between a callback_->OnEncodedImage() call return a positive |
268 // value and the next Encode() call being ignored. | 268 // value and the next Encode() call being ignored. |
269 bool drop_next_input_frame_; | 269 bool drop_next_input_frame_; |
| 270 bool scale_; |
270 // Global references; must be deleted in Release(). | 271 // Global references; must be deleted in Release(). |
271 std::vector<jobject> input_buffers_; | 272 std::vector<jobject> input_buffers_; |
272 QualityScaler quality_scaler_; | |
273 // Dynamic resolution change, off by default. | |
274 bool scale_; | |
275 | |
276 // H264 bitstream parser, used to extract QP from encoded bitstreams. | |
277 webrtc::H264BitstreamParser h264_bitstream_parser_; | 273 webrtc::H264BitstreamParser h264_bitstream_parser_; |
278 | 274 |
279 // VP9 variables to populate codec specific structure. | 275 // VP9 variables to populate codec specific structure. |
280 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for | 276 webrtc::GofInfoVP9 gof_; // Contains each frame's temporal information for |
281 // non-flexible VP9 mode. | 277 // non-flexible VP9 mode. |
282 uint8_t tl0_pic_idx_; | 278 uint8_t tl0_pic_idx_; |
283 size_t gof_idx_; | 279 size_t gof_idx_; |
284 | 280 |
285 // EGL context - owned by factory, should not be allocated/destroyed | 281 // EGL context - owned by factory, should not be allocated/destroyed |
286 // by MediaCodecVideoEncoder. | 282 // by MediaCodecVideoEncoder. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 scale_ = false; | 402 scale_ = false; |
407 if (codecType_ == kVideoCodecVP8) { | 403 if (codecType_ == kVideoCodecVP8) { |
408 scale_ = codec_settings->VP8().automaticResizeOn; | 404 scale_ = codec_settings->VP8().automaticResizeOn; |
409 } else if (codecType_ != kVideoCodecVP9) { | 405 } else if (codecType_ != kVideoCodecVP9) { |
410 scale_ = true; | 406 scale_ = true; |
411 } | 407 } |
412 | 408 |
413 ALOGD << "InitEncode request: " << init_width << " x " << init_height; | 409 ALOGD << "InitEncode request: " << init_width << " x " << init_height; |
414 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); | 410 ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled"); |
415 | 411 |
416 if (scale_) { | |
417 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecH264) { | |
418 quality_scaler_.Init(codecType_, codec_settings->startBitrate, | |
419 codec_settings->width, codec_settings->height, | |
420 codec_settings->maxFramerate); | |
421 } else { | |
422 // When adding codec support to additional hardware codecs, also configure | |
423 // their QP thresholds for scaling. | |
424 RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds."; | |
425 scale_ = false; | |
426 } | |
427 QualityScaler::Resolution res = quality_scaler_.GetScaledResolution(); | |
428 init_width = res.width; | |
429 init_height = res.height; | |
430 ALOGD << "Scaled resolution: " << init_width << " x " << init_height; | |
431 } | |
432 | |
433 return codec_thread_->Invoke<int32_t>( | 412 return codec_thread_->Invoke<int32_t>( |
434 RTC_FROM_HERE, | 413 RTC_FROM_HERE, |
435 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, | 414 Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread, this, init_width, |
436 init_height, codec_settings->startBitrate, | 415 init_height, codec_settings->startBitrate, |
437 codec_settings->maxFramerate, | 416 codec_settings->maxFramerate, |
438 codec_settings->expect_encode_from_texture)); | 417 codec_settings->expect_encode_from_texture)); |
439 } | 418 } |
440 | 419 |
441 int32_t MediaCodecVideoEncoder::Encode( | 420 int32_t MediaCodecVideoEncoder::Encode( |
442 const webrtc::VideoFrame& frame, | 421 const webrtc::VideoFrame& frame, |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 682 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
704 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ | 683 << ". Q: " << input_frame_infos_.size() << ". Fps: " << last_set_fps_ |
705 << ". Kbps: " << last_set_bitrate_kbps_; | 684 << ". Kbps: " << last_set_bitrate_kbps_; |
706 } | 685 } |
707 | 686 |
708 if (drop_next_input_frame_) { | 687 if (drop_next_input_frame_) { |
709 ALOGW << "Encoder drop frame - failed callback."; | 688 ALOGW << "Encoder drop frame - failed callback."; |
710 drop_next_input_frame_ = false; | 689 drop_next_input_frame_ = false; |
711 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 690 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
712 frames_dropped_media_encoder_++; | 691 frames_dropped_media_encoder_++; |
713 OnDroppedFrameOnCodecThread(); | |
714 return WEBRTC_VIDEO_CODEC_OK; | 692 return WEBRTC_VIDEO_CODEC_OK; |
715 } | 693 } |
716 | 694 |
717 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; | 695 RTC_CHECK(frame_types->size() == 1) << "Unexpected stream count"; |
718 | 696 |
719 // Check if we accumulated too many frames in encoder input buffers and drop | 697 // Check if we accumulated too many frames in encoder input buffers and drop |
720 // frame if so. | 698 // frame if so. |
721 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { | 699 if (input_frame_infos_.size() > MAX_ENCODER_Q_SIZE) { |
722 ALOGD << "Already " << input_frame_infos_.size() | 700 ALOGD << "Already " << input_frame_infos_.size() |
723 << " frames in the queue, dropping" | 701 << " frames in the queue, dropping" |
724 << ". TS: " << (int)(current_timestamp_us_ / 1000) | 702 << ". TS: " << (int)(current_timestamp_us_ / 1000) |
725 << ". Fps: " << last_set_fps_ | 703 << ". Fps: " << last_set_fps_ |
726 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; | 704 << ". Consecutive drops: " << consecutive_full_queue_frame_drops_; |
727 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 705 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
728 consecutive_full_queue_frame_drops_++; | 706 consecutive_full_queue_frame_drops_++; |
729 if (consecutive_full_queue_frame_drops_ >= | 707 if (consecutive_full_queue_frame_drops_ >= |
730 ENCODER_STALL_FRAMEDROP_THRESHOLD) { | 708 ENCODER_STALL_FRAMEDROP_THRESHOLD) { |
731 ALOGE << "Encoder got stuck."; | 709 ALOGE << "Encoder got stuck."; |
732 return ProcessHWErrorOnEncodeOnCodecThread(); | 710 return ProcessHWErrorOnEncodeOnCodecThread(); |
733 } | 711 } |
734 frames_dropped_media_encoder_++; | 712 frames_dropped_media_encoder_++; |
735 OnDroppedFrameOnCodecThread(); | |
736 return WEBRTC_VIDEO_CODEC_OK; | 713 return WEBRTC_VIDEO_CODEC_OK; |
737 } | 714 } |
738 consecutive_full_queue_frame_drops_ = 0; | 715 consecutive_full_queue_frame_drops_ = 0; |
739 | 716 |
740 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( | 717 rtc::scoped_refptr<webrtc::VideoFrameBuffer> input_buffer( |
741 frame.video_frame_buffer()); | 718 frame.video_frame_buffer()); |
742 if (scale_) { | |
743 // Check framerate before spatial resolution change. | |
744 quality_scaler_.OnEncodeFrame(frame.width(), frame.height()); | |
745 const webrtc::QualityScaler::Resolution scaled_resolution = | |
746 quality_scaler_.GetScaledResolution(); | |
747 if (scaled_resolution.width != frame.width() || | |
748 scaled_resolution.height != frame.height()) { | |
749 if (input_buffer->native_handle() != nullptr) { | |
750 input_buffer = static_cast<AndroidTextureBuffer*>(input_buffer.get()) | |
751 ->CropScaleAndRotate(frame.width(), frame.height(), | |
752 0, 0, | |
753 scaled_resolution.width, | |
754 scaled_resolution.height, | |
755 webrtc::kVideoRotation_0); | |
756 } else { | |
757 input_buffer = quality_scaler_.GetScaledBuffer(input_buffer); | |
758 } | |
759 } | |
760 } | |
761 | 719 |
762 VideoFrame input_frame(input_buffer, frame.timestamp(), | 720 VideoFrame input_frame(input_buffer, frame.timestamp(), |
763 frame.render_time_ms(), frame.rotation()); | 721 frame.render_time_ms(), frame.rotation()); |
764 | 722 |
765 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { | 723 if (!MaybeReconfigureEncoderOnCodecThread(input_frame)) { |
766 ALOGE << "Failed to reconfigure encoder."; | 724 ALOGE << "Failed to reconfigure encoder."; |
767 return WEBRTC_VIDEO_CODEC_ERROR; | 725 return WEBRTC_VIDEO_CODEC_ERROR; |
768 } | 726 } |
769 | 727 |
770 const bool key_frame = | 728 const bool key_frame = |
771 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; | 729 frame_types->front() != webrtc::kVideoFrameDelta || send_key_frame; |
772 bool encode_status = true; | 730 bool encode_status = true; |
773 if (!input_frame.video_frame_buffer()->native_handle()) { | 731 if (!input_frame.video_frame_buffer()->native_handle()) { |
774 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, | 732 int j_input_buffer_index = jni->CallIntMethod(*j_media_codec_video_encoder_, |
775 j_dequeue_input_buffer_method_); | 733 j_dequeue_input_buffer_method_); |
776 if (CheckException(jni)) { | 734 if (CheckException(jni)) { |
777 ALOGE << "Exception in dequeu input buffer."; | 735 ALOGE << "Exception in dequeu input buffer."; |
778 return ProcessHWErrorOnEncodeOnCodecThread(); | 736 return ProcessHWErrorOnEncodeOnCodecThread(); |
779 } | 737 } |
780 if (j_input_buffer_index == -1) { | 738 if (j_input_buffer_index == -1) { |
781 // Video codec falls behind - no input buffer available. | 739 // Video codec falls behind - no input buffer available. |
782 ALOGW << "Encoder drop frame - no input buffers available"; | 740 ALOGW << "Encoder drop frame - no input buffers available"; |
783 if (frames_received_ > 1) { | 741 if (frames_received_ > 1) { |
784 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; | 742 current_timestamp_us_ += rtc::kNumMicrosecsPerSec / last_set_fps_; |
785 frames_dropped_media_encoder_++; | 743 frames_dropped_media_encoder_++; |
786 OnDroppedFrameOnCodecThread(); | |
787 } else { | 744 } else { |
788 // Input buffers are not ready after codec initialization, HW is still | 745 // Input buffers are not ready after codec initialization, HW is still |
789 // allocating thme - this is expected and should not result in drop | 746 // allocating thme - this is expected and should not result in drop |
790 // frame report. | 747 // frame report. |
791 frames_received_ = 0; | 748 frames_received_ = 0; |
792 } | 749 } |
793 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. | 750 return WEBRTC_VIDEO_CODEC_OK; // TODO(fischman): see webrtc bug 2887. |
794 } else if (j_input_buffer_index == -2) { | 751 } else if (j_input_buffer_index == -2) { |
795 return ProcessHWErrorOnEncodeOnCodecThread(); | 752 return ProcessHWErrorOnEncodeOnCodecThread(); |
796 } | 753 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
957 uint32_t frame_rate) { | 914 uint32_t frame_rate) { |
958 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | 915 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); |
959 if (sw_fallback_required_) | 916 if (sw_fallback_required_) |
960 return WEBRTC_VIDEO_CODEC_OK; | 917 return WEBRTC_VIDEO_CODEC_OK; |
961 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? | 918 frame_rate = (frame_rate < MAX_ALLOWED_VIDEO_FPS) ? |
962 frame_rate : MAX_ALLOWED_VIDEO_FPS; | 919 frame_rate : MAX_ALLOWED_VIDEO_FPS; |
963 if (last_set_bitrate_kbps_ == new_bit_rate && | 920 if (last_set_bitrate_kbps_ == new_bit_rate && |
964 last_set_fps_ == frame_rate) { | 921 last_set_fps_ == frame_rate) { |
965 return WEBRTC_VIDEO_CODEC_OK; | 922 return WEBRTC_VIDEO_CODEC_OK; |
966 } | 923 } |
967 if (scale_) { | |
968 quality_scaler_.ReportFramerate(frame_rate); | |
969 } | |
970 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 924 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
971 ScopedLocalRefFrame local_ref_frame(jni); | 925 ScopedLocalRefFrame local_ref_frame(jni); |
972 if (new_bit_rate > 0) { | 926 if (new_bit_rate > 0) { |
973 last_set_bitrate_kbps_ = new_bit_rate; | 927 last_set_bitrate_kbps_ = new_bit_rate; |
974 } | 928 } |
975 if (frame_rate > 0) { | 929 if (frame_rate > 0) { |
976 last_set_fps_ = frame_rate; | 930 last_set_fps_ = frame_rate; |
977 } | 931 } |
978 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, | 932 bool ret = jni->CallBooleanMethod(*j_media_codec_video_encoder_, |
979 j_set_rates_method_, | 933 j_set_rates_method_, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1071 std::unique_ptr<webrtc::EncodedImage> image( | 1025 std::unique_ptr<webrtc::EncodedImage> image( |
1072 new webrtc::EncodedImage(payload, payload_size, payload_size)); | 1026 new webrtc::EncodedImage(payload, payload_size, payload_size)); |
1073 image->_encodedWidth = width_; | 1027 image->_encodedWidth = width_; |
1074 image->_encodedHeight = height_; | 1028 image->_encodedHeight = height_; |
1075 image->_timeStamp = output_timestamp_; | 1029 image->_timeStamp = output_timestamp_; |
1076 image->capture_time_ms_ = output_render_time_ms_; | 1030 image->capture_time_ms_ = output_render_time_ms_; |
1077 image->rotation_ = output_rotation_; | 1031 image->rotation_ = output_rotation_; |
1078 image->_frameType = | 1032 image->_frameType = |
1079 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); | 1033 (key_frame ? webrtc::kVideoFrameKey : webrtc::kVideoFrameDelta); |
1080 image->_completeFrame = true; | 1034 image->_completeFrame = true; |
1081 image->adapt_reason_.quality_resolution_downscales = | |
1082 scale_ ? quality_scaler_.downscale_shift() : -1; | |
1083 | |
1084 webrtc::CodecSpecificInfo info; | 1035 webrtc::CodecSpecificInfo info; |
1085 memset(&info, 0, sizeof(info)); | 1036 memset(&info, 0, sizeof(info)); |
1086 info.codecType = codecType_; | 1037 info.codecType = codecType_; |
1087 if (codecType_ == kVideoCodecVP8) { | 1038 if (codecType_ == kVideoCodecVP8) { |
1088 info.codecSpecific.VP8.pictureId = picture_id_; | 1039 info.codecSpecific.VP8.pictureId = picture_id_; |
1089 info.codecSpecific.VP8.nonReference = false; | 1040 info.codecSpecific.VP8.nonReference = false; |
1090 info.codecSpecific.VP8.simulcastIdx = 0; | 1041 info.codecSpecific.VP8.simulcastIdx = 0; |
1091 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; | 1042 info.codecSpecific.VP8.temporalIdx = webrtc::kNoTemporalIdx; |
1092 info.codecSpecific.VP8.layerSync = false; | 1043 info.codecSpecific.VP8.layerSync = false; |
1093 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; | 1044 info.codecSpecific.VP8.tl0PicIdx = webrtc::kNoTl0PicIdx; |
(...skipping 26 matching lines...) Expand all Loading... |
1120 | 1071 |
1121 // Generate a header describing a single fragment. | 1072 // Generate a header describing a single fragment. |
1122 webrtc::RTPFragmentationHeader header; | 1073 webrtc::RTPFragmentationHeader header; |
1123 memset(&header, 0, sizeof(header)); | 1074 memset(&header, 0, sizeof(header)); |
1124 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { | 1075 if (codecType_ == kVideoCodecVP8 || codecType_ == kVideoCodecVP9) { |
1125 header.VerifyAndAllocateFragmentationHeader(1); | 1076 header.VerifyAndAllocateFragmentationHeader(1); |
1126 header.fragmentationOffset[0] = 0; | 1077 header.fragmentationOffset[0] = 0; |
1127 header.fragmentationLength[0] = image->_length; | 1078 header.fragmentationLength[0] = image->_length; |
1128 header.fragmentationPlType[0] = 0; | 1079 header.fragmentationPlType[0] = 0; |
1129 header.fragmentationTimeDiff[0] = 0; | 1080 header.fragmentationTimeDiff[0] = 0; |
1130 if (codecType_ == kVideoCodecVP8 && scale_) { | 1081 if (codecType_ == kVideoCodecVP8) { |
1131 int qp; | 1082 int qp; |
1132 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { | 1083 if (webrtc::vp8::GetQp(payload, payload_size, &qp)) { |
1133 current_acc_qp_ += qp; | 1084 current_acc_qp_ += qp; |
1134 quality_scaler_.ReportQP(qp); | |
1135 image->qp_ = qp; | 1085 image->qp_ = qp; |
1136 } | 1086 } |
1137 } | 1087 } |
1138 } else if (codecType_ == kVideoCodecH264) { | 1088 } else if (codecType_ == kVideoCodecH264) { |
1139 if (scale_) { | 1089 h264_bitstream_parser_.ParseBitstream(payload, payload_size); |
1140 h264_bitstream_parser_.ParseBitstream(payload, payload_size); | 1090 int qp; |
1141 int qp; | 1091 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { |
1142 if (h264_bitstream_parser_.GetLastSliceQp(&qp)) { | 1092 current_acc_qp_ += qp; |
1143 current_acc_qp_ += qp; | 1093 image->qp_ = qp; |
1144 quality_scaler_.ReportQP(qp); | |
1145 } | |
1146 } | 1094 } |
1147 // For H.264 search for start codes. | 1095 // For H.264 search for start codes. |
1148 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; | 1096 int32_t scPositions[MAX_NALUS_PERFRAME + 1] = {}; |
1149 int32_t scPositionsLength = 0; | 1097 int32_t scPositionsLength = 0; |
1150 int32_t scPosition = 0; | 1098 int32_t scPosition = 0; |
1151 while (scPositionsLength < MAX_NALUS_PERFRAME) { | 1099 while (scPositionsLength < MAX_NALUS_PERFRAME) { |
1152 int32_t naluPosition = NextNaluPosition( | 1100 int32_t naluPosition = NextNaluPosition( |
1153 payload + scPosition, payload_size - scPosition); | 1101 payload + scPosition, payload_size - scPosition); |
1154 if (naluPosition < 0) { | 1102 if (naluPosition < 0) { |
1155 break; | 1103 break; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1236 ". QP: " << (current_acc_qp_ / current_frames_divider) << | 1184 ". QP: " << (current_acc_qp_ / current_frames_divider) << |
1237 " for last " << statistic_time_ms << " ms."; | 1185 " for last " << statistic_time_ms << " ms."; |
1238 stat_start_time_ms_ = rtc::TimeMillis(); | 1186 stat_start_time_ms_ = rtc::TimeMillis(); |
1239 current_frames_ = 0; | 1187 current_frames_ = 0; |
1240 current_bytes_ = 0; | 1188 current_bytes_ = 0; |
1241 current_acc_qp_ = 0; | 1189 current_acc_qp_ = 0; |
1242 current_encoding_time_ms_ = 0; | 1190 current_encoding_time_ms_ = 0; |
1243 } | 1191 } |
1244 } | 1192 } |
1245 | 1193 |
| 1194 webrtc::VideoEncoder::ScalingSettings |
| 1195 MediaCodecVideoEncoder::GetScalingSettings() const { |
| 1196 return VideoEncoder::ScalingSettings(scale_); |
| 1197 } |
| 1198 |
1246 int32_t MediaCodecVideoEncoder::NextNaluPosition( | 1199 int32_t MediaCodecVideoEncoder::NextNaluPosition( |
1247 uint8_t *buffer, size_t buffer_size) { | 1200 uint8_t *buffer, size_t buffer_size) { |
1248 if (buffer_size < H264_SC_LENGTH) { | 1201 if (buffer_size < H264_SC_LENGTH) { |
1249 return -1; | 1202 return -1; |
1250 } | 1203 } |
1251 uint8_t *head = buffer; | 1204 uint8_t *head = buffer; |
1252 // Set end buffer pointer to 4 bytes before actual buffer end so we can | 1205 // Set end buffer pointer to 4 bytes before actual buffer end so we can |
1253 // access head[1], head[2] and head[3] in a loop without buffer overrun. | 1206 // access head[1], head[2] and head[3] in a loop without buffer overrun. |
1254 uint8_t *end = buffer + buffer_size - H264_SC_LENGTH; | 1207 uint8_t *end = buffer + buffer_size - H264_SC_LENGTH; |
1255 | 1208 |
(...skipping 12 matching lines...) Expand all Loading... |
1268 } | 1221 } |
1269 if (head[3] != 0x01) { // got 000000xx | 1222 if (head[3] != 0x01) { // got 000000xx |
1270 head++; // xx != 1, continue searching. | 1223 head++; // xx != 1, continue searching. |
1271 continue; | 1224 continue; |
1272 } | 1225 } |
1273 return (int32_t)(head - buffer); | 1226 return (int32_t)(head - buffer); |
1274 } | 1227 } |
1275 return -1; | 1228 return -1; |
1276 } | 1229 } |
1277 | 1230 |
1278 void MediaCodecVideoEncoder::OnDroppedFrame() { | |
1279 // Methods running on the codec thread should call OnDroppedFrameOnCodecThread | |
1280 // directly. | |
1281 RTC_DCHECK(!codec_thread_checker_.CalledOnValidThread()); | |
1282 codec_thread_->Invoke<void>( | |
1283 RTC_FROM_HERE, | |
1284 Bind(&MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread, this)); | |
1285 } | |
1286 | |
1287 void MediaCodecVideoEncoder::OnDroppedFrameOnCodecThread() { | |
1288 RTC_DCHECK(codec_thread_checker_.CalledOnValidThread()); | |
1289 // Report dropped frame to quality_scaler_. | |
1290 if (scale_) | |
1291 quality_scaler_.ReportDroppedFrame(); | |
1292 } | |
1293 | |
1294 const char* MediaCodecVideoEncoder::ImplementationName() const { | 1231 const char* MediaCodecVideoEncoder::ImplementationName() const { |
1295 return "MediaCodec"; | 1232 return "MediaCodec"; |
1296 } | 1233 } |
1297 | 1234 |
1298 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() | 1235 MediaCodecVideoEncoderFactory::MediaCodecVideoEncoderFactory() |
1299 : egl_context_(nullptr) { | 1236 : egl_context_(nullptr) { |
1300 JNIEnv* jni = AttachCurrentThreadIfNeeded(); | 1237 JNIEnv* jni = AttachCurrentThreadIfNeeded(); |
1301 ScopedLocalRefFrame local_ref_frame(jni); | 1238 ScopedLocalRefFrame local_ref_frame(jni); |
1302 jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder"); | 1239 jclass j_encoder_class = FindClass(jni, "org/webrtc/MediaCodecVideoEncoder"); |
1303 supported_codecs_.clear(); | 1240 supported_codecs_.clear(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1372 return supported_codecs_; | 1309 return supported_codecs_; |
1373 } | 1310 } |
1374 | 1311 |
1375 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( | 1312 void MediaCodecVideoEncoderFactory::DestroyVideoEncoder( |
1376 webrtc::VideoEncoder* encoder) { | 1313 webrtc::VideoEncoder* encoder) { |
1377 ALOGD << "Destroy video encoder."; | 1314 ALOGD << "Destroy video encoder."; |
1378 delete encoder; | 1315 delete encoder; |
1379 } | 1316 } |
1380 | 1317 |
1381 } // namespace webrtc_jni | 1318 } // namespace webrtc_jni |
OLD | NEW |