| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/gpu/rtc_video_encoder.h" | 5 #include "content/renderer/media/gpu/rtc_video_encoder.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "media/filters/h264_parser.h" | 25 #include "media/filters/h264_parser.h" |
| 26 #include "media/renderers/gpu_video_accelerator_factories.h" | 26 #include "media/renderers/gpu_video_accelerator_factories.h" |
| 27 #include "media/video/video_encode_accelerator.h" | 27 #include "media/video/video_encode_accelerator.h" |
| 28 #include "third_party/libyuv/include/libyuv.h" | 28 #include "third_party/libyuv/include/libyuv.h" |
| 29 #include "third_party/webrtc/base/timeutils.h" | 29 #include "third_party/webrtc/base/timeutils.h" |
| 30 | 30 |
| 31 namespace content { | 31 namespace content { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 // Translate from webrtc::VideoCodecType and webrtc::VideoCodec to | |
| 36 // media::VideoCodecProfile. | |
| 37 media::VideoCodecProfile WebRTCVideoCodecToVideoCodecProfile( | |
| 38 webrtc::VideoCodecType type, | |
| 39 const webrtc::VideoCodec* codec_settings) { | |
| 40 DCHECK_EQ(type, codec_settings->codecType); | |
| 41 switch (type) { | |
| 42 case webrtc::kVideoCodecVP8: | |
| 43 return media::VP8PROFILE_ANY; | |
| 44 case webrtc::kVideoCodecH264: | |
| 45 // TODO(magjed): WebRTC is only using Baseline profile for now. Update | |
| 46 // once http://crbug/webrtc/6337 is fixed. | |
| 47 return media::H264PROFILE_BASELINE; | |
| 48 default: | |
| 49 NOTREACHED() << "Unrecognized video codec type"; | |
| 50 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. | 35 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. |
| 55 // Each entry specifies the offset and length (excluding start code) of a NALU. | 36 // Each entry specifies the offset and length (excluding start code) of a NALU. |
| 56 // Returns true if successful. | 37 // Returns true if successful. |
| 57 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, | 38 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, |
| 58 const uint8_t* data, uint32_t length) { | 39 const uint8_t* data, uint32_t length) { |
| 59 media::H264Parser parser; | 40 media::H264Parser parser; |
| 60 parser.SetStream(data, length); | 41 parser.SetStream(data, length); |
| 61 | 42 |
| 62 std::vector<media::H264NALU> nalu_vector; | 43 std::vector<media::H264NALU> nalu_vector; |
| 63 while (true) { | 44 while (true) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 int32_t* async_retval, | 111 int32_t* async_retval, |
| 131 webrtc::EncodedImageCallback* callback); | 112 webrtc::EncodedImageCallback* callback); |
| 132 | 113 |
| 133 // Destroy this Impl's encoder. The destructor is not explicitly called, as | 114 // Destroy this Impl's encoder. The destructor is not explicitly called, as |
| 134 // Impl is a base::RefCountedThreadSafe. | 115 // Impl is a base::RefCountedThreadSafe. |
| 135 void Destroy(base::WaitableEvent* async_waiter); | 116 void Destroy(base::WaitableEvent* async_waiter); |
| 136 | 117 |
| 137 // Return the status of Impl. One of WEBRTC_VIDEO_CODEC_XXX value. | 118 // Return the status of Impl. One of WEBRTC_VIDEO_CODEC_XXX value. |
| 138 int32_t GetStatus() const; | 119 int32_t GetStatus() const; |
| 139 | 120 |
| 140 webrtc::VideoCodecType video_codec_type() { return video_codec_type_; } | |
| 141 | |
| 142 // media::VideoEncodeAccelerator::Client implementation. | 121 // media::VideoEncodeAccelerator::Client implementation. |
| 143 void RequireBitstreamBuffers(unsigned int input_count, | 122 void RequireBitstreamBuffers(unsigned int input_count, |
| 144 const gfx::Size& input_coded_size, | 123 const gfx::Size& input_coded_size, |
| 145 size_t output_buffer_size) override; | 124 size_t output_buffer_size) override; |
| 146 void BitstreamBufferReady(int32_t bitstream_buffer_id, | 125 void BitstreamBufferReady(int32_t bitstream_buffer_id, |
| 147 size_t payload_size, | 126 size_t payload_size, |
| 148 bool key_frame, | 127 bool key_frame, |
| 149 base::TimeDelta timestamp) override; | 128 base::TimeDelta timestamp) override; |
| 150 void NotifyError(media::VideoEncodeAccelerator::Error error) override; | 129 void NotifyError(media::VideoEncodeAccelerator::Error error) override; |
| 151 | 130 |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 if (result.error != webrtc::EncodedImageCallback::Result::OK) { | 699 if (result.error != webrtc::EncodedImageCallback::Result::OK) { |
| 721 DVLOG(2) | 700 DVLOG(2) |
| 722 << "ReturnEncodedImage(): webrtc::EncodedImageCallback::Result.error = " | 701 << "ReturnEncodedImage(): webrtc::EncodedImageCallback::Result.error = " |
| 723 << result.error; | 702 << result.error; |
| 724 } | 703 } |
| 725 | 704 |
| 726 UseOutputBitstreamBufferId(bitstream_buffer_id); | 705 UseOutputBitstreamBufferId(bitstream_buffer_id); |
| 727 } | 706 } |
| 728 | 707 |
| 729 RTCVideoEncoder::RTCVideoEncoder( | 708 RTCVideoEncoder::RTCVideoEncoder( |
| 730 webrtc::VideoCodecType type, | 709 media::VideoCodecProfile profile, |
| 731 media::GpuVideoAcceleratorFactories* gpu_factories) | 710 media::GpuVideoAcceleratorFactories* gpu_factories) |
| 732 : video_codec_type_(type), | 711 : profile_(profile), |
| 733 gpu_factories_(gpu_factories), | 712 gpu_factories_(gpu_factories), |
| 734 gpu_task_runner_(gpu_factories->GetTaskRunner()) { | 713 gpu_task_runner_(gpu_factories->GetTaskRunner()) { |
| 735 DVLOG(1) << "RTCVideoEncoder(): codec type=" << type; | 714 DVLOG(1) << "RTCVideoEncoder(): profile=" << GetProfileName(profile); |
| 736 } | 715 } |
| 737 | 716 |
| 738 RTCVideoEncoder::~RTCVideoEncoder() { | 717 RTCVideoEncoder::~RTCVideoEncoder() { |
| 739 DVLOG(3) << "~RTCVideoEncoder"; | 718 DVLOG(3) << "~RTCVideoEncoder"; |
| 740 Release(); | 719 Release(); |
| 741 DCHECK(!impl_.get()); | 720 DCHECK(!impl_.get()); |
| 742 } | 721 } |
| 743 | 722 |
| 744 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, | 723 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, |
| 745 int32_t number_of_cores, | 724 int32_t number_of_cores, |
| 746 size_t max_payload_size) { | 725 size_t max_payload_size) { |
| 747 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType | 726 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType |
| 748 << ", width=" << codec_settings->width | 727 << ", width=" << codec_settings->width |
| 749 << ", height=" << codec_settings->height | 728 << ", height=" << codec_settings->height |
| 750 << ", startBitrate=" << codec_settings->startBitrate; | 729 << ", startBitrate=" << codec_settings->startBitrate; |
| 751 if (impl_) { | 730 if (impl_) { |
| 752 DVLOG(1) << "Release because of reinitialization"; | 731 DVLOG(1) << "Release because of reinitialization"; |
| 753 Release(); | 732 Release(); |
| 754 } | 733 } |
| 755 | 734 |
| 756 impl_ = new Impl(gpu_factories_, video_codec_type_); | 735 webrtc::VideoCodecType video_codec_type; |
| 757 const media::VideoCodecProfile profile = WebRTCVideoCodecToVideoCodecProfile( | 736 if (profile_ >= media::VP8PROFILE_MIN && profile_ <= media::VP8PROFILE_MAX) { |
| 758 impl_->video_codec_type(), codec_settings); | 737 video_codec_type = webrtc::kVideoCodecVP8; |
| 738 } else if (profile_ >= media::H264PROFILE_MIN && |
| 739 profile_ <= media::H264PROFILE_MAX) { |
| 740 video_codec_type = webrtc::kVideoCodecH264; |
| 741 } else { |
| 742 NOTREACHED() << "Invalid video codec type"; |
| 743 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER; |
| 744 } |
| 745 impl_ = new Impl(gpu_factories_, video_codec_type); |
| 759 | 746 |
| 760 base::WaitableEvent initialization_waiter( | 747 base::WaitableEvent initialization_waiter( |
| 761 base::WaitableEvent::ResetPolicy::MANUAL, | 748 base::WaitableEvent::ResetPolicy::MANUAL, |
| 762 base::WaitableEvent::InitialState::NOT_SIGNALED); | 749 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 763 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 750 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 764 gpu_task_runner_->PostTask( | 751 gpu_task_runner_->PostTask( |
| 765 FROM_HERE, | 752 FROM_HERE, |
| 766 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, | 753 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, |
| 767 impl_, | 754 impl_, |
| 768 gfx::Size(codec_settings->width, codec_settings->height), | 755 gfx::Size(codec_settings->width, codec_settings->height), |
| 769 codec_settings->startBitrate, | 756 codec_settings->startBitrate, |
| 770 profile, | 757 profile_, |
| 771 &initialization_waiter, | 758 &initialization_waiter, |
| 772 &initialization_retval)); | 759 &initialization_retval)); |
| 773 | 760 |
| 774 // webrtc::VideoEncoder expects this call to be synchronous. | 761 // webrtc::VideoEncoder expects this call to be synchronous. |
| 775 initialization_waiter.Wait(); | 762 initialization_waiter.Wait(); |
| 776 RecordInitEncodeUMA(initialization_retval, profile); | 763 RecordInitEncodeUMA(initialization_retval, profile_); |
| 777 return initialization_retval; | 764 return initialization_retval; |
| 778 } | 765 } |
| 779 | 766 |
| 780 int32_t RTCVideoEncoder::Encode( | 767 int32_t RTCVideoEncoder::Encode( |
| 781 const webrtc::VideoFrame& input_image, | 768 const webrtc::VideoFrame& input_image, |
| 782 const webrtc::CodecSpecificInfo* codec_specific_info, | 769 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 783 const std::vector<webrtc::FrameType>* frame_types) { | 770 const std::vector<webrtc::FrameType>* frame_types) { |
| 784 DVLOG(3) << "Encode()"; | 771 DVLOG(3) << "Encode()"; |
| 785 if (!impl_.get()) { | 772 if (!impl_.get()) { |
| 786 DVLOG(3) << "Encoder is not initialized"; | 773 DVLOG(3) << "Encoder is not initialized"; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 867 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
| 881 init_retval == WEBRTC_VIDEO_CODEC_OK); | 868 init_retval == WEBRTC_VIDEO_CODEC_OK); |
| 882 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 869 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
| 883 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 870 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
| 884 profile, | 871 profile, |
| 885 media::VIDEO_CODEC_PROFILE_MAX + 1); | 872 media::VIDEO_CODEC_PROFILE_MAX + 1); |
| 886 } | 873 } |
| 887 } | 874 } |
| 888 | 875 |
| 889 } // namespace content | 876 } // namespace content |
| OLD | NEW |