| 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_track.h" | 12 #include "content/renderer/media/webrtc_local_audio_track.h" |
| 12 #include "media/audio/audio_parameters.h" | 13 #include "media/audio/audio_parameters.h" |
| 13 #include "media/base/audio_bus.h" | 14 #include "media/base/audio_bus.h" |
| 14 #include "media/base/audio_capturer_source.h" | 15 #include "media/base/audio_capturer_source.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 ACTION_P(SignalEvent, event) { | 30 ACTION_P(SignalEvent, event) { |
| 30 event->Signal(); | 31 event->Signal(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // A simple thread that we use to fake the audio thread which provides data to | 34 // A simple thread that we use to fake the audio thread which provides data to |
| 34 // the |WebRtcAudioCapturer|. | 35 // the |WebRtcAudioCapturer|. |
| 35 class FakeAudioThread : public base::PlatformThread::Delegate { | 36 class FakeAudioThread : public base::PlatformThread::Delegate { |
| 36 public: | 37 public: |
| 37 FakeAudioThread(const scoped_refptr<WebRtcAudioCapturer>& capturer, | 38 FakeAudioThread(WebRtcAudioCapturer* capturer, |
| 38 const media::AudioParameters& params) | 39 const media::AudioParameters& params) |
| 39 : capturer_(capturer), | 40 : capturer_(capturer), |
| 40 thread_(), | 41 thread_(), |
| 41 closure_(false, false) { | 42 closure_(false, false) { |
| 42 DCHECK(capturer.get()); | 43 DCHECK(capturer); |
| 43 audio_bus_ = media::AudioBus::Create(params); | 44 audio_bus_ = media::AudioBus::Create(params); |
| 44 } | 45 } |
| 45 | 46 |
| 46 virtual ~FakeAudioThread() { DCHECK(thread_.is_null()); } | 47 virtual ~FakeAudioThread() { DCHECK(thread_.is_null()); } |
| 47 | 48 |
| 48 // base::PlatformThread::Delegate: | 49 // base::PlatformThread::Delegate: |
| 49 virtual void ThreadMain() OVERRIDE { | 50 virtual void ThreadMain() OVERRIDE { |
| 50 while (true) { | 51 while (true) { |
| 51 if (closure_.IsSignaled()) | 52 if (closure_.IsSignaled()) |
| 52 return; | 53 return; |
| 53 | 54 |
| 54 media::AudioCapturerSource::CaptureCallback* callback = | 55 media::AudioCapturerSource::CaptureCallback* callback = |
| 55 static_cast<media::AudioCapturerSource::CaptureCallback*>( | 56 static_cast<media::AudioCapturerSource::CaptureCallback*>( |
| 56 capturer_.get()); | 57 capturer_); |
| 57 audio_bus_->Zero(); | 58 audio_bus_->Zero(); |
| 58 callback->Capture(audio_bus_.get(), 0, 0, false); | 59 callback->Capture(audio_bus_.get(), 0, 0, false); |
| 59 | 60 |
| 60 // Sleep 1ms to yield the resource for the main thread. | 61 // Sleep 1ms to yield the resource for the main thread. |
| 61 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); | 62 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 | 65 |
| 65 void Start() { | 66 void Start() { |
| 66 base::PlatformThread::CreateWithPriority( | 67 base::PlatformThread::CreateWithPriority( |
| 67 0, this, &thread_, base::kThreadPriority_RealtimeAudio); | 68 0, this, &thread_, base::kThreadPriority_RealtimeAudio); |
| 68 CHECK(!thread_.is_null()); | 69 CHECK(!thread_.is_null()); |
| 69 } | 70 } |
| 70 | 71 |
| 71 void Stop() { | 72 void Stop() { |
| 72 closure_.Signal(); | 73 closure_.Signal(); |
| 73 base::PlatformThread::Join(thread_); | 74 base::PlatformThread::Join(thread_); |
| 74 thread_ = base::PlatformThreadHandle(); | 75 thread_ = base::PlatformThreadHandle(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 private: | 78 private: |
| 78 scoped_ptr<media::AudioBus> audio_bus_; | 79 scoped_ptr<media::AudioBus> audio_bus_; |
| 79 scoped_refptr<WebRtcAudioCapturer> capturer_; | 80 WebRtcAudioCapturer* capturer_; |
| 80 base::PlatformThreadHandle thread_; | 81 base::PlatformThreadHandle thread_; |
| 81 base::WaitableEvent closure_; | 82 base::WaitableEvent closure_; |
| 82 DISALLOW_COPY_AND_ASSIGN(FakeAudioThread); | 83 DISALLOW_COPY_AND_ASSIGN(FakeAudioThread); |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 class MockCapturerSource : public media::AudioCapturerSource { | 86 class MockCapturerSource : public media::AudioCapturerSource { |
| 86 public: | 87 public: |
| 87 explicit MockCapturerSource(WebRtcAudioCapturer* capturer) | 88 explicit MockCapturerSource(WebRtcAudioCapturer* capturer) |
| 88 : capturer_(capturer) {} | 89 : capturer_(capturer) {} |
| 89 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params, | 90 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 }; | 164 }; |
| 164 | 165 |
| 165 } // namespace | 166 } // namespace |
| 166 | 167 |
| 167 class WebRtcLocalAudioTrackTest : public ::testing::Test { | 168 class WebRtcLocalAudioTrackTest : public ::testing::Test { |
| 168 protected: | 169 protected: |
| 169 virtual void SetUp() OVERRIDE { | 170 virtual void SetUp() OVERRIDE { |
| 170 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 171 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 171 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); | 172 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); |
| 172 blink::WebMediaConstraints constraints; | 173 blink::WebMediaConstraints constraints; |
| 174 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio, |
| 175 "dummy"); |
| 176 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource(); |
| 177 blink_source_.setExtraData(audio_source); |
| 178 |
| 173 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | 179 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 174 std::string(), std::string()); | 180 std::string(), std::string()); |
| 175 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, device, | 181 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, device, |
| 176 constraints, NULL); | 182 constraints, NULL, |
| 183 audio_source); |
| 184 audio_source->SetAudioCapturer(capturer_); |
| 177 capturer_source_ = new MockCapturerSource(capturer_.get()); | 185 capturer_source_ = new MockCapturerSource(capturer_.get()); |
| 178 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) | 186 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) |
| 179 .WillOnce(Return()); | 187 .WillOnce(Return()); |
| 188 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); |
| 189 EXPECT_CALL(*capturer_source_.get(), OnStart()); |
| 180 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); | 190 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); |
| 181 } | 191 } |
| 182 | 192 |
| 183 media::AudioParameters params_; | 193 media::AudioParameters params_; |
| 194 blink::WebMediaStreamSource blink_source_; |
| 184 scoped_refptr<MockCapturerSource> capturer_source_; | 195 scoped_refptr<MockCapturerSource> capturer_source_; |
| 185 scoped_refptr<WebRtcAudioCapturer> capturer_; | 196 scoped_refptr<WebRtcAudioCapturer> capturer_; |
| 186 }; | 197 }; |
| 187 | 198 |
| 188 // Creates a capturer and audio track, fakes its audio thread, and | 199 // Creates a capturer and audio track, fakes its audio thread, and |
| 189 // connect/disconnect the sink to the audio track on the fly, the sink should | 200 // connect/disconnect the sink to the audio track on the fly, the sink should |
| 190 // get data callback when the track is connected to the capturer but not when | 201 // get data callback when the track is connected to the capturer but not when |
| 191 // the track is disconnected from the capturer. | 202 // the track is disconnected from the capturer. |
| 192 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { | 203 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { |
| 193 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 194 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 195 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 204 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 196 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 205 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 197 scoped_ptr<WebRtcLocalAudioTrack> track( | 206 scoped_ptr<WebRtcLocalAudioTrack> track( |
| 198 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); | 207 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); |
| 199 track->Start(); | 208 track->Start(); |
| 200 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); | 209 EXPECT_TRUE(track->GetAudioAdapter()->enabled()); |
| 201 | 210 |
| 202 // Connect a number of network channels to the audio track. | 211 // Connect a number of network channels to the audio track. |
| 203 static const int kNumberOfNetworkChannels = 4; | 212 static const int kNumberOfNetworkChannels = 4; |
| 204 for (int i = 0; i < kNumberOfNetworkChannels; ++i) { | 213 for (int i = 0; i < kNumberOfNetworkChannels; ++i) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 271 |
| 263 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | 272 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); |
| 264 capturer_->Stop(); | 273 capturer_->Stop(); |
| 265 track.reset(); | 274 track.reset(); |
| 266 } | 275 } |
| 267 | 276 |
| 268 // Create multiple audio tracks and enable/disable them, verify that the audio | 277 // Create multiple audio tracks and enable/disable them, verify that the audio |
| 269 // callbacks appear/disappear. | 278 // callbacks appear/disappear. |
| 270 // Flaky due to a data race, see http://crbug.com/295418 | 279 // Flaky due to a data race, see http://crbug.com/295418 |
| 271 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) { | 280 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) { |
| 272 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 273 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 274 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | 281 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( |
| 275 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 282 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 276 scoped_ptr<WebRtcLocalAudioTrack> track_1( | 283 scoped_ptr<WebRtcLocalAudioTrack> track_1( |
| 277 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); | 284 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); |
| 278 track_1->Start(); | 285 track_1->Start(); |
| 279 static_cast<webrtc::AudioTrackInterface*>( | 286 static_cast<webrtc::AudioTrackInterface*>( |
| 280 adapter_1.get())->GetRenderer()->AddChannel(0); | 287 adapter_1.get())->GetRenderer()->AddChannel(0); |
| 281 EXPECT_TRUE(track_1->GetAudioAdapter()->enabled()); | 288 EXPECT_TRUE(track_1->GetAudioAdapter()->enabled()); |
| 282 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); | 289 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); |
| 283 const media::AudioParameters params = capturer_->source_audio_parameters(); | 290 const media::AudioParameters params = capturer_->source_audio_parameters(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); | 326 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); |
| 320 | 327 |
| 321 track_1->RemoveSink(sink_1.get()); | 328 track_1->RemoveSink(sink_1.get()); |
| 322 track_1->Stop(); | 329 track_1->Stop(); |
| 323 track_1.reset(); | 330 track_1.reset(); |
| 324 | 331 |
| 325 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); | 332 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); |
| 326 track_2->RemoveSink(sink_2.get()); | 333 track_2->RemoveSink(sink_2.get()); |
| 327 track_2->Stop(); | 334 track_2->Stop(); |
| 328 track_2.reset(); | 335 track_2.reset(); |
| 329 | |
| 330 capturer_->Stop(); | |
| 331 } | 336 } |
| 332 | 337 |
| 333 | 338 |
| 334 // Start one track and verify the capturer is correctly starting its source. | 339 // Start one track and verify the capturer is correctly starting its source. |
| 335 // And it should be fine to not to call Stop() explicitly. | 340 // And it should be fine to not to call Stop() explicitly. |
| 336 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { | 341 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { |
| 337 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 338 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 339 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 342 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 340 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 343 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 341 scoped_ptr<WebRtcLocalAudioTrack> track( | 344 scoped_ptr<WebRtcLocalAudioTrack> track( |
| 342 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); | 345 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); |
| 343 track->Start(); | 346 track->Start(); |
| 344 | 347 |
| 345 // When the track goes away, it will automatically stop the | 348 // When the track goes away, it will automatically stop the |
| 346 // |capturer_source_|. | 349 // |capturer_source_|. |
| 347 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 350 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 348 capturer_->Stop(); | |
| 349 track.reset(); | 351 track.reset(); |
| 350 } | 352 } |
| 351 | 353 |
| 354 // Start two tracks and verify the capturer is correctly starting its source. |
| 355 // When the last track connected to the capturer is stopped, the source is |
| 356 // stopped. |
| 357 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) { |
| 358 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter1( |
| 359 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 360 scoped_ptr<WebRtcLocalAudioTrack> track1( |
| 361 new WebRtcLocalAudioTrack(adapter1, capturer_, NULL)); |
| 362 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 363 track1->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 364 track1->Start(); |
| 365 |
| 366 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter2( |
| 367 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 368 scoped_ptr<WebRtcLocalAudioTrack> track2( |
| 369 new WebRtcLocalAudioTrack(adapter2, capturer_, NULL)); |
| 370 static_cast<WebRtcLocalAudioSourceProvider*>( |
| 371 track2->audio_source_provider())->SetSinkParamsForTesting(params_); |
| 372 track2->Start(); |
| 373 |
| 374 track1->Stop(); |
| 375 // When the last track is stopped, it will automatically stop the |
| 376 // |capturer_source_|. |
| 377 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 378 track2->Stop(); |
| 379 } |
| 380 |
| 381 |
| 352 // Start/Stop tracks and verify the capturer is correctly starting/stopping | 382 // Start/Stop tracks and verify the capturer is correctly starting/stopping |
| 353 // its source. | 383 // its source. |
| 354 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { | 384 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { |
| 355 // Starting the first audio track will start the |capturer_source_|. | |
| 356 base::WaitableEvent event(false, false); | 385 base::WaitableEvent event(false, false); |
| 357 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 358 EXPECT_CALL(*capturer_source_.get(), OnStart()).WillOnce(SignalEvent(&event)); | |
| 359 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | 386 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( |
| 360 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 387 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 361 scoped_ptr<WebRtcLocalAudioTrack> track_1( | 388 scoped_ptr<WebRtcLocalAudioTrack> track_1( |
| 362 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); | 389 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); |
| 363 static_cast<webrtc::AudioTrackInterface*>( | 390 static_cast<webrtc::AudioTrackInterface*>( |
| 364 adapter_1.get())->GetRenderer()->AddChannel(0); | 391 adapter_1.get())->GetRenderer()->AddChannel(0); |
| 365 track_1->Start(); | 392 track_1->Start(); |
| 366 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | |
| 367 | 393 |
| 368 // Verify the data flow by connecting the sink to |track_1|. | 394 // Verify the data flow by connecting the sink to |track_1|. |
| 369 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | 395 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); |
| 370 event.Reset(); | 396 event.Reset(); |
| 371 EXPECT_CALL(*sink, FormatIsSet()).WillOnce(SignalEvent(&event)); | 397 EXPECT_CALL(*sink, FormatIsSet()).WillOnce(SignalEvent(&event)); |
| 372 EXPECT_CALL(*sink, CaptureData(_, 0, 0, _, false)) | 398 EXPECT_CALL(*sink, CaptureData(_, 0, 0, _, false)) |
| 373 .Times(AnyNumber()).WillRepeatedly(Return()); | 399 .Times(AnyNumber()).WillRepeatedly(Return()); |
| 374 track_1->AddSink(sink.get()); | 400 track_1->AddSink(sink.get()); |
| 375 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 401 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 376 | 402 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 396 // Stop the capturer again will not trigger stopping the source of the | 422 // Stop the capturer again will not trigger stopping the source of the |
| 397 // capturer again.. | 423 // capturer again.. |
| 398 event.Reset(); | 424 event.Reset(); |
| 399 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); | 425 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); |
| 400 capturer_->Stop(); | 426 capturer_->Stop(); |
| 401 } | 427 } |
| 402 | 428 |
| 403 // Create a new capturer with new source, connect it to a new audio track. | 429 // Create a new capturer with new source, connect it to a new audio track. |
| 404 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { | 430 TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { |
| 405 // Setup the first audio track and start it. | 431 // Setup the first audio track and start it. |
| 406 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); | |
| 407 EXPECT_CALL(*capturer_source_.get(), OnStart()); | |
| 408 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( | 432 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( |
| 409 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 433 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 410 scoped_ptr<WebRtcLocalAudioTrack> track_1( | 434 scoped_ptr<WebRtcLocalAudioTrack> track_1( |
| 411 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); | 435 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); |
| 412 track_1->Start(); | 436 track_1->Start(); |
| 413 | 437 |
| 414 // Connect a number of network channels to the |track_1|. | 438 // Connect a number of network channels to the |track_1|. |
| 415 static const int kNumberOfNetworkChannelsForTrack1 = 2; | 439 static const int kNumberOfNetworkChannelsForTrack1 = 2; |
| 416 for (int i = 0; i < kNumberOfNetworkChannelsForTrack1; ++i) { | 440 for (int i = 0; i < kNumberOfNetworkChannelsForTrack1; ++i) { |
| 417 static_cast<webrtc::AudioTrackInterface*>( | 441 static_cast<webrtc::AudioTrackInterface*>( |
| 418 adapter_1.get())->GetRenderer()->AddChannel(i); | 442 adapter_1.get())->GetRenderer()->AddChannel(i); |
| 419 } | 443 } |
| 420 // Verify the data flow by connecting the |sink_1| to |track_1|. | 444 // Verify the data flow by connecting the |sink_1| to |track_1|. |
| 421 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); | 445 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); |
| 422 EXPECT_CALL(*sink_1.get(), | 446 EXPECT_CALL(*sink_1.get(), |
| 423 CaptureData(kNumberOfNetworkChannelsForTrack1, | 447 CaptureData(kNumberOfNetworkChannelsForTrack1, |
| 424 0, 0, _, false)) | 448 0, 0, _, false)) |
| 425 .Times(AnyNumber()).WillRepeatedly(Return()); | 449 .Times(AnyNumber()).WillRepeatedly(Return()); |
| 426 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber()); | 450 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber()); |
| 427 track_1->AddSink(sink_1.get()); | 451 track_1->AddSink(sink_1.get()); |
| 428 | 452 |
| 429 // Create a new capturer with new source with different audio format. | 453 // Create a new capturer with new source with different audio format. |
| 430 blink::WebMediaConstraints constraints; | 454 blink::WebMediaConstraints constraints; |
| 431 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, | 455 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 432 std::string(), std::string()); | 456 std::string(), std::string()); |
| 433 scoped_refptr<WebRtcAudioCapturer> new_capturer( | 457 scoped_refptr<WebRtcAudioCapturer> new_capturer( |
| 434 WebRtcAudioCapturer::CreateCapturer(-1, device, constraints, NULL)); | 458 WebRtcAudioCapturer::CreateCapturer(-1, device, constraints, NULL, NULL)); |
| 435 scoped_refptr<MockCapturerSource> new_source( | 459 scoped_refptr<MockCapturerSource> new_source( |
| 436 new MockCapturerSource(new_capturer.get())); | 460 new MockCapturerSource(new_capturer.get())); |
| 437 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); | 461 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); |
| 462 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); |
| 463 EXPECT_CALL(*new_source.get(), OnStart()); |
| 464 |
| 438 media::AudioParameters new_param( | 465 media::AudioParameters new_param( |
| 439 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 466 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 440 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); | 467 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); |
| 441 new_capturer->SetCapturerSourceForTesting(new_source, new_param); | 468 new_capturer->SetCapturerSourceForTesting(new_source, new_param); |
| 442 | 469 |
| 443 // Setup the second audio track, connect it to the new capturer and start it. | 470 // Setup the second audio track, connect it to the new capturer and start it. |
| 444 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); | |
| 445 EXPECT_CALL(*new_source.get(), OnStart()); | |
| 446 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( | 471 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( |
| 447 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 472 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 448 scoped_ptr<WebRtcLocalAudioTrack> track_2( | 473 scoped_ptr<WebRtcLocalAudioTrack> track_2( |
| 449 new WebRtcLocalAudioTrack(adapter_2, new_capturer, NULL)); | 474 new WebRtcLocalAudioTrack(adapter_2, new_capturer, NULL)); |
| 450 track_2->Start(); | 475 track_2->Start(); |
| 451 | 476 |
| 452 // Connect a number of network channels to the |track_2|. | 477 // Connect a number of network channels to the |track_2|. |
| 453 static const int kNumberOfNetworkChannelsForTrack2 = 3; | 478 static const int kNumberOfNetworkChannelsForTrack2 = 3; |
| 454 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { | 479 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { |
| 455 static_cast<webrtc::AudioTrackInterface*>( | 480 static_cast<webrtc::AudioTrackInterface*>( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 470 EXPECT_CALL(*new_source.get(), OnStop()) | 495 EXPECT_CALL(*new_source.get(), OnStop()) |
| 471 .Times(1).WillOnce(SignalEvent(&event)); | 496 .Times(1).WillOnce(SignalEvent(&event)); |
| 472 new_capturer->Stop(); | 497 new_capturer->Stop(); |
| 473 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 498 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 474 | 499 |
| 475 // Stop the capturer of the first audio track. | 500 // Stop the capturer of the first audio track. |
| 476 EXPECT_CALL(*capturer_source_.get(), OnStop()); | 501 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 477 capturer_->Stop(); | 502 capturer_->Stop(); |
| 478 } | 503 } |
| 479 | 504 |
| 480 | |
| 481 // Make sure a audio track can deliver packets with a buffer size smaller than | 505 // Make sure a audio track can deliver packets with a buffer size smaller than |
| 482 // 10ms when it is not connected with a peer connection. | 506 // 10ms when it is not connected with a peer connection. |
| 483 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { | 507 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { |
| 484 // Setup a capturer which works with a buffer size smaller than 10ms. | 508 // Setup a capturer which works with a buffer size smaller than 10ms. |
| 485 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | 509 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 486 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); | 510 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); |
| 487 | 511 |
| 488 // Create a capturer with new source which works with the format above. | 512 // Create a capturer with new source which works with the format above. |
| 489 MockMediaConstraintFactory factory; | 513 MockMediaConstraintFactory factory; |
| 490 factory.DisableDefaultAudioConstraints(); | 514 factory.DisableDefaultAudioConstraints(); |
| 491 scoped_refptr<WebRtcAudioCapturer> capturer( | 515 scoped_refptr<WebRtcAudioCapturer> capturer( |
| 492 WebRtcAudioCapturer::CreateCapturer( | 516 WebRtcAudioCapturer::CreateCapturer( |
| 493 -1, | 517 -1, |
| 494 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, | 518 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 495 "", "", params.sample_rate(), | 519 "", "", params.sample_rate(), |
| 496 params.channel_layout(), | 520 params.channel_layout(), |
| 497 params.frames_per_buffer()), | 521 params.frames_per_buffer()), |
| 498 factory.CreateWebMediaConstraints(), | 522 factory.CreateWebMediaConstraints(), |
| 499 NULL)); | 523 NULL, NULL)); |
| 500 scoped_refptr<MockCapturerSource> source( | 524 scoped_refptr<MockCapturerSource> source( |
| 501 new MockCapturerSource(capturer.get())); | 525 new MockCapturerSource(capturer.get())); |
| 502 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); | 526 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); |
| 527 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); |
| 528 EXPECT_CALL(*source.get(), OnStart()); |
| 503 capturer->SetCapturerSourceForTesting(source, params); | 529 capturer->SetCapturerSourceForTesting(source, params); |
| 504 | 530 |
| 505 // Setup a audio track, connect it to the capturer and start it. | 531 // Setup a audio track, connect it to the capturer and start it. |
| 506 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); | |
| 507 EXPECT_CALL(*source.get(), OnStart()); | |
| 508 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( | 532 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( |
| 509 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); | 533 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); |
| 510 scoped_ptr<WebRtcLocalAudioTrack> track( | 534 scoped_ptr<WebRtcLocalAudioTrack> track( |
| 511 new WebRtcLocalAudioTrack(adapter, capturer, NULL)); | 535 new WebRtcLocalAudioTrack(adapter, capturer, NULL)); |
| 512 track->Start(); | 536 track->Start(); |
| 513 | 537 |
| 514 // Verify the data flow by connecting the |sink| to |track|. | 538 // Verify the data flow by connecting the |sink| to |track|. |
| 515 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); | 539 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); |
| 516 base::WaitableEvent event(false, false); | 540 base::WaitableEvent event(false, false); |
| 517 EXPECT_CALL(*sink, FormatIsSet()).Times(1); | 541 EXPECT_CALL(*sink, FormatIsSet()).Times(1); |
| 518 // Verify the sinks are getting the packets with an expecting buffer size. | 542 // Verify the sinks are getting the packets with an expecting buffer size. |
| 519 #if defined(OS_ANDROID) | 543 #if defined(OS_ANDROID) |
| 520 const int expected_buffer_size = params.sample_rate() / 100; | 544 const int expected_buffer_size = params.sample_rate() / 100; |
| 521 #else | 545 #else |
| 522 const int expected_buffer_size = params.frames_per_buffer(); | 546 const int expected_buffer_size = params.frames_per_buffer(); |
| 523 #endif | 547 #endif |
| 524 EXPECT_CALL(*sink, CaptureData( | 548 EXPECT_CALL(*sink, CaptureData( |
| 525 0, 0, 0, _, false)) | 549 0, 0, 0, _, false)) |
| 526 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); | 550 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); |
| 527 track->AddSink(sink.get()); | 551 track->AddSink(sink.get()); |
| 528 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); | 552 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); |
| 529 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); | 553 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer()); |
| 530 | 554 |
| 531 // Stopping the new source will stop the second track. | 555 // Stopping the new source will stop the second track. |
| 532 EXPECT_CALL(*source, OnStop()).Times(1); | 556 EXPECT_CALL(*source, OnStop()).Times(1); |
| 533 capturer->Stop(); | 557 capturer->Stop(); |
| 558 |
| 559 // Even though this test don't use |capturer_source_| it will be stopped |
| 560 // during teardown of the test harness. |
| 561 EXPECT_CALL(*capturer_source_.get(), OnStop()); |
| 534 } | 562 } |
| 535 | 563 |
| 536 } // namespace content | 564 } // namespace content |
| OLD | NEW |