| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
| 6 #include "base/test/test_timeouts.h" | 6 #include "base/test/test_timeouts.h" |
| 7 #include "content/renderer/media/rtc_media_constraints.h" | 7 #include "content/renderer/media/rtc_media_constraints.h" |
| 8 #include "content/renderer/media/webrtc_audio_capturer.h" | 8 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 9 #include "content/renderer/media/webrtc_audio_device_impl.h" | 9 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 10 #include "content/renderer/media/webrtc_local_audio_source_provider.h" | 10 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 MOCK_METHOD1(OnSetFormat, void(const media::AudioParameters& params)); | 155 MOCK_METHOD1(OnSetFormat, void(const media::AudioParameters& params)); |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 } // namespace | 158 } // namespace |
| 159 | 159 |
| 160 class WebRtcLocalAudioTrackTest : public ::testing::Test { | 160 class WebRtcLocalAudioTrackTest : public ::testing::Test { |
| 161 protected: | 161 protected: |
| 162 virtual void SetUp() OVERRIDE { | 162 virtual void SetUp() OVERRIDE { |
| 163 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 163 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 164 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); | 164 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); |
| 165 capturer_ = WebRtcAudioCapturer::CreateCapturer(); | 165 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo()); |
| 166 capturer_source_ = new MockCapturerSource(capturer_.get()); | 166 capturer_source_ = new MockCapturerSource(capturer_.get()); |
| 167 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), 0)) | 167 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) |
| 168 .WillOnce(Return()); | 168 .WillOnce(Return()); |
| 169 capturer_->SetCapturerSource(capturer_source_, | 169 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); |
| 170 params_.channel_layout(), | |
| 171 params_.sample_rate(), | |
| 172 params_.effects()); | |
| 173 } | 170 } |
| 174 | 171 |
| 175 media::AudioParameters params_; | 172 media::AudioParameters params_; |
| 176 scoped_refptr<MockCapturerSource> capturer_source_; | 173 scoped_refptr<MockCapturerSource> capturer_source_; |
| 177 scoped_refptr<WebRtcAudioCapturer> capturer_; | 174 scoped_refptr<WebRtcAudioCapturer> capturer_; |
| 178 }; | 175 }; |
| 179 | 176 |
| 180 // Creates a capturer and audio track, fakes its audio thread, and | 177 // Creates a capturer and audio track, fakes its audio thread, and |
| 181 // connect/disconnect the sink to the audio track on the fly, the sink should | 178 // connect/disconnect the sink to the audio track on the fly, the sink should |
| 182 // get data callback when the track is connected to the capturer but not when | 179 // get data callback when the track is connected to the capturer but not when |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 &constraints); | 446 &constraints); |
| 450 static_cast<WebRtcLocalAudioSourceProvider*>( | 447 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 451 track->audio_source_provider())->SetSinkParamsForTesting(params_); | 448 track->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 452 track->Start(); | 449 track->Start(); |
| 453 | 450 |
| 454 // Setting new source to the capturer and the track should still get packets. | 451 // Setting new source to the capturer and the track should still get packets. |
| 455 scoped_refptr<MockCapturerSource> new_source( | 452 scoped_refptr<MockCapturerSource> new_source( |
| 456 new MockCapturerSource(capturer_.get())); | 453 new MockCapturerSource(capturer_.get())); |
| 457 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 454 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 458 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | 455 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); |
| 459 EXPECT_CALL(*new_source.get(), OnInitialize(_, capturer_.get(), 0)) | 456 EXPECT_CALL(*new_source.get(), OnInitialize(_, capturer_.get(), -1)) |
| 460 .WillOnce(Return()); | 457 .WillOnce(Return()); |
| 461 EXPECT_CALL(*new_source.get(), OnStart()); | 458 EXPECT_CALL(*new_source.get(), OnStart()); |
| 462 capturer_->SetCapturerSource(new_source, | 459 capturer_->SetCapturerSourceForTesting(new_source, params_); |
| 463 params_.channel_layout(), | |
| 464 params_.sample_rate(), | |
| 465 params_.effects()); | |
| 466 | 460 |
| 467 // Stop the track. | 461 // Stop the track. |
| 468 EXPECT_CALL(*new_source.get(), OnStop()); | 462 EXPECT_CALL(*new_source.get(), OnStop()); |
| 469 capturer_->Stop(); | 463 capturer_->Stop(); |
| 470 } | 464 } |
| 471 | 465 |
| 472 // Create a new capturer with new source, connect it to a new audio track. | 466 // Create a new capturer with new source, connect it to a new audio track. |
| 473 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { | 467 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { |
| 474 // Setup the first audio track and start it. | 468 // Setup the first audio track and start it. |
| 475 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 469 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 493 EXPECT_CALL( | 487 EXPECT_CALL( |
| 494 *sink_1.get(), | 488 *sink_1.get(), |
| 495 CaptureData( | 489 CaptureData( |
| 496 kNumberOfNetworkChannelsForTrack1, 48000, 2, _, 0, 0, false, false)) | 490 kNumberOfNetworkChannelsForTrack1, 48000, 2, _, 0, 0, false, false)) |
| 497 .Times(AnyNumber()).WillRepeatedly(Return()); | 491 .Times(AnyNumber()).WillRepeatedly(Return()); |
| 498 EXPECT_CALL(*sink_1.get(), OnSetFormat(_)).Times(AnyNumber()); | 492 EXPECT_CALL(*sink_1.get(), OnSetFormat(_)).Times(AnyNumber()); |
| 499 track_1->AddSink(sink_1.get()); | 493 track_1->AddSink(sink_1.get()); |
| 500 | 494 |
| 501 // Create a new capturer with new source with different audio format. | 495 // Create a new capturer with new source with different audio format. |
| 502 scoped_refptr<WebRtcAudioCapturer> new_capturer( | 496 scoped_refptr<WebRtcAudioCapturer> new_capturer( |
| 503 WebRtcAudioCapturer::CreateCapturer()); | 497 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo())); |
| 504 scoped_refptr<MockCapturerSource> new_source( | 498 scoped_refptr<MockCapturerSource> new_source( |
| 505 new MockCapturerSource(new_capturer.get())); | 499 new MockCapturerSource(new_capturer.get())); |
| 506 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), 0)); | 500 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); |
| 507 new_capturer->SetCapturerSource(new_source, | 501 media::AudioParameters new_param( |
| 508 media::CHANNEL_LAYOUT_MONO, | 502 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 509 44100, | 503 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); |
| 510 media::AudioParameters::NO_EFFECTS); | 504 new_capturer->SetCapturerSourceForTesting(new_source, new_param); |
| 511 | 505 |
| 512 // Setup the second audio track, connect it to the new capturer and start it. | 506 // Setup the second audio track, connect it to the new capturer and start it. |
| 513 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | 507 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); |
| 514 EXPECT_CALL(*new_source.get(), OnStart()); | 508 EXPECT_CALL(*new_source.get(), OnStart()); |
| 515 scoped_refptr<WebRtcLocalAudioTrack> track_2 = | 509 scoped_refptr<WebRtcLocalAudioTrack> track_2 = |
| 516 WebRtcLocalAudioTrack::Create(std::string(), new_capturer, NULL, NULL, | 510 WebRtcLocalAudioTrack::Create(std::string(), new_capturer, NULL, NULL, |
| 517 &constraints); | 511 &constraints); |
| 518 static_cast<WebRtcLocalAudioSourceProvider*>( | 512 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 519 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); | 513 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 520 track_2->Start(); | 514 track_2->Start(); |
| 521 | 515 |
| 522 // Connect a number of network channels to the |track_2|. | 516 // Connect a number of network channels to the |track_2|. |
| 523 static const int kNumberOfNetworkChannelsForTrack2 = 3; | 517 static const int kNumberOfNetworkChannelsForTrack2 = 3; |
| 524 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { | 518 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { |
| 525 static_cast<webrtc::AudioTrackInterface*>(track_2.get())-> | 519 static_cast<webrtc::AudioTrackInterface*>(track_2.get())-> |
| 526 GetRenderer()->AddChannel(i); | 520 GetRenderer()->AddChannel(i); |
| 527 } | 521 } |
| 528 // Verify the data flow by connecting the |sink_2| to |track_2|. | 522 // Verify the data flow by connecting the |sink_2| to |track_2|. |
| 529 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); | 523 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); |
| 530 base::WaitableEvent event(false, false); | 524 base::WaitableEvent event(false, false); |
| 531 EXPECT_CALL( | 525 EXPECT_CALL( |
| 532 *sink_2, | 526 *sink_2, |
| 533 CaptureData( | 527 CaptureData( |
| 534 kNumberOfNetworkChannelsForTrack2, 44100, 1, _, 0, 0, false, false)) | 528 kNumberOfNetworkChannelsForTrack2, new_param.sample_rate(), |
| 529 new_param.channels(), _, 0, 0, false, false)) |
| 535 .Times(AnyNumber()).WillRepeatedly(Return()); | 530 .Times(AnyNumber()).WillRepeatedly(Return()); |
| 536 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(SignalEvent(&event)); | 531 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(SignalEvent(&event)); |
| 537 track_2->AddSink(sink_2.get()); | 532 track_2->AddSink(sink_2.get()); |
| 538 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 533 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 539 | 534 |
| 540 // Stopping the new source will stop the second track. | 535 // Stopping the new source will stop the second track. |
| 541 event.Reset(); | 536 event.Reset(); |
| 542 EXPECT_CALL(*new_source.get(), OnStop()) | 537 EXPECT_CALL(*new_source.get(), OnStop()) |
| 543 .Times(1).WillOnce(SignalEvent(&event)); | 538 .Times(1).WillOnce(SignalEvent(&event)); |
| 544 new_capturer->Stop(); | 539 new_capturer->Stop(); |
| 545 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 540 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 546 | 541 |
| 547 // Stop the capturer of the first audio track. | 542 // Stop the capturer of the first audio track. |
| 548 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 543 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 549 capturer_->Stop(); | 544 capturer_->Stop(); |
| 550 } | 545 } |
| 551 | 546 |
| 552 | 547 |
| 553 // Make sure a audio track can deliver packets with a buffer size smaller than | 548 // Make sure a audio track can deliver packets with a buffer size smaller than |
| 554 // 10ms when it is not connected with a peer connection. | 549 // 10ms when it is not connected with a peer connection. |
| 555 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { | 550 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { |
| 556 // Setup a capturer which works with a buffer size smaller than 10ms. | 551 // Setup a capturer which works with a buffer size smaller than 10ms. |
| 557 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 552 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 558 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); | 553 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); |
| 559 | 554 |
| 560 // Create a capturer with new source which works with the format above. | 555 // Create a capturer with new source which works with the format above. |
| 561 scoped_refptr<WebRtcAudioCapturer> capturer( | 556 scoped_refptr<WebRtcAudioCapturer> capturer( |
| 562 WebRtcAudioCapturer::CreateCapturer()); | 557 WebRtcAudioCapturer::CreateCapturer( |
| 558 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 559 "", "", params.sample_rate(), |
| 560 params.channel_layout(), |
| 561 params.frames_per_buffer()))); |
| 563 scoped_refptr<MockCapturerSource> source( | 562 scoped_refptr<MockCapturerSource> source( |
| 564 new MockCapturerSource(capturer.get())); | 563 new MockCapturerSource(capturer.get())); |
| 565 capturer->Initialize(-1, params.channel_layout(), params.sample_rate(), | 564 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); |
| 566 params.frames_per_buffer(), 0, std::string(), 0, 0, | 565 capturer->SetCapturerSourceForTesting(source, params); |
| 567 params.effects()); | |
| 568 | |
| 569 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), 0)); | |
| 570 capturer->SetCapturerSource(source, params.channel_layout(), | |
| 571 params.sample_rate(), params.effects()); | |
| 572 | 566 |
| 573 // Setup a audio track, connect it to the capturer and start it. | 567 // Setup a audio track, connect it to the capturer and start it. |
| 574 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); | 568 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); |
| 575 EXPECT_CALL(*source.get(), OnStart()); | 569 EXPECT_CALL(*source.get(), OnStart()); |
| 576 RTCMediaConstraints constraints; | 570 RTCMediaConstraints constraints; |
| 577 scoped_refptr<WebRtcLocalAudioTrack> track = | 571 scoped_refptr<WebRtcLocalAudioTrack> track = |
| 578 WebRtcLocalAudioTrack::Create(std::string(), capturer, NULL, NULL, | 572 WebRtcLocalAudioTrack::Create(std::string(), capturer, NULL, NULL, |
| 579 &constraints); | 573 &constraints); |
| 580 static_cast<WebRtcLocalAudioSourceProvider*>( | 574 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 581 track->audio_source_provider())->SetSinkParamsForTesting(params); | 575 track->audio_source_provider())->SetSinkParamsForTesting(params); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 597 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); | 591 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); |
| 598 track->AddSink(sink.get()); | 592 track->AddSink(sink.get()); |
| 599 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 593 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 600 | 594 |
| 601 // Stopping the new source will stop the second track. | 595 // Stopping the new source will stop the second track. |
| 602 EXPECT_CALL(*source, OnStop()).Times(1); | 596 EXPECT_CALL(*source, OnStop()).Times(1); |
| 603 capturer->Stop(); | 597 capturer->Stop(); |
| 604 } | 598 } |
| 605 | 599 |
| 606 } // namespace content | 600 } // namespace content |
| OLD | NEW |