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/webrtc_audio_capturer.h" | 7 #include "content/renderer/media/webrtc_audio_capturer.h" |
8 #include "content/renderer/media/webrtc_audio_device_impl.h" | 8 #include "content/renderer/media/webrtc_audio_device_impl.h" |
9 #include "content/renderer/media/webrtc_local_audio_source_provider.h" | 9 #include "content/renderer/media/webrtc_local_audio_source_provider.h" |
10 #include "content/renderer/media/webrtc_local_audio_track.h" | 10 #include "content/renderer/media/webrtc_local_audio_track.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 blink::WebMediaConstraints constraints; |
| 166 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), |
| 167 constraints, NULL); |
166 capturer_source_ = new MockCapturerSource(capturer_.get()); | 168 capturer_source_ = new MockCapturerSource(capturer_.get()); |
167 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), 0)) | 169 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) |
168 .WillOnce(Return()); | 170 .WillOnce(Return()); |
169 blink::WebMediaConstraints constraints; | 171 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); |
170 capturer_->SetCapturerSource(capturer_source_, | |
171 params_.channel_layout(), | |
172 params_.sample_rate(), | |
173 params_.effects(), | |
174 constraints); | |
175 } | 172 } |
176 | 173 |
177 media::AudioParameters params_; | 174 media::AudioParameters params_; |
178 scoped_refptr<MockCapturerSource> capturer_source_; | 175 scoped_refptr<MockCapturerSource> capturer_source_; |
179 scoped_refptr<WebRtcAudioCapturer> capturer_; | 176 scoped_refptr<WebRtcAudioCapturer> capturer_; |
180 }; | 177 }; |
181 | 178 |
182 // Creates a capturer and audio track, fakes its audio thread, and | 179 // Creates a capturer and audio track, fakes its audio thread, and |
183 // connect/disconnect the sink to the audio track on the fly, the sink should | 180 // connect/disconnect the sink to the audio track on the fly, the sink should |
184 // get data callback when the track is connected to the capturer but not when | 181 // get data callback when the track is connected to the capturer but not when |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 track_2->AddSink(sink.get()); | 418 track_2->AddSink(sink.get()); |
422 EXPECT_CALL(*sink, OnSetFormat(_)).Times(0); | 419 EXPECT_CALL(*sink, OnSetFormat(_)).Times(0); |
423 | 420 |
424 // Stop the capturer again will not trigger stopping the source of the | 421 // Stop the capturer again will not trigger stopping the source of the |
425 // capturer again.. | 422 // capturer again.. |
426 event.Reset(); | 423 event.Reset(); |
427 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); | 424 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); |
428 capturer_->Stop(); | 425 capturer_->Stop(); |
429 } | 426 } |
430 | 427 |
431 // Set new source to the existing capturer. | |
432 TEST_F(WebRtcLocalAudioTrackTest, SetNewSourceForCapturerAfterStartTrack) { | |
433 // Setup the audio track and start the track. | |
434 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
435 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
436 scoped_refptr<WebRtcLocalAudioTrack> track = | |
437 WebRtcLocalAudioTrack::Create(std::string(), capturer_, NULL, NULL); | |
438 static_cast<WebRtcLocalAudioSourceProvider*>( | |
439 track->audio_source_provider())->SetSinkParamsForTesting(params_); | |
440 track->Start(); | |
441 | |
442 // Setting new source to the capturer and the track should still get packets. | |
443 scoped_refptr<MockCapturerSource> new_source( | |
444 new MockCapturerSource(capturer_.get())); | |
445 EXPECT_CALL(*capturer_source_.get(), OnStop()); | |
446 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | |
447 EXPECT_CALL(*new_source.get(), OnInitialize(_, capturer_.get(), 0)) | |
448 .WillOnce(Return()); | |
449 EXPECT_CALL(*new_source.get(), OnStart()); | |
450 blink::WebMediaConstraints constraints; | |
451 capturer_->SetCapturerSource(new_source, | |
452 params_.channel_layout(), | |
453 params_.sample_rate(), | |
454 params_.effects(), | |
455 constraints); | |
456 | |
457 // Stop the track. | |
458 EXPECT_CALL(*new_source.get(), OnStop()); | |
459 capturer_->Stop(); | |
460 } | |
461 | |
462 // Create a new capturer with new source, connect it to a new audio track. | 428 // Create a new capturer with new source, connect it to a new audio track. |
463 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { | 429 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { |
464 // Setup the first audio track and start it. | 430 // Setup the first audio track and start it. |
465 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | 431 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
466 EXPECT_CALL(*capturer_source_.get(), OnStart()); | 432 EXPECT_CALL(*capturer_source_.get(), OnStart()); |
467 scoped_refptr<WebRtcLocalAudioTrack> track_1 = | 433 scoped_refptr<WebRtcLocalAudioTrack> track_1 = |
468 WebRtcLocalAudioTrack::Create(std::string(), capturer_, NULL, NULL); | 434 WebRtcLocalAudioTrack::Create(std::string(), capturer_, NULL, NULL); |
469 static_cast<WebRtcLocalAudioSourceProvider*>( | 435 static_cast<WebRtcLocalAudioSourceProvider*>( |
470 track_1->audio_source_provider())->SetSinkParamsForTesting(params_); | 436 track_1->audio_source_provider())->SetSinkParamsForTesting(params_); |
471 track_1->Start(); | 437 track_1->Start(); |
472 | 438 |
473 // Connect a number of network channels to the |track_1|. | 439 // Connect a number of network channels to the |track_1|. |
474 static const int kNumberOfNetworkChannelsForTrack1 = 2; | 440 static const int kNumberOfNetworkChannelsForTrack1 = 2; |
475 for (int i = 0; i < kNumberOfNetworkChannelsForTrack1; ++i) { | 441 for (int i = 0; i < kNumberOfNetworkChannelsForTrack1; ++i) { |
476 static_cast<webrtc::AudioTrackInterface*>(track_1.get())-> | 442 static_cast<webrtc::AudioTrackInterface*>(track_1.get())-> |
477 GetRenderer()->AddChannel(i); | 443 GetRenderer()->AddChannel(i); |
478 } | 444 } |
479 // Verify the data flow by connecting the |sink_1| to |track_1|. | 445 // Verify the data flow by connecting the |sink_1| to |track_1|. |
480 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); | 446 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); |
481 EXPECT_CALL( | 447 EXPECT_CALL( |
482 *sink_1.get(), | 448 *sink_1.get(), |
483 CaptureData( | 449 CaptureData( |
484 kNumberOfNetworkChannelsForTrack1, 48000, 2, _, 0, 0, true, false)) | 450 kNumberOfNetworkChannelsForTrack1, 48000, 2, _, 0, 0, true, false)) |
485 .Times(AnyNumber()).WillRepeatedly(Return()); | 451 .Times(AnyNumber()).WillRepeatedly(Return()); |
486 EXPECT_CALL(*sink_1.get(), OnSetFormat(_)).Times(AnyNumber()); | 452 EXPECT_CALL(*sink_1.get(), OnSetFormat(_)).Times(AnyNumber()); |
487 track_1->AddSink(sink_1.get()); | 453 track_1->AddSink(sink_1.get()); |
488 | 454 |
489 // Create a new capturer with new source with different audio format. | 455 // Create a new capturer with new source with different audio format. |
| 456 blink::WebMediaConstraints constraints; |
490 scoped_refptr<WebRtcAudioCapturer> new_capturer( | 457 scoped_refptr<WebRtcAudioCapturer> new_capturer( |
491 WebRtcAudioCapturer::CreateCapturer()); | 458 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), |
| 459 constraints, NULL)); |
492 scoped_refptr<MockCapturerSource> new_source( | 460 scoped_refptr<MockCapturerSource> new_source( |
493 new MockCapturerSource(new_capturer.get())); | 461 new MockCapturerSource(new_capturer.get())); |
494 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), 0)); | 462 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); |
495 blink::WebMediaConstraints constraints; | 463 media::AudioParameters new_param( |
496 new_capturer->SetCapturerSource(new_source, | 464 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
497 media::CHANNEL_LAYOUT_MONO, | 465 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); |
498 44100, | 466 new_capturer->SetCapturerSourceForTesting(new_source, new_param); |
499 media::AudioParameters::NO_EFFECTS, | |
500 constraints); | |
501 | 467 |
502 // Setup the second audio track, connect it to the new capturer and start it. | 468 // Setup the second audio track, connect it to the new capturer and start it. |
503 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | 469 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); |
504 EXPECT_CALL(*new_source.get(), OnStart()); | 470 EXPECT_CALL(*new_source.get(), OnStart()); |
505 scoped_refptr<WebRtcLocalAudioTrack> track_2 = | 471 scoped_refptr<WebRtcLocalAudioTrack> track_2 = |
506 WebRtcLocalAudioTrack::Create(std::string(), new_capturer, NULL, NULL); | 472 WebRtcLocalAudioTrack::Create(std::string(), new_capturer, NULL, NULL); |
507 static_cast<WebRtcLocalAudioSourceProvider*>( | 473 static_cast<WebRtcLocalAudioSourceProvider*>( |
508 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); | 474 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); |
509 track_2->Start(); | 475 track_2->Start(); |
510 | 476 |
511 // Connect a number of network channels to the |track_2|. | 477 // Connect a number of network channels to the |track_2|. |
512 static const int kNumberOfNetworkChannelsForTrack2 = 3; | 478 static const int kNumberOfNetworkChannelsForTrack2 = 3; |
513 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { | 479 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { |
514 static_cast<webrtc::AudioTrackInterface*>(track_2.get())-> | 480 static_cast<webrtc::AudioTrackInterface*>(track_2.get())-> |
515 GetRenderer()->AddChannel(i); | 481 GetRenderer()->AddChannel(i); |
516 } | 482 } |
517 // Verify the data flow by connecting the |sink_2| to |track_2|. | 483 // Verify the data flow by connecting the |sink_2| to |track_2|. |
518 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); | 484 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); |
519 base::WaitableEvent event(false, false); | 485 base::WaitableEvent event(false, false); |
520 EXPECT_CALL( | 486 EXPECT_CALL( |
521 *sink_2, | 487 *sink_2, |
522 CaptureData( | 488 CaptureData( |
523 kNumberOfNetworkChannelsForTrack2, 44100, 1, _, 0, 0, true, false)) | 489 kNumberOfNetworkChannelsForTrack2, new_param.sample_rate(), |
| 490 new_param.channels(), _, 0, 0, true, false)) |
524 .Times(AnyNumber()).WillRepeatedly(Return()); | 491 .Times(AnyNumber()).WillRepeatedly(Return()); |
525 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(SignalEvent(&event)); | 492 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(SignalEvent(&event)); |
526 track_2->AddSink(sink_2.get()); | 493 track_2->AddSink(sink_2.get()); |
527 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 494 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
528 | 495 |
529 // Stopping the new source will stop the second track. | 496 // Stopping the new source will stop the second track. |
530 event.Reset(); | 497 event.Reset(); |
531 EXPECT_CALL(*new_source.get(), OnStop()) | 498 EXPECT_CALL(*new_source.get(), OnStop()) |
532 .Times(1).WillOnce(SignalEvent(&event)); | 499 .Times(1).WillOnce(SignalEvent(&event)); |
533 new_capturer->Stop(); | 500 new_capturer->Stop(); |
534 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 501 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
535 | 502 |
536 // Stop the capturer of the first audio track. | 503 // Stop the capturer of the first audio track. |
537 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 504 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
538 capturer_->Stop(); | 505 capturer_->Stop(); |
539 } | 506 } |
540 | 507 |
541 | 508 |
542 // Make sure a audio track can deliver packets with a buffer size smaller than | 509 // Make sure a audio track can deliver packets with a buffer size smaller than |
543 // 10ms when it is not connected with a peer connection. | 510 // 10ms when it is not connected with a peer connection. |
544 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { | 511 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { |
545 // Setup a capturer which works with a buffer size smaller than 10ms. | 512 // Setup a capturer which works with a buffer size smaller than 10ms. |
546 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 513 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
547 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); | 514 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); |
548 | 515 |
549 // Create a capturer with new source which works with the format above. | 516 // Create a capturer with new source which works with the format above. |
| 517 blink::WebMediaConstraints constraints; |
550 scoped_refptr<WebRtcAudioCapturer> capturer( | 518 scoped_refptr<WebRtcAudioCapturer> capturer( |
551 WebRtcAudioCapturer::CreateCapturer()); | 519 WebRtcAudioCapturer::CreateCapturer( |
| 520 -1, |
| 521 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 522 "", "", params.sample_rate(), |
| 523 params.channel_layout(), |
| 524 params.frames_per_buffer()), |
| 525 constraints, |
| 526 NULL)); |
552 scoped_refptr<MockCapturerSource> source( | 527 scoped_refptr<MockCapturerSource> source( |
553 new MockCapturerSource(capturer.get())); | 528 new MockCapturerSource(capturer.get())); |
554 blink::WebMediaConstraints constraints; | 529 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); |
555 capturer->Initialize(-1, params.channel_layout(), params.sample_rate(), | 530 capturer->SetCapturerSourceForTesting(source, params); |
556 params.frames_per_buffer(), 0, std::string(), 0, 0, | |
557 params.effects(), constraints); | |
558 | |
559 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), 0)); | |
560 capturer->SetCapturerSource(source, params.channel_layout(), | |
561 params.sample_rate(), params.effects(), | |
562 constraints); | |
563 | 531 |
564 // Setup a audio track, connect it to the capturer and start it. | 532 // Setup a audio track, connect it to the capturer and start it. |
565 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); | 533 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); |
566 EXPECT_CALL(*source.get(), OnStart()); | 534 EXPECT_CALL(*source.get(), OnStart()); |
567 scoped_refptr<WebRtcLocalAudioTrack> track = | 535 scoped_refptr<WebRtcLocalAudioTrack> track = |
568 WebRtcLocalAudioTrack::Create(std::string(), capturer, NULL, NULL); | 536 WebRtcLocalAudioTrack::Create(std::string(), capturer, NULL, NULL); |
569 static_cast<WebRtcLocalAudioSourceProvider*>( | 537 static_cast<WebRtcLocalAudioSourceProvider*>( |
570 track->audio_source_provider())->SetSinkParamsForTesting(params); | 538 track->audio_source_provider())->SetSinkParamsForTesting(params); |
571 track->Start(); | 539 track->Start(); |
572 | 540 |
(...skipping 13 matching lines...) Expand all Loading... |
586 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); | 554 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); |
587 track->AddSink(sink.get()); | 555 track->AddSink(sink.get()); |
588 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 556 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
589 | 557 |
590 // Stopping the new source will stop the second track. | 558 // Stopping the new source will stop the second track. |
591 EXPECT_CALL(*source, OnStop()).Times(1); | 559 EXPECT_CALL(*source, OnStop()).Times(1); |
592 capturer->Stop(); | 560 capturer->Stop(); |
593 } | 561 } |
594 | 562 |
595 } // namespace content | 563 } // namespace content |
OLD | NEW |