| 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/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "chrome/common/chrome_version_info.h" | 10 #include "chrome/common/chrome_version_info.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "media/cast/net/cast_transport_sender.h" | 22 #include "media/cast/net/cast_transport_sender.h" |
| 23 | 23 |
| 24 using media::cast::AudioSenderConfig; | 24 using media::cast::AudioSenderConfig; |
| 25 using media::cast::CastEnvironment; | 25 using media::cast::CastEnvironment; |
| 26 using media::cast::CastSender; | 26 using media::cast::CastSender; |
| 27 using media::cast::VideoSenderConfig; | 27 using media::cast::VideoSenderConfig; |
| 28 | 28 |
| 29 static base::LazyInstance<CastThreads> g_cast_threads = | 29 static base::LazyInstance<CastThreads> g_cast_threads = |
| 30 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| 31 | 31 |
| 32 CastSessionDelegate::CastSessionDelegate() | 32 CastSessionDelegateBase::CastSessionDelegateBase() |
| 33 : io_message_loop_proxy_( | 33 : io_message_loop_proxy_( |
| 34 content::RenderThread::Get()->GetIOMessageLoopProxy()), | 34 content::RenderThread::Get()->GetIOMessageLoopProxy()), |
| 35 weak_factory_(this) { | 35 weak_factory_(this) { |
| 36 DCHECK(io_message_loop_proxy_.get()); | 36 DCHECK(io_message_loop_proxy_.get()); |
| 37 } | 37 } |
| 38 | 38 |
| 39 CastSessionDelegateBase::~CastSessionDelegateBase() { |
| 40 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 41 } |
| 42 |
| 43 void CastSessionDelegateBase::StartUDP( |
| 44 const net::IPEndPoint& local_endpoint, |
| 45 const net::IPEndPoint& remote_endpoint, |
| 46 scoped_ptr<base::DictionaryValue> options) { |
| 47 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 48 |
| 49 // CastSender uses the renderer's IO thread as the main thread. This reduces |
| 50 // thread hopping for incoming video frames and outgoing network packets. |
| 51 cast_environment_ = new CastEnvironment( |
| 52 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), |
| 53 base::MessageLoopProxy::current(), |
| 54 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), |
| 55 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); |
| 56 |
| 57 // Rationale for using unretained: The callback cannot be called after the |
| 58 // destruction of CastTransportSenderIPC, and they both share the same thread. |
| 59 cast_transport_.reset(new CastTransportSenderIPC( |
| 60 local_endpoint, |
| 61 remote_endpoint, |
| 62 options.Pass(), |
| 63 media::cast::PacketReceiverCallback(), |
| 64 base::Bind(&CastSessionDelegateBase::StatusNotificationCB, |
| 65 base::Unretained(this)), |
| 66 base::Bind(&CastSessionDelegateBase::LogRawEvents, |
| 67 base::Unretained(this)))); |
| 68 } |
| 69 |
| 70 void CastSessionDelegateBase::StatusNotificationCB( |
| 71 media::cast::CastTransportStatus unused_status) { |
| 72 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 73 // TODO(hubbe): Call javascript UDPTransport error function. |
| 74 } |
| 75 |
| 76 CastSessionDelegate::CastSessionDelegate() |
| 77 : weak_factory_(this) { |
| 78 DCHECK(io_message_loop_proxy_.get()); |
| 79 } |
| 80 |
| 39 CastSessionDelegate::~CastSessionDelegate() { | 81 CastSessionDelegate::~CastSessionDelegate() { |
| 40 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 82 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 41 } | 83 } |
| 42 | 84 |
| 43 void CastSessionDelegate::StartAudio( | 85 void CastSessionDelegate::StartAudio( |
| 44 const AudioSenderConfig& config, | 86 const AudioSenderConfig& config, |
| 45 const AudioFrameInputAvailableCallback& callback, | 87 const AudioFrameInputAvailableCallback& callback, |
| 46 const ErrorCallback& error_callback) { | 88 const ErrorCallback& error_callback) { |
| 47 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 89 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 48 | 90 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 video_frame_input_available_callback_ = callback; | 117 video_frame_input_available_callback_ = callback; |
| 76 | 118 |
| 77 cast_sender_->InitializeVideo( | 119 cast_sender_->InitializeVideo( |
| 78 config, | 120 config, |
| 79 base::Bind(&CastSessionDelegate::InitializationResultCB, | 121 base::Bind(&CastSessionDelegate::InitializationResultCB, |
| 80 weak_factory_.GetWeakPtr(), error_callback), | 122 weak_factory_.GetWeakPtr(), error_callback), |
| 81 create_vea_cb, | 123 create_vea_cb, |
| 82 create_video_encode_mem_cb); | 124 create_video_encode_mem_cb); |
| 83 } | 125 } |
| 84 | 126 |
| 85 void CastSessionDelegate::StartUDP(const net::IPEndPoint& remote_endpoint, | 127 |
| 86 scoped_ptr<base::DictionaryValue> options) { | 128 void CastSessionDelegate::StartUDP( |
| 129 const net::IPEndPoint& local_endpoint, |
| 130 const net::IPEndPoint& remote_endpoint, |
| 131 scoped_ptr<base::DictionaryValue> options) { |
| 87 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 132 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 88 | 133 CastSessionDelegateBase::StartUDP(local_endpoint, |
| 89 // CastSender uses the renderer's IO thread as the main thread. This reduces | 134 remote_endpoint, |
| 90 // thread hopping for incoming video frames and outgoing network packets. | 135 options.Pass()); |
| 91 cast_environment_ = new CastEnvironment( | |
| 92 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), | |
| 93 base::MessageLoopProxy::current(), | |
| 94 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), | |
| 95 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); | |
| 96 | |
| 97 event_subscribers_.reset( | 136 event_subscribers_.reset( |
| 98 new media::cast::RawEventSubscriberBundle(cast_environment_)); | 137 new media::cast::RawEventSubscriberBundle(cast_environment_)); |
| 99 | 138 |
| 100 // Rationale for using unretained: The callback cannot be called after the | |
| 101 // destruction of CastTransportSenderIPC, and they both share the same thread. | |
| 102 cast_transport_.reset(new CastTransportSenderIPC( | |
| 103 net::IPEndPoint(), | |
| 104 remote_endpoint, | |
| 105 options.Pass(), | |
| 106 media::cast::PacketReceiverCallback(), | |
| 107 base::Bind(&CastSessionDelegate::StatusNotificationCB, | |
| 108 base::Unretained(this)), | |
| 109 base::Bind(&CastSessionDelegate::LogRawEvents, base::Unretained(this)))); | |
| 110 | |
| 111 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); | 139 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); |
| 112 } | 140 } |
| 113 | 141 |
| 114 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { | 142 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { |
| 115 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 143 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 116 if (!event_subscribers_.get()) | 144 if (!event_subscribers_.get()) |
| 117 return; | 145 return; |
| 118 | 146 |
| 119 if (enable) | 147 if (enable) |
| 120 event_subscribers_->AddEventSubscribers(is_audio); | 148 event_subscribers_->AddEventSubscribers(is_audio); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); | 221 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); |
| 194 return; | 222 return; |
| 195 } | 223 } |
| 196 | 224 |
| 197 scoped_ptr<base::DictionaryValue> stats = subscriber->GetStats(); | 225 scoped_ptr<base::DictionaryValue> stats = subscriber->GetStats(); |
| 198 subscriber->Reset(); | 226 subscriber->Reset(); |
| 199 | 227 |
| 200 callback.Run(stats.Pass()); | 228 callback.Run(stats.Pass()); |
| 201 } | 229 } |
| 202 | 230 |
| 203 void CastSessionDelegate::StatusNotificationCB( | |
| 204 media::cast::CastTransportStatus unused_status) { | |
| 205 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | |
| 206 // TODO(hubbe): Call javascript UDPTransport error function. | |
| 207 } | |
| 208 | |
| 209 void CastSessionDelegate::InitializationResultCB( | 231 void CastSessionDelegate::InitializationResultCB( |
| 210 const ErrorCallback& error_callback, | 232 const ErrorCallback& error_callback, |
| 211 media::cast::CastInitializationStatus result) const { | 233 media::cast::CastInitializationStatus result) const { |
| 212 DCHECK(cast_sender_); | 234 DCHECK(cast_sender_); |
| 213 | 235 |
| 214 switch (result) { | 236 switch (result) { |
| 215 case media::cast::STATUS_AUDIO_INITIALIZED: | 237 case media::cast::STATUS_AUDIO_INITIALIZED: |
| 216 audio_frame_input_available_callback_.Run( | 238 audio_frame_input_available_callback_.Run( |
| 217 cast_sender_->audio_frame_input()); | 239 cast_sender_->audio_frame_input()); |
| 218 break; | 240 break; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 } else { | 303 } else { |
| 282 cast_environment_->Logging()->InsertFrameEvent( | 304 cast_environment_->Logging()->InsertFrameEvent( |
| 283 it->timestamp, | 305 it->timestamp, |
| 284 it->type, | 306 it->type, |
| 285 it->media_type, | 307 it->media_type, |
| 286 it->rtp_timestamp, | 308 it->rtp_timestamp, |
| 287 it->frame_id); | 309 it->frame_id); |
| 288 } | 310 } |
| 289 } | 311 } |
| 290 } | 312 } |
| OLD | NEW |