| 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 "media/gpu/ipc/service/gpu_video_encode_accelerator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 14 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" | 17 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" |
| 18 #include "gpu/ipc/service/gpu_channel.h" | 18 #include "gpu/ipc/service/gpu_channel.h" |
| 19 #include "gpu/ipc/service/gpu_channel_manager.h" | 19 #include "gpu/ipc/service/gpu_channel_manager.h" |
| 20 #include "ipc/ipc_message_macros.h" | 20 #include "ipc/ipc_message_macros.h" |
| 21 #include "media/base/bind_to_current_loop.h" | 21 #include "media/base/bind_to_current_loop.h" |
| 22 #include "media/base/limits.h" | 22 #include "media/base/limits.h" |
| 23 #include "media/base/video_frame.h" | 23 #include "media/base/video_frame.h" |
| 24 #include "media/gpu/ipc/common/gpu_video_accelerator_util.h" | 24 #include "media/gpu/gpu_video_accelerator_util.h" |
| 25 #include "media/gpu/ipc/common/media_messages.h" | 25 #include "media/gpu/ipc/common/media_messages.h" |
| 26 | 26 |
| 27 #if defined(OS_CHROMEOS) | 27 #if defined(OS_CHROMEOS) |
| 28 #if defined(USE_V4L2_CODEC) | 28 #if defined(USE_V4L2_CODEC) |
| 29 #include "content/common/gpu/media/v4l2_video_encode_accelerator.h" | 29 #include "media/gpu/v4l2_video_encode_accelerator.h" |
| 30 #endif | 30 #endif |
| 31 #if defined(ARCH_CPU_X86_FAMILY) | 31 #if defined(ARCH_CPU_X86_FAMILY) |
| 32 #include "content/common/gpu/media/vaapi_video_encode_accelerator.h" | 32 #include "media/gpu/vaapi_video_encode_accelerator.h" |
| 33 #endif | 33 #endif |
| 34 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) | 34 #elif defined(OS_ANDROID) && defined(ENABLE_WEBRTC) |
| 35 #include "content/common/gpu/media/android_video_encode_accelerator.h" | 35 #include "media/gpu/android_video_encode_accelerator.h" |
| 36 #elif defined(OS_MACOSX) | 36 #elif defined(OS_MACOSX) |
| 37 #include "content/common/gpu/media/vt_video_encode_accelerator_mac.h" | 37 #include "media/gpu/vt_video_encode_accelerator_mac.h" |
| 38 #endif | 38 #endif |
| 39 | 39 |
| 40 namespace content { | 40 namespace media { |
| 41 | 41 |
| 42 static bool MakeDecoderContextCurrent( | 42 static bool MakeDecoderContextCurrent( |
| 43 const base::WeakPtr<gpu::GpuCommandBufferStub> stub) { | 43 const base::WeakPtr<gpu::GpuCommandBufferStub> stub) { |
| 44 if (!stub) { | 44 if (!stub) { |
| 45 DLOG(ERROR) << "Stub is gone; won't MakeCurrent()."; | 45 DLOG(ERROR) << "Stub is gone; won't MakeCurrent()."; |
| 46 return false; | 46 return false; |
| 47 } | 47 } |
| 48 | 48 |
| 49 if (!stub->decoder()->MakeCurrent()) { | 49 if (!stub->decoder()->MakeCurrent()) { |
| 50 DLOG(ERROR) << "Failed to MakeCurrent()"; | 50 DLOG(ERROR) << "Failed to MakeCurrent()"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 72 // the VEA has already been destroyed in there. | 72 // the VEA has already been destroyed in there. |
| 73 DCHECK(!encoder_); | 73 DCHECK(!encoder_); |
| 74 } | 74 } |
| 75 | 75 |
| 76 bool GpuVideoEncodeAccelerator::Initialize( | 76 bool GpuVideoEncodeAccelerator::Initialize( |
| 77 media::VideoPixelFormat input_format, | 77 media::VideoPixelFormat input_format, |
| 78 const gfx::Size& input_visible_size, | 78 const gfx::Size& input_visible_size, |
| 79 media::VideoCodecProfile output_profile, | 79 media::VideoCodecProfile output_profile, |
| 80 uint32_t initial_bitrate) { | 80 uint32_t initial_bitrate) { |
| 81 DVLOG(2) << "GpuVideoEncodeAccelerator::Initialize(): " | 81 DVLOG(2) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 82 "input_format=" << input_format | 82 "input_format=" |
| 83 << input_format |
| 83 << ", input_visible_size=" << input_visible_size.ToString() | 84 << ", input_visible_size=" << input_visible_size.ToString() |
| 84 << ", output_profile=" << output_profile | 85 << ", output_profile=" << output_profile |
| 85 << ", initial_bitrate=" << initial_bitrate; | 86 << ", initial_bitrate=" << initial_bitrate; |
| 86 DCHECK(!encoder_); | 87 DCHECK(!encoder_); |
| 87 | 88 |
| 88 if (!stub_->channel()->AddRoute(host_route_id_, stub_->stream_id(), this)) { | 89 if (!stub_->channel()->AddRoute(host_route_id_, stub_->stream_id(), this)) { |
| 89 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 90 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 90 "failed to add route"; | 91 "failed to add route"; |
| 91 return false; | 92 return false; |
| 92 } | 93 } |
| 93 | 94 |
| 94 if (input_visible_size.width() > media::limits::kMaxDimension || | 95 if (input_visible_size.width() > media::limits::kMaxDimension || |
| 95 input_visible_size.height() > media::limits::kMaxDimension || | 96 input_visible_size.height() > media::limits::kMaxDimension || |
| 96 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 97 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
| 97 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 98 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 98 "input_visible_size " << input_visible_size.ToString() | 99 "input_visible_size " |
| 99 << " too large"; | 100 << input_visible_size.ToString() << " too large"; |
| 100 return false; | 101 return false; |
| 101 } | 102 } |
| 102 | 103 |
| 103 const gpu::GpuPreferences& gpu_preferences = | 104 const gpu::GpuPreferences& gpu_preferences = |
| 104 stub_->channel()->gpu_channel_manager()->gpu_preferences(); | 105 stub_->channel()->gpu_channel_manager()->gpu_preferences(); |
| 105 | 106 |
| 106 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = | 107 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = |
| 107 CreateVEAFps(gpu_preferences); | 108 CreateVEAFps(gpu_preferences); |
| 108 // Try all possible encoders and use the first successful encoder. | 109 // Try all possible encoders and use the first successful encoder. |
| 109 for (size_t i = 0; i < create_vea_fps.size(); ++i) { | 110 for (size_t i = 0; i < create_vea_fps.size(); ++i) { |
| 110 encoder_ = (*create_vea_fps[i])(); | 111 encoder_ = (*create_vea_fps[i])(); |
| 111 if (encoder_ && encoder_->Initialize(input_format, | 112 if (encoder_ && |
| 112 input_visible_size, | 113 encoder_->Initialize(input_format, input_visible_size, output_profile, |
| 113 output_profile, | 114 initial_bitrate, this)) { |
| 114 initial_bitrate, | |
| 115 this)) { | |
| 116 input_format_ = input_format; | 115 input_format_ = input_format; |
| 117 input_visible_size_ = input_visible_size; | 116 input_visible_size_ = input_visible_size; |
| 118 return true; | 117 return true; |
| 119 } | 118 } |
| 120 } | 119 } |
| 121 encoder_.reset(); | 120 encoder_.reset(); |
| 122 DLOG(ERROR) | 121 DLOG(ERROR) |
| 123 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; | 122 << "GpuVideoEncodeAccelerator::Initialize(): VEA initialization failed"; |
| 124 return false; | 123 return false; |
| 125 } | 124 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| | 263 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| |
| 265 // is cleaned properly in case of an early return. | 264 // is cleaned properly in case of an early return. |
| 266 std::unique_ptr<base::SharedMemory> shm( | 265 std::unique_ptr<base::SharedMemory> shm( |
| 267 new base::SharedMemory(params.buffer_handle, true)); | 266 new base::SharedMemory(params.buffer_handle, true)); |
| 268 | 267 |
| 269 if (!encoder_) | 268 if (!encoder_) |
| 270 return; | 269 return; |
| 271 | 270 |
| 272 if (params.frame_id < 0) { | 271 if (params.frame_id < 0) { |
| 273 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid " | 272 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid " |
| 274 "frame_id=" << params.frame_id; | 273 "frame_id=" |
| 274 << params.frame_id; |
| 275 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 275 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 276 return; | 276 return; |
| 277 } | 277 } |
| 278 | 278 |
| 279 const uint32_t aligned_offset = | 279 const uint32_t aligned_offset = |
| 280 params.buffer_offset % base::SysInfo::VMAllocationGranularity(); | 280 params.buffer_offset % base::SysInfo::VMAllocationGranularity(); |
| 281 base::CheckedNumeric<off_t> map_offset = params.buffer_offset; | 281 base::CheckedNumeric<off_t> map_offset = params.buffer_offset; |
| 282 map_offset -= aligned_offset; | 282 map_offset -= aligned_offset; |
| 283 base::CheckedNumeric<size_t> map_size = params.buffer_size; | 283 base::CheckedNumeric<size_t> map_size = params.buffer_size; |
| 284 map_size += aligned_offset; | 284 map_size += aligned_offset; |
| 285 | 285 |
| 286 if (!map_offset.IsValid() || !map_size.IsValid()) { | 286 if (!map_offset.IsValid() || !map_size.IsValid()) { |
| 287 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode():" | 287 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode():" |
| 288 << " invalid (buffer_offset,buffer_size)"; | 288 << " invalid (buffer_offset,buffer_size)"; |
| 289 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 289 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 290 return; | 290 return; |
| 291 } | 291 } |
| 292 | 292 |
| 293 if (!shm->MapAt(map_offset.ValueOrDie(), map_size.ValueOrDie())) { | 293 if (!shm->MapAt(map_offset.ValueOrDie(), map_size.ValueOrDie())) { |
| 294 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " | 294 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 295 << "could not map frame_id=" << params.frame_id; | 295 << "could not map frame_id=" << params.frame_id; |
| 296 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 296 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 297 return; | 297 return; |
| 298 } | 298 } |
| 299 | 299 |
| 300 uint8_t* shm_memory = | 300 uint8_t* shm_memory = |
| 301 reinterpret_cast<uint8_t*>(shm->memory()) + aligned_offset; | 301 reinterpret_cast<uint8_t*>(shm->memory()) + aligned_offset; |
| 302 scoped_refptr<media::VideoFrame> frame = | 302 scoped_refptr<media::VideoFrame> frame = |
| 303 media::VideoFrame::WrapExternalSharedMemory( | 303 media::VideoFrame::WrapExternalSharedMemory( |
| 304 input_format_, | 304 input_format_, input_coded_size_, gfx::Rect(input_visible_size_), |
| 305 input_coded_size_, | 305 input_visible_size_, shm_memory, params.buffer_size, |
| 306 gfx::Rect(input_visible_size_), | 306 params.buffer_handle, params.buffer_offset, params.timestamp); |
| 307 input_visible_size_, | |
| 308 shm_memory, | |
| 309 params.buffer_size, | |
| 310 params.buffer_handle, | |
| 311 params.buffer_offset, | |
| 312 params.timestamp); | |
| 313 if (!frame) { | 307 if (!frame) { |
| 314 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " | 308 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): " |
| 315 << "could not create a frame"; | 309 << "could not create a frame"; |
| 316 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 310 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 317 return; | 311 return; |
| 318 } | 312 } |
| 319 frame->AddDestructionObserver( | 313 frame->AddDestructionObserver(media::BindToCurrentLoop(base::Bind( |
| 320 media::BindToCurrentLoop( | 314 &GpuVideoEncodeAccelerator::EncodeFrameFinished, |
| 321 base::Bind(&GpuVideoEncodeAccelerator::EncodeFrameFinished, | 315 weak_this_factory_.GetWeakPtr(), params.frame_id, base::Passed(&shm)))); |
| 322 weak_this_factory_.GetWeakPtr(), | |
| 323 params.frame_id, | |
| 324 base::Passed(&shm)))); | |
| 325 encoder_->Encode(frame, params.force_keyframe); | 316 encoder_->Encode(frame, params.force_keyframe); |
| 326 } | 317 } |
| 327 | 318 |
| 328 void GpuVideoEncodeAccelerator::OnEncode2( | 319 void GpuVideoEncodeAccelerator::OnEncode2( |
| 329 const AcceleratedVideoEncoderMsg_Encode_Params2& params) { | 320 const AcceleratedVideoEncoderMsg_Encode_Params2& params) { |
| 330 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode2: frame_id = " | 321 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode2: frame_id = " |
| 331 << params.frame_id << ", size=" << params.size.ToString() | 322 << params.frame_id << ", size=" << params.size.ToString() |
| 332 << ", force_keyframe=" << params.force_keyframe << ", handle type=" | 323 << ", force_keyframe=" << params.force_keyframe |
| 333 << params.gpu_memory_buffer_handles[0].type; | 324 << ", handle type=" << params.gpu_memory_buffer_handles[0].type; |
| 334 // Encoding GpuMemoryBuffer backed frames is not supported. | 325 // Encoding GpuMemoryBuffer backed frames is not supported. |
| 335 NOTREACHED(); | 326 NOTREACHED(); |
| 336 } | 327 } |
| 337 | 328 |
| 338 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( | 329 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( |
| 339 int32_t buffer_id, | 330 int32_t buffer_id, |
| 340 base::SharedMemoryHandle buffer_handle, | 331 base::SharedMemoryHandle buffer_handle, |
| 341 uint32_t buffer_size) { | 332 uint32_t buffer_size) { |
| 342 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 333 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 343 "buffer_id=" << buffer_id | 334 "buffer_id=" |
| 344 << ", buffer_size=" << buffer_size; | 335 << buffer_id << ", buffer_size=" << buffer_size; |
| 345 if (!encoder_) | 336 if (!encoder_) |
| 346 return; | 337 return; |
| 347 if (buffer_id < 0) { | 338 if (buffer_id < 0) { |
| 348 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 339 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 349 "invalid buffer_id=" << buffer_id; | 340 "invalid buffer_id=" |
| 341 << buffer_id; |
| 350 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 342 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 351 return; | 343 return; |
| 352 } | 344 } |
| 353 if (buffer_size < output_buffer_size_) { | 345 if (buffer_size < output_buffer_size_) { |
| 354 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 346 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 355 "buffer too small for buffer_id=" << buffer_id; | 347 "buffer too small for buffer_id=" |
| 348 << buffer_id; |
| 356 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 349 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 357 return; | 350 return; |
| 358 } | 351 } |
| 359 encoder_->UseOutputBitstreamBuffer( | 352 encoder_->UseOutputBitstreamBuffer( |
| 360 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); | 353 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
| 361 } | 354 } |
| 362 | 355 |
| 363 void GpuVideoEncodeAccelerator::OnDestroy() { | 356 void GpuVideoEncodeAccelerator::OnDestroy() { |
| 364 DVLOG(2) << "GpuVideoEncodeAccelerator::OnDestroy()"; | 357 DVLOG(2) << "GpuVideoEncodeAccelerator::OnDestroy()"; |
| 365 OnWillDestroyStub(); | 358 OnWillDestroyStub(); |
| 366 } | 359 } |
| 367 | 360 |
| 368 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( | 361 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( |
| 369 uint32_t bitrate, | 362 uint32_t bitrate, |
| 370 uint32_t framerate) { | 363 uint32_t framerate) { |
| 371 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " | 364 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
| 372 "bitrate=" << bitrate | 365 "bitrate=" |
| 373 << ", framerate=" << framerate; | 366 << bitrate << ", framerate=" << framerate; |
| 374 if (!encoder_) | 367 if (!encoder_) |
| 375 return; | 368 return; |
| 376 encoder_->RequestEncodingParametersChange(bitrate, framerate); | 369 encoder_->RequestEncodingParametersChange(bitrate, framerate); |
| 377 } | 370 } |
| 378 | 371 |
| 379 void GpuVideoEncodeAccelerator::EncodeFrameFinished( | 372 void GpuVideoEncodeAccelerator::EncodeFrameFinished( |
| 380 int32_t frame_id, | 373 int32_t frame_id, |
| 381 std::unique_ptr<base::SharedMemory> shm) { | 374 std::unique_ptr<base::SharedMemory> shm) { |
| 382 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, | 375 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, |
| 383 frame_id)); | 376 frame_id)); |
| 384 // Just let |shm| fall out of scope. | 377 // Just let |shm| fall out of scope. |
| 385 } | 378 } |
| 386 | 379 |
| 387 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { | 380 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| 388 stub_->channel()->Send(message); | 381 stub_->channel()->Send(message); |
| 389 } | 382 } |
| 390 | 383 |
| 391 } // namespace content | 384 } // namespace media |
| OLD | NEW |