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_transport_sender_ipc.h"
7 #include "base/callback.h"
8 #include "base/id_map.h"
9 #include "chrome/common/cast_messages.h"
10 #include "chrome/renderer/media/cast_ipc_dispatcher.h"
11 #include "ipc/ipc_channel_proxy.h"
12 #include "media/cast/cast_sender.h"
14 CastTransportSenderIPC::ClientCallbacks::ClientCallbacks() {}
15 CastTransportSenderIPC::ClientCallbacks::~ClientCallbacks() {}
17 CastTransportSenderIPC::CastTransportSenderIPC(
18 const net::IPEndPoint
& local_end_point
,
19 const net::IPEndPoint
& remote_end_point
,
20 scoped_ptr
<base::DictionaryValue
> options
,
21 const media::cast::PacketReceiverCallback
& packet_callback
,
22 const media::cast::CastTransportStatusCallback
& status_cb
,
23 const media::cast::BulkRawEventsCallback
& raw_events_cb
)
24 : packet_callback_(packet_callback
) ,
25 status_callback_(status_cb
),
26 raw_events_callback_(raw_events_cb
) {
27 if (CastIPCDispatcher::Get()) {
28 channel_id_
= CastIPCDispatcher::Get()->AddSender(this);
30 Send(new CastHostMsg_New(channel_id_
,
36 CastTransportSenderIPC::~CastTransportSenderIPC() {
37 Send(new CastHostMsg_Delete(channel_id_
));
38 if (CastIPCDispatcher::Get()) {
39 CastIPCDispatcher::Get()->RemoveSender(channel_id_
);
43 void CastTransportSenderIPC::InitializeAudio(
44 const media::cast::CastTransportRtpConfig
& config
,
45 const media::cast::RtcpCastMessageCallback
& cast_message_cb
,
46 const media::cast::RtcpRttCallback
& rtt_cb
) {
47 clients_
[config
.ssrc
].cast_message_cb
= cast_message_cb
;
48 clients_
[config
.ssrc
].rtt_cb
= rtt_cb
;
49 Send(new CastHostMsg_InitializeAudio(channel_id_
, config
));
52 void CastTransportSenderIPC::InitializeVideo(
53 const media::cast::CastTransportRtpConfig
& config
,
54 const media::cast::RtcpCastMessageCallback
& cast_message_cb
,
55 const media::cast::RtcpRttCallback
& rtt_cb
) {
56 clients_
[config
.ssrc
].cast_message_cb
= cast_message_cb
;
57 clients_
[config
.ssrc
].rtt_cb
= rtt_cb
;
58 Send(new CastHostMsg_InitializeVideo(channel_id_
, config
));
61 void CastTransportSenderIPC::InsertFrame(uint32 ssrc
,
62 const media::cast::EncodedFrame
& frame
) {
63 Send(new CastHostMsg_InsertFrame(channel_id_
, ssrc
, frame
));
66 void CastTransportSenderIPC::SendSenderReport(
68 base::TimeTicks current_time
,
69 uint32 current_time_as_rtp_timestamp
) {
70 Send(new CastHostMsg_SendSenderReport(channel_id_
,
73 current_time_as_rtp_timestamp
));
76 void CastTransportSenderIPC::CancelSendingFrames(
77 uint32 ssrc
, const std::vector
<uint32
>& frame_ids
) {
78 Send(new CastHostMsg_CancelSendingFrames(channel_id_
,
83 void CastTransportSenderIPC::ResendFrameForKickstart(
84 uint32 ssrc
, uint32 frame_id
) {
85 Send(new CastHostMsg_ResendFrameForKickstart(channel_id_
,
90 void CastTransportSenderIPC::AddValidSsrc(uint32 ssrc
) {
91 Send(new CastHostMsg_AddValidSsrc(channel_id_
, ssrc
));
95 void CastTransportSenderIPC::SendRtcpFromRtpReceiver(
98 const media::cast::RtcpTimeData
& time_data
,
99 const media::cast::RtcpCastMessage
* cast_message
,
100 base::TimeDelta target_delay
,
101 const media::cast::ReceiverRtcpEventSubscriber::RtcpEvents
*
103 const media::cast::RtpReceiverStatistics
* rtp_receiver_statistics
) {
104 // To avoid copies, we put pointers to objects we don't really
105 // own into scoped pointers and then very carefully extract them again
106 // before the scoped pointers go out of scope.
107 media::cast::SendRtcpFromRtpReceiver_Params params
;
109 params
.sender_ssrc
= sender_ssrc
;
110 params
.time_data
= time_data
;
112 params
.cast_message
.reset(
113 const_cast<media::cast::RtcpCastMessage
*>(cast_message
));
115 params
.target_delay
= target_delay
;
117 params
.rtcp_events
.reset(
118 const_cast<media::cast::ReceiverRtcpEventSubscriber::RtcpEvents
*>(
121 if (rtp_receiver_statistics
) {
122 params
.rtp_receiver_statistics
.reset(
123 const_cast<media::cast::RtpReceiverStatistics
*>(
124 rtp_receiver_statistics
));
126 // Note, params contains scoped_ptr<>, but this still works because
127 // CastHostMsg_SendRtcpFromRtpReceiver doesn't take ownership, it
128 // serializes it and remember the serialized form instead.
129 Send(new CastHostMsg_SendRtcpFromRtpReceiver(channel_id_
, params
));
131 ignore_result(params
.rtp_receiver_statistics
.release());
132 ignore_result(params
.cast_message
.release());
133 ignore_result(params
.rtcp_events
.release());
137 void CastTransportSenderIPC::OnNotifyStatusChange(
138 media::cast::CastTransportStatus status
) {
139 status_callback_
.Run(status
);
142 void CastTransportSenderIPC::OnRawEvents(
143 const std::vector
<media::cast::PacketEvent
>& packet_events
,
144 const std::vector
<media::cast::FrameEvent
>& frame_events
) {
145 raw_events_callback_
.Run(packet_events
, frame_events
);
148 void CastTransportSenderIPC::OnRtt(uint32 ssrc
, base::TimeDelta rtt
) {
149 ClientMap::iterator it
= clients_
.find(ssrc
);
150 if (it
== clients_
.end()) {
151 LOG(ERROR
) << "Received RTT report from for unknown SSRC: " << ssrc
;
154 if (!it
->second
.rtt_cb
.is_null())
155 it
->second
.rtt_cb
.Run(rtt
);
158 void CastTransportSenderIPC::OnRtcpCastMessage(
160 const media::cast::RtcpCastMessage
& cast_message
) {
161 ClientMap::iterator it
= clients_
.find(ssrc
);
162 if (it
== clients_
.end()) {
163 LOG(ERROR
) << "Received cast message from for unknown SSRC: " << ssrc
;
166 if (it
->second
.cast_message_cb
.is_null())
168 it
->second
.cast_message_cb
.Run(cast_message
);
171 void CastTransportSenderIPC::OnReceivedPacket(
172 const media::cast::Packet
& packet
) {
173 if (!packet_callback_
.is_null()) {
174 // TODO(hubbe): Perhaps an non-ownership-transferring cb here?
175 scoped_ptr
<media::cast::Packet
> packet_copy(
176 new media::cast::Packet(packet
));
177 packet_callback_
.Run(packet_copy
.Pass());
179 DVLOG(1) << "CastIPCDispatcher::OnReceivedPacket no packet callback yet.";
183 void CastTransportSenderIPC::Send(IPC::Message
* message
) {
184 if (CastIPCDispatcher::Get()) {
185 CastIPCDispatcher::Get()->Send(message
);