OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/test/test_timeouts.h" | 7 #include "base/test/test_timeouts.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "media/audio/audio_io.h" | 9 #include "media/audio/audio_io.h" |
10 #include "media/audio/audio_manager_base.h" | 10 #include "media/audio/audio_manager_base.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 | 77 |
78 virtual void OnClose(AudioInputStream* stream) {} | 78 virtual void OnClose(AudioInputStream* stream) {} |
79 virtual void OnError(AudioInputStream* stream, int code) {} | 79 virtual void OnError(AudioInputStream* stream, int code) {} |
80 | 80 |
81 private: | 81 private: |
82 media::SeekableBuffer buffer_; | 82 media::SeekableBuffer buffer_; |
83 FILE* file_; | 83 FILE* file_; |
84 size_t bytes_to_write_; | 84 size_t bytes_to_write_; |
85 }; | 85 }; |
86 | 86 |
87 // Convenience method which ensures that we are not running on the build | 87 class MacAudioInputTest : public testing::Test { |
88 // bots and that at least one valid input device can be found. | 88 protected: |
89 static bool CanRunAudioTests() { | 89 MacAudioInputTest() : audio_manager_(AudioManager::Create()) {} |
90 scoped_ptr<base::Environment> env(base::Environment::Create()); | 90 ~MacAudioInputTest() {} |
scherkus (not reviewing)
2011/12/09 22:47:30
virtual
tommi (sloooow) - chröme
2011/12/10 00:11:14
Done.
| |
91 if (env->HasVar("CHROME_HEADLESS")) | |
92 return false; | |
93 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
94 if (NULL == audio_man) | |
95 return false; | |
96 return audio_man->HasAudioInputDevices(); | |
97 } | |
98 | 91 |
99 // Convenience method which creates a default AudioInputStream object using | 92 // Convenience method which ensures that we are not running on the build |
100 // a 10ms frame size and a sample rate which is set to the hardware sample rate. | 93 // bots and that at least one valid input device can be found. |
101 static AudioInputStream* CreateDefaultAudioInputStream() { | 94 bool CanRunAudioTests() { |
102 AudioManager* audio_man = AudioManager::GetAudioManager(); | 95 scoped_ptr<base::Environment> env(base::Environment::Create()); |
103 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 96 if (env->HasVar("CHROME_HEADLESS")) |
104 int samples_per_packet = fs / 100; | 97 return false; |
105 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 98 return audio_manager_->HasAudioInputDevices(); |
106 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 99 } |
107 CHANNEL_LAYOUT_STEREO, fs, 16, samples_per_packet), | |
108 AudioManagerBase::kDefaultDeviceId); | |
109 EXPECT_TRUE(ais); | |
110 return ais; | |
111 } | |
112 | 100 |
113 // Convenience method which creates an AudioInputStream object with a specified | 101 // Convenience method which creates a default AudioInputStream object using |
114 // channel layout. | 102 // a 10ms frame size and a sample rate which is set to the hardware sample |
115 static AudioInputStream* CreateAudioInputStream(ChannelLayout channel_layout) { | 103 // rate. |
116 AudioManager* audio_man = AudioManager::GetAudioManager(); | 104 AudioInputStream* CreateDefaultAudioInputStream() { |
117 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 105 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
118 int samples_per_packet = fs / 100; | 106 int samples_per_packet = fs / 100; |
119 AudioInputStream* ais = audio_man->MakeAudioInputStream( | 107 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( |
120 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 108 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
121 channel_layout, fs, 16, samples_per_packet), | 109 CHANNEL_LAYOUT_STEREO, fs, 16, samples_per_packet), |
122 AudioManagerBase::kDefaultDeviceId); | 110 AudioManagerBase::kDefaultDeviceId); |
123 EXPECT_TRUE(ais); | 111 EXPECT_TRUE(ais); |
124 return ais; | 112 return ais; |
125 } | 113 } |
126 | 114 |
115 // Convenience method which creates an AudioInputStream object with a | |
116 // specified channel layout. | |
117 AudioInputStream* CreateAudioInputStream(ChannelLayout channel_layout) { | |
118 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | |
119 int samples_per_packet = fs / 100; | |
120 AudioInputStream* ais = audio_manager_->MakeAudioInputStream( | |
121 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
122 channel_layout, fs, 16, samples_per_packet), | |
123 AudioManagerBase::kDefaultDeviceId); | |
124 EXPECT_TRUE(ais); | |
125 return ais; | |
126 } | |
127 | |
128 scoped_refptr<AudioManager> audio_manager_; | |
129 }; | |
127 | 130 |
128 // Test Create(), Close(). | 131 // Test Create(), Close(). |
129 TEST(MacAudioInputTest, AUAudioInputStreamCreateAndClose) { | 132 TEST_F(MacAudioInputTest, AUAudioInputStreamCreateAndClose) { |
130 if (!CanRunAudioTests()) | 133 if (!CanRunAudioTests()) |
131 return; | 134 return; |
132 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 135 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
133 ais->Close(); | 136 ais->Close(); |
134 } | 137 } |
135 | 138 |
136 // Test Open(), Close(). | 139 // Test Open(), Close(). |
137 TEST(MacAudioInputTest, AUAudioInputStreamOpenAndClose) { | 140 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenAndClose) { |
138 if (!CanRunAudioTests()) | 141 if (!CanRunAudioTests()) |
139 return; | 142 return; |
140 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 143 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
141 EXPECT_TRUE(ais->Open()); | 144 EXPECT_TRUE(ais->Open()); |
142 ais->Close(); | 145 ais->Close(); |
143 } | 146 } |
144 | 147 |
145 // Test Open(), Start(), Close(). | 148 // Test Open(), Start(), Close(). |
146 TEST(MacAudioInputTest, AUAudioInputStreamOpenStartAndClose) { | 149 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartAndClose) { |
147 if (!CanRunAudioTests()) | 150 if (!CanRunAudioTests()) |
148 return; | 151 return; |
149 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 152 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
150 EXPECT_TRUE(ais->Open()); | 153 EXPECT_TRUE(ais->Open()); |
151 MockAudioInputCallback sink; | 154 MockAudioInputCallback sink; |
152 ais->Start(&sink); | 155 ais->Start(&sink); |
153 EXPECT_CALL(sink, OnClose(ais)) | 156 EXPECT_CALL(sink, OnClose(ais)) |
154 .Times(1); | 157 .Times(1); |
155 ais->Close(); | 158 ais->Close(); |
156 } | 159 } |
157 | 160 |
158 // Test Open(), Start(), Stop(), Close(). | 161 // Test Open(), Start(), Stop(), Close(). |
159 TEST(MacAudioInputTest, AUAudioInputStreamOpenStartStopAndClose) { | 162 TEST_F(MacAudioInputTest, AUAudioInputStreamOpenStartStopAndClose) { |
160 if (!CanRunAudioTests()) | 163 if (!CanRunAudioTests()) |
161 return; | 164 return; |
162 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 165 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
163 EXPECT_TRUE(ais->Open()); | 166 EXPECT_TRUE(ais->Open()); |
164 MockAudioInputCallback sink; | 167 MockAudioInputCallback sink; |
165 ais->Start(&sink); | 168 ais->Start(&sink); |
166 ais->Stop(); | 169 ais->Stop(); |
167 EXPECT_CALL(sink, OnClose(ais)) | 170 EXPECT_CALL(sink, OnClose(ais)) |
168 .Times(1); | 171 .Times(1); |
169 ais->Close(); | 172 ais->Close(); |
170 } | 173 } |
171 | 174 |
172 // Test some additional calling sequences. | 175 // Test some additional calling sequences. |
173 TEST(MacAudioInputTest, AUAudioInputStreamMiscCallingSequences) { | 176 TEST_F(MacAudioInputTest, AUAudioInputStreamMiscCallingSequences) { |
174 if (!CanRunAudioTests()) | 177 if (!CanRunAudioTests()) |
175 return; | 178 return; |
176 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 179 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
177 AUAudioInputStream* auais = static_cast<AUAudioInputStream*>(ais); | 180 AUAudioInputStream* auais = static_cast<AUAudioInputStream*>(ais); |
178 | 181 |
179 // Open(), Open() should fail the second time. | 182 // Open(), Open() should fail the second time. |
180 EXPECT_TRUE(ais->Open()); | 183 EXPECT_TRUE(ais->Open()); |
181 EXPECT_FALSE(ais->Open()); | 184 EXPECT_FALSE(ais->Open()); |
182 | 185 |
183 MockAudioInputCallback sink; | 186 MockAudioInputCallback sink; |
184 | 187 |
185 // Start(), Start() is a valid calling sequence (second call does nothing). | 188 // Start(), Start() is a valid calling sequence (second call does nothing). |
186 ais->Start(&sink); | 189 ais->Start(&sink); |
187 EXPECT_TRUE(auais->started()); | 190 EXPECT_TRUE(auais->started()); |
188 ais->Start(&sink); | 191 ais->Start(&sink); |
189 EXPECT_TRUE(auais->started()); | 192 EXPECT_TRUE(auais->started()); |
190 | 193 |
191 // Stop(), Stop() is a valid calling sequence (second call does nothing). | 194 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
192 ais->Stop(); | 195 ais->Stop(); |
193 EXPECT_FALSE(auais->started()); | 196 EXPECT_FALSE(auais->started()); |
194 ais->Stop(); | 197 ais->Stop(); |
195 EXPECT_FALSE(auais->started()); | 198 EXPECT_FALSE(auais->started()); |
196 | 199 |
197 EXPECT_CALL(sink, OnClose(ais)) | 200 EXPECT_CALL(sink, OnClose(ais)) |
198 .Times(1); | 201 .Times(1); |
199 ais->Close(); | 202 ais->Close(); |
200 } | 203 } |
201 | 204 |
202 // Verify that recording starts and stops correctly in mono using mocked sink. | 205 // Verify that recording starts and stops correctly in mono using mocked sink. |
203 TEST(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { | 206 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyMonoRecording) { |
204 if (!CanRunAudioTests()) | 207 if (!CanRunAudioTests()) |
205 return; | 208 return; |
206 | 209 |
207 // Create an audio input stream which records in mono. | 210 // Create an audio input stream which records in mono. |
208 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); | 211 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_MONO); |
209 EXPECT_TRUE(ais->Open()); | 212 EXPECT_TRUE(ais->Open()); |
210 | 213 |
211 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 214 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
212 int samples_per_packet = fs / 100; | 215 int samples_per_packet = fs / 100; |
213 int bits_per_sample = 16; | 216 int bits_per_sample = 16; |
(...skipping 13 matching lines...) Expand all Loading... | |
227 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | 230 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
228 ais->Stop(); | 231 ais->Stop(); |
229 | 232 |
230 // Verify that the sink receieves OnClose() call when calling Close(). | 233 // Verify that the sink receieves OnClose() call when calling Close(). |
231 EXPECT_CALL(sink, OnClose(ais)) | 234 EXPECT_CALL(sink, OnClose(ais)) |
232 .Times(1); | 235 .Times(1); |
233 ais->Close(); | 236 ais->Close(); |
234 } | 237 } |
235 | 238 |
236 // Verify that recording starts and stops correctly in mono using mocked sink. | 239 // Verify that recording starts and stops correctly in mono using mocked sink. |
237 TEST(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { | 240 TEST_F(MacAudioInputTest, AUAudioInputStreamVerifyStereoRecording) { |
238 if (!CanRunAudioTests()) | 241 if (!CanRunAudioTests()) |
239 return; | 242 return; |
240 | 243 |
241 // Create an audio input stream which records in stereo. | 244 // Create an audio input stream which records in stereo. |
242 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); | 245 AudioInputStream* ais = CreateAudioInputStream(CHANNEL_LAYOUT_STEREO); |
243 EXPECT_TRUE(ais->Open()); | 246 EXPECT_TRUE(ais->Open()); |
244 | 247 |
245 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 248 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
246 int samples_per_packet = fs / 100; | 249 int samples_per_packet = fs / 100; |
247 int bits_per_sample = 16; | 250 int bits_per_sample = 16; |
(...skipping 18 matching lines...) Expand all Loading... | |
266 .Times(1); | 269 .Times(1); |
267 ais->Close(); | 270 ais->Close(); |
268 } | 271 } |
269 | 272 |
270 // 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 |
271 // 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. |
272 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | 275 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
273 // 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 |
274 // 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 |
275 // environment variable to a value greater than 0. | 278 // environment variable to a value greater than 0. |
276 TEST(MacAudioInputTest, DISABLED_AUAudioInputStreamRecordToFile) { | 279 TEST_F(MacAudioInputTest, DISABLED_AUAudioInputStreamRecordToFile) { |
277 if (!CanRunAudioTests()) | 280 if (!CanRunAudioTests()) |
278 return; | 281 return; |
279 const char* file_name = "out_stereo_10sec.pcm"; | 282 const char* file_name = "out_stereo_10sec.pcm"; |
280 | 283 |
281 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); | 284 int fs = static_cast<int>(AUAudioInputStream::HardwareSampleRate()); |
282 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 285 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
283 EXPECT_TRUE(ais->Open()); | 286 EXPECT_TRUE(ais->Open()); |
284 | 287 |
285 fprintf(stderr, " File name : %s\n", file_name); | 288 fprintf(stderr, " File name : %s\n", file_name); |
286 fprintf(stderr, " Sample rate: %d\n", fs); | 289 fprintf(stderr, " Sample rate: %d\n", fs); |
287 WriteToFileAudioSink file_sink(file_name); | 290 WriteToFileAudioSink file_sink(file_name); |
288 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 291 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
289 ais->Start(&file_sink); | 292 ais->Start(&file_sink); |
290 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | 293 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); |
291 ais->Stop(); | 294 ais->Stop(); |
292 fprintf(stderr, " >> Recording has stopped.\n"); | 295 fprintf(stderr, " >> Recording has stopped.\n"); |
293 ais->Close(); | 296 ais->Close(); |
294 } | 297 } |
295 | 298 |
OLD | NEW |