Chromium Code Reviews| 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/rtc_video_encoder_factory.h" | 5 #include "content/renderer/media/rtc_video_encoder_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" | |
| 10 #include "base/message_loop/message_loop_proxy.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 8 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" | 12 #include "content/common/gpu/client/gpu_video_encode_accelerator_host.h" |
| 9 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/public/renderer/video_encode_accelerator.h" | |
| 10 #include "content/renderer/media/rtc_video_encoder.h" | 15 #include "content/renderer/media/rtc_video_encoder.h" |
| 11 #include "media/filters/gpu_video_accelerator_factories.h" | 16 #include "media/filters/gpu_video_accelerator_factories.h" |
| 12 #include "media/video/video_encode_accelerator.h" | |
| 13 | 17 |
| 14 namespace content { | 18 namespace content { |
| 15 | 19 |
| 16 namespace { | |
| 17 | |
| 18 // Translate from media::VideoEncodeAccelerator::SupportedProfile to | 20 // Translate from media::VideoEncodeAccelerator::SupportedProfile to |
| 19 // one or more instances of cricket::WebRtcVideoEncoderFactory::VideoCodec | 21 // one or more instances of cricket::WebRtcVideoEncoderFactory::VideoCodec |
| 20 void VEAToWebRTCCodecs( | 22 void RTCVideoEncoderFactory::VEAToWebRTCCodecs(const std::vector< |
| 21 std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>* codecs, | 23 media::VideoEncodeAccelerator::SupportedProfile>& profiles) { |
| 22 const media::VideoEncodeAccelerator::SupportedProfile& profile) { | 24 for (size_t i = 0; i < profiles.size(); i++) { |
| 23 int width = profile.max_resolution.width(); | 25 const media::VideoEncodeAccelerator::SupportedProfile& profile = |
| 24 int height = profile.max_resolution.height(); | 26 profiles[i]; |
| 25 int fps = profile.max_framerate.numerator; | 27 int width = profile.max_resolution.width(); |
| 26 DCHECK_EQ(profile.max_framerate.denominator, 1U); | 28 int height = profile.max_resolution.height(); |
| 29 int fps = profile.max_framerate_numerator; | |
| 30 DCHECK_EQ(profile.max_framerate_denominator, 1U); | |
| 27 | 31 |
| 28 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 32 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 29 if (profile.profile >= media::VP8PROFILE_MIN && | 33 if (profile.profile >= media::VP8PROFILE_MIN && |
| 30 profile.profile <= media::VP8PROFILE_MAX) { | 34 profile.profile <= media::VP8PROFILE_MAX) { |
| 31 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWVp8Encoding)) { | 35 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWVp8Encoding)) { |
| 32 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | 36 codecs_.push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( |
| 33 webrtc::kVideoCodecVP8, "VP8", width, height, fps)); | 37 webrtc::kVideoCodecVP8, "VP8", width, height, fps)); |
| 38 } | |
| 39 } else if (profile.profile >= media::H264PROFILE_MIN && | |
| 40 profile.profile <= media::H264PROFILE_MAX) { | |
| 41 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWH264Encoding)) { | |
| 42 codecs_.push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | |
| 43 webrtc::kVideoCodecH264, "H264", width, height, fps)); | |
| 44 } | |
| 45 // TODO(hshi): remove the generic codec type after CASTv1 deprecation. | |
| 46 codecs_.push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | |
| 47 webrtc::kVideoCodecGeneric, "CAST1", width, height, fps)); | |
| 34 } | 48 } |
| 35 } else if (profile.profile >= media::H264PROFILE_MIN && | 49 } |
| 36 profile.profile <= media::H264PROFILE_MAX) { | 50 for (std::list<Observer*>::iterator it = observers_.begin(); |
| 37 if (cmd_line->HasSwitch(switches::kEnableWebRtcHWH264Encoding)) { | 51 it != observers_.end(); |
| 38 codecs->push_back(cricket::WebRtcVideoEncoderFactory::VideoCodec( | 52 it++) { |
|
kcwu
2014/09/19 11:15:54
++it
| |
| 39 webrtc::kVideoCodecH264, "H264", width, height, fps)); | 53 (*it)->OnCodecsAvailable(); |
| 40 } | |
| 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)); | |
| 44 } | 54 } |
| 45 } | 55 } |
| 46 | 56 |
| 47 // Translate from cricket::WebRtcVideoEncoderFactory::VideoCodec to | 57 // Translate from cricket::WebRtcVideoEncoderFactory::VideoCodec to |
| 48 // media::VideoCodecProfile. Pick a default profile for each codec type. | 58 // media::VideoCodecProfile. Pick a default profile for each codec type. |
| 49 media::VideoCodecProfile WebRTCCodecToVideoCodecProfile( | 59 media::VideoCodecProfile WebRTCCodecToVideoCodecProfile( |
| 50 webrtc::VideoCodecType type) { | 60 webrtc::VideoCodecType type) { |
| 51 switch (type) { | 61 switch (type) { |
| 52 case webrtc::kVideoCodecVP8: | 62 case webrtc::kVideoCodecVP8: |
| 53 return media::VP8PROFILE_ANY; | 63 return media::VP8PROFILE_ANY; |
| 54 case webrtc::kVideoCodecH264: | 64 case webrtc::kVideoCodecH264: |
| 55 case webrtc::kVideoCodecGeneric: | 65 case webrtc::kVideoCodecGeneric: |
| 56 return media::H264PROFILE_MAIN; | 66 return media::H264PROFILE_MAIN; |
| 57 default: | 67 default: |
| 58 return media::VIDEO_CODEC_PROFILE_UNKNOWN; | 68 return media::VIDEO_CODEC_PROFILE_UNKNOWN; |
| 59 } | 69 } |
| 60 } | 70 } |
| 61 | 71 |
| 62 } // anonymous namespace | |
| 63 | |
| 64 RTCVideoEncoderFactory::RTCVideoEncoderFactory( | 72 RTCVideoEncoderFactory::RTCVideoEncoderFactory( |
| 65 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories) | 73 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories) |
| 66 : gpu_factories_(gpu_factories) { | 74 : gpu_factories_(gpu_factories), |
| 67 // Query media::VideoEncodeAccelerator (statically) for our supported codecs. | 75 get_supported_profiles_waiter_(true, false), |
| 68 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles = | 76 weak_factory_(this) { |
| 69 GpuVideoEncodeAcceleratorHost::GetSupportedProfiles(); | 77 // Query media::VideoEncodeAccelerator for our supported codecs. |
| 70 for (size_t i = 0; i < profiles.size(); ++i) | 78 gpu_factories_->GetTaskRunner()->PostTask( |
| 71 VEAToWebRTCCodecs(&codecs_, profiles[i]); | 79 FROM_HERE, |
| 80 base::Bind(&RTCVideoEncoderFactory::GetSupportedCodecs, | |
| 81 base::Unretained(this), | |
|
wuchengli
2014/09/19 14:27:24
The constructor and destructor run on the child th
| |
| 82 base::MessageLoopProxy::current())); | |
| 72 } | 83 } |
| 73 | 84 |
| 74 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() {} | 85 RTCVideoEncoderFactory::~RTCVideoEncoderFactory() { |
| 86 weak_factory_.InvalidateWeakPtrs(); | |
|
wuchengli
2014/09/19 14:27:24
Invalidate the weak pointer in case the codecs_ ha
| |
| 87 get_supported_profiles_waiter_.Wait(); | |
| 88 } | |
| 75 | 89 |
| 76 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( | 90 webrtc::VideoEncoder* RTCVideoEncoderFactory::CreateVideoEncoder( |
| 77 webrtc::VideoCodecType type) { | 91 webrtc::VideoCodecType type) { |
| 78 bool found = false; | 92 bool found = false; |
| 79 for (size_t i = 0; i < codecs_.size(); ++i) { | 93 for (size_t i = 0; i < codecs_.size(); ++i) { |
| 80 if (codecs_[i].type == type) { | 94 if (codecs_[i].type == type) { |
| 81 found = true; | 95 found = true; |
| 82 break; | 96 break; |
| 83 } | 97 } |
| 84 } | 98 } |
| 85 if (!found) | 99 if (!found) |
| 86 return NULL; | 100 return NULL; |
| 87 return new RTCVideoEncoder( | 101 return new RTCVideoEncoder( |
| 88 type, WebRTCCodecToVideoCodecProfile(type), gpu_factories_); | 102 type, WebRTCCodecToVideoCodecProfile(type), gpu_factories_); |
| 89 } | 103 } |
| 90 | 104 |
| 91 void RTCVideoEncoderFactory::AddObserver(Observer* observer) { | 105 void RTCVideoEncoderFactory::AddObserver(Observer* observer) { |
| 92 // No-op: our codec list is populated on installation. | 106 observers_.push_back(observer); |
| 93 } | 107 } |
| 94 | 108 |
| 95 void RTCVideoEncoderFactory::RemoveObserver(Observer* observer) {} | 109 void RTCVideoEncoderFactory::RemoveObserver(Observer* observer) { |
| 110 observers_.remove(observer); | |
| 111 } | |
| 96 | 112 |
| 97 const std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>& | 113 const std::vector<cricket::WebRtcVideoEncoderFactory::VideoCodec>& |
| 98 RTCVideoEncoderFactory::codecs() const { | 114 RTCVideoEncoderFactory::codecs() const { |
| 99 return codecs_; | 115 return codecs_; |
| 100 } | 116 } |
| 101 | 117 |
| 102 void RTCVideoEncoderFactory::DestroyVideoEncoder( | 118 void RTCVideoEncoderFactory::DestroyVideoEncoder( |
| 103 webrtc::VideoEncoder* encoder) { | 119 webrtc::VideoEncoder* encoder) { |
| 104 delete encoder; | 120 delete encoder; |
| 105 } | 121 } |
| 106 | 122 |
| 123 void RTCVideoEncoderFactory::GetSupportedCodecs( | |
| 124 const scoped_refptr<base::MessageLoopProxy>& message_loop) { | |
| 125 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; | |
| 126 scoped_ptr<media::VideoEncodeAccelerator> video_encoder = | |
| 127 gpu_factories_->CreateVideoEncodeAccelerator(); | |
| 128 if (video_encoder) | |
| 129 profiles = video_encoder->GetSupportedProfiles(); | |
| 130 video_encoder.reset(); | |
| 131 message_loop->PostTask(FROM_HERE, | |
| 132 base::Bind(&RTCVideoEncoderFactory::VEAToWebRTCCodecs, | |
| 133 weak_factory_.GetWeakPtr(), | |
| 134 profiles)); | |
| 135 get_supported_profiles_waiter_.Signal(); | |
| 136 } | |
| 137 | |
| 107 } // namespace content | 138 } // namespace content |
| OLD | NEW |