| 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 "media/gpu/ipc/service/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" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after 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=" | 82 << "input_format=" << input_format |
| 83 << input_format | |
| 84 << ", input_visible_size=" << input_visible_size.ToString() | 83 << ", input_visible_size=" << input_visible_size.ToString() |
| 85 << ", output_profile=" << output_profile | 84 << ", output_profile=" << output_profile |
| 86 << ", initial_bitrate=" << initial_bitrate; | 85 << ", initial_bitrate=" << initial_bitrate; |
| 87 DCHECK(!encoder_); | 86 DCHECK(!encoder_); |
| 88 | 87 |
| 89 if (!stub_->channel()->AddRoute(host_route_id_, stub_->stream_id(), this)) { | 88 if (!stub_->channel()->AddRoute(host_route_id_, stub_->stream_id(), this)) { |
| 90 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 89 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 91 "failed to add route"; | 90 "failed to add route"; |
| 92 return false; | 91 return false; |
| 93 } | 92 } |
| 94 | 93 |
| 95 if (input_visible_size.width() > media::limits::kMaxDimension || | 94 if (input_visible_size.width() > media::limits::kMaxDimension || |
| 96 input_visible_size.height() > media::limits::kMaxDimension || | 95 input_visible_size.height() > media::limits::kMaxDimension || |
| 97 input_visible_size.GetArea() > media::limits::kMaxCanvas) { | 96 input_visible_size.GetArea() > media::limits::kMaxCanvas) { |
| 98 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " | 97 DLOG(ERROR) << "GpuVideoEncodeAccelerator::Initialize(): " |
| 99 "input_visible_size " | 98 << "input_visible_size " << input_visible_size.ToString() |
| 100 << input_visible_size.ToString() << " too large"; | 99 << " too large"; |
| 101 return false; | 100 return false; |
| 102 } | 101 } |
| 103 | 102 |
| 104 const gpu::GpuPreferences& gpu_preferences = | 103 const gpu::GpuPreferences& gpu_preferences = |
| 105 stub_->channel()->gpu_channel_manager()->gpu_preferences(); | 104 stub_->channel()->gpu_channel_manager()->gpu_preferences(); |
| 106 | 105 |
| 107 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = | 106 std::vector<GpuVideoEncodeAccelerator::CreateVEAFp> create_vea_fps = |
| 108 CreateVEAFps(gpu_preferences); | 107 CreateVEAFps(gpu_preferences); |
| 109 // Try all possible encoders and use the first successful encoder. | 108 // Try all possible encoders and use the first successful encoder. |
| 110 for (size_t i = 0; i < create_vea_fps.size(); ++i) { | 109 for (size_t i = 0; i < create_vea_fps.size(); ++i) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| | 262 // Wrap into a SharedMemory in the beginning, so that |params.buffer_handle| |
| 264 // is cleaned properly in case of an early return. | 263 // is cleaned properly in case of an early return. |
| 265 std::unique_ptr<base::SharedMemory> shm( | 264 std::unique_ptr<base::SharedMemory> shm( |
| 266 new base::SharedMemory(params.buffer_handle, true)); | 265 new base::SharedMemory(params.buffer_handle, true)); |
| 267 | 266 |
| 268 if (!encoder_) | 267 if (!encoder_) |
| 269 return; | 268 return; |
| 270 | 269 |
| 271 if (params.frame_id < 0) { | 270 if (params.frame_id < 0) { |
| 272 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid " | 271 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid " |
| 273 "frame_id=" | 272 << "frame_id=" << params.frame_id; |
| 274 << params.frame_id; | |
| 275 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 273 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 276 return; | 274 return; |
| 277 } | 275 } |
| 278 | 276 |
| 279 const uint32_t aligned_offset = | 277 const uint32_t aligned_offset = |
| 280 params.buffer_offset % base::SysInfo::VMAllocationGranularity(); | 278 params.buffer_offset % base::SysInfo::VMAllocationGranularity(); |
| 281 base::CheckedNumeric<off_t> map_offset = params.buffer_offset; | 279 base::CheckedNumeric<off_t> map_offset = params.buffer_offset; |
| 282 map_offset -= aligned_offset; | 280 map_offset -= aligned_offset; |
| 283 base::CheckedNumeric<size_t> map_size = params.buffer_size; | 281 base::CheckedNumeric<size_t> map_size = params.buffer_size; |
| 284 map_size += aligned_offset; | 282 map_size += aligned_offset; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 311 return; | 309 return; |
| 312 } | 310 } |
| 313 frame->AddDestructionObserver(media::BindToCurrentLoop(base::Bind( | 311 frame->AddDestructionObserver(media::BindToCurrentLoop(base::Bind( |
| 314 &GpuVideoEncodeAccelerator::EncodeFrameFinished, | 312 &GpuVideoEncodeAccelerator::EncodeFrameFinished, |
| 315 weak_this_factory_.GetWeakPtr(), params.frame_id, base::Passed(&shm)))); | 313 weak_this_factory_.GetWeakPtr(), params.frame_id, base::Passed(&shm)))); |
| 316 encoder_->Encode(frame, params.force_keyframe); | 314 encoder_->Encode(frame, params.force_keyframe); |
| 317 } | 315 } |
| 318 | 316 |
| 319 void GpuVideoEncodeAccelerator::OnEncode2( | 317 void GpuVideoEncodeAccelerator::OnEncode2( |
| 320 const AcceleratedVideoEncoderMsg_Encode_Params2& params) { | 318 const AcceleratedVideoEncoderMsg_Encode_Params2& params) { |
| 321 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode2: frame_id = " | 319 DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode2: " |
| 322 << params.frame_id << ", size=" << params.size.ToString() | 320 << "frame_id = " << params.frame_id |
| 321 << ", size=" << params.size.ToString() |
| 323 << ", force_keyframe=" << params.force_keyframe | 322 << ", force_keyframe=" << params.force_keyframe |
| 324 << ", handle type=" << params.gpu_memory_buffer_handles[0].type; | 323 << ", handle type=" << params.gpu_memory_buffer_handles[0].type; |
| 325 // Encoding GpuMemoryBuffer backed frames is not supported. | 324 // Encoding GpuMemoryBuffer backed frames is not supported. |
| 326 NOTREACHED(); | 325 NOTREACHED(); |
| 327 } | 326 } |
| 328 | 327 |
| 329 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( | 328 void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( |
| 330 int32_t buffer_id, | 329 int32_t buffer_id, |
| 331 base::SharedMemoryHandle buffer_handle, | 330 base::SharedMemoryHandle buffer_handle, |
| 332 uint32_t buffer_size) { | 331 uint32_t buffer_size) { |
| 333 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 332 DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 334 "buffer_id=" | 333 << "buffer_id=" << buffer_id << ", buffer_size=" << buffer_size; |
| 335 << buffer_id << ", buffer_size=" << buffer_size; | |
| 336 if (!encoder_) | 334 if (!encoder_) |
| 337 return; | 335 return; |
| 338 if (buffer_id < 0) { | 336 if (buffer_id < 0) { |
| 339 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 337 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 340 "invalid buffer_id=" | 338 << "invalid buffer_id=" << buffer_id; |
| 341 << buffer_id; | |
| 342 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 339 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 343 return; | 340 return; |
| 344 } | 341 } |
| 345 if (buffer_size < output_buffer_size_) { | 342 if (buffer_size < output_buffer_size_) { |
| 346 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " | 343 DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| 347 "buffer too small for buffer_id=" | 344 << "buffer too small for buffer_id=" << buffer_id; |
| 348 << buffer_id; | |
| 349 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 345 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 350 return; | 346 return; |
| 351 } | 347 } |
| 352 encoder_->UseOutputBitstreamBuffer( | 348 encoder_->UseOutputBitstreamBuffer( |
| 353 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); | 349 media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
| 354 } | 350 } |
| 355 | 351 |
| 356 void GpuVideoEncodeAccelerator::OnDestroy() { | 352 void GpuVideoEncodeAccelerator::OnDestroy() { |
| 357 DVLOG(2) << "GpuVideoEncodeAccelerator::OnDestroy()"; | 353 DVLOG(2) << "GpuVideoEncodeAccelerator::OnDestroy()"; |
| 358 OnWillDestroyStub(); | 354 OnWillDestroyStub(); |
| 359 } | 355 } |
| 360 | 356 |
| 361 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( | 357 void GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange( |
| 362 uint32_t bitrate, | 358 uint32_t bitrate, |
| 363 uint32_t framerate) { | 359 uint32_t framerate) { |
| 364 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " | 360 DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
| 365 "bitrate=" | 361 << "bitrate=" << bitrate << ", framerate=" << framerate; |
| 366 << bitrate << ", framerate=" << framerate; | |
| 367 if (!encoder_) | 362 if (!encoder_) |
| 368 return; | 363 return; |
| 369 encoder_->RequestEncodingParametersChange(bitrate, framerate); | 364 encoder_->RequestEncodingParametersChange(bitrate, framerate); |
| 370 } | 365 } |
| 371 | 366 |
| 372 void GpuVideoEncodeAccelerator::EncodeFrameFinished( | 367 void GpuVideoEncodeAccelerator::EncodeFrameFinished( |
| 373 int32_t frame_id, | 368 int32_t frame_id, |
| 374 std::unique_ptr<base::SharedMemory> shm) { | 369 std::unique_ptr<base::SharedMemory> shm) { |
| 375 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, | 370 Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone(host_route_id_, |
| 376 frame_id)); | 371 frame_id)); |
| 377 // Just let |shm| fall out of scope. | 372 // Just let |shm| fall out of scope. |
| 378 } | 373 } |
| 379 | 374 |
| 380 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { | 375 void GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| 381 stub_->channel()->Send(message); | 376 stub_->channel()->Send(message); |
| 382 } | 377 } |
| 383 | 378 |
| 384 } // namespace media | 379 } // namespace media |
| OLD | NEW |