OLD | NEW |
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/environment.h" | 6 #include "base/environment.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
11 #include "media/audio/audio_io.h" | 11 #include "media/audio/audio_io.h" |
12 #include "media/audio/audio_manager_base.h" | 12 #include "media/audio/audio_manager_base.h" |
| 13 #include "media/audio/audio_unittest_util.h" |
13 #include "media/audio/mac/audio_low_latency_input_mac.h" | 14 #include "media/audio/mac/audio_low_latency_input_mac.h" |
14 #include "media/base/seekable_buffer.h" | 15 #include "media/base/seekable_buffer.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using ::testing::_; | 19 using ::testing::_; |
19 using ::testing::AnyNumber; | 20 using ::testing::AnyNumber; |
20 using ::testing::AtLeast; | 21 using ::testing::AtLeast; |
21 using ::testing::Ge; | 22 using ::testing::Ge; |
22 using ::testing::NotNull; | 23 using ::testing::NotNull; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 protected: | 106 protected: |
106 MacAudioInputTest() | 107 MacAudioInputTest() |
107 : message_loop_(base::MessageLoop::TYPE_UI), | 108 : message_loop_(base::MessageLoop::TYPE_UI), |
108 audio_manager_(AudioManager::CreateForTesting()) { | 109 audio_manager_(AudioManager::CreateForTesting()) { |
109 // Wait for the AudioManager to finish any initialization on the audio loop. | 110 // Wait for the AudioManager to finish any initialization on the audio loop. |
110 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
111 } | 112 } |
112 | 113 |
113 ~MacAudioInputTest() override { base::RunLoop().RunUntilIdle(); } | 114 ~MacAudioInputTest() override { base::RunLoop().RunUntilIdle(); } |
114 | 115 |
115 // Convenience method which ensures that we are not running on the build | 116 bool InputDevicesAvailable() { |
116 // bots and that at least one valid input device can be found. | 117 return audio_manager_->HasAudioInputDevices(); |
117 bool CanRunAudioTests() { | |
118 bool has_input = audio_manager_->HasAudioInputDevices(); | |
119 if (!has_input) | |
120 LOG(WARNING) << "No input devices detected"; | |
121 return has_input; | |
122 } | 118 } |
123 | 119 |
124 // Convenience method which creates a default AudioInputStream object using | 120 // Convenience method which creates a default AudioInputStream object using |
125 // a 10ms frame size and a sample rate which is set to the hardware sample | 121 // a 10ms frame size and a sample rate which is set to the hardware sample |
126 // rate. | 122 // rate. |
127 AudioInputStream* CreateDefaultAudioInputStream() { | 123 AudioInputStream* CreateDefaultAudioInputStream() { |
128 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 124 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
129 int samples_per_packet = fs / 100; | 125 int samples_per_packet = fs / 100; |
130 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( | 126 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( |
131 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 127 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
(...skipping 15 matching lines...) Expand all Loading... |
147 EXPECT_TRUE(ais); | 143 EXPECT_TRUE(ais); |
148 return ais; | 144 return ais; |
149 } | 145 } |
150 | 146 |
151 base::MessageLoop message_loop_; | 147 base::MessageLoop message_loop_; |
152 scoped_ptr<AudioManager> audio_manager_; | 148 scoped_ptr<AudioManager> audio_manager_; |
153 }; | 149 }; |
154 | 150 |
155 // Test Create(), Close(). | 151 // Test Create(), Close(). |
156 TEST_F(MacAudioInputTest, AUAudioInputStreamCreateAndClose) { | 152 TEST_F(MacAudioInputTest, AUAudioInputStreamCreateAndClose) { |
157 if (!CanRunAudioTests()) | 153 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
158 return; | |
159 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 154 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
160 ais->Close(); | 155 ais->Close(); |
161 } | 156 } |
162 | 157 |
163 // Test Open(), Close(). | 158 // Test Open(), Close(). |
164 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenAndClose) { | 159 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenAndClose) { |
165 if (!CanRunAudioTests()) | 160 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
166 return; | |
167 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 161 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
168 EXPECT_TRUE(ais->Open()); | 162 EXPECT_TRUE(ais->Open()); |
169 ais->Close(); | 163 ais->Close(); |
170 } | 164 } |
171 | 165 |
172 // Test Open(), Start(), Close(). | 166 // Test Open(), Start(), Close(). |
173 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartAndClose) { | 167 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartAndClose) { |
174 if (!CanRunAudioTests()) | 168 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
175 return; | |
176 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 169 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
177 EXPECT_TRUE(ais->Open()); | 170 EXPECT_TRUE(ais->Open()); |
178 MockAudioInputCallback sink; | 171 MockAudioInputCallback sink; |
179 ais->Start(&sink); | 172 ais->Start(&sink); |
180 ais->Close(); | 173 ais->Close(); |
181 } | 174 } |
182 | 175 |
183 // Test Open(), Start(), Stop(), Close(). | 176 // Test Open(), Start(), Stop(), Close(). |
184 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartStopAndClose) { | 177 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartStopAndClose) { |
185 if (!CanRunAudioTests()) | 178 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
186 return; | |
187 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 179 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
188 EXPECT_TRUE(ais->Open()); | 180 EXPECT_TRUE(ais->Open()); |
189 MockAudioInputCallback sink; | 181 MockAudioInputCallback sink; |
190 ais->Start(&sink); | 182 ais->Start(&sink); |
191 ais->Stop(); | 183 ais->Stop(); |
192 ais->Close(); | 184 ais->Close(); |
193 } | 185 } |
194 | 186 |
195 // Test some additional calling sequences. | 187 // Test some additional calling sequences. |
196 TEST_F(MacAudioInputTest, AUAudioInputStreamMiscCallingSequences) { | 188 TEST_F(MacAudioInputTest, AUAudioInputStreamMiscCallingSequences) { |
197 if (!CanRunAudioTests()) | 189 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
198 return; | |
199 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 190 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
200 AUAudioInputStream* auais = static_cast<AUAudioInputStream*>(ais); | 191 AUAudioInputStream* auais = static_cast<AUAudioInputStream*>(ais); |
201 | 192 |
202 // Open(), Open() should fail the second time. | 193 // Open(), Open() should fail the second time. |
203 EXPECT_TRUE(ais->Open()); | 194 EXPECT_TRUE(ais->Open()); |
204 EXPECT_FALSE(ais->Open()); | 195 EXPECT_FALSE(ais->Open()); |
205 | 196 |
206 MockAudioInputCallback sink; | 197 MockAudioInputCallback sink; |
207 | 198 |
208 // Start(), Start() is a valid calling sequence (second call does nothing). | 199 // Start(), Start() is a valid calling sequence (second call does nothing). |
209 ais->Start(&sink); | 200 ais->Start(&sink); |
210 EXPECT_TRUE(auais->started()); | 201 EXPECT_TRUE(auais->started()); |
211 ais->Start(&sink); | 202 ais->Start(&sink); |
212 EXPECT_TRUE(auais->started()); | 203 EXPECT_TRUE(auais->started()); |
213 | 204 |
214 // Stop(), Stop() is a valid calling sequence (second call does nothing). | 205 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
215 ais->Stop(); | 206 ais->Stop(); |
216 EXPECT_FALSE(auais->started()); | 207 EXPECT_FALSE(auais->started()); |
217 ais->Stop(); | 208 ais->Stop(); |
218 EXPECT_FALSE(auais->started()); | 209 EXPECT_FALSE(auais->started()); |
219 | 210 |
220 ais->Close(); | 211 ais->Close(); |
221 } | 212 } |
222 | 213 |
223 // Verify that recording starts and stops correctly in mono using mocked sink. | 214 // Verify that recording starts and stops correctly in mono using mocked sink. |
224 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { | 215 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { |
225 if (!CanRunAudioTests()) | 216 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
226 return; | |
227 | 217 |
228 int count = 0; | 218 int count = 0; |
229 | 219 |
230 // Create an audio input stream which records in mono. | 220 // Create an audio input stream which records in mono. |
231 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); | 221 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); |
232 EXPECT_TRUE(ais->Open()); | 222 EXPECT_TRUE(ais->Open()); |
233 | 223 |
234 MockAudioInputCallback sink; | 224 MockAudioInputCallback sink; |
235 | 225 |
236 // We use 10ms packets and will run the test until ten packets are received. | 226 // We use 10ms packets and will run the test until ten packets are received. |
237 // All should contain valid packets of the same size and a valid delay | 227 // All should contain valid packets of the same size and a valid delay |
238 // estimate. | 228 // estimate. |
239 base::RunLoop run_loop; | 229 base::RunLoop run_loop; |
240 EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) | 230 EXPECT_CALL(sink, OnData(ais, NotNull(), _, _)) |
241 .Times(AtLeast(10)) | 231 .Times(AtLeast(10)) |
242 .WillRepeatedly(CheckCountAndPostQuitTask( | 232 .WillRepeatedly(CheckCountAndPostQuitTask( |
243 &count, 10, &message_loop_, run_loop.QuitClosure())); | 233 &count, 10, &message_loop_, run_loop.QuitClosure())); |
244 ais->Start(&sink); | 234 ais->Start(&sink); |
245 run_loop.Run(); | 235 run_loop.Run(); |
246 ais->Stop(); | 236 ais->Stop(); |
247 ais->Close(); | 237 ais->Close(); |
248 } | 238 } |
249 | 239 |
250 // Verify that recording starts and stops correctly in mono using mocked sink. | 240 // Verify that recording starts and stops correctly in mono using mocked sink. |
251 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { | 241 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { |
252 if (!CanRunAudioTests()) | 242 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
253 return; | |
254 | 243 |
255 int count = 0; | 244 int count = 0; |
256 | 245 |
257 // Create an audio input stream which records in stereo. | 246 // Create an audio input stream which records in stereo. |
258 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); | 247 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); |
259 EXPECT_TRUE(ais->Open()); | 248 EXPECT_TRUE(ais->Open()); |
260 | 249 |
261 MockAudioInputCallback sink; | 250 MockAudioInputCallback sink; |
262 | 251 |
263 // We use 10ms packets and will run the test until ten packets are received. | 252 // We use 10ms packets and will run the test until ten packets are received. |
(...skipping 17 matching lines...) Expand all Loading... |
281 ais->Close(); | 270 ais->Close(); |
282 } | 271 } |
283 | 272 |
284 // This test is intended for manual tests and should only be enabled | 273 // This test is intended for manual tests and should only be enabled |
285 // when it is required to store the captured data on a local file. | 274 // when it is required to store the captured data on a local file. |
286 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | 275 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
287 // To include disabled tests in test execution, just invoke the test program | 276 // To include disabled tests in test execution, just invoke the test program |
288 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS | 277 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS |
289 // environment variable to a value greater than 0. | 278 // environment variable to a value greater than 0. |
290 TEST_F(MacAudioInputTest, DISABLED_AUAudioInputStreamRecordToFile) { | 279 TEST_F(MacAudioInputTest, DISABLED_AUAudioInputStreamRecordToFile) { |
291 if (!CanRunAudioTests()) | 280 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
292 return; | |
293 const char* file_name = "out_stereo_10sec.pcm"; | 281 const char* file_name = "out_stereo_10sec.pcm"; |
294 | 282 |
295 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 283 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
296 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 284 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
297 EXPECT_TRUE(ais->Open()); | 285 EXPECT_TRUE(ais->Open()); |
298 | 286 |
299 fprintf(stderr, " File name : %s\n", file_name); | 287 fprintf(stderr, " File name : %s\n", file_name); |
300 fprintf(stderr, " Sample rate: %d\n", fs); | 288 fprintf(stderr, " Sample rate: %d\n", fs); |
301 WriteToFileAudioSink file_sink(file_name); | 289 WriteToFileAudioSink file_sink(file_name); |
302 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 290 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
303 ais->Start(&file_sink); | 291 ais->Start(&file_sink); |
304 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 292 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
305 ais->Stop(); | 293 ais->Stop(); |
306 fprintf(stderr, " >> Recording has stopped.\n"); | 294 fprintf(stderr, " >> Recording has stopped.\n"); |
307 ais->Close(); | 295 ais->Close(); |
308 } | 296 } |
309 | 297 |
310 } // namespace media | 298 } // namespace media |
OLD | NEW |