| 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_factory.h" | 5 #include "content/renderer/media/gpu/rtc_video_encoder_factory.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "content/public/common/content_features.h" | 8 #include "content/public/common/content_features.h" |
| 9 #include "content/public/common/content_switches.h" | 9 #include "content/public/common/content_switches.h" |
| 10 #include "content/public/common/feature_h264_with_openh264_ffmpeg.h" | 10 #include "content/public/common/feature_h264_with_openh264_ffmpeg.h" |
| 11 #include "content/renderer/media/gpu/rtc_video_encoder.h" | 11 #include "content/renderer/media/gpu/rtc_video_encoder.h" |
| 12 #include "media/gpu/ipc/client/gpu_video_encode_accelerator_host.h" | 12 #include "media/gpu/ipc/client/gpu_video_encode_accelerator_host.h" |
| 13 #include "media/renderers/gpu_video_accelerator_factories.h" | 13 #include "media/renderers/gpu_video_accelerator_factories.h" |
| 14 #include "third_party/webrtc/common_video/h264/profile_level_id.h" | 14 #include "media/video/video_encode_accelerator.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Translate from media::VideoEncodeAccelerator::SupportedProfile to | 20 // Translate from media::VideoEncodeAccelerator::SupportedProfile to |
| 21 // cricket::WebRtcVideoEncoderFactory::VideoCodec, or return nothing if the | 21 // one or more instances of cricket::WebRtcVideoEncoderFactory::VideoCodec |
| 22 // profile isn't supported. | 22 void VEAToWebRTCCodecs( |
| 23 base::Optional<cricket::VideoCodec> VEAToWebRTCCodec( | 23 std::vector<cricket::VideoCodec>* codecs, |
| 24 const media::VideoEncodeAccelerator::SupportedProfile& profile) { | 24 const media::VideoEncodeAccelerator::SupportedProfile& profile) { |
| 25 DCHECK_EQ(profile.max_framerate_denominator, 1U); | 25 DCHECK_EQ(profile.max_framerate_denominator, 1U); |
| 26 | 26 |
| 27 if (profile.profile >= media::VP8PROFILE_MIN && | 27 if (profile.profile >= media::VP8PROFILE_MIN && |
| 28 profile.profile <= media::VP8PROFILE_MAX) { | 28 profile.profile <= media::VP8PROFILE_MAX) { |
| 29 if (base::FeatureList::IsEnabled(features::kWebRtcHWVP8Encoding)) { | 29 if (base::FeatureList::IsEnabled(features::kWebRtcHWVP8Encoding)) { |
| 30 return base::Optional<cricket::VideoCodec>(cricket::VideoCodec("VP8")); | 30 codecs->push_back(cricket::VideoCodec("VP8")); |
| 31 } | 31 } |
| 32 } else if (profile.profile >= media::H264PROFILE_MIN && | 32 } else if (profile.profile >= media::H264PROFILE_MIN && |
| 33 profile.profile <= media::H264PROFILE_MAX) { | 33 profile.profile <= media::H264PROFILE_MAX) { |
| 34 // Enable H264 HW encode for WebRTC when SW fallback is available, which is | 34 // Enable H264 HW encode for WebRTC when SW fallback is available, which is |
| 35 // checked by kWebRtcH264WithOpenH264FFmpeg flag. This check should be | 35 // checked by kWebRtcH264WithOpenH264FFmpeg flag. This check should be |
| 36 // removed when SW implementation is fully enabled. | 36 // removed when SW implementation is fully enabled. |
| 37 bool webrtc_h264_sw_enabled = false; | 37 bool webrtc_h264_sw_enabled = false; |
| 38 #if BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) | 38 #if BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) |
| 39 webrtc_h264_sw_enabled = | 39 webrtc_h264_sw_enabled = |
| 40 base::FeatureList::IsEnabled(kWebRtcH264WithOpenH264FFmpeg); | 40 base::FeatureList::IsEnabled(kWebRtcH264WithOpenH264FFmpeg); |
| 41 #endif // BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) | 41 #endif // BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) |
| 42 if (webrtc_h264_sw_enabled || | 42 if (webrtc_h264_sw_enabled || |
| 43 base::FeatureList::IsEnabled(features::kWebRtcHWH264Encoding)) { | 43 base::FeatureList::IsEnabled(features::kWebRtcHWH264Encoding)) { |
| 44 webrtc::H264::Profile h264_profile; | 44 // TODO(magjed): Propagate H264 profile information. |
| 45 switch (profile.profile) { | 45 codecs->push_back(cricket::VideoCodec("H264")); |
| 46 case media::H264PROFILE_BASELINE: | |
| 47 h264_profile = webrtc::H264::kProfileBaseline; | |
| 48 break; | |
| 49 case media::H264PROFILE_MAIN: | |
| 50 h264_profile = webrtc::H264::kProfileMain; | |
| 51 break; | |
| 52 case media::H264PROFILE_HIGH: | |
| 53 h264_profile = webrtc::H264::kProfileHigh; | |
| 54 break; | |
| 55 default: | |
| 56 // Unsupported H264 profile in WebRTC. | |
| 57 return base::Optional<cricket::VideoCodec>(); | |
| 58 } | |
| 59 | |
| 60 const int width = profile.max_resolution.width(); | |
| 61 const int height = profile.max_resolution.height(); | |
| 62 const int fps = profile.max_framerate_numerator; | |
| 63 DCHECK_EQ(1u, profile.max_framerate_denominator); | |
| 64 | |
| 65 const rtc::Optional<webrtc::H264::Level> h264_level = | |
| 66 webrtc::H264::SupportedLevel(width * height, fps); | |
| 67 const webrtc::H264::ProfileLevelId profile_level_id( | |
| 68 h264_profile, h264_level.value_or(webrtc::H264::kLevel1)); | |
| 69 | |
| 70 cricket::VideoCodec codec("H264"); | |
| 71 codec.SetParam(cricket::kH264FmtpProfileLevelId, | |
| 72 *webrtc::H264::ProfileLevelIdToString(profile_level_id)); | |
| 73 codec.SetParam(cricket::kH264FmtpLevelAsymmetryAllowed, "1"); | |
| 74 codec.SetParam(cricket::kH264FmtpPacketizationMode, "1"); | |
| 75 return base::Optional<cricket::VideoCodec>(codec); | |
| 76 } | 46 } |
| 77 } | 47 } |
| 78 return base::Optional<cricket::VideoCodec>(); | |
| 79 } | 48 } |
| 80 | 49 |
| 81 } // anonymous namespace | 50 } // anonymous namespace |
| 82 | 51 |
| 83 RTCVideoEncoderFactory::RTCVideoEncoderFactory( | 52 RTCVideoEncoderFactory::RTCVideoEncoderFactory( |
| 84 media::GpuVideoAcceleratorFactories* gpu_factories) | 53 media::GpuVideoAcceleratorFactories* gpu_factories) |
| 85 : gpu_factories_(gpu_factories) { | 54 : gpu_factories_(gpu_factories) { |
| 86 const media::VideoEncodeAccelerator::SupportedProfiles& profiles = | 55 const media::VideoEncodeAccelerator::SupportedProfiles& profiles = |
| 87 gpu_factories_->GetVideoEncodeAcceleratorSupportedProfiles(); | 56 gpu_factories_->GetVideoEncodeAcceleratorSupportedProfiles(); |
| 88 for (const auto& profile : profiles) { | 57 for (const auto& profile : profiles) |
| 89 base::Optional<cricket::VideoCodec> codec = VEAToWebRTCCodec(profile); | 58 VEAToWebRTCCodecs(&supported_codecs_, profile); |
| 90 if (codec) { | |
| 91 supported_codecs_.push_back(std::move(*codec)); | |
| 92 profiles_.push_back(profile.profile); | |
| 93 } | |
| 94 } | |
| 95 // There should be a 1:1 mapping between media::VideoCodecProfile and | |
| 96 // cricket::VideoCodec. | |
| 97 CHECK_EQ(profiles_.size(), supported_codecs_.size()); | |
| 98 } | 59 } |
| 99 | 60 |
| 100 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {} | 61 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {} |
| 101 | 62 |
| 102 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( | 63 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( |
| 103 const cricket::VideoCodec& codec) { | 64 const cricket::VideoCodec& codec) { |
| 104 for (size_t i = 0; i < supported_codecs_.size(); ++i) { | 65 for (const cricket::VideoCodec& supported_codec : supported_codecs_) { |
| 105 if (!cricket::CodecNamesEq(codec.name, supported_codecs_[i].name)) | 66 if (cricket::CodecNamesEq(codec.name, supported_codec.name)) { |
| 106 continue; | 67 webrtc::VideoCodecType type = webrtc::PayloadNameToCodecType(codec.name) |
| 107 // Check H264 profile. | 68 .value_or(webrtc::kVideoCodecUnknown); |
| 108 using webrtc::H264::ParseSdpProfileLevelId; | 69 return new RTCVideoEncoder(type, gpu_factories_); |
| 109 if (cricket::CodecNamesEq(codec.name.c_str(), cricket::kH264CodecName) && | |
| 110 ParseSdpProfileLevelId(codec.params)->profile != | |
| 111 ParseSdpProfileLevelId(supported_codecs_[i].params)->profile) { | |
| 112 continue; | |
| 113 } | 70 } |
| 114 // There should be a 1:1 mapping between media::VideoCodecProfile and | |
| 115 // cricket::VideoCodec. | |
| 116 CHECK_EQ(profiles_.size(), supported_codecs_.size()); | |
| 117 return new RTCVideoEncoder(profiles_[i], gpu_factories_); | |
| 118 } | 71 } |
| 119 return nullptr; | 72 return nullptr; |
| 120 } | 73 } |
| 121 | 74 |
| 122 const std::vector<cricket::VideoCodec>& | 75 const std::vector<cricket::VideoCodec>& |
| 123 RTCVideoEncoderFactory::supported_codecs() const { | 76 RTCVideoEncoderFactory::supported_codecs() const { |
| 124 return supported_codecs_; | 77 return supported_codecs_; |
| 125 } | 78 } |
| 126 | 79 |
| 127 void RTCVideoEncoderFactory::DestroyVideoEncoder( | 80 void RTCVideoEncoderFactory::DestroyVideoEncoder( |
| 128 webrtc::VideoEncoder* encoder) { | 81 webrtc::VideoEncoder* encoder) { |
| 129 delete encoder; | 82 delete encoder; |
| 130 } | 83 } |
| 131 | 84 |
| 132 } // namespace content | 85 } // namespace content |
| OLD | NEW |