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/common/gpu/media/gpu_video_encode_accelerator.h" | 5 #include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "content/common/gpu/gpu_channel.h" | 13 #include "content/common/gpu/gpu_channel.h" |
14 #include "content/common/gpu/gpu_messages.h" | 14 #include "content/common/gpu/gpu_messages.h" |
15 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
16 #include "ipc/ipc_message_macros.h" | 16 #include "ipc/ipc_message_macros.h" |
17 #include "media/base/limits.h" | 17 #include "media/base/limits.h" |
18 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
19 | 19 |
20 #if defined(OS_CHROMEOS) && defined(USE_X11) | 20 #if defined(OS_CHROMEOS) && defined(USE_X11) |
21 | 21 |
22 #if defined(ARCH_CPU_ARMEL) | 22 #if defined(ARCH_CPU_ARMEL) |
23 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | 23 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" |
24 #elif defined(ARCH_CPU_X86_FAMILY) | 24 #elif defined(ARCH_CPU_X86_FAMILY) |
25 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | |
25 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | 26 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" |
26 #include "ui/gfx/x/x11_types.h" | 27 #include "ui/gfx/x/x11_types.h" |
27 #endif | 28 #endif |
28 | 29 |
29 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 30 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
30 #include "content/common/gpu/media/android_video_encode_accelerator.h" | 31 #include "content/common/gpu/media/android_video_encode_accelerator.h" |
31 #endif | 32 #endif |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 | 35 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 if (input_visible_size.width() > media::limits::kMaxDimension || | 89 if (input_visible_size.width() > media::limits::kMaxDimension || |
89 input_visible_size.height() > media::limits::kMaxDimension || | 90 input_visible_size.height() > media::limits::kMaxDimension || |
90 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 91 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
91 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 92 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
92 "input_visible_size " << input_visible_size.ToString() | 93 "input_visible_size " << input_visible_size.ToString() |
93 << " too large"; | 94 << " too large"; |
94 SendCreateEncoderReply(init_done_msg, false); | 95 SendCreateEncoderReply(init_done_msg, false); |
95 return; | 96 return; |
96 } | 97 } |
97 | 98 |
98 encoder_ = CreateEncoder(); | 99 scoped_ptr<media::VideoEncodeAccelerator> encoders[2] = { |
99 if (!encoder_) { | 100 CreateDefaultEncoder(), CreateVaapiEncoder() |
Pawel Osciak
2014/12/30 06:14:38
Oh, this is not a bad idea! But I'd instead have a
henryhsu
2014/12/30 14:40:51
Good idea!
| |
101 }; | |
102 if (!encoders[0] && !encoders[1]) { | |
100 DLOG(ERROR) | 103 DLOG(ERROR) |
101 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; | 104 << "GpuVideoEncodeAccelerator::Initialize(): VEA creation failed"; |
102 SendCreateEncoderReply(init_done_msg, false); | 105 SendCreateEncoderReply(init_done_msg, false); |
103 return; | 106 return; |
104 } | 107 } |
105 if (!encoder_->Initialize(input_format, | 108 // Try all possible encoders and use the first successful encoder. |
106 input_visible_size, | 109 for (size_t i = 0; i < 2; ++i) { |
107 output_profile, | 110 if (encoders[i] && encoders[i]->Initialize(input_format, |
108 initial_bitrate, | 111 input_visible_size, |
109 this)) { | 112 output_profile, |
110 DLOG(ERROR) | 113 initial_bitrate, |
111 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; | 114 this)) { |
112 SendCreateEncoderReply(init_done_msg, false); | 115 encoder_ = encoders[i].Pass(); |
113 return; | 116 input_format_ = input_format; |
117 input_visible_size_ = input_visible_size; | |
118 SendCreateEncoderReply(init_done_msg, true); | |
119 return; | |
120 } | |
114 } | 121 } |
115 input_format_ = input_format; | 122 DLOG(ERROR) |
116 input_visible_size_ = input_visible_size; | 123 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; |
117 SendCreateEncoderReply(init_done_msg, true); | 124 SendCreateEncoderReply(init_done_msg, false); |
118 } | 125 } |
119 | 126 |
120 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { | 127 bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
121 bool handled = true; | 128 bool handled = true; |
122 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) | 129 IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
123 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) | 130 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
124 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, | 131 IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
125 OnUseOutputBitstreamBuffer) | 132 OnUseOutputBitstreamBuffer) |
126 IPC_MESSAGE_HANDLER( | 133 IPC_MESSAGE_HANDLER( |
127 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, | 134 AcceleratedVideoEncoderMsg_RequestEncodingParametersChange, |
(...skipping 30 matching lines...) Expand all Loading... | |
158 DCHECK(stub_); | 165 DCHECK(stub_); |
159 stub_->channel()->RemoveRoute(host_route_id_); | 166 stub_->channel()->RemoveRoute(host_route_id_); |
160 stub_->RemoveDestructionObserver(this); | 167 stub_->RemoveDestructionObserver(this); |
161 encoder_.reset(); | 168 encoder_.reset(); |
162 delete this; | 169 delete this; |
163 } | 170 } |
164 | 171 |
165 // static | 172 // static |
166 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 173 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
167 GpuVideoEncodeAccelerator::GetSupportedProfiles() { | 174 GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
168 scoped_ptr<media::VideoEncodeAccelerator> encoder = CreateEncoder(); | 175 std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
169 if (!encoder) | 176 scoped_ptr<media::VideoEncodeAccelerator> encoders[2] = { |
177 CreateDefaultEncoder(), CreateVaapiEncoder() | |
178 }; | |
179 if (!encoders[0] && !encoders[1]) | |
170 return std::vector<gpu::VideoEncodeAcceleratorSupportedProfile>(); | 180 return std::vector<gpu::VideoEncodeAcceleratorSupportedProfile>(); |
171 return ConvertMediaToGpuProfiles(encoder->GetSupportedProfiles()); | 181 |
182 std::set<media::VideoEncodeAccelerator::SupportedProfile> profile_set; | |
183 for (size_t i = 0; i < 2; ++i) { | |
184 if (!encoders[i]) | |
185 continue; | |
186 std::vector<media::VideoEncodeAccelerator::SupportedProfile> | |
187 vea_profiles = encoders[i]->GetSupportedProfiles(); | |
188 for (size_t j = 0; j < vea_profiles.size(); ++j) | |
189 profile_set.insert(vea_profiles[j]); | |
190 } | |
191 profiles.assign(profile_set.begin(), profile_set.end()); | |
192 return ConvertMediaToGpuProfiles(profiles); | |
172 } | 193 } |
173 | 194 |
195 // static | |
174 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> | 196 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> |
175 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< | 197 GpuVideoEncodeAccelerator::ConvertMediaToGpuProfiles(const std::vector< |
176 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { | 198 media::VideoEncodeAccelerator::SupportedProfile>& media_profiles) { |
177 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; | 199 std::vector<gpu::VideoEncodeAcceleratorSupportedProfile> profiles; |
178 for (size_t i = 0; i < media_profiles.size(); i++) { | 200 for (size_t i = 0; i < media_profiles.size(); i++) { |
179 gpu::VideoEncodeAcceleratorSupportedProfile profile; | 201 gpu::VideoEncodeAcceleratorSupportedProfile profile; |
180 profile.profile = | 202 profile.profile = |
181 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); | 203 static_cast<gpu::VideoCodecProfile>(media_profiles[i].profile); |
182 profile.max_resolution = media_profiles[i].max_resolution; | 204 profile.max_resolution = media_profiles[i].max_resolution; |
183 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; | 205 profile.max_framerate_numerator = media_profiles[i].max_framerate_numerator; |
184 profile.max_framerate_denominator = | 206 profile.max_framerate_denominator = |
185 media_profiles[i].max_framerate_denominator; | 207 media_profiles[i].max_framerate_denominator; |
186 profiles.push_back(profile); | 208 profiles.push_back(profile); |
187 } | 209 } |
188 return profiles; | 210 return profiles; |
189 } | 211 } |
190 | 212 |
213 // static | |
191 scoped_ptr<media::VideoEncodeAccelerator> | 214 scoped_ptr<media::VideoEncodeAccelerator> |
192 GpuVideoEncodeAccelerator::CreateEncoder() { | 215 GpuVideoEncodeAccelerator::CreateDefaultEncoder() { |
193 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 216 scoped_ptr<media::VideoEncodeAccelerator> encoder; |
194 #if defined(OS_CHROMEOS) && defined(USE_X11) | 217 #if defined(OS_CHROMEOS) && defined(USE_X11) |
195 #if defined(ARCH_CPU_ARMEL) | |
196 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 218 scoped_ptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
197 if (device) | 219 if (device) |
198 encoder.reset(new V4L2VideoEncodeAccelerator(device.Pass())); | 220 encoder.reset(new V4L2VideoEncodeAccelerator(device.Pass())); |
199 #elif defined(ARCH_CPU_X86_FAMILY) | |
200 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
201 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) | |
202 encoder.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); | |
203 #endif | |
204 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 221 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
205 encoder.reset(new AndroidVideoEncodeAccelerator()); | 222 encoder.reset(new AndroidVideoEncodeAccelerator()); |
206 #endif | 223 #endif |
207 return encoder.Pass(); | 224 return encoder.Pass(); |
208 } | 225 } |
209 | 226 |
227 // static | |
228 scoped_ptr<media::VideoEncodeAccelerator> | |
229 GpuVideoEncodeAccelerator::CreateVaapiEncoder() { | |
230 scoped_ptr<media::VideoEncodeAccelerator> encoder; | |
231 #if defined(OS_CHROMEOS) && defined(USE_X11) && defined(ARCH_CPU_X86_FAMILY) | |
232 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
233 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) | |
234 encoder.reset(new VaapiVideoEncodeAccelerator(gfx::GetXDisplay())); | |
235 #endif | |
236 return encoder.Pass(); | |
237 } | |
238 | |
210 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, | 239 void GpuVideoEncodeAccelerator::OnEncode(int32 frame_id, |
211 base::SharedMemoryHandle buffer_handle, | 240 base::SharedMemoryHandle buffer_handle, |
212 uint32 buffer_size, | 241 uint32 buffer_size, |
213 bool force_keyframe) { | 242 bool force_keyframe) { |
214 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id | 243 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): frame_id=" << frame_id |
215 << ", buffer_size=" << buffer_size | 244 << ", buffer_size=" << buffer_size |
216 << ", force_keyframe=" << force_keyframe; | 245 << ", force_keyframe=" << force_keyframe; |
217 if (!encoder_) | 246 if (!encoder_) |
218 return; | 247 return; |
219 if (frame_id < 0) { | 248 if (frame_id < 0) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 stub_->channel()->Send(message); | 344 stub_->channel()->Send(message); |
316 } | 345 } |
317 | 346 |
318 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, | 347 void GpuVideoEncodeAccelerator::SendCreateEncoderReply(IPC::Message* message, |
319 bool succeeded) { | 348 bool succeeded) { |
320 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); | 349 GpuCommandBufferMsg_CreateVideoEncoder::WriteReplyParams(message, succeeded); |
321 Send(message); | 350 Send(message); |
322 } | 351 } |
323 | 352 |
324 } // namespace content | 353 } // namespace content |
OLD | NEW |