| 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 |
| 35 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. | 54 // Populates struct webrtc::RTPFragmentationHeader for H264 codec. |
| 36 // Each entry specifies the offset and length (excluding start code) of a NALU. | 55 // Each entry specifies the offset and length (excluding start code) of a NALU. |
| 37 // Returns true if successful. | 56 // Returns true if successful. |
| 38 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, | 57 bool GetRTPFragmentationHeaderH264(webrtc::RTPFragmentationHeader* header, |
| 39 const uint8_t* data, uint32_t length) { | 58 const uint8_t* data, uint32_t length) { |
| 40 media::H264Parser parser; | 59 media::H264Parser parser; |
| 41 parser.SetStream(data, length); | 60 parser.SetStream(data, length); |
| 42 | 61 |
| 43 std::vector<media::H264NALU> nalu_vector; | 62 std::vector<media::H264NALU> nalu_vector; |
| 44 while (true) { | 63 while (true) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 int32_t* async_retval, | 130 int32_t* async_retval, |
| 112 webrtc::EncodedImageCallback* callback); | 131 webrtc::EncodedImageCallback* callback); |
| 113 | 132 |
| 114 // Destroy this Impl's encoder. The destructor is not explicitly called, as | 133 // Destroy this Impl's encoder. The destructor is not explicitly called, as |
| 115 // Impl is a base::RefCountedThreadSafe. | 134 // Impl is a base::RefCountedThreadSafe. |
| 116 void Destroy(base::WaitableEvent* async_waiter); | 135 void Destroy(base::WaitableEvent* async_waiter); |
| 117 | 136 |
| 118 // Return the status of Impl. One of WEBRTC_VIDEO_CODEC_XXX value. | 137 // Return the status of Impl. One of WEBRTC_VIDEO_CODEC_XXX value. |
| 119 int32_t GetStatus() const; | 138 int32_t GetStatus() const; |
| 120 | 139 |
| 140 webrtc::VideoCodecType video_codec_type() { return video_codec_type_; } |
| 141 |
| 121 // media::VideoEncodeAccelerator::Client implementation. | 142 // media::VideoEncodeAccelerator::Client implementation. |
| 122 void RequireBitstreamBuffers(unsigned int input_count, | 143 void RequireBitstreamBuffers(unsigned int input_count, |
| 123 const gfx::Size& input_coded_size, | 144 const gfx::Size& input_coded_size, |
| 124 size_t output_buffer_size) override; | 145 size_t output_buffer_size) override; |
| 125 void BitstreamBufferReady(int32_t bitstream_buffer_id, | 146 void BitstreamBufferReady(int32_t bitstream_buffer_id, |
| 126 size_t payload_size, | 147 size_t payload_size, |
| 127 bool key_frame, | 148 bool key_frame, |
| 128 base::TimeDelta timestamp) override; | 149 base::TimeDelta timestamp) override; |
| 129 void NotifyError(media::VideoEncodeAccelerator::Error error) override; | 150 void NotifyError(media::VideoEncodeAccelerator::Error error) override; |
| 130 | 151 |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 if (result.error != webrtc::EncodedImageCallback::Result::OK) { | 720 if (result.error != webrtc::EncodedImageCallback::Result::OK) { |
| 700 DVLOG(2) | 721 DVLOG(2) |
| 701 << "ReturnEncodedImage(): webrtc::EncodedImageCallback::Result.error = " | 722 << "ReturnEncodedImage(): webrtc::EncodedImageCallback::Result.error = " |
| 702 << result.error; | 723 << result.error; |
| 703 } | 724 } |
| 704 | 725 |
| 705 UseOutputBitstreamBufferId(bitstream_buffer_id); | 726 UseOutputBitstreamBufferId(bitstream_buffer_id); |
| 706 } | 727 } |
| 707 | 728 |
| 708 RTCVideoEncoder::RTCVideoEncoder( | 729 RTCVideoEncoder::RTCVideoEncoder( |
| 709 media::VideoCodecProfile profile, | 730 webrtc::VideoCodecType type, |
| 710 media::GpuVideoAcceleratorFactories* gpu_factories) | 731 media::GpuVideoAcceleratorFactories* gpu_factories) |
| 711 : profile_(profile), | 732 : video_codec_type_(type), |
| 712 gpu_factories_(gpu_factories), | 733 gpu_factories_(gpu_factories), |
| 713 gpu_task_runner_(gpu_factories->GetTaskRunner()) { | 734 gpu_task_runner_(gpu_factories->GetTaskRunner()) { |
| 714 DVLOG(1) << "RTCVideoEncoder(): profile=" << GetProfileName(profile); | 735 DVLOG(1) << "RTCVideoEncoder(): codec type=" << type; |
| 715 } | 736 } |
| 716 | 737 |
| 717 RTCVideoEncoder::~RTCVideoEncoder() { | 738 RTCVideoEncoder::~RTCVideoEncoder() { |
| 718 DVLOG(3) << "~RTCVideoEncoder"; | 739 DVLOG(3) << "~RTCVideoEncoder"; |
| 719 Release(); | 740 Release(); |
| 720 DCHECK(!impl_.get()); | 741 DCHECK(!impl_.get()); |
| 721 } | 742 } |
| 722 | 743 |
| 723 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, | 744 int32_t RTCVideoEncoder::InitEncode(const webrtc::VideoCodec* codec_settings, |
| 724 int32_t number_of_cores, | 745 int32_t number_of_cores, |
| 725 size_t max_payload_size) { | 746 size_t max_payload_size) { |
| 726 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType | 747 DVLOG(1) << "InitEncode(): codecType=" << codec_settings->codecType |
| 727 << ", width=" << codec_settings->width | 748 << ", width=" << codec_settings->width |
| 728 << ", height=" << codec_settings->height | 749 << ", height=" << codec_settings->height |
| 729 << ", startBitrate=" << codec_settings->startBitrate; | 750 << ", startBitrate=" << codec_settings->startBitrate; |
| 730 if (impl_) { | 751 if (impl_) { |
| 731 DVLOG(1) << "Release because of reinitialization"; | 752 DVLOG(1) << "Release because of reinitialization"; |
| 732 Release(); | 753 Release(); |
| 733 } | 754 } |
| 734 | 755 |
| 735 webrtc::VideoCodecType video_codec_type; | 756 impl_ = new Impl(gpu_factories_, video_codec_type_); |
| 736 if (profile_ >= media::VP8PROFILE_MIN && profile_ <= media::VP8PROFILE_MAX) { | 757 const media::VideoCodecProfile profile = WebRTCVideoCodecToVideoCodecProfile( |
| 737 video_codec_type = webrtc::kVideoCodecVP8; | 758 impl_->video_codec_type(), codec_settings); |
| 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); | |
| 746 | 759 |
| 747 base::WaitableEvent initialization_waiter( | 760 base::WaitableEvent initialization_waiter( |
| 748 base::WaitableEvent::ResetPolicy::MANUAL, | 761 base::WaitableEvent::ResetPolicy::MANUAL, |
| 749 base::WaitableEvent::InitialState::NOT_SIGNALED); | 762 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 750 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; | 763 int32_t initialization_retval = WEBRTC_VIDEO_CODEC_UNINITIALIZED; |
| 751 gpu_task_runner_->PostTask( | 764 gpu_task_runner_->PostTask( |
| 752 FROM_HERE, | 765 FROM_HERE, |
| 753 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, | 766 base::Bind(&RTCVideoEncoder::Impl::CreateAndInitializeVEA, |
| 754 impl_, | 767 impl_, |
| 755 gfx::Size(codec_settings->width, codec_settings->height), | 768 gfx::Size(codec_settings->width, codec_settings->height), |
| 756 codec_settings->startBitrate, | 769 codec_settings->startBitrate, |
| 757 profile_, | 770 profile, |
| 758 &initialization_waiter, | 771 &initialization_waiter, |
| 759 &initialization_retval)); | 772 &initialization_retval)); |
| 760 | 773 |
| 761 // webrtc::VideoEncoder expects this call to be synchronous. | 774 // webrtc::VideoEncoder expects this call to be synchronous. |
| 762 initialization_waiter.Wait(); | 775 initialization_waiter.Wait(); |
| 763 RecordInitEncodeUMA(initialization_retval, profile_); | 776 RecordInitEncodeUMA(initialization_retval, profile); |
| 764 return initialization_retval; | 777 return initialization_retval; |
| 765 } | 778 } |
| 766 | 779 |
| 767 int32_t RTCVideoEncoder::Encode( | 780 int32_t RTCVideoEncoder::Encode( |
| 768 const webrtc::VideoFrame& input_image, | 781 const webrtc::VideoFrame& input_image, |
| 769 const webrtc::CodecSpecificInfo* codec_specific_info, | 782 const webrtc::CodecSpecificInfo* codec_specific_info, |
| 770 const std::vector<webrtc::FrameType>* frame_types) { | 783 const std::vector<webrtc::FrameType>* frame_types) { |
| 771 DVLOG(3) << "Encode()"; | 784 DVLOG(3) << "Encode()"; |
| 772 if (!impl_.get()) { | 785 if (!impl_.get()) { |
| 773 DVLOG(3) << "Encoder is not initialized"; | 786 DVLOG(3) << "Encoder is not initialized"; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", | 880 UMA_HISTOGRAM_BOOLEAN("Media.RTCVideoEncoderInitEncodeSuccess", |
| 868 init_retval == WEBRTC_VIDEO_CODEC_OK); | 881 init_retval == WEBRTC_VIDEO_CODEC_OK); |
| 869 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { | 882 if (init_retval == WEBRTC_VIDEO_CODEC_OK) { |
| 870 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", | 883 UMA_HISTOGRAM_ENUMERATION("Media.RTCVideoEncoderProfile", |
| 871 profile, | 884 profile, |
| 872 media::VIDEO_CODEC_PROFILE_MAX + 1); | 885 media::VIDEO_CODEC_PROFILE_MAX + 1); |
| 873 } | 886 } |
| 874 } | 887 } |
| 875 | 888 |
| 876 } // namespace content | 889 } // namespace content |
| OLD | NEW |