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