[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / renderer / media / webrtc / peer_connection_dependency_factory.cc
blobd9f485bcb363823c225743d6f5384ba80f696748
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"
7 #include <vector>
9 #include "base/command_line.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "content/common/media/media_stream_messages.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/renderer_preferences.h"
15 #include "content/renderer/media/media_stream.h"
16 #include "content/renderer/media/media_stream_audio_processor.h"
17 #include "content/renderer/media/media_stream_audio_processor_options.h"
18 #include "content/renderer/media/media_stream_audio_source.h"
19 #include "content/renderer/media/media_stream_video_source.h"
20 #include "content/renderer/media/media_stream_video_track.h"
21 #include "content/renderer/media/peer_connection_identity_service.h"
22 #include "content/renderer/media/rtc_media_constraints.h"
23 #include "content/renderer/media/rtc_peer_connection_handler.h"
24 #include "content/renderer/media/rtc_video_decoder_factory.h"
25 #include "content/renderer/media/rtc_video_encoder_factory.h"
26 #include "content/renderer/media/webaudio_capturer_source.h"
27 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
28 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h"
29 #include "content/renderer/media/webrtc_audio_device_impl.h"
30 #include "content/renderer/media/webrtc_local_audio_track.h"
31 #include "content/renderer/media/webrtc_logging.h"
32 #include "content/renderer/media/webrtc_uma_histograms.h"
33 #include "content/renderer/p2p/ipc_network_manager.h"
34 #include "content/renderer/p2p/ipc_socket_factory.h"
35 #include "content/renderer/p2p/port_allocator.h"
36 #include "content/renderer/render_thread_impl.h"
37 #include "content/renderer/render_view_impl.h"
38 #include "jingle/glue/thread_wrapper.h"
39 #include "media/filters/gpu_video_accelerator_factories.h"
40 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
41 #include "third_party/WebKit/public/platform/WebMediaStream.h"
42 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
43 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
44 #include "third_party/WebKit/public/platform/WebURL.h"
45 #include "third_party/WebKit/public/web/WebDocument.h"
46 #include "third_party/WebKit/public/web/WebFrame.h"
47 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
49 #if defined(USE_OPENSSL)
50 #include "third_party/webrtc/base/ssladapter.h"
51 #else
52 #include "net/socket/nss_ssl_util.h"
53 #endif
55 #if defined(OS_ANDROID)
56 #include "media/base/android/media_codec_bridge.h"
57 #endif
59 namespace content {
61 // Map of corresponding media constraints and platform effects.
62 struct {
63 const char* constraint;
64 const media::AudioParameters::PlatformEffectsMask effect;
65 } const kConstraintEffectMap[] = {
66 { content::kMediaStreamAudioDucking,
67 media::AudioParameters::DUCKING },
68 { webrtc::MediaConstraintsInterface::kEchoCancellation,
69 media::AudioParameters::ECHO_CANCELLER },
72 // If any platform effects are available, check them against the constraints.
73 // Disable effects to match false constraints, but if a constraint is true, set
74 // the constraint to false to later disable the software effect.
76 // This function may modify both |constraints| and |effects|.
77 void HarmonizeConstraintsAndEffects(RTCMediaConstraints* constraints,
78 int* effects) {
79 if (*effects != media::AudioParameters::NO_EFFECTS) {
80 for (size_t i = 0; i < arraysize(kConstraintEffectMap); ++i) {
81 bool value;
82 size_t is_mandatory = 0;
83 if (!webrtc::FindConstraint(constraints,
84 kConstraintEffectMap[i].constraint,
85 &value,
86 &is_mandatory) || !value) {
87 // If the constraint is false, or does not exist, disable the platform
88 // effect.
89 *effects &= ~kConstraintEffectMap[i].effect;
90 DVLOG(1) << "Disabling platform effect: "
91 << kConstraintEffectMap[i].effect;
92 } else if (*effects & kConstraintEffectMap[i].effect) {
93 // If the constraint is true, leave the platform effect enabled, and
94 // set the constraint to false to later disable the software effect.
95 if (is_mandatory) {
96 constraints->AddMandatory(kConstraintEffectMap[i].constraint,
97 webrtc::MediaConstraintsInterface::kValueFalse, true);
98 } else {
99 constraints->AddOptional(kConstraintEffectMap[i].constraint,
100 webrtc::MediaConstraintsInterface::kValueFalse, true);
102 DVLOG(1) << "Disabling constraint: "
103 << kConstraintEffectMap[i].constraint;
104 } else if (kConstraintEffectMap[i].effect ==
105 media::AudioParameters::DUCKING && value && !is_mandatory) {
106 // Special handling of the DUCKING flag that sets the optional
107 // constraint to |false| to match what the device will support.
108 constraints->AddOptional(kConstraintEffectMap[i].constraint,
109 webrtc::MediaConstraintsInterface::kValueFalse, true);
110 // No need to modify |effects| since the ducking flag is already off.
111 DCHECK((*effects & media::AudioParameters::DUCKING) == 0);
117 class P2PPortAllocatorFactory : public webrtc::PortAllocatorFactoryInterface {
118 public:
119 P2PPortAllocatorFactory(P2PSocketDispatcher* socket_dispatcher,
120 rtc::NetworkManager* network_manager,
121 rtc::PacketSocketFactory* socket_factory,
122 bool enable_multiple_routes)
123 : socket_dispatcher_(socket_dispatcher),
124 network_manager_(network_manager),
125 socket_factory_(socket_factory),
126 enable_multiple_routes_(enable_multiple_routes) {}
128 cricket::PortAllocator* CreatePortAllocator(
129 const std::vector<StunConfiguration>& stun_servers,
130 const std::vector<TurnConfiguration>& turn_configurations) override {
131 P2PPortAllocator::Config config;
132 for (size_t i = 0; i < stun_servers.size(); ++i) {
133 config.stun_servers.insert(rtc::SocketAddress(
134 stun_servers[i].server.hostname(),
135 stun_servers[i].server.port()));
137 for (size_t i = 0; i < turn_configurations.size(); ++i) {
138 P2PPortAllocator::Config::RelayServerConfig relay_config;
139 relay_config.server_address = turn_configurations[i].server.hostname();
140 relay_config.port = turn_configurations[i].server.port();
141 relay_config.username = turn_configurations[i].username;
142 relay_config.password = turn_configurations[i].password;
143 relay_config.transport_type = turn_configurations[i].transport_type;
144 relay_config.secure = turn_configurations[i].secure;
145 config.relays.push_back(relay_config);
147 // Use turn servers as stun servers.
148 config.stun_servers.insert(rtc::SocketAddress(
149 turn_configurations[i].server.hostname(),
150 turn_configurations[i].server.port()));
152 config.enable_multiple_routes = enable_multiple_routes_;
154 return new P2PPortAllocator(
155 socket_dispatcher_.get(), network_manager_, socket_factory_, config);
158 protected:
159 ~P2PPortAllocatorFactory() override {}
161 private:
162 scoped_refptr<P2PSocketDispatcher> socket_dispatcher_;
163 // |network_manager_| and |socket_factory_| are a weak references, owned by
164 // PeerConnectionDependencyFactory.
165 rtc::NetworkManager* network_manager_;
166 rtc::PacketSocketFactory* socket_factory_;
168 // When false, only 'any' address (all 0s) will be bound for address
169 // discovery.
170 bool enable_multiple_routes_;
173 PeerConnectionDependencyFactory::PeerConnectionDependencyFactory(
174 P2PSocketDispatcher* p2p_socket_dispatcher)
175 : network_manager_(NULL),
176 p2p_socket_dispatcher_(p2p_socket_dispatcher),
177 signaling_thread_(NULL),
178 worker_thread_(NULL),
179 chrome_signaling_thread_("Chrome_libJingle_Signaling"),
180 chrome_worker_thread_("Chrome_libJingle_WorkerThread") {
183 PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() {
184 DVLOG(1) << "~PeerConnectionDependencyFactory()";
185 DCHECK(pc_factory_ == NULL);
188 blink::WebRTCPeerConnectionHandler*
189 PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler(
190 blink::WebRTCPeerConnectionHandlerClient* client) {
191 // Save histogram data so we can see how much PeerConnetion is used.
192 // The histogram counts the number of calls to the JS API
193 // webKitRTCPeerConnection.
194 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION);
196 return new RTCPeerConnectionHandler(client, this);
199 bool PeerConnectionDependencyFactory::InitializeMediaStreamAudioSource(
200 int render_view_id,
201 const blink::WebMediaConstraints& audio_constraints,
202 MediaStreamAudioSource* source_data) {
203 DVLOG(1) << "InitializeMediaStreamAudioSources()";
205 // Do additional source initialization if the audio source is a valid
206 // microphone or tab audio.
207 RTCMediaConstraints native_audio_constraints(audio_constraints);
208 MediaAudioConstraints::ApplyFixedAudioConstraints(&native_audio_constraints);
210 StreamDeviceInfo device_info = source_data->device_info();
211 RTCMediaConstraints constraints = native_audio_constraints;
212 // May modify both |constraints| and |effects|.
213 HarmonizeConstraintsAndEffects(&constraints,
214 &device_info.device.input.effects);
216 scoped_refptr<WebRtcAudioCapturer> capturer(
217 CreateAudioCapturer(render_view_id, device_info, audio_constraints,
218 source_data));
219 if (!capturer.get()) {
220 const std::string log_string =
221 "PCDF::InitializeMediaStreamAudioSource: fails to create capturer";
222 WebRtcLogMessage(log_string);
223 DVLOG(1) << log_string;
224 // TODO(xians): Don't we need to check if source_observer is observing
225 // something? If not, then it looks like we have a leak here.
226 // OTOH, if it _is_ observing something, then the callback might
227 // be called multiple times which is likely also a bug.
228 return false;
230 source_data->SetAudioCapturer(capturer.get());
232 // Creates a LocalAudioSource object which holds audio options.
233 // TODO(xians): The option should apply to the track instead of the source.
234 // TODO(perkj): Move audio constraints parsing to Chrome.
235 // Currently there are a few constraints that are parsed by libjingle and
236 // the state is set to ended if parsing fails.
237 scoped_refptr<webrtc::AudioSourceInterface> rtc_source(
238 CreateLocalAudioSource(&constraints).get());
239 if (rtc_source->state() != webrtc::MediaSourceInterface::kLive) {
240 DLOG(WARNING) << "Failed to create rtc LocalAudioSource.";
241 return false;
243 source_data->SetLocalAudioSource(rtc_source.get());
244 return true;
247 WebRtcVideoCapturerAdapter*
248 PeerConnectionDependencyFactory::CreateVideoCapturer(
249 bool is_screeencast) {
250 // We need to make sure the libjingle thread wrappers have been created
251 // before we can use an instance of a WebRtcVideoCapturerAdapter. This is
252 // since the base class of WebRtcVideoCapturerAdapter is a
253 // cricket::VideoCapturer and it uses the libjingle thread wrappers.
254 if (!GetPcFactory().get())
255 return NULL;
256 return new WebRtcVideoCapturerAdapter(is_screeencast);
259 scoped_refptr<webrtc::VideoSourceInterface>
260 PeerConnectionDependencyFactory::CreateVideoSource(
261 cricket::VideoCapturer* capturer,
262 const blink::WebMediaConstraints& constraints) {
263 RTCMediaConstraints webrtc_constraints(constraints);
264 scoped_refptr<webrtc::VideoSourceInterface> source =
265 GetPcFactory()->CreateVideoSource(capturer, &webrtc_constraints).get();
266 return source;
269 const scoped_refptr<webrtc::PeerConnectionFactoryInterface>&
270 PeerConnectionDependencyFactory::GetPcFactory() {
271 if (!pc_factory_.get())
272 CreatePeerConnectionFactory();
273 CHECK(pc_factory_.get());
274 return pc_factory_;
278 void PeerConnectionDependencyFactory::WillDestroyCurrentMessageLoop() {
279 CleanupPeerConnectionFactory();
282 void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() {
283 DCHECK(!pc_factory_.get());
284 DCHECK(!signaling_thread_);
285 DCHECK(!worker_thread_);
286 DCHECK(!network_manager_);
287 DCHECK(!socket_factory_);
288 DCHECK(!chrome_signaling_thread_.IsRunning());
289 DCHECK(!chrome_worker_thread_.IsRunning());
291 DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()";
293 base::MessageLoop::current()->AddDestructionObserver(this);
294 // To allow sending to the signaling/worker threads.
295 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
296 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
298 CHECK(chrome_signaling_thread_.Start());
299 CHECK(chrome_worker_thread_.Start());
301 base::WaitableEvent start_worker_event(true, false);
302 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
303 &PeerConnectionDependencyFactory::InitializeWorkerThread,
304 base::Unretained(this),
305 &worker_thread_,
306 &start_worker_event));
308 base::WaitableEvent create_network_manager_event(true, false);
309 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
310 &PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread,
311 base::Unretained(this),
312 &create_network_manager_event));
314 start_worker_event.Wait();
315 create_network_manager_event.Wait();
317 CHECK(worker_thread_);
319 // Init SSL, which will be needed by PeerConnection.
320 #if defined(USE_OPENSSL)
321 if (!rtc::InitializeSSL()) {
322 LOG(ERROR) << "Failed on InitializeSSL.";
323 NOTREACHED();
324 return;
326 #else
327 // TODO(ronghuawu): Replace this call with InitializeSSL.
328 net::EnsureNSSSSLInit();
329 #endif
331 base::WaitableEvent start_signaling_event(true, false);
332 chrome_signaling_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
333 &PeerConnectionDependencyFactory::InitializeSignalingThread,
334 base::Unretained(this),
335 RenderThreadImpl::current()->GetGpuFactories(),
336 &start_signaling_event));
338 start_signaling_event.Wait();
339 CHECK(signaling_thread_);
342 void PeerConnectionDependencyFactory::InitializeSignalingThread(
343 const scoped_refptr<media::GpuVideoAcceleratorFactories>& gpu_factories,
344 base::WaitableEvent* event) {
345 DCHECK(chrome_signaling_thread_.task_runner()->BelongsToCurrentThread());
346 DCHECK(worker_thread_);
347 DCHECK(p2p_socket_dispatcher_.get());
349 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
350 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
351 signaling_thread_ = jingle_glue::JingleThreadWrapper::current();
353 EnsureWebRtcAudioDeviceImpl();
355 socket_factory_.reset(
356 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get()));
358 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory;
359 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory;
361 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
362 if (gpu_factories.get()) {
363 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWDecoding))
364 decoder_factory.reset(new RTCVideoDecoderFactory(gpu_factories));
366 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWEncoding))
367 encoder_factory.reset(new RTCVideoEncoderFactory(gpu_factories));
370 #if defined(OS_ANDROID)
371 if (!media::MediaCodecBridge::SupportsSetParameters())
372 encoder_factory.reset();
373 #endif
375 pc_factory_ = webrtc::CreatePeerConnectionFactory(
376 worker_thread_, signaling_thread_, audio_device_.get(),
377 encoder_factory.release(), decoder_factory.release());
378 CHECK(pc_factory_.get());
380 webrtc::PeerConnectionFactoryInterface::Options factory_options;
381 factory_options.disable_sctp_data_channels = false;
382 factory_options.disable_encryption =
383 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption);
384 pc_factory_->SetOptions(factory_options);
386 event->Signal();
389 bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() {
390 return pc_factory_.get() != NULL;
393 scoped_refptr<webrtc::PeerConnectionInterface>
394 PeerConnectionDependencyFactory::CreatePeerConnection(
395 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
396 const webrtc::MediaConstraintsInterface* constraints,
397 blink::WebFrame* web_frame,
398 webrtc::PeerConnectionObserver* observer) {
399 CHECK(web_frame);
400 CHECK(observer);
401 if (!GetPcFactory().get())
402 return NULL;
404 // Copy the flag from Preference associated with this WebFrame.
405 bool enable_multiple_routes = true;
406 if (web_frame && web_frame->view()) {
407 RenderViewImpl* renderer_view_impl =
408 RenderViewImpl::FromWebView(web_frame->view());
409 if (renderer_view_impl) {
410 enable_multiple_routes = renderer_view_impl->renderer_preferences()
411 .enable_webrtc_multiple_routes;
415 scoped_refptr<P2PPortAllocatorFactory> pa_factory =
416 new rtc::RefCountedObject<P2PPortAllocatorFactory>(
417 p2p_socket_dispatcher_.get(), network_manager_, socket_factory_.get(),
418 enable_multiple_routes);
420 PeerConnectionIdentityService* identity_service =
421 new PeerConnectionIdentityService(
422 GURL(web_frame->document().url().spec()).GetOrigin());
424 return GetPcFactory()->CreatePeerConnection(config,
425 constraints,
426 pa_factory.get(),
427 identity_service,
428 observer).get();
431 scoped_refptr<webrtc::MediaStreamInterface>
432 PeerConnectionDependencyFactory::CreateLocalMediaStream(
433 const std::string& label) {
434 return GetPcFactory()->CreateLocalMediaStream(label).get();
437 scoped_refptr<webrtc::AudioSourceInterface>
438 PeerConnectionDependencyFactory::CreateLocalAudioSource(
439 const webrtc::MediaConstraintsInterface* constraints) {
440 scoped_refptr<webrtc::AudioSourceInterface> source =
441 GetPcFactory()->CreateAudioSource(constraints).get();
442 return source;
445 void PeerConnectionDependencyFactory::CreateLocalAudioTrack(
446 const blink::WebMediaStreamTrack& track) {
447 blink::WebMediaStreamSource source = track.source();
448 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio);
449 MediaStreamAudioSource* source_data =
450 static_cast<MediaStreamAudioSource*>(source.extraData());
452 scoped_refptr<WebAudioCapturerSource> webaudio_source;
453 if (!source_data) {
454 if (source.requiresAudioConsumer()) {
455 // We're adding a WebAudio MediaStream.
456 // Create a specific capturer for each WebAudio consumer.
457 webaudio_source = CreateWebAudioSource(&source);
458 source_data =
459 static_cast<MediaStreamAudioSource*>(source.extraData());
460 } else {
461 // TODO(perkj): Implement support for sources from
462 // remote MediaStreams.
463 NOTIMPLEMENTED();
464 return;
468 // Creates an adapter to hold all the libjingle objects.
469 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
470 WebRtcLocalAudioTrackAdapter::Create(track.id().utf8(),
471 source_data->local_audio_source()));
472 static_cast<webrtc::AudioTrackInterface*>(adapter.get())->set_enabled(
473 track.isEnabled());
475 // TODO(xians): Merge |source| to the capturer(). We can't do this today
476 // because only one capturer() is supported while one |source| is created
477 // for each audio track.
478 scoped_ptr<WebRtcLocalAudioTrack> audio_track(new WebRtcLocalAudioTrack(
479 adapter.get(), source_data->GetAudioCapturer(), webaudio_source.get()));
481 StartLocalAudioTrack(audio_track.get());
483 // Pass the ownership of the native local audio track to the blink track.
484 blink::WebMediaStreamTrack writable_track = track;
485 writable_track.setExtraData(audio_track.release());
488 void PeerConnectionDependencyFactory::StartLocalAudioTrack(
489 WebRtcLocalAudioTrack* audio_track) {
490 // Start the audio track. This will hook the |audio_track| to the capturer
491 // as the sink of the audio, and only start the source of the capturer if
492 // it is the first audio track connecting to the capturer.
493 audio_track->Start();
496 scoped_refptr<WebAudioCapturerSource>
497 PeerConnectionDependencyFactory::CreateWebAudioSource(
498 blink::WebMediaStreamSource* source) {
499 DVLOG(1) << "PeerConnectionDependencyFactory::CreateWebAudioSource()";
501 scoped_refptr<WebAudioCapturerSource>
502 webaudio_capturer_source(new WebAudioCapturerSource());
503 MediaStreamAudioSource* source_data = new MediaStreamAudioSource();
505 // Use the current default capturer for the WebAudio track so that the
506 // WebAudio track can pass a valid delay value and |need_audio_processing|
507 // flag to PeerConnection.
508 // TODO(xians): Remove this after moving APM to Chrome.
509 if (GetWebRtcAudioDevice()) {
510 source_data->SetAudioCapturer(
511 GetWebRtcAudioDevice()->GetDefaultCapturer());
514 // Create a LocalAudioSource object which holds audio options.
515 // SetLocalAudioSource() affects core audio parts in third_party/Libjingle.
516 source_data->SetLocalAudioSource(CreateLocalAudioSource(NULL).get());
517 source->setExtraData(source_data);
519 // Replace the default source with WebAudio as source instead.
520 source->addAudioConsumer(webaudio_capturer_source.get());
522 return webaudio_capturer_source;
525 scoped_refptr<webrtc::VideoTrackInterface>
526 PeerConnectionDependencyFactory::CreateLocalVideoTrack(
527 const std::string& id,
528 webrtc::VideoSourceInterface* source) {
529 return GetPcFactory()->CreateVideoTrack(id, source).get();
532 scoped_refptr<webrtc::VideoTrackInterface>
533 PeerConnectionDependencyFactory::CreateLocalVideoTrack(
534 const std::string& id, cricket::VideoCapturer* capturer) {
535 if (!capturer) {
536 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer.";
537 return NULL;
540 // Create video source from the |capturer|.
541 scoped_refptr<webrtc::VideoSourceInterface> source =
542 GetPcFactory()->CreateVideoSource(capturer, NULL).get();
544 // Create native track from the source.
545 return GetPcFactory()->CreateVideoTrack(id, source.get()).get();
548 webrtc::SessionDescriptionInterface*
549 PeerConnectionDependencyFactory::CreateSessionDescription(
550 const std::string& type,
551 const std::string& sdp,
552 webrtc::SdpParseError* error) {
553 return webrtc::CreateSessionDescription(type, sdp, error);
556 webrtc::IceCandidateInterface*
557 PeerConnectionDependencyFactory::CreateIceCandidate(
558 const std::string& sdp_mid,
559 int sdp_mline_index,
560 const std::string& sdp) {
561 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp);
564 WebRtcAudioDeviceImpl*
565 PeerConnectionDependencyFactory::GetWebRtcAudioDevice() {
566 return audio_device_.get();
569 void PeerConnectionDependencyFactory::InitializeWorkerThread(
570 rtc::Thread** thread,
571 base::WaitableEvent* event) {
572 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
573 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
574 *thread = jingle_glue::JingleThreadWrapper::current();
575 event->Signal();
578 void PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread(
579 base::WaitableEvent* event) {
580 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop());
581 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_.get());
582 event->Signal();
585 void PeerConnectionDependencyFactory::DeleteIpcNetworkManager() {
586 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop());
587 delete network_manager_;
588 network_manager_ = NULL;
591 void PeerConnectionDependencyFactory::CleanupPeerConnectionFactory() {
592 DVLOG(1) << "PeerConnectionDependencyFactory::CleanupPeerConnectionFactory()";
593 pc_factory_ = NULL;
594 if (network_manager_) {
595 // The network manager needs to free its resources on the thread they were
596 // created, which is the worked thread.
597 if (chrome_worker_thread_.IsRunning()) {
598 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
599 &PeerConnectionDependencyFactory::DeleteIpcNetworkManager,
600 base::Unretained(this)));
601 // Stopping the thread will wait until all tasks have been
602 // processed before returning. We wait for the above task to finish before
603 // letting the the function continue to avoid any potential race issues.
604 chrome_worker_thread_.Stop();
605 } else {
606 NOTREACHED() << "Worker thread not running.";
611 scoped_refptr<WebRtcAudioCapturer>
612 PeerConnectionDependencyFactory::CreateAudioCapturer(
613 int render_view_id,
614 const StreamDeviceInfo& device_info,
615 const blink::WebMediaConstraints& constraints,
616 MediaStreamAudioSource* audio_source) {
617 // TODO(xians): Handle the cases when gUM is called without a proper render
618 // view, for example, by an extension.
619 DCHECK_GE(render_view_id, 0);
621 EnsureWebRtcAudioDeviceImpl();
622 DCHECK(GetWebRtcAudioDevice());
623 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info,
624 constraints,
625 GetWebRtcAudioDevice(),
626 audio_source);
629 scoped_refptr<base::MessageLoopProxy>
630 PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const {
631 DCHECK(CalledOnValidThread());
632 return chrome_worker_thread_.message_loop_proxy();
635 scoped_refptr<base::MessageLoopProxy>
636 PeerConnectionDependencyFactory::GetWebRtcSignalingThread() const {
637 DCHECK(CalledOnValidThread());
638 return chrome_signaling_thread_.message_loop_proxy();
641 void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() {
642 if (audio_device_.get())
643 return;
645 audio_device_ = new WebRtcAudioDeviceImpl();
648 } // namespace content