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 "media/video/video_encode_accelerator.h" | 13 #include "third_party/webrtc/common_video/h264/profile_level_id.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 // one or more instances of cricket::WebRtcVideoEncoderFactory::VideoCodec | 29 // cricket::WebRtcVideoEncoderFactory::VideoCodec, or return nothing if the |
30 void VEAToWebRTCCodecs( | 30 // profile isn't supported. |
31 std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>* codecs, | 31 base::Optional<cricket::VideoCodec> VEAToWebRTCCodec( |
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 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | 43 return base::Optional<cricket::VideoCodec>(cricket::VideoCodec("VP8")); |
44 webrtc::kVideoCodecVP8, "VP8", width, height, fps)); | |
45 } | 44 } |
46 } else if (profile.profile >= media::H264PROFILE_MIN && | 45 } else if (profile.profile >= media::H264PROFILE_MIN && |
47 profile.profile <= media::H264PROFILE_MAX) { | 46 profile.profile <= media::H264PROFILE_MAX) { |
48 // Enable H264 HW encode for WebRTC when SW fallback is available, which is | 47 // Enable H264 HW encode for WebRTC when SW fallback is available, which is |
49 // checked by kWebRtcH264WithOpenH264FFmpeg flag. This check should be | 48 // checked by kWebRtcH264WithOpenH264FFmpeg flag. This check should be |
50 // removed when SW implementation is fully enabled. | 49 // removed when SW implementation is fully enabled. |
51 bool webrtc_h264_sw_enabled = false; | 50 bool webrtc_h264_sw_enabled = false; |
52 #if BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) | 51 #if BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) |
53 webrtc_h264_sw_enabled = | 52 webrtc_h264_sw_enabled = |
54 base::FeatureList::IsEnabled(kWebRtcH264WithOpenH264FFmpeg); | 53 base::FeatureList::IsEnabled(kWebRtcH264WithOpenH264FFmpeg); |
55 #endif // BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) | 54 #endif // BUILDFLAG(RTC_USE_H264) && !defined(MEDIA_DISABLE_FFMPEG) |
56 if (webrtc_h264_sw_enabled || | 55 if (webrtc_h264_sw_enabled || |
57 !IsCodecDisabledByCommandLine(cmd_line, | 56 !IsCodecDisabledByCommandLine(cmd_line, |
58 switches::kDisableWebRtcHWEncodingH264)) { | 57 switches::kDisableWebRtcHWEncodingH264)) { |
59 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | 58 webrtc::H264::Profile h264_profile; |
60 webrtc::kVideoCodecH264, "H264", width, height, fps)); | 59 switch (profile.profile) { |
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); | |
61 } | 85 } |
62 } | 86 } |
87 return base::Optional<cricket::VideoCodec>(); | |
63 } | 88 } |
64 | 89 |
65 } // anonymous namespace | 90 } // anonymous namespace |
66 | 91 |
67 RTCVideoEncoderFactory::RTCVideoEncoderFactory( | 92 RTCVideoEncoderFactory::RTCVideoEncoderFactory( |
68 media::GpuVideoAcceleratorFactories* gpu_factories) | 93 media::GpuVideoAcceleratorFactories* gpu_factories) |
69 : gpu_factories_(gpu_factories) { | 94 : gpu_factories_(gpu_factories) { |
70 const media::VideoEncodeAccelerator::SupportedProfiles& profiles = | 95 const media::VideoEncodeAccelerator::SupportedProfiles& profiles = |
71 gpu_factories_->GetVideoEncodeAcceleratorSupportedProfiles(); | 96 gpu_factories_->GetVideoEncodeAcceleratorSupportedProfiles(); |
72 for (const auto& profile : profiles) | 97 for (const auto& profile : profiles) { |
73 VEAToWebRTCCodecs(&codecs_, profile); | 98 base::Optional<cricket::VideoCodec> codec = VEAToWebRTCCodec(profile); |
emircan
2016/11/17 19:02:15
const
magjed_chromium
2016/11/18 09:46:47
I need to have it non-const in order to move it a
| |
99 if (codec) { | |
100 supported_codecs_.push_back(std::move(*codec)); | |
101 profiles_.push_back(profile.profile); | |
102 } | |
103 } | |
74 } | 104 } |
75 | 105 |
76 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {} | 106 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {} |
77 | 107 |
78 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( | 108 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( |
79 webrtc::VideoCodecType type) { | 109 const cricket::VideoCodec& codec) { |
80 for (const auto& codec : codecs_) { | 110 for (size_t i = 0; i < supported_codecs_.size(); ++i) { |
81 if (codec.type == type) | 111 if (!cricket::CodecNamesEq(codec.name, supported_codecs_[i].name)) |
82 return new RTCVideoEncoder(type, gpu_factories_); | 112 continue; |
113 // Check H264 profile. | |
114 using webrtc::H264::ParseSdpProfileLevelId; | |
115 if (cricket::CodecNamesEq(codec.name.c_str(), cricket::kH264CodecName) && | |
116 ParseSdpProfileLevelId(codec.params)->profile != | |
117 ParseSdpProfileLevelId(supported_codecs_[i].params)->profile) { | |
118 continue; | |
119 } | |
120 return new RTCVideoEncoder(profiles_[i], gpu_factories_); | |
83 } | 121 } |
84 return nullptr; | 122 return nullptr; |
85 } | 123 } |
86 | 124 |
87 const std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>& | 125 const std::vector<cricket::VideoCodec>& |
88 RTCVideoEncoderFactory::codecs() const { | 126 RTCVideoEncoderFactory::supported_codecs() const { |
89 return codecs_; | 127 return supported_codecs_; |
90 } | 128 } |
91 | 129 |
92 void RTCVideoEncoderFactory::DestroyVideoEncoder( | 130 void RTCVideoEncoderFactory::DestroyVideoEncoder( |
93 webrtc::VideoEncoder* encoder) { | 131 webrtc::VideoEncoder* encoder) { |
94 delete encoder; | 132 delete encoder; |
95 } | 133 } |
96 | 134 |
97 } // namespace content | 135 } // namespace content |
OLD | NEW |