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
3 // found in the LICENSE file.
5 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "content/common/media/media_stream_messages.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/common/renderer_preferences.h"
16 #include "content/renderer/media/media_stream.h"
17 #include "content/renderer/media/media_stream_audio_processor.h"
18 #include "content/renderer/media/media_stream_audio_processor_options.h"
19 #include "content/renderer/media/media_stream_audio_source.h"
20 #include "content/renderer/media/media_stream_video_source.h"
21 #include "content/renderer/media/media_stream_video_track.h"
22 #include "content/renderer/media/peer_connection_identity_service.h"
23 #include "content/renderer/media/rtc_media_constraints.h"
24 #include "content/renderer/media/rtc_peer_connection_handler.h"
25 #include "content/renderer/media/rtc_video_decoder_factory.h"
26 #include "content/renderer/media/rtc_video_encoder_factory.h"
27 #include "content/renderer/media/webaudio_capturer_source.h"
28 #include "content/renderer/media/webrtc/stun_field_trial.h"
29 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
30 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h"
31 #include "content/renderer/media/webrtc_audio_device_impl.h"
32 #include "content/renderer/media/webrtc_local_audio_track.h"
33 #include "content/renderer/media/webrtc_logging.h"
34 #include "content/renderer/media/webrtc_uma_histograms.h"
35 #include "content/renderer/p2p/ipc_network_manager.h"
36 #include "content/renderer/p2p/ipc_socket_factory.h"
37 #include "content/renderer/p2p/port_allocator.h"
38 #include "content/renderer/render_thread_impl.h"
39 #include "content/renderer/render_view_impl.h"
40 #include "jingle/glue/thread_wrapper.h"
41 #include "media/renderers/gpu_video_accelerator_factories.h"
42 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
43 #include "third_party/WebKit/public/platform/WebMediaStream.h"
44 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
45 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
46 #include "third_party/WebKit/public/platform/WebURL.h"
47 #include "third_party/WebKit/public/web/WebDocument.h"
48 #include "third_party/WebKit/public/web/WebFrame.h"
49 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
51 #if defined(USE_OPENSSL)
52 #include "third_party/webrtc/base/ssladapter.h"
54 #include "net/socket/nss_ssl_util.h"
57 #if defined(OS_ANDROID)
58 #include "media/base/android/media_codec_bridge.h"
63 // Map of corresponding media constraints and platform effects.
65 const char* constraint
;
66 const media::AudioParameters::PlatformEffectsMask effect
;
67 } const kConstraintEffectMap
[] = {
68 { content::kMediaStreamAudioDucking
,
69 media::AudioParameters::DUCKING
},
70 { webrtc::MediaConstraintsInterface::kGoogEchoCancellation
,
71 media::AudioParameters::ECHO_CANCELLER
},
74 // If any platform effects are available, check them against the constraints.
75 // Disable effects to match false constraints, but if a constraint is true, set
76 // the constraint to false to later disable the software effect.
78 // This function may modify both |constraints| and |effects|.
79 void HarmonizeConstraintsAndEffects(RTCMediaConstraints
* constraints
,
81 if (*effects
!= media::AudioParameters::NO_EFFECTS
) {
82 for (size_t i
= 0; i
< arraysize(kConstraintEffectMap
); ++i
) {
84 size_t is_mandatory
= 0;
85 if (!webrtc::FindConstraint(constraints
,
86 kConstraintEffectMap
[i
].constraint
,
88 &is_mandatory
) || !value
) {
89 // If the constraint is false, or does not exist, disable the platform
91 *effects
&= ~kConstraintEffectMap
[i
].effect
;
92 DVLOG(1) << "Disabling platform effect: "
93 << kConstraintEffectMap
[i
].effect
;
94 } else if (*effects
& kConstraintEffectMap
[i
].effect
) {
95 // If the constraint is true, leave the platform effect enabled, and
96 // set the constraint to false to later disable the software effect.
98 constraints
->AddMandatory(kConstraintEffectMap
[i
].constraint
,
99 webrtc::MediaConstraintsInterface::kValueFalse
, true);
101 constraints
->AddOptional(kConstraintEffectMap
[i
].constraint
,
102 webrtc::MediaConstraintsInterface::kValueFalse
, true);
104 DVLOG(1) << "Disabling constraint: "
105 << kConstraintEffectMap
[i
].constraint
;
106 } else if (kConstraintEffectMap
[i
].effect
==
107 media::AudioParameters::DUCKING
&& value
&& !is_mandatory
) {
108 // Special handling of the DUCKING flag that sets the optional
109 // constraint to |false| to match what the device will support.
110 constraints
->AddOptional(kConstraintEffectMap
[i
].constraint
,
111 webrtc::MediaConstraintsInterface::kValueFalse
, true);
112 // No need to modify |effects| since the ducking flag is already off.
113 DCHECK((*effects
& media::AudioParameters::DUCKING
) == 0);
119 class P2PPortAllocatorFactory
: public webrtc::PortAllocatorFactoryInterface
{
121 P2PPortAllocatorFactory(P2PSocketDispatcher
* socket_dispatcher
,
122 rtc::NetworkManager
* network_manager
,
123 rtc::PacketSocketFactory
* socket_factory
,
125 bool enable_multiple_routes
)
126 : socket_dispatcher_(socket_dispatcher
),
127 network_manager_(network_manager
),
128 socket_factory_(socket_factory
),
130 enable_multiple_routes_(enable_multiple_routes
) {}
132 cricket::PortAllocator
* CreatePortAllocator(
133 const std::vector
<StunConfiguration
>& stun_servers
,
134 const std::vector
<TurnConfiguration
>& turn_configurations
) override
{
135 P2PPortAllocator::Config config
;
136 for (size_t i
= 0; i
< stun_servers
.size(); ++i
) {
137 config
.stun_servers
.insert(rtc::SocketAddress(
138 stun_servers
[i
].server
.hostname(),
139 stun_servers
[i
].server
.port()));
141 for (size_t i
= 0; i
< turn_configurations
.size(); ++i
) {
142 P2PPortAllocator::Config::RelayServerConfig relay_config
;
143 relay_config
.server_address
= turn_configurations
[i
].server
.hostname();
144 relay_config
.port
= turn_configurations
[i
].server
.port();
145 relay_config
.username
= turn_configurations
[i
].username
;
146 relay_config
.password
= turn_configurations
[i
].password
;
147 relay_config
.transport_type
= turn_configurations
[i
].transport_type
;
148 relay_config
.secure
= turn_configurations
[i
].secure
;
149 config
.relays
.push_back(relay_config
);
151 // Use turn servers as stun servers.
152 config
.stun_servers
.insert(rtc::SocketAddress(
153 turn_configurations
[i
].server
.hostname(),
154 turn_configurations
[i
].server
.port()));
156 config
.enable_multiple_routes
= enable_multiple_routes_
;
158 return new P2PPortAllocator(
159 socket_dispatcher_
.get(), network_manager_
,
160 socket_factory_
, config
, origin_
);
164 ~P2PPortAllocatorFactory() override
{}
167 scoped_refptr
<P2PSocketDispatcher
> socket_dispatcher_
;
168 // |network_manager_| and |socket_factory_| are a weak references, owned by
169 // PeerConnectionDependencyFactory.
170 rtc::NetworkManager
* network_manager_
;
171 rtc::PacketSocketFactory
* socket_factory_
;
172 // The origin URL of the WebFrame that created the
173 // P2PPortAllocatorFactory.
175 // When false, only 'any' address (all 0s) will be bound for address
177 bool enable_multiple_routes_
;
180 PeerConnectionDependencyFactory::PeerConnectionDependencyFactory(
181 P2PSocketDispatcher
* p2p_socket_dispatcher
)
182 : network_manager_(NULL
),
183 p2p_socket_dispatcher_(p2p_socket_dispatcher
),
184 signaling_thread_(NULL
),
185 worker_thread_(NULL
),
186 chrome_signaling_thread_("Chrome_libJingle_Signaling"),
187 chrome_worker_thread_("Chrome_libJingle_WorkerThread") {
188 TryScheduleStunProbeTrial();
191 PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() {
192 DVLOG(1) << "~PeerConnectionDependencyFactory()";
193 DCHECK(pc_factory_
== NULL
);
196 blink::WebRTCPeerConnectionHandler
*
197 PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler(
198 blink::WebRTCPeerConnectionHandlerClient
* client
) {
199 // Save histogram data so we can see how much PeerConnetion is used.
200 // The histogram counts the number of calls to the JS API
201 // webKitRTCPeerConnection.
202 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION
);
204 return new RTCPeerConnectionHandler(client
, this);
207 bool PeerConnectionDependencyFactory::InitializeMediaStreamAudioSource(
209 const blink::WebMediaConstraints
& audio_constraints
,
210 MediaStreamAudioSource
* source_data
) {
211 DVLOG(1) << "InitializeMediaStreamAudioSources()";
213 // Do additional source initialization if the audio source is a valid
214 // microphone or tab audio.
215 RTCMediaConstraints
native_audio_constraints(audio_constraints
);
216 MediaAudioConstraints::ApplyFixedAudioConstraints(&native_audio_constraints
);
218 StreamDeviceInfo device_info
= source_data
->device_info();
219 RTCMediaConstraints constraints
= native_audio_constraints
;
220 // May modify both |constraints| and |effects|.
221 HarmonizeConstraintsAndEffects(&constraints
,
222 &device_info
.device
.input
.effects
);
224 scoped_refptr
<WebRtcAudioCapturer
> capturer(CreateAudioCapturer(
225 render_frame_id
, device_info
, audio_constraints
, source_data
));
226 if (!capturer
.get()) {
227 const std::string log_string
=
228 "PCDF::InitializeMediaStreamAudioSource: fails to create capturer";
229 WebRtcLogMessage(log_string
);
230 DVLOG(1) << log_string
;
231 // TODO(xians): Don't we need to check if source_observer is observing
232 // something? If not, then it looks like we have a leak here.
233 // OTOH, if it _is_ observing something, then the callback might
234 // be called multiple times which is likely also a bug.
237 source_data
->SetAudioCapturer(capturer
.get());
239 // Creates a LocalAudioSource object which holds audio options.
240 // TODO(xians): The option should apply to the track instead of the source.
241 // TODO(perkj): Move audio constraints parsing to Chrome.
242 // Currently there are a few constraints that are parsed by libjingle and
243 // the state is set to ended if parsing fails.
244 scoped_refptr
<webrtc::AudioSourceInterface
> rtc_source(
245 CreateLocalAudioSource(&constraints
).get());
246 if (rtc_source
->state() != webrtc::MediaSourceInterface::kLive
) {
247 DLOG(WARNING
) << "Failed to create rtc LocalAudioSource.";
250 source_data
->SetLocalAudioSource(rtc_source
.get());
254 WebRtcVideoCapturerAdapter
*
255 PeerConnectionDependencyFactory::CreateVideoCapturer(
256 bool is_screeencast
) {
257 // We need to make sure the libjingle thread wrappers have been created
258 // before we can use an instance of a WebRtcVideoCapturerAdapter. This is
259 // since the base class of WebRtcVideoCapturerAdapter is a
260 // cricket::VideoCapturer and it uses the libjingle thread wrappers.
261 if (!GetPcFactory().get())
263 return new WebRtcVideoCapturerAdapter(is_screeencast
);
266 scoped_refptr
<webrtc::VideoSourceInterface
>
267 PeerConnectionDependencyFactory::CreateVideoSource(
268 cricket::VideoCapturer
* capturer
,
269 const blink::WebMediaConstraints
& constraints
) {
270 RTCMediaConstraints
webrtc_constraints(constraints
);
271 scoped_refptr
<webrtc::VideoSourceInterface
> source
=
272 GetPcFactory()->CreateVideoSource(capturer
, &webrtc_constraints
).get();
276 const scoped_refptr
<webrtc::PeerConnectionFactoryInterface
>&
277 PeerConnectionDependencyFactory::GetPcFactory() {
278 if (!pc_factory_
.get())
279 CreatePeerConnectionFactory();
280 CHECK(pc_factory_
.get());
285 void PeerConnectionDependencyFactory::WillDestroyCurrentMessageLoop() {
286 CleanupPeerConnectionFactory();
289 void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() {
290 DCHECK(!pc_factory_
.get());
291 DCHECK(!signaling_thread_
);
292 DCHECK(!worker_thread_
);
293 DCHECK(!network_manager_
);
294 DCHECK(!socket_factory_
);
295 DCHECK(!chrome_signaling_thread_
.IsRunning());
296 DCHECK(!chrome_worker_thread_
.IsRunning());
298 DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()";
300 base::MessageLoop::current()->AddDestructionObserver(this);
301 // To allow sending to the signaling/worker threads.
302 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
303 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
305 CHECK(chrome_signaling_thread_
.Start());
306 CHECK(chrome_worker_thread_
.Start());
308 base::WaitableEvent
start_worker_event(true, false);
309 chrome_worker_thread_
.task_runner()->PostTask(
311 base::Bind(&PeerConnectionDependencyFactory::InitializeWorkerThread
,
312 base::Unretained(this), &worker_thread_
, &start_worker_event
));
314 base::WaitableEvent
create_network_manager_event(true, false);
315 chrome_worker_thread_
.task_runner()->PostTask(
317 base::Bind(&PeerConnectionDependencyFactory::
318 CreateIpcNetworkManagerOnWorkerThread
,
319 base::Unretained(this), &create_network_manager_event
));
321 start_worker_event
.Wait();
322 create_network_manager_event
.Wait();
324 CHECK(worker_thread_
);
326 // Init SSL, which will be needed by PeerConnection.
327 #if defined(USE_OPENSSL)
328 if (!rtc::InitializeSSL()) {
329 LOG(ERROR
) << "Failed on InitializeSSL.";
334 // TODO(ronghuawu): Replace this call with InitializeSSL.
335 net::EnsureNSSSSLInit();
338 base::WaitableEvent
start_signaling_event(true, false);
339 chrome_signaling_thread_
.task_runner()->PostTask(
341 base::Bind(&PeerConnectionDependencyFactory::InitializeSignalingThread
,
342 base::Unretained(this),
343 RenderThreadImpl::current()->GetGpuFactories(),
344 &start_signaling_event
));
346 start_signaling_event
.Wait();
347 CHECK(signaling_thread_
);
350 void PeerConnectionDependencyFactory::InitializeSignalingThread(
351 const scoped_refptr
<media::GpuVideoAcceleratorFactories
>& gpu_factories
,
352 base::WaitableEvent
* event
) {
353 DCHECK(chrome_signaling_thread_
.task_runner()->BelongsToCurrentThread());
354 DCHECK(worker_thread_
);
355 DCHECK(p2p_socket_dispatcher_
.get());
357 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
358 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
359 signaling_thread_
= jingle_glue::JingleThreadWrapper::current();
361 EnsureWebRtcAudioDeviceImpl();
363 socket_factory_
.reset(
364 new IpcPacketSocketFactory(p2p_socket_dispatcher_
.get()));
366 scoped_ptr
<cricket::WebRtcVideoDecoderFactory
> decoder_factory
;
367 scoped_ptr
<cricket::WebRtcVideoEncoderFactory
> encoder_factory
;
369 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
370 if (gpu_factories
&& gpu_factories
->IsGpuVideoAcceleratorEnabled()) {
371 if (!cmd_line
->HasSwitch(switches::kDisableWebRtcHWDecoding
))
372 decoder_factory
.reset(new RTCVideoDecoderFactory(gpu_factories
));
374 if (!cmd_line
->HasSwitch(switches::kDisableWebRtcHWEncoding
))
375 encoder_factory
.reset(new RTCVideoEncoderFactory(gpu_factories
));
378 #if defined(OS_ANDROID)
379 if (!media::MediaCodecBridge::SupportsSetParameters())
380 encoder_factory
.reset();
383 pc_factory_
= webrtc::CreatePeerConnectionFactory(
384 worker_thread_
, signaling_thread_
, audio_device_
.get(),
385 encoder_factory
.release(), decoder_factory
.release());
386 CHECK(pc_factory_
.get());
388 webrtc::PeerConnectionFactoryInterface::Options factory_options
;
389 factory_options
.disable_sctp_data_channels
= false;
390 factory_options
.disable_encryption
=
391 cmd_line
->HasSwitch(switches::kDisableWebRtcEncryption
);
392 if (cmd_line
->HasSwitch(switches::kEnableWebRtcDtls12
))
393 factory_options
.ssl_max_version
= rtc::SSL_PROTOCOL_DTLS_12
;
394 pc_factory_
->SetOptions(factory_options
);
399 bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() {
400 return pc_factory_
.get() != NULL
;
403 scoped_refptr
<webrtc::PeerConnectionInterface
>
404 PeerConnectionDependencyFactory::CreatePeerConnection(
405 const webrtc::PeerConnectionInterface::RTCConfiguration
& config
,
406 const webrtc::MediaConstraintsInterface
* constraints
,
407 blink::WebFrame
* web_frame
,
408 webrtc::PeerConnectionObserver
* observer
) {
411 if (!GetPcFactory().get())
414 // Copy the flag from Preference associated with this WebFrame.
415 bool enable_multiple_routes
= true;
416 if (web_frame
&& web_frame
->view()) {
417 RenderViewImpl
* renderer_view_impl
=
418 RenderViewImpl::FromWebView(web_frame
->view());
419 if (renderer_view_impl
) {
420 enable_multiple_routes
= renderer_view_impl
->renderer_preferences()
421 .enable_webrtc_multiple_routes
;
425 scoped_refptr
<P2PPortAllocatorFactory
> pa_factory
=
426 new rtc::RefCountedObject
<P2PPortAllocatorFactory
>(
427 p2p_socket_dispatcher_
.get(), network_manager_
, socket_factory_
.get(),
428 GURL(web_frame
->document().url().spec()).GetOrigin(),
429 enable_multiple_routes
);
431 PeerConnectionIdentityService
* identity_service
=
432 new PeerConnectionIdentityService(
433 GURL(web_frame
->document().url().spec()).GetOrigin());
435 return GetPcFactory()->CreatePeerConnection(config
,
442 scoped_refptr
<webrtc::MediaStreamInterface
>
443 PeerConnectionDependencyFactory::CreateLocalMediaStream(
444 const std::string
& label
) {
445 return GetPcFactory()->CreateLocalMediaStream(label
).get();
448 scoped_refptr
<webrtc::AudioSourceInterface
>
449 PeerConnectionDependencyFactory::CreateLocalAudioSource(
450 const webrtc::MediaConstraintsInterface
* constraints
) {
451 scoped_refptr
<webrtc::AudioSourceInterface
> source
=
452 GetPcFactory()->CreateAudioSource(constraints
).get();
456 void PeerConnectionDependencyFactory::CreateLocalAudioTrack(
457 const blink::WebMediaStreamTrack
& track
) {
458 blink::WebMediaStreamSource source
= track
.source();
459 DCHECK_EQ(source
.type(), blink::WebMediaStreamSource::TypeAudio
);
460 MediaStreamAudioSource
* source_data
=
461 static_cast<MediaStreamAudioSource
*>(source
.extraData());
463 scoped_refptr
<WebAudioCapturerSource
> webaudio_source
;
465 if (source
.requiresAudioConsumer()) {
466 // We're adding a WebAudio MediaStream.
467 // Create a specific capturer for each WebAudio consumer.
468 webaudio_source
= CreateWebAudioSource(&source
);
470 static_cast<MediaStreamAudioSource
*>(source
.extraData());
472 // TODO(perkj): Implement support for sources from
473 // remote MediaStreams.
479 // Creates an adapter to hold all the libjingle objects.
480 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
481 WebRtcLocalAudioTrackAdapter::Create(track
.id().utf8(),
482 source_data
->local_audio_source()));
483 static_cast<webrtc::AudioTrackInterface
*>(adapter
.get())->set_enabled(
486 // TODO(xians): Merge |source| to the capturer(). We can't do this today
487 // because only one capturer() is supported while one |source| is created
488 // for each audio track.
489 scoped_ptr
<WebRtcLocalAudioTrack
> audio_track(new WebRtcLocalAudioTrack(
490 adapter
.get(), source_data
->GetAudioCapturer(), webaudio_source
.get()));
492 StartLocalAudioTrack(audio_track
.get());
494 // Pass the ownership of the native local audio track to the blink track.
495 blink::WebMediaStreamTrack writable_track
= track
;
496 writable_track
.setExtraData(audio_track
.release());
499 void PeerConnectionDependencyFactory::StartLocalAudioTrack(
500 WebRtcLocalAudioTrack
* audio_track
) {
501 // Start the audio track. This will hook the |audio_track| to the capturer
502 // as the sink of the audio, and only start the source of the capturer if
503 // it is the first audio track connecting to the capturer.
504 audio_track
->Start();
507 scoped_refptr
<WebAudioCapturerSource
>
508 PeerConnectionDependencyFactory::CreateWebAudioSource(
509 blink::WebMediaStreamSource
* source
) {
510 DVLOG(1) << "PeerConnectionDependencyFactory::CreateWebAudioSource()";
512 scoped_refptr
<WebAudioCapturerSource
>
513 webaudio_capturer_source(new WebAudioCapturerSource(*source
));
514 MediaStreamAudioSource
* source_data
= new MediaStreamAudioSource();
516 // Use the current default capturer for the WebAudio track so that the
517 // WebAudio track can pass a valid delay value and |need_audio_processing|
518 // flag to PeerConnection.
519 // TODO(xians): Remove this after moving APM to Chrome.
520 if (GetWebRtcAudioDevice()) {
521 source_data
->SetAudioCapturer(
522 GetWebRtcAudioDevice()->GetDefaultCapturer());
525 // Create a LocalAudioSource object which holds audio options.
526 // SetLocalAudioSource() affects core audio parts in third_party/Libjingle.
527 source_data
->SetLocalAudioSource(CreateLocalAudioSource(NULL
).get());
528 source
->setExtraData(source_data
);
530 // Replace the default source with WebAudio as source instead.
531 source
->addAudioConsumer(webaudio_capturer_source
.get());
533 return webaudio_capturer_source
;
536 scoped_refptr
<webrtc::VideoTrackInterface
>
537 PeerConnectionDependencyFactory::CreateLocalVideoTrack(
538 const std::string
& id
,
539 webrtc::VideoSourceInterface
* source
) {
540 return GetPcFactory()->CreateVideoTrack(id
, source
).get();
543 scoped_refptr
<webrtc::VideoTrackInterface
>
544 PeerConnectionDependencyFactory::CreateLocalVideoTrack(
545 const std::string
& id
, cricket::VideoCapturer
* capturer
) {
547 LOG(ERROR
) << "CreateLocalVideoTrack called with null VideoCapturer.";
551 // Create video source from the |capturer|.
552 scoped_refptr
<webrtc::VideoSourceInterface
> source
=
553 GetPcFactory()->CreateVideoSource(capturer
, NULL
).get();
555 // Create native track from the source.
556 return GetPcFactory()->CreateVideoTrack(id
, source
.get()).get();
559 webrtc::SessionDescriptionInterface
*
560 PeerConnectionDependencyFactory::CreateSessionDescription(
561 const std::string
& type
,
562 const std::string
& sdp
,
563 webrtc::SdpParseError
* error
) {
564 return webrtc::CreateSessionDescription(type
, sdp
, error
);
567 webrtc::IceCandidateInterface
*
568 PeerConnectionDependencyFactory::CreateIceCandidate(
569 const std::string
& sdp_mid
,
571 const std::string
& sdp
) {
572 return webrtc::CreateIceCandidate(sdp_mid
, sdp_mline_index
, sdp
, nullptr);
575 WebRtcAudioDeviceImpl
*
576 PeerConnectionDependencyFactory::GetWebRtcAudioDevice() {
577 return audio_device_
.get();
580 void PeerConnectionDependencyFactory::InitializeWorkerThread(
581 rtc::Thread
** thread
,
582 base::WaitableEvent
* event
) {
583 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
584 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
585 *thread
= jingle_glue::JingleThreadWrapper::current();
589 void PeerConnectionDependencyFactory::TryScheduleStunProbeTrial() {
590 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
592 if (!cmd_line
->HasSwitch(switches::kWebRtcStunProbeTrialParameter
))
597 // The underneath IPC channel has to be connected before sending any IPC
599 if (!p2p_socket_dispatcher_
->connected()) {
600 base::MessageLoop::current()->PostDelayedTask(
602 base::Bind(&PeerConnectionDependencyFactory::TryScheduleStunProbeTrial
,
603 base::Unretained(this)),
604 base::TimeDelta::FromSeconds(1));
608 const std::string params
=
609 cmd_line
->GetSwitchValueASCII(switches::kWebRtcStunProbeTrialParameter
);
611 chrome_worker_thread_
.task_runner()->PostDelayedTask(
614 &PeerConnectionDependencyFactory::StartStunProbeTrialOnWorkerThread
,
615 base::Unretained(this), params
),
616 base::TimeDelta::FromMilliseconds(kExperimentStartDelayMs
));
619 void PeerConnectionDependencyFactory::StartStunProbeTrialOnWorkerThread(
620 const std::string
& params
) {
621 DCHECK(chrome_worker_thread_
.task_runner()->BelongsToCurrentThread());
622 rtc::NetworkManager::NetworkList networks
;
623 network_manager_
->GetNetworks(&networks
);
624 stun_prober_
= StartStunProbeTrial(networks
, params
, socket_factory_
.get());
627 void PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread(
628 base::WaitableEvent
* event
) {
629 DCHECK(chrome_worker_thread_
.task_runner()->BelongsToCurrentThread());
630 network_manager_
= new IpcNetworkManager(p2p_socket_dispatcher_
.get());
634 void PeerConnectionDependencyFactory::DeleteIpcNetworkManager() {
635 DCHECK(chrome_worker_thread_
.task_runner()->BelongsToCurrentThread());
636 delete network_manager_
;
637 network_manager_
= NULL
;
640 void PeerConnectionDependencyFactory::CleanupPeerConnectionFactory() {
641 DVLOG(1) << "PeerConnectionDependencyFactory::CleanupPeerConnectionFactory()";
643 if (network_manager_
) {
644 // The network manager needs to free its resources on the thread they were
645 // created, which is the worked thread.
646 if (chrome_worker_thread_
.IsRunning()) {
647 chrome_worker_thread_
.task_runner()->PostTask(
649 base::Bind(&PeerConnectionDependencyFactory::DeleteIpcNetworkManager
,
650 base::Unretained(this)));
651 // Stopping the thread will wait until all tasks have been
652 // processed before returning. We wait for the above task to finish before
653 // letting the the function continue to avoid any potential race issues.
654 chrome_worker_thread_
.Stop();
656 NOTREACHED() << "Worker thread not running.";
661 scoped_refptr
<WebRtcAudioCapturer
>
662 PeerConnectionDependencyFactory::CreateAudioCapturer(
664 const StreamDeviceInfo
& device_info
,
665 const blink::WebMediaConstraints
& constraints
,
666 MediaStreamAudioSource
* audio_source
) {
667 // TODO(xians): Handle the cases when gUM is called without a proper render
668 // view, for example, by an extension.
669 DCHECK_GE(render_frame_id
, 0);
671 EnsureWebRtcAudioDeviceImpl();
672 DCHECK(GetWebRtcAudioDevice());
673 return WebRtcAudioCapturer::CreateCapturer(
674 render_frame_id
, device_info
, constraints
, GetWebRtcAudioDevice(),
678 scoped_refptr
<base::SingleThreadTaskRunner
>
679 PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const {
680 DCHECK(CalledOnValidThread());
681 return chrome_worker_thread_
.IsRunning() ? chrome_worker_thread_
.task_runner()
685 scoped_refptr
<base::SingleThreadTaskRunner
>
686 PeerConnectionDependencyFactory::GetWebRtcSignalingThread() const {
687 DCHECK(CalledOnValidThread());
688 return chrome_signaling_thread_
.IsRunning()
689 ? chrome_signaling_thread_
.task_runner()
693 void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() {
694 if (audio_device_
.get())
697 audio_device_
= new WebRtcAudioDeviceImpl();
700 } // namespace content