Chromium Code Reviews| Index: content/common/gpu/media/gpu_video_encode_accelerator.cc |
| diff --git a/content/common/gpu/media/gpu_video_encode_accelerator.cc b/content/common/gpu/media/gpu_video_encode_accelerator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fa0a2b5449c1d9606915d23154cf94ba1689fd1c |
| --- /dev/null |
| +++ b/content/common/gpu/media/gpu_video_encode_accelerator.cc |
| @@ -0,0 +1,190 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
| + |
| +#include "base/logging.h" |
| +#include "content/common/gpu/gpu_channel.h" |
| +#include "content/common/gpu/gpu_messages.h" |
| +#include "ipc/ipc_message_macros.h" |
| + |
| +namespace content { |
| + |
| +GpuVideoEncodeAccelerator::GpuVideoEncodeAccelerator(GpuChannel* gpu_channel, |
| + int32 route_id) |
| + : channel_(gpu_channel), route_id_(route_id) {} |
| + |
| +GpuVideoEncodeAccelerator::~GpuVideoEncodeAccelerator() { |
| + encoder_.release()->Destroy(); |
|
Ami GONE FROM CHROMIUM
2013/07/31 23:01:12
will crash if CreateEncoder() didn't succeed.
sheu
2013/08/02 01:27:49
Done.
|
| +} |
| + |
| +bool GpuVideoEncodeAccelerator::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAccelerator, message) |
| + IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Initialize, OnInitialize) |
| + IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_Encode, OnEncode) |
| + IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer, |
| + OnUseOutputBitstreamBuffer) |
| + IPC_MESSAGE_HANDLER( |
| + AcceleratedVideoEncoderMsg_RequestEncodingParameterChange, |
| + OnRequestEncodingParameterChange) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::OnChannelError() { |
| + NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| + if (channel_) |
| + channel_ = NULL; |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::NotifyInitializeDone() { |
| + if (!Send( |
| + new AcceleratedVideoEncoderHostMsg_NotifyInitializeDone(route_id_))) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::NotifyInitializeDone(): " |
|
Ami GONE FROM CHROMIUM
2013/07/31 23:01:12
Here and (lots of) elsewhere, Send() does its own
sheu
2013/08/02 01:27:49
Sounds good.
|
| + "Send() failed"; |
| + } |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::RequireBitstreamBuffers( |
| + int input_count, |
| + const gfx::Size& input_dimensions, |
| + size_t output_size) { |
| + if (!Send(new AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers( |
| + route_id_, input_count, input_dimensions, output_size))) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::RequireBitstreamBuffers(): " |
| + "Send() failed"; |
| + } |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::NotifyInputDone(int32 bitstream_buffer_id) { |
| + if (!Send(new AcceleratedVideoEncoderHostMsg_NotifyInputDone( |
| + route_id_, bitstream_buffer_id))) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::NotifyInputDone(): " |
| + "Send() failed: bitstream_buffer_id=" << bitstream_buffer_id; |
| + } |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::BitstreamBufferReady(int32 bitstream_buffer_id, |
| + size_t payload_size, |
| + bool key_frame) { |
| + if (!Send(new AcceleratedVideoEncoderHostMsg_BitstreamBufferReady( |
| + route_id_, bitstream_buffer_id, payload_size, key_frame))) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::BitstreamBufferReady(): " |
| + "Send() failed: bitstream_buffer_id=" << bitstream_buffer_id; |
| + } |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::NotifyError( |
| + media::VideoEncodeAccelerator::Error error) { |
| + if (!Send(new AcceleratedVideoEncoderHostMsg_NotifyError(route_id_, error))) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::NotifyError(): Send() failed: " |
| + "error=" << error; |
| + } |
| +} |
| + |
| +// static |
| +std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
| +GpuVideoEncodeAccelerator::GetSupportedProfiles() { |
| + std::vector<media::VideoEncodeAccelerator::SupportedProfile> profiles; |
| + |
| + // TODO(sheu): return platform-specific profiles. |
| + return profiles; |
| +} |
| + |
| +scoped_ptr<media::VideoEncodeAccelerator> |
| +GpuVideoEncodeAccelerator::CreateEncoder() { |
| + scoped_ptr<media::VideoEncodeAccelerator> encoder; |
| + |
| + // TODO(sheu): return platform-specific encoder. |
| + return encoder.Pass(); |
| +} |
| + |
| +bool GpuVideoEncodeAccelerator::Send(IPC::Message* message) { |
| + if (!channel_) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): no channel"; |
| + delete message; |
|
Ami GONE FROM CHROMIUM
2013/07/31 23:01:12
NotifyError?
sheu
2013/08/02 01:27:49
<zen> what is the sound of a NULL IPC channel send
|
| + return false; |
| + } else if (!channel_->Send(message)) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::Send(): sending failed"; |
| + NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::OnInitialize( |
| + media::VideoFrame::Format input_format, |
| + const gfx::Size& input_resolution, |
| + media::VideoCodecProfile output_profile, |
| + int32 initial_bitrate) { |
| + DVLOG(2) << "GpuVideoEncodeAccelerator::OnInitialize(): " |
| + "input_format=" << input_format |
| + << ", input_resolution=" << input_resolution.width() |
| + << "x" << input_resolution.height() |
|
Ami GONE FROM CHROMIUM
2013/07/31 23:01:12
here and elsewhere you could use gfx::Size::ToStri
sheu
2013/08/02 01:27:49
Did not know that existed. Sweet!
|
| + << ", output_profile=" << output_profile |
| + << ", initial_bitrate=" << initial_bitrate; |
| + DCHECK(!encoder_); |
| + encoder_ = CreateEncoder().Pass(); |
| + if (!encoder_) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnInitialize(): VEA creation " |
| + "failed"; |
| + NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| + return; |
| + } |
| + |
| + encoder_->Initialize( |
| + input_format, input_resolution, output_profile, initial_bitrate); |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::OnEncode(int32 buffer_id, |
| + base::SharedMemoryHandle buffer_handle, |
| + uint32 buffer_size, |
| + bool force_keyframe) { |
| + DVLOG(3) << "GpuVideoEncodeAccelerator::OnEncode(): buffer_id=" << buffer_id |
| + << ", buffer_size=" << buffer_size |
| + << ", force_keyframe=" << force_keyframe; |
| + if (!encoder_) |
| + return; |
| + if (buffer_id < 0) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid buffer_id=" |
| + << buffer_id; |
| + NotifyError(media::VideoEncodeAccelerator::kInvalidArgumentError); |
| + return; |
| + } |
| + encoder_->Encode( |
| + media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size), |
| + force_keyframe); |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer( |
| + int32 buffer_id, |
| + base::SharedMemoryHandle buffer_handle, |
| + uint32 buffer_size) { |
| + DVLOG(3) << "GpuVideoEncodeAccelerator::OnUseOutputBitstreamBuffer(): " |
| + "buffer_id=" << buffer_id |
| + << ", buffer_size=" << buffer_size; |
| + if (!encoder_) |
| + return; |
| + if (buffer_id < 0) { |
| + DLOG(ERROR) << "GpuVideoEncodeAccelerator::OnEncode(): invalid buffer_id=" |
| + << buffer_id; |
| + NotifyError(media::VideoEncodeAccelerator::kInvalidArgumentError); |
| + return; |
| + } |
| + encoder_->UseOutputBitstreamBuffer( |
| + media::BitstreamBuffer(buffer_id, buffer_handle, buffer_size)); |
| +} |
| + |
| +void GpuVideoEncodeAccelerator::OnRequestEncodingParameterChange( |
| + int32 bitrate) { |
| + DVLOG(2) << "GpuVideoEncodeAccelerator::OnRequestEncodingParametersChange(): " |
| + "bitrate=" << bitrate; |
| + if (!encoder_) |
| + return; |
| + encoder_->RequestEncodingParameterChange(bitrate); |
| +} |
| + |
| +} // namespace content |