OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "chrome/renderer/media/cast_transport_sender_ipc.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/id_map.h" |
| 9 #include "chrome/common/cast_messages.h" |
| 10 #include "ipc/ipc_channel_proxy.h" |
| 11 #include "media/cast/cast_sender.h" |
| 12 #include "media/cast/transport/cast_transport_sender.h" |
| 13 |
| 14 class CastIPCDispatcher; |
| 15 |
| 16 // This implementation of the CastTransportSender interface |
| 17 // communicates with the browser process over IPC and relays |
| 18 // all calls to/from the transport sender to the browser process. |
| 19 // The primary reason for this arrangement is to give the |
| 20 // renderer less direct control over the UDP sockets. |
| 21 class CastTransportSenderIPC |
| 22 : public media::cast::transport::CastTransportSender { |
| 23 public: |
| 24 CastTransportSenderIPC( |
| 25 const media::cast::transport::CastTransportConfig& config, |
| 26 const media::cast::transport::CastTransportStatusCallback& status_cb); |
| 27 |
| 28 virtual ~CastTransportSenderIPC(); |
| 29 |
| 30 // media::cast::transport::CastTransportSender implementation. |
| 31 virtual void SetPacketReceiver( |
| 32 const media::cast::transport::PacketReceiverCallback& packet_callback) |
| 33 OVERRIDE { |
| 34 packet_callback_ = packet_callback; |
| 35 } |
| 36 |
| 37 virtual void InsertCodedAudioFrame( |
| 38 const media::cast::transport::EncodedAudioFrame* audio_frame, |
| 39 const base::TimeTicks& recorded_time) OVERRIDE { |
| 40 Send(new CastHostMsg_InsertCodedAudioFrame(channel_id_, |
| 41 *audio_frame, |
| 42 recorded_time)); |
| 43 } |
| 44 |
| 45 virtual void InsertCodedVideoFrame( |
| 46 const media::cast::transport::EncodedVideoFrame* video_frame, |
| 47 const base::TimeTicks& capture_time) OVERRIDE { |
| 48 Send(new CastHostMsg_InsertCodedVideoFrame(channel_id_, |
| 49 *video_frame, |
| 50 capture_time)); |
| 51 } |
| 52 |
| 53 virtual void SendRtcpFromRtpSender( |
| 54 uint32 packet_type_flags, |
| 55 const media::cast::transport::RtcpSenderInfo& sender_info, |
| 56 const media::cast::transport::RtcpDlrrReportBlock& dlrr, |
| 57 const media::cast::transport::RtcpSenderLogMessage& sender_log, |
| 58 uint32 sending_ssrc, |
| 59 const std::string& c_name) OVERRIDE { |
| 60 struct media::cast::transport::SendRtcpFromRtpSenderData data; |
| 61 data.packet_type_flags = packet_type_flags; |
| 62 data.sending_ssrc = sending_ssrc; |
| 63 data.c_name = data.c_name; |
| 64 Send(new CastHostMsg_SendRtcpFromRtpSender( |
| 65 channel_id_, |
| 66 data, |
| 67 sender_info, |
| 68 dlrr, |
| 69 sender_log)); |
| 70 } |
| 71 |
| 72 virtual void ResendPackets( |
| 73 bool is_audio, |
| 74 const media::cast::MissingFramesAndPacketsMap& missing_packets) OVERRIDE { |
| 75 Send(new CastHostMsg_ResendPackets(channel_id_, |
| 76 is_audio, |
| 77 missing_packets)); |
| 78 } |
| 79 |
| 80 virtual void SubscribeAudioRtpStatsCallback( |
| 81 const media::cast::transport::CastTransportRtpStatistics& callback) |
| 82 OVERRIDE { |
| 83 audio_rtp_callback_ = callback; |
| 84 } |
| 85 |
| 86 virtual void SubscribeVideoRtpStatsCallback( |
| 87 const media::cast::transport::CastTransportRtpStatistics& callback) |
| 88 OVERRIDE { |
| 89 video_rtp_callback_ = callback; |
| 90 } |
| 91 |
| 92 private: |
| 93 friend class CastIPCDispatcher; |
| 94 void Send(IPC::Message *message); |
| 95 |
| 96 int32 channel_id_; |
| 97 media::cast::transport::PacketReceiverCallback packet_callback_; |
| 98 media::cast::transport::CastTransportStatusCallback status_callback_; |
| 99 media::cast::transport::CastTransportRtpStatistics audio_rtp_callback_; |
| 100 media::cast::transport::CastTransportRtpStatistics video_rtp_callback_; |
| 101 DISALLOW_COPY_AND_ASSIGN(CastTransportSenderIPC); |
| 102 }; |
| 103 |
| 104 // This dispatcher listens to incoming IPC messages and sends |
| 105 // the call to the correct CastTransportSenderIPC instance. |
| 106 class CastIPCDispatcher : public IPC::ChannelProxy::MessageFilter { |
| 107 public: |
| 108 CastIPCDispatcher( |
| 109 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 110 : channel_(NULL), |
| 111 io_message_loop_(io_message_loop) { |
| 112 DCHECK(!global_instance_); |
| 113 global_instance_ = this; |
| 114 } |
| 115 |
| 116 // IPC::ChannelProxy::MessageFilter implementation |
| 117 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 118 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 119 bool handled = true; |
| 120 IPC_BEGIN_MESSAGE_MAP(CastIPCDispatcher, message) |
| 121 IPC_MESSAGE_HANDLER(CastMsg_ReceivedPacket, OnReceivedPacket) |
| 122 IPC_MESSAGE_HANDLER(CastMsg_NotifyStatusChange, OnNotifyStatusChange) |
| 123 IPC_MESSAGE_HANDLER(CastMsg_RtpStatistics, OnRtpStatistics) |
| 124 IPC_MESSAGE_UNHANDLED(handled = false); |
| 125 IPC_END_MESSAGE_MAP(); |
| 126 return handled; |
| 127 } |
| 128 |
| 129 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE { |
| 130 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 131 channel_ = channel; |
| 132 } |
| 133 |
| 134 virtual void OnFilterRemoved() OVERRIDE { |
| 135 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 136 channel_ = NULL; |
| 137 } |
| 138 |
| 139 virtual void OnChannelClosing() OVERRIDE { |
| 140 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 141 channel_ = NULL; |
| 142 } |
| 143 |
| 144 protected: |
| 145 virtual ~CastIPCDispatcher() { |
| 146 global_instance_ = NULL; |
| 147 } |
| 148 |
| 149 private: |
| 150 void OnReceivedPacket(int32 channel_id, const media::cast::Packet& packet) { |
| 151 CastTransportSenderIPC* ptr = id_map_.Lookup(channel_id); |
| 152 if (ptr) { |
| 153 if (!ptr->packet_callback_.is_null()) { |
| 154 // TODO(hubbe): Perhaps an non-ownership-transferring cb here? |
| 155 scoped_ptr<media::cast::transport::Packet> packet_copy( |
| 156 new media::cast::transport::Packet(packet)); |
| 157 ptr->packet_callback_.Run(packet_copy.Pass()); |
| 158 } else { |
| 159 LOG(ERROR) << "CastIPCDispatcher::OnReceivedPacket " |
| 160 << "no packet callback yet."; |
| 161 } |
| 162 } else { |
| 163 LOG(ERROR) << "CastIPCDispatcher::OnReceivedPacket " |
| 164 << "on non-existing channel."; |
| 165 } |
| 166 } |
| 167 |
| 168 void OnNotifyStatusChange( |
| 169 int32 channel_id, |
| 170 media::cast::transport::CastTransportStatus status) { |
| 171 CastTransportSenderIPC* ptr = id_map_.Lookup(channel_id); |
| 172 if (ptr) { |
| 173 ptr->status_callback_.Run(status); |
| 174 } else { |
| 175 LOG(ERROR) |
| 176 << "CastIPCDispatcher::OnNotifystatusChange on non-existing channel."; |
| 177 } |
| 178 } |
| 179 void OnRtpStatistics( |
| 180 int32 channel_id, |
| 181 bool audio, |
| 182 const media::cast::transport::RtcpSenderInfo& sender_info, |
| 183 base::TimeTicks time_sent, |
| 184 uint32 rtp_timestamp) { |
| 185 CastTransportSenderIPC* ptr = id_map_.Lookup(channel_id); |
| 186 if (ptr) { |
| 187 const media::cast::transport::CastTransportRtpStatistics& callback = |
| 188 audio ? ptr->audio_rtp_callback_ : ptr->video_rtp_callback_; |
| 189 callback.Run(sender_info, time_sent, rtp_timestamp); |
| 190 } else { |
| 191 LOG(ERROR) |
| 192 << "CastIPCDispatcher::OnNotifystatusChange on non-existing channel."; |
| 193 } |
| 194 } |
| 195 |
| 196 friend class CastTransportSenderIPC; |
| 197 static CastIPCDispatcher* global_instance_; |
| 198 |
| 199 // IPC channel for Send(); must only be accesed on |io_message_loop_|. |
| 200 IPC::Channel* channel_; |
| 201 |
| 202 // Message loop on which IPC calls are driven. |
| 203 const scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
| 204 |
| 205 // A map of stream ids to delegates; must only be accessed on |
| 206 // |io_message_loop_|. |
| 207 IDMap<CastTransportSenderIPC> id_map_; |
| 208 DISALLOW_COPY_AND_ASSIGN(CastIPCDispatcher); |
| 209 }; |
| 210 |
| 211 CastIPCDispatcher* CastIPCDispatcher::global_instance_ = NULL; |
| 212 |
| 213 // These three methods can't be written inside the class becasue |
| 214 // they access CastIPCDispatcher, which isn't declared yet. |
| 215 CastTransportSenderIPC::CastTransportSenderIPC( |
| 216 const media::cast::transport::CastTransportConfig& config, |
| 217 const media::cast::transport::CastTransportStatusCallback& status_cb) |
| 218 : status_callback_(status_cb) { |
| 219 if (CastIPCDispatcher::global_instance_) { |
| 220 channel_id_ = CastIPCDispatcher::global_instance_->id_map_.Add(this); |
| 221 } |
| 222 Send(new CastHostMsg_New(channel_id_, config)); |
| 223 } |
| 224 |
| 225 CastTransportSenderIPC::~CastTransportSenderIPC() { |
| 226 Send(new CastHostMsg_Delete(channel_id_)); |
| 227 if (CastIPCDispatcher::global_instance_) { |
| 228 CastIPCDispatcher::global_instance_->id_map_.Remove(channel_id_); |
| 229 } |
| 230 } |
| 231 |
| 232 void CastTransportSenderIPC::Send(IPC::Message* message) { |
| 233 if (CastIPCDispatcher::global_instance_ && |
| 234 CastIPCDispatcher::global_instance_->channel_) { |
| 235 DCHECK(CastIPCDispatcher::global_instance_->io_message_loop_-> |
| 236 BelongsToCurrentThread()); |
| 237 CastIPCDispatcher::global_instance_->channel_->Send(message); |
| 238 } else { |
| 239 delete message; |
| 240 } |
| 241 } |
| 242 |
| 243 // Factory functions. |
| 244 media::cast::transport::CastTransportSender* NewCastTransportSenderIPC( |
| 245 const media::cast::transport::CastTransportConfig& config, |
| 246 const media::cast::transport::CastTransportStatusCallback& status_cb) { |
| 247 return new CastTransportSenderIPC(config, status_cb); |
| 248 } |
| 249 |
| 250 scoped_refptr<IPC::ChannelProxy::MessageFilter> NewCastIPCDispatcher( |
| 251 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) { |
| 252 return scoped_refptr<IPC::ChannelProxy::MessageFilter>( |
| 253 new CastIPCDispatcher(io_message_loop)); |
| 254 } |
OLD | NEW |