1 // Copyright (c) 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 "content/browser/media/capture/web_contents_audio_input_stream.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h"
15 #include "content/browser/media/capture/audio_mirroring_manager.h"
16 #include "content/browser/media/capture/web_contents_tracker.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "media/audio/simple_sources.h"
20 #include "media/audio/virtual_audio_input_stream.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
25 using ::testing::Assign
;
26 using ::testing::DoAll
;
27 using ::testing::Invoke
;
28 using ::testing::InvokeWithoutArgs
;
29 using ::testing::NotNull
;
30 using ::testing::SaveArg
;
31 using ::testing::WithArgs
;
33 using media::AudioInputStream
;
34 using media::AudioOutputStream
;
35 using media::AudioParameters
;
36 using media::SineWaveAudioSource
;
37 using media::VirtualAudioInputStream
;
38 using media::VirtualAudioOutputStream
;
44 const int kRenderProcessId
= 123;
45 const int kRenderFrameId
= 456;
46 const int kAnotherRenderProcessId
= 789;
47 const int kAnotherRenderFrameId
= 1;
49 const AudioParameters
& TestAudioParameters() {
50 static const AudioParameters
params(
51 AudioParameters::AUDIO_FAKE
,
52 media::CHANNEL_LAYOUT_STEREO
,
53 AudioParameters::kAudioCDSampleRate
, 16,
54 AudioParameters::kAudioCDSampleRate
/ 100);
58 class MockAudioMirroringManager
: public AudioMirroringManager
{
60 MockAudioMirroringManager() : AudioMirroringManager() {}
61 virtual ~MockAudioMirroringManager() {}
63 MOCK_METHOD1(StartMirroring
, void(MirroringDestination
* destination
));
64 MOCK_METHOD1(StopMirroring
, void(MirroringDestination
* destination
));
67 DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager
);
70 class MockWebContentsTracker
: public WebContentsTracker
{
72 MockWebContentsTracker() : WebContentsTracker(false) {}
75 void(int render_process_id
, int render_frame_id
,
76 const ChangeCallback
& callback
));
77 MOCK_METHOD0(Stop
, void());
80 virtual ~MockWebContentsTracker() {}
82 DISALLOW_COPY_AND_ASSIGN(MockWebContentsTracker
);
85 // A fully-functional VirtualAudioInputStream, but methods are mocked to allow
86 // tests to check how/when they are invoked.
87 class MockVirtualAudioInputStream
: public VirtualAudioInputStream
{
89 explicit MockVirtualAudioInputStream(
90 const scoped_refptr
<base::SingleThreadTaskRunner
>& worker_loop
)
91 : VirtualAudioInputStream(TestAudioParameters(), worker_loop
,
92 VirtualAudioInputStream::AfterCloseCallback()),
93 real_(TestAudioParameters(), worker_loop
,
94 base::Bind(&MockVirtualAudioInputStream::OnRealStreamHasClosed
,
95 base::Unretained(this))),
96 real_stream_is_closed_(false) {
97 // Set default actions of mocked methods to delegate to the concrete
99 ON_CALL(*this, Open())
100 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::Open
));
101 ON_CALL(*this, Start(_
))
102 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::Start
));
103 ON_CALL(*this, Stop())
104 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::Stop
));
105 ON_CALL(*this, Close())
106 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::Close
));
107 ON_CALL(*this, GetMaxVolume())
108 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::GetMaxVolume
));
109 ON_CALL(*this, SetVolume(_
))
110 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::SetVolume
));
111 ON_CALL(*this, GetVolume())
112 .WillByDefault(Invoke(&real_
, &VirtualAudioInputStream::GetVolume
));
113 ON_CALL(*this, SetAutomaticGainControl(_
))
115 Invoke(&real_
, &VirtualAudioInputStream::SetAutomaticGainControl
));
116 ON_CALL(*this, GetAutomaticGainControl())
118 Invoke(&real_
, &VirtualAudioInputStream::GetAutomaticGainControl
));
119 ON_CALL(*this, AddOutputStream(NotNull(), _
))
121 Invoke(&real_
, &VirtualAudioInputStream::AddOutputStream
));
122 ON_CALL(*this, RemoveOutputStream(NotNull(), _
))
124 Invoke(&real_
, &VirtualAudioInputStream::RemoveOutputStream
));
127 ~MockVirtualAudioInputStream() {
128 DCHECK(real_stream_is_closed_
);
131 MOCK_METHOD0(Open
, bool());
132 MOCK_METHOD1(Start
, void(AudioInputStream::AudioInputCallback
*));
133 MOCK_METHOD0(Stop
, void());
134 MOCK_METHOD0(Close
, void());
135 MOCK_METHOD0(GetMaxVolume
, double());
136 MOCK_METHOD1(SetVolume
, void(double));
137 MOCK_METHOD0(GetVolume
, double());
138 MOCK_METHOD1(SetAutomaticGainControl
, bool(bool));
139 MOCK_METHOD0(GetAutomaticGainControl
, bool());
140 MOCK_METHOD2(AddOutputStream
, void(VirtualAudioOutputStream
*,
141 const AudioParameters
&));
142 MOCK_METHOD2(RemoveOutputStream
, void(VirtualAudioOutputStream
*,
143 const AudioParameters
&));
146 void OnRealStreamHasClosed(VirtualAudioInputStream
* stream
) {
147 DCHECK_EQ(&real_
, stream
);
148 DCHECK(!real_stream_is_closed_
);
149 real_stream_is_closed_
= true;
152 VirtualAudioInputStream real_
;
153 bool real_stream_is_closed_
;
155 DISALLOW_COPY_AND_ASSIGN(MockVirtualAudioInputStream
);
158 class MockAudioInputCallback
: public AudioInputStream::AudioInputCallback
{
160 MockAudioInputCallback() {}
163 void(AudioInputStream
* stream
,
164 const media::AudioBus
* src
,
165 uint32 hardware_delay_bytes
,
167 MOCK_METHOD1(OnError
, void(AudioInputStream
* stream
));
170 DISALLOW_COPY_AND_ASSIGN(MockAudioInputCallback
);
175 class WebContentsAudioInputStreamTest
: public testing::Test
{
177 WebContentsAudioInputStreamTest()
178 : thread_bundle_(new TestBrowserThreadBundle(
179 TestBrowserThreadBundle::REAL_IO_THREAD
)),
180 audio_thread_("Audio thread"),
181 mock_mirroring_manager_(new MockAudioMirroringManager()),
182 mock_tracker_(new MockWebContentsTracker()),
186 current_render_process_id_(kRenderProcessId
),
187 current_render_frame_id_(kRenderFrameId
),
188 on_data_event_(false, false) {
189 audio_thread_
.Start();
192 ~WebContentsAudioInputStreamTest() override
{
193 audio_thread_
.Stop();
194 thread_bundle_
.reset();
198 EXPECT_FALSE(destination_
);
199 DCHECK(streams_
.empty());
200 DCHECK(sources_
.empty());
204 mock_vais_
= new MockVirtualAudioInputStream(audio_thread_
.task_runner());
205 EXPECT_CALL(*mock_vais_
, Open());
206 EXPECT_CALL(*mock_vais_
, Close()); // At Close() time.
208 ASSERT_EQ(kRenderProcessId
, current_render_process_id_
);
209 ASSERT_EQ(kRenderFrameId
, current_render_frame_id_
);
210 EXPECT_CALL(*mock_tracker_
.get(),
211 Start(kRenderProcessId
, kRenderFrameId
, _
))
213 SaveArg
<2>(&change_callback_
),
214 WithArgs
<0, 1>(Invoke(this,
215 &WebContentsAudioInputStreamTest::
216 SimulateChangeCallback
))));
218 EXPECT_CALL(*mock_tracker_
.get(), Stop()); // At Close() time.
220 wcais_
= new WebContentsAudioInputStream(
221 current_render_process_id_
, current_render_frame_id_
,
222 mock_mirroring_manager_
.get(),
223 mock_tracker_
, mock_vais_
);
228 EXPECT_CALL(*mock_vais_
, Start(&mock_input_callback_
));
229 EXPECT_CALL(*mock_vais_
, Stop()); // At Stop() time.
231 EXPECT_CALL(*mock_mirroring_manager_
, StartMirroring(NotNull()))
232 .WillOnce(SaveArg
<0>(&destination_
))
233 .RetiresOnSaturation();
234 // At Stop() time, or when the mirroring target changes:
235 EXPECT_CALL(*mock_mirroring_manager_
, StopMirroring(NotNull()))
238 static_cast<AudioMirroringManager::MirroringDestination
*>(NULL
)))
239 .RetiresOnSaturation();
241 EXPECT_CALL(mock_input_callback_
, OnData(NotNull(), NotNull(), _
, _
))
243 InvokeWithoutArgs(&on_data_event_
, &base::WaitableEvent::Signal
));
245 wcais_
->Start(&mock_input_callback_
);
247 // Test plumbing of volume controls and automatic gain controls. Calls to
248 // wcais_ methods should delegate directly to mock_vais_.
249 EXPECT_CALL(*mock_vais_
, GetVolume());
250 double volume
= wcais_
->GetVolume();
251 EXPECT_CALL(*mock_vais_
, GetMaxVolume());
252 const double max_volume
= wcais_
->GetMaxVolume();
254 if (volume
< max_volume
) {
257 EXPECT_CALL(*mock_vais_
, SetVolume(volume
));
258 wcais_
->SetVolume(volume
);
259 EXPECT_CALL(*mock_vais_
, GetAutomaticGainControl());
260 bool auto_gain
= wcais_
->GetAutomaticGainControl();
261 auto_gain
= !auto_gain
;
262 EXPECT_CALL(*mock_vais_
, SetAutomaticGainControl(auto_gain
));
263 wcais_
->SetAutomaticGainControl(auto_gain
);
266 void AddAnotherInput() {
267 // Note: WCAIS posts a task to invoke
268 // MockAudioMirroringManager::StartMirroring() on the IO thread, which
269 // causes our mock to set |destination_|. Block until that has happened.
270 base::WaitableEvent
done(false, false);
271 BrowserThread::PostTask(
272 BrowserThread::IO
, FROM_HERE
, base::Bind(
273 &base::WaitableEvent::Signal
, base::Unretained(&done
)));
275 ASSERT_TRUE(destination_
);
277 EXPECT_CALL(*mock_vais_
, AddOutputStream(NotNull(), _
))
278 .RetiresOnSaturation();
279 // Later, when stream is closed:
280 EXPECT_CALL(*mock_vais_
, RemoveOutputStream(NotNull(), _
))
281 .RetiresOnSaturation();
283 const AudioParameters
& params
= TestAudioParameters();
284 AudioOutputStream
* const out
= destination_
->AddInput(params
);
286 streams_
.push_back(out
);
287 EXPECT_TRUE(out
->Open());
288 SineWaveAudioSource
* const source
= new SineWaveAudioSource(
289 params
.channel_layout(), 200.0, params
.sample_rate());
290 sources_
.push_back(source
);
294 void RemoveOneInputInFIFOOrder() {
295 ASSERT_FALSE(streams_
.empty());
296 AudioOutputStream
* const out
= streams_
.front();
297 streams_
.pop_front();
299 out
->Close(); // Self-deletes.
300 ASSERT_TRUE(!sources_
.empty());
301 delete sources_
.front();
302 sources_
.pop_front();
305 void ChangeMirroringTarget() {
306 const int next_render_process_id
=
307 current_render_process_id_
== kRenderProcessId
?
308 kAnotherRenderProcessId
: kRenderProcessId
;
309 const int next_render_frame_id
=
310 current_render_frame_id_
== kRenderFrameId
?
311 kAnotherRenderFrameId
: kRenderFrameId
;
313 EXPECT_CALL(*mock_mirroring_manager_
, StartMirroring(NotNull()))
314 .WillOnce(SaveArg
<0>(&destination_
))
315 .RetiresOnSaturation();
317 SimulateChangeCallback(next_render_process_id
, next_render_frame_id
);
319 current_render_process_id_
= next_render_process_id
;
320 current_render_frame_id_
= next_render_frame_id
;
323 void LoseMirroringTarget() {
324 EXPECT_CALL(mock_input_callback_
, OnError(_
));
326 SimulateChangeCallback(-1, -1);
334 // WebContentsAudioInputStream self-destructs on Close(). Its internal
335 // objects hang around until they are no longer referred to (e.g., as tasks
336 // on other threads shut things down).
342 void RunOnAudioThread(const base::Closure
& closure
) {
343 audio_thread_
.task_runner()->PostTask(FROM_HERE
, closure
);
346 // Block the calling thread until OnData() callbacks are being made.
348 // Note: Arbitrarily chosen, but more iterations causes tests to take
349 // significantly more time.
350 static const int kNumIterations
= 3;
351 for (int i
= 0; i
< kNumIterations
; ++i
)
352 on_data_event_
.Wait();
356 void SimulateChangeCallback(int render_process_id
, int render_frame_id
) {
357 ASSERT_FALSE(change_callback_
.is_null());
358 change_callback_
.Run(render_process_id
!= -1 && render_frame_id
!= -1);
361 scoped_ptr
<TestBrowserThreadBundle
> thread_bundle_
;
362 base::Thread audio_thread_
;
364 scoped_ptr
<MockAudioMirroringManager
> mock_mirroring_manager_
;
365 scoped_refptr
<MockWebContentsTracker
> mock_tracker_
;
367 MockVirtualAudioInputStream
* mock_vais_
; // Owned by wcais_.
368 WebContentsAudioInputStream
* wcais_
; // Self-destructs on Close().
370 // Mock consumer of audio data.
371 MockAudioInputCallback mock_input_callback_
;
373 // Provided by WebContentsAudioInputStream to the mock WebContentsTracker.
374 // This callback is saved here, and test code will invoke it to simulate
375 // target change events.
376 WebContentsTracker::ChangeCallback change_callback_
;
378 // Provided by WebContentsAudioInputStream to the mock AudioMirroringManager.
379 // A pointer to the implementation is saved here, and test code will invoke it
380 // to simulate: 1) calls to AddInput(); and 2) diverting audio data.
381 AudioMirroringManager::MirroringDestination
* destination_
;
383 // Current target RenderFrame. These get flipped in ChangedMirroringTarget().
384 int current_render_process_id_
;
385 int current_render_frame_id_
;
387 // Streams provided by calls to WebContentsAudioInputStream::AddInput(). Each
388 // is started with a simulated source of audio data.
389 std::list
<AudioOutputStream
*> streams_
;
390 std::list
<SineWaveAudioSource
*> sources_
; // 1:1 with elements in streams_.
392 base::WaitableEvent on_data_event_
;
394 DISALLOW_COPY_AND_ASSIGN(WebContentsAudioInputStreamTest
);
397 #define RUN_ON_AUDIO_THREAD(method) \
398 RunOnAudioThread(base::Bind(&WebContentsAudioInputStreamTest::method, \
399 base::Unretained(this)))
401 TEST_F(WebContentsAudioInputStreamTest
, OpenedButNeverStarted
) {
402 RUN_ON_AUDIO_THREAD(Open
);
403 RUN_ON_AUDIO_THREAD(Close
);
406 TEST_F(WebContentsAudioInputStreamTest
, MirroringNothing
) {
407 RUN_ON_AUDIO_THREAD(Open
);
408 RUN_ON_AUDIO_THREAD(Start
);
410 RUN_ON_AUDIO_THREAD(Stop
);
411 RUN_ON_AUDIO_THREAD(Close
);
414 TEST_F(WebContentsAudioInputStreamTest
, MirroringOutputOutlivesSession
) {
415 RUN_ON_AUDIO_THREAD(Open
);
416 RUN_ON_AUDIO_THREAD(Start
);
417 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
419 RUN_ON_AUDIO_THREAD(Stop
);
420 RUN_ON_AUDIO_THREAD(Close
);
421 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
424 TEST_F(WebContentsAudioInputStreamTest
, MirroringOutputWithinSession
) {
425 RUN_ON_AUDIO_THREAD(Open
);
426 RUN_ON_AUDIO_THREAD(Start
);
427 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
429 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
430 RUN_ON_AUDIO_THREAD(Stop
);
431 RUN_ON_AUDIO_THREAD(Close
);
434 TEST_F(WebContentsAudioInputStreamTest
, MirroringNothingWithTargetChange
) {
435 RUN_ON_AUDIO_THREAD(Open
);
436 RUN_ON_AUDIO_THREAD(Start
);
437 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget
);
438 RUN_ON_AUDIO_THREAD(Stop
);
439 RUN_ON_AUDIO_THREAD(Close
);
442 TEST_F(WebContentsAudioInputStreamTest
, MirroringOneStreamAfterTargetChange
) {
443 RUN_ON_AUDIO_THREAD(Open
);
444 RUN_ON_AUDIO_THREAD(Start
);
445 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget
);
446 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
448 RUN_ON_AUDIO_THREAD(Stop
);
449 RUN_ON_AUDIO_THREAD(Close
);
450 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
453 TEST_F(WebContentsAudioInputStreamTest
, MirroringOneStreamWithTargetChange
) {
454 RUN_ON_AUDIO_THREAD(Open
);
455 RUN_ON_AUDIO_THREAD(Start
);
456 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
458 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget
);
459 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
460 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
462 RUN_ON_AUDIO_THREAD(Stop
);
463 RUN_ON_AUDIO_THREAD(Close
);
464 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
467 TEST_F(WebContentsAudioInputStreamTest
, MirroringLostTarget
) {
468 RUN_ON_AUDIO_THREAD(Open
);
469 RUN_ON_AUDIO_THREAD(Start
);
470 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
472 RUN_ON_AUDIO_THREAD(LoseMirroringTarget
);
473 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
474 RUN_ON_AUDIO_THREAD(Stop
);
475 RUN_ON_AUDIO_THREAD(Close
);
478 TEST_F(WebContentsAudioInputStreamTest
, MirroringMultipleStreamsAndTargets
) {
479 RUN_ON_AUDIO_THREAD(Open
);
480 RUN_ON_AUDIO_THREAD(Start
);
481 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
483 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
484 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
485 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
487 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget
);
488 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
490 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
491 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
492 RUN_ON_AUDIO_THREAD(AddAnotherInput
);
494 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
496 RUN_ON_AUDIO_THREAD(ChangeMirroringTarget
);
497 RUN_ON_AUDIO_THREAD(RemoveOneInputInFIFOOrder
);
498 RUN_ON_AUDIO_THREAD(Stop
);
499 RUN_ON_AUDIO_THREAD(Close
);
502 } // namespace content