Index: content/common/gpu/client/gpu_video_encode_accelerator_host.cc |
diff --git a/content/common/gpu/client/gpu_video_encode_accelerator_host.cc b/content/common/gpu/client/gpu_video_encode_accelerator_host.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ef1506b6431c57021b1356fadca83b1e94b767d |
--- /dev/null |
+++ b/content/common/gpu/client/gpu_video_encode_accelerator_host.cc |
@@ -0,0 +1,211 @@ |
+// 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/client/gpu_video_encode_accelerator_host.h" |
+ |
+#include "base/logging.h" |
+#include "content/common/gpu/client/gpu_channel_host.h" |
+#include "content/common/gpu/gpu_messages.h" |
+#include "content/common/gpu/media/gpu_video_encode_accelerator.h" |
+#include "media/base/video_frame.h" |
+ |
+namespace content { |
+ |
+GpuVideoEncodeAcceleratorHost::GpuVideoEncodeAcceleratorHost( |
+ media::VideoEncodeAccelerator::Client* client, |
+ const scoped_refptr<GpuChannelHost>& gpu_channel_host, |
+ int32 route_id) |
+ : client_(client), |
+ channel_(gpu_channel_host), |
+ route_id_(route_id), |
+ next_frame_id_(0) { |
+ channel_->AddRoute(route_id_, AsWeakPtr()); |
+} |
+ |
+GpuVideoEncodeAcceleratorHost::~GpuVideoEncodeAcceleratorHost() { |
+ if (channel_) |
+ channel_->RemoveRoute(route_id_); |
+} |
+ |
+// static |
+std::vector<media::VideoEncodeAccelerator::SupportedProfile> |
+GpuVideoEncodeAcceleratorHost::GetSupportedProfiles() { |
+ return GpuVideoEncodeAccelerator::GetSupportedProfiles(); |
+} |
+ |
+bool GpuVideoEncodeAcceleratorHost::OnMessageReceived( |
+ const IPC::Message& message) { |
+ bool handled = true; |
+ IPC_BEGIN_MESSAGE_MAP(GpuVideoEncodeAcceleratorHost, message) |
+ IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInitializeDone, |
+ OnNotifyInitializeDone) |
+ IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_RequireBitstreamBuffers, |
+ OnRequireBitstreamBuffers) |
+ IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyInputDone, |
+ OnNotifyInputDone) |
+ IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_BitstreamBufferReady, |
+ OnBitstreamBufferReady) |
+ IPC_MESSAGE_HANDLER(AcceleratedVideoEncoderHostMsg_NotifyError, |
+ OnNotifyError) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP() |
+ DCHECK(handled); |
+ return handled; |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnChannelError() { |
+ DLOG(ERROR) << "OnChannelError()"; |
+ OnNotifyError(kPlatformFailureError); |
+ if (channel_) { |
+ channel_->RemoveRoute(route_id_); |
+ channel_ = NULL; |
+ } |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Initialize( |
+ media::VideoFrame::Format input_format, |
+ const gfx::Size& input_visible_size, |
+ media::VideoCodecProfile output_profile, |
+ int32 initial_bitrate) { |
+ if (!channel_) |
+ return; |
+ Send(new AcceleratedVideoEncoderMsg_Initialize(route_id_, |
+ input_format, |
+ input_visible_size, |
+ output_profile, |
+ initial_bitrate)); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Encode( |
+ const scoped_refptr<media::VideoFrame>& frame, |
+ bool force_keyframe) { |
+ if (!channel_) |
+ return; |
+ if (!base::SharedMemory::IsHandleValid(frame->shared_memory_handle())) { |
+ DLOG(ERROR) << "Encode(): cannot encode frame not backed by shared memory"; |
+ OnNotifyError(kPlatformFailureError); |
+ } |
+ base::SharedMemoryHandle handle = |
+ channel_->ShareToGpuProcess(frame->shared_memory_handle()); |
+ if (!base::SharedMemory::IsHandleValid(handle)) { |
+ DLOG(ERROR) << "Encode(): failed to duplicate buffer handle for GPU " |
+ "process"; |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
+ } |
+ |
+ // We assume that planar frame data passed here is packed and contiguous. |
+ const size_t plane_count = media::VideoFrame::NumPlanes(frame->format()); |
+ size_t frame_size = 0; |
+ for (size_t i = 0; i < plane_count; ++i) { |
+ // Cast DCHECK parameters to void* to avoid printing uint8* as a string. |
+ DCHECK_EQ(reinterpret_cast<void*>(frame->data(i)), |
+ reinterpret_cast<void*>((frame->data(0) + frame_size))) |
+ << "plane=" << i; |
+ frame_size += frame->stride(i) * frame->rows(i); |
+ } |
+ |
+ Send(new AcceleratedVideoEncoderMsg_Encode( |
+ route_id_, next_frame_id_, handle, frame_size, force_keyframe)); |
+ frame_map_[next_frame_id_] = frame; |
+ |
+ // Mask against 30 bits, to avoid (undefined) wraparound on signed integer. |
piman
2013/08/09 04:04:58
why not use unsigned?
sheu
2013/08/09 08:26:41
Generally using negative values as error condition
|
+ next_frame_id_ = (next_frame_id_ + 1) & 0x3FFFFFFF; |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::UseOutputBitstreamBuffer( |
+ const media::BitstreamBuffer& buffer) { |
+ if (!channel_) |
+ return; |
+ base::SharedMemoryHandle handle = |
+ channel_->ShareToGpuProcess(buffer.handle()); |
+ if (!base::SharedMemory::IsHandleValid(handle)) { |
+ DLOG(ERROR) << "UseOutputBitstreamBuffer(): failed to duplicate buffer " |
+ "handle for GPU process: buffer.id()=" << buffer.id(); |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
+ } |
+ Send(new AcceleratedVideoEncoderMsg_UseOutputBitstreamBuffer( |
+ route_id_, buffer.id(), handle, buffer.size())); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::RequestEncodingParametersChange( |
+ int32 bitrate, |
+ uint32 framerate) { |
+ if (!channel_) |
+ return; |
+ Send(new AcceleratedVideoEncoderMsg_RequestEncodingParametersChange( |
+ route_id_, bitrate, framerate)); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Destroy() { |
+ if (channel_) |
+ Send(new GpuChannelMsg_DestroyVideoEncoder(route_id_)); |
+ delete this; |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone() { |
+ DVLOG(2) << "OnNotifyInitializeDone()"; |
+ if (client_) |
+ client_->NotifyInitializeDone(); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers( |
+ int input_count, |
+ const gfx::Size& input_coded_size, |
+ uint32 output_buffer_size) { |
+ DVLOG(2) << "OnRequireBitstreamBuffers(): input_count=" << input_count |
+ << ", input_coded_size=" << input_coded_size.ToString() |
+ << ", output_buffer_size=" << output_buffer_size; |
+ if (client_) { |
+ client_->RequireBitstreamBuffers( |
+ input_count, input_coded_size, output_buffer_size); |
+ } |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnNotifyInputDone(int32 frame_id) { |
+ DVLOG(3) << "OnNotifyInputDone(): frame_id=" << frame_id; |
+ if (!frame_map_.erase(frame_id)) { |
+ DLOG(ERROR) << "OnNotifyInputDone(): " |
+ "invalid frame_id=" << frame_id; |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
+ } |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady( |
+ int32 bitstream_buffer_id, |
+ uint32 payload_size, |
+ bool key_frame) { |
+ DVLOG(3) << "OnBitstreamBufferReady(): " |
+ "bitstream_buffer_id=" << bitstream_buffer_id |
+ << ", payload_size=" << payload_size |
+ << ", key_frame=" << key_frame; |
+ if (client_) |
+ client_->BitstreamBufferReady(bitstream_buffer_id, payload_size, key_frame); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnNotifyError(Error error) { |
+ DVLOG(2) << "OnNotifyError(): error=" << error; |
+ if (client_) { |
+ client_->NotifyError(error); |
+ client_ = NULL; |
+ } |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) { |
+ if (!channel_) { |
piman
2013/08/09 04:04:58
I think all the callers are checking channel_ anyw
sheu
2013/08/09 08:26:41
All things considered, I think we should remove th
|
+ DLOG(ERROR) << "Send(): no channel"; |
+ delete message; |
+ return; |
+ OnNotifyError(kPlatformFailureError); |
piman
2013/08/09 04:04:58
dead code (after return).
Do you need the explici
sheu
2013/08/09 08:26:41
Done.
|
+ } else if (!channel_->Send(message)) { |
+ DLOG(ERROR) << "Send(): sending failed: message->type()=" |
+ << message->type(); |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
piman
2013/08/09 04:04:58
same here, do you need the explicit return?
sheu
2013/08/09 08:26:41
Done.
|
+ } |
+} |
+ |
+} // namespace content |