Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(463)

Side by Side Diff: chrome/renderer/media/cast_session_delegate.cc

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

Powered by Google App Engine
This is Rietveld 408576698