| 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/single_thread_task_runner.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 12 #include "chrome/common/chrome_version_info.h" | 13 #include "chrome/common/chrome_version_info.h" |
| 13 #include "chrome/renderer/media/cast_threads.h" | 14 #include "chrome/renderer/media/cast_threads.h" |
| 14 #include "chrome/renderer/media/cast_transport_sender_ipc.h" | 15 #include "chrome/renderer/media/cast_transport_sender_ipc.h" |
| 15 #include "content/public/renderer/render_thread.h" | 16 #include "content/public/renderer/render_thread.h" |
| 16 #include "media/cast/cast_config.h" | 17 #include "media/cast/cast_config.h" |
| 17 #include "media/cast/cast_environment.h" | 18 #include "media/cast/cast_environment.h" |
| 18 #include "media/cast/cast_sender.h" | 19 #include "media/cast/cast_sender.h" |
| 19 #include "media/cast/logging/log_serializer.h" | 20 #include "media/cast/logging/log_serializer.h" |
| 20 #include "media/cast/logging/logging_defines.h" | 21 #include "media/cast/logging/logging_defines.h" |
| 21 #include "media/cast/logging/proto/raw_events.pb.h" | 22 #include "media/cast/logging/proto/raw_events.pb.h" |
| 22 #include "media/cast/logging/raw_event_subscriber_bundle.h" | 23 #include "media/cast/logging/raw_event_subscriber_bundle.h" |
| 23 #include "media/cast/net/cast_transport_config.h" | 24 #include "media/cast/net/cast_transport_config.h" |
| 24 #include "media/cast/net/cast_transport_sender.h" | 25 #include "media/cast/net/cast_transport_sender.h" |
| 25 | 26 |
| 26 using media::cast::AudioSenderConfig; | 27 using media::cast::AudioSenderConfig; |
| 27 using media::cast::CastEnvironment; | 28 using media::cast::CastEnvironment; |
| 28 using media::cast::CastSender; | 29 using media::cast::CastSender; |
| 29 using media::cast::VideoSenderConfig; | 30 using media::cast::VideoSenderConfig; |
| 30 | 31 |
| 31 static base::LazyInstance<CastThreads> g_cast_threads = | 32 static base::LazyInstance<CastThreads> g_cast_threads = |
| 32 LAZY_INSTANCE_INITIALIZER; | 33 LAZY_INSTANCE_INITIALIZER; |
| 33 | 34 |
| 34 CastSessionDelegateBase::CastSessionDelegateBase() | 35 CastSessionDelegateBase::CastSessionDelegateBase() |
| 35 : io_message_loop_proxy_( | 36 : io_task_runner_( |
| 36 content::RenderThread::Get()->GetIOMessageLoopProxy()), | 37 content::RenderThread::Get()->GetIOMessageLoopProxy()), |
| 37 weak_factory_(this) { | 38 weak_factory_(this) { |
| 38 DCHECK(io_message_loop_proxy_.get()); | 39 DCHECK(io_task_runner_.get()); |
| 39 } | 40 } |
| 40 | 41 |
| 41 CastSessionDelegateBase::~CastSessionDelegateBase() { | 42 CastSessionDelegateBase::~CastSessionDelegateBase() { |
| 42 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 43 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 43 } | 44 } |
| 44 | 45 |
| 45 void CastSessionDelegateBase::StartUDP( | 46 void CastSessionDelegateBase::StartUDP( |
| 46 const net::IPEndPoint& local_endpoint, | 47 const net::IPEndPoint& local_endpoint, |
| 47 const net::IPEndPoint& remote_endpoint, | 48 const net::IPEndPoint& remote_endpoint, |
| 48 scoped_ptr<base::DictionaryValue> options, | 49 scoped_ptr<base::DictionaryValue> options, |
| 49 const ErrorCallback& error_callback) { | 50 const ErrorCallback& error_callback) { |
| 50 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 51 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 51 | 52 |
| 52 // CastSender uses the renderer's IO thread as the main thread. This reduces | 53 // CastSender uses the renderer's IO thread as the main thread. This reduces |
| 53 // thread hopping for incoming video frames and outgoing network packets. | 54 // thread hopping for incoming video frames and outgoing network packets. |
| 54 // TODO(hubbe): Create cast environment in ctor instead. | 55 // TODO(hubbe): Create cast environment in ctor instead. |
| 55 cast_environment_ = new CastEnvironment( | 56 cast_environment_ = new CastEnvironment( |
| 56 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), | 57 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), |
| 57 base::MessageLoopProxy::current(), | 58 base::ThreadTaskRunnerHandle::Get(), |
| 58 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), | 59 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), |
| 59 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); | 60 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); |
| 60 | 61 |
| 61 // Rationale for using unretained: The callback cannot be called after the | 62 // Rationale for using unretained: The callback cannot be called after the |
| 62 // destruction of CastTransportSenderIPC, and they both share the same thread. | 63 // destruction of CastTransportSenderIPC, and they both share the same thread. |
| 63 cast_transport_.reset(new CastTransportSenderIPC( | 64 cast_transport_.reset(new CastTransportSenderIPC( |
| 64 local_endpoint, | 65 local_endpoint, |
| 65 remote_endpoint, | 66 remote_endpoint, |
| 66 options.Pass(), | 67 options.Pass(), |
| 67 base::Bind(&CastSessionDelegateBase::ReceivePacket, | 68 base::Bind(&CastSessionDelegateBase::ReceivePacket, |
| 68 base::Unretained(this)), | 69 base::Unretained(this)), |
| 69 base::Bind(&CastSessionDelegateBase::StatusNotificationCB, | 70 base::Bind(&CastSessionDelegateBase::StatusNotificationCB, |
| 70 base::Unretained(this), error_callback), | 71 base::Unretained(this), error_callback), |
| 71 base::Bind(&CastSessionDelegateBase::LogRawEvents, | 72 base::Bind(&CastSessionDelegateBase::LogRawEvents, |
| 72 base::Unretained(this)))); | 73 base::Unretained(this)))); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void CastSessionDelegateBase::StatusNotificationCB( | 76 void CastSessionDelegateBase::StatusNotificationCB( |
| 76 const ErrorCallback& error_callback, | 77 const ErrorCallback& error_callback, |
| 77 media::cast::CastTransportStatus status) { | 78 media::cast::CastTransportStatus status) { |
| 78 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 79 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 79 std::string error_message; | 80 std::string error_message; |
| 80 | 81 |
| 81 switch (status) { | 82 switch (status) { |
| 82 case media::cast::TRANSPORT_AUDIO_UNINITIALIZED: | 83 case media::cast::TRANSPORT_AUDIO_UNINITIALIZED: |
| 83 case media::cast::TRANSPORT_VIDEO_UNINITIALIZED: | 84 case media::cast::TRANSPORT_VIDEO_UNINITIALIZED: |
| 84 case media::cast::TRANSPORT_AUDIO_INITIALIZED: | 85 case media::cast::TRANSPORT_AUDIO_INITIALIZED: |
| 85 case media::cast::TRANSPORT_VIDEO_INITIALIZED: | 86 case media::cast::TRANSPORT_VIDEO_INITIALIZED: |
| 86 return; // Not errors, do nothing. | 87 return; // Not errors, do nothing. |
| 87 case media::cast::TRANSPORT_INVALID_CRYPTO_CONFIG: | 88 case media::cast::TRANSPORT_INVALID_CRYPTO_CONFIG: |
| 88 error_callback.Run("Invalid encrypt/decrypt configuration."); | 89 error_callback.Run("Invalid encrypt/decrypt configuration."); |
| 89 break; | 90 break; |
| 90 case media::cast::TRANSPORT_SOCKET_ERROR: | 91 case media::cast::TRANSPORT_SOCKET_ERROR: |
| 91 error_callback.Run("Socket error."); | 92 error_callback.Run("Socket error."); |
| 92 break; | 93 break; |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 96 CastSessionDelegate::CastSessionDelegate() | 97 CastSessionDelegate::CastSessionDelegate() |
| 97 : weak_factory_(this) { | 98 : weak_factory_(this) { |
| 98 DCHECK(io_message_loop_proxy_.get()); | 99 DCHECK(io_task_runner_.get()); |
| 99 } | 100 } |
| 100 | 101 |
| 101 CastSessionDelegate::~CastSessionDelegate() { | 102 CastSessionDelegate::~CastSessionDelegate() { |
| 102 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 103 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 103 } | 104 } |
| 104 | 105 |
| 105 void CastSessionDelegate::StartAudio( | 106 void CastSessionDelegate::StartAudio( |
| 106 const AudioSenderConfig& config, | 107 const AudioSenderConfig& config, |
| 107 const AudioFrameInputAvailableCallback& callback, | 108 const AudioFrameInputAvailableCallback& callback, |
| 108 const ErrorCallback& error_callback) { | 109 const ErrorCallback& error_callback) { |
| 109 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 110 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 110 | 111 |
| 111 if (!cast_transport_ || !cast_sender_) { | 112 if (!cast_transport_ || !cast_sender_) { |
| 112 error_callback.Run("Destination not set."); | 113 error_callback.Run("Destination not set."); |
| 113 return; | 114 return; |
| 114 } | 115 } |
| 115 | 116 |
| 116 audio_frame_input_available_callback_ = callback; | 117 audio_frame_input_available_callback_ = callback; |
| 117 cast_sender_->InitializeAudio( | 118 cast_sender_->InitializeAudio( |
| 118 config, | 119 config, |
| 119 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, | 120 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, |
| 120 weak_factory_.GetWeakPtr(), true, error_callback)); | 121 weak_factory_.GetWeakPtr(), true, error_callback)); |
| 121 } | 122 } |
| 122 | 123 |
| 123 void CastSessionDelegate::StartVideo( | 124 void CastSessionDelegate::StartVideo( |
| 124 const VideoSenderConfig& config, | 125 const VideoSenderConfig& config, |
| 125 const VideoFrameInputAvailableCallback& callback, | 126 const VideoFrameInputAvailableCallback& callback, |
| 126 const ErrorCallback& error_callback, | 127 const ErrorCallback& error_callback, |
| 127 const media::cast::CreateVideoEncodeAcceleratorCallback& create_vea_cb, | 128 const media::cast::CreateVideoEncodeAcceleratorCallback& create_vea_cb, |
| 128 const media::cast::CreateVideoEncodeMemoryCallback& | 129 const media::cast::CreateVideoEncodeMemoryCallback& |
| 129 create_video_encode_mem_cb) { | 130 create_video_encode_mem_cb) { |
| 130 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 131 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 131 | 132 |
| 132 if (!cast_transport_ || !cast_sender_) { | 133 if (!cast_transport_ || !cast_sender_) { |
| 133 error_callback.Run("Destination not set."); | 134 error_callback.Run("Destination not set."); |
| 134 return; | 135 return; |
| 135 } | 136 } |
| 136 | 137 |
| 137 video_frame_input_available_callback_ = callback; | 138 video_frame_input_available_callback_ = callback; |
| 138 | 139 |
| 139 cast_sender_->InitializeVideo( | 140 cast_sender_->InitializeVideo( |
| 140 config, | 141 config, |
| 141 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, | 142 base::Bind(&CastSessionDelegate::OnOperationalStatusChange, |
| 142 weak_factory_.GetWeakPtr(), false, error_callback), | 143 weak_factory_.GetWeakPtr(), false, error_callback), |
| 143 create_vea_cb, | 144 create_vea_cb, |
| 144 create_video_encode_mem_cb); | 145 create_video_encode_mem_cb); |
| 145 } | 146 } |
| 146 | 147 |
| 147 | 148 |
| 148 void CastSessionDelegate::StartUDP( | 149 void CastSessionDelegate::StartUDP( |
| 149 const net::IPEndPoint& local_endpoint, | 150 const net::IPEndPoint& local_endpoint, |
| 150 const net::IPEndPoint& remote_endpoint, | 151 const net::IPEndPoint& remote_endpoint, |
| 151 scoped_ptr<base::DictionaryValue> options, | 152 scoped_ptr<base::DictionaryValue> options, |
| 152 const ErrorCallback& error_callback) { | 153 const ErrorCallback& error_callback) { |
| 153 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 154 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 154 CastSessionDelegateBase::StartUDP(local_endpoint, | 155 CastSessionDelegateBase::StartUDP(local_endpoint, |
| 155 remote_endpoint, | 156 remote_endpoint, |
| 156 options.Pass(), | 157 options.Pass(), |
| 157 error_callback); | 158 error_callback); |
| 158 event_subscribers_.reset( | 159 event_subscribers_.reset( |
| 159 new media::cast::RawEventSubscriberBundle(cast_environment_)); | 160 new media::cast::RawEventSubscriberBundle(cast_environment_)); |
| 160 | 161 |
| 161 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); | 162 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); |
| 162 } | 163 } |
| 163 | 164 |
| 164 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { | 165 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { |
| 165 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 166 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 166 if (!event_subscribers_.get()) | 167 if (!event_subscribers_.get()) |
| 167 return; | 168 return; |
| 168 | 169 |
| 169 if (enable) | 170 if (enable) |
| 170 event_subscribers_->AddEventSubscribers(is_audio); | 171 event_subscribers_->AddEventSubscribers(is_audio); |
| 171 else | 172 else |
| 172 event_subscribers_->RemoveEventSubscribers(is_audio); | 173 event_subscribers_->RemoveEventSubscribers(is_audio); |
| 173 } | 174 } |
| 174 | 175 |
| 175 void CastSessionDelegate::GetEventLogsAndReset( | 176 void CastSessionDelegate::GetEventLogsAndReset( |
| 176 bool is_audio, | 177 bool is_audio, |
| 177 const std::string& extra_data, | 178 const std::string& extra_data, |
| 178 const EventLogsCallback& callback) { | 179 const EventLogsCallback& callback) { |
| 179 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 180 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 180 | 181 |
| 181 if (!event_subscribers_.get()) { | 182 if (!event_subscribers_.get()) { |
| 182 callback.Run(make_scoped_ptr(new base::BinaryValue).Pass()); | 183 callback.Run(make_scoped_ptr(new base::BinaryValue).Pass()); |
| 183 return; | 184 return; |
| 184 } | 185 } |
| 185 | 186 |
| 186 media::cast::EncodingEventSubscriber* subscriber = | 187 media::cast::EncodingEventSubscriber* subscriber = |
| 187 event_subscribers_->GetEncodingEventSubscriber(is_audio); | 188 event_subscribers_->GetEncodingEventSubscriber(is_audio); |
| 188 if (!subscriber) { | 189 if (!subscriber) { |
| 189 callback.Run(make_scoped_ptr(new base::BinaryValue).Pass()); | 190 callback.Run(make_scoped_ptr(new base::BinaryValue).Pass()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 224 |
| 224 DVLOG(2) << "Serialized log length: " << output_bytes; | 225 DVLOG(2) << "Serialized log length: " << output_bytes; |
| 225 | 226 |
| 226 scoped_ptr<base::BinaryValue> blob( | 227 scoped_ptr<base::BinaryValue> blob( |
| 227 new base::BinaryValue(serialized_log.Pass(), output_bytes)); | 228 new base::BinaryValue(serialized_log.Pass(), output_bytes)); |
| 228 callback.Run(blob.Pass()); | 229 callback.Run(blob.Pass()); |
| 229 } | 230 } |
| 230 | 231 |
| 231 void CastSessionDelegate::GetStatsAndReset(bool is_audio, | 232 void CastSessionDelegate::GetStatsAndReset(bool is_audio, |
| 232 const StatsCallback& callback) { | 233 const StatsCallback& callback) { |
| 233 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 234 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 234 | 235 |
| 235 if (!event_subscribers_.get()) { | 236 if (!event_subscribers_.get()) { |
| 236 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); | 237 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); |
| 237 return; | 238 return; |
| 238 } | 239 } |
| 239 | 240 |
| 240 media::cast::StatsEventSubscriber* subscriber = | 241 media::cast::StatsEventSubscriber* subscriber = |
| 241 event_subscribers_->GetStatsEventSubscriber(is_audio); | 242 event_subscribers_->GetStatsEventSubscriber(is_audio); |
| 242 if (!subscriber) { | 243 if (!subscriber) { |
| 243 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); | 244 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 301 } |
| 301 | 302 |
| 302 void CastSessionDelegate::ReceivePacket( | 303 void CastSessionDelegate::ReceivePacket( |
| 303 scoped_ptr<media::cast::Packet> packet) { | 304 scoped_ptr<media::cast::Packet> packet) { |
| 304 // Do nothing (frees packet) | 305 // Do nothing (frees packet) |
| 305 } | 306 } |
| 306 | 307 |
| 307 void CastSessionDelegate::LogRawEvents( | 308 void CastSessionDelegate::LogRawEvents( |
| 308 const std::vector<media::cast::PacketEvent>& packet_events, | 309 const std::vector<media::cast::PacketEvent>& packet_events, |
| 309 const std::vector<media::cast::FrameEvent>& frame_events) { | 310 const std::vector<media::cast::FrameEvent>& frame_events) { |
| 310 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 311 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 311 | 312 |
| 312 for (std::vector<media::cast::PacketEvent>::const_iterator it = | 313 for (std::vector<media::cast::PacketEvent>::const_iterator it = |
| 313 packet_events.begin(); | 314 packet_events.begin(); |
| 314 it != packet_events.end(); | 315 it != packet_events.end(); |
| 315 ++it) { | 316 ++it) { |
| 316 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, | 317 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, |
| 317 it->type, | 318 it->type, |
| 318 it->media_type, | 319 it->media_type, |
| 319 it->rtp_timestamp, | 320 it->rtp_timestamp, |
| 320 it->frame_id, | 321 it->frame_id, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 337 } else { | 338 } else { |
| 338 cast_environment_->Logging()->InsertFrameEvent( | 339 cast_environment_->Logging()->InsertFrameEvent( |
| 339 it->timestamp, | 340 it->timestamp, |
| 340 it->type, | 341 it->type, |
| 341 it->media_type, | 342 it->media_type, |
| 342 it->rtp_timestamp, | 343 it->rtp_timestamp, |
| 343 it->frame_id); | 344 it->frame_id); |
| 344 } | 345 } |
| 345 } | 346 } |
| 346 } | 347 } |
| OLD | NEW |