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