| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/media_stream_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" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "content/common/media/media_stream_messages.h" | 12 #include "content/common/media/media_stream_messages.h" |
| 13 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/renderer/media/media_stream.h" | 14 #include "content/renderer/media/media_stream.h" |
| 15 #include "content/renderer/media/media_stream_audio_processor_options.h" | 15 #include "content/renderer/media/media_stream_audio_processor_options.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 web_frame_, socket_dispatcher_.get(), network_manager_, | 146 web_frame_, socket_dispatcher_.get(), network_manager_, |
| 147 socket_factory_, config); | 147 socket_factory_, config); |
| 148 } | 148 } |
| 149 | 149 |
| 150 protected: | 150 protected: |
| 151 virtual ~P2PPortAllocatorFactory() {} | 151 virtual ~P2PPortAllocatorFactory() {} |
| 152 | 152 |
| 153 private: | 153 private: |
| 154 scoped_refptr<P2PSocketDispatcher> socket_dispatcher_; | 154 scoped_refptr<P2PSocketDispatcher> socket_dispatcher_; |
| 155 // |network_manager_| and |socket_factory_| are a weak references, owned by | 155 // |network_manager_| and |socket_factory_| are a weak references, owned by |
| 156 // MediaStreamDependencyFactory. | 156 // PeerConnectionDependencyFactory. |
| 157 talk_base::NetworkManager* network_manager_; | 157 talk_base::NetworkManager* network_manager_; |
| 158 talk_base::PacketSocketFactory* socket_factory_; | 158 talk_base::PacketSocketFactory* socket_factory_; |
| 159 // Raw ptr to the WebFrame that created the P2PPortAllocatorFactory. | 159 // Raw ptr to the WebFrame that created the P2PPortAllocatorFactory. |
| 160 blink::WebFrame* web_frame_; | 160 blink::WebFrame* web_frame_; |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 MediaStreamDependencyFactory::MediaStreamDependencyFactory( | 163 PeerConnectionDependencyFactory::PeerConnectionDependencyFactory( |
| 164 P2PSocketDispatcher* p2p_socket_dispatcher) | 164 P2PSocketDispatcher* p2p_socket_dispatcher) |
| 165 : network_manager_(NULL), | 165 : network_manager_(NULL), |
| 166 p2p_socket_dispatcher_(p2p_socket_dispatcher), | 166 p2p_socket_dispatcher_(p2p_socket_dispatcher), |
| 167 signaling_thread_(NULL), | 167 signaling_thread_(NULL), |
| 168 worker_thread_(NULL), | 168 worker_thread_(NULL), |
| 169 chrome_worker_thread_("Chrome_libJingle_WorkerThread") { | 169 chrome_worker_thread_("Chrome_libJingle_WorkerThread") { |
| 170 } | 170 } |
| 171 | 171 |
| 172 MediaStreamDependencyFactory::~MediaStreamDependencyFactory() { | 172 PeerConnectionDependencyFactory::~PeerConnectionDependencyFactory() { |
| 173 CleanupPeerConnectionFactory(); | 173 CleanupPeerConnectionFactory(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 blink::WebRTCPeerConnectionHandler* | 176 blink::WebRTCPeerConnectionHandler* |
| 177 MediaStreamDependencyFactory::CreateRTCPeerConnectionHandler( | 177 PeerConnectionDependencyFactory::CreateRTCPeerConnectionHandler( |
| 178 blink::WebRTCPeerConnectionHandlerClient* client) { | 178 blink::WebRTCPeerConnectionHandlerClient* client) { |
| 179 // Save histogram data so we can see how much PeerConnetion is used. | 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 | 180 // The histogram counts the number of calls to the JS API |
| 181 // webKitRTCPeerConnection. | 181 // webKitRTCPeerConnection. |
| 182 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION); | 182 UpdateWebRTCMethodCount(WEBKIT_RTC_PEER_CONNECTION); |
| 183 | 183 |
| 184 return new RTCPeerConnectionHandler(client, this); | 184 return new RTCPeerConnectionHandler(client, this); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool MediaStreamDependencyFactory::InitializeMediaStreamAudioSource( | 187 bool PeerConnectionDependencyFactory::InitializeMediaStreamAudioSource( |
| 188 int render_view_id, | 188 int render_view_id, |
| 189 const blink::WebMediaConstraints& audio_constraints, | 189 const blink::WebMediaConstraints& audio_constraints, |
| 190 MediaStreamAudioSource* source_data) { | 190 MediaStreamAudioSource* source_data) { |
| 191 DVLOG(1) << "InitializeMediaStreamAudioSources()"; | 191 DVLOG(1) << "InitializeMediaStreamAudioSources()"; |
| 192 | 192 |
| 193 // Do additional source initialization if the audio source is a valid | 193 // Do additional source initialization if the audio source is a valid |
| 194 // microphone or tab audio. | 194 // microphone or tab audio. |
| 195 RTCMediaConstraints native_audio_constraints(audio_constraints); | 195 RTCMediaConstraints native_audio_constraints(audio_constraints); |
| 196 MediaAudioConstraints::ApplyFixedAudioConstraints(&native_audio_constraints); | 196 MediaAudioConstraints::ApplyFixedAudioConstraints(&native_audio_constraints); |
| 197 | 197 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 223 scoped_refptr<webrtc::AudioSourceInterface> rtc_source( | 223 scoped_refptr<webrtc::AudioSourceInterface> rtc_source( |
| 224 CreateLocalAudioSource(&constraints).get()); | 224 CreateLocalAudioSource(&constraints).get()); |
| 225 if (rtc_source->state() != webrtc::MediaSourceInterface::kLive) { | 225 if (rtc_source->state() != webrtc::MediaSourceInterface::kLive) { |
| 226 DLOG(WARNING) << "Failed to create rtc LocalAudioSource."; | 226 DLOG(WARNING) << "Failed to create rtc LocalAudioSource."; |
| 227 return false; | 227 return false; |
| 228 } | 228 } |
| 229 source_data->SetLocalAudioSource(rtc_source); | 229 source_data->SetLocalAudioSource(rtc_source); |
| 230 return true; | 230 return true; |
| 231 } | 231 } |
| 232 | 232 |
| 233 WebRtcVideoCapturerAdapter* MediaStreamDependencyFactory::CreateVideoCapturer( | 233 WebRtcVideoCapturerAdapter* |
| 234 PeerConnectionDependencyFactory::CreateVideoCapturer( |
| 234 bool is_screeencast) { | 235 bool is_screeencast) { |
| 235 // We need to make sure the libjingle thread wrappers have been created | 236 // 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 // before we can use an instance of a WebRtcVideoCapturerAdapter. This is |
| 237 // since the base class of WebRtcVideoCapturerAdapter is a | 238 // since the base class of WebRtcVideoCapturerAdapter is a |
| 238 // cricket::VideoCapturer and it uses the libjingle thread wrappers. | 239 // cricket::VideoCapturer and it uses the libjingle thread wrappers. |
| 239 if (!GetPcFactory()) | 240 if (!GetPcFactory()) |
| 240 return NULL; | 241 return NULL; |
| 241 return new WebRtcVideoCapturerAdapter(is_screeencast); | 242 return new WebRtcVideoCapturerAdapter(is_screeencast); |
| 242 } | 243 } |
| 243 | 244 |
| 244 scoped_refptr<webrtc::VideoSourceInterface> | 245 scoped_refptr<webrtc::VideoSourceInterface> |
| 245 MediaStreamDependencyFactory::CreateVideoSource( | 246 PeerConnectionDependencyFactory::CreateVideoSource( |
| 246 cricket::VideoCapturer* capturer, | 247 cricket::VideoCapturer* capturer, |
| 247 const blink::WebMediaConstraints& constraints) { | 248 const blink::WebMediaConstraints& constraints) { |
| 248 RTCMediaConstraints webrtc_constraints(constraints); | 249 RTCMediaConstraints webrtc_constraints(constraints); |
| 249 scoped_refptr<webrtc::VideoSourceInterface> source = | 250 scoped_refptr<webrtc::VideoSourceInterface> source = |
| 250 GetPcFactory()->CreateVideoSource(capturer, &webrtc_constraints).get(); | 251 GetPcFactory()->CreateVideoSource(capturer, &webrtc_constraints).get(); |
| 251 return source; | 252 return source; |
| 252 } | 253 } |
| 253 | 254 |
| 254 const scoped_refptr<webrtc::PeerConnectionFactoryInterface>& | 255 const scoped_refptr<webrtc::PeerConnectionFactoryInterface>& |
| 255 MediaStreamDependencyFactory::GetPcFactory() { | 256 PeerConnectionDependencyFactory::GetPcFactory() { |
| 256 if (!pc_factory_) | 257 if (!pc_factory_) |
| 257 CreatePeerConnectionFactory(); | 258 CreatePeerConnectionFactory(); |
| 258 CHECK(pc_factory_); | 259 CHECK(pc_factory_); |
| 259 return pc_factory_; | 260 return pc_factory_; |
| 260 } | 261 } |
| 261 | 262 |
| 262 void MediaStreamDependencyFactory::CreatePeerConnectionFactory() { | 263 void PeerConnectionDependencyFactory::CreatePeerConnectionFactory() { |
| 263 DCHECK(!pc_factory_.get()); | 264 DCHECK(!pc_factory_.get()); |
| 264 DCHECK(!signaling_thread_); | 265 DCHECK(!signaling_thread_); |
| 265 DCHECK(!worker_thread_); | 266 DCHECK(!worker_thread_); |
| 266 DCHECK(!network_manager_); | 267 DCHECK(!network_manager_); |
| 267 DCHECK(!socket_factory_); | 268 DCHECK(!socket_factory_); |
| 268 DCHECK(!chrome_worker_thread_.IsRunning()); | 269 DCHECK(!chrome_worker_thread_.IsRunning()); |
| 269 | 270 |
| 270 DVLOG(1) << "MediaStreamDependencyFactory::CreatePeerConnectionFactory()"; | 271 DVLOG(1) << "PeerConnectionDependencyFactory::CreatePeerConnectionFactory()"; |
| 271 | 272 |
| 272 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 273 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 273 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | 274 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
| 274 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); | 275 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); |
| 275 CHECK(signaling_thread_); | 276 CHECK(signaling_thread_); |
| 276 | 277 |
| 277 CHECK(chrome_worker_thread_.Start()); | 278 CHECK(chrome_worker_thread_.Start()); |
| 278 | 279 |
| 279 base::WaitableEvent start_worker_event(true, false); | 280 base::WaitableEvent start_worker_event(true, false); |
| 280 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | 281 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( |
| 281 &MediaStreamDependencyFactory::InitializeWorkerThread, | 282 &PeerConnectionDependencyFactory::InitializeWorkerThread, |
| 282 base::Unretained(this), | 283 base::Unretained(this), |
| 283 &worker_thread_, | 284 &worker_thread_, |
| 284 &start_worker_event)); | 285 &start_worker_event)); |
| 285 start_worker_event.Wait(); | 286 start_worker_event.Wait(); |
| 286 CHECK(worker_thread_); | 287 CHECK(worker_thread_); |
| 287 | 288 |
| 288 base::WaitableEvent create_network_manager_event(true, false); | 289 base::WaitableEvent create_network_manager_event(true, false); |
| 289 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | 290 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( |
| 290 &MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread, | 291 &PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread, |
| 291 base::Unretained(this), | 292 base::Unretained(this), |
| 292 &create_network_manager_event)); | 293 &create_network_manager_event)); |
| 293 create_network_manager_event.Wait(); | 294 create_network_manager_event.Wait(); |
| 294 | 295 |
| 295 socket_factory_.reset( | 296 socket_factory_.reset( |
| 296 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get())); | 297 new IpcPacketSocketFactory(p2p_socket_dispatcher_.get())); |
| 297 | 298 |
| 298 // Init SSL, which will be needed by PeerConnection. | 299 // Init SSL, which will be needed by PeerConnection. |
| 299 #if defined(USE_OPENSSL) | 300 #if defined(USE_OPENSSL) |
| 300 if (!talk_base::InitializeSSL()) { | 301 if (!talk_base::InitializeSSL()) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 factory_options.disable_sctp_data_channels = false; | 344 factory_options.disable_sctp_data_channels = false; |
| 344 factory_options.disable_encryption = | 345 factory_options.disable_encryption = |
| 345 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption); | 346 cmd_line->HasSwitch(switches::kDisableWebRtcEncryption); |
| 346 pc_factory_->SetOptions(factory_options); | 347 pc_factory_->SetOptions(factory_options); |
| 347 | 348 |
| 348 // |aec_dump_file| will be invalid when dump is not enabled. | 349 // |aec_dump_file| will be invalid when dump is not enabled. |
| 349 if (aec_dump_file_.IsValid()) | 350 if (aec_dump_file_.IsValid()) |
| 350 StartAecDump(aec_dump_file_.Pass()); | 351 StartAecDump(aec_dump_file_.Pass()); |
| 351 } | 352 } |
| 352 | 353 |
| 353 bool MediaStreamDependencyFactory::PeerConnectionFactoryCreated() { | 354 bool PeerConnectionDependencyFactory::PeerConnectionFactoryCreated() { |
| 354 return pc_factory_.get() != NULL; | 355 return pc_factory_.get() != NULL; |
| 355 } | 356 } |
| 356 | 357 |
| 357 scoped_refptr<webrtc::PeerConnectionInterface> | 358 scoped_refptr<webrtc::PeerConnectionInterface> |
| 358 MediaStreamDependencyFactory::CreatePeerConnection( | 359 PeerConnectionDependencyFactory::CreatePeerConnection( |
| 359 const webrtc::PeerConnectionInterface::IceServers& ice_servers, | 360 const webrtc::PeerConnectionInterface::IceServers& ice_servers, |
| 360 const webrtc::MediaConstraintsInterface* constraints, | 361 const webrtc::MediaConstraintsInterface* constraints, |
| 361 blink::WebFrame* web_frame, | 362 blink::WebFrame* web_frame, |
| 362 webrtc::PeerConnectionObserver* observer) { | 363 webrtc::PeerConnectionObserver* observer) { |
| 363 CHECK(web_frame); | 364 CHECK(web_frame); |
| 364 CHECK(observer); | 365 CHECK(observer); |
| 365 if (!GetPcFactory()) | 366 if (!GetPcFactory()) |
| 366 return NULL; | 367 return NULL; |
| 367 | 368 |
| 368 scoped_refptr<P2PPortAllocatorFactory> pa_factory = | 369 scoped_refptr<P2PPortAllocatorFactory> pa_factory = |
| 369 new talk_base::RefCountedObject<P2PPortAllocatorFactory>( | 370 new talk_base::RefCountedObject<P2PPortAllocatorFactory>( |
| 370 p2p_socket_dispatcher_.get(), | 371 p2p_socket_dispatcher_.get(), |
| 371 network_manager_, | 372 network_manager_, |
| 372 socket_factory_.get(), | 373 socket_factory_.get(), |
| 373 web_frame); | 374 web_frame); |
| 374 | 375 |
| 375 PeerConnectionIdentityService* identity_service = | 376 PeerConnectionIdentityService* identity_service = |
| 376 new PeerConnectionIdentityService( | 377 new PeerConnectionIdentityService( |
| 377 GURL(web_frame->document().url().spec()).GetOrigin()); | 378 GURL(web_frame->document().url().spec()).GetOrigin()); |
| 378 | 379 |
| 379 return GetPcFactory()->CreatePeerConnection(ice_servers, | 380 return GetPcFactory()->CreatePeerConnection(ice_servers, |
| 380 constraints, | 381 constraints, |
| 381 pa_factory.get(), | 382 pa_factory.get(), |
| 382 identity_service, | 383 identity_service, |
| 383 observer).get(); | 384 observer).get(); |
| 384 } | 385 } |
| 385 | 386 |
| 386 scoped_refptr<webrtc::MediaStreamInterface> | 387 scoped_refptr<webrtc::MediaStreamInterface> |
| 387 MediaStreamDependencyFactory::CreateLocalMediaStream( | 388 PeerConnectionDependencyFactory::CreateLocalMediaStream( |
| 388 const std::string& label) { | 389 const std::string& label) { |
| 389 return GetPcFactory()->CreateLocalMediaStream(label).get(); | 390 return GetPcFactory()->CreateLocalMediaStream(label).get(); |
| 390 } | 391 } |
| 391 | 392 |
| 392 scoped_refptr<webrtc::AudioSourceInterface> | 393 scoped_refptr<webrtc::AudioSourceInterface> |
| 393 MediaStreamDependencyFactory::CreateLocalAudioSource( | 394 PeerConnectionDependencyFactory::CreateLocalAudioSource( |
| 394 const webrtc::MediaConstraintsInterface* constraints) { | 395 const webrtc::MediaConstraintsInterface* constraints) { |
| 395 scoped_refptr<webrtc::AudioSourceInterface> source = | 396 scoped_refptr<webrtc::AudioSourceInterface> source = |
| 396 GetPcFactory()->CreateAudioSource(constraints).get(); | 397 GetPcFactory()->CreateAudioSource(constraints).get(); |
| 397 return source; | 398 return source; |
| 398 } | 399 } |
| 399 | 400 |
| 400 void MediaStreamDependencyFactory::CreateLocalAudioTrack( | 401 void PeerConnectionDependencyFactory::CreateLocalAudioTrack( |
| 401 const blink::WebMediaStreamTrack& track) { | 402 const blink::WebMediaStreamTrack& track) { |
| 402 blink::WebMediaStreamSource source = track.source(); | 403 blink::WebMediaStreamSource source = track.source(); |
| 403 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio); | 404 DCHECK_EQ(source.type(), blink::WebMediaStreamSource::TypeAudio); |
| 404 MediaStreamAudioSource* source_data = | 405 MediaStreamAudioSource* source_data = |
| 405 static_cast<MediaStreamAudioSource*>(source.extraData()); | 406 static_cast<MediaStreamAudioSource*>(source.extraData()); |
| 406 | 407 |
| 407 scoped_refptr<WebAudioCapturerSource> webaudio_source; | 408 scoped_refptr<WebAudioCapturerSource> webaudio_source; |
| 408 if (!source_data) { | 409 if (!source_data) { |
| 409 if (source.requiresAudioConsumer()) { | 410 if (source.requiresAudioConsumer()) { |
| 410 // We're adding a WebAudio MediaStream. | 411 // We're adding a WebAudio MediaStream. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 435 source_data->GetAudioCapturer(), | 436 source_data->GetAudioCapturer(), |
| 436 webaudio_source)); | 437 webaudio_source)); |
| 437 | 438 |
| 438 StartLocalAudioTrack(audio_track.get()); | 439 StartLocalAudioTrack(audio_track.get()); |
| 439 | 440 |
| 440 // Pass the ownership of the native local audio track to the blink track. | 441 // Pass the ownership of the native local audio track to the blink track. |
| 441 blink::WebMediaStreamTrack writable_track = track; | 442 blink::WebMediaStreamTrack writable_track = track; |
| 442 writable_track.setExtraData(audio_track.release()); | 443 writable_track.setExtraData(audio_track.release()); |
| 443 } | 444 } |
| 444 | 445 |
| 445 void MediaStreamDependencyFactory::StartLocalAudioTrack( | 446 void PeerConnectionDependencyFactory::StartLocalAudioTrack( |
| 446 WebRtcLocalAudioTrack* audio_track) { | 447 WebRtcLocalAudioTrack* audio_track) { |
| 447 // Add the WebRtcAudioDevice as the sink to the local audio track. | 448 // Add the WebRtcAudioDevice as the sink to the local audio track. |
| 448 // TODO(xians): Implement a PeerConnection sink adapter and remove this | 449 // TODO(xians): Implement a PeerConnection sink adapter and remove this |
| 449 // AddSink() call. | 450 // AddSink() call. |
| 450 audio_track->AddSink(GetWebRtcAudioDevice()); | 451 audio_track->AddSink(GetWebRtcAudioDevice()); |
| 451 // Start the audio track. This will hook the |audio_track| to the capturer | 452 // 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 // 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 // it is the first audio track connecting to the capturer. |
| 454 audio_track->Start(); | 455 audio_track->Start(); |
| 455 } | 456 } |
| 456 | 457 |
| 457 scoped_refptr<WebAudioCapturerSource> | 458 scoped_refptr<WebAudioCapturerSource> |
| 458 MediaStreamDependencyFactory::CreateWebAudioSource( | 459 PeerConnectionDependencyFactory::CreateWebAudioSource( |
| 459 blink::WebMediaStreamSource* source) { | 460 blink::WebMediaStreamSource* source) { |
| 460 DVLOG(1) << "MediaStreamDependencyFactory::CreateWebAudioSource()"; | 461 DVLOG(1) << "PeerConnectionDependencyFactory::CreateWebAudioSource()"; |
| 461 | 462 |
| 462 scoped_refptr<WebAudioCapturerSource> | 463 scoped_refptr<WebAudioCapturerSource> |
| 463 webaudio_capturer_source(new WebAudioCapturerSource()); | 464 webaudio_capturer_source(new WebAudioCapturerSource()); |
| 464 MediaStreamAudioSource* source_data = new MediaStreamAudioSource(); | 465 MediaStreamAudioSource* source_data = new MediaStreamAudioSource(); |
| 465 | 466 |
| 466 // Use the current default capturer for the WebAudio track so that the | 467 // 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 // WebAudio track can pass a valid delay value and |need_audio_processing| |
| 468 // flag to PeerConnection. | 469 // flag to PeerConnection. |
| 469 // TODO(xians): Remove this after moving APM to Chrome. | 470 // TODO(xians): Remove this after moving APM to Chrome. |
| 470 if (GetWebRtcAudioDevice()) { | 471 if (GetWebRtcAudioDevice()) { |
| 471 source_data->SetAudioCapturer( | 472 source_data->SetAudioCapturer( |
| 472 GetWebRtcAudioDevice()->GetDefaultCapturer()); | 473 GetWebRtcAudioDevice()->GetDefaultCapturer()); |
| 473 } | 474 } |
| 474 | 475 |
| 475 // Create a LocalAudioSource object which holds audio options. | 476 // Create a LocalAudioSource object which holds audio options. |
| 476 // SetLocalAudioSource() affects core audio parts in third_party/Libjingle. | 477 // SetLocalAudioSource() affects core audio parts in third_party/Libjingle. |
| 477 source_data->SetLocalAudioSource(CreateLocalAudioSource(NULL).get()); | 478 source_data->SetLocalAudioSource(CreateLocalAudioSource(NULL).get()); |
| 478 source->setExtraData(source_data); | 479 source->setExtraData(source_data); |
| 479 | 480 |
| 480 // Replace the default source with WebAudio as source instead. | 481 // Replace the default source with WebAudio as source instead. |
| 481 source->addAudioConsumer(webaudio_capturer_source.get()); | 482 source->addAudioConsumer(webaudio_capturer_source.get()); |
| 482 | 483 |
| 483 return webaudio_capturer_source; | 484 return webaudio_capturer_source; |
| 484 } | 485 } |
| 485 | 486 |
| 486 scoped_refptr<webrtc::VideoTrackInterface> | 487 scoped_refptr<webrtc::VideoTrackInterface> |
| 487 MediaStreamDependencyFactory::CreateLocalVideoTrack( | 488 PeerConnectionDependencyFactory::CreateLocalVideoTrack( |
| 488 const std::string& id, | 489 const std::string& id, |
| 489 webrtc::VideoSourceInterface* source) { | 490 webrtc::VideoSourceInterface* source) { |
| 490 return GetPcFactory()->CreateVideoTrack(id, source).get(); | 491 return GetPcFactory()->CreateVideoTrack(id, source).get(); |
| 491 } | 492 } |
| 492 | 493 |
| 493 scoped_refptr<webrtc::VideoTrackInterface> | 494 scoped_refptr<webrtc::VideoTrackInterface> |
| 494 MediaStreamDependencyFactory::CreateLocalVideoTrack( | 495 PeerConnectionDependencyFactory::CreateLocalVideoTrack( |
| 495 const std::string& id, cricket::VideoCapturer* capturer) { | 496 const std::string& id, cricket::VideoCapturer* capturer) { |
| 496 if (!capturer) { | 497 if (!capturer) { |
| 497 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer."; | 498 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer."; |
| 498 return NULL; | 499 return NULL; |
| 499 } | 500 } |
| 500 | 501 |
| 501 // Create video source from the |capturer|. | 502 // Create video source from the |capturer|. |
| 502 scoped_refptr<webrtc::VideoSourceInterface> source = | 503 scoped_refptr<webrtc::VideoSourceInterface> source = |
| 503 GetPcFactory()->CreateVideoSource(capturer, NULL).get(); | 504 GetPcFactory()->CreateVideoSource(capturer, NULL).get(); |
| 504 | 505 |
| 505 // Create native track from the source. | 506 // Create native track from the source. |
| 506 return GetPcFactory()->CreateVideoTrack(id, source.get()).get(); | 507 return GetPcFactory()->CreateVideoTrack(id, source.get()).get(); |
| 507 } | 508 } |
| 508 | 509 |
| 509 webrtc::SessionDescriptionInterface* | 510 webrtc::SessionDescriptionInterface* |
| 510 MediaStreamDependencyFactory::CreateSessionDescription( | 511 PeerConnectionDependencyFactory::CreateSessionDescription( |
| 511 const std::string& type, | 512 const std::string& type, |
| 512 const std::string& sdp, | 513 const std::string& sdp, |
| 513 webrtc::SdpParseError* error) { | 514 webrtc::SdpParseError* error) { |
| 514 return webrtc::CreateSessionDescription(type, sdp, error); | 515 return webrtc::CreateSessionDescription(type, sdp, error); |
| 515 } | 516 } |
| 516 | 517 |
| 517 webrtc::IceCandidateInterface* MediaStreamDependencyFactory::CreateIceCandidate( | 518 webrtc::IceCandidateInterface* |
| 519 PeerConnectionDependencyFactory::CreateIceCandidate( |
| 518 const std::string& sdp_mid, | 520 const std::string& sdp_mid, |
| 519 int sdp_mline_index, | 521 int sdp_mline_index, |
| 520 const std::string& sdp) { | 522 const std::string& sdp) { |
| 521 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp); | 523 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp); |
| 522 } | 524 } |
| 523 | 525 |
| 524 WebRtcAudioDeviceImpl* | 526 WebRtcAudioDeviceImpl* |
| 525 MediaStreamDependencyFactory::GetWebRtcAudioDevice() { | 527 PeerConnectionDependencyFactory::GetWebRtcAudioDevice() { |
| 526 return audio_device_.get(); | 528 return audio_device_.get(); |
| 527 } | 529 } |
| 528 | 530 |
| 529 void MediaStreamDependencyFactory::InitializeWorkerThread( | 531 void PeerConnectionDependencyFactory::InitializeWorkerThread( |
| 530 talk_base::Thread** thread, | 532 talk_base::Thread** thread, |
| 531 base::WaitableEvent* event) { | 533 base::WaitableEvent* event) { |
| 532 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 534 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 533 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | 535 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
| 534 *thread = jingle_glue::JingleThreadWrapper::current(); | 536 *thread = jingle_glue::JingleThreadWrapper::current(); |
| 535 event->Signal(); | 537 event->Signal(); |
| 536 } | 538 } |
| 537 | 539 |
| 538 void MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread( | 540 void PeerConnectionDependencyFactory::CreateIpcNetworkManagerOnWorkerThread( |
| 539 base::WaitableEvent* event) { | 541 base::WaitableEvent* event) { |
| 540 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop()); | 542 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop()); |
| 541 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_.get()); | 543 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_.get()); |
| 542 event->Signal(); | 544 event->Signal(); |
| 543 } | 545 } |
| 544 | 546 |
| 545 void MediaStreamDependencyFactory::DeleteIpcNetworkManager() { | 547 void PeerConnectionDependencyFactory::DeleteIpcNetworkManager() { |
| 546 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop()); | 548 DCHECK_EQ(base::MessageLoop::current(), chrome_worker_thread_.message_loop()); |
| 547 delete network_manager_; | 549 delete network_manager_; |
| 548 network_manager_ = NULL; | 550 network_manager_ = NULL; |
| 549 } | 551 } |
| 550 | 552 |
| 551 void MediaStreamDependencyFactory::CleanupPeerConnectionFactory() { | 553 void PeerConnectionDependencyFactory::CleanupPeerConnectionFactory() { |
| 552 pc_factory_ = NULL; | 554 pc_factory_ = NULL; |
| 553 if (network_manager_) { | 555 if (network_manager_) { |
| 554 // The network manager needs to free its resources on the thread they were | 556 // The network manager needs to free its resources on the thread they were |
| 555 // created, which is the worked thread. | 557 // created, which is the worked thread. |
| 556 if (chrome_worker_thread_.IsRunning()) { | 558 if (chrome_worker_thread_.IsRunning()) { |
| 557 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( | 559 chrome_worker_thread_.message_loop()->PostTask(FROM_HERE, base::Bind( |
| 558 &MediaStreamDependencyFactory::DeleteIpcNetworkManager, | 560 &PeerConnectionDependencyFactory::DeleteIpcNetworkManager, |
| 559 base::Unretained(this))); | 561 base::Unretained(this))); |
| 560 // Stopping the thread will wait until all tasks have been | 562 // Stopping the thread will wait until all tasks have been |
| 561 // processed before returning. We wait for the above task to finish before | 563 // processed before returning. We wait for the above task to finish before |
| 562 // letting the the function continue to avoid any potential race issues. | 564 // letting the the function continue to avoid any potential race issues. |
| 563 chrome_worker_thread_.Stop(); | 565 chrome_worker_thread_.Stop(); |
| 564 } else { | 566 } else { |
| 565 NOTREACHED() << "Worker thread not running."; | 567 NOTREACHED() << "Worker thread not running."; |
| 566 } | 568 } |
| 567 } | 569 } |
| 568 } | 570 } |
| 569 | 571 |
| 570 scoped_refptr<WebRtcAudioCapturer> | 572 scoped_refptr<WebRtcAudioCapturer> |
| 571 MediaStreamDependencyFactory::CreateAudioCapturer( | 573 PeerConnectionDependencyFactory::CreateAudioCapturer( |
| 572 int render_view_id, | 574 int render_view_id, |
| 573 const StreamDeviceInfo& device_info, | 575 const StreamDeviceInfo& device_info, |
| 574 const blink::WebMediaConstraints& constraints, | 576 const blink::WebMediaConstraints& constraints, |
| 575 MediaStreamAudioSource* audio_source) { | 577 MediaStreamAudioSource* audio_source) { |
| 576 // TODO(xians): Handle the cases when gUM is called without a proper render | 578 // TODO(xians): Handle the cases when gUM is called without a proper render |
| 577 // view, for example, by an extension. | 579 // view, for example, by an extension. |
| 578 DCHECK_GE(render_view_id, 0); | 580 DCHECK_GE(render_view_id, 0); |
| 579 | 581 |
| 580 EnsureWebRtcAudioDeviceImpl(); | 582 EnsureWebRtcAudioDeviceImpl(); |
| 581 DCHECK(GetWebRtcAudioDevice()); | 583 DCHECK(GetWebRtcAudioDevice()); |
| 582 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info, | 584 return WebRtcAudioCapturer::CreateCapturer(render_view_id, device_info, |
| 583 constraints, | 585 constraints, |
| 584 GetWebRtcAudioDevice(), | 586 GetWebRtcAudioDevice(), |
| 585 audio_source); | 587 audio_source); |
| 586 } | 588 } |
| 587 | 589 |
| 588 void MediaStreamDependencyFactory::AddNativeAudioTrackToBlinkTrack( | 590 void PeerConnectionDependencyFactory::AddNativeAudioTrackToBlinkTrack( |
| 589 webrtc::MediaStreamTrackInterface* native_track, | 591 webrtc::MediaStreamTrackInterface* native_track, |
| 590 const blink::WebMediaStreamTrack& webkit_track, | 592 const blink::WebMediaStreamTrack& webkit_track, |
| 591 bool is_local_track) { | 593 bool is_local_track) { |
| 592 DCHECK(!webkit_track.isNull() && !webkit_track.extraData()); | 594 DCHECK(!webkit_track.isNull() && !webkit_track.extraData()); |
| 593 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, | 595 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, |
| 594 webkit_track.source().type()); | 596 webkit_track.source().type()); |
| 595 blink::WebMediaStreamTrack track = webkit_track; | 597 blink::WebMediaStreamTrack track = webkit_track; |
| 596 | 598 |
| 597 DVLOG(1) << "AddNativeTrackToBlinkTrack() audio"; | 599 DVLOG(1) << "AddNativeTrackToBlinkTrack() audio"; |
| 598 track.setExtraData( | 600 track.setExtraData( |
| 599 new MediaStreamTrack( | 601 new MediaStreamTrack( |
| 600 static_cast<webrtc::AudioTrackInterface*>(native_track), | 602 static_cast<webrtc::AudioTrackInterface*>(native_track), |
| 601 is_local_track)); | 603 is_local_track)); |
| 602 } | 604 } |
| 603 | 605 |
| 604 scoped_refptr<base::MessageLoopProxy> | 606 scoped_refptr<base::MessageLoopProxy> |
| 605 MediaStreamDependencyFactory::GetWebRtcWorkerThread() const { | 607 PeerConnectionDependencyFactory::GetWebRtcWorkerThread() const { |
| 606 DCHECK(CalledOnValidThread()); | 608 DCHECK(CalledOnValidThread()); |
| 607 return chrome_worker_thread_.message_loop_proxy(); | 609 return chrome_worker_thread_.message_loop_proxy(); |
| 608 } | 610 } |
| 609 | 611 |
| 610 bool MediaStreamDependencyFactory::OnControlMessageReceived( | 612 bool PeerConnectionDependencyFactory::OnControlMessageReceived( |
| 611 const IPC::Message& message) { | 613 const IPC::Message& message) { |
| 612 bool handled = true; | 614 bool handled = true; |
| 613 IPC_BEGIN_MESSAGE_MAP(MediaStreamDependencyFactory, message) | 615 IPC_BEGIN_MESSAGE_MAP(PeerConnectionDependencyFactory, message) |
| 614 IPC_MESSAGE_HANDLER(MediaStreamMsg_EnableAecDump, OnAecDumpFile) | 616 IPC_MESSAGE_HANDLER(MediaStreamMsg_EnableAecDump, OnAecDumpFile) |
| 615 IPC_MESSAGE_HANDLER(MediaStreamMsg_DisableAecDump, OnDisableAecDump) | 617 IPC_MESSAGE_HANDLER(MediaStreamMsg_DisableAecDump, OnDisableAecDump) |
| 616 IPC_MESSAGE_UNHANDLED(handled = false) | 618 IPC_MESSAGE_UNHANDLED(handled = false) |
| 617 IPC_END_MESSAGE_MAP() | 619 IPC_END_MESSAGE_MAP() |
| 618 return handled; | 620 return handled; |
| 619 } | 621 } |
| 620 | 622 |
| 621 void MediaStreamDependencyFactory::OnAecDumpFile( | 623 void PeerConnectionDependencyFactory::OnAecDumpFile( |
| 622 IPC::PlatformFileForTransit file_handle) { | 624 IPC::PlatformFileForTransit file_handle) { |
| 623 DCHECK(!aec_dump_file_.IsValid()); | 625 DCHECK(!aec_dump_file_.IsValid()); |
| 624 base::File file = IPC::PlatformFileForTransitToFile(file_handle); | 626 base::File file = IPC::PlatformFileForTransitToFile(file_handle); |
| 625 DCHECK(file.IsValid()); | 627 DCHECK(file.IsValid()); |
| 626 | 628 |
| 627 if (CommandLine::ForCurrentProcess()->HasSwitch( | 629 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 628 switches::kEnableAudioTrackProcessing)) { | 630 switches::kEnableAudioTrackProcessing)) { |
| 629 EnsureWebRtcAudioDeviceImpl(); | 631 EnsureWebRtcAudioDeviceImpl(); |
| 630 GetWebRtcAudioDevice()->EnableAecDump(file.Pass()); | 632 GetWebRtcAudioDevice()->EnableAecDump(file.Pass()); |
| 631 return; | 633 return; |
| 632 } | 634 } |
| 633 | 635 |
| 634 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing | 636 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing |
| 635 // is removed. | 637 // is removed. |
| 636 if (PeerConnectionFactoryCreated()) | 638 if (PeerConnectionFactoryCreated()) |
| 637 StartAecDump(file.Pass()); | 639 StartAecDump(file.Pass()); |
| 638 else | 640 else |
| 639 aec_dump_file_ = file.Pass(); | 641 aec_dump_file_ = file.Pass(); |
| 640 } | 642 } |
| 641 | 643 |
| 642 void MediaStreamDependencyFactory::OnDisableAecDump() { | 644 void PeerConnectionDependencyFactory::OnDisableAecDump() { |
| 643 if (CommandLine::ForCurrentProcess()->HasSwitch( | 645 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 644 switches::kEnableAudioTrackProcessing)) { | 646 switches::kEnableAudioTrackProcessing)) { |
| 645 GetWebRtcAudioDevice()->DisableAecDump(); | 647 GetWebRtcAudioDevice()->DisableAecDump(); |
| 646 return; | 648 return; |
| 647 } | 649 } |
| 648 | 650 |
| 649 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing | 651 // TODO(xians): Remove the following code after kEnableAudioTrackProcessing |
| 650 // is removed. | 652 // is removed. |
| 651 if (aec_dump_file_.IsValid()) | 653 if (aec_dump_file_.IsValid()) |
| 652 aec_dump_file_.Close(); | 654 aec_dump_file_.Close(); |
| 653 } | 655 } |
| 654 | 656 |
| 655 void MediaStreamDependencyFactory::StartAecDump(base::File aec_dump_file) { | 657 void PeerConnectionDependencyFactory::StartAecDump(base::File aec_dump_file) { |
| 656 // |pc_factory_| always takes ownership of |aec_dump_file|. If StartAecDump() | 658 // |pc_factory_| always takes ownership of |aec_dump_file|. If StartAecDump() |
| 657 // fails, |aec_dump_file| will be closed. | 659 // fails, |aec_dump_file| will be closed. |
| 658 if (!GetPcFactory()->StartAecDump(aec_dump_file.TakePlatformFile())) | 660 if (!GetPcFactory()->StartAecDump(aec_dump_file.TakePlatformFile())) |
| 659 VLOG(1) << "Could not start AEC dump."; | 661 VLOG(1) << "Could not start AEC dump."; |
| 660 } | 662 } |
| 661 | 663 |
| 662 void MediaStreamDependencyFactory::EnsureWebRtcAudioDeviceImpl() { | 664 void PeerConnectionDependencyFactory::EnsureWebRtcAudioDeviceImpl() { |
| 663 if (audio_device_) | 665 if (audio_device_) |
| 664 return; | 666 return; |
| 665 | 667 |
| 666 audio_device_ = new WebRtcAudioDeviceImpl(); | 668 audio_device_ = new WebRtcAudioDeviceImpl(); |
| 667 } | 669 } |
| 668 | 670 |
| 669 } // namespace content | 671 } // namespace content |
| OLD | NEW |