| 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/renderer/media/cast_threads.h" | 10 #include "chrome/renderer/media/cast_threads.h" |
| 11 #include "chrome/renderer/media/cast_transport_sender_ipc.h" | 11 #include "chrome/renderer/media/cast_transport_sender_ipc.h" |
| 12 #include "content/public/renderer/render_thread.h" | 12 #include "content/public/renderer/render_thread.h" |
| 13 #include "media/cast/cast_config.h" | 13 #include "media/cast/cast_config.h" |
| 14 #include "media/cast/cast_environment.h" | 14 #include "media/cast/cast_environment.h" |
| 15 #include "media/cast/cast_sender.h" | 15 #include "media/cast/cast_sender.h" |
| 16 #include "media/cast/logging/encoding_event_subscriber.h" | 16 #include "media/cast/logging/encoding_event_subscriber.h" |
| 17 #include "media/cast/logging/log_serializer.h" | 17 #include "media/cast/logging/log_serializer.h" |
| 18 #include "media/cast/logging/logging_defines.h" | 18 #include "media/cast/logging/logging_defines.h" |
| 19 #include "media/cast/logging/stats_event_subscriber.h" |
| 20 #include "media/cast/logging/stats_util.h" |
| 19 #include "media/cast/transport/cast_transport_config.h" | 21 #include "media/cast/transport/cast_transport_config.h" |
| 20 #include "media/cast/transport/cast_transport_sender.h" | 22 #include "media/cast/transport/cast_transport_sender.h" |
| 21 | 23 |
| 22 using media::cast::AudioSenderConfig; | 24 using media::cast::AudioSenderConfig; |
| 23 using media::cast::CastEnvironment; | 25 using media::cast::CastEnvironment; |
| 24 using media::cast::CastSender; | 26 using media::cast::CastSender; |
| 25 using media::cast::VideoSenderConfig; | 27 using media::cast::VideoSenderConfig; |
| 26 | 28 |
| 27 static base::LazyInstance<CastThreads> g_cast_threads = | 29 static base::LazyInstance<CastThreads> g_cast_threads = |
| 28 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 config, | 112 config, |
| 111 base::Bind(&CastSessionDelegate::InitializationResultCB, | 113 base::Bind(&CastSessionDelegate::InitializationResultCB, |
| 112 weak_factory_.GetWeakPtr()), | 114 weak_factory_.GetWeakPtr()), |
| 113 create_vea_cb, | 115 create_vea_cb, |
| 114 create_video_encode_mem_cb); | 116 create_video_encode_mem_cb); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void CastSessionDelegate::StartUDP(const net::IPEndPoint& remote_endpoint) { | 119 void CastSessionDelegate::StartUDP(const net::IPEndPoint& remote_endpoint) { |
| 118 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 120 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 119 | 121 |
| 120 // Logging: enable raw events and stats collection. | |
| 121 media::cast::CastLoggingConfig logging_config = | |
| 122 media::cast::GetLoggingConfigWithRawEventsAndStatsEnabled(); | |
| 123 | |
| 124 // CastSender uses the renderer's IO thread as the main thread. This reduces | 122 // CastSender uses the renderer's IO thread as the main thread. This reduces |
| 125 // thread hopping for incoming video frames and outgoing network packets. | 123 // thread hopping for incoming video frames and outgoing network packets. |
| 126 cast_environment_ = new CastEnvironment( | 124 cast_environment_ = new CastEnvironment( |
| 127 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), | 125 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), |
| 128 base::MessageLoopProxy::current(), | 126 base::MessageLoopProxy::current(), |
| 129 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), | 127 g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(), |
| 130 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy(), | 128 g_cast_threads.Get().GetVideoEncodeMessageLoopProxy()); |
| 131 logging_config); | |
| 132 | 129 |
| 133 // Rationale for using unretained: The callback cannot be called after the | 130 // Rationale for using unretained: The callback cannot be called after the |
| 134 // destruction of CastTransportSenderIPC, and they both share the same thread. | 131 // destruction of CastTransportSenderIPC, and they both share the same thread. |
| 135 cast_transport_.reset(new CastTransportSenderIPC( | 132 cast_transport_.reset(new CastTransportSenderIPC( |
| 136 remote_endpoint, | 133 remote_endpoint, |
| 137 base::Bind(&CastSessionDelegate::StatusNotificationCB, | 134 base::Bind(&CastSessionDelegate::StatusNotificationCB, |
| 138 base::Unretained(this)), | 135 base::Unretained(this)), |
| 139 logging_config, | |
| 140 base::Bind(&CastSessionDelegate::LogRawEvents, base::Unretained(this)))); | 136 base::Bind(&CastSessionDelegate::LogRawEvents, base::Unretained(this)))); |
| 141 | 137 |
| 142 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); | 138 cast_sender_ = CastSender::Create(cast_environment_, cast_transport_.get()); |
| 143 cast_transport_->SetPacketReceiver(cast_sender_->packet_receiver()); | 139 cast_transport_->SetPacketReceiver(cast_sender_->packet_receiver()); |
| 144 } | 140 } |
| 145 | 141 |
| 146 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { | 142 void CastSessionDelegate::ToggleLogging(bool is_audio, bool enable) { |
| 147 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 143 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 148 if (enable) { | 144 if (enable) { |
| 149 if (is_audio) { | 145 if (is_audio) { |
| 150 if (audio_event_subscriber_.get()) | 146 if (!audio_event_subscriber_.get()) { |
| 151 return; | 147 audio_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( |
| 152 audio_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( | 148 media::cast::AUDIO_EVENT, kMaxAudioEventEntries)); |
| 153 media::cast::AUDIO_EVENT, kMaxAudioEventEntries)); | 149 cast_environment_->Logging()->AddRawEventSubscriber( |
| 154 cast_environment_->Logging()->AddRawEventSubscriber( | 150 audio_event_subscriber_.get()); |
| 155 audio_event_subscriber_.get()); | 151 } |
| 152 if (!audio_stats_subscriber_.get()) { |
| 153 audio_stats_subscriber_.reset( |
| 154 new media::cast::StatsEventSubscriber(media::cast::AUDIO_EVENT)); |
| 155 cast_environment_->Logging()->AddRawEventSubscriber( |
| 156 audio_stats_subscriber_.get()); |
| 157 } |
| 156 } else { | 158 } else { |
| 157 if (video_event_subscriber_.get()) | 159 if (!video_event_subscriber_.get()) { |
| 158 return; | 160 video_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( |
| 159 video_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( | 161 media::cast::VIDEO_EVENT, kMaxVideoEventEntries)); |
| 160 media::cast::VIDEO_EVENT, kMaxVideoEventEntries)); | 162 cast_environment_->Logging()->AddRawEventSubscriber( |
| 161 cast_environment_->Logging()->AddRawEventSubscriber( | 163 video_event_subscriber_.get()); |
| 162 video_event_subscriber_.get()); | 164 } |
| 165 if (!video_stats_subscriber_.get()) { |
| 166 video_stats_subscriber_.reset( |
| 167 new media::cast::StatsEventSubscriber(media::cast::VIDEO_EVENT)); |
| 168 cast_environment_->Logging()->AddRawEventSubscriber( |
| 169 video_stats_subscriber_.get()); |
| 170 } |
| 163 } | 171 } |
| 164 } else { | 172 } else { |
| 165 if (is_audio) { | 173 if (is_audio) { |
| 166 if (!audio_event_subscriber_.get()) | 174 if (audio_event_subscriber_.get()) { |
| 167 return; | 175 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 168 cast_environment_->Logging()->RemoveRawEventSubscriber( | 176 audio_event_subscriber_.get()); |
| 169 audio_event_subscriber_.get()); | 177 audio_event_subscriber_.reset(); |
| 170 audio_event_subscriber_.reset(); | 178 } |
| 179 if (audio_stats_subscriber_.get()) { |
| 180 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 181 audio_stats_subscriber_.get()); |
| 182 audio_stats_subscriber_.reset(); |
| 183 } |
| 171 } else { | 184 } else { |
| 172 if (!video_event_subscriber_.get()) | 185 if (video_event_subscriber_.get()) { |
| 173 return; | 186 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 174 cast_environment_->Logging()->RemoveRawEventSubscriber( | 187 video_event_subscriber_.get()); |
| 175 video_event_subscriber_.get()); | 188 video_event_subscriber_.reset(); |
| 176 video_event_subscriber_.reset(); | 189 } |
| 190 if (video_stats_subscriber_.get()) { |
| 191 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 192 video_stats_subscriber_.get()); |
| 193 video_stats_subscriber_.reset(); |
| 194 } |
| 177 } | 195 } |
| 178 } | 196 } |
| 179 } | 197 } |
| 180 | 198 |
| 181 void CastSessionDelegate::GetEventLogsAndReset( | 199 void CastSessionDelegate::GetEventLogsAndReset( |
| 182 bool is_audio, | 200 bool is_audio, |
| 183 const EventLogsCallback& callback) { | 201 const EventLogsCallback& callback) { |
| 184 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 202 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 185 | 203 |
| 186 media::cast::EncodingEventSubscriber* subscriber = | 204 media::cast::EncodingEventSubscriber* subscriber = |
| (...skipping 27 matching lines...) Expand all Loading... |
| 214 | 232 |
| 215 DVLOG(2) << "Serialized log length: " << output_bytes; | 233 DVLOG(2) << "Serialized log length: " << output_bytes; |
| 216 | 234 |
| 217 scoped_ptr<base::BinaryValue> blob( | 235 scoped_ptr<base::BinaryValue> blob( |
| 218 new base::BinaryValue(serialized_log.Pass(), output_bytes)); | 236 new base::BinaryValue(serialized_log.Pass(), output_bytes)); |
| 219 callback.Run(blob.Pass()); | 237 callback.Run(blob.Pass()); |
| 220 } | 238 } |
| 221 | 239 |
| 222 void CastSessionDelegate::GetStatsAndReset(bool is_audio, | 240 void CastSessionDelegate::GetStatsAndReset(bool is_audio, |
| 223 const StatsCallback& callback) { | 241 const StatsCallback& callback) { |
| 224 media::cast::FrameStatsMap frame_stats = | 242 media::cast::StatsEventSubscriber* subscriber = |
| 225 cast_environment_->Logging()->GetFrameStatsData( | 243 is_audio ? audio_stats_subscriber_.get() : video_stats_subscriber_.get(); |
| 226 is_audio ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT); | 244 if (!subscriber) { |
| 227 media::cast::PacketStatsMap packet_stats = | 245 callback.Run(make_scoped_ptr(new base::DictionaryValue).Pass()); |
| 228 cast_environment_->Logging()->GetPacketStatsData( | 246 return; |
| 229 is_audio ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT); | 247 } |
| 248 |
| 249 media::cast::FrameStatsMap frame_stats; |
| 250 subscriber->GetFrameStats(&frame_stats); |
| 251 media::cast::PacketStatsMap packet_stats; |
| 252 subscriber->GetPacketStats(&packet_stats); |
| 253 subscriber->Reset(); |
| 230 | 254 |
| 231 scoped_ptr<base::DictionaryValue> stats = media::cast::ConvertStats( | 255 scoped_ptr<base::DictionaryValue> stats = media::cast::ConvertStats( |
| 232 frame_stats, packet_stats); | 256 frame_stats, packet_stats); |
| 233 | 257 |
| 234 callback.Run(stats.Pass()); | 258 callback.Run(stats.Pass()); |
| 235 } | 259 } |
| 236 | 260 |
| 237 void CastSessionDelegate::StatusNotificationCB( | 261 void CastSessionDelegate::StatusNotificationCB( |
| 238 media::cast::transport::CastTransportStatus unused_status) { | 262 media::cast::transport::CastTransportStatus unused_status) { |
| 239 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 263 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 264 ++it) { | 288 ++it) { |
| 265 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, | 289 cast_environment_->Logging()->InsertPacketEvent(it->timestamp, |
| 266 it->type, | 290 it->type, |
| 267 it->rtp_timestamp, | 291 it->rtp_timestamp, |
| 268 it->frame_id, | 292 it->frame_id, |
| 269 it->packet_id, | 293 it->packet_id, |
| 270 it->max_packet_id, | 294 it->max_packet_id, |
| 271 it->size); | 295 it->size); |
| 272 } | 296 } |
| 273 } | 297 } |
| OLD | NEW |