| 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/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" |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 WebKit::WebMediaStream* web_stream, | 365 WebKit::WebMediaStream* web_stream, |
| 366 const MediaStreamExtraData::StreamStopCallback& stream_stop) { | 366 const MediaStreamExtraData::StreamStopCallback& stream_stop) { |
| 367 CreateNativeLocalMediaStream(web_stream); | 367 CreateNativeLocalMediaStream(web_stream); |
| 368 | 368 |
| 369 MediaStreamExtraData* extra_data = | 369 MediaStreamExtraData* extra_data = |
| 370 static_cast<MediaStreamExtraData*>(web_stream->extraData()); | 370 static_cast<MediaStreamExtraData*>(web_stream->extraData()); |
| 371 extra_data->SetLocalStreamStopCallback(stream_stop); | 371 extra_data->SetLocalStreamStopCallback(stream_stop); |
| 372 } | 372 } |
| 373 | 373 |
| 374 bool MediaStreamDependencyFactory::AddNativeMediaStreamTrack( | 374 bool MediaStreamDependencyFactory::AddNativeMediaStreamTrack( |
| 375 const WebKit::WebMediaStream& stream, | 375 const WebKit::WebMediaStream& stream, |
| 376 const WebKit::WebMediaStreamTrack& track) { | 376 const WebKit::WebMediaStreamTrack& track) { |
| 377 MediaStreamExtraData* extra_data = | 377 MediaStreamExtraData* extra_data = |
| 378 static_cast<MediaStreamExtraData*>(stream.extraData()); | 378 static_cast<MediaStreamExtraData*>(stream.extraData()); |
| 379 webrtc::MediaStreamInterface* native_stream = extra_data->stream().get(); | 379 webrtc::MediaStreamInterface* native_stream = extra_data->stream().get(); |
| 380 DCHECK(native_stream); | 380 DCHECK(native_stream); |
| 381 | 381 |
| 382 WebKit::WebMediaStreamSource source = track.source(); | 382 WebKit::WebMediaStreamSource source = track.source(); |
| 383 MediaStreamSourceExtraData* source_data = | 383 MediaStreamSourceExtraData* source_data = |
| 384 static_cast<MediaStreamSourceExtraData*>(source.extraData()); | 384 static_cast<MediaStreamSourceExtraData*>(source.extraData()); |
| 385 | 385 |
| 386 // In the future the constraints will belong to the track itself, but | 386 // In the future the constraints will belong to the track itself, but |
| 387 // right now they're on the source, so we fetch them from there. | 387 // right now they're on the source, so we fetch them from there. |
| 388 RTCMediaConstraints track_constraints(source.constraints()); | 388 RTCMediaConstraints track_constraints(source.constraints()); |
| 389 | 389 |
| 390 scoped_refptr<WebRtcAudioCapturer> capturer; | 390 scoped_refptr<WebAudioCapturerSource> webaudio_source; |
| 391 if (!source_data) { | 391 if (!source_data) { |
| 392 if (source.requiresAudioConsumer()) { | 392 if (source.requiresAudioConsumer()) { |
| 393 // We're adding a WebAudio MediaStream. | 393 // We're adding a WebAudio MediaStream. |
| 394 // Create a specific capturer for each WebAudio consumer. | 394 // Create a specific capturer for each WebAudio consumer. |
| 395 capturer = CreateWebAudioSource(&source, &track_constraints); | 395 webaudio_source = CreateWebAudioSource(&source, &track_constraints); |
| 396 source_data = | 396 source_data = |
| 397 static_cast<MediaStreamSourceExtraData*>(source.extraData()); | 397 static_cast<MediaStreamSourceExtraData*>(source.extraData()); |
| 398 } else { | 398 } else { |
| 399 // TODO(perkj): Implement support for sources from | 399 // TODO(perkj): Implement support for sources from |
| 400 // remote MediaStreams. | 400 // remote MediaStreams. |
| 401 NOTIMPLEMENTED(); | 401 NOTIMPLEMENTED(); |
| 402 return false; | 402 return false; |
| 403 } | 403 } |
| 404 } | 404 } |
| 405 | 405 |
| 406 WebKit::WebMediaStreamSource::Type type = track.source().type(); | 406 WebKit::WebMediaStreamSource::Type type = track.source().type(); |
| 407 DCHECK(type == WebKit::WebMediaStreamSource::TypeAudio || | 407 DCHECK(type == WebKit::WebMediaStreamSource::TypeAudio || |
| 408 type == WebKit::WebMediaStreamSource::TypeVideo); | 408 type == WebKit::WebMediaStreamSource::TypeVideo); |
| 409 | 409 |
| 410 std::string track_id = UTF16ToUTF8(track.id()); | 410 std::string track_id = UTF16ToUTF8(track.id()); |
| 411 if (source.type() == WebKit::WebMediaStreamSource::TypeAudio) { | 411 if (source.type() == WebKit::WebMediaStreamSource::TypeAudio) { |
| 412 if (!capturer.get() && GetWebRtcAudioDevice()) | 412 scoped_refptr<WebRtcAudioCapturer> capturer; |
| 413 if (GetWebRtcAudioDevice()) |
| 413 capturer = GetWebRtcAudioDevice()->GetDefaultCapturer(); | 414 capturer = GetWebRtcAudioDevice()->GetDefaultCapturer(); |
| 414 | 415 |
| 415 scoped_refptr<webrtc::AudioTrackInterface> audio_track( | 416 scoped_refptr<webrtc::AudioTrackInterface> audio_track( |
| 416 CreateLocalAudioTrack(track_id, | 417 CreateLocalAudioTrack(track_id, |
| 417 capturer, | 418 capturer, |
| 419 webaudio_source.get(), |
| 418 source_data->local_audio_source(), | 420 source_data->local_audio_source(), |
| 419 &track_constraints)); | 421 &track_constraints)); |
| 420 audio_track->set_enabled(track.isEnabled()); | 422 audio_track->set_enabled(track.isEnabled()); |
| 423 if (capturer.get()) { |
| 424 WebKit::WebMediaStreamTrack writable_track = track; |
| 425 writable_track.setSourceProvider(capturer->audio_source_provider()); |
| 426 } |
| 421 return native_stream->AddTrack(audio_track.get()); | 427 return native_stream->AddTrack(audio_track.get()); |
| 422 } else { | 428 } else { |
| 423 DCHECK(source.type() == WebKit::WebMediaStreamSource::TypeVideo); | 429 DCHECK(source.type() == WebKit::WebMediaStreamSource::TypeVideo); |
| 424 scoped_refptr<webrtc::VideoTrackInterface> video_track( | 430 scoped_refptr<webrtc::VideoTrackInterface> video_track( |
| 425 CreateLocalVideoTrack(track_id, source_data->video_source())); | 431 CreateLocalVideoTrack(track_id, source_data->video_source())); |
| 426 video_track->set_enabled(track.isEnabled()); | 432 video_track->set_enabled(track.isEnabled()); |
| 427 return native_stream->AddTrack(video_track.get()); | 433 return native_stream->AddTrack(video_track.get()); |
| 428 } | 434 } |
| 429 } | 435 } |
| 430 | 436 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 MediaStreamExtraData* extra_data = | 474 MediaStreamExtraData* extra_data = |
| 469 static_cast<MediaStreamExtraData*>(stream.extraData()); | 475 static_cast<MediaStreamExtraData*>(stream.extraData()); |
| 470 webrtc::MediaStreamInterface* native_stream = extra_data->stream().get(); | 476 webrtc::MediaStreamInterface* native_stream = extra_data->stream().get(); |
| 471 DCHECK(native_stream); | 477 DCHECK(native_stream); |
| 472 | 478 |
| 473 WebKit::WebMediaStreamSource::Type type = track.source().type(); | 479 WebKit::WebMediaStreamSource::Type type = track.source().type(); |
| 474 DCHECK(type == WebKit::WebMediaStreamSource::TypeAudio || | 480 DCHECK(type == WebKit::WebMediaStreamSource::TypeAudio || |
| 475 type == WebKit::WebMediaStreamSource::TypeVideo); | 481 type == WebKit::WebMediaStreamSource::TypeVideo); |
| 476 | 482 |
| 477 std::string track_id = UTF16ToUTF8(track.id()); | 483 std::string track_id = UTF16ToUTF8(track.id()); |
| 478 return type == WebKit::WebMediaStreamSource::TypeAudio ? | 484 if (type == WebKit::WebMediaStreamSource::TypeAudio) { |
| 479 native_stream->RemoveTrack(native_stream->FindAudioTrack(track_id)) : | 485 // Remove the source provider as the track is going away. |
| 480 native_stream->RemoveTrack(native_stream->FindVideoTrack(track_id)); | 486 WebKit::WebMediaStreamTrack writable_track = track; |
| 487 writable_track.setSourceProvider(NULL); |
| 488 return native_stream->RemoveTrack(native_stream->FindAudioTrack(track_id)); |
| 489 } |
| 490 |
| 491 CHECK_EQ(type, WebKit::WebMediaStreamSource::TypeVideo); |
| 492 return native_stream->RemoveTrack(native_stream->FindVideoTrack(track_id)); |
| 481 } | 493 } |
| 482 | 494 |
| 483 bool MediaStreamDependencyFactory::CreatePeerConnectionFactory() { | 495 bool MediaStreamDependencyFactory::CreatePeerConnectionFactory() { |
| 484 DVLOG(1) << "MediaStreamDependencyFactory::CreatePeerConnectionFactory()"; | 496 DVLOG(1) << "MediaStreamDependencyFactory::CreatePeerConnectionFactory()"; |
| 485 if (!pc_factory_.get()) { | 497 if (!pc_factory_.get()) { |
| 486 DCHECK(!audio_device_.get()); | 498 DCHECK(!audio_device_.get()); |
| 487 audio_device_ = new WebRtcAudioDeviceImpl(); | 499 audio_device_ = new WebRtcAudioDeviceImpl(); |
| 488 | 500 |
| 489 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory; | 501 scoped_ptr<cricket::WebRtcVideoDecoderFactory> decoder_factory; |
| 490 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory; | 502 scoped_ptr<cricket::WebRtcVideoEncoderFactory> encoder_factory; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 const webrtc::MediaConstraintsInterface* constraints) { | 599 const webrtc::MediaConstraintsInterface* constraints) { |
| 588 RtcVideoCapturer* capturer = new RtcVideoCapturer( | 600 RtcVideoCapturer* capturer = new RtcVideoCapturer( |
| 589 video_session_id, vc_manager_.get(), is_screencast); | 601 video_session_id, vc_manager_.get(), is_screencast); |
| 590 | 602 |
| 591 // The video source takes ownership of |capturer|. | 603 // The video source takes ownership of |capturer|. |
| 592 scoped_refptr<webrtc::VideoSourceInterface> source = | 604 scoped_refptr<webrtc::VideoSourceInterface> source = |
| 593 pc_factory_->CreateVideoSource(capturer, constraints).get(); | 605 pc_factory_->CreateVideoSource(capturer, constraints).get(); |
| 594 return source; | 606 return source; |
| 595 } | 607 } |
| 596 | 608 |
| 597 scoped_refptr<WebRtcAudioCapturer> | 609 scoped_refptr<WebAudioCapturerSource> |
| 598 MediaStreamDependencyFactory::CreateWebAudioSource( | 610 MediaStreamDependencyFactory::CreateWebAudioSource( |
| 599 WebKit::WebMediaStreamSource* source, | 611 WebKit::WebMediaStreamSource* source, |
| 600 RTCMediaConstraints* constraints) { | 612 RTCMediaConstraints* constraints) { |
| 601 DVLOG(1) << "MediaStreamDependencyFactory::CreateWebAudioSource()"; | 613 DVLOG(1) << "MediaStreamDependencyFactory::CreateWebAudioSource()"; |
| 602 DCHECK(GetWebRtcAudioDevice()); | 614 DCHECK(GetWebRtcAudioDevice()); |
| 603 | 615 |
| 604 // Set up the source and ensure that WebAudio is driving things instead of | |
| 605 // a microphone. For WebAudio, we always create a new capturer without | |
| 606 // calling initialize(), WebAudio will re-configure the capturer later on. | |
| 607 // Pass -1 as the |render_view_id| and an empty device struct to tell the | |
| 608 // capturer not to start the default source. | |
| 609 scoped_refptr<WebRtcAudioCapturer> capturer( | |
| 610 MaybeCreateAudioCapturer(-1, StreamDeviceInfo())); | |
| 611 DCHECK(capturer.get()); | |
| 612 | |
| 613 scoped_refptr<WebAudioCapturerSource> | 616 scoped_refptr<WebAudioCapturerSource> |
| 614 webaudio_capturer_source(new WebAudioCapturerSource(capturer.get())); | 617 webaudio_capturer_source(new WebAudioCapturerSource()); |
| 615 MediaStreamSourceExtraData* source_data = | 618 MediaStreamSourceExtraData* source_data = |
| 616 new content::MediaStreamSourceExtraData(webaudio_capturer_source.get()); | 619 new content::MediaStreamSourceExtraData(); |
| 617 | 620 |
| 618 // Create a LocalAudioSource object which holds audio options. | 621 // Create a LocalAudioSource object which holds audio options. |
| 619 // Use audio constraints where all values are true, i.e., enable | 622 // Use audio constraints where all values are true, i.e., enable |
| 620 // echo cancellation, automatic gain control, noise suppression and | 623 // echo cancellation, automatic gain control, noise suppression and |
| 621 // high-pass filter. SetLocalAudioSource() affects core audio parts in | 624 // high-pass filter. SetLocalAudioSource() affects core audio parts in |
| 622 // third_party/Libjingle. | 625 // third_party/Libjingle. |
| 623 ApplyFixedWebAudioConstraints(constraints); | 626 ApplyFixedWebAudioConstraints(constraints); |
| 624 source_data->SetLocalAudioSource(CreateLocalAudioSource(constraints).get()); | 627 source_data->SetLocalAudioSource(CreateLocalAudioSource(constraints).get()); |
| 625 source->setExtraData(source_data); | 628 source->setExtraData(source_data); |
| 626 | 629 |
| 627 // Replace the default source with WebAudio as source instead. | 630 // Replace the default source with WebAudio as source instead. |
| 628 source->addAudioConsumer(webaudio_capturer_source.get()); | 631 source->addAudioConsumer(webaudio_capturer_source.get()); |
| 629 | 632 |
| 630 return capturer; | 633 return webaudio_capturer_source; |
| 631 } | 634 } |
| 632 | 635 |
| 633 scoped_refptr<webrtc::VideoTrackInterface> | 636 scoped_refptr<webrtc::VideoTrackInterface> |
| 634 MediaStreamDependencyFactory::CreateLocalVideoTrack( | 637 MediaStreamDependencyFactory::CreateLocalVideoTrack( |
| 635 const std::string& id, | 638 const std::string& id, |
| 636 webrtc::VideoSourceInterface* source) { | 639 webrtc::VideoSourceInterface* source) { |
| 637 return pc_factory_->CreateVideoTrack(id, source).get(); | 640 return pc_factory_->CreateVideoTrack(id, source).get(); |
| 638 } | 641 } |
| 639 | 642 |
| 640 scoped_refptr<webrtc::VideoTrackInterface> | 643 scoped_refptr<webrtc::VideoTrackInterface> |
| 641 MediaStreamDependencyFactory::CreateLocalVideoTrack( | 644 MediaStreamDependencyFactory::CreateLocalVideoTrack( |
| 642 const std::string& id, cricket::VideoCapturer* capturer) { | 645 const std::string& id, cricket::VideoCapturer* capturer) { |
| 643 if (!capturer) { | 646 if (!capturer) { |
| 644 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer."; | 647 LOG(ERROR) << "CreateLocalVideoTrack called with null VideoCapturer."; |
| 645 return NULL; | 648 return NULL; |
| 646 } | 649 } |
| 647 | 650 |
| 648 // Create video source from the |capturer|. | 651 // Create video source from the |capturer|. |
| 649 scoped_refptr<webrtc::VideoSourceInterface> source = | 652 scoped_refptr<webrtc::VideoSourceInterface> source = |
| 650 pc_factory_->CreateVideoSource(capturer, NULL).get(); | 653 pc_factory_->CreateVideoSource(capturer, NULL).get(); |
| 651 | 654 |
| 652 // Create native track from the source. | 655 // Create native track from the source. |
| 653 return pc_factory_->CreateVideoTrack(id, source.get()).get(); | 656 return pc_factory_->CreateVideoTrack(id, source.get()).get(); |
| 654 } | 657 } |
| 655 | 658 |
| 656 scoped_refptr<webrtc::AudioTrackInterface> | 659 scoped_refptr<webrtc::AudioTrackInterface> |
| 657 MediaStreamDependencyFactory::CreateLocalAudioTrack( | 660 MediaStreamDependencyFactory::CreateLocalAudioTrack( |
| 658 const std::string& id, | 661 const std::string& id, |
| 659 const scoped_refptr<WebRtcAudioCapturer>& capturer, | 662 const scoped_refptr<WebRtcAudioCapturer>& capturer, |
| 663 WebAudioCapturerSource* webaudio_source, |
| 660 webrtc::AudioSourceInterface* source, | 664 webrtc::AudioSourceInterface* source, |
| 661 const webrtc::MediaConstraintsInterface* constraints) { | 665 const webrtc::MediaConstraintsInterface* constraints) { |
| 662 // TODO(xians): Merge |source| to the capturer(). We can't do this today | 666 // TODO(xians): Merge |source| to the capturer(). We can't do this today |
| 663 // because only one capturer() is supported while one |source| is created | 667 // because only one capturer() is supported while one |source| is created |
| 664 // for each audio track. | 668 // for each audio track. |
| 665 scoped_refptr<WebRtcLocalAudioTrack> audio_track( | 669 scoped_refptr<WebRtcLocalAudioTrack> audio_track( |
| 666 WebRtcLocalAudioTrack::Create(id, capturer, source, constraints)); | 670 WebRtcLocalAudioTrack::Create(id, capturer, webaudio_source, |
| 671 source, constraints)); |
| 672 |
| 667 // Add the WebRtcAudioDevice as the sink to the local audio track. | 673 // Add the WebRtcAudioDevice as the sink to the local audio track. |
| 668 audio_track->AddSink(GetWebRtcAudioDevice()); | 674 audio_track->AddSink(GetWebRtcAudioDevice()); |
| 669 // Start the audio track. This will hook the |audio_track| to the capturer | 675 // Start the audio track. This will hook the |audio_track| to the capturer |
| 670 // as the sink of the audio, and only start the source of the capturer if | 676 // as the sink of the audio, and only start the source of the capturer if |
| 671 // it is the first audio track connecting to the capturer. | 677 // it is the first audio track connecting to the capturer. |
| 672 audio_track->Start(); | 678 audio_track->Start(); |
| 673 return audio_track; | 679 return audio_track; |
| 674 } | 680 } |
| 675 | 681 |
| 676 webrtc::SessionDescriptionInterface* | 682 webrtc::SessionDescriptionInterface* |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 } else { | 799 } else { |
| 794 NOTREACHED() << "Worker thread not running."; | 800 NOTREACHED() << "Worker thread not running."; |
| 795 } | 801 } |
| 796 } | 802 } |
| 797 } | 803 } |
| 798 | 804 |
| 799 scoped_refptr<WebRtcAudioCapturer> | 805 scoped_refptr<WebRtcAudioCapturer> |
| 800 MediaStreamDependencyFactory::MaybeCreateAudioCapturer( | 806 MediaStreamDependencyFactory::MaybeCreateAudioCapturer( |
| 801 int render_view_id, | 807 int render_view_id, |
| 802 const StreamDeviceInfo& device_info) { | 808 const StreamDeviceInfo& device_info) { |
| 803 scoped_refptr<WebRtcAudioCapturer> capturer; | 809 // TODO(xians): Handle the cases when gUM is called without a proper render |
| 804 if (render_view_id != -1) { | 810 // view, for example, by an extension. |
| 805 // From a normal getUserMedia, re-use the existing default capturer. | 811 DCHECK_GE(render_view_id, 0); |
| 806 capturer = GetWebRtcAudioDevice()->GetDefaultCapturer(); | 812 |
| 807 } | 813 scoped_refptr<WebRtcAudioCapturer> capturer = |
| 814 GetWebRtcAudioDevice()->GetDefaultCapturer(); |
| 815 |
| 808 // If the default capturer does not exist or |render_view_id| == -1, create | 816 // If the default capturer does not exist or |render_view_id| == -1, create |
| 809 // a new capturer. | 817 // a new capturer. |
| 810 bool is_new_capturer = false; | 818 bool is_new_capturer = false; |
| 811 if (!capturer.get()) { | 819 if (!capturer.get()) { |
| 812 capturer = WebRtcAudioCapturer::CreateCapturer(); | 820 capturer = WebRtcAudioCapturer::CreateCapturer(); |
| 813 is_new_capturer = true; | 821 is_new_capturer = true; |
| 814 } | 822 } |
| 815 | 823 |
| 816 if (!capturer->Initialize( | 824 if (!capturer->Initialize( |
| 817 render_view_id, | 825 render_view_id, |
| 818 static_cast<media::ChannelLayout>( | 826 static_cast<media::ChannelLayout>( |
| 819 device_info.device.input.channel_layout), | 827 device_info.device.input.channel_layout), |
| 820 device_info.device.input.sample_rate, | 828 device_info.device.input.sample_rate, |
| 829 device_info.device.input.frames_per_buffer, |
| 821 device_info.session_id, | 830 device_info.session_id, |
| 822 device_info.device.id)) { | 831 device_info.device.id)) { |
| 823 return NULL; | 832 return NULL; |
| 824 } | 833 } |
| 825 | 834 |
| 826 // Add the capturer to the WebRtcAudioDeviceImpl if it is a new capturer. | 835 // Add the capturer to the WebRtcAudioDeviceImpl if it is a new capturer. |
| 827 if (is_new_capturer) | 836 if (is_new_capturer) |
| 828 GetWebRtcAudioDevice()->AddAudioCapturer(capturer); | 837 GetWebRtcAudioDevice()->AddAudioCapturer(capturer); |
| 829 | 838 |
| 830 return capturer; | 839 return capturer; |
| 831 } | 840 } |
| 832 | 841 |
| 833 } // namespace content | 842 } // namespace content |
| OLD | NEW |