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