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/WebKit/public/web/WebHeap.h"
20 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
23 using ::testing::AnyNumber;
24 using ::testing::AtLeast;
25 using ::testing::Return;
31 ACTION_P(SignalEvent, event) {
35 // A simple thread that we use to fake the audio thread which provides data to
36 // the |WebRtcAudioCapturer|.
37 class FakeAudioThread : public base::PlatformThread::Delegate {
39 FakeAudioThread(WebRtcAudioCapturer* capturer,
40 const media::AudioParameters& params)
41 : capturer_(capturer),
43 closure_(false, false) {
45 audio_bus_ = media::AudioBus::Create(params);
48 virtual ~FakeAudioThread() { DCHECK(thread_.is_null()); }
50 // base::PlatformThread::Delegate:
51 virtual void ThreadMain() OVERRIDE {
53 if (closure_.IsSignaled())
56 media::AudioCapturerSource::CaptureCallback* callback =
57 static_cast<media::AudioCapturerSource::CaptureCallback*>(
60 callback->Capture(audio_bus_.get(), 0, 0, false);
62 // Sleep 1ms to yield the resource for the main thread.
63 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
68 base::PlatformThread::CreateWithPriority(
69 0, this, &thread_, base::kThreadPriority_RealtimeAudio);
70 CHECK(!thread_.is_null());
75 base::PlatformThread::Join(thread_);
76 thread_ = base::PlatformThreadHandle();
80 scoped_ptr<media::AudioBus> audio_bus_;
81 WebRtcAudioCapturer* capturer_;
82 base::PlatformThreadHandle thread_;
83 base::WaitableEvent closure_;
84 DISALLOW_COPY_AND_ASSIGN(FakeAudioThread);
87 class MockCapturerSource : public media::AudioCapturerSource {
89 explicit MockCapturerSource(WebRtcAudioCapturer* capturer)
90 : capturer_(capturer) {}
91 MOCK_METHOD3(OnInitialize, void(const media::AudioParameters& params,
92 CaptureCallback* callback,
94 MOCK_METHOD0(OnStart, void());
95 MOCK_METHOD0(OnStop, void());
96 MOCK_METHOD1(SetVolume, void(double volume));
97 MOCK_METHOD1(SetAutomaticGainControl, void(bool enable));
99 virtual void Initialize(const media::AudioParameters& params,
100 CaptureCallback* callback,
101 int session_id) OVERRIDE {
102 DCHECK(params.IsValid());
104 OnInitialize(params, callback, session_id);
106 virtual void Start() OVERRIDE {
107 audio_thread_.reset(new FakeAudioThread(capturer_, params_));
108 audio_thread_->Start();
111 virtual void Stop() OVERRIDE {
112 audio_thread_->Stop();
113 audio_thread_.reset();
117 virtual ~MockCapturerSource() {}
120 scoped_ptr<FakeAudioThread> audio_thread_;
121 WebRtcAudioCapturer* capturer_;
122 media::AudioParameters params_;
125 // TODO(xians): Use MediaStreamAudioSink.
126 class MockMediaStreamAudioSink : public PeerConnectionAudioSink {
128 MockMediaStreamAudioSink() {}
129 ~MockMediaStreamAudioSink() {}
130 int OnData(const int16* audio_data,
132 int number_of_channels,
133 int number_of_frames,
134 const std::vector<int>& channels,
135 int audio_delay_milliseconds,
137 bool need_audio_processing,
138 bool key_pressed) OVERRIDE {
139 EXPECT_EQ(params_.sample_rate(), sample_rate);
140 EXPECT_EQ(params_.channels(), number_of_channels);
141 EXPECT_EQ(params_.frames_per_buffer(), number_of_frames);
142 CaptureData(channels.size(),
143 audio_delay_milliseconds,
145 need_audio_processing,
149 MOCK_METHOD5(CaptureData,
150 void(int number_of_network_channels,
151 int audio_delay_milliseconds,
153 bool need_audio_processing,
155 void OnSetFormat(const media::AudioParameters& params) {
159 MOCK_METHOD0(FormatIsSet, void());
161 const media::AudioParameters& audio_params() const { return params_; }
164 media::AudioParameters params_;
169 class WebRtcLocalAudioTrackTest : public ::testing::Test {
171 virtual void SetUp() OVERRIDE {
172 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
173 media::CHANNEL_LAYOUT_STEREO, 2, 48000, 16, 480);
174 MockMediaConstraintFactory constraint_factory;
175 blink_source_.initialize("dummy", blink::WebMediaStreamSource::TypeAudio,
177 MediaStreamAudioSource* audio_source = new MediaStreamAudioSource();
178 blink_source_.setExtraData(audio_source);
180 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE,
181 std::string(), std::string());
182 capturer_ = WebRtcAudioCapturer::CreateCapturer(
183 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL,
185 audio_source->SetAudioCapturer(capturer_.get());
186 capturer_source_ = new MockCapturerSource(capturer_.get());
187 EXPECT_CALL(*capturer_source_.get(), OnInitialize(_, capturer_.get(), -1))
189 EXPECT_CALL(*capturer_source_.get(), SetAutomaticGainControl(true));
190 EXPECT_CALL(*capturer_source_.get(), OnStart());
191 capturer_->SetCapturerSourceForTesting(capturer_source_, params_);
194 virtual void TearDown() OVERRIDE {
195 blink_source_.reset();
196 blink::WebHeap::collectAllGarbageForTesting();
199 media::AudioParameters params_;
200 blink::WebMediaStreamSource blink_source_;
201 scoped_refptr<MockCapturerSource> capturer_source_;
202 scoped_refptr<WebRtcAudioCapturer> capturer_;
205 // Creates a capturer and audio track, fakes its audio thread, and
206 // connect/disconnect the sink to the audio track on the fly, the sink should
207 // get data callback when the track is connected to the capturer but not when
208 // the track is disconnected from the capturer.
209 TEST_F(WebRtcLocalAudioTrackTest, ConnectAndDisconnectOneSink) {
210 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
211 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
212 scoped_ptr<WebRtcLocalAudioTrack> track(
213 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL));
215 EXPECT_TRUE(track->GetAudioAdapter()->enabled());
217 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
218 base::WaitableEvent event(false, false);
219 EXPECT_CALL(*sink, FormatIsSet());
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());
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
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.get(), capturer_, NULL));
249 EXPECT_TRUE(track->GetAudioAdapter()->enabled());
250 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(false));
251 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
252 const media::AudioParameters params = capturer_->source_audio_parameters();
253 base::WaitableEvent event(false, false);
254 EXPECT_CALL(*sink, FormatIsSet()).Times(1);
256 CaptureData(0, 0, 0, _, false)).Times(0);
257 EXPECT_EQ(sink->audio_params().frames_per_buffer(),
258 params.sample_rate() / 100);
259 track->AddSink(sink.get());
260 EXPECT_FALSE(event.TimedWait(TestTimeouts::tiny_timeout()));
263 EXPECT_CALL(*sink, CaptureData(0, 0, 0, _, false)).Times(AtLeast(1))
264 .WillRepeatedly(SignalEvent(&event));
265 EXPECT_TRUE(track->GetAudioAdapter()->set_enabled(true));
266 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
267 track->RemoveSink(sink.get());
269 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return());
274 // Create multiple audio tracks and enable/disable them, verify that the audio
275 // callbacks appear/disappear.
276 // Flaky due to a data race, see http://crbug.com/295418
277 TEST_F(WebRtcLocalAudioTrackTest, DISABLED_MultipleAudioTracks) {
278 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1(
279 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
280 scoped_ptr<WebRtcLocalAudioTrack> track_1(
281 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL));
283 EXPECT_TRUE(track_1->GetAudioAdapter()->enabled());
284 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink());
285 const media::AudioParameters params = capturer_->source_audio_parameters();
286 base::WaitableEvent event_1(false, false);
287 EXPECT_CALL(*sink_1, FormatIsSet()).WillOnce(Return());
289 CaptureData(0, 0, 0, _, false)).Times(AtLeast(1))
290 .WillRepeatedly(SignalEvent(&event_1));
291 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(),
292 params.sample_rate() / 100);
293 track_1->AddSink(sink_1.get());
294 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout()));
296 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2(
297 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
298 scoped_ptr<WebRtcLocalAudioTrack> track_2(
299 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL));
301 EXPECT_TRUE(track_2->GetAudioAdapter()->enabled());
303 // Verify both |sink_1| and |sink_2| get data.
305 base::WaitableEvent event_2(false, false);
307 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink());
308 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(Return());
309 EXPECT_CALL(*sink_1, CaptureData(0, 0, 0, _, false)).Times(AtLeast(1))
310 .WillRepeatedly(SignalEvent(&event_1));
311 EXPECT_EQ(sink_1->audio_params().frames_per_buffer(),
312 params.sample_rate() / 100);
313 EXPECT_CALL(*sink_2, CaptureData(0, 0, 0, _, false)).Times(AtLeast(1))
314 .WillRepeatedly(SignalEvent(&event_2));
315 EXPECT_EQ(sink_2->audio_params().frames_per_buffer(),
316 params.sample_rate() / 100);
317 track_2->AddSink(sink_2.get());
318 EXPECT_TRUE(event_1.TimedWait(TestTimeouts::tiny_timeout()));
319 EXPECT_TRUE(event_2.TimedWait(TestTimeouts::tiny_timeout()));
321 track_1->RemoveSink(sink_1.get());
325 EXPECT_CALL(*capturer_source_.get(), OnStop()).WillOnce(Return());
326 track_2->RemoveSink(sink_2.get());
332 // Start one track and verify the capturer is correctly starting its source.
333 // And it should be fine to not to call Stop() explicitly.
334 TEST_F(WebRtcLocalAudioTrackTest, StartOneAudioTrack) {
335 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
336 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
337 scoped_ptr<WebRtcLocalAudioTrack> track(
338 new WebRtcLocalAudioTrack(adapter.get(), capturer_, NULL));
341 // When the track goes away, it will automatically stop the
342 // |capturer_source_|.
343 EXPECT_CALL(*capturer_source_.get(), OnStop());
347 // Start two tracks and verify the capturer is correctly starting its source.
348 // When the last track connected to the capturer is stopped, the source is
350 TEST_F(WebRtcLocalAudioTrackTest, StartTwoAudioTracks) {
351 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter1(
352 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
353 scoped_ptr<WebRtcLocalAudioTrack> track1(
354 new WebRtcLocalAudioTrack(adapter1.get(), capturer_, NULL));
357 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter2(
358 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
359 scoped_ptr<WebRtcLocalAudioTrack> track2(
360 new WebRtcLocalAudioTrack(adapter2.get(), capturer_, NULL));
364 // When the last track is stopped, it will automatically stop the
365 // |capturer_source_|.
366 EXPECT_CALL(*capturer_source_.get(), OnStop());
370 // Start/Stop tracks and verify the capturer is correctly starting/stopping
372 TEST_F(WebRtcLocalAudioTrackTest, StartAndStopAudioTracks) {
373 base::WaitableEvent event(false, false);
374 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1(
375 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
376 scoped_ptr<WebRtcLocalAudioTrack> track_1(
377 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL));
380 // Verify the data flow by connecting the sink to |track_1|.
381 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
383 EXPECT_CALL(*sink, FormatIsSet()).WillOnce(SignalEvent(&event));
384 EXPECT_CALL(*sink, CaptureData(_, 0, 0, _, false))
385 .Times(AnyNumber()).WillRepeatedly(Return());
386 track_1->AddSink(sink.get());
387 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
389 // Start the second audio track will not start the |capturer_source_|
390 // since it has been started.
391 EXPECT_CALL(*capturer_source_.get(), OnStart()).Times(0);
392 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2(
393 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
394 scoped_ptr<WebRtcLocalAudioTrack> track_2(
395 new WebRtcLocalAudioTrack(adapter_2.get(), capturer_, NULL));
398 // Stop the capturer will clear up the track lists in the capturer.
399 EXPECT_CALL(*capturer_source_.get(), OnStop());
402 // Adding a new track to the capturer.
403 track_2->AddSink(sink.get());
404 EXPECT_CALL(*sink, FormatIsSet()).Times(0);
406 // Stop the capturer again will not trigger stopping the source of the
409 EXPECT_CALL(*capturer_source_.get(), OnStop()).Times(0);
413 // Contains data races reported by tsan: crbug.com/404133
414 #if defined(THREAD_SANITIZER)
415 #define DISABLE_ON_TSAN(function) DISABLED_##function
417 #define DISABLE_ON_TSAN(function) function
420 // Create a new capturer with new source, connect it to a new audio track.
421 TEST_F(WebRtcLocalAudioTrackTest,
422 DISABLE_ON_TSAN(ConnectTracksToDifferentCapturers)) {
423 // Setup the first audio track and start it.
424 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_1(
425 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
426 scoped_ptr<WebRtcLocalAudioTrack> track_1(
427 new WebRtcLocalAudioTrack(adapter_1.get(), capturer_, NULL));
430 // Verify the data flow by connecting the |sink_1| to |track_1|.
431 scoped_ptr<MockMediaStreamAudioSink> sink_1(new MockMediaStreamAudioSink());
432 EXPECT_CALL(*sink_1.get(), CaptureData(0, 0, 0, _, false))
433 .Times(AnyNumber()).WillRepeatedly(Return());
434 EXPECT_CALL(*sink_1.get(), FormatIsSet()).Times(AnyNumber());
435 track_1->AddSink(sink_1.get());
437 // Create a new capturer with new source with different audio format.
438 MockMediaConstraintFactory constraint_factory;
439 StreamDeviceInfo device(MEDIA_DEVICE_AUDIO_CAPTURE,
440 std::string(), std::string());
441 scoped_refptr<WebRtcAudioCapturer> new_capturer(
442 WebRtcAudioCapturer::CreateCapturer(
443 -1, device, constraint_factory.CreateWebMediaConstraints(), NULL,
445 scoped_refptr<MockCapturerSource> new_source(
446 new MockCapturerSource(new_capturer.get()));
447 EXPECT_CALL(*new_source.get(), OnInitialize(_, new_capturer.get(), -1));
448 EXPECT_CALL(*new_source.get(), SetAutomaticGainControl(true));
449 EXPECT_CALL(*new_source.get(), OnStart());
451 media::AudioParameters new_param(
452 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
453 media::CHANNEL_LAYOUT_MONO, 44100, 16, 441);
454 new_capturer->SetCapturerSourceForTesting(new_source, new_param);
456 // Setup the second audio track, connect it to the new capturer and start it.
457 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter_2(
458 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
459 scoped_ptr<WebRtcLocalAudioTrack> track_2(
460 new WebRtcLocalAudioTrack(adapter_2.get(), new_capturer, NULL));
463 // Verify the data flow by connecting the |sink_2| to |track_2|.
464 scoped_ptr<MockMediaStreamAudioSink> sink_2(new MockMediaStreamAudioSink());
465 base::WaitableEvent event(false, false);
466 EXPECT_CALL(*sink_2, CaptureData(0, 0, 0, _, false))
467 .Times(AnyNumber()).WillRepeatedly(Return());
468 EXPECT_CALL(*sink_2, FormatIsSet()).WillOnce(SignalEvent(&event));
469 track_2->AddSink(sink_2.get());
470 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
472 // Stopping the new source will stop the second track.
474 EXPECT_CALL(*new_source.get(), OnStop())
475 .Times(1).WillOnce(SignalEvent(&event));
476 new_capturer->Stop();
477 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
479 // Stop the capturer of the first audio track.
480 EXPECT_CALL(*capturer_source_.get(), OnStop());
484 // Make sure a audio track can deliver packets with a buffer size smaller than
485 // 10ms when it is not connected with a peer connection.
486 TEST_F(WebRtcLocalAudioTrackTest, TrackWorkWithSmallBufferSize) {
487 // Setup a capturer which works with a buffer size smaller than 10ms.
488 media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
489 media::CHANNEL_LAYOUT_STEREO, 48000, 16, 128);
491 // Create a capturer with new source which works with the format above.
492 MockMediaConstraintFactory factory;
493 factory.DisableDefaultAudioConstraints();
494 scoped_refptr<WebRtcAudioCapturer> capturer(
495 WebRtcAudioCapturer::CreateCapturer(
497 StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE,
498 "", "", params.sample_rate(),
499 params.channel_layout(),
500 params.frames_per_buffer()),
501 factory.CreateWebMediaConstraints(),
503 scoped_refptr<MockCapturerSource> source(
504 new MockCapturerSource(capturer.get()));
505 EXPECT_CALL(*source.get(), OnInitialize(_, capturer.get(), -1));
506 EXPECT_CALL(*source.get(), SetAutomaticGainControl(true));
507 EXPECT_CALL(*source.get(), OnStart());
508 capturer->SetCapturerSourceForTesting(source, params);
510 // Setup a audio track, connect it to the capturer and start it.
511 scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
512 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
513 scoped_ptr<WebRtcLocalAudioTrack> track(
514 new WebRtcLocalAudioTrack(adapter.get(), capturer, NULL));
517 // Verify the data flow by connecting the |sink| to |track|.
518 scoped_ptr<MockMediaStreamAudioSink> sink(new MockMediaStreamAudioSink());
519 base::WaitableEvent event(false, false);
520 EXPECT_CALL(*sink, FormatIsSet()).Times(1);
521 // Verify the sinks are getting the packets with an expecting buffer size.
522 #if defined(OS_ANDROID)
523 const int expected_buffer_size = params.sample_rate() / 100;
525 const int expected_buffer_size = params.frames_per_buffer();
527 EXPECT_CALL(*sink, CaptureData(
529 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event));
530 track->AddSink(sink.get());
531 EXPECT_TRUE(event.TimedWait(TestTimeouts::tiny_timeout()));
532 EXPECT_EQ(expected_buffer_size, sink->audio_params().frames_per_buffer());
534 // Stopping the new source will stop the second track.
535 EXPECT_CALL(*source.get(), OnStop()).Times(1);
538 // Even though this test don't use |capturer_source_| it will be stopped
539 // during teardown of the test harness.
540 EXPECT_CALL(*capturer_source_.get(), OnStop());
543 } // namespace content