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

Side by Side Diff: content/renderer/media/webrtc/peer_connection_dependency_factory.cc

Issue 675013005: Split libjingle's signaling thread from the UI thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Broken changes for review Created 6 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/renderer/media/webrtc/peer_connection_dependency_factory.h" 5 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 rtc::NetworkManager* network_manager_; 162 rtc::NetworkManager* network_manager_;
163 rtc::PacketSocketFactory* socket_factory_; 163 rtc::PacketSocketFactory* socket_factory_;
164 }; 164 };
165 165
166 PeerConnectionDependencyFactory::PeerConnectionDependencyFactory( 166 PeerConnectionDependencyFactory::PeerConnectionDependencyFactory(
167 P2PSocketDispatcher* p2p_socket_dispatcher) 167 P2PSocketDispatcher* p2p_socket_dispatcher)
168 : network_manager_(NULL), 168 : network_manager_(NULL),
169 p2p_socket_dispatcher_(p2p_socket_dispatcher), 169 p2p_socket_dispatcher_(p2p_socket_dispatcher),
170 signaling_thread_(NULL), 170 signaling_thread_(NULL),
171 worker_thread_(NULL), 171 worker_thread_(NULL),
172 chrome_signaling_thread_("Chrome_libJingle_Signaling"),
172 chrome_worker_thread_("Chrome_libJingle_WorkerThread") { 173 chrome_worker_thread_("Chrome_libJingle_WorkerThread") {
173 } 174 }
174 175
175 PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() { 176 PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() {
176 CleanupPeerConnectionFactory(); 177 CleanupPeerConnectionFactory();
177 if (aec_dump_message_filter_.get()) 178 if (aec_dump_message_filter_.get())
178 aec_dump_message_filter_->RemoveDelegate(this); 179 aec_dump_message_filter_->RemoveDelegate(this);
179 } 180 }
180 181
181 blink::WebRTCPeerConnectionHandler* 182 blink::WebRTCPeerConnectionHandler*
182 PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler( 183 PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler(
183 blink::WebRTCPeerConnectionHandlerClient* client) { 184 blink::WebRTCPeerConnectionHandlerClient* client) {
185 if (!GetPcFactory().get())
186 return NULL;
184 // Save histogram data so we can see how much PeerConnetion is used. 187 // Save histogram data so we can see how much PeerConnetion is used.
185 // The histogram counts the number of calls to the JS API 188 // The histogram counts the number of calls to the JS API
186 // webKitRTCPeerConnection. 189 // webKitRTCPeerConnection.
187 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION); 190 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION);
188 191
189 return new RTCPeerConnectionHandler(client, this); 192 return new RTCPeerConnectionHandler(client, this, GetWebRtcSignalingThread());
190 } 193 }
191 194
192 bool PeerConnectionDependencyFactory::InitializeMediaStreamAudioSource( 195 bool PeerConnectionDependencyFactory::InitializeMediaStreamAudioSource(
193 int render_view_id, 196 int render_view_id,
194 const blink::WebMediaConstraints& audio_constraints, 197 const blink::WebMediaConstraints& audio_constraints,
195 MediaStreamAudioSource* source_data) { 198 MediaStreamAudioSource* source_data) {
196 DVLOG(1) << "InitializeMediaStreamAudioSources()"; 199 DVLOG(1) << "InitializeMediaStreamAudioSources()";
197 200
198 // Do additional source initialization if the audio source is a valid 201 // Do additional source initialization if the audio source is a valid
199 // microphone or tab audio. 202 // microphone or tab audio.
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 CHECK(pc_factory_.get()); 269 CHECK(pc_factory_.get());
267 return pc_factory_; 270 return pc_factory_;
268 } 271 }
269 272
270 void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() { 273 void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() {
271 DCHECK(!pc_factory_.get()); 274 DCHECK(!pc_factory_.get());
272 DCHECK(!signaling_thread_); 275 DCHECK(!signaling_thread_);
273 DCHECK(!worker_thread_); 276 DCHECK(!worker_thread_);
274 DCHECK(!network_manager_); 277 DCHECK(!network_manager_);
275 DCHECK(!socket_factory_); 278 DCHECK(!socket_factory_);
279 DCHECK(!chrome_signaling_thread_.IsRunning());
276 DCHECK(!chrome_worker_thread_.IsRunning()); 280 DCHECK(!chrome_worker_thread_.IsRunning());
277 281
278 DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()"; 282 DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()";
279 283
284 // To allow sending to the signaling/worker threads.
280 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); 285 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
281 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); 286 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
282 signaling_thread_ = jingle_glue::JingleThreadWrapper::current();
283 CHECK(signaling_thread_);
284 287
288 CHECK(chrome_signaling_thread_.Start());
285 CHECK(chrome_worker_thread_.Start()); 289 CHECK(chrome_worker_thread_.Start());
286 290
287 base::WaitableEvent start_worker_event(true, false); 291 base::WaitableEvent start_worker_event(true, false);
288 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( 292 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
289 &PeerConnectionDependencyFactory::InitializeWorkerThread, 293 &PeerConnectionDependencyFactory::InitializeWorkerThread,
290 base::Unretained(this), 294 base::Unretained(this),
291 &worker_thread_, 295 &worker_thread_,
292 &start_worker_event)); 296 &start_worker_event));
293 start_worker_event.Wait();
294 CHECK(worker_thread_);
295 297
296 base::WaitableEvent create_network_manager_event(true, false); 298 base::WaitableEvent create_network_manager_event(true, false);
297 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( 299 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
298 &PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread, 300 &PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread,
299 base::Unretained(this), 301 base::Unretained(this),
300 &create_network_manager_event)); 302 &create_network_manager_event));
303
304 start_worker_event.Wait();
301 create_network_manager_event.Wait(); 305 create_network_manager_event.Wait();
302 306
303 socket_factory_.reset( 307 CHECK(worker_thread_);
304 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get()));
305 308
306 // Init SSL, which will be needed by PeerConnection. 309 // Init SSL, which will be needed by PeerConnection.
307 #if defined(USE_OPENSSL) 310 #if defined(USE_OPENSSL)
308 if (!rtc::InitializeSSL()) { 311 if (!rtc::InitializeSSL()) {
309 LOG(ERROR) << "Failed on InitializeSSL."; 312 LOG(ERROR) << "Failed on InitializeSSL.";
310 NOTREACHED(); 313 NOTREACHED();
311 return; 314 return;
312 } 315 }
313 #else 316 #else
314 // TODO(ronghuawu): Replace this call with InitializeSSL. 317 // TODO(ronghuawu): Replace this call with InitializeSSL.
315 net::EnsureNSSSSLInit(); 318 net::EnsureNSSSSLInit();
316 #endif 319 #endif
317 320
321 base::WaitableEvent start_signaling_event(true, false);
322 chrome_signaling_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
323 &PeerConnectionDependencyFactory::InitializeSignalingThread,
324 base::Unretained(this),
325 RenderThreadImpl::current()->GetGpuFactories(),
326 &start_signaling_event));
327
328 // TODO(xians): Remove the following code after kDisableAudioTrackProcessing
329 // is removed.
330 if (!MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled()) {
331 aec_dump_message_filter_ = AecDumpMessageFilter::Get();
332 // In unit tests not creating a message filter, |aec_dump_message_filter_|
333 // will be NULL. We can just ignore that. Other unit tests and browser tests
334 // ensure that we do get the filter when we should.
335 if (aec_dump_message_filter_.get())
336 aec_dump_message_filter_->AddDelegate(this);
337 }
338
339 start_signaling_event.Wait();
340 CHECK(signaling_thread_);
341 }
342
343 void PeerConnectionDependencyFactory::InitializeSignalingThread(
344 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories,
345 base::WaitableEvent* event) {
346 DCHECK(chrome_signaling_thread_.task_runner()->BelongsToCurrentThread());
347 DCHECK(worker_thread_);
348 DCHECK(p2p_socket_dispatcher_.get());
349
350 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
351 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
352 signaling_thread_ = jingle_glue::JingleThreadWrapper::current();
353
354 EnsureWebRtcAudioDeviceImpl();
355
356 socket_factory_.reset(
357 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get()));
358
318 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory; 359 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory;
319 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory; 360 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory;
320 361
321 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); 362 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
322 scoped_refptr<media::GpuVideoAcceleratorFactories> gpu_factories = 363 if (gpu_factories.get()) {
323 RenderThreadImpl::current()->GetGpuFactories(); 364 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWDecoding))
324 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWDecoding)) {
325 if (gpu_factories.get())
326 decoder_factory.reset(new RTCVideoDecoderFactory(gpu_factories)); 365 decoder_factory.reset(new RTCVideoDecoderFactory(gpu_factories));
327 }
328 366
329 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWEncoding)) { 367 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWEncoding))
330 if (gpu_factories.get())
331 encoder_factory.reset(new RTCVideoEncoderFactory(gpu_factories)); 368 encoder_factory.reset(new RTCVideoEncoderFactory(gpu_factories));
332 } 369 }
333 370
334 #if defined(OS_ANDROID) 371 #if defined(OS_ANDROID)
335 if (!media::MediaCodecBridge::SupportsSetParameters()) 372 if (!media::MediaCodecBridge::SupportsSetParameters())
336 encoder_factory.reset(); 373 encoder_factory.reset();
337 #endif 374 #endif
338 375
339 EnsureWebRtcAudioDeviceImpl(); 376 pc_factory_ = webrtc::CreatePeerConnectionFactory(
377 worker_thread_, signaling_thread_, audio_device_.get(),
378 encoder_factory.release(), decoder_factory.release());
379 CHECK(pc_factory_.get());
340 380
341 scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory(
342 webrtc::CreatePeerConnectionFactory(worker_thread_,
343 signaling_thread_,
344 audio_device_.get(),
345 encoder_factory.release(),
346 decoder_factory.release()));
347 CHECK(factory.get());
348
349 pc_factory_ = factory;
350 webrtc::PeerConnectionFactoryInterface::Options factory_options; 381 webrtc::PeerConnectionFactoryInterface::Options factory_options;
351 factory_options.disable_sctp_data_channels = false; 382 factory_options.disable_sctp_data_channels = false;
352 factory_options.disable_encryption = 383 factory_options.disable_encryption =
353 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption); 384 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption);
354 pc_factory_->SetOptions(factory_options); 385 pc_factory_->SetOptions(factory_options);
355 386
356 // TODO(xians): Remove the following code after kDisableAudioTrackProcessing 387 event->Signal();
357 // is removed.
358 if (!MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled()) {
359 aec_dump_message_filter_ = AecDumpMessageFilter::Get();
360 // In unit tests not creating a message filter, |aec_dump_message_filter_|
361 // will be NULL. We can just ignore that. Other unit tests and browser tests
362 // ensure that we do get the filter when we should.
363 if (aec_dump_message_filter_.get())
364 aec_dump_message_filter_->AddDelegate(this);
365 }
366 } 388 }
367 389
368 bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() { 390 bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() {
369 return pc_factory_.get() != NULL; 391 return pc_factory_.get() != NULL;
370 } 392 }
371 393
372 scoped_refptr<webrtc::PeerConnectionInterface> 394 scoped_refptr<webrtc::PeerConnectionInterface>
373 PeerConnectionDependencyFactory::CreatePeerConnection( 395 PeerConnectionDependencyFactory::CreatePeerConnection(
374 const webrtc::PeerConnectionInterface::RTCConfiguration& config, 396 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
375 const webrtc::MediaConstraintsInterface* constraints, 397 const webrtc::MediaConstraintsInterface* constraints,
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 DCHECK_GE(render_view_id, 0); 615 DCHECK_GE(render_view_id, 0);
594 616
595 EnsureWebRtcAudioDeviceImpl(); 617 EnsureWebRtcAudioDeviceImpl();
596 DCHECK(GetWebRtcAudioDevice()); 618 DCHECK(GetWebRtcAudioDevice());
597 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info, 619 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info,
598 constraints, 620 constraints,
599 GetWebRtcAudioDevice(), 621 GetWebRtcAudioDevice(),
600 audio_source); 622 audio_source);
601 } 623 }
602 624
603 void PeerConnectionDependencyFactory::AddNativeAudioTrackToBlinkTrack(
604 webrtc::MediaStreamTrackInterface* native_track,
605 const blink::WebMediaStreamTrack& webkit_track,
606 bool is_local_track) {
607 DCHECK(!webkit_track.isNull() && !webkit_track.extraData());
608 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio,
609 webkit_track.source().type());
610 blink::WebMediaStreamTrack track = webkit_track;
611
612 DVLOG(1) << "AddNativeTrackToBlinkTrack() audio";
613 track.setExtraData(
614 new MediaStreamTrack(
615 static_cast<webrtc::AudioTrackInterface*>(native_track),
616 is_local_track));
617 }
618
619 scoped_refptr<base::MessageLoopProxy> 625 scoped_refptr<base::MessageLoopProxy>
620 PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const { 626 PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const {
621 DCHECK(CalledOnValidThread()); 627 DCHECK(CalledOnValidThread());
622 return chrome_worker_thread_.message_loop_proxy(); 628 return chrome_worker_thread_.message_loop_proxy();
623 } 629 }
624 630
625 scoped_refptr<base::MessageLoopProxy> 631 scoped_refptr<base::MessageLoopProxy>
626 PeerConnectionDependencyFactory::GetWebRtcSignalingThread() const { 632 PeerConnectionDependencyFactory::GetWebRtcSignalingThread() const {
627 DCHECK(CalledOnValidThread()); 633 DCHECK(CalledOnValidThread());
628 return RenderThreadImpl::current()->GetMessageLoop()->message_loop_proxy(); 634 return chrome_signaling_thread_.message_loop_proxy();
629 } 635 }
630 636
631 void PeerConnectionDependencyFactory::OnAecDumpFile( 637 void PeerConnectionDependencyFactory::OnAecDumpFile(
632 const IPC::PlatformFileForTransit& file_handle) { 638 const IPC::PlatformFileForTransit& file_handle) {
633 DCHECK(CalledOnValidThread()); 639 DCHECK(CalledOnValidThread());
634 DCHECK(!MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled()); 640 DCHECK(!MediaStreamAudioProcessor::IsAudioTrackProcessingEnabled());
635 DCHECK(PeerConnectionFactoryCreated()); 641 DCHECK(PeerConnectionFactoryCreated());
636 642
637 base::File file = IPC::PlatformFileForTransitToFile(file_handle); 643 base::File file = IPC::PlatformFileForTransitToFile(file_handle);
638 DCHECK(file.IsValid()); 644 DCHECK(file.IsValid());
(...skipping 16 matching lines...) Expand all
655 } 661 }
656 662
657 void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() { 663 void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() {
658 if (audio_device_.get()) 664 if (audio_device_.get())
659 return; 665 return;
660 666
661 audio_device_ = new WebRtcAudioDeviceImpl(); 667 audio_device_ = new WebRtcAudioDeviceImpl();
662 } 668 }
663 669
664 } // namespace content 670 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698