webrtc_local_audio_track_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 3// found in the LICENSE file. 4 5#include "base/synchronization/waitable_event.h" 6#include "base/test/test_timeouts.h" 7#include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h" 8#include "content/renderer/media/webrtc_audio_capturer.h" 9#include "content/renderer/media/webrtc_audio_device_impl.h" 10#include "content/renderer/media/webrtc_local_audio_source_provider.h" 11#include "content/renderer/media/webrtc_local_audio_track.h" 12#include "media/audio/audio_parameters.h" 13#include "media/base/audio_bus.h" 14#include "media/base/audio_capturer_source.h" 15#include "testing/gmock/include/gmock/gmock.h" 16#include "testing/gtest/include/gtest/gtest.h" 17#include "third_party/WebKit/public/platform/WebMediaConstraints.h" 18#include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h" 19 20using ::testing::_; 21using ::testing::AnyNumber; 22using ::testing::AtLeast; 23using ::testing::Return; 24 25namespace content { 26 27namespace { 28 29ACTION_P(SignalEvent, event) { 30 event->Signal(); 31} 32 33// A simple thread that we use to fake the audio thread which provides data to 34// the |WebRtcAudioCapturer|. 35class FakeAudioThread : public base::PlatformThread::Delegate { 36 public: 37 FakeAudioThread(const scoped_refptr<WebRtcAudioCapturer>& capturer, 38 const media::AudioParameters& params) 39 : capturer_(capturer), 40 thread_(), 41 closure_(false, false) { 42 DCHECK(capturer.get()); 43 audio_bus_ = media::AudioBus::Create(params); 44 } 45 46 virtual ~FakeAudioThread() { DCHECK(thread_.is_null()); } 47 48 // base::PlatformThread::Delegate: 49 virtual void ThreadMain() OVERRIDE { 50 while (true) { 51 if (closure_.IsSignaled()) 52 return; 53 54 media::AudioCapturerSource::CaptureCallback* callback = 55 static_cast<media::AudioCapturerSource::CaptureCallback*>( 56 capturer_.get()); 57 audio_bus_->Zero(); 58 callback->Capture(audio_bus_.get(), 0, 0, false); 59 60 // Sleep 1ms to yield the resource for the main thread. 61 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); 62 } 63 } 64 65 void Start() { 66 base::PlatformThread::CreateWithPriority( 67 0, this, &thread_, base::kThreadPriority_RealtimeAudio); 68 CHECK(!thread_.is_null()); 69 } 70 71 void Stop() { 72 closure_.Signal(); 73 base::PlatformThread::Join(thread_); 74 thread_ = base::PlatformThreadHandle(); 75 } 76 77 private: 78 scoped_ptr<media::AudioBus> audio_bus_; 79 scoped_refptr<WebRtcAudioCapturer> capturer_; 80 base::PlatformThreadHandle thread_; 81 base::WaitableEvent closure_; 82 DISALLOW_COPY_AND_ASSIGN(FakeAudioThread); 83}; 84 85class MockCapturerSource : public media::AudioCapturerSource { 86 public: 87 explicit MockCapturerSource(WebRtcAudioCapturer* capturer) 88 : capturer_(capturer) {} 89 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params, 90 CaptureCallback* callback, 91 int session_id)); 92 MOCK_METHOD0(OnStart, void()); 93 MOCK_METHOD0(OnStop, void()); 94 MOCK_METHOD1(SetVolume, void(double volume)); 95 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable)); 96 97 virtual void Initialize(const media::AudioParameters& params, 98 CaptureCallback* callback, 99 int session_id) OVERRIDE { 100 DCHECK(params.IsValid()); 101 params_ = params; 102 OnInitialize(params, callback, session_id); 103 } 104 virtual void Start() OVERRIDE { 105 audio_thread_.reset(new FakeAudioThread(capturer_, params_)); 106 audio_thread_->Start(); 107 OnStart(); 108 } 109 virtual void Stop() OVERRIDE { 110 audio_thread_->Stop(); 111 audio_thread_.reset(); 112 OnStop(); 113 } 114 protected: 115 virtual ~MockCapturerSource() {} 116 117 private: 118 scoped_ptr<FakeAudioThread> audio_thread_; 119 WebRtcAudioCapturer* capturer_; 120 media::AudioParameters params_; 121}; 122 123// TODO(xians): Use MediaStreamAudioSink. 124class MockMediaStreamAudioSink : public PeerConnectionAudioSink { 125 public: 126 MockMediaStreamAudioSink() {} 127 ~MockMediaStreamAudioSink() {} 128 int OnData(const int16* audio_data, 129 int sample_rate, 130 int number_of_channels, 131 int number_of_frames, 132 const std::vector<int>& channels, 133 int audio_delay_milliseconds, 134 int current_volume, 135 bool need_audio_processing, 136 bool key_pressed) OVERRIDE { 137 CaptureData(channels.size(), 138 sample_rate, 139 number_of_channels, 140 number_of_frames, 141 audio_delay_milliseconds, 142 current_volume, 143 need_audio_processing, 144 key_pressed); 145 return 0; 146 } 147 MOCK_METHOD8(CaptureData, 148 void(int number_of_network_channels, 149 int sample_rate, 150 int number_of_channels, 151 int number_of_frames, 152 int audio_delay_milliseconds, 153 int current_volume, 154 bool need_audio_processing, 155 bool key_pressed)); 156 MOCK_METHOD1(OnSetFormat, void(const media::AudioParameters& params)); 157}; 158 159} // namespace 160 161class WebRtcLocalAudioTrackTest : public ::testing::Test { 162 protected: 163 virtual void SetUp() OVERRIDE { 164 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 165 media::CHANNEL_LAYOUT_STEREO, 2, 0, 48000, 16, 480); 166 blink::WebMediaConstraints constraints; 167 capturer_ = WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), 168 constraints, NULL); 169 capturer_source_ = new MockCapturerSource(capturer_.get()); 170 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1)) 171 .WillOnce(Return()); 172 capturer_->SetCapturerSourceForTesting(capturer_source_, params_); 173 } 174 175 media::AudioParameters params_; 176 scoped_refptr<MockCapturerSource> capturer_source_; 177 scoped_refptr<WebRtcAudioCapturer> capturer_; 178}; 179 180// 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 182// get data callback when the track is connected to the capturer but not when 183// the track is disconnected from the capturer. 184TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) { 185 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 186 EXPECT_CALL(*capturer_source_.get(), OnStart()); 187 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 188 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 189 scoped_ptr<WebRtcLocalAudioTrack> track( 190 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); 191 static_cast<WebRtcLocalAudioSourceProvider*>( 192 track->audio_source_provider())->SetSinkParamsForTesting(params_); 193 track->Start(); 194 EXPECT_TRUE(track->track()->enabled()); 195 196 // Connect a number of network channels to the audio track. 197 static const int kNumberOfNetworkChannels = 4; 198 for (int i = 0; i < kNumberOfNetworkChannels; ++i) { 199 static_cast<webrtc::AudioTrackInterface*>( 200 adapter.get())->GetRenderer()->AddChannel(i); 201 } 202 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 203 const media::AudioParameters params = capturer_->source_audio_parameters(); 204 base::WaitableEvent event(false, false); 205 EXPECT_CALL(*sink, OnSetFormat(_)).WillOnce(Return()); 206 EXPECT_CALL(*sink, 207 CaptureData(kNumberOfNetworkChannels, 208 params.sample_rate(), 209 params.channels(), 210 params.sample_rate() / 100, 211 0, 212 0, 213 true, 214 false)).Times(AtLeast(1)) 215 .WillRepeatedly(SignalEvent(&event)); 216 track->AddSink(sink.get()); 217 218 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 219 track->RemoveSink(sink.get()); 220 221 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 222 capturer_->Stop(); 223} 224 225// The same setup as ConnectAndDisconnectOneSink, but enable and disable the 226// audio track on the fly. When the audio track is disabled, there is no data 227// callback to the sink; when the audio track is enabled, there comes data 228// callback. 229// TODO(xians): Enable this test after resolving the racing issue that TSAN 230// reports on MediaStreamTrack::enabled(); 231TEST_F(WebRtcLocalAudioTrackTest, DISABLED_DisableEnableAudioTrack) { 232 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 233 EXPECT_CALL(*capturer_source_.get(), OnStart()); 234 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 235 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 236 scoped_ptr<WebRtcLocalAudioTrack> track( 237 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); 238 static_cast<WebRtcLocalAudioSourceProvider*>( 239 track->audio_source_provider())->SetSinkParamsForTesting(params_); 240 track->Start(); 241 static_cast<webrtc::AudioTrackInterface*>( 242 adapter.get())->GetRenderer()->AddChannel(0); 243 EXPECT_TRUE(track->track()->enabled()); 244 EXPECT_TRUE(track->track()->set_enabled(false)); 245 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 246 const media::AudioParameters params = capturer_->source_audio_parameters(); 247 base::WaitableEvent event(false, false); 248 EXPECT_CALL(*sink, OnSetFormat(_)).Times(1); 249 EXPECT_CALL(*sink, 250 CaptureData(1, 251 params.sample_rate(), 252 params.channels(), 253 params.sample_rate() / 100, 254 0, 255 0, 256 true, 257 false)).Times(0); 258 track->AddSink(sink.get()); 259 EXPECT_FALSE(event.TimedWait(TestTimeouts::tiny_timeout())); 260 261 event.Reset(); 262 EXPECT_CALL(*sink, 263 CaptureData(1, 264 params.sample_rate(), 265 params.channels(), 266 params.sample_rate() / 100, 267 0, 268 0, 269 true, 270 false)).Times(AtLeast(1)) 271 .WillRepeatedly(SignalEvent(&event)); 272 EXPECT_TRUE(track->track()->set_enabled(true)); 273 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 274 track->RemoveSink(sink.get()); 275 276 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 277 capturer_->Stop(); 278 track.reset(); 279} 280 281// Create multiple audio tracks and enable/disable them, verify that the audio 282// callbacks appear/disappear. 283// Flaky due to a data race, see http://crbug.com/295418 284TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) { 285 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 286 EXPECT_CALL(*capturer_source_.get(), OnStart()); 287 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( 288 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 289 scoped_ptr<WebRtcLocalAudioTrack> track_1( 290 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); 291 static_cast<WebRtcLocalAudioSourceProvider*>( 292 track_1->audio_source_provider())->SetSinkParamsForTesting(params_); 293 track_1->Start(); 294 static_cast<webrtc::AudioTrackInterface*>( 295 adapter_1.get())->GetRenderer()->AddChannel(0); 296 EXPECT_TRUE(track_1->track()->enabled()); 297 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); 298 const media::AudioParameters params = capturer_->source_audio_parameters(); 299 base::WaitableEvent event_1(false, false); 300 EXPECT_CALL(*sink_1, OnSetFormat(_)).WillOnce(Return()); 301 EXPECT_CALL(*sink_1, 302 CaptureData(1, 303 params.sample_rate(), 304 params.channels(), 305 params.sample_rate() / 100, 306 0, 307 0, 308 true, 309 false)).Times(AtLeast(1)) 310 .WillRepeatedly(SignalEvent(&event_1)); 311 track_1->AddSink(sink_1.get()); 312 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); 313 314 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( 315 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 316 scoped_ptr<WebRtcLocalAudioTrack> track_2( 317 new WebRtcLocalAudioTrack(adapter_2, capturer_, NULL)); 318 static_cast<WebRtcLocalAudioSourceProvider*>( 319 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); 320 track_2->Start(); 321 static_cast<webrtc::AudioTrackInterface*>( 322 adapter_2.get())->GetRenderer()->AddChannel(1); 323 EXPECT_TRUE(track_2->track()->enabled()); 324 325 // Verify both |sink_1| and |sink_2| get data. 326 event_1.Reset(); 327 base::WaitableEvent event_2(false, false); 328 329 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); 330 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(Return()); 331 EXPECT_CALL(*sink_1, 332 CaptureData(1, 333 params.sample_rate(), 334 params.channels(), 335 params.sample_rate() / 100, 336 0, 337 0, 338 true, 339 false)).Times(AtLeast(1)) 340 .WillRepeatedly(SignalEvent(&event_1)); 341 EXPECT_CALL(*sink_2, 342 CaptureData(1, 343 params.sample_rate(), 344 params.channels(), 345 params.sample_rate() / 100, 346 0, 347 0, 348 true, 349 false)).Times(AtLeast(1)) 350 .WillRepeatedly(SignalEvent(&event_2)); 351 track_2->AddSink(sink_2.get()); 352 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout())); 353 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout())); 354 355 track_1->RemoveSink(sink_1.get()); 356 track_1->Stop(); 357 track_1.reset(); 358 359 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return()); 360 track_2->RemoveSink(sink_2.get()); 361 track_2->Stop(); 362 track_2.reset(); 363 364 capturer_->Stop(); 365} 366 367 368// Start one track and verify the capturer is correctly starting its source. 369// And it should be fine to not to call Stop() explicitly. 370TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) { 371 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 372 EXPECT_CALL(*capturer_source_.get(), OnStart()); 373 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 374 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 375 scoped_ptr<WebRtcLocalAudioTrack> track( 376 new WebRtcLocalAudioTrack(adapter, capturer_, NULL)); 377 static_cast<WebRtcLocalAudioSourceProvider*>( 378 track->audio_source_provider())->SetSinkParamsForTesting(params_); 379 track->Start(); 380 381 // When the track goes away, it will automatically stop the 382 // |capturer_source_|. 383 EXPECT_CALL(*capturer_source_.get(), OnStop()); 384 capturer_->Stop(); 385 track.reset(); 386} 387 388// Start/Stop tracks and verify the capturer is correctly starting/stopping 389// its source. 390TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) { 391 // Starting the first audio track will start the |capturer_source_|. 392 base::WaitableEvent event(false, false); 393 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 394 EXPECT_CALL(*capturer_source_.get(), OnStart()).WillOnce(SignalEvent(&event)); 395 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( 396 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 397 scoped_ptr<WebRtcLocalAudioTrack> track_1( 398 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); 399 static_cast<webrtc::AudioTrackInterface*>( 400 adapter_1.get())->GetRenderer()->AddChannel(0); 401 static_cast<WebRtcLocalAudioSourceProvider*>( 402 track_1->audio_source_provider())->SetSinkParamsForTesting(params_); 403 track_1->Start(); 404 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 405 406 // Verify the data flow by connecting the sink to |track_1|. 407 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 408 event.Reset(); 409 EXPECT_CALL(*sink, OnSetFormat(_)).WillOnce(SignalEvent(&event)); 410 EXPECT_CALL(*sink, CaptureData(_, _, _, _, 0, 0, true, false)) 411 .Times(AnyNumber()).WillRepeatedly(Return()); 412 track_1->AddSink(sink.get()); 413 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 414 415 // Start the second audio track will not start the |capturer_source_| 416 // since it has been started. 417 EXPECT_CALL(*capturer_source_.get(), OnStart()).Times(0); 418 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( 419 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 420 scoped_ptr<WebRtcLocalAudioTrack> track_2( 421 new WebRtcLocalAudioTrack(adapter_2, capturer_, NULL)); 422 static_cast<WebRtcLocalAudioSourceProvider*>( 423 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); 424 track_2->Start(); 425 static_cast<webrtc::AudioTrackInterface*>( 426 adapter_2.get())->GetRenderer()->AddChannel(1); 427 428 // Stop the capturer will clear up the track lists in the capturer. 429 EXPECT_CALL(*capturer_source_.get(), OnStop()); 430 capturer_->Stop(); 431 432 // Adding a new track to the capturer. 433 track_2->AddSink(sink.get()); 434 EXPECT_CALL(*sink, OnSetFormat(_)).Times(0); 435 436 // Stop the capturer again will not trigger stopping the source of the 437 // capturer again.. 438 event.Reset(); 439 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0); 440 capturer_->Stop(); 441} 442 443// Create a new capturer with new source, connect it to a new audio track. 444TEST_F(WebRtcLocalAudioTrackTest, ConnectTracksToDifferentCapturers) { 445 // Setup the first audio track and start it. 446 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true)); 447 EXPECT_CALL(*capturer_source_.get(), OnStart()); 448 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1( 449 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 450 scoped_ptr<WebRtcLocalAudioTrack> track_1( 451 new WebRtcLocalAudioTrack(adapter_1, capturer_, NULL)); 452 static_cast<WebRtcLocalAudioSourceProvider*>( 453 track_1->audio_source_provider())->SetSinkParamsForTesting(params_); 454 track_1->Start(); 455 456 // Connect a number of network channels to the |track_1|. 457 static const int kNumberOfNetworkChannelsForTrack1 = 2; 458 for (int i = 0; i < kNumberOfNetworkChannelsForTrack1; ++i) { 459 static_cast<webrtc::AudioTrackInterface*>( 460 adapter_1.get())->GetRenderer()->AddChannel(i); 461 } 462 // Verify the data flow by connecting the |sink_1| to |track_1|. 463 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink()); 464 EXPECT_CALL( 465 *sink_1.get(), 466 CaptureData( 467 kNumberOfNetworkChannelsForTrack1, 48000, 2, _, 0, 0, true, false)) 468 .Times(AnyNumber()).WillRepeatedly(Return()); 469 EXPECT_CALL(*sink_1.get(), OnSetFormat(_)).Times(AnyNumber()); 470 track_1->AddSink(sink_1.get()); 471 472 // Create a new capturer with new source with different audio format. 473 blink::WebMediaConstraints constraints; 474 scoped_refptr<WebRtcAudioCapturer> new_capturer( 475 WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(), 476 constraints, NULL)); 477 scoped_refptr<MockCapturerSource> new_source( 478 new MockCapturerSource(new_capturer.get())); 479 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1)); 480 media::AudioParameters new_param( 481 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 482 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441); 483 new_capturer->SetCapturerSourceForTesting(new_source, new_param); 484 485 // Setup the second audio track, connect it to the new capturer and start it. 486 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true)); 487 EXPECT_CALL(*new_source.get(), OnStart()); 488 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2( 489 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 490 scoped_ptr<WebRtcLocalAudioTrack> track_2( 491 new WebRtcLocalAudioTrack(adapter_2, new_capturer, NULL)); 492 static_cast<WebRtcLocalAudioSourceProvider*>( 493 track_2->audio_source_provider())->SetSinkParamsForTesting(params_); 494 track_2->Start(); 495 496 // Connect a number of network channels to the |track_2|. 497 static const int kNumberOfNetworkChannelsForTrack2 = 3; 498 for (int i = 0; i < kNumberOfNetworkChannelsForTrack2; ++i) { 499 static_cast<webrtc::AudioTrackInterface*>( 500 adapter_2.get())->GetRenderer()->AddChannel(i); 501 } 502 // Verify the data flow by connecting the |sink_2| to |track_2|. 503 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink()); 504 base::WaitableEvent event(false, false); 505 EXPECT_CALL( 506 *sink_2, 507 CaptureData( 508 kNumberOfNetworkChannelsForTrack2, new_param.sample_rate(), 509 new_param.channels(), _, 0, 0, true, false)) 510 .Times(AnyNumber()).WillRepeatedly(Return()); 511 EXPECT_CALL(*sink_2, OnSetFormat(_)).WillOnce(SignalEvent(&event)); 512 track_2->AddSink(sink_2.get()); 513 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 514 515 // Stopping the new source will stop the second track. 516 event.Reset(); 517 EXPECT_CALL(*new_source.get(), OnStop()) 518 .Times(1).WillOnce(SignalEvent(&event)); 519 new_capturer->Stop(); 520 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 521 522 // Stop the capturer of the first audio track. 523 EXPECT_CALL(*capturer_source_.get(), OnStop()); 524 capturer_->Stop(); 525} 526 527 528// Make sure a audio track can deliver packets with a buffer size smaller than 529// 10ms when it is not connected with a peer connection. 530TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) { 531 // Setup a capturer which works with a buffer size smaller than 10ms. 532 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, 533 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128); 534 535 // Create a capturer with new source which works with the format above. 536 blink::WebMediaConstraints constraints; 537 scoped_refptr<WebRtcAudioCapturer> capturer( 538 WebRtcAudioCapturer::CreateCapturer( 539 -1, 540 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE, 541 "", "", params.sample_rate(), 542 params.channel_layout(), 543 params.frames_per_buffer()), 544 constraints, 545 NULL)); 546 scoped_refptr<MockCapturerSource> source( 547 new MockCapturerSource(capturer.get())); 548 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1)); 549 capturer->SetCapturerSourceForTesting(source, params); 550 551 // Setup a audio track, connect it to the capturer and start it. 552 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true)); 553 EXPECT_CALL(*source.get(), OnStart()); 554 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter( 555 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL)); 556 scoped_ptr<WebRtcLocalAudioTrack> track( 557 new WebRtcLocalAudioTrack(adapter, capturer, NULL)); 558 static_cast<WebRtcLocalAudioSourceProvider*>( 559 track->audio_source_provider())->SetSinkParamsForTesting(params); 560 track->Start(); 561 562 // Verify the data flow by connecting the |sink| to |track|. 563 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink()); 564 base::WaitableEvent event(false, false); 565 EXPECT_CALL(*sink, OnSetFormat(_)).Times(1); 566 // Verify the sinks are getting the packets with an expecting buffer size. 567#if defined(OS_ANDROID) 568 const int expected_buffer_size = params.sample_rate() / 100; 569#else 570 const int expected_buffer_size = params.frames_per_buffer(); 571#endif 572 EXPECT_CALL(*sink, CaptureData( 573 0, params.sample_rate(), params.channels(), expected_buffer_size, 574 0, 0, true, false)) 575 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event)); 576 track->AddSink(sink.get()); 577 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout())); 578 579 // Stopping the new source will stop the second track. 580 EXPECT_CALL(*source, OnStop()).Times(1); 581 capturer->Stop(); 582} 583 584} // namespace content 585