Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / renderer / media / cast_ipc_dispatcher.cc
blob921a27d4409ca511b149d4759090fb59af1e8927
1 // Copyright 2014 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.
5 #include "chrome/renderer/media/cast_ipc_dispatcher.h"
7 #include "base/single_thread_task_runner.h"
8 #include "chrome/common/cast_messages.h"
9 #include "chrome/renderer/media/cast_transport_sender_ipc.h"
10 #include "ipc/ipc_message_macros.h"
12 CastIPCDispatcher* CastIPCDispatcher::global_instance_ = NULL;
14 CastIPCDispatcher::CastIPCDispatcher(
15 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner)
16 : sender_(NULL),
17 io_task_runner_(io_task_runner) {
18 DCHECK(io_task_runner_.get());
19 DCHECK(!global_instance_);
22 CastIPCDispatcher::~CastIPCDispatcher() {
23 DCHECK(io_task_runner_->BelongsToCurrentThread());
24 DCHECK(!global_instance_);
27 CastIPCDispatcher* CastIPCDispatcher::Get() {
28 return global_instance_;
31 void CastIPCDispatcher::Send(IPC::Message* message) {
32 DCHECK(io_task_runner_->BelongsToCurrentThread());
33 if (sender_) {
34 sender_->Send(message);
35 } else {
36 delete message;
40 int32 CastIPCDispatcher::AddSender(CastTransportSenderIPC* sender) {
41 return id_map_.Add(sender);
44 void CastIPCDispatcher::RemoveSender(int32 channel_id) {
45 return id_map_.Remove(channel_id);
48 bool CastIPCDispatcher::OnMessageReceived(const IPC::Message& message) {
49 DCHECK(io_task_runner_->BelongsToCurrentThread());
50 bool handled = true;
51 IPC_BEGIN_MESSAGE_MAP(CastIPCDispatcher, message)
52 IPC_MESSAGE_HANDLER(CastMsg_NotifyStatusChange, OnNotifyStatusChange)
53 IPC_MESSAGE_HANDLER(CastMsg_RawEvents, OnRawEvents)
54 IPC_MESSAGE_HANDLER(CastMsg_Rtt, OnRtt)
55 IPC_MESSAGE_HANDLER(CastMsg_RtcpCastMessage, OnRtcpCastMessage)
56 IPC_MESSAGE_HANDLER(CastMsg_ReceivedPacket, OnReceivedPacket)
57 IPC_MESSAGE_UNHANDLED(handled = false);
58 IPC_END_MESSAGE_MAP();
59 return handled;
62 void CastIPCDispatcher::OnFilterAdded(IPC::Sender* sender) {
63 DCHECK(io_task_runner_->BelongsToCurrentThread());
64 DCHECK(!global_instance_);
65 global_instance_ = this;
66 sender_ = sender;
69 void CastIPCDispatcher::OnFilterRemoved() {
70 DCHECK(io_task_runner_->BelongsToCurrentThread());
71 DCHECK_EQ(this, global_instance_);
72 global_instance_ = NULL;
73 sender_ = NULL;
76 void CastIPCDispatcher::OnChannelClosing() {
77 DCHECK(io_task_runner_->BelongsToCurrentThread());
78 DCHECK_EQ(this, global_instance_);
81 void CastIPCDispatcher::OnNotifyStatusChange(
82 int32 channel_id,
83 media::cast::CastTransportStatus status) {
84 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
85 if (sender) {
86 sender->OnNotifyStatusChange(status);
87 } else {
88 DVLOG(1)
89 << "CastIPCDispatcher::OnNotifystatusChange on non-existing channel.";
93 void CastIPCDispatcher::OnRawEvents(
94 int32 channel_id,
95 const std::vector<media::cast::PacketEvent>& packet_events,
96 const std::vector<media::cast::FrameEvent>& frame_events) {
97 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
98 if (sender) {
99 sender->OnRawEvents(packet_events, frame_events);
100 } else {
101 DVLOG(1) << "CastIPCDispatcher::OnRawEvents on non-existing channel.";
105 void CastIPCDispatcher::OnRtt(int32 channel_id,
106 uint32 ssrc,
107 base::TimeDelta rtt) {
108 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
109 if (sender) {
110 sender->OnRtt(ssrc, rtt);
111 } else {
112 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
116 void CastIPCDispatcher::OnRtcpCastMessage(
117 int32 channel_id,
118 uint32 ssrc,
119 const media::cast::RtcpCastMessage& cast_message) {
120 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
121 if (sender) {
122 sender->OnRtcpCastMessage(ssrc, cast_message);
123 } else {
124 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
128 void CastIPCDispatcher::OnReceivedPacket(
129 int32 channel_id,
130 const media::cast::Packet& packet) {
131 CastTransportSenderIPC* sender = id_map_.Lookup(channel_id);
132 if (sender) {
133 sender->OnReceivedPacket(packet);
134 } else {
135 DVLOG(1) << "CastIPCDispatcher::OnReceievdPacket on non-existing channel.";