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
)
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());
34 sender_
->Send(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());
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();
62 void CastIPCDispatcher::OnFilterAdded(IPC::Sender
* sender
) {
63 DCHECK(io_task_runner_
->BelongsToCurrentThread());
64 DCHECK(!global_instance_
);
65 global_instance_
= this;
69 void CastIPCDispatcher::OnFilterRemoved() {
70 DCHECK(io_task_runner_
->BelongsToCurrentThread());
71 DCHECK_EQ(this, global_instance_
);
72 global_instance_
= NULL
;
76 void CastIPCDispatcher::OnChannelClosing() {
77 DCHECK(io_task_runner_
->BelongsToCurrentThread());
78 DCHECK_EQ(this, global_instance_
);
81 void CastIPCDispatcher::OnNotifyStatusChange(
83 media::cast::CastTransportStatus status
) {
84 CastTransportSenderIPC
* sender
= id_map_
.Lookup(channel_id
);
86 sender
->OnNotifyStatusChange(status
);
89 << "CastIPCDispatcher::OnNotifystatusChange on non-existing channel.";
93 void CastIPCDispatcher::OnRawEvents(
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
);
99 sender
->OnRawEvents(packet_events
, frame_events
);
101 DVLOG(1) << "CastIPCDispatcher::OnRawEvents on non-existing channel.";
105 void CastIPCDispatcher::OnRtt(int32 channel_id
,
107 base::TimeDelta rtt
) {
108 CastTransportSenderIPC
* sender
= id_map_
.Lookup(channel_id
);
110 sender
->OnRtt(ssrc
, rtt
);
112 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
116 void CastIPCDispatcher::OnRtcpCastMessage(
119 const media::cast::RtcpCastMessage
& cast_message
) {
120 CastTransportSenderIPC
* sender
= id_map_
.Lookup(channel_id
);
122 sender
->OnRtcpCastMessage(ssrc
, cast_message
);
124 DVLOG(1) << "CastIPCDispatcher::OnRtt on non-existing channel.";
128 void CastIPCDispatcher::OnReceivedPacket(
130 const media::cast::Packet
& packet
) {
131 CastTransportSenderIPC
* sender
= id_map_
.Lookup(channel_id
);
133 sender
->OnReceivedPacket(packet
);
135 DVLOG(1) << "CastIPCDispatcher::OnReceievdPacket on non-existing channel.";