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/p2p_socket_client.h" | 12 #include "content/public/renderer/p2p_socket_client.h" |
13 #include "content/public/renderer/render_thread.h" | 13 #include "content/public/renderer/render_thread.h" |
14 #include "media/cast/cast_config.h" | 14 #include "media/cast/cast_config.h" |
15 #include "media/cast/cast_environment.h" | 15 #include "media/cast/cast_environment.h" |
16 #include "media/cast/cast_sender.h" | 16 #include "media/cast/cast_sender.h" |
17 #include "media/cast/logging/encoding_event_subscriber.h" | 17 #include "media/cast/logging/encoding_event_subscriber.h" |
| 18 #include "media/cast/logging/log_serializer.h" |
18 #include "media/cast/logging/logging_defines.h" | 19 #include "media/cast/logging/logging_defines.h" |
19 #include "media/cast/transport/cast_transport_config.h" | 20 #include "media/cast/transport/cast_transport_config.h" |
20 #include "media/cast/transport/cast_transport_sender.h" | 21 #include "media/cast/transport/cast_transport_sender.h" |
21 | 22 |
22 using media::cast::AudioSenderConfig; | 23 using media::cast::AudioSenderConfig; |
23 using media::cast::CastEnvironment; | 24 using media::cast::CastEnvironment; |
24 using media::cast::CastSender; | 25 using media::cast::CastSender; |
25 using media::cast::VideoSenderConfig; | 26 using media::cast::VideoSenderConfig; |
26 | 27 |
27 static base::LazyInstance<CastThreads> g_cast_threads = | 28 static base::LazyInstance<CastThreads> g_cast_threads = |
28 LAZY_INSTANCE_INITIALIZER; | 29 LAZY_INSTANCE_INITIALIZER; |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
| 33 // Allow 10MB for serialized video / audio event logs. |
| 34 const int kMaxSerializedBytes = 9000000; |
| 35 |
32 // Allow about 9MB for video event logs. Assume serialized log data for | 36 // Allow about 9MB for video event logs. Assume serialized log data for |
33 // each frame will take up to 150 bytes. | 37 // each frame will take up to 150 bytes. |
34 const int kMaxVideoEventEntries = 9000000 / 150; | 38 const int kMaxVideoEventEntries = kMaxSerializedBytes / 150; |
35 | 39 |
36 // Allow about 9MB for audio event logs. Assume serialized log data for | 40 // Allow about 9MB for audio event logs. Assume serialized log data for |
37 // each frame will take up to 75 bytes. | 41 // each frame will take up to 75 bytes. |
38 const int kMaxAudioEventEntries = 9000000 / 75; | 42 const int kMaxAudioEventEntries = kMaxSerializedBytes / 75; |
39 | 43 |
40 } // namespace | 44 } // namespace |
41 | 45 |
42 CastSessionDelegate::CastSessionDelegate() | 46 CastSessionDelegate::CastSessionDelegate() |
43 : transport_configured_(false), | 47 : transport_configured_(false), |
44 io_message_loop_proxy_( | 48 io_message_loop_proxy_( |
45 content::RenderThread::Get()->GetIOMessageLoopProxy()) { | 49 content::RenderThread::Get()->GetIOMessageLoopProxy()) { |
46 DCHECK(io_message_loop_proxy_); | 50 DCHECK(io_message_loop_proxy_); |
47 } | 51 } |
48 | 52 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 void CastSessionDelegate::StartUDP( | 105 void CastSessionDelegate::StartUDP( |
102 const net::IPEndPoint& local_endpoint, | 106 const net::IPEndPoint& local_endpoint, |
103 const net::IPEndPoint& remote_endpoint) { | 107 const net::IPEndPoint& remote_endpoint) { |
104 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 108 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
105 transport_configured_ = true; | 109 transport_configured_ = true; |
106 local_endpoint_ = local_endpoint; | 110 local_endpoint_ = local_endpoint; |
107 remote_endpoint_ = remote_endpoint; | 111 remote_endpoint_ = remote_endpoint; |
108 StartSendingInternal(); | 112 StartSendingInternal(); |
109 } | 113 } |
110 | 114 |
| 115 void CastSessionDelegate::ToggleLogging(bool is_audio, |
| 116 bool enable) { |
| 117 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 118 if (enable) { |
| 119 if (is_audio) { |
| 120 if (audio_event_subscriber_.get()) |
| 121 return; |
| 122 audio_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( |
| 123 media::cast::AUDIO_EVENT, kMaxAudioEventEntries)); |
| 124 cast_environment_->Logging()->AddRawEventSubscriber( |
| 125 audio_event_subscriber_.get()); |
| 126 } else { |
| 127 if (video_event_subscriber_.get()) |
| 128 return; |
| 129 video_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( |
| 130 media::cast::VIDEO_EVENT, kMaxVideoEventEntries)); |
| 131 cast_environment_->Logging()->AddRawEventSubscriber( |
| 132 video_event_subscriber_.get()); |
| 133 } |
| 134 } else { |
| 135 if (is_audio) { |
| 136 if (!audio_event_subscriber_.get()) |
| 137 return; |
| 138 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 139 audio_event_subscriber_.get()); |
| 140 audio_event_subscriber_.reset(); |
| 141 } else { |
| 142 if (!video_event_subscriber_.get()) |
| 143 return; |
| 144 cast_environment_->Logging()->RemoveRawEventSubscriber( |
| 145 video_event_subscriber_.get()); |
| 146 video_event_subscriber_.reset(); |
| 147 } |
| 148 } |
| 149 } |
| 150 |
111 void CastSessionDelegate::GetEventLogsAndReset( | 151 void CastSessionDelegate::GetEventLogsAndReset( |
112 const EventLogsCallback& callback) { | 152 bool is_audio, const EventLogsCallback& callback) { |
113 if (!cast_sender_.get()) | 153 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 154 |
| 155 media::cast::EncodingEventSubscriber* subscriber = is_audio ? |
| 156 audio_event_subscriber_.get() : video_event_subscriber_.get(); |
| 157 if (!subscriber) { |
| 158 VLOG(2) << "Logging is currently disabled."; |
| 159 callback.Run(make_scoped_ptr(new std::string).Pass()); |
114 return; | 160 return; |
| 161 } |
115 | 162 |
116 // TODO(imcheng): Get data from event subscribers. | 163 media::cast::FrameEventMap frame_events; |
117 scoped_ptr<std::string> result(new std::string); | 164 media::cast::PacketEventMap packet_events; |
118 callback.Run(result.Pass()); | 165 media::cast::RtpTimestamp rtp_timestamp; |
| 166 |
| 167 subscriber->GetEventsAndReset(&frame_events, &packet_events, &rtp_timestamp); |
| 168 |
| 169 media::cast::LogSerializer log_serializer(kMaxSerializedBytes); |
| 170 bool success = log_serializer.SerializeEventsForStream( |
| 171 is_audio, frame_events, packet_events, rtp_timestamp); |
| 172 |
| 173 if (!success) { |
| 174 VLOG(2) << "Failed to serialize event log."; |
| 175 callback.Run(make_scoped_ptr(new std::string).Pass()); |
| 176 return; |
| 177 } |
| 178 |
| 179 scoped_ptr<std::string> serialized_log = |
| 180 log_serializer.GetSerializedLogAndReset(); |
| 181 DVLOG(2) << "Serialized log length: " << serialized_log->size(); |
| 182 |
| 183 callback.Run(serialized_log.Pass()); |
119 } | 184 } |
120 | 185 |
121 void CastSessionDelegate::StatusNotificationCB( | 186 void CastSessionDelegate::StatusNotificationCB( |
122 media::cast::transport::CastTransportStatus unused_status) { | 187 media::cast::transport::CastTransportStatus unused_status) { |
123 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 188 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
124 // TODO(hubbe): Call javascript UDPTransport error function. | 189 // TODO(hubbe): Call javascript UDPTransport error function. |
125 } | 190 } |
126 | 191 |
127 void CastSessionDelegate::StartSendingInternal() { | 192 void CastSessionDelegate::StartSendingInternal() { |
128 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 193 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 206 |
142 // TODO(hubbe): set config.aes_key and config.aes_iv_mask. | 207 // TODO(hubbe): set config.aes_key and config.aes_iv_mask. |
143 config.local_endpoint = local_endpoint_; | 208 config.local_endpoint = local_endpoint_; |
144 config.receiver_endpoint = remote_endpoint_; | 209 config.receiver_endpoint = remote_endpoint_; |
145 if (audio_config_) { | 210 if (audio_config_) { |
146 config.audio_ssrc = audio_config_->sender_ssrc; | 211 config.audio_ssrc = audio_config_->sender_ssrc; |
147 config.audio_codec = audio_config_->codec; | 212 config.audio_codec = audio_config_->codec; |
148 config.audio_rtp_config = audio_config_->rtp_config; | 213 config.audio_rtp_config = audio_config_->rtp_config; |
149 config.audio_frequency = audio_config_->frequency; | 214 config.audio_frequency = audio_config_->frequency; |
150 config.audio_channels = audio_config_->channels; | 215 config.audio_channels = audio_config_->channels; |
151 | |
152 if (!audio_event_subscriber_.get()) { | |
153 audio_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( | |
154 media::cast::AUDIO_EVENT, kMaxAudioEventEntries)); | |
155 cast_environment_->Logging()->AddRawEventSubscriber( | |
156 audio_event_subscriber_.get()); | |
157 } | |
158 } | 216 } |
159 if (video_config_) { | 217 if (video_config_) { |
160 config.video_ssrc = video_config_->sender_ssrc; | 218 config.video_ssrc = video_config_->sender_ssrc; |
161 config.video_codec = video_config_->codec; | 219 config.video_codec = video_config_->codec; |
162 config.video_rtp_config = video_config_->rtp_config; | 220 config.video_rtp_config = video_config_->rtp_config; |
163 | |
164 if (!video_event_subscriber_.get()) { | |
165 video_event_subscriber_.reset(new media::cast::EncodingEventSubscriber( | |
166 media::cast::VIDEO_EVENT, kMaxVideoEventEntries)); | |
167 cast_environment_->Logging()->AddRawEventSubscriber( | |
168 video_event_subscriber_.get()); | |
169 } | |
170 } | 221 } |
171 | 222 |
172 cast_transport_.reset(new CastTransportSenderIPC( | 223 cast_transport_.reset(new CastTransportSenderIPC( |
173 config, | 224 config, |
174 base::Bind(&CastSessionDelegate::StatusNotificationCB, | 225 base::Bind(&CastSessionDelegate::StatusNotificationCB, |
175 base::Unretained(this)))); | 226 base::Unretained(this)))); |
176 | 227 |
177 cast_sender_.reset(CastSender::CreateCastSender( | 228 cast_sender_.reset(CastSender::CreateCastSender( |
178 cast_environment_, | 229 cast_environment_, |
179 audio_config_.get(), | 230 audio_config_.get(), |
(...skipping 12 matching lines...) Expand all Loading... |
192 // TODO(pwestin): handle the error codes. | 243 // TODO(pwestin): handle the error codes. |
193 if (result == media::cast::STATUS_INITIALIZED) { | 244 if (result == media::cast::STATUS_INITIALIZED) { |
194 if (!audio_frame_input_available_callback_.is_null()) { | 245 if (!audio_frame_input_available_callback_.is_null()) { |
195 audio_frame_input_available_callback_.Run(cast_sender_->frame_input()); | 246 audio_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
196 } | 247 } |
197 if (!video_frame_input_available_callback_.is_null()) { | 248 if (!video_frame_input_available_callback_.is_null()) { |
198 video_frame_input_available_callback_.Run(cast_sender_->frame_input()); | 249 video_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
199 } | 250 } |
200 } | 251 } |
201 } | 252 } |
| 253 |
OLD | NEW |