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..655966ebe0daf9e7c75fc2c1984313d91948fa1d |
--- /dev/null |
+++ b/content/common/gpu/client/gpu_video_encode_accelerator_host.cc |
@@ -0,0 +1,208 @@ |
+// 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_NotifyEncodeDone, |
+ OnNotifyEncodeDone) |
+ 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) << "GpuVideoEncodeAcceleratorHost::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; |
+ base::SharedMemoryHandle handle = |
+ channel_->ShareToGpuProcess(frame->shared_memory_handle()); |
+ if (!base::SharedMemory::IsHandleValid(handle)) { |
+ DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Encode(): failed to " |
+ "duplicate buffer handle for GPU process"; |
Ami GONE FROM CHROMIUM
2013/08/05 18:44:38
This is the message that someone would get if they
sheu
2013/08/06 06:16:36
Good point.
|
+ 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) { |
+ DCHECK(frame->data(i) == (frame->data(0) + frame_size)); |
+ 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. |
+ 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) << "GpuVideoEncodeAcceleratorHost::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::RequestEncodingParameterChange( |
+ int32 bitrate) { |
+ if (!channel_) |
+ return; |
+ Send(new AcceleratedVideoEncoderMsg_RequestEncodingParameterChange(route_id_, |
+ bitrate)); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Destroy() { |
+ if (channel_) |
+ Send(new GpuChannelMsg_DestroyVideoEncoder(route_id_)); |
+ delete this; |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone() { |
+ DVLOG(2) << "GpuVideoEncodeAcceleratorHost::OnNotifyInitializeDone()"; |
+ if (client_) |
+ client_->NotifyInitializeDone(); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnRequireBitstreamBuffers( |
+ int input_count, |
+ const gfx::Size& input_coded_size, |
+ uint32 output_buffer_size) { |
+ DVLOG(2) << "GpuVideoEncodeAcceleratorHost::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::OnNotifyEncodeDone(int32 frame_id) { |
+ DVLOG(3) << "GpuVideoEncodeAcceleratorHost::OnNotifyEncodeDone(): " |
+ "frame_id=" << frame_id; |
+ FrameMap::iterator iter = frame_map_.find(frame_id); |
+ if (iter == frame_map_.end()) { |
Ami GONE FROM CHROMIUM
2013/08/05 18:44:38
l.164, 165, and 171 can be collapsed into:
if (fra
sheu
2013/08/06 06:16:36
Done.
|
+ DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::OnNotifyEncodeDone(): " |
+ "invalid frame_id=" << frame_id; |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
+ } |
+ frame_map_.erase(frame_id); |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::OnBitstreamBufferReady( |
+ int32 bitstream_buffer_id, |
+ uint32 payload_size, |
+ bool key_frame) { |
+ DVLOG(3) << "GpuVideoEncodeAcceleratorHost::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) << "GpuVideoEncodeAcceleratorHost::OnNotifyError(): error=" << error; |
+ if (client_) { |
+ client_->NotifyError(error); |
+ client_ = NULL; |
+ } |
+} |
+ |
+void GpuVideoEncodeAcceleratorHost::Send(IPC::Message* message) { |
+ if (!channel_) { |
+ DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Send(): no channel"; |
+ delete message; |
+ return; |
+ OnNotifyError(kPlatformFailureError); |
+ } else if (!channel_->Send(message)) { |
+ DLOG(ERROR) << "GpuVideoEncodeAcceleratorHost::Send(): sending failed: " |
+ "message->type()=" << message->type(); |
+ OnNotifyError(kPlatformFailureError); |
+ return; |
+ } |
+} |
+ |
+} // namespace content |