| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/media/cast_session_delegate.h" | 5 #include "chrome/renderer/media/cast_session_delegate.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "media/cast/net/cast_transport_sender.h" | 24 #include "media/cast/net/cast_transport_sender.h" |
| 25 | 25 |
| 26 using media::cast::AudioSenderConfig; | 26 using media::cast::AudioSenderConfig; |
| 27 using media::cast::CastEnvironment; | 27 using media::cast::CastEnvironment; |
| 28 using media::cast::CastSender; | 28 using media::cast::CastSender; |
| 29 using media::cast::VideoSenderConfig; | 29 using media::cast::VideoSenderConfig; |
| 30 | 30 |
| 31 static base::LazyInstance<CastThreads> g_cast_threads = | 31 static base::LazyInstance<CastThreads> g_cast_threads = |
| 32 LAZY_INSTANCE_INITIALIZER; | 32 LAZY_INSTANCE_INITIALIZER; |
| 33 | 33 |
| 34 CastSessionDelegate::CastSessionDelegate() | 34 CastSessionDelegateBase::CastSessionDelegateBase() |
| 35 : io_message_loop_proxy_( | 35 : io_message_loop_proxy_( |
| 36 content::RenderThread::Get()->GetIOMessageLoopProxy()), | 36 content::RenderThread::Get()->GetIOMessageLoopProxy()), |
| 37 weak_factory_(this) { | 37 weak_factory_(this) { |
| 38 DCHECK(io_message_loop_proxy_.get()); | 38 DCHECK(io_message_loop_proxy_.get()); |
| 39 } | 39 } |
| 40 | 40 |
| 41 CastSessionDelegateBase::~CastSessionDelegateBase() { |
| 42 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 43 } |
| 44 |
| 45 void CastSessionDelegateBase::StartUDP( |
| 46 const net::IPEndPoint& local_endpoint, |
| 47 const net::IPEndPoint& remote_endpoint, |
| 48 scoped_ptr<base::DictionaryValue> options) { |
| 49 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 50 |
| 51 // CastSender uses the renderer's IO thread as the main thread. This reduces |
| 52 // thread hopping for incoming video frames and outgoing network packets. |
| 53 // TODO(hubbe): Create cast environment in ctor instead. |
| 54 cast_environment_ = new CastEnvironment( |
| 55 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), |
| 56 base::MessageLoopProxy::current(), |
| 57 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), |
| 58 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); |
| 59 |
| 60 // Rationale for using unretained: The callback cannot be called after the |
| 61 // destruction of CastTransportSenderIPC, and they both share the same thread. |
| 62 cast_transport_.reset(new CastTransportSenderIPC( |
| 63 local_endpoint, |
| 64 remote_endpoint, |
| 65 options.Pass(), |
| 66 base::Bind(&CastSessionDelegateBase::ReceivePacket, |
| 67 base::Unretained(this)), |
| 68 base::Bind(&CastSessionDelegateBase::StatusNotificationCB, |
| 69 base::Unretained(this)), |
| 70 base::Bind(&CastSessionDelegateBase::LogRawEvents, |
| 71 base::Unretained(this)))); |
| 72 } |
| 73 |
| 74 void CastSessionDelegateBase::StatusNotificationCB( |
| 75 media::cast::CastTransportStatus unused_status) { |
| 76 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 77 // TODO(hubbe): Call javascript UDPTransport error function. |
| 78 } |
| 79 |
| 80 CastSessionDelegate::CastSessionDelegate() |
| 81 : weak_factory_(this) { |
| 82 DCHECK(io_message_loop_proxy_.get()); |
| 83 } |
| 84 |
| 41 CastSessionDelegate::~CastSessionDelegate() { | 85 CastSessionDelegate::~CastSessionDelegate() { |
| 42 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 86 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 43 } | 87 } |
| 44 | 88 |
| 45 void CastSessionDelegate::StartAudio( | 89 void CastSessionDelegate::StartAudio( |
| 46 const AudioSenderConfig& config, | 90 const AudioSenderConfig& config, |
| 47 const AudioFrameInputAvailableCallback& callback, | 91 const AudioFrameInputAvailableCallback& callback, |
| 48 const ErrorCallback& error_callback) { | 92 const ErrorCallback& error_callback) { |
| 49 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 93 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 50 | 94 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 77 video_frame_input_available_callback_ = callback; | 121 video_frame_input_available_callback_ = callback; |
| 78 | 122 |
| 79 cast_sender_->InitializeVideo( | 123 cast_sender_->InitializeVideo( |
| 80 config, | 124 config, |
| 81 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, | 125 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, |
| 82 weak_factory_.GetWeakPtr(), false, error_callback), | 126 weak_factory_.GetWeakPtr(), false, error_callback), |
| 83 create_vea_cb, | 127 create_vea_cb, |
| 84 create_video_encode_mem_cb); | 128 create_video_encode_mem_cb); |
| 85 } | 129 } |
| 86 | 130 |
| 87 void CastSessionDelegate::StartUDP(const net::IPEndPoint& remote_endpoint, | 131 |
| 88 scoped_ptr<base::DictionaryValue> options) { | 132 void CastSessionDelegate::StartUDP( |
| 133 const net::IPEndPoint& local_endpoint, |
| 134 const net::IPEndPoint& remote_endpoint, |
| 135 scoped_ptr<base::DictionaryValue> options) { |
| 89 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 136 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 90 | 137 CastSessionDelegateBase::StartUDP(local_endpoint, |
| 91 // CastSender uses the renderer's IO thread as the main thread. This reduces | 138 remote_endpoint, |
| 92 // thread hopping for incoming video frames and outgoing network packets. | 139 options.Pass()); |
| 93 cast_environment_ = new CastEnvironment( | |
| 94 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), | |
| 95 base::MessageLoopProxy::current(), | |
| 96 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), | |
| 97 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); | |
| 98 | |
| 99 event_subscribers_.reset( | 140 event_subscribers_.reset( |
| 100 new media::cast::RawEventSubscriberBundle(cast_environment_)); | 141 new media::cast::RawEventSubscriberBundle(cast_environment_)); |
| 101 | 142 |
| 102 // Rationale for using unretained: The callback cannot be called after the | |
| 103 // destruction of CastTransportSenderIPC, and they both share the same thread. | |
| 104 cast_transport_.reset(new CastTransportSenderIPC( | |
| 105 net::IPEndPoint(), | |
| 106 remote_endpoint, | |
| 107 options.Pass(), | |
| 108 media::cast::PacketReceiverCallback(), | |
| 109 base::Bind(&CastSessionDelegate::StatusNotificationCB, | |
| 110 base::Unretained(this)), | |
| 111 base::Bind(&CastSessionDelegate::LogRawEvents, base::Unretained(this)))); | |
| 112 | |
| 113 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); | 143 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); |
| 114 } | 144 } |
| 115 | 145 |
| 116 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { | 146 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { |
| 117 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 147 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 118 if (!event_subscribers_.get()) | 148 if (!event_subscribers_.get()) |
| 119 return; | 149 return; |
| 120 | 150 |
| 121 if (enable) | 151 if (enable) |
| 122 event_subscribers_->AddEventSubscribers(is_audio); | 152 event_subscribers_->AddEventSubscribers(is_audio); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); | 225 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); |
| 196 return; | 226 return; |
| 197 } | 227 } |
| 198 | 228 |
| 199 scoped_ptr<base::DictionaryValue> stats = subscriber->GetStats(); | 229 scoped_ptr<base::DictionaryValue> stats = subscriber->GetStats(); |
| 200 subscriber->Reset(); | 230 subscriber->Reset(); |
| 201 | 231 |
| 202 callback.Run(stats.Pass()); | 232 callback.Run(stats.Pass()); |
| 203 } | 233 } |
| 204 | 234 |
| 205 void CastSessionDelegate::StatusNotificationCB( | |
| 206 media::cast::CastTransportStatus unused_status) { | |
| 207 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 208 // TODO(hubbe): Call javascript UDPTransport error function. | |
| 209 } | |
| 210 | |
| 211 void CastSessionDelegate::OnOperationalStatusChange( | 235 void CastSessionDelegate::OnOperationalStatusChange( |
| 212 bool is_for_audio, | 236 bool is_for_audio, |
| 213 const ErrorCallback& error_callback, | 237 const ErrorCallback& error_callback, |
| 214 media::cast::OperationalStatus status) { | 238 media::cast::OperationalStatus status) { |
| 215 DCHECK(cast_sender_); | 239 DCHECK(cast_sender_); |
| 216 | 240 |
| 217 switch (status) { | 241 switch (status) { |
| 218 case media::cast::STATUS_UNINITIALIZED: | 242 case media::cast::STATUS_UNINITIALIZED: |
| 219 case media::cast::STATUS_CODEC_REINIT_PENDING: | 243 case media::cast::STATUS_CODEC_REINIT_PENDING: |
| 220 // Not an error. | 244 // Not an error. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 250 error_callback.Run(base::StringPrintf("%s codec initialization failed.", | 274 error_callback.Run(base::StringPrintf("%s codec initialization failed.", |
| 251 is_for_audio ? "Audio" : "Video")); | 275 is_for_audio ? "Audio" : "Video")); |
| 252 break; | 276 break; |
| 253 case media::cast::STATUS_CODEC_RUNTIME_ERROR: | 277 case media::cast::STATUS_CODEC_RUNTIME_ERROR: |
| 254 error_callback.Run(base::StringPrintf("%s codec runtime error.", | 278 error_callback.Run(base::StringPrintf("%s codec runtime error.", |
| 255 is_for_audio ? "Audio" : "Video")); | 279 is_for_audio ? "Audio" : "Video")); |
| 256 break; | 280 break; |
| 257 } | 281 } |
| 258 } | 282 } |
| 259 | 283 |
| 284 void CastSessionDelegate::ReceivePacket( |
| 285 scoped_ptr<media::cast::Packet> packet) { |
| 286 // Do nothing (frees packet) |
| 287 } |
| 288 |
| 260 void CastSessionDelegate::LogRawEvents( | 289 void CastSessionDelegate::LogRawEvents( |
| 261 const std::vector<media::cast::PacketEvent>& packet_events, | 290 const std::vector<media::cast::PacketEvent>& packet_events, |
| 262 const std::vector<media::cast::FrameEvent>& frame_events) { | 291 const std::vector<media::cast::FrameEvent>& frame_events) { |
| 263 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 292 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 264 | 293 |
| 265 for (std::vector<media::cast::PacketEvent>::const_iterator it = | 294 for (std::vector<media::cast::PacketEvent>::const_iterator it = |
| 266 packet_events.begin(); | 295 packet_events.begin(); |
| 267 it != packet_events.end(); | 296 it != packet_events.end(); |
| 268 ++it) { | 297 ++it) { |
| 269 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, | 298 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 290 } else { | 319 } else { |
| 291 cast_environment_->Logging()->InsertFrameEvent( | 320 cast_environment_->Logging()->InsertFrameEvent( |
| 292 it->timestamp, | 321 it->timestamp, |
| 293 it->type, | 322 it->type, |
| 294 it->media_type, | 323 it->media_type, |
| 295 it->rtp_timestamp, | 324 it->rtp_timestamp, |
| 296 it->frame_id); | 325 it->frame_id); |
| 297 } | 326 } |
| 298 } | 327 } |
| 299 } | 328 } |
| OLD | NEW |