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 "media/audio/audio_unittest_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 // This class allows to find out if the callbacks are occurring as
21 // expected and if any error has been reported.
22 class TestInputCallback
: public AudioInputStream::AudioInputCallback
{
24 explicit TestInputCallback()
28 void OnData(AudioInputStream
* stream
,
29 const AudioBus
* source
,
30 uint32 hardware_delay_bytes
,
31 double volume
) override
{
34 void OnError(AudioInputStream
* stream
) override
{ ++had_error_
; }
35 // Returns how many times OnData() has been called.
36 int callback_count() const {
37 return callback_count_
;
39 // Returns how many times the OnError callback was called.
40 int had_error() const {
49 class AudioInputTest
: public testing::Test
{
52 message_loop_(base::MessageLoop::TYPE_UI
),
53 audio_manager_(AudioManager::CreateForTesting()),
54 audio_input_stream_(NULL
) {
55 // Wait for the AudioManager to finish any initialization on the audio loop.
56 base::RunLoop().RunUntilIdle();
59 ~AudioInputTest() override
{ base::RunLoop().RunUntilIdle(); }
62 bool InputDevicesAvailable() {
63 return audio_manager_
->HasAudioInputDevices();
66 void MakeAudioInputStreamOnAudioThread() {
68 base::Bind(&AudioInputTest::MakeAudioInputStream
,
69 base::Unretained(this)));
72 void CloseAudioInputStreamOnAudioThread() {
74 base::Bind(&AudioInputStream::Close
,
75 base::Unretained(audio_input_stream_
)));
76 audio_input_stream_
= NULL
;
79 void OpenAndCloseAudioInputStreamOnAudioThread() {
81 base::Bind(&AudioInputTest::OpenAndClose
,
82 base::Unretained(this)));
85 void OpenStopAndCloseAudioInputStreamOnAudioThread() {
87 base::Bind(&AudioInputTest::OpenStopAndClose
,
88 base::Unretained(this)));
91 void OpenAndStartAudioInputStreamOnAudioThread(
92 AudioInputStream::AudioInputCallback
* sink
) {
94 base::Bind(&AudioInputTest::OpenAndStart
,
95 base::Unretained(this),
99 void StopAndCloseAudioInputStreamOnAudioThread() {
101 base::Bind(&AudioInputTest::StopAndClose
,
102 base::Unretained(this)));
105 void MakeAudioInputStream() {
106 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
107 AudioParameters params
= audio_manager_
->GetInputStreamParameters(
108 AudioManagerBase::kDefaultDeviceId
);
109 audio_input_stream_
= audio_manager_
->MakeAudioInputStream(params
,
110 AudioManagerBase::kDefaultDeviceId
);
111 EXPECT_TRUE(audio_input_stream_
);
114 void OpenAndClose() {
115 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
116 EXPECT_TRUE(audio_input_stream_
->Open());
117 audio_input_stream_
->Close();
118 audio_input_stream_
= NULL
;
121 void OpenAndStart(AudioInputStream::AudioInputCallback
* sink
) {
122 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
123 EXPECT_TRUE(audio_input_stream_
->Open());
124 audio_input_stream_
->Start(sink
);
127 void OpenStopAndClose() {
128 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
129 EXPECT_TRUE(audio_input_stream_
->Open());
130 audio_input_stream_
->Stop();
131 audio_input_stream_
->Close();
132 audio_input_stream_
= NULL
;
135 void StopAndClose() {
136 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
137 audio_input_stream_
->Stop();
138 audio_input_stream_
->Close();
139 audio_input_stream_
= NULL
;
142 // Synchronously runs the provided callback/closure on the audio thread.
143 void RunOnAudioThread(const base::Closure
& closure
) {
144 if (!audio_manager_
->GetTaskRunner()->BelongsToCurrentThread()) {
145 base::WaitableEvent
event(false, false);
146 audio_manager_
->GetTaskRunner()->PostTask(
148 base::Bind(&AudioInputTest::RunOnAudioThreadImpl
,
149 base::Unretained(this),
158 void RunOnAudioThreadImpl(const base::Closure
& closure
,
159 base::WaitableEvent
* event
) {
160 DCHECK(audio_manager_
->GetTaskRunner()->BelongsToCurrentThread());
165 base::MessageLoop message_loop_
;
166 scoped_ptr
<AudioManager
> audio_manager_
;
167 AudioInputStream
* audio_input_stream_
;
170 DISALLOW_COPY_AND_ASSIGN(AudioInputTest
);
173 // Test create and close of an AudioInputStream without recording audio.
174 TEST_F(AudioInputTest
, CreateAndClose
) {
175 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
176 MakeAudioInputStreamOnAudioThread();
177 CloseAudioInputStreamOnAudioThread();
180 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
181 // This test is failing on ARM linux: http://crbug.com/238490
182 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
184 #define MAYBE_OpenAndClose OpenAndClose
186 // Test create, open and close of an AudioInputStream without recording audio.
187 TEST_F(AudioInputTest
, MAYBE_OpenAndClose
) {
188 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
189 MakeAudioInputStreamOnAudioThread();
190 OpenAndCloseAudioInputStreamOnAudioThread();
193 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
194 // This test is failing on ARM linux: http://crbug.com/238490
195 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
197 #define MAYBE_OpenStopAndClose OpenStopAndClose
199 // Test create, open, stop and close of an AudioInputStream without recording.
200 TEST_F(AudioInputTest
, MAYBE_OpenStopAndClose
) {
201 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
202 MakeAudioInputStreamOnAudioThread();
203 OpenStopAndCloseAudioInputStreamOnAudioThread();
206 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
207 // This test is failing on ARM linux: http://crbug.com/238490
208 #define MAYBE_Record DISABLED_Record
210 #define MAYBE_Record Record
212 // Test a normal recording sequence using an AudioInputStream.
213 // Very simple test which starts capturing during half a second and verifies
214 // that recording starts.
215 TEST_F(AudioInputTest
, MAYBE_Record
) {
216 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
217 MakeAudioInputStreamOnAudioThread();
219 TestInputCallback test_callback
;
220 OpenAndStartAudioInputStreamOnAudioThread(&test_callback
);
222 message_loop_
.PostDelayedTask(
224 base::MessageLoop::QuitClosure(),
225 base::TimeDelta::FromMilliseconds(500));
227 EXPECT_GE(test_callback
.callback_count(), 2);
228 EXPECT_FALSE(test_callback
.had_error());
230 StopAndCloseAudioInputStreamOnAudioThread();