| 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/media_stream_audio_source.h" |
| 7 #include "content/renderer/media/mock_media_constraint_factory.h" | 8 #include "content/renderer/media/mock_media_constraint_factory.h" |
| 8 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" | 9 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" |
| 9 #include "content/renderer/media/webrtc_audio_capturer.h" | 10 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 10 #include "content/renderer/media/webrtc_audio_device_impl.h" | 11 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 11 #include "content/renderer/media/webrtc_local_audio_source_provider.h" | 12 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
| 12 #include "content/renderer/media/webrtc_local_audio_track.h" | 13 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 13 #include "media/audio/audio_parameters.h" | 14 #include "media/audio/audio_parameters.h" |
| 14 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
| 15 #include "media/base/audio_capturer_source.h" | 16 #include "media/base/audio_capturer_source.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 }; | 165 }; |
| 165 | 166 |
| 166 } // namespace | 167 } // namespace |
| 167 | 168 |
| 168 class WebRtcLocalAudioTrackTest : public ::testing::Test { | 169 class WebRtcLocalAudioTrackTest : public ::testing::Test { |
| 169 protected: | 170 protected: |
| 170 virtual void SetUp() OVERRIDE { | 171 virtual void SetUp() OVERRIDE { |
| 171 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 172 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 172 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); | 173 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); |
| 173 blink::WebMediaConstraints constraints; | 174 blink::WebMediaConstraints constraints; |
| 175 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio, |
| 176 "dummy"); |
| 177 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource(); |
| 178 blink_source_.setExtraData(audio_source); |
| 179 |
| 174 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | 180 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 175 std::string(), std::string()); | 181 std::string(), std::string()); |
| 176 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, device, | 182 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, device, |
| 177 constraints, NULL); | 183 constraints, NULL, |
| 184 audio_source); |
| 185 audio_source->SetAudioCapturer(capturer_); |
| 178 capturer_source_ = new MockCapturerSource(capturer_.get()); | 186 capturer_source_ = new MockCapturerSource(capturer_.get()); |
| 179 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) | 187 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) |
| 180 .WillOnce(Return()); | 188 .WillOnce(Return()); |
| 181 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); | 189 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); |
| 182 } | 190 } |
| 183 | 191 |
| 184 media::AudioParameters params_; | 192 media::AudioParameters params_; |
| 193 blink::WebMediaStreamSource blink_source_; |
| 185 scoped_refptr<MockCapturerSource> capturer_source_; | 194 scoped_refptr<MockCapturerSource> capturer_source_; |
| 186 scoped_refptr<WebRtcAudioCapturer> capturer_; | 195 scoped_refptr<WebRtcAudioCapturer> capturer_; |
| 187 }; | 196 }; |
| 188 | 197 |
| 189 // Creates a capturer and audio track, fakes its audio thread, and | 198 // Creates a capturer and audio track, fakes its audio thread, and |
| 190 // connect/disconnect the sink to the audio track on the fly, the sink should | 199 // connect/disconnect the sink to the audio track on the fly, the sink should |
| 191 // get data callback when the track is connected to the capturer but not when | 200 // get data callback when the track is connected to the capturer but not when |
| 192 // the track is disconnected from the capturer. | 201 // the track is disconnected from the capturer. |
| 193 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { | 202 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { |
| 194 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 203 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 params.sample_rate() / 100); | 330 params.sample_rate() / 100); |
| 322 EXPECT_CALL(*sink_2, CaptureData(1, 0, 0, _, false)).Times(AtLeast(1)) | 331 EXPECT_CALL(*sink_2, CaptureData(1, 0, 0, _, false)).Times(AtLeast(1)) |
| 323 .WillRepeatedly(SignalEvent(&event_2)); | 332 .WillRepeatedly(SignalEvent(&event_2)); |
| 324 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(), | 333 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(), |
| 325 params.sample_rate() / 100); | 334 params.sample_rate() / 100); |
| 326 track_2->AddSink(sink_2.get()); | 335 track_2->AddSink(sink_2.get()); |
| 327 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); | 336 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); |
| 328 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); | 337 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); |
| 329 | 338 |
| 330 track_1->RemoveSink(sink_1.get()); | 339 track_1->RemoveSink(sink_1.get()); |
| 331 track_1->Stop(); | 340 track_1->StopTrack(); |
| 332 track_1.reset(); | 341 track_1.reset(); |
| 333 | 342 |
| 334 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | 343 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); |
| 335 track_2->RemoveSink(sink_2.get()); | 344 track_2->RemoveSink(sink_2.get()); |
| 336 track_2->Stop(); | 345 track_2->StopTrack(); |
| 337 track_2.reset(); | 346 track_2.reset(); |
| 338 | |
| 339 capturer_->Stop(); | |
| 340 } | 347 } |
| 341 | 348 |
| 342 | 349 |
| 343 // Start one track and verify the capturer is correctly starting its source. | 350 // Start one track and verify the capturer is correctly starting its source. |
| 344 // And it should be fine to not to call Stop() explicitly. | 351 // And it should be fine to not to call Stop() explicitly. |
| 345 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { | 352 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { |
| 346 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 353 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| 347 EXPECT_CALL(*capturer_source_.get(), OnStart()); | 354 EXPECT_CALL(*capturer_source_.get(), OnStart()); |
| 348 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 355 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 349 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 356 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 350 scoped_ptr<WebRtcLocalAudioTrack> track( | 357 scoped_ptr<WebRtcLocalAudioTrack> track( |
| 351 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); | 358 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); |
| 352 static_cast<WebRtcLocalAudioSourceProvider*>( | 359 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 353 track->audio_source_provider())->SetSinkParamsForTesting(params_); | 360 track->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 354 track->Start(); | 361 track->Start(); |
| 355 | 362 |
| 356 // When the track goes away, it will automatically stop the | 363 // When the track goes away, it will automatically stop the |
| 357 // |capturer_source_|. | 364 // |capturer_source_|. |
| 358 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 365 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 359 capturer_->Stop(); | |
| 360 track.reset(); | 366 track.reset(); |
| 361 } | 367 } |
| 362 | 368 |
| 369 // Start two tracks and verify the capturer is correctly starting its source. |
| 370 // When the last track connected to the capturer is stopped, the source is |
| 371 // stopped. |
| 372 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) { |
| 373 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| 374 EXPECT_CALL(*capturer_source_.get(), OnStart()); |
| 375 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter1( |
| 376 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 377 scoped_ptr<WebRtcLocalAudioTrack> track1( |
| 378 new WebRtcLocalAudioTrack(adapter1, capturer_, NULL)); |
| 379 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 380 track1->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 381 track1->Start(); |
| 382 |
| 383 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter2( |
| 384 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 385 scoped_ptr<WebRtcLocalAudioTrack> track2( |
| 386 new WebRtcLocalAudioTrack(adapter2, capturer_, NULL)); |
| 387 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 388 track2->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 389 track2->Start(); |
| 390 |
| 391 track1->StopTrack(); |
| 392 // When the last track is stopped, it will automatically stop the |
| 393 // |capturer_source_|. |
| 394 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 395 track2->StopTrack(); |
| 396 } |
| 397 |
| 398 |
| 363 // Start/Stop tracks and verify the capturer is correctly starting/stopping | 399 // Start/Stop tracks and verify the capturer is correctly starting/stopping |
| 364 // its source. | 400 // its source. |
| 365 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { | 401 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { |
| 366 // Starting the first audio track will start the |capturer_source_|. | 402 // Starting the first audio track will start the |capturer_source_|. |
| 367 base::WaitableEvent event(false, false); | 403 base::WaitableEvent event(false, false); |
| 368 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 404 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| 369 EXPECT_CALL(*capturer_source_.get(), OnStart()).WillOnce(SignalEvent(&event)); | 405 EXPECT_CALL(*capturer_source_.get(), OnStart()).WillOnce(SignalEvent(&event)); |
| 370 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | 406 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( |
| 371 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 407 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 372 scoped_ptr<WebRtcLocalAudioTrack> track_1( | 408 scoped_ptr<WebRtcLocalAudioTrack> track_1( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 0, 0, _, false)) | 477 0, 0, _, false)) |
| 442 .Times(AnyNumber()).WillRepeatedly(Return()); | 478 .Times(AnyNumber()).WillRepeatedly(Return()); |
| 443 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber()); | 479 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber()); |
| 444 track_1->AddSink(sink_1.get()); | 480 track_1->AddSink(sink_1.get()); |
| 445 | 481 |
| 446 // Create a new capturer with new source with different audio format. | 482 // Create a new capturer with new source with different audio format. |
| 447 blink::WebMediaConstraints constraints; | 483 blink::WebMediaConstraints constraints; |
| 448 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | 484 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 449 std::string(), std::string()); | 485 std::string(), std::string()); |
| 450 scoped_refptr<WebRtcAudioCapturer> new_capturer( | 486 scoped_refptr<WebRtcAudioCapturer> new_capturer( |
| 451 WebRtcAudioCapturer::CreateCapturer(-1, device, constraints, NULL)); | 487 WebRtcAudioCapturer::CreateCapturer(-1, device, constraints, NULL, NULL)); |
| 452 scoped_refptr<MockCapturerSource> new_source( | 488 scoped_refptr<MockCapturerSource> new_source( |
| 453 new MockCapturerSource(new_capturer.get())); | 489 new MockCapturerSource(new_capturer.get())); |
| 454 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); | 490 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); |
| 455 media::AudioParameters new_param( | 491 media::AudioParameters new_param( |
| 456 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 492 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 457 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); | 493 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); |
| 458 new_capturer->SetCapturerSourceForTesting(new_source, new_param); | 494 new_capturer->SetCapturerSourceForTesting(new_source, new_param); |
| 459 | 495 |
| 460 // Setup the second audio track, connect it to the new capturer and start it. | 496 // Setup the second audio track, connect it to the new capturer and start it. |
| 461 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | 497 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 MockMediaConstraintFactory factory; | 544 MockMediaConstraintFactory factory; |
| 509 factory.DisableDefaultAudioConstraints(); | 545 factory.DisableDefaultAudioConstraints(); |
| 510 scoped_refptr<WebRtcAudioCapturer> capturer( | 546 scoped_refptr<WebRtcAudioCapturer> capturer( |
| 511 WebRtcAudioCapturer::CreateCapturer( | 547 WebRtcAudioCapturer::CreateCapturer( |
| 512 -1, | 548 -1, |
| 513 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, | 549 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 514 "", "", params.sample_rate(), | 550 "", "", params.sample_rate(), |
| 515 params.channel_layout(), | 551 params.channel_layout(), |
| 516 params.frames_per_buffer()), | 552 params.frames_per_buffer()), |
| 517 factory.CreateWebMediaConstraints(), | 553 factory.CreateWebMediaConstraints(), |
| 518 NULL)); | 554 NULL, NULL)); |
| 519 scoped_refptr<MockCapturerSource> source( | 555 scoped_refptr<MockCapturerSource> source( |
| 520 new MockCapturerSource(capturer.get())); | 556 new MockCapturerSource(capturer.get())); |
| 521 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); | 557 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); |
| 522 capturer->SetCapturerSourceForTesting(source, params); | 558 capturer->SetCapturerSourceForTesting(source, params); |
| 523 | 559 |
| 524 // Setup a audio track, connect it to the capturer and start it. | 560 // Setup a audio track, connect it to the capturer and start it. |
| 525 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); | 561 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); |
| 526 EXPECT_CALL(*source.get(), OnStart()); | 562 EXPECT_CALL(*source.get(), OnStart()); |
| 527 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 563 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 528 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 564 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 548 track->AddSink(sink.get()); | 584 track->AddSink(sink.get()); |
| 549 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 585 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 550 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); | 586 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); |
| 551 | 587 |
| 552 // Stopping the new source will stop the second track. | 588 // Stopping the new source will stop the second track. |
| 553 EXPECT_CALL(*source, OnStop()).Times(1); | 589 EXPECT_CALL(*source, OnStop()).Times(1); |
| 554 capturer->Stop(); | 590 capturer->Stop(); |
| 555 } | 591 } |
| 556 | 592 |
| 557 } // namespace content | 593 } // namespace content |
| OLD | NEW |