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 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 void Start() override
{
107 audio_thread_
.reset(new FakeAudioThread(capturer_
, params_
));
108 audio_thread_
->Start();
111 void Stop() override
{
112 audio_thread_
->Stop();
113 audio_thread_
.reset();
118 ~MockCapturerSource() override
{}
121 scoped_ptr
<FakeAudioThread
> audio_thread_
;
122 WebRtcAudioCapturer
* capturer_
;
123 media::AudioParameters params_
;
126 class MockMediaStreamAudioSink
: public MediaStreamAudioSink
{
128 MockMediaStreamAudioSink() {}
129 ~MockMediaStreamAudioSink() {}
130 void OnData(const media::AudioBus
& audio_bus
,
131 base::TimeTicks estimated_capture_time
) override
{
132 EXPECT_EQ(params_
.channels(), audio_bus
.channels());
133 EXPECT_EQ(params_
.frames_per_buffer(), audio_bus
.frames());
134 EXPECT_FALSE(estimated_capture_time
.is_null());
137 MOCK_METHOD0(CaptureData
, void());
138 void OnSetFormat(const media::AudioParameters
& params
) {
142 MOCK_METHOD0(FormatIsSet
, void());
144 const media::AudioParameters
& audio_params() const { return params_
; }
147 media::AudioParameters params_
;
152 class WebRtcLocalAudioTrackTest
: public ::testing::Test
{
154 void SetUp() override
{
155 params_
.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
156 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 480);
157 MockMediaConstraintFactory constraint_factory
;
158 blink_source_
.initialize("dummy", blink::WebMediaStreamSource::TypeAudio
,
160 false /* remote */, true /* readonly */);
161 MediaStreamAudioSource
* audio_source
= new MediaStreamAudioSource();
162 blink_source_
.setExtraData(audio_source
);
164 StreamDeviceInfo
device(MEDIA_DEVICE_AUDIO_CAPTURE
,
165 std::string(), std::string());
166 capturer_
= WebRtcAudioCapturer::CreateCapturer(
167 -1, device
, constraint_factory
.CreateWebMediaConstraints(), NULL
,
169 audio_source
->SetAudioCapturer(capturer_
.get());
170 capturer_source_
= new MockCapturerSource(capturer_
.get());
171 EXPECT_CALL(*capturer_source_
.get(), OnInitialize(_
, capturer_
.get(), -1))
173 EXPECT_CALL(*capturer_source_
.get(), SetAutomaticGainControl(true));
174 EXPECT_CALL(*capturer_source_
.get(), OnStart());
175 capturer_
->SetCapturerSource(capturer_source_
, params_
);
178 void TearDown() override
{
179 blink_source_
.reset();
180 blink::WebHeap::collectAllGarbageForTesting();
183 media::AudioParameters params_
;
184 blink::WebMediaStreamSource blink_source_
;
185 scoped_refptr
<MockCapturerSource
> capturer_source_
;
186 scoped_refptr
<WebRtcAudioCapturer
> capturer_
;
189 // Creates a capturer and audio track, fakes its audio thread, and
190 // connect/disconnect the sink to the audio track on the fly, the sink should
191 // get data callback when the track is connected to the capturer but not when
192 // the track is disconnected from the capturer.
193 TEST_F(WebRtcLocalAudioTrackTest
, ConnectAndDisconnectOneSink
) {
194 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
195 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
196 scoped_ptr
<WebRtcLocalAudioTrack
> track(
197 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
199 EXPECT_TRUE(track
->GetAudioAdapter()->enabled());
201 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
202 base::WaitableEvent
event(false, false);
203 EXPECT_CALL(*sink
, FormatIsSet());
205 CaptureData()).Times(AtLeast(1))
206 .WillRepeatedly(SignalEvent(&event
));
207 track
->AddSink(sink
.get());
208 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
209 track
->RemoveSink(sink
.get());
211 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
215 // The same setup as ConnectAndDisconnectOneSink, but enable and disable the
216 // audio track on the fly. When the audio track is disabled, there is no data
217 // callback to the sink; when the audio track is enabled, there comes data
219 // TODO(xians): Enable this test after resolving the racing issue that TSAN
220 // reports on MediaStreamTrack::enabled();
221 TEST_F(WebRtcLocalAudioTrackTest
, DISABLED_DisableEnableAudioTrack
) {
222 EXPECT_CALL(*capturer_source_
.get(), SetAutomaticGainControl(true));
223 EXPECT_CALL(*capturer_source_
.get(), OnStart());
224 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
225 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
226 scoped_ptr
<WebRtcLocalAudioTrack
> track(
227 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
229 EXPECT_TRUE(track
->GetAudioAdapter()->enabled());
230 EXPECT_TRUE(track
->GetAudioAdapter()->set_enabled(false));
231 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
232 const media::AudioParameters params
= capturer_
->source_audio_parameters();
233 base::WaitableEvent
event(false, false);
234 EXPECT_CALL(*sink
, FormatIsSet()).Times(1);
235 EXPECT_CALL(*sink
, CaptureData()).Times(0);
236 EXPECT_EQ(sink
->audio_params().frames_per_buffer(),
237 params
.sample_rate() / 100);
238 track
->AddSink(sink
.get());
239 EXPECT_FALSE(event
.TimedWait(TestTimeouts::tiny_timeout()));
242 EXPECT_CALL(*sink
, CaptureData()).Times(AtLeast(1))
243 .WillRepeatedly(SignalEvent(&event
));
244 EXPECT_TRUE(track
->GetAudioAdapter()->set_enabled(true));
245 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
246 track
->RemoveSink(sink
.get());
248 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
253 // Create multiple audio tracks and enable/disable them, verify that the audio
254 // callbacks appear/disappear.
255 // Flaky due to a data race, see http://crbug.com/295418
256 TEST_F(WebRtcLocalAudioTrackTest
, DISABLED_MultipleAudioTracks
) {
257 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
258 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
259 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
260 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
262 EXPECT_TRUE(track_1
->GetAudioAdapter()->enabled());
263 scoped_ptr
<MockMediaStreamAudioSink
> sink_1(new MockMediaStreamAudioSink());
264 const media::AudioParameters params
= capturer_
->source_audio_parameters();
265 base::WaitableEvent
event_1(false, false);
266 EXPECT_CALL(*sink_1
, FormatIsSet()).WillOnce(Return());
267 EXPECT_CALL(*sink_1
, CaptureData()).Times(AtLeast(1))
268 .WillRepeatedly(SignalEvent(&event_1
));
269 EXPECT_EQ(sink_1
->audio_params().frames_per_buffer(),
270 params
.sample_rate() / 100);
271 track_1
->AddSink(sink_1
.get());
272 EXPECT_TRUE(event_1
.TimedWait(TestTimeouts::tiny_timeout()));
274 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
275 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
276 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
277 new WebRtcLocalAudioTrack(adapter_2
.get(), capturer_
, NULL
));
279 EXPECT_TRUE(track_2
->GetAudioAdapter()->enabled());
281 // Verify both |sink_1| and |sink_2| get data.
283 base::WaitableEvent
event_2(false, false);
285 scoped_ptr
<MockMediaStreamAudioSink
> sink_2(new MockMediaStreamAudioSink());
286 EXPECT_CALL(*sink_2
, FormatIsSet()).WillOnce(Return());
287 EXPECT_CALL(*sink_1
, CaptureData()).Times(AtLeast(1))
288 .WillRepeatedly(SignalEvent(&event_1
));
289 EXPECT_EQ(sink_1
->audio_params().frames_per_buffer(),
290 params
.sample_rate() / 100);
291 EXPECT_CALL(*sink_2
, CaptureData()).Times(AtLeast(1))
292 .WillRepeatedly(SignalEvent(&event_2
));
293 EXPECT_EQ(sink_2
->audio_params().frames_per_buffer(),
294 params
.sample_rate() / 100);
295 track_2
->AddSink(sink_2
.get());
296 EXPECT_TRUE(event_1
.TimedWait(TestTimeouts::tiny_timeout()));
297 EXPECT_TRUE(event_2
.TimedWait(TestTimeouts::tiny_timeout()));
299 track_1
->RemoveSink(sink_1
.get());
303 EXPECT_CALL(*capturer_source_
.get(), OnStop()).WillOnce(Return());
304 track_2
->RemoveSink(sink_2
.get());
310 // Start one track and verify the capturer is correctly starting its source.
311 // And it should be fine to not to call Stop() explicitly.
312 TEST_F(WebRtcLocalAudioTrackTest
, StartOneAudioTrack
) {
313 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
314 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
315 scoped_ptr
<WebRtcLocalAudioTrack
> track(
316 new WebRtcLocalAudioTrack(adapter
.get(), capturer_
, NULL
));
319 // When the track goes away, it will automatically stop the
320 // |capturer_source_|.
321 EXPECT_CALL(*capturer_source_
.get(), OnStop());
325 // Start two tracks and verify the capturer is correctly starting its source.
326 // When the last track connected to the capturer is stopped, the source is
328 TEST_F(WebRtcLocalAudioTrackTest
, StartTwoAudioTracks
) {
329 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter1(
330 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
331 scoped_ptr
<WebRtcLocalAudioTrack
> track1(
332 new WebRtcLocalAudioTrack(adapter1
.get(), capturer_
, NULL
));
335 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter2(
336 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
337 scoped_ptr
<WebRtcLocalAudioTrack
> track2(
338 new WebRtcLocalAudioTrack(adapter2
.get(), capturer_
, NULL
));
342 // When the last track is stopped, it will automatically stop the
343 // |capturer_source_|.
344 EXPECT_CALL(*capturer_source_
.get(), OnStop());
348 // Start/Stop tracks and verify the capturer is correctly starting/stopping
350 TEST_F(WebRtcLocalAudioTrackTest
, StartAndStopAudioTracks
) {
351 base::WaitableEvent
event(false, false);
352 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
353 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
354 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
355 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
358 // Verify the data flow by connecting the sink to |track_1|.
359 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
361 EXPECT_CALL(*sink
, FormatIsSet()).WillOnce(SignalEvent(&event
));
362 EXPECT_CALL(*sink
, CaptureData())
363 .Times(AnyNumber()).WillRepeatedly(Return());
364 track_1
->AddSink(sink
.get());
365 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
367 // Start the second audio track will not start the |capturer_source_|
368 // since it has been started.
369 EXPECT_CALL(*capturer_source_
.get(), OnStart()).Times(0);
370 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
371 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
372 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
373 new WebRtcLocalAudioTrack(adapter_2
.get(), capturer_
, NULL
));
376 // Stop the capturer will clear up the track lists in the capturer.
377 EXPECT_CALL(*capturer_source_
.get(), OnStop());
380 // Adding a new track to the capturer.
381 track_2
->AddSink(sink
.get());
382 EXPECT_CALL(*sink
, FormatIsSet()).Times(0);
384 // Stop the capturer again will not trigger stopping the source of the
387 EXPECT_CALL(*capturer_source_
.get(), OnStop()).Times(0);
391 // Create a new capturer with new source, connect it to a new audio track.
392 TEST_F(WebRtcLocalAudioTrackTest
, ConnectTracksToDifferentCapturers
) {
393 // Setup the first audio track and start it.
394 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_1(
395 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
396 scoped_ptr
<WebRtcLocalAudioTrack
> track_1(
397 new WebRtcLocalAudioTrack(adapter_1
.get(), capturer_
, NULL
));
400 // Verify the data flow by connecting the |sink_1| to |track_1|.
401 scoped_ptr
<MockMediaStreamAudioSink
> sink_1(new MockMediaStreamAudioSink());
402 EXPECT_CALL(*sink_1
.get(), CaptureData())
403 .Times(AnyNumber()).WillRepeatedly(Return());
404 EXPECT_CALL(*sink_1
.get(), FormatIsSet()).Times(AnyNumber());
405 track_1
->AddSink(sink_1
.get());
407 // Create a new capturer with new source with different audio format.
408 MockMediaConstraintFactory constraint_factory
;
409 StreamDeviceInfo
device(MEDIA_DEVICE_AUDIO_CAPTURE
,
410 std::string(), std::string());
411 scoped_refptr
<WebRtcAudioCapturer
> new_capturer(
412 WebRtcAudioCapturer::CreateCapturer(
413 -1, device
, constraint_factory
.CreateWebMediaConstraints(), NULL
,
415 scoped_refptr
<MockCapturerSource
> new_source(
416 new MockCapturerSource(new_capturer
.get()));
417 EXPECT_CALL(*new_source
.get(), OnInitialize(_
, new_capturer
.get(), -1));
418 EXPECT_CALL(*new_source
.get(), SetAutomaticGainControl(true));
419 EXPECT_CALL(*new_source
.get(), OnStart());
421 media::AudioParameters
new_param(
422 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
423 media::CHANNEL_LAYOUT_MONO
, 44100, 16, 441);
424 new_capturer
->SetCapturerSource(new_source
, new_param
);
426 // Setup the second audio track, connect it to the new capturer and start it.
427 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter_2(
428 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
429 scoped_ptr
<WebRtcLocalAudioTrack
> track_2(
430 new WebRtcLocalAudioTrack(adapter_2
.get(), new_capturer
, NULL
));
433 // Verify the data flow by connecting the |sink_2| to |track_2|.
434 scoped_ptr
<MockMediaStreamAudioSink
> sink_2(new MockMediaStreamAudioSink());
435 base::WaitableEvent
event(false, false);
436 EXPECT_CALL(*sink_2
, CaptureData())
437 .Times(AnyNumber()).WillRepeatedly(Return());
438 EXPECT_CALL(*sink_2
, FormatIsSet()).WillOnce(SignalEvent(&event
));
439 track_2
->AddSink(sink_2
.get());
440 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
442 // Stopping the new source will stop the second track.
444 EXPECT_CALL(*new_source
.get(), OnStop())
445 .Times(1).WillOnce(SignalEvent(&event
));
446 new_capturer
->Stop();
447 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
449 // Stop the capturer of the first audio track.
450 EXPECT_CALL(*capturer_source_
.get(), OnStop());
454 // Make sure a audio track can deliver packets with a buffer size smaller than
455 // 10ms when it is not connected with a peer connection.
456 TEST_F(WebRtcLocalAudioTrackTest
, TrackWorkWithSmallBufferSize
) {
457 // Setup a capturer which works with a buffer size smaller than 10ms.
458 media::AudioParameters
params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
459 media::CHANNEL_LAYOUT_STEREO
, 48000, 16, 128);
461 // Create a capturer with new source which works with the format above.
462 MockMediaConstraintFactory factory
;
463 factory
.DisableDefaultAudioConstraints();
464 scoped_refptr
<WebRtcAudioCapturer
> capturer(
465 WebRtcAudioCapturer::CreateCapturer(
466 -1, StreamDeviceInfo(MEDIA_DEVICE_AUDIO_CAPTURE
, "", "",
467 params
.sample_rate(), params
.channel_layout(),
468 params
.frames_per_buffer()),
469 factory
.CreateWebMediaConstraints(), NULL
, NULL
));
470 scoped_refptr
<MockCapturerSource
> source(
471 new MockCapturerSource(capturer
.get()));
472 EXPECT_CALL(*source
.get(), OnInitialize(_
, capturer
.get(), -1));
473 EXPECT_CALL(*source
.get(), SetAutomaticGainControl(true));
474 EXPECT_CALL(*source
.get(), OnStart());
475 capturer
->SetCapturerSource(source
, params
);
477 // Setup a audio track, connect it to the capturer and start it.
478 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
479 WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL
));
480 scoped_ptr
<WebRtcLocalAudioTrack
> track(
481 new WebRtcLocalAudioTrack(adapter
.get(), capturer
, NULL
));
484 // Verify the data flow by connecting the |sink| to |track|.
485 scoped_ptr
<MockMediaStreamAudioSink
> sink(new MockMediaStreamAudioSink());
486 base::WaitableEvent
event(false, false);
487 EXPECT_CALL(*sink
, FormatIsSet()).Times(1);
488 // Verify the sinks are getting the packets with an expecting buffer size.
489 #if defined(OS_ANDROID)
490 const int expected_buffer_size
= params
.sample_rate() / 100;
492 const int expected_buffer_size
= params
.frames_per_buffer();
494 EXPECT_CALL(*sink
, CaptureData())
495 .Times(AtLeast(1)).WillRepeatedly(SignalEvent(&event
));
496 track
->AddSink(sink
.get());
497 EXPECT_TRUE(event
.TimedWait(TestTimeouts::tiny_timeout()));
498 EXPECT_EQ(expected_buffer_size
, sink
->audio_params().frames_per_buffer());
500 // Stopping the new source will stop the second track.
501 EXPECT_CALL(*source
.get(), OnStop()).Times(1);
504 // Even though this test don't use |capturer_source_| it will be stopped
505 // during teardown of the test harness.
506 EXPECT_CALL(*capturer_source_
.get(), OnStop());
509 } // namespace content