Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(303)

Side by Side Diff: media/audio/audio_input_unittest.cc

Issue 914483002: Add flag --require-audio-hardware-for-testing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/environment.h" 7 #include "base/environment.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
13 #include "media/audio/audio_io.h" 13 #include "media/audio/audio_io.h"
14 #include "media/audio/audio_manager_base.h" 14 #include "media/audio/audio_manager_base.h"
15 #include "media/audio/audio_unittest_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace media { 18 namespace media {
18 19
19 // This class allows to find out if the callbacks are occurring as 20 // This class allows to find out if the callbacks are occurring as
20 // expected and if any error has been reported. 21 // expected and if any error has been reported.
21 class TestInputCallback : public AudioInputStream::AudioInputCallback { 22 class TestInputCallback : public AudioInputStream::AudioInputCallback {
22 public: 23 public:
23 explicit TestInputCallback() 24 explicit TestInputCallback()
24 : callback_count_(0), 25 : callback_count_(0),
(...skipping 16 matching lines...) Expand all
41 } 42 }
42 43
43 private: 44 private:
44 int callback_count_; 45 int callback_count_;
45 int had_error_; 46 int had_error_;
46 }; 47 };
47 48
48 class AudioInputTest : public testing::Test { 49 class AudioInputTest : public testing::Test {
49 public: 50 public:
50 AudioInputTest() : 51 AudioInputTest() :
51 message_loop_(base::MessageLoop::TYPE_UI), 52 message_loop_(base::MessageLoop::TYPE_UI),
52 audio_manager_(AudioManager::CreateForTesting()), 53 audio_manager_(AudioManager::CreateForTesting()),
53 audio_input_stream_(NULL) { 54 audio_input_stream_(NULL) {
54 // Wait for the AudioManager to finish any initialization on the audio loop. 55 // Wait for the AudioManager to finish any initialization on the audio loop.
55 base::RunLoop().RunUntilIdle(); 56 base::RunLoop().RunUntilIdle();
56 } 57 }
57 58
58 ~AudioInputTest() override { base::RunLoop().RunUntilIdle(); } 59 ~AudioInputTest() override { base::RunLoop().RunUntilIdle(); }
59 60
60 protected: 61 protected:
61 AudioManager* audio_manager() { return audio_manager_.get(); } 62 bool InputDevicesAvailable() {
62 63 return audio_manager_->HasAudioInputDevices();
63 bool CanRunAudioTests() {
64 bool has_input = audio_manager()->HasAudioInputDevices();
65 LOG_IF(WARNING, !has_input) << "No input devices detected";
66 return has_input;
67 } 64 }
68 65
69 void MakeAudioInputStreamOnAudioThread() { 66 void MakeAudioInputStreamOnAudioThread() {
70 RunOnAudioThread( 67 RunOnAudioThread(
71 base::Bind(&AudioInputTest::MakeAudioInputStream, 68 base::Bind(&AudioInputTest::MakeAudioInputStream,
72 base::Unretained(this))); 69 base::Unretained(this)));
73 } 70 }
74 71
75 void CloseAudioInputStreamOnAudioThread() { 72 void CloseAudioInputStreamOnAudioThread() {
76 RunOnAudioThread( 73 RunOnAudioThread(
(...skipping 22 matching lines...) Expand all
99 sink)); 96 sink));
100 } 97 }
101 98
102 void StopAndCloseAudioInputStreamOnAudioThread() { 99 void StopAndCloseAudioInputStreamOnAudioThread() {
103 RunOnAudioThread( 100 RunOnAudioThread(
104 base::Bind(&AudioInputTest::StopAndClose, 101 base::Bind(&AudioInputTest::StopAndClose,
105 base::Unretained(this))); 102 base::Unretained(this)));
106 } 103 }
107 104
108 void MakeAudioInputStream() { 105 void MakeAudioInputStream() {
109 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 106 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
110 AudioParameters params = audio_manager()->GetInputStreamParameters( 107 AudioParameters params = audio_manager_->GetInputStreamParameters(
111 AudioManagerBase::kDefaultDeviceId); 108 AudioManagerBase::kDefaultDeviceId);
112 audio_input_stream_ = audio_manager()->MakeAudioInputStream(params, 109 audio_input_stream_ = audio_manager_->MakeAudioInputStream(params,
113 AudioManagerBase::kDefaultDeviceId); 110 AudioManagerBase::kDefaultDeviceId);
114 EXPECT_TRUE(audio_input_stream_); 111 EXPECT_TRUE(audio_input_stream_);
115 } 112 }
116 113
117 void OpenAndClose() { 114 void OpenAndClose() {
118 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 115 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
119 EXPECT_TRUE(audio_input_stream_->Open()); 116 EXPECT_TRUE(audio_input_stream_->Open());
120 audio_input_stream_->Close(); 117 audio_input_stream_->Close();
121 audio_input_stream_ = NULL; 118 audio_input_stream_ = NULL;
122 } 119 }
123 120
124 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) { 121 void OpenAndStart(AudioInputStream::AudioInputCallback* sink) {
125 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 122 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
126 EXPECT_TRUE(audio_input_stream_->Open()); 123 EXPECT_TRUE(audio_input_stream_->Open());
127 audio_input_stream_->Start(sink); 124 audio_input_stream_->Start(sink);
128 } 125 }
129 126
130 void OpenStopAndClose() { 127 void OpenStopAndClose() {
131 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 128 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
132 EXPECT_TRUE(audio_input_stream_->Open()); 129 EXPECT_TRUE(audio_input_stream_->Open());
133 audio_input_stream_->Stop(); 130 audio_input_stream_->Stop();
134 audio_input_stream_->Close(); 131 audio_input_stream_->Close();
135 audio_input_stream_ = NULL; 132 audio_input_stream_ = NULL;
136 } 133 }
137 134
138 void StopAndClose() { 135 void StopAndClose() {
139 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 136 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
140 audio_input_stream_->Stop(); 137 audio_input_stream_->Stop();
141 audio_input_stream_->Close(); 138 audio_input_stream_->Close();
142 audio_input_stream_ = NULL; 139 audio_input_stream_ = NULL;
143 } 140 }
144 141
145 // Synchronously runs the provided callback/closure on the audio thread. 142 // Synchronously runs the provided callback/closure on the audio thread.
146 void RunOnAudioThread(const base::Closure& closure) { 143 void RunOnAudioThread(const base::Closure& closure) {
147 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) { 144 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) {
148 base::WaitableEvent event(false, false); 145 base::WaitableEvent event(false, false);
149 audio_manager()->GetTaskRunner()->PostTask( 146 audio_manager_->GetTaskRunner()->PostTask(
150 FROM_HERE, 147 FROM_HERE,
151 base::Bind(&AudioInputTest::RunOnAudioThreadImpl, 148 base::Bind(&AudioInputTest::RunOnAudioThreadImpl,
152 base::Unretained(this), 149 base::Unretained(this),
153 closure, 150 closure,
154 &event)); 151 &event));
155 event.Wait(); 152 event.Wait();
156 } else { 153 } else {
157 closure.Run(); 154 closure.Run();
158 } 155 }
159 } 156 }
160 157
161 void RunOnAudioThreadImpl(const base::Closure& closure, 158 void RunOnAudioThreadImpl(const base::Closure& closure,
162 base::WaitableEvent* event) { 159 base::WaitableEvent* event) {
163 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); 160 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
164 closure.Run(); 161 closure.Run();
165 event->Signal(); 162 event->Signal();
166 } 163 }
167 164
168 base::MessageLoop message_loop_; 165 base::MessageLoop message_loop_;
169 scoped_ptr<AudioManager> audio_manager_; 166 scoped_ptr<AudioManager> audio_manager_;
170 AudioInputStream* audio_input_stream_; 167 AudioInputStream* audio_input_stream_;
171 168
172 private: 169 private:
173 DISALLOW_COPY_AND_ASSIGN(AudioInputTest); 170 DISALLOW_COPY_AND_ASSIGN(AudioInputTest);
174 }; 171 };
175 172
176 // Test create and close of an AudioInputStream without recording audio. 173 // Test create and close of an AudioInputStream without recording audio.
177 TEST_F(AudioInputTest, CreateAndClose) { 174 TEST_F(AudioInputTest, CreateAndClose) {
178 if (!CanRunAudioTests()) 175 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
179 return;
180 MakeAudioInputStreamOnAudioThread(); 176 MakeAudioInputStreamOnAudioThread();
181 CloseAudioInputStreamOnAudioThread(); 177 CloseAudioInputStreamOnAudioThread();
182 } 178 }
183 179
184 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 180 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
185 // This test is failing on ARM linux: http://crbug.com/238490 181 // This test is failing on ARM linux: http://crbug.com/238490
186 #define MAYBE_OpenAndClose DISABLED_OpenAndClose 182 #define MAYBE_OpenAndClose DISABLED_OpenAndClose
187 #else 183 #else
188 #define MAYBE_OpenAndClose OpenAndClose 184 #define MAYBE_OpenAndClose OpenAndClose
189 #endif 185 #endif
190 // Test create, open and close of an AudioInputStream without recording audio. 186 // Test create, open and close of an AudioInputStream without recording audio.
191 TEST_F(AudioInputTest, MAYBE_OpenAndClose) { 187 TEST_F(AudioInputTest, MAYBE_OpenAndClose) {
192 if (!CanRunAudioTests()) 188 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
193 return;
194 MakeAudioInputStreamOnAudioThread(); 189 MakeAudioInputStreamOnAudioThread();
195 OpenAndCloseAudioInputStreamOnAudioThread(); 190 OpenAndCloseAudioInputStreamOnAudioThread();
196 } 191 }
197 192
198 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 193 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
199 // This test is failing on ARM linux: http://crbug.com/238490 194 // This test is failing on ARM linux: http://crbug.com/238490
200 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose 195 #define MAYBE_OpenStopAndClose DISABLED_OpenStopAndClose
201 #else 196 #else
202 #define MAYBE_OpenStopAndClose OpenStopAndClose 197 #define MAYBE_OpenStopAndClose OpenStopAndClose
203 #endif 198 #endif
204 // Test create, open, stop and close of an AudioInputStream without recording. 199 // Test create, open, stop and close of an AudioInputStream without recording.
205 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) { 200 TEST_F(AudioInputTest, MAYBE_OpenStopAndClose) {
206 if (!CanRunAudioTests()) 201 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
207 return;
208 MakeAudioInputStreamOnAudioThread(); 202 MakeAudioInputStreamOnAudioThread();
209 OpenStopAndCloseAudioInputStreamOnAudioThread(); 203 OpenStopAndCloseAudioInputStreamOnAudioThread();
210 } 204 }
211 205
212 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY) 206 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
213 // This test is failing on ARM linux: http://crbug.com/238490 207 // This test is failing on ARM linux: http://crbug.com/238490
214 #define MAYBE_Record DISABLED_Record 208 #define MAYBE_Record DISABLED_Record
215 #else 209 #else
216 #define MAYBE_Record Record 210 #define MAYBE_Record Record
217 #endif 211 #endif
218 // Test a normal recording sequence using an AudioInputStream. 212 // Test a normal recording sequence using an AudioInputStream.
219 // Very simple test which starts capturing during half a second and verifies 213 // Very simple test which starts capturing during half a second and verifies
220 // that recording starts. 214 // that recording starts.
221 TEST_F(AudioInputTest, MAYBE_Record) { 215 TEST_F(AudioInputTest, MAYBE_Record) {
222 if (!CanRunAudioTests()) 216 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
223 return;
224 MakeAudioInputStreamOnAudioThread(); 217 MakeAudioInputStreamOnAudioThread();
225 218
226 TestInputCallback test_callback; 219 TestInputCallback test_callback;
227 OpenAndStartAudioInputStreamOnAudioThread(&test_callback); 220 OpenAndStartAudioInputStreamOnAudioThread(&test_callback);
228 221
229 message_loop_.PostDelayedTask( 222 message_loop_.PostDelayedTask(
230 FROM_HERE, 223 FROM_HERE,
231 base::MessageLoop::QuitClosure(), 224 base::MessageLoop::QuitClosure(),
232 base::TimeDelta::FromMilliseconds(500)); 225 base::TimeDelta::FromMilliseconds(500));
233 message_loop_.Run(); 226 message_loop_.Run();
234 EXPECT_GE(test_callback.callback_count(), 2); 227 EXPECT_GE(test_callback.callback_count(), 2);
235 EXPECT_FALSE(test_callback.had_error()); 228 EXPECT_FALSE(test_callback.had_error());
236 229
237 StopAndCloseAudioInputStreamOnAudioThread(); 230 StopAndCloseAudioInputStreamOnAudioThread();
238 } 231 }
239 232
240 } // namespace media 233 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698