Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / media / rtc_video_encoder_factory.cc
blobec92f5572f9bd7c1877985cd1e1f3f073186e7f0
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
3 // found in the LICENSE file.
5 #include "content/renderer/media/rtc_video_encoder_factory.h"
7 #include "base/command_line.h"
8 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/renderer/media/rtc_video_encoder.h"
11 #include "media/filters/gpu_video_accelerator_factories.h"
12 #include "media/video/video_encode_accelerator.h"
14 namespace content {
16 namespace {
18 // Translate from media::VideoEncodeAccelerator::SupportedProfile to
19 // one or more instances of cricket::WebRtcVideoEncoderFactory::VideoCodec
20 void VEAToWebRTCCodecs(
21 std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>* codecs,
22 const media::VideoEncodeAccelerator::SupportedProfile& profile) {
23 int width = profile.max_resolution.width();
24 int height = profile.max_resolution.height();
25 int fps = profile.max_framerate.numerator;
26 DCHECK_EQ(profile.max_framerate.denominator, 1U);
28 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
29 if (profile.profile >= media::VP8PROFILE_MIN &&
30 profile.profile <= media::VP8PROFILE_MAX) {
31 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWVp8Encoding)) {
32 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec(
33 webrtc::kVideoCodecVP8, "VP8", width, height, fps));
35 } else if (profile.profile >= media::H264PROFILE_MIN &&
36 profile.profile <= media::H264PROFILE_MAX) {
37 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWH264Encoding)) {
38 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec(
39 webrtc::kVideoCodecH264, "H264", width, height, fps));
41 // TODO(hshi): remove the generic codec type after CASTv1 deprecation.
42 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec(
43 webrtc::kVideoCodecGeneric, "CAST1", width, height, fps));
47 // Translate from cricket::WebRtcVideoEncoderFactory::VideoCodec to
48 // media::VideoCodecProfile. Pick a default profile for each codec type.
49 media::VideoCodecProfile WebRTCCodecToVideoCodecProfile(
50 webrtc::VideoCodecType type) {
51 switch (type) {
52 case webrtc::kVideoCodecVP8:
53 return media::VP8PROFILE_ANY;
54 case webrtc::kVideoCodecH264:
55 case webrtc::kVideoCodecGeneric:
56 return media::H264PROFILE_MAIN;
57 default:
58 return media::VIDEO_CODEC_PROFILE_UNKNOWN;
62 } // anonymous namespace
64 RTCVideoEncoderFactory::RTCVideoEncoderFactory(
65 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories)
66 : gpu_factories_(gpu_factories) {
67 // Query media::VideoEncodeAccelerator (statically) for our supported codecs.
68 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles =
69 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles();
70 for (size_t i = 0; i < profiles.size(); ++i)
71 VEAToWebRTCCodecs(&codecs_, profiles[i]);
74 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {}
76 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder(
77 webrtc::VideoCodecType type) {
78 bool found = false;
79 for (size_t i = 0; i < codecs_.size(); ++i) {
80 if (codecs_[i].type == type) {
81 found = true;
82 break;
85 if (!found)
86 return NULL;
87 return new RTCVideoEncoder(
88 type, WebRTCCodecToVideoCodecProfile(type), gpu_factories_);
91 void RTCVideoEncoderFactory::AddObserver(Observer* observer) {
92 // No-op: our codec list is populated on installation.
95 void RTCVideoEncoderFactory::RemoveObserver(Observer* observer) {}
97 const std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>&
98 RTCVideoEncoderFactory::codecs() const {
99 return codecs_;
102 void RTCVideoEncoderFactory::DestroyVideoEncoder(
103 webrtc::VideoEncoder* encoder) {
104 delete encoder;
107 } // namespace content