| 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/media_stream_dependency_factory.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 CreateVideoSource(source_data->device_info().session_id, | 221 CreateVideoSource(source_data->device_info().session_id, |
| 222 is_screencast, | 222 is_screencast, |
| 223 &native_video_constraints)); | 223 &native_video_constraints)); |
| 224 source_observer->AddSource(source_data->video_source()); | 224 source_observer->AddSource(source_data->video_source()); |
| 225 } | 225 } |
| 226 source_observer->StartObservering(); | 226 source_observer->StartObservering(); |
| 227 } | 227 } |
| 228 | 228 |
| 229 void MediaStreamDependencyFactory::CreateNativeLocalMediaStream( | 229 void MediaStreamDependencyFactory::CreateNativeLocalMediaStream( |
| 230 WebKit::WebMediaStreamDescriptor* description) { | 230 WebKit::WebMediaStreamDescriptor* description) { |
| 231 DCHECK(PeerConnectionFactoryCreated()); | 231 if (!EnsurePeerConnectionFactory()) { |
| 232 DVLOG(1) << "EnsurePeerConnectionFactory() failed!"; |
| 233 return; |
| 234 } |
| 235 |
| 236 DVLOG(1) << "---MediaStreamDependencyFactory::CreateNativeLocalMediaStream()"; |
| 232 | 237 |
| 233 std::string label = UTF16ToUTF8(description->label()); | 238 std::string label = UTF16ToUTF8(description->label()); |
| 234 scoped_refptr<webrtc::LocalMediaStreamInterface> native_stream = | 239 scoped_refptr<webrtc::LocalMediaStreamInterface> native_stream = |
| 235 CreateLocalMediaStream(label); | 240 CreateLocalMediaStream(label); |
| 236 | 241 |
| 242 WebRtcAudioCapturer* capturer = GetWebRtcAudioDevice()->capturer(); |
| 243 if (!capturer) |
| 244 DVLOG(1) << "CreateNativeLocalMediaStream: missing WebRtcAudioCapturer."; |
| 245 |
| 237 // Add audio tracks. | 246 // Add audio tracks. |
| 238 WebKit::WebVector<WebKit::WebMediaStreamComponent> audio_components; | 247 WebKit::WebVector<WebKit::WebMediaStreamComponent> audio_components; |
| 239 description->audioSources(audio_components); | 248 description->audioSources(audio_components); |
| 249 |
| 240 for (size_t i = 0; i < audio_components.size(); ++i) { | 250 for (size_t i = 0; i < audio_components.size(); ++i) { |
| 241 const WebKit::WebMediaStreamSource& source = audio_components[i].source(); | 251 WebKit::WebMediaStreamSource source = audio_components[i].source(); |
| 242 MediaStreamSourceExtraData* source_data = | 252 |
| 243 static_cast<MediaStreamSourceExtraData*>(source.extraData()); | 253 // See if we're adding a WebAudio MediaStream. |
| 244 if (!source_data) { | 254 if (source.requiresAudioConsumer()) { |
| 245 // TODO(perkj): Implement support for sources from remote MediaStreams. | 255 if (!webaudio_capturer_source_.get() && capturer) { |
| 246 NOTIMPLEMENTED(); | 256 DVLOG(1) << "CreateNativeLocalMediaStream: WebAudio MediaStream."; |
| 247 continue; | 257 DCHECK(GetWebRtcAudioDevice()); |
| 258 |
| 259 // TODO(crogers, xians): In reality we should be able to send a unique |
| 260 // audio stream to each PeerConnection separately. But currently WebRTC |
| 261 // is only able to handle a global audio stream sent to ALL peers. |
| 262 |
| 263 // TODO(henrika) - why store it here webaudio_capturer_source_? |
| 264 |
| 265 webaudio_capturer_source_ = new WebAudioCapturerSource(capturer); |
| 266 |
| 267 // For lifetime, we're relying on the fact that |
| 268 // |webaudio_capturer_source_| will live longer than any |
| 269 // MediaStreamSource, since we're never calling removeAudioConsumer(). |
| 270 source.addAudioConsumer(webaudio_capturer_source_.get()); |
| 271 |
| 272 scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track( |
| 273 CreateLocalAudioTrack(label + "a0", NULL)); |
| 274 native_stream->AddTrack(audio_track); |
| 275 audio_track->set_enabled(audio_components[i].isEnabled()); |
| 276 } else { |
| 277 // TODO(crogers): this is very likely to be less important, but |
| 278 // in theory we should be able to "connect" multiple WebAudio |
| 279 // MediaStreams to a single peer, mixing their results. |
| 280 // Instead we just ignore additional ones after the first. |
| 281 DVLOG(1) |
| 282 << "Multiple MediaStreamAudioDestinationNodes not yet supported!"; |
| 283 } |
| 284 } else { |
| 285 MediaStreamSourceExtraData* source_data = |
| 286 static_cast<MediaStreamSourceExtraData*>(source.extraData()); |
| 287 |
| 288 if (!source_data) { |
| 289 // TODO(perkj): Implement support for sources from |
| 290 // remote MediaStreams. |
| 291 NOTIMPLEMENTED(); |
| 292 continue; |
| 293 } |
| 294 |
| 295 // TODO(perkj): Refactor the creation of audio tracks to use a proper |
| 296 // interface for receiving audio input data. Currently NULL is passed |
| 297 // since the |audio_device| is the wrong class and is unused. |
| 298 scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track( |
| 299 CreateLocalAudioTrack(UTF16ToUTF8(source.id()), NULL)); |
| 300 native_stream->AddTrack(audio_track); |
| 301 audio_track->set_enabled(audio_components[i].isEnabled()); |
| 302 // TODO(xians): This set the source of all audio tracks to the same |
| 303 // microphone. Implement support for setting the source per audio track |
| 304 // instead. |
| 305 SetAudioDeviceSessionId(source_data->device_info().session_id); |
| 248 } | 306 } |
| 249 // TODO(perkj): Refactor the creation of audio tracks to use a proper | |
| 250 // interface for receiving audio input data. Currently NULL is passed since | |
| 251 // the |audio_device| is the wrong class and is unused. | |
| 252 scoped_refptr<webrtc::LocalAudioTrackInterface> audio_track( | |
| 253 CreateLocalAudioTrack(UTF16ToUTF8(source.id()), NULL)); | |
| 254 native_stream->AddTrack(audio_track); | |
| 255 audio_track->set_enabled(audio_components[i].isEnabled()); | |
| 256 // TODO(xians): This set the source of all audio tracks to the same | |
| 257 // microphone. Implement support for setting the source per audio track | |
| 258 // instead. | |
| 259 SetAudioDeviceSessionId(source_data->device_info().session_id); | |
| 260 } | 307 } |
| 261 | 308 |
| 262 // Add video tracks. | 309 // Add video tracks. |
| 263 WebKit::WebVector<WebKit::WebMediaStreamComponent> video_components; | 310 WebKit::WebVector<WebKit::WebMediaStreamComponent> video_components; |
| 264 description->videoSources(video_components); | 311 description->videoSources(video_components); |
| 265 for (size_t i = 0; i < video_components.size(); ++i) { | 312 for (size_t i = 0; i < video_components.size(); ++i) { |
| 266 const WebKit::WebMediaStreamSource& source = video_components[i].source(); | 313 const WebKit::WebMediaStreamSource& source = video_components[i].source(); |
| 267 MediaStreamSourceExtraData* source_data = | 314 MediaStreamSourceExtraData* source_data = |
| 268 static_cast<MediaStreamSourceExtraData*>(source.extraData()); | 315 static_cast<MediaStreamSourceExtraData*>(source.extraData()); |
| 269 if (!source_data || !source_data->video_source()) { | 316 if (!source_data || !source_data->video_source()) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 network_manager_, | 375 network_manager_, |
| 329 socket_factory_.get(), | 376 socket_factory_.get(), |
| 330 web_frame); | 377 web_frame); |
| 331 return pc_factory_->CreatePeerConnection( | 378 return pc_factory_->CreatePeerConnection( |
| 332 ice_servers, constraints, pa_factory, observer).get(); | 379 ice_servers, constraints, pa_factory, observer).get(); |
| 333 } | 380 } |
| 334 | 381 |
| 335 scoped_refptr<webrtc::LocalMediaStreamInterface> | 382 scoped_refptr<webrtc::LocalMediaStreamInterface> |
| 336 MediaStreamDependencyFactory::CreateLocalMediaStream( | 383 MediaStreamDependencyFactory::CreateLocalMediaStream( |
| 337 const std::string& label) { | 384 const std::string& label) { |
| 385 DVLOG(1) << "---MediaStreamDependencyFactory::CreateLocalMediaStream()"; |
| 338 return pc_factory_->CreateLocalMediaStream(label).get(); | 386 return pc_factory_->CreateLocalMediaStream(label).get(); |
| 339 } | 387 } |
| 340 | 388 |
| 341 scoped_refptr<webrtc::VideoSourceInterface> | 389 scoped_refptr<webrtc::VideoSourceInterface> |
| 342 MediaStreamDependencyFactory::CreateVideoSource( | 390 MediaStreamDependencyFactory::CreateVideoSource( |
| 343 int video_session_id, | 391 int video_session_id, |
| 344 bool is_screencast, | 392 bool is_screencast, |
| 345 const webrtc::MediaConstraintsInterface* constraints) { | 393 const webrtc::MediaConstraintsInterface* constraints) { |
| 394 DVLOG(1) << "---MediaStreamDependencyFactory::CreateVideoSource()"; |
| 346 RtcVideoCapturer* capturer = new RtcVideoCapturer( | 395 RtcVideoCapturer* capturer = new RtcVideoCapturer( |
| 347 video_session_id, vc_manager_.get(), is_screencast); | 396 video_session_id, vc_manager_.get(), is_screencast); |
| 348 | 397 |
| 349 // The video source takes ownership of |capturer|. | 398 // The video source takes ownership of |capturer|. |
| 350 scoped_refptr<webrtc::VideoSourceInterface> source = | 399 scoped_refptr<webrtc::VideoSourceInterface> source = |
| 351 pc_factory_->CreateVideoSource(capturer, constraints).get(); | 400 pc_factory_->CreateVideoSource(capturer, constraints).get(); |
| 352 return source; | 401 return source; |
| 353 } | 402 } |
| 354 | 403 |
| 355 scoped_refptr<webrtc::VideoTrackInterface> | 404 scoped_refptr<webrtc::VideoTrackInterface> |
| 356 MediaStreamDependencyFactory::CreateLocalVideoTrack( | 405 MediaStreamDependencyFactory::CreateLocalVideoTrack( |
| 357 const std::string& label, | 406 const std::string& label, |
| 358 webrtc::VideoSourceInterface* source) { | 407 webrtc::VideoSourceInterface* source) { |
| 408 DVLOG(1) << "---MediaStreamDependencyFactory::CreateLocalVideoTrack()"; |
| 359 return pc_factory_->CreateVideoTrack(label, source).get(); | 409 return pc_factory_->CreateVideoTrack(label, source).get(); |
| 360 } | 410 } |
| 361 | 411 |
| 362 scoped_refptr<webrtc::LocalAudioTrackInterface> | 412 scoped_refptr<webrtc::LocalAudioTrackInterface> |
| 363 MediaStreamDependencyFactory::CreateLocalAudioTrack( | 413 MediaStreamDependencyFactory::CreateLocalAudioTrack( |
| 364 const std::string& label, | 414 const std::string& label, |
| 365 webrtc::AudioDeviceModule* audio_device) { | 415 webrtc::AudioDeviceModule* audio_device) { |
| 416 DVLOG(1) << "---MediaStreamDependencyFactory::CreateLocalAudioTrack()"; |
| 366 return pc_factory_->CreateLocalAudioTrack(label, audio_device).get(); | 417 return pc_factory_->CreateLocalAudioTrack(label, audio_device).get(); |
| 367 } | 418 } |
| 368 | 419 |
| 369 webrtc::SessionDescriptionInterface* | 420 webrtc::SessionDescriptionInterface* |
| 370 MediaStreamDependencyFactory::CreateSessionDescription(const std::string& sdp) { | 421 MediaStreamDependencyFactory::CreateSessionDescription(const std::string& sdp) { |
| 371 return webrtc::CreateSessionDescription(sdp); | 422 return webrtc::CreateSessionDescription(sdp); |
| 372 } | 423 } |
| 373 | 424 |
| 374 webrtc::SessionDescriptionInterface* | 425 webrtc::SessionDescriptionInterface* |
| 375 MediaStreamDependencyFactory::CreateSessionDescription(const std::string& type, | 426 MediaStreamDependencyFactory::CreateSessionDescription(const std::string& type, |
| 376 const std::string& sdp) { | 427 const std::string& sdp) { |
| 377 return webrtc::CreateSessionDescription(type, sdp); | 428 return webrtc::CreateSessionDescription(type, sdp); |
| 378 } | 429 } |
| 379 | 430 |
| 380 webrtc::IceCandidateInterface* MediaStreamDependencyFactory::CreateIceCandidate( | 431 webrtc::IceCandidateInterface* MediaStreamDependencyFactory::CreateIceCandidate( |
| 381 const std::string& sdp_mid, | 432 const std::string& sdp_mid, |
| 382 int sdp_mline_index, | 433 int sdp_mline_index, |
| 383 const std::string& sdp) { | 434 const std::string& sdp) { |
| 384 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp); | 435 return webrtc::CreateIceCandidate(sdp_mid, sdp_mline_index, sdp); |
| 385 } | 436 } |
| 386 | 437 |
| 387 WebRtcAudioDeviceImpl* | 438 WebRtcAudioDeviceImpl* |
| 388 MediaStreamDependencyFactory::GetWebRtcAudioDevice() { | 439 MediaStreamDependencyFactory::GetWebRtcAudioDevice() { |
| 389 return audio_device_; | 440 return audio_device_; |
| 390 } | 441 } |
| 391 | 442 |
| 392 void MediaStreamDependencyFactory::SetAudioDeviceSessionId(int session_id) { | 443 void MediaStreamDependencyFactory::SetAudioDeviceSessionId(int session_id) { |
| 444 DVLOG(1) << "--- MediaStreamDependencyFactory::SetAudioDeviceSessionId()"; |
| 445 DVLOG(1) << "--- session_id: " << session_id; |
| 393 audio_device_->SetSessionId(session_id); | 446 audio_device_->SetSessionId(session_id); |
| 394 } | 447 } |
| 395 | 448 |
| 396 void MediaStreamDependencyFactory::InitializeWorkerThread( | 449 void MediaStreamDependencyFactory::InitializeWorkerThread( |
| 397 talk_base::Thread** thread, | 450 talk_base::Thread** thread, |
| 398 base::WaitableEvent* event) { | 451 base::WaitableEvent* event) { |
| 399 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 452 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 400 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | 453 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
| 401 *thread = jingle_glue::JingleThreadWrapper::current(); | 454 *thread = jingle_glue::JingleThreadWrapper::current(); |
| 402 event->Signal(); | 455 event->Signal(); |
| 403 } | 456 } |
| 404 | 457 |
| 405 void MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread( | 458 void MediaStreamDependencyFactory::CreateIpcNetworkManagerOnWorkerThread( |
| 406 base::WaitableEvent* event) { | 459 base::WaitableEvent* event) { |
| 407 DCHECK_EQ(MessageLoop::current(), chrome_worker_thread_.message_loop()); | 460 DCHECK_EQ(MessageLoop::current(), chrome_worker_thread_.message_loop()); |
| 408 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_); | 461 network_manager_ = new IpcNetworkManager(p2p_socket_dispatcher_); |
| 409 event->Signal(); | 462 event->Signal(); |
| 410 } | 463 } |
| 411 | 464 |
| 412 void MediaStreamDependencyFactory::DeleteIpcNetworkManager() { | 465 void MediaStreamDependencyFactory::DeleteIpcNetworkManager() { |
| 413 DCHECK_EQ(MessageLoop::current(), chrome_worker_thread_.message_loop()); | 466 DCHECK_EQ(MessageLoop::current(), chrome_worker_thread_.message_loop()); |
| 414 delete network_manager_; | 467 delete network_manager_; |
| 415 network_manager_ = NULL; | 468 network_manager_ = NULL; |
| 416 } | 469 } |
| 417 | 470 |
| 418 bool MediaStreamDependencyFactory::EnsurePeerConnectionFactory() { | 471 bool MediaStreamDependencyFactory::EnsurePeerConnectionFactory() { |
| 472 DVLOG(1) << "--- MediaStreamDependencyFactory::EnsurePeerConnectionFactory()"; |
| 419 DCHECK(CalledOnValidThread()); | 473 DCHECK(CalledOnValidThread()); |
| 420 if (PeerConnectionFactoryCreated()) | 474 if (PeerConnectionFactoryCreated()) |
| 421 return true; | 475 return true; |
| 422 | 476 |
| 423 if (!signaling_thread_) { | 477 if (!signaling_thread_) { |
| 424 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 478 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 425 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); | 479 jingle_glue::JingleThreadWrapper::current()->set_send_allowed(true); |
| 426 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); | 480 signaling_thread_ = jingle_glue::JingleThreadWrapper::current(); |
| 427 CHECK(signaling_thread_); | 481 CHECK(signaling_thread_); |
| 428 } | 482 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 // processed before returning. We wait for the above task to finish before | 538 // processed before returning. We wait for the above task to finish before |
| 485 // letting the the function continue to avoid any potential race issues. | 539 // letting the the function continue to avoid any potential race issues. |
| 486 chrome_worker_thread_.Stop(); | 540 chrome_worker_thread_.Stop(); |
| 487 } else { | 541 } else { |
| 488 NOTREACHED() << "Worker thread not running."; | 542 NOTREACHED() << "Worker thread not running."; |
| 489 } | 543 } |
| 490 } | 544 } |
| 491 } | 545 } |
| 492 | 546 |
| 493 } // namespace content | 547 } // namespace content |
| OLD | NEW |