| 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/numerics/safe_math.h" | 11 #include "base/numerics/safe_math.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" | 14 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" |
| 15 #include "content/common/gpu/gpu_channel.h" | 15 #include "content/common/gpu/gpu_channel.h" |
| 16 #include "content/common/gpu/gpu_channel_manager.h" |
| 16 #include "content/common/gpu/media/gpu_video_accelerator_util.h" | 17 #include "content/common/gpu/media/gpu_video_accelerator_util.h" |
| 17 #include "content/common/gpu/media/media_messages.h" | 18 #include "content/common/gpu/media/media_messages.h" |
| 18 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 19 #include "ipc/ipc_message_macros.h" | 20 #include "ipc/ipc_message_macros.h" |
| 20 #include "media/base/bind_to_current_loop.h" | 21 #include "media/base/bind_to_current_loop.h" |
| 21 #include "media/base/limits.h" | 22 #include "media/base/limits.h" |
| 22 #include "media/base/video_frame.h" | 23 #include "media/base/video_frame.h" |
| 23 | 24 |
| 24 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 25 #if defined(USE_V4L2_CODEC) | 26 #if defined(USE_V4L2_CODEC) |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 | 96 |
| 96 if (input_visible_size.width() > media::limits::kMaxDimension || | 97 if (input_visible_size.width() > media::limits::kMaxDimension || |
| 97 input_visible_size.height() > media::limits::kMaxDimension || | 98 input_visible_size.height() > media::limits::kMaxDimension || |
| 98 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 99 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
| 99 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 100 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 100 "input_visible_size " << input_visible_size.ToString() | 101 "input_visible_size " << input_visible_size.ToString() |
| 101 << " too large"; | 102 << " too large"; |
| 102 return false; | 103 return false; |
| 103 } | 104 } |
| 104 | 105 |
| 105 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> | 106 const gpu::GpuPreferences& gpu_preferences = |
| 106 create_vea_fps = CreateVEAFps(); | 107 stub_->channel()->gpu_channel_manager()->gpu_preferences(); |
| 108 |
| 109 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = |
| 110 CreateVEAFps(gpu_preferences); |
| 107 // Try all possible encoders and use the first successful encoder. | 111 // Try all possible encoders and use the first successful encoder. |
| 108 for (size_t i = 0; i < create_vea_fps.size(); ++i) { | 112 for (size_t i = 0; i < create_vea_fps.size(); ++i) { |
| 109 encoder_ = (*create_vea_fps[i])(); | 113 encoder_ = (*create_vea_fps[i])(); |
| 110 if (encoder_ && encoder_->Initialize(input_format, | 114 if (encoder_ && encoder_->Initialize(input_format, |
| 111 input_visible_size, | 115 input_visible_size, |
| 112 output_profile, | 116 output_profile, |
| 113 initial_bitrate, | 117 initial_bitrate, |
| 114 this)) { | 118 this)) { |
| 115 input_format_ = input_format; | 119 input_format_ = input_format; |
| 116 input_visible_size_ = input_visible_size; | 120 input_visible_size_ = input_visible_size; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 void GpuVideoEncodeAccelerator::OnWillDestroyStub() { | 169 void GpuVideoEncodeAccelerator::OnWillDestroyStub() { |
| 166 DCHECK(stub_); | 170 DCHECK(stub_); |
| 167 stub_->channel()->RemoveRoute(host_route_id_); | 171 stub_->channel()->RemoveRoute(host_route_id_); |
| 168 stub_->RemoveDestructionObserver(this); | 172 stub_->RemoveDestructionObserver(this); |
| 169 encoder_.reset(); | 173 encoder_.reset(); |
| 170 delete this; | 174 delete this; |
| 171 } | 175 } |
| 172 | 176 |
| 173 // static | 177 // static |
| 174 gpu::VideoEncodeAcceleratorSupportedProfiles | 178 gpu::VideoEncodeAcceleratorSupportedProfiles |
| 175 GpuVideoEncodeAccelerator::GetSupportedProfiles() { | 179 GpuVideoEncodeAccelerator::GetSupportedProfiles( |
| 180 const gpu::GpuPreferences& gpu_preferences) { |
| 176 media::VideoEncodeAccelerator::SupportedProfiles profiles; | 181 media::VideoEncodeAccelerator::SupportedProfiles profiles; |
| 177 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> | 182 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = |
| 178 create_vea_fps = CreateVEAFps(); | 183 CreateVEAFps(gpu_preferences); |
| 179 | 184 |
| 180 for (size_t i = 0; i < create_vea_fps.size(); ++i) { | 185 for (size_t i = 0; i < create_vea_fps.size(); ++i) { |
| 181 scoped_ptr<media::VideoEncodeAccelerator> | 186 scoped_ptr<media::VideoEncodeAccelerator> |
| 182 encoder = (*create_vea_fps[i])(); | 187 encoder = (*create_vea_fps[i])(); |
| 183 if (!encoder) | 188 if (!encoder) |
| 184 continue; | 189 continue; |
| 185 media::VideoEncodeAccelerator::SupportedProfiles vea_profiles = | 190 media::VideoEncodeAccelerator::SupportedProfiles vea_profiles = |
| 186 encoder->GetSupportedProfiles(); | 191 encoder->GetSupportedProfiles(); |
| 187 GpuVideoAcceleratorUtil::InsertUniqueEncodeProfiles( | 192 GpuVideoAcceleratorUtil::InsertUniqueEncodeProfiles( |
| 188 vea_profiles, &profiles); | 193 vea_profiles, &profiles); |
| 189 } | 194 } |
| 190 return GpuVideoAcceleratorUtil::ConvertMediaToGpuEncodeProfiles(profiles); | 195 return GpuVideoAcceleratorUtil::ConvertMediaToGpuEncodeProfiles(profiles); |
| 191 } | 196 } |
| 192 | 197 |
| 193 // static | 198 // static |
| 194 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> | 199 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> |
| 195 GpuVideoEncodeAccelerator::CreateVEAFps() { | 200 GpuVideoEncodeAccelerator::CreateVEAFps( |
| 201 const gpu::GpuPreferences& gpu_preferences) { |
| 196 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps; | 202 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps; |
| 203 #if defined(OS_CHROMEOS) && defined(USE_V4L2_CODEC) |
| 197 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateV4L2VEA); | 204 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateV4L2VEA); |
| 198 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateVaapiVEA); | 205 #endif |
| 199 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateAndroidVEA); | 206 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| 207 if (!gpu_preferences.disable_vaapi_accelerated_video_encode) |
| 208 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateVaapiVEA); |
| 209 #endif |
| 210 #if defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
| 211 if (!gpu_preferences.disable_web_rtc_hw_encoding) |
| 212 create_vea_fps.push_back(&GpuVideoEncodeAccelerator::CreateAndroidVEA); |
| 213 #endif |
| 200 return create_vea_fps; | 214 return create_vea_fps; |
| 201 } | 215 } |
| 202 | 216 |
| 217 #if defined(OS_CHROMEOS) && defined(USE_V4L2_CODEC) |
| 203 // static | 218 // static |
| 204 scoped_ptr<media::VideoEncodeAccelerator> | 219 scoped_ptr<media::VideoEncodeAccelerator> |
| 205 GpuVideoEncodeAccelerator::CreateV4L2VEA() { | 220 GpuVideoEncodeAccelerator::CreateV4L2VEA() { |
| 206 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 221 scoped_ptr<media::VideoEncodeAccelerator> encoder; |
| 207 #if defined(OS_CHROMEOS) && defined(USE_V4L2_CODEC) | |
| 208 scoped_refptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); | 222 scoped_refptr<V4L2Device> device = V4L2Device::Create(V4L2Device::kEncoder); |
| 209 if (device) | 223 if (device) |
| 210 encoder.reset(new V4L2VideoEncodeAccelerator(device)); | 224 encoder.reset(new V4L2VideoEncodeAccelerator(device)); |
| 211 #endif | |
| 212 return encoder; | 225 return encoder; |
| 213 } | 226 } |
| 227 #endif |
| 214 | 228 |
| 229 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) |
| 215 // static | 230 // static |
| 216 scoped_ptr<media::VideoEncodeAccelerator> | 231 scoped_ptr<media::VideoEncodeAccelerator> |
| 217 GpuVideoEncodeAccelerator::CreateVaapiVEA() { | 232 GpuVideoEncodeAccelerator::CreateVaapiVEA() { |
| 218 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 233 return make_scoped_ptr<media::VideoEncodeAccelerator>( |
| 219 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_X86_FAMILY) | 234 new VaapiVideoEncodeAccelerator()); |
| 220 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 235 } |
| 221 if (!cmd_line->HasSwitch(switches::kDisableVaapiAcceleratedVideoEncode)) | |
| 222 encoder.reset(new VaapiVideoEncodeAccelerator()); | |
| 223 #endif | 236 #endif |
| 224 return encoder; | |
| 225 } | |
| 226 | 237 |
| 238 #if defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
| 227 // static | 239 // static |
| 228 scoped_ptr<media::VideoEncodeAccelerator> | 240 scoped_ptr<media::VideoEncodeAccelerator> |
| 229 GpuVideoEncodeAccelerator::CreateAndroidVEA() { | 241 GpuVideoEncodeAccelerator::CreateAndroidVEA() { |
| 230 scoped_ptr<media::VideoEncodeAccelerator> encoder; | 242 return make_scoped_ptr<media::VideoEncodeAccelerator>( |
| 231 #if defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 243 new AndroidVideoEncodeAccelerator()); |
| 232 encoder.reset(new AndroidVideoEncodeAccelerator()); | 244 } |
| 233 #endif | 245 #endif |
| 234 return encoder; | |
| 235 } | |
| 236 | 246 |
| 237 void GpuVideoEncodeAccelerator::OnEncode( | 247 void GpuVideoEncodeAccelerator::OnEncode( |
| 238 const AcceleratedVideoEncoderMsg_Encode_Params& params) { | 248 const AcceleratedVideoEncoderMsg_Encode_Params& params) { |
| 239 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode: frame_id = " | 249 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode: frame_id = " |
| 240 << params.frame_id << ", buffer_size=" << params.buffer_size | 250 << params.frame_id << ", buffer_size=" << params.buffer_size |
| 241 << ", force_keyframe=" << params.force_keyframe; | 251 << ", force_keyframe=" << params.force_keyframe; |
| 242 DCHECK_EQ(media::PIXEL_FORMAT_I420, input_format_); | 252 DCHECK_EQ(media::PIXEL_FORMAT_I420, input_format_); |
| 243 | 253 |
| 244 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| | 254 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| |
| 245 // is cleaned properly in case of an early return. | 255 // is cleaned properly in case of an early return. |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, | 454 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, |
| 445 frame_id)); | 455 frame_id)); |
| 446 // Just let |buffers| fall out of scope. | 456 // Just let |buffers| fall out of scope. |
| 447 } | 457 } |
| 448 | 458 |
| 449 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { | 459 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| 450 stub_->channel()->Send(message); | 460 stub_->channel()->Send(message); |
| 451 } | 461 } |
| 452 | 462 |
| 453 } // namespace content | 463 } // namespace content |
| OLD | NEW |