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