OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/gpu/media/gpu_arc_video_service.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "content/common/gpu/gpu_messages.h" |
| 14 #include "ipc/ipc_listener.h" |
| 15 #include "ipc/ipc_message_macros.h" |
| 16 #include "ipc/ipc_sync_channel.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 // AcceleratorStub's all methods are running on arc thread. |
| 21 // TODO(kcwu) implement ArcVideoAccelerator::Client. |
| 22 class GpuArcVideoService::AcceleratorStub : public IPC::Listener, |
| 23 public IPC::Sender, |
| 24 public base::NonThreadSafe { |
| 25 public: |
| 26 // |owner| outlives AcceleratorStub. |
| 27 AcceleratorStub(GpuArcVideoService* owner) |
| 28 : owner_(owner), arc_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| 29 |
| 30 ~AcceleratorStub() override { |
| 31 DCHECK(CalledOnValidThread()); |
| 32 channel_->Close(); |
| 33 } |
| 34 |
| 35 IPC::ChannelHandle CreateChannel( |
| 36 base::WaitableEvent* shutdown_event, |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { |
| 38 IPC::ChannelHandle handle = |
| 39 IPC::Channel::GenerateVerifiedChannelID("arc-video"); |
| 40 channel_ = IPC::SyncChannel::Create(handle, IPC::Channel::MODE_SERVER, this, |
| 41 io_task_runner, false, shutdown_event); |
| 42 #if defined(OS_POSIX) |
| 43 base::ScopedFD client_fd = channel_->TakeClientFileDescriptor(); |
| 44 DCHECK(client_fd.is_valid()); |
| 45 handle.socket = base::FileDescriptor(std::move(client_fd)); |
| 46 #endif |
| 47 return handle; |
| 48 } |
| 49 |
| 50 // IPC::Sender implementation: |
| 51 bool Send(IPC::Message* msg) override { |
| 52 DCHECK(msg); |
| 53 return channel_->Send(msg); |
| 54 } |
| 55 |
| 56 // IPC::Listener implementation: |
| 57 void OnChannelError() override { |
| 58 DCHECK(CalledOnValidThread()); |
| 59 // RemoveClientOnArcThread will delete |this|. |
| 60 owner_->RemoveClientOnArcThread(this); |
| 61 } |
| 62 |
| 63 // IPC::Listener implementation: |
| 64 bool OnMessageReceived(const IPC::Message& msg) override { |
| 65 DCHECK(CalledOnValidThread()); |
| 66 |
| 67 // TODO(kcwu) Add handlers here. |
| 68 return false; |
| 69 } |
| 70 |
| 71 private: |
| 72 GpuArcVideoService* owner_; |
| 73 scoped_ptr<IPC::SyncChannel> channel_; |
| 74 const scoped_refptr<base::SingleThreadTaskRunner> arc_task_runner_; |
| 75 }; |
| 76 |
| 77 GpuArcVideoService::GpuArcVideoService( |
| 78 base::WaitableEvent* shutdown_event, |
| 79 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 80 : shutdown_event_(shutdown_event), |
| 81 io_task_runner_(io_task_runner), |
| 82 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 83 arc_video_accelerator_thread_("ArcVideoAcceleratorThread") {} |
| 84 |
| 85 GpuArcVideoService::~GpuArcVideoService() { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 |
| 88 if (!arc_video_accelerator_thread_.IsRunning()) |
| 89 return; |
| 90 |
| 91 base::WaitableEvent done(false, false); |
| 92 arc_task_runner_->PostTask( |
| 93 FROM_HERE, base::Bind(&GpuArcVideoService::RemoveAllClientsOnArcThread, |
| 94 base::Unretained(this), base::Unretained(&done))); |
| 95 done.Wait(); |
| 96 |
| 97 arc_video_accelerator_thread_.Stop(); |
| 98 } |
| 99 |
| 100 void GpuArcVideoService::Initialize() { |
| 101 DCHECK(CalledOnValidThread()); |
| 102 |
| 103 arc_video_accelerator_thread_.Start(); |
| 104 arc_task_runner_ = arc_video_accelerator_thread_.task_runner(); |
| 105 } |
| 106 |
| 107 void GpuArcVideoService::CreateChannel(const CreateChannelCallback& callback) { |
| 108 DCHECK(CalledOnValidThread()); |
| 109 DCHECK(arc_task_runner_); |
| 110 |
| 111 // It's safe to Unretained |this| because the thread is owned by |this|. |
| 112 arc_task_runner_->PostTask( |
| 113 FROM_HERE, base::Bind(&GpuArcVideoService::CreateClientOnArcThread, |
| 114 base::Unretained(this), callback)); |
| 115 } |
| 116 |
| 117 void GpuArcVideoService::CreateClientOnArcThread( |
| 118 const CreateChannelCallback& callback) { |
| 119 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 120 scoped_ptr<AcceleratorStub> stub(new AcceleratorStub(this)); |
| 121 |
| 122 IPC::ChannelHandle handle = |
| 123 stub->CreateChannel(shutdown_event_, io_task_runner_); |
| 124 accelerator_stubs_[stub.get()] = std::move(stub); |
| 125 |
| 126 task_runner_->PostTask(FROM_HERE, base::Bind(callback, handle)); |
| 127 } |
| 128 |
| 129 void GpuArcVideoService::RemoveClientOnArcThread(AcceleratorStub* stub) { |
| 130 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 131 accelerator_stubs_.erase(stub); |
| 132 } |
| 133 |
| 134 void GpuArcVideoService::RemoveAllClientsOnArcThread( |
| 135 base::WaitableEvent* done) { |
| 136 DCHECK(arc_task_runner_->BelongsToCurrentThread()); |
| 137 accelerator_stubs_.clear(); |
| 138 done->Signal(); |
| 139 } |
| 140 |
| 141 } // namespace content |
OLD | NEW |