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 |
49 CastSessionDelegate::~CastSessionDelegate() { | 53 CastSessionDelegate::~CastSessionDelegate() { |
50 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 54 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
51 | 55 |
52 if (audio_event_subscriber_.get()) { | 56 for (EventSubscriberMap::iterator it = event_subscriber_map_.begin(); |
53 cast_environment_->Logging()->RemoveRawEventSubscriber( | 57 it != event_subscriber_map_.end(); |
54 audio_event_subscriber_.get()); | 58 ++it) |
55 } | 59 cast_environment_->Logging()->RemoveRawEventSubscriber(it->second.get()); |
56 if (video_event_subscriber_.get()) { | |
57 cast_environment_->Logging()->RemoveRawEventSubscriber( | |
58 video_event_subscriber_.get()); | |
59 } | |
60 } | 60 } |
61 | 61 |
62 void CastSessionDelegate::Initialize() { | 62 void CastSessionDelegate::Initialize() { |
63 if (cast_environment_) | 63 if (cast_environment_) |
64 return; // Already initialized. | 64 return; // Already initialized. |
65 | 65 |
66 // CastSender uses the renderer's IO thread as the main thread. This reduces | 66 // CastSender uses the renderer's IO thread as the main thread. This reduces |
67 // thread hopping for incoming video frames and outgoing network packets. | 67 // thread hopping for incoming video frames and outgoing network packets. |
68 // There's no need to decode so no thread assigned for decoding. | 68 // There's no need to decode so no thread assigned for decoding. |
69 // Get default logging: All disabled. | 69 // Get default logging: All disabled. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 void CastSessionDelegate::StartUDP( | 101 void CastSessionDelegate::StartUDP( |
102 const net::IPEndPoint& local_endpoint, | 102 const net::IPEndPoint& local_endpoint, |
103 const net::IPEndPoint& remote_endpoint) { | 103 const net::IPEndPoint& remote_endpoint) { |
104 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 104 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
105 transport_configured_ = true; | 105 transport_configured_ = true; |
106 local_endpoint_ = local_endpoint; | 106 local_endpoint_ = local_endpoint; |
107 remote_endpoint_ = remote_endpoint; | 107 remote_endpoint_ = remote_endpoint; |
108 StartSendingInternal(); | 108 StartSendingInternal(); |
109 } | 109 } |
110 | 110 |
| 111 void CastSessionDelegate::ToggleLogging(const int stream_id, |
| 112 const bool enable, |
| 113 const bool is_audio) { |
| 114 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 115 EventSubscriberMap::iterator it = event_subscriber_map_.find(stream_id); |
| 116 if (enable) { |
| 117 DCHECK(it == event_subscriber_map_.end()); |
| 118 |
| 119 linked_ptr<media::cast::EncodingEventSubscriber> event_subscriber( |
| 120 new media::cast::EncodingEventSubscriber( |
| 121 is_audio ? media::cast::AUDIO_EVENT : media::cast::VIDEO_EVENT, |
| 122 is_audio ? kMaxAudioEventEntries : kMaxVideoEventEntries)); |
| 123 |
| 124 cast_environment_->Logging()->AddRawEventSubscriber(event_subscriber.get()); |
| 125 |
| 126 event_subscriber_map_.insert(std::make_pair(stream_id, event_subscriber)); |
| 127 } else { |
| 128 DCHECK(it != event_subscriber_map_.end()); |
| 129 cast_environment_->Logging()->RemoveRawEventSubscriber(it->second.get()); |
| 130 event_subscriber_map_.erase(it); |
| 131 } |
| 132 } |
| 133 |
111 void CastSessionDelegate::GetEventLogsAndReset( | 134 void CastSessionDelegate::GetEventLogsAndReset( |
112 const EventLogsCallback& callback) { | 135 const EventLogsCallback& callback, |
113 if (!cast_sender_.get()) | 136 const int stream_id) { |
| 137 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 138 EventSubscriberMap::iterator it = event_subscriber_map_.find(stream_id); |
| 139 if (it == event_subscriber_map_.end()) { |
| 140 VLOG(2) << "Did not find event subscriber for stream " << stream_id << "."; |
114 return; | 141 return; |
| 142 } |
115 | 143 |
116 // TODO(imcheng): Get data from event subscribers. | 144 media::cast::FrameEventMap frame_events; |
117 scoped_ptr<std::string> result(new std::string); | 145 media::cast::PacketEventMap packet_events; |
118 callback.Run(result.Pass()); | 146 media::cast::RtpTimestamp rtp_timestamp; |
| 147 |
| 148 it->second->GetEventsAndReset(&frame_events, &packet_events, &rtp_timestamp); |
| 149 |
| 150 media::cast::LogSerializer log_serializer(kMaxSerializedBytes); |
| 151 bool success = log_serializer.SerializeEventsForStream( |
| 152 stream_id, frame_events, packet_events, rtp_timestamp); |
| 153 |
| 154 if (!success) { |
| 155 VLOG(2) << "Failed to serialize event log."; |
| 156 return; |
| 157 } |
| 158 |
| 159 scoped_ptr<std::string> serialized_log = |
| 160 log_serializer.GetSerializedLogAndReset(); |
| 161 DVLOG(2) << "Serialized log length: " << serialized_log->size(); |
| 162 |
| 163 callback.Run(serialized_log.Pass()); |
119 } | 164 } |
120 | 165 |
121 void CastSessionDelegate::StatusNotificationCB( | 166 void CastSessionDelegate::StatusNotificationCB( |
122 media::cast::transport::CastTransportStatus unused_status) { | 167 media::cast::transport::CastTransportStatus unused_status) { |
123 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 168 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
124 // TODO(hubbe): Call javascript UDPTransport error function. | 169 // TODO(hubbe): Call javascript UDPTransport error function. |
125 } | 170 } |
126 | 171 |
127 void CastSessionDelegate::StartSendingInternal() { | 172 void CastSessionDelegate::StartSendingInternal() { |
128 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 173 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
(...skipping 12 matching lines...) Expand all Loading... |
141 | 186 |
142 // TODO(hubbe): set config.aes_key and config.aes_iv_mask. | 187 // TODO(hubbe): set config.aes_key and config.aes_iv_mask. |
143 config.local_endpoint = local_endpoint_; | 188 config.local_endpoint = local_endpoint_; |
144 config.receiver_endpoint = remote_endpoint_; | 189 config.receiver_endpoint = remote_endpoint_; |
145 if (audio_config_) { | 190 if (audio_config_) { |
146 config.audio_ssrc = audio_config_->sender_ssrc; | 191 config.audio_ssrc = audio_config_->sender_ssrc; |
147 config.audio_codec = audio_config_->codec; | 192 config.audio_codec = audio_config_->codec; |
148 config.audio_rtp_config = audio_config_->rtp_config; | 193 config.audio_rtp_config = audio_config_->rtp_config; |
149 config.audio_frequency = audio_config_->frequency; | 194 config.audio_frequency = audio_config_->frequency; |
150 config.audio_channels = audio_config_->channels; | 195 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 } | 196 } |
159 if (video_config_) { | 197 if (video_config_) { |
160 config.video_ssrc = video_config_->sender_ssrc; | 198 config.video_ssrc = video_config_->sender_ssrc; |
161 config.video_codec = video_config_->codec; | 199 config.video_codec = video_config_->codec; |
162 config.video_rtp_config = video_config_->rtp_config; | 200 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 } | 201 } |
171 | 202 |
172 cast_transport_.reset(new CastTransportSenderIPC( | 203 cast_transport_.reset(new CastTransportSenderIPC( |
173 config, | 204 config, |
174 base::Bind(&CastSessionDelegate::StatusNotificationCB, | 205 base::Bind(&CastSessionDelegate::StatusNotificationCB, |
175 base::Unretained(this)))); | 206 base::Unretained(this)))); |
176 | 207 |
177 cast_sender_.reset(CastSender::CreateCastSender( | 208 cast_sender_.reset(CastSender::CreateCastSender( |
178 cast_environment_, | 209 cast_environment_, |
179 audio_config_.get(), | 210 audio_config_.get(), |
(...skipping 12 matching lines...) Expand all Loading... |
192 // TODO(pwestin): handle the error codes. | 223 // TODO(pwestin): handle the error codes. |
193 if (result == media::cast::STATUS_INITIALIZED) { | 224 if (result == media::cast::STATUS_INITIALIZED) { |
194 if (!audio_frame_input_available_callback_.is_null()) { | 225 if (!audio_frame_input_available_callback_.is_null()) { |
195 audio_frame_input_available_callback_.Run(cast_sender_->frame_input()); | 226 audio_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
196 } | 227 } |
197 if (!video_frame_input_available_callback_.is_null()) { | 228 if (!video_frame_input_available_callback_.is_null()) { |
198 video_frame_input_available_callback_.Run(cast_sender_->frame_input()); | 229 video_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
199 } | 230 } |
200 } | 231 } |
201 } | 232 } |
| 233 |
OLD | NEW |