Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: content/renderer/media/media_stream_dependency_factory.cc

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

Powered by Google App Engine
This is Rietveld 408576698