Index: content/common/gpu/media/gpu_arc_video_service.cc |
diff --git a/content/common/gpu/media/gpu_arc_video_service.cc b/content/common/gpu/media/gpu_arc_video_service.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2a7ca2e4858350e895fc6708a98f2d044bd2fca5 |
--- /dev/null |
+++ b/content/common/gpu/media/gpu_arc_video_service.cc |
@@ -0,0 +1,141 @@ |
+// Copyright 2015 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_arc_video_service.h" |
+ |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "base/logging.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "content/common/gpu/gpu_messages.h" |
+#include "ipc/ipc_listener.h" |
+#include "ipc/ipc_message_macros.h" |
+#include "ipc/ipc_sync_channel.h" |
+ |
+namespace content { |
+ |
+// AcceleratorStub's all methods are running on arc thread. |
+// TODO(kcwu) implement ArcVideoAccelerator::Client. |
+class GpuArcVideoService::AcceleratorStub : public IPC::Listener, |
+ public IPC::Sender, |
+ public base::NonThreadSafe { |
+ public: |
+ // |owner| outlives AcceleratorStub. |
+ AcceleratorStub(GpuArcVideoService* owner) |
+ : owner_(owner), arc_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
+ |
+ ~AcceleratorStub() override { |
+ DCHECK(CalledOnValidThread()); |
+ channel_->Close(); |
+ } |
+ |
+ IPC::ChannelHandle CreateChannel( |
+ base::WaitableEvent* shutdown_event, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { |
+ IPC::ChannelHandle handle = |
+ IPC::Channel::GenerateVerifiedChannelID("arc-video"); |
+ channel_ = IPC::SyncChannel::Create(handle, IPC::Channel::MODE_SERVER, this, |
+ io_task_runner, false, shutdown_event); |
+#if defined(OS_POSIX) |
+ base::ScopedFD client_fd = channel_->TakeClientFileDescriptor(); |
+ DCHECK(client_fd.is_valid()); |
+ handle.socket = base::FileDescriptor(std::move(client_fd)); |
+#endif |
+ return handle; |
+ } |
+ |
+ // IPC::Sender implementation: |
+ bool Send(IPC::Message* msg) override { |
+ DCHECK(msg); |
+ return channel_->Send(msg); |
+ } |
+ |
+ // IPC::Listener implementation: |
+ void OnChannelError() override { |
+ DCHECK(CalledOnValidThread()); |
+ // RemoveClientOnArcThread will delete |this|. |
+ owner_->RemoveClientOnArcThread(this); |
+ } |
+ |
+ // IPC::Listener implementation: |
+ bool OnMessageReceived(const IPC::Message& msg) override { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ // TODO(kcwu) Add handlers here. |
+ return false; |
+ } |
+ |
+ private: |
+ GpuArcVideoService* owner_; |
+ scoped_ptr<IPC::SyncChannel> channel_; |
+ const scoped_refptr<base::SingleThreadTaskRunner> arc_task_runner_; |
+}; |
+ |
+GpuArcVideoService::GpuArcVideoService( |
+ base::WaitableEvent* shutdown_event, |
+ const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
+ : shutdown_event_(shutdown_event), |
+ io_task_runner_(io_task_runner), |
+ task_runner_(base::ThreadTaskRunnerHandle::Get()), |
+ arc_video_accelerator_thread_("ArcVideoAcceleratorThread") {} |
+ |
+GpuArcVideoService::~GpuArcVideoService() { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ if (!arc_video_accelerator_thread_.IsRunning()) |
+ return; |
+ |
+ base::WaitableEvent done(false, false); |
+ arc_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GpuArcVideoService::RemoveAllClientsOnArcThread, |
+ base::Unretained(this), base::Unretained(&done))); |
+ done.Wait(); |
+ |
+ arc_video_accelerator_thread_.Stop(); |
+} |
+ |
+void GpuArcVideoService::Initialize() { |
+ DCHECK(CalledOnValidThread()); |
+ |
+ arc_video_accelerator_thread_.Start(); |
+ arc_task_runner_ = arc_video_accelerator_thread_.task_runner(); |
+} |
+ |
+void GpuArcVideoService::CreateChannel(const CreateChannelCallback& callback) { |
+ DCHECK(CalledOnValidThread()); |
+ DCHECK(arc_task_runner_); |
+ |
+ // It's safe to Unretained |this| because the thread is owned by |this|. |
+ arc_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&GpuArcVideoService::CreateClientOnArcThread, |
+ base::Unretained(this), callback)); |
+} |
+ |
+void GpuArcVideoService::CreateClientOnArcThread( |
+ const CreateChannelCallback& callback) { |
+ DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
+ scoped_ptr<AcceleratorStub> stub(new AcceleratorStub(this)); |
+ |
+ IPC::ChannelHandle handle = |
+ stub->CreateChannel(shutdown_event_, io_task_runner_); |
+ accelerator_stubs_[stub.get()] = std::move(stub); |
+ |
+ task_runner_->PostTask(FROM_HERE, base::Bind(callback, handle)); |
+} |
+ |
+void GpuArcVideoService::RemoveClientOnArcThread(AcceleratorStub* stub) { |
+ DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
+ accelerator_stubs_.erase(stub); |
+} |
+ |
+void GpuArcVideoService::RemoveAllClientsOnArcThread( |
+ base::WaitableEvent* done) { |
+ DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
+ accelerator_stubs_.clear(); |
+ done->Signal(); |
+} |
+ |
+} // namespace content |