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/public/renderer/media_stream_audio_sink.h"
8 #include "content/renderer/media/media_stream_audio_source.h"
9 #include "content/renderer/media/mock_media_constraint_factory.h"
10 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
11 #include "content/renderer/media/webrtc_audio_capturer.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 ~FakeAudioThread() override
{ DCHECK(thread_
.is_null()); }
50 // base::PlatformThread::Delegate:
51 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::ThreadPriority::REALTIME_AUDIO
);
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 class MockMediaStreamAudioSink
: public MediaStreamAudioSink
{
127 MockMediaStreamAudioSink() {}
128 ~MockMediaStreamAudioSink() {}
129 void OnData(const media::AudioBus
& audio_bus
,
130 base::TimeTicks estimated_capture_time
) override
{
131 EXPECT_EQ(params_
.channels(), audio_bus
.channels());
132 EXPECT_EQ(params_
.frames_per_buffer(), audio_bus
.frames());
133 EXPECT_FALSE(estimated_capture_time
.is_null());
136 MOCK_METHOD0(CaptureData
, void());
137 void OnSetFormat(const media::AudioParameters
& params
) {
141 MOCK_METHOD0(FormatIsSet
, void());
143 const media::AudioParameters
& audio_params() const { return params_
; }
146 media::AudioParameters params_
;
151 class WebRtcLocalAudioTrackTest
: public ::testing::Test
{
153 void SetUp() override
{
154 params_
.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
155 media::CHANNEL_LAYOUT_STEREO
, 2, 48000, 16, 480);
156 MockMediaConstraintFactory constraint_factory
;
157 blink_source_
.initialize("dummy", blink::WebMediaStreamSource::TypeAudio
,
159 false /* remote */, true /* readonly */);
160 MediaStreamAudioSource
* audio_source
= new MediaStreamAudioSource();
161 blink_source_
.setExtraData(audio_source
);
163 StreamDeviceInfo
device(MEDIA_DEVICE_AUDIO_CAPTURE
,
164 std::string(), std::string());
165 capturer_
= WebRtcAudioCapturer::CreateCapturer(
166 -1, device
, constraint_factory
.CreateWebMediaConstraints(), NULL
,
168 audio_source
->SetAudioCapturer(capturer_
.get());
169 capturer_source_
= new MockCapturerSource(capturer_
.get());
170 EXPECT_CALL(*capturer_source_
.get(), OnInitialize(_
, capturer_
.get(), -1))
172 EXPECT_CALL(*capturer_source_
.get(), SetAutomaticGainControl(true));
173 EXPECT_CALL(*capturer_source_
.get(), OnStart());
174 capturer_
->SetCapturerSource(capturer_source_
, params_
);
177 void TearDown() override
{
178 blink_source_
.reset();
179 blink::WebHeap::collectAllGarbageForTesting();
182 media::AudioParameters params_
;
183 blink::WebMediaStreamSource blink_source_
;
184 scoped_refptr
<MockCapturerSource
> capturer_source_
;
185 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
188 // 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
190 // get data callback when the track is connected to the capturer but not when
191 // the track is disconnected from the capturer.
192 TEST_F(WebRtcLocalAudioTrackTest
, ConnectAndDisconnectOneSink
) {
193 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
194 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
195 scoped_ptr
<WebRtcLocalAudioTrack
> track(
196 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
198 EXPECT_TRUE(track
->GetAudioAdapter()->enabled());
200 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
201 base::WaitableEvent
event(false, false);
202 EXPECT_CALL(*sink
, FormatIsSet());
204 CaptureData()).Times(AtLeast(1))
205 .WillRepeatedly(SignalEvent(&event
));
206 track
->AddSink(sink
.get());
207 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
208 track
->RemoveSink(sink
.get());
210 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
214 // The same setup as ConnectAndDisconnectOneSink, but enable and disable the
215 // audio track on the fly. When the audio track is disabled, there is no data
216 // callback to the sink; when the audio track is enabled, there comes data
218 // TODO(xians): Enable this test after resolving the racing issue that TSAN
219 // reports on MediaStreamTrack::enabled();
220 TEST_F(WebRtcLocalAudioTrackTest
, DISABLED_DisableEnableAudioTrack
) {
221 EXPECT_CALL(*capturer_source_
.get(), SetAutomaticGainControl(true));
222 EXPECT_CALL(*capturer_source_
.get(), OnStart());
223 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
224 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
225 scoped_ptr
<WebRtcLocalAudioTrack
> track(
226 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
228 EXPECT_TRUE(track
->GetAudioAdapter()->enabled());
229 EXPECT_TRUE(track
->GetAudioAdapter()->set_enabled(false));
230 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
231 const media::AudioParameters params
= capturer_
->source_audio_parameters();
232 base::WaitableEvent
event(false, false);
233 EXPECT_CALL(*sink
, FormatIsSet()).Times(1);
234 EXPECT_CALL(*sink
, CaptureData()).Times(0);
235 EXPECT_EQ(sink
->audio_params().frames_per_buffer(),
236 params
.sample_rate() / 100);
237 track
->AddSink(sink
.get());
238 EXPECT_FALSE(event
.TimedWait(TestTimeouts::tiny_timeout()));
241 EXPECT_CALL(*sink
, CaptureData()).Times(AtLeast(1))
242 .WillRepeatedly(SignalEvent(&event
));
243 EXPECT_TRUE(track
->GetAudioAdapter()->set_enabled(true));
244 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
245 track
->RemoveSink(sink
.get());
247 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
252 // Create multiple audio tracks and enable/disable them, verify that the audio
253 // callbacks appear/disappear.
254 // Flaky due to a data race, see http://crbug.com/295418
255 TEST_F(WebRtcLocalAudioTrackTest
, DISABLED_MultipleAudioTracks
) {
256 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
257 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
258 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
259 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
261 EXPECT_TRUE(track_1
->GetAudioAdapter()->enabled());
262 scoped_ptr
<MockMediaStreamAudioSink
> sink_1(new MockMediaStreamAudioSink());
263 const media::AudioParameters params
= capturer_
->source_audio_parameters();
264 base::WaitableEvent
event_1(false, false);
265 EXPECT_CALL(*sink_1
, FormatIsSet()).WillOnce(Return());
266 EXPECT_CALL(*sink_1
, CaptureData()).Times(AtLeast(1))
267 .WillRepeatedly(SignalEvent(&event_1
));
268 EXPECT_EQ(sink_1
->audio_params().frames_per_buffer(),
269 params
.sample_rate() / 100);
270 track_1
->AddSink(sink_1
.get());
271 EXPECT_TRUE(event_1
.TimedWait(TestTimeouts::tiny_timeout()));
273 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
274 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
275 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
276 new WebRtcLocalAudioTrack(adapter_2
.get(), capturer_
, NULL
));
278 EXPECT_TRUE(track_2
->GetAudioAdapter()->enabled());
280 // Verify both |sink_1| and |sink_2| get data.
282 base::WaitableEvent
event_2(false, false);
284 scoped_ptr
<MockMediaStreamAudioSink
> sink_2(new MockMediaStreamAudioSink());
285 EXPECT_CALL(*sink_2
, FormatIsSet()).WillOnce(Return());
286 EXPECT_CALL(*sink_1
, CaptureData()).Times(AtLeast(1))
287 .WillRepeatedly(SignalEvent(&event_1
));
288 EXPECT_EQ(sink_1
->audio_params().frames_per_buffer(),
289 params
.sample_rate() / 100);
290 EXPECT_CALL(*sink_2
, CaptureData()).Times(AtLeast(1))
291 .WillRepeatedly(SignalEvent(&event_2
));
292 EXPECT_EQ(sink_2
->audio_params().frames_per_buffer(),
293 params
.sample_rate() / 100);
294 track_2
->AddSink(sink_2
.get());
295 EXPECT_TRUE(event_1
.TimedWait(TestTimeouts::tiny_timeout()));
296 EXPECT_TRUE(event_2
.TimedWait(TestTimeouts::tiny_timeout()));
298 track_1
->RemoveSink(sink_1
.get());
302 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
303 track_2
->RemoveSink(sink_2
.get());
309 // Start one track and verify the capturer is correctly starting its source.
310 // And it should be fine to not to call Stop() explicitly.
311 TEST_F(WebRtcLocalAudioTrackTest
, StartOneAudioTrack
) {
312 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
313 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
314 scoped_ptr
<WebRtcLocalAudioTrack
> track(
315 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
318 // When the track goes away, it will automatically stop the
319 // |capturer_source_|.
320 EXPECT_CALL(*capturer_source_
.get(), OnStop());
324 // Start two tracks and verify the capturer is correctly starting its source.
325 // When the last track connected to the capturer is stopped, the source is
327 TEST_F(WebRtcLocalAudioTrackTest
, StartTwoAudioTracks
) {
328 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter1(
329 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
330 scoped_ptr
<WebRtcLocalAudioTrack
> track1(
331 new WebRtcLocalAudioTrack(adapter1
.get(), capturer_
, NULL
));
334 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter2(
335 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
336 scoped_ptr
<WebRtcLocalAudioTrack
> track2(
337 new WebRtcLocalAudioTrack(adapter2
.get(), capturer_
, NULL
));
341 // When the last track is stopped, it will automatically stop the
342 // |capturer_source_|.
343 EXPECT_CALL(*capturer_source_
.get(), OnStop());
347 // Start/Stop tracks and verify the capturer is correctly starting/stopping
349 TEST_F(WebRtcLocalAudioTrackTest
, StartAndStopAudioTracks
) {
350 base::WaitableEvent
event(false, false);
351 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
352 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
353 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
354 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
357 // Verify the data flow by connecting the sink to |track_1|.
358 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
360 EXPECT_CALL(*sink
, FormatIsSet()).WillOnce(SignalEvent(&event
));
361 EXPECT_CALL(*sink
, CaptureData())
362 .Times(AnyNumber()).WillRepeatedly(Return());
363 track_1
->AddSink(sink
.get());
364 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
366 // Start the second audio track will not start the |capturer_source_|
367 // since it has been started.
368 EXPECT_CALL(*capturer_source_
.get(), OnStart()).Times(0);
369 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
370 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
371 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
372 new WebRtcLocalAudioTrack(adapter_2
.get(), capturer_
, NULL
));
375 // Stop the capturer will clear up the track lists in the capturer.
376 EXPECT_CALL(*capturer_source_
.get(), OnStop());
379 // Adding a new track to the capturer.
380 track_2
->AddSink(sink
.get());
381 EXPECT_CALL(*sink
, FormatIsSet()).Times(0);
383 // Stop the capturer again will not trigger stopping the source of the
386 EXPECT_CALL(*capturer_source_
.get(), OnStop()).Times(0);
390 // Contains data races reported by tsan: crbug.com/404133
391 #if defined(THREAD_SANITIZER)
392 #define DISABLE_ON_TSAN(function) DISABLED_##function
394 #define DISABLE_ON_TSAN(function) function
397 // Create a new capturer with new source, connect it to a new audio track.
398 TEST_F(WebRtcLocalAudioTrackTest
,
399 DISABLE_ON_TSAN(ConnectTracksToDifferentCapturers
)) {
400 // Setup the first audio track and start it.
401 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
402 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
403 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
404 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
407 // Verify the data flow by connecting the |sink_1| to |track_1|.
408 scoped_ptr
<MockMediaStreamAudioSink
> sink_1(new MockMediaStreamAudioSink());
409 EXPECT_CALL(*sink_1
.get(), CaptureData())
410 .Times(AnyNumber()).WillRepeatedly(Return());
411 EXPECT_CALL(*sink_1
.get(), FormatIsSet()).Times(AnyNumber());
412 track_1
->AddSink(sink_1
.get());
414 // Create a new capturer with new source with different audio format.
415 MockMediaConstraintFactory constraint_factory
;
416 StreamDeviceInfo
device(MEDIA_DEVICE_AUDIO_CAPTURE
,
417 std::string(), std::string());
418 scoped_refptr
<WebRtcAudioCapturer
> new_capturer(
419 WebRtcAudioCapturer::CreateCapturer(
420 -1, device
, constraint_factory
.CreateWebMediaConstraints(), NULL
,
422 scoped_refptr
<MockCapturerSource
> new_source(
423 new MockCapturerSource(new_capturer
.get()));
424 EXPECT_CALL(*new_source
.get(), OnInitialize(_
, new_capturer
.get(), -1));
425 EXPECT_CALL(*new_source
.get(), SetAutomaticGainControl(true));
426 EXPECT_CALL(*new_source
.get(), OnStart());
428 media::AudioParameters
new_param(
429 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
430 media::CHANNEL_LAYOUT_MONO
, 44100, 16, 441);
431 new_capturer
->SetCapturerSource(new_source
, new_param
);
433 // Setup the second audio track, connect it to the new capturer and start it.
434 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
435 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
436 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
437 new WebRtcLocalAudioTrack(adapter_2
.get(), new_capturer
, NULL
));
440 // Verify the data flow by connecting the |sink_2| to |track_2|.
441 scoped_ptr
<MockMediaStreamAudioSink
> sink_2(new MockMediaStreamAudioSink());
442 base::WaitableEvent
event(false, false);
443 EXPECT_CALL(*sink_2
, CaptureData())
444 .Times(AnyNumber()).WillRepeatedly(Return());
445 EXPECT_CALL(*sink_2
, FormatIsSet()).WillOnce(SignalEvent(&event
));
446 track_2
->AddSink(sink_2
.get());
447 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
449 // Stopping the new source will stop the second track.
451 EXPECT_CALL(*new_source
.get(), OnStop())
452 .Times(1).WillOnce(SignalEvent(&event
));
453 new_capturer
->Stop();
454 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
456 // Stop the capturer of the first audio track.
457 EXPECT_CALL(*capturer_source_
.get(), OnStop());
461 // Make sure a audio track can deliver packets with a buffer size smaller than
462 // 10ms when it is not connected with a peer connection.
463 TEST_F(WebRtcLocalAudioTrackTest
, TrackWorkWithSmallBufferSize
) {
464 // Setup a capturer which works with a buffer size smaller than 10ms.
465 media::AudioParameters
params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
466 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 128);
468 // Create a capturer with new source which works with the format above.
469 MockMediaConstraintFactory factory
;
470 factory
.DisableDefaultAudioConstraints();
471 scoped_refptr
<WebRtcAudioCapturer
> capturer(
472 WebRtcAudioCapturer::CreateCapturer(
473 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", "",
474 params
.sample_rate(), params
.channel_layout(),
475 params
.frames_per_buffer()),
476 factory
.CreateWebMediaConstraints(), NULL
, NULL
));
477 scoped_refptr
<MockCapturerSource
> source(
478 new MockCapturerSource(capturer
.get()));
479 EXPECT_CALL(*source
.get(), OnInitialize(_
, capturer
.get(), -1));
480 EXPECT_CALL(*source
.get(), SetAutomaticGainControl(true));
481 EXPECT_CALL(*source
.get(), OnStart());
482 capturer
->SetCapturerSource(source
, params
);
484 // Setup a audio track, connect it to the capturer and start it.
485 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
486 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
487 scoped_ptr
<WebRtcLocalAudioTrack
> track(
488 new WebRtcLocalAudioTrack(adapter
.get(), capturer
, NULL
));
491 // Verify the data flow by connecting the |sink| to |track|.
492 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
493 base::WaitableEvent
event(false, false);
494 EXPECT_CALL(*sink
, FormatIsSet()).Times(1);
495 // Verify the sinks are getting the packets with an expecting buffer size.
496 #if defined(OS_ANDROID)
497 const int expected_buffer_size
= params
.sample_rate() / 100;
499 const int expected_buffer_size
= params
.frames_per_buffer();
501 EXPECT_CALL(*sink
, CaptureData())
502 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event
));
503 track
->AddSink(sink
.get());
504 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
505 EXPECT_EQ(expected_buffer_size
, sink
->audio_params().frames_per_buffer());
507 // Stopping the new source will stop the second track.
508 EXPECT_CALL(*source
.get(), OnStop()).Times(1);
511 // Even though this test don't use |capturer_source_| it will be stopped
512 // during teardown of the test harness.
513 EXPECT_CALL(*capturer_source_
.get(), OnStop());
516 } // namespace content