1 // Copyright (c) 2012 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/basictypes.h"
7 #include "base/environment.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/platform_thread.h"
13 #include "media/audio/audio_io.h"
14 #include "media/audio/audio_manager_base.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // This class allows to find out if the callbacks are occurring as
20 // expected and if any error has been reported.
21 class TestInputCallback
: public AudioInputStream::AudioInputCallback
{
23 explicit TestInputCallback()
27 void OnData(AudioInputStream
* stream
,
28 const AudioBus
* source
,
29 uint32 hardware_delay_bytes
,
30 double volume
) override
{
33 void OnError(AudioInputStream
* stream
) override
{ ++had_error_
; }
34 // Returns how many times OnData() has been called.
35 int callback_count() const {
36 return callback_count_
;
38 // Returns how many times the OnError callback was called.
39 int had_error() const {
48 class AudioInputTest
: public testing::Test
{
51 message_loop_(base::MessageLoop::TYPE_UI
),
52 audio_manager_(AudioManager::CreateForTesting()),
53 audio_input_stream_(NULL
) {
54 // Wait for the AudioManager to finish any initialization on the audio loop.
55 base::RunLoop().RunUntilIdle();
58 ~AudioInputTest() override
{ base::RunLoop().RunUntilIdle(); }
61 AudioManager
* audio_manager() { return audio_manager_
.get(); }
63 bool CanRunAudioTests() {
64 bool has_input
= audio_manager()->HasAudioInputDevices();
65 LOG_IF(WARNING
, !has_input
) << "No input devices detected";
69 void MakeAudioInputStreamOnAudioThread() {
71 base::Bind(&AudioInputTest::MakeAudioInputStream
,
72 base::Unretained(this)));
75 void CloseAudioInputStreamOnAudioThread() {
77 base::Bind(&AudioInputStream::Close
,
78 base::Unretained(audio_input_stream_
)));
79 audio_input_stream_
= NULL
;
82 void OpenAndCloseAudioInputStreamOnAudioThread() {
84 base::Bind(&AudioInputTest::OpenAndClose
,
85 base::Unretained(this)));
88 void OpenStopAndCloseAudioInputStreamOnAudioThread() {
90 base::Bind(&AudioInputTest::OpenStopAndClose
,
91 base::Unretained(this)));
94 void OpenAndStartAudioInputStreamOnAudioThread(
95 AudioInputStream::AudioInputCallback
* sink
) {
97 base::Bind(&AudioInputTest::OpenAndStart
,
98 base::Unretained(this),
102 void StopAndCloseAudioInputStreamOnAudioThread() {
104 base::Bind(&AudioInputTest::StopAndClose
,
105 base::Unretained(this)));
108 void MakeAudioInputStream() {
109 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
110 AudioParameters params
= audio_manager()->GetInputStreamParameters(
111 AudioManagerBase::kDefaultDeviceId
);
112 audio_input_stream_
= audio_manager()->MakeAudioInputStream(params
,
113 AudioManagerBase::kDefaultDeviceId
);
114 EXPECT_TRUE(audio_input_stream_
);
117 void OpenAndClose() {
118 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
119 EXPECT_TRUE(audio_input_stream_
->Open());
120 audio_input_stream_
->Close();
121 audio_input_stream_
= NULL
;
124 void OpenAndStart(AudioInputStream::AudioInputCallback
* sink
) {
125 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
126 EXPECT_TRUE(audio_input_stream_
->Open());
127 audio_input_stream_
->Start(sink
);
130 void OpenStopAndClose() {
131 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
132 EXPECT_TRUE(audio_input_stream_
->Open());
133 audio_input_stream_
->Stop();
134 audio_input_stream_
->Close();
135 audio_input_stream_
= NULL
;
138 void StopAndClose() {
139 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
140 audio_input_stream_
->Stop();
141 audio_input_stream_
->Close();
142 audio_input_stream_
= NULL
;
145 // Synchronously runs the provided callback/closure on the audio thread.
146 void RunOnAudioThread(const base::Closure
& closure
) {
147 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) {
148 base::WaitableEvent
event(false, false);
149 audio_manager()->GetTaskRunner()->PostTask(
151 base::Bind(&AudioInputTest::RunOnAudioThreadImpl
,
152 base::Unretained(this),
161 void RunOnAudioThreadImpl(const base::Closure
& closure
,
162 base::WaitableEvent
* event
) {
163 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread());
168 base::MessageLoop message_loop_
;
169 scoped_ptr
<AudioManager
> audio_manager_
;
170 AudioInputStream
* audio_input_stream_
;
173 DISALLOW_COPY_AND_ASSIGN(AudioInputTest
);
176 // Test create and close of an AudioInputStream without recording audio.
177 TEST_F(AudioInputTest
, CreateAndClose
) {
178 if (!CanRunAudioTests())
180 MakeAudioInputStreamOnAudioThread();
181 CloseAudioInputStreamOnAudioThread();
184 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
185 // This test is failing on ARM linux: http://crbug.com/238490
186 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
188 #define MAYBE_OpenAndClose OpenAndClose
190 // Test create, open and close of an AudioInputStream without recording audio.
191 TEST_F(AudioInputTest
, MAYBE_OpenAndClose
) {
192 if (!CanRunAudioTests())
194 MakeAudioInputStreamOnAudioThread();
195 OpenAndCloseAudioInputStreamOnAudioThread();
198 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
199 // This test is failing on ARM linux: http://crbug.com/238490
200 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
202 #define MAYBE_OpenStopAndClose OpenStopAndClose
204 // Test create, open, stop and close of an AudioInputStream without recording.
205 TEST_F(AudioInputTest
, MAYBE_OpenStopAndClose
) {
206 if (!CanRunAudioTests())
208 MakeAudioInputStreamOnAudioThread();
209 OpenStopAndCloseAudioInputStreamOnAudioThread();
212 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
213 // This test is failing on ARM linux: http://crbug.com/238490
214 #define MAYBE_Record DISABLED_Record
216 #define MAYBE_Record Record
218 // Test a normal recording sequence using an AudioInputStream.
219 // Very simple test which starts capturing during half a second and verifies
220 // that recording starts.
221 TEST_F(AudioInputTest
, MAYBE_Record
) {
222 if (!CanRunAudioTests())
224 MakeAudioInputStreamOnAudioThread();
226 TestInputCallback test_callback
;
227 OpenAndStartAudioInputStreamOnAudioThread(&test_callback
);
229 message_loop_
.PostDelayedTask(
231 base::MessageLoop::QuitClosure(),
232 base::TimeDelta::FromMilliseconds(500));
234 EXPECT_GE(test_callback
.callback_count(), 2);
235 EXPECT_FALSE(test_callback
.had_error());
237 StopAndCloseAudioInputStreamOnAudioThread();