1 // Copyright (c) 2012 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/media_stream_dependency_factory.h"
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/renderer/media/media_stream.h"
15 #include "content/renderer/media/media_stream_audio_processor_options.h"
16 #include "content/renderer/media/media_stream_audio_source.h"
17 #include "content/renderer/media/media_stream_video_source.h"
18 #include "content/renderer/media/media_stream_video_track.h"
19 #include "content/renderer/media/peer_connection_identity_service.h"
20 #include "content/renderer/media/rtc_media_constraints.h"
21 #include "content/renderer/media/rtc_peer_connection_handler.h"
22 #include "content/renderer/media/rtc_video_decoder_factory.h"
23 #include "content/renderer/media/rtc_video_encoder_factory.h"
24 #include "content/renderer/media/webaudio_capturer_source.h"
25 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
26 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h"
27 #include "content/renderer/media/webrtc_audio_device_impl.h"
28 #include "content/renderer/media/webrtc_local_audio_track.h"
29 #include "content/renderer/media/webrtc_uma_histograms.h"
30 #include "content/renderer/p2p/ipc_network_manager.h"
31 #include "content/renderer/p2p/ipc_socket_factory.h"
32 #include "content/renderer/p2p/port_allocator.h"
33 #include "content/renderer/render_thread_impl.h"
34 #include "jingle/glue/thread_wrapper.h"
35 #include "media/filters/gpu_video_accelerator_factories.h"
36 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
37 #include "third_party/WebKit/public/platform/WebMediaStream.h"
38 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
39 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
40 #include "third_party/WebKit/public/platform/WebURL.h"
41 #include "third_party/WebKit/public/web/WebDocument.h"
42 #include "third_party/WebKit/public/web/WebFrame.h"
43 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
45 #if defined(USE_OPENSSL)
46 #include "third_party/libjingle/source/talk/base/ssladapter.h"
48 #include "net/socket/nss_ssl_util.h"
51 #if defined(OS_ANDROID)
52 #include "media/base/android/media_codec_bridge.h"
57 // Map of corresponding media constraints and platform effects.
59 const char* constraint;
60 const media::AudioParameters::PlatformEffectsMask effect;
61 } const kConstraintEffectMap[] = {
62 { content::kMediaStreamAudioDucking,
63 media::AudioParameters::DUCKING },
64 { webrtc::MediaConstraintsInterface::kEchoCancellation,
65 media::AudioParameters::ECHO_CANCELLER },
68 // If any platform effects are available, check them against the constraints.
69 // Disable effects to match false constraints, but if a constraint is true, set
70 // the constraint to false to later disable the software effect.
72 // This function may modify both |constraints| and |effects|.
73 void HarmonizeConstraintsAndEffects(RTCMediaConstraints* constraints,
75 if (*effects != media::AudioParameters::NO_EFFECTS) {
76 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kConstraintEffectMap); ++i) {
78 size_t is_mandatory = 0;
79 if (!webrtc::FindConstraint(constraints,
80 kConstraintEffectMap[i].constraint,
82 &is_mandatory) || !value) {
83 // If the constraint is false, or does not exist, disable the platform
85 *effects &= ~kConstraintEffectMap[i].effect;
86 DVLOG(1) << "Disabling platform effect: "
87 << kConstraintEffectMap[i].effect;
88 } else if (*effects & kConstraintEffectMap[i].effect) {
89 // If the constraint is true, leave the platform effect enabled, and
90 // set the constraint to false to later disable the software effect.
92 constraints->AddMandatory(kConstraintEffectMap[i].constraint,
93 webrtc::MediaConstraintsInterface::kValueFalse, true);
95 constraints->AddOptional(kConstraintEffectMap[i].constraint,
96 webrtc::MediaConstraintsInterface::kValueFalse, true);
98 DVLOG(1) << "Disabling constraint: "
99 << kConstraintEffectMap[i].constraint;
105 class P2PPortAllocatorFactory : public webrtc::PortAllocatorFactoryInterface {
107 P2PPortAllocatorFactory(
108 P2PSocketDispatcher* socket_dispatcher,
109 talk_base::NetworkManager* network_manager,
110 talk_base::PacketSocketFactory* socket_factory,
111 blink::WebFrame* web_frame)
112 : socket_dispatcher_(socket_dispatcher),
113 network_manager_(network_manager),
114 socket_factory_(socket_factory),
115 web_frame_(web_frame) {
118 virtual cricket::PortAllocator* CreatePortAllocator(
119 const std::vector<StunConfiguration>& stun_servers,
120 const std::vector<TurnConfiguration>& turn_configurations) OVERRIDE {
122 P2PPortAllocator::Config config;
123 if (stun_servers.size() > 0) {
124 config.stun_server = stun_servers[0].server.hostname();
125 config.stun_server_port = stun_servers[0].server.port();
127 config.legacy_relay = false;
128 for (size_t i = 0; i < turn_configurations.size(); ++i) {
129 P2PPortAllocator::Config::RelayServerConfig relay_config;
130 relay_config.server_address = turn_configurations[i].server.hostname();
131 relay_config.port = turn_configurations[i].server.port();
132 relay_config.username = turn_configurations[i].username;
133 relay_config.password = turn_configurations[i].password;
134 relay_config.transport_type = turn_configurations[i].transport_type;
135 relay_config.secure = turn_configurations[i].secure;
136 config.relays.push_back(relay_config);
139 // Use first turn server as the stun server.
140 if (turn_configurations.size() > 0) {
141 config.stun_server = config.relays[0].server_address;
142 config.stun_server_port = config.relays[0].port;
145 return new P2PPortAllocator(
146 web_frame_, socket_dispatcher_.get(), network_manager_,
147 socket_factory_, config);
151 virtual ~P2PPortAllocatorFactory() {}
154 scoped_refptr<P2PSocketDispatcher> socket_dispatcher_;
155 // |network_manager_| and |socket_factory_| are a weak references, owned by
156 // MediaStreamDependencyFactory.
157 talk_base::NetworkManager* network_manager_;
158 talk_base::PacketSocketFactory* socket_factory_;
159 // Raw ptr to the WebFrame that created the P2PPortAllocatorFactory.
160 blink::WebFrame* web_frame_;
163 MediaStreamDependencyFactory::MediaStreamDependencyFactory(
164 P2PSocketDispatcher* p2p_socket_dispatcher)
165 : network_manager_(NULL),
166 p2p_socket_dispatcher_(p2p_socket_dispatcher),
167 signaling_thread_(NULL),
168 worker_thread_(NULL),
169 chrome_worker_thread_("Chrome_libJingle_WorkerThread") {
172 MediaStreamDependencyFactory::~MediaStreamDependencyFactory() {
173 CleanupPeerConnectionFactory();
176 blink::WebRTCPeerConnectionHandler*
177 MediaStreamDependencyFactory::CreateRTCPeerConnectionHandler(
178 blink::WebRTCPeerConnectionHandlerClient* client) {
179 // Save histogram data so we can see how much PeerConnetion is used.
180 // The histogram counts the number of calls to the JS API
181 // webKitRTCPeerConnection.
182 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION);
184 return new RTCPeerConnectionHandler(client, this);
187 bool MediaStreamDependencyFactory::InitializeMediaStreamAudioSource(
189 const blink::WebMediaConstraints& audio_constraints,
190 MediaStreamAudioSource* source_data) {
191 DVLOG(1) << "InitializeMediaStreamAudioSources()";
193 // Do additional source initialization if the audio source is a valid
194 // microphone or tab audio.
195 RTCMediaConstraints native_audio_constraints(audio_constraints);
196 ApplyFixedAudioConstraints(&native_audio_constraints);
198 StreamDeviceInfo device_info = source_data->device_info();
199 RTCMediaConstraints constraints = native_audio_constraints;
200 // May modify both |constraints| and |effects|.
201 HarmonizeConstraintsAndEffects(&constraints,
202 &device_info.device.input.effects);
204 scoped_refptr<WebRtcAudioCapturer> capturer(
205 CreateAudioCapturer(render_view_id, device_info, audio_constraints,
207 if (!capturer.get()) {
208 DLOG(WARNING) << "Failed to create the capturer for device "
209 << device_info.device.id;
210 // TODO(xians): Don't we need to check if source_observer is observing
211 // something? If not, then it looks like we have a leak here.
212 // OTOH, if it _is_ observing something, then the callback might
213 // be called multiple times which is likely also a bug.
216 source_data->SetAudioCapturer(capturer);
218 // Creates a LocalAudioSource object which holds audio options.
219 // TODO(xians): The option should apply to the track instead of the source.
220 // TODO(perkj): Move audio constraints parsing to Chrome.
221 // Currently there are a few constraints that are parsed by libjingle and
222 // the state is set to ended if parsing fails.
223 scoped_refptr<webrtc::AudioSourceInterface> rtc_source(
224 CreateLocalAudioSource(&constraints).get());
225 if (rtc_source->state() != webrtc::MediaSourceInterface::kLive) {
226 DLOG(WARNING) << "Failed to create rtc LocalAudioSource.";
229 source_data->SetLocalAudioSource(rtc_source);
233 WebRtcVideoCapturerAdapter* MediaStreamDependencyFactory::CreateVideoCapturer(
234 bool is_screeencast) {
235 // We need to make sure the libjingle thread wrappers have been created
236 // before we can use an instance of a WebRtcVideoCapturerAdapter. This is
237 // since the base class of WebRtcVideoCapturerAdapter is a
238 // cricket::VideoCapturer and it uses the libjingle thread wrappers.
241 return new WebRtcVideoCapturerAdapter(is_screeencast);
244 scoped_refptr<webrtc::VideoSourceInterface>
245 MediaStreamDependencyFactory::CreateVideoSource(
246 cricket::VideoCapturer* capturer,
247 const blink::WebMediaConstraints& constraints) {
248 RTCMediaConstraints webrtc_constraints(constraints);
249 scoped_refptr<webrtc::VideoSourceInterface> source =
250 GetPcFactory()->CreateVideoSource(capturer, &webrtc_constraints).get();
254 const scoped_refptr<webrtc::PeerConnectionFactoryInterface>&
255 MediaStreamDependencyFactory::GetPcFactory() {
257 CreatePeerConnectionFactory();
262 void MediaStreamDependencyFactory::CreatePeerConnectionFactory() {
263 DCHECK(!pc_factory_.get());
264 DCHECK(!signaling_thread_);
265 DCHECK(!worker_thread_);
266 DCHECK(!network_manager_);
267 DCHECK(!socket_factory_);
268 DCHECK(!chrome_worker_thread_.IsRunning());
270 DVLOG(1) << "MediaStreamDependencyFactory::CreatePeerConnectionFactory()";
272 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
273 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
274 signaling_thread_ = jingle_glue::JingleThreadWrapper::current();
275 CHECK(signaling_thread_);
277 CHECK(chrome_worker_thread_.Start());
279 base::WaitableEvent start_worker_event(true, false);
280 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
281 &MediaStreamDependencyFactory::InitializeWorkerThread,
282 base::Unretained(this),
284 &start_worker_event));
285 start_worker_event.Wait();
286 CHECK(worker_thread_);
288 base::WaitableEvent create_network_manager_event(true, false);
289 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
290 &MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread,
291 base::Unretained(this),
292 &create_network_manager_event));
293 create_network_manager_event.Wait();
295 socket_factory_.reset(
296 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get()));
298 // Init SSL, which will be needed by PeerConnection.
299 #if defined(USE_OPENSSL)
300 if (!talk_base::InitializeSSL()) {
301 LOG(ERROR) << "Failed on InitializeSSL.";
306 // TODO(ronghuawu): Replace this call with InitializeSSL.
307 net::EnsureNSSSSLInit();
310 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory;
311 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory;
313 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
314 scoped_refptr<media::GpuVideoAcceleratorFactories> gpu_factories =
315 RenderThreadImpl::current()->GetGpuFactories();
316 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWDecoding)) {
318 decoder_factory.reset(new RTCVideoDecoderFactory(gpu_factories));
321 if (!cmd_line->HasSwitch(switches::kDisableWebRtcHWEncoding)) {
323 encoder_factory.reset(new RTCVideoEncoderFactory(gpu_factories));
326 #if defined(OS_ANDROID)
327 if (!media::MediaCodecBridge::SupportsSetParameters())
328 encoder_factory.reset();
331 EnsureWebRtcAudioDeviceImpl();
333 scoped_refptr<webrtc::PeerConnectionFactoryInterface> factory(
334 webrtc::CreatePeerConnectionFactory(worker_thread_,
337 encoder_factory.release(),
338 decoder_factory.release()));
341 pc_factory_ = factory;
342 webrtc::PeerConnectionFactoryInterface::Options factory_options;
343 factory_options.disable_sctp_data_channels = false;
344 factory_options.disable_encryption =
345 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption);
346 pc_factory_->SetOptions(factory_options);
348 // |aec_dump_file| will be invalid when dump is not enabled.
349 if (aec_dump_file_.IsValid())
350 StartAecDump(aec_dump_file_.Pass());
353 bool MediaStreamDependencyFactory::PeerConnectionFactoryCreated() {
354 return pc_factory_.get() != NULL;
357 scoped_refptr<webrtc::PeerConnectionInterface>
358 MediaStreamDependencyFactory::CreatePeerConnection(
359 const webrtc::PeerConnectionInterface::IceServers& ice_servers,
360 const webrtc::MediaConstraintsInterface* constraints,
361 blink::WebFrame* web_frame,
362 webrtc::PeerConnectionObserver* observer) {
368 scoped_refptr<P2PPortAllocatorFactory> pa_factory =
369 new talk_base::RefCountedObject<P2PPortAllocatorFactory>(
370 p2p_socket_dispatcher_.get(),
372 socket_factory_.get(),
375 PeerConnectionIdentityService* identity_service =
376 new PeerConnectionIdentityService(
377 GURL(web_frame->document().url().spec()).GetOrigin());
379 return GetPcFactory()->CreatePeerConnection(ice_servers,
386 scoped_refptr<webrtc::MediaStreamInterface>
387 MediaStreamDependencyFactory::CreateLocalMediaStream(
388 const std::string& label) {
389 return GetPcFactory()->CreateLocalMediaStream(label).get();
392 scoped_refptr<webrtc::AudioSourceInterface>
393 MediaStreamDependencyFactory::CreateLocalAudioSource(
394 const webrtc::MediaConstraintsInterface* constraints) {
395 scoped_refptr<webrtc::AudioSourceInterface> source =
396 GetPcFactory()->CreateAudioSource(constraints).get();
400 void MediaStreamDependencyFactory::CreateLocalAudioTrack(
401 const blink::WebMediaStreamTrack& track) {
402 blink::WebMediaStreamSource source = track.source();
403 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio);
404 MediaStreamAudioSource* source_data =
405 static_cast<MediaStreamAudioSource*>(source.extraData());
407 scoped_refptr<WebAudioCapturerSource> webaudio_source;
409 if (source.requiresAudioConsumer()) {
410 // We're adding a WebAudio MediaStream.
411 // Create a specific capturer for each WebAudio consumer.
412 webaudio_source = CreateWebAudioSource(&source);
414 static_cast<MediaStreamAudioSource*>(source.extraData());
416 // TODO(perkj): Implement support for sources from
417 // remote MediaStreams.
423 // Creates an adapter to hold all the libjingle objects.
424 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
425 WebRtcLocalAudioTrackAdapter::Create(track.id().utf8(),
426 source_data->local_audio_source()));
427 static_cast<webrtc::AudioTrackInterface*>(adapter.get())->set_enabled(
430 // TODO(xians): Merge |source| to the capturer(). We can't do this today
431 // because only one capturer() is supported while one |source| is created
432 // for each audio track.
433 scoped_ptr<WebRtcLocalAudioTrack> audio_track(
434 new WebRtcLocalAudioTrack(adapter,
435 source_data->GetAudioCapturer(),
438 StartLocalAudioTrack(audio_track.get());
440 // Pass the ownership of the native local audio track to the blink track.
441 blink::WebMediaStreamTrack writable_track = track;
442 writable_track.setExtraData(audio_track.release());
445 void MediaStreamDependencyFactory::StartLocalAudioTrack(
446 WebRtcLocalAudioTrack* audio_track) {
447 // Add the WebRtcAudioDevice as the sink to the local audio track.
448 // TODO(xians): Implement a PeerConnection sink adapter and remove this
450 audio_track->AddSink(GetWebRtcAudioDevice());
451 // Start the audio track. This will hook the |audio_track| to the capturer
452 // as the sink of the audio, and only start the source of the capturer if
453 // it is the first audio track connecting to the capturer.
454 audio_track->Start();
457 scoped_refptr<WebAudioCapturerSource>
458 MediaStreamDependencyFactory::CreateWebAudioSource(
459 blink::WebMediaStreamSource* source) {
460 DVLOG(1) << "MediaStreamDependencyFactory::CreateWebAudioSource()";
462 scoped_refptr<WebAudioCapturerSource>
463 webaudio_capturer_source(new WebAudioCapturerSource());
464 MediaStreamAudioSource* source_data = new MediaStreamAudioSource();
466 // Use the current default capturer for the WebAudio track so that the
467 // WebAudio track can pass a valid delay value and |need_audio_processing|
468 // flag to PeerConnection.
469 // TODO(xians): Remove this after moving APM to Chrome.
470 if (GetWebRtcAudioDevice()) {
471 source_data->SetAudioCapturer(
472 GetWebRtcAudioDevice()->GetDefaultCapturer());
475 // Create a LocalAudioSource object which holds audio options.
476 // SetLocalAudioSource() affects core audio parts in third_party/Libjingle.
477 source_data->SetLocalAudioSource(CreateLocalAudioSource(NULL).get());
478 source->setExtraData(source_data);
480 // Replace the default source with WebAudio as source instead.
481 source->addAudioConsumer(webaudio_capturer_source.get());
483 return webaudio_capturer_source;
486 scoped_refptr<webrtc::VideoTrackInterface>
487 MediaStreamDependencyFactory::CreateLocalVideoTrack(
488 const std::string& id,
489 webrtc::VideoSourceInterface* source) {
490 return GetPcFactory()->CreateVideoTrack(id, source).get();
493 scoped_refptr<webrtc::VideoTrackInterface>
494 MediaStreamDependencyFactory::CreateLocalVideoTrack(
495 const std::string& id, cricket::VideoCapturer* capturer) {
497 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer.";
501 // Create video source from the |capturer|.
502 scoped_refptr<webrtc::VideoSourceInterface> source =
503 GetPcFactory()->CreateVideoSource(capturer, NULL).get();
505 // Create native track from the source.
506 return GetPcFactory()->CreateVideoTrack(id, source.get()).get();
509 webrtc::SessionDescriptionInterface*
510 MediaStreamDependencyFactory::CreateSessionDescription(
511 const std::string& type,
512 const std::string& sdp,
513 webrtc::SdpParseError* error) {
514 return webrtc::CreateSessionDescription(type, sdp, error);
517 webrtc::IceCandidateInterface* MediaStreamDependencyFactory::CreateIceCandidate(
518 const std::string& sdp_mid,
520 const std::string& sdp) {
521 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp);
524 WebRtcAudioDeviceImpl*
525 MediaStreamDependencyFactory::GetWebRtcAudioDevice() {
526 return audio_device_.get();
529 void MediaStreamDependencyFactory::InitializeWorkerThread(
530 talk_base::Thread** thread,
531 base::WaitableEvent* event) {
532 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop();
533 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true);
534 *thread = jingle_glue::JingleThreadWrapper::current();
538 void MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread(
539 base::WaitableEvent* event) {
540 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop());
541 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_.get());
545 void MediaStreamDependencyFactory::DeleteIpcNetworkManager() {
546 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop());
547 delete network_manager_;
548 network_manager_ = NULL;
551 void MediaStreamDependencyFactory::CleanupPeerConnectionFactory() {
553 if (network_manager_) {
554 // The network manager needs to free its resources on the thread they were
555 // created, which is the worked thread.
556 if (chrome_worker_thread_.IsRunning()) {
557 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
558 &MediaStreamDependencyFactory::DeleteIpcNetworkManager,
559 base::Unretained(this)));
560 // Stopping the thread will wait until all tasks have been
561 // processed before returning. We wait for the above task to finish before
562 // letting the the function continue to avoid any potential race issues.
563 chrome_worker_thread_.Stop();
565 NOTREACHED() << "Worker thread not running.";
570 scoped_refptr<WebRtcAudioCapturer>
571 MediaStreamDependencyFactory::CreateAudioCapturer(
573 const StreamDeviceInfo& device_info,
574 const blink::WebMediaConstraints& constraints,
575 MediaStreamAudioSource* audio_source) {
576 // TODO(xians): Handle the cases when gUM is called without a proper render
577 // view, for example, by an extension.
578 DCHECK_GE(render_view_id, 0);
580 EnsureWebRtcAudioDeviceImpl();
581 DCHECK(GetWebRtcAudioDevice());
582 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info,
584 GetWebRtcAudioDevice(),
588 void MediaStreamDependencyFactory::AddNativeAudioTrackToBlinkTrack(
589 webrtc::MediaStreamTrackInterface* native_track,
590 const blink::WebMediaStreamTrack& webkit_track,
591 bool is_local_track) {
592 DCHECK(!webkit_track.isNull() && !webkit_track.extraData());
593 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio,
594 webkit_track.source().type());
595 blink::WebMediaStreamTrack track = webkit_track;
597 DVLOG(1) << "AddNativeTrackToBlinkTrack() audio";
599 new MediaStreamTrack(
600 static_cast<webrtc::AudioTrackInterface*>(native_track),
604 scoped_refptr<base::MessageLoopProxy>
605 MediaStreamDependencyFactory::GetWebRtcWorkerThread() const {
606 DCHECK(CalledOnValidThread());
607 return chrome_worker_thread_.message_loop_proxy();
610 bool MediaStreamDependencyFactory::OnControlMessageReceived(
611 const IPC::Message& message) {
613 IPC_BEGIN_MESSAGE_MAP(MediaStreamDependencyFactory, message)
614 IPC_MESSAGE_HANDLER(MediaStreamMsg_EnableAecDump, OnAecDumpFile)
615 IPC_MESSAGE_HANDLER(MediaStreamMsg_DisableAecDump, OnDisableAecDump)
616 IPC_MESSAGE_UNHANDLED(handled = false)
617 IPC_END_MESSAGE_MAP()
621 void MediaStreamDependencyFactory::OnAecDumpFile(
622 IPC::PlatformFileForTransit file_handle) {
623 DCHECK(!aec_dump_file_.IsValid());
624 base::File file = IPC::PlatformFileForTransitToFile(file_handle);
625 DCHECK(file.IsValid());
627 if (CommandLine::ForCurrentProcess()->HasSwitch(
628 switches::kEnableAudioTrackProcessing)) {
629 EnsureWebRtcAudioDeviceImpl();
630 GetWebRtcAudioDevice()->EnableAecDump(file.Pass());
634 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing
636 if (PeerConnectionFactoryCreated())
637 StartAecDump(file.Pass());
639 aec_dump_file_ = file.Pass();
642 void MediaStreamDependencyFactory::OnDisableAecDump() {
643 if (CommandLine::ForCurrentProcess()->HasSwitch(
644 switches::kEnableAudioTrackProcessing)) {
645 GetWebRtcAudioDevice()->DisableAecDump();
649 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing
651 if (aec_dump_file_.IsValid())
652 aec_dump_file_.Close();
655 void MediaStreamDependencyFactory::StartAecDump(base::File aec_dump_file) {
656 // |pc_factory_| always takes ownership of |aec_dump_file|. If StartAecDump()
657 // fails, |aec_dump_file| will be closed.
658 if (!GetPcFactory()->StartAecDump(aec_dump_file.TakePlatformFile()))
659 VLOG(1) << "Could not start AEC dump.";
662 void MediaStreamDependencyFactory::EnsureWebRtcAudioDeviceImpl() {
666 audio_device_ = new WebRtcAudioDeviceImpl();
669 } // namespace content