Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1562)

Unified Diff: content/common/gpu/client/gpu_video_encode_accelerator_host.cc

Issue 20632002: Add media::VideoEncodeAccelerator with WebRTC integration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@git-svn
Patch Set: fcfd089c More comments addressed. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..61bf7bc5de4308bf9a457b0fb20d5b5a142e349c
--- /dev/null
+++ b/content/common/gpu/client/gpu_video_encode_accelerator_host.cc
@@ -0,0 +1,210 @@
+// 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) << "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.
+ 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::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) << "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::OnNotifyEncodeDone(int32 frame_id) {
+ DVLOG(3) << "OnNotifyEncodeDone(): frame_id=" << frame_id;
+ if (!frame_map_.erase(frame_id)) {
+ DLOG(ERROR) << "OnNotifyEncodeDone(): "
+ "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_) {
+ DLOG(ERROR) << "Send(): no channel";
+ delete message;
+ return;
+ OnNotifyError(kPlatformFailureError);
+ } else if (!channel_->Send(message)) {
+ DLOG(ERROR) << "Send(): sending failed: message->type()="
+ << message->type();
+ OnNotifyError(kPlatformFailureError);
+ return;
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698