|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <windows.h> | |
6 #include <mmsystem.h> | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/environment.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/test/test_timeouts.h" | |
12 #include "media/audio/audio_io.h" | |
13 #include "media/audio/audio_manager.h" | |
14 #include "media/audio/win/audio_low_latency_input_win.h" | |
15 #include "media/base/seekable_buffer.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using ::testing::Gt; | |
20 using ::testing::AnyNumber; | |
scherkus (not reviewing)
2011/10/18 21:12:30
A->Z sorting
henrika (OOO until Aug 14)
2011/10/19 15:42:43
Done.
| |
21 using ::testing::Between; | |
22 using ::testing::NotNull; | |
23 | |
24 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { | |
25 public: | |
26 MOCK_METHOD4(OnData, void(AudioInputStream* stream, | |
27 const uint8* src, uint32 size, | |
28 uint32 hardware_delay_bytes)); | |
29 MOCK_METHOD1(OnClose, void(AudioInputStream* stream)); | |
30 MOCK_METHOD2(OnError, void(AudioInputStream* stream, int code)); | |
31 }; | |
32 | |
33 // This audio sink implementation should be used for manual tests only since | |
34 // the recorded data is stored on a raw binary data file. | |
35 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { | |
36 public: | |
37 // Allocate space for ~10 seconds of data @ 48kHz in stereo: | |
38 // 2 bytes per sample, 2 channels, 10ms @ 48kHz, 10 seconds <=> 1920000 bytes. | |
39 static const size_t kMaxBufferSize = 2 * 2 * 480 * 100 * 10; | |
40 | |
41 explicit WriteToFileAudioSink(const char* file_name) | |
42 : buffer_(0, kMaxBufferSize), | |
scherkus (not reviewing)
2011/10/18 21:12:30
indent 2 more spaces
henrika (OOO until Aug 14)
2011/10/19 15:42:43
Done.
| |
43 file_(fopen(file_name, "wb")), | |
44 bytes_to_write_(0) { | |
45 } | |
46 | |
47 virtual ~WriteToFileAudioSink() { | |
48 size_t bytes_written = 0; | |
49 while (bytes_written < bytes_to_write_) { | |
50 const uint8* chunk; | |
51 size_t chunk_size; | |
52 | |
53 // Stop writing if no more data is available. | |
54 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) | |
55 break; | |
56 | |
57 // Write recorded data chunk to the file and prepare for next chunk. | |
58 fwrite(chunk, 1, chunk_size, file_); | |
59 buffer_.Seek(chunk_size); | |
60 bytes_written += chunk_size; | |
61 } | |
62 fclose(file_); | |
63 } | |
64 | |
65 // AudioInputStream::AudioInputCallback implementation. | |
66 virtual void OnData(AudioInputStream* stream, | |
67 const uint8* src, uint32 size, uint32 hardware_delay_bytes) { | |
scherkus (not reviewing)
2011/10/18 21:12:30
these parameters should be aligned at the (
henrika (OOO until Aug 14)
2011/10/19 15:42:43
Done.
| |
68 // Store data data in a temporary buffer to avoid making blocking | |
scherkus (not reviewing)
2011/10/18 21:12:30
de-indent block of code by 2 spaces
henrika (OOO until Aug 14)
2011/10/19 15:42:43
Done.
| |
69 // fwrite() calls in the audio callback. The complete buffer will be | |
70 // written to file in the destructor. | |
71 if (buffer_.Append(src, size)) { | |
72 bytes_to_write_ += size; | |
73 } | |
74 } | |
75 | |
76 virtual void OnClose(AudioInputStream* stream) {} | |
77 virtual void OnError(AudioInputStream* stream, int code) {} | |
78 | |
79 private: | |
80 media::SeekableBuffer buffer_; | |
81 FILE* file_; | |
82 size_t bytes_to_write_; | |
83 }; | |
84 | |
85 // Convenience method which ensures that we are not running on the build | |
86 // bots and that at least one valid input device can be found. | |
87 static bool CanRunAudioTests() { | |
88 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
89 if (env->HasVar("CHROME_HEADLESS")) | |
90 return false; | |
91 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
92 if (NULL == audio_man) | |
93 return false; | |
94 // TODO(henrika): note that we use Wave today to query the number of | |
95 // existing input devices. | |
96 return audio_man->HasAudioInputDevices(); | |
97 } | |
98 | |
99 // Convenience method which creates a default AudioInputStream object but | |
100 // also allows the user to modify the default settings. | |
101 class AudioInputStreamWrapper { | |
102 public: | |
103 AudioInputStreamWrapper() | |
104 : audio_man_(AudioManager::GetAudioManager()), | |
105 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | |
106 channel_layout_(CHANNEL_LAYOUT_STEREO), | |
107 bits_per_sample_(16) { | |
108 // Use native/mixing sample rate and 10ms frame size as default. | |
109 sample_rate_ = static_cast<int>( | |
110 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | |
111 samples_per_packet_ = sample_rate_ / 100; | |
112 } | |
113 | |
114 ~AudioInputStreamWrapper() { } | |
scherkus (not reviewing)
2011/10/18 21:12:30
nit: { } -> {}
henrika (OOO until Aug 14)
2011/10/19 15:42:43
Done.
| |
115 | |
116 // Creates AudioInputStream object using default parameters. | |
117 AudioInputStream* Create() { | |
118 return CreateInputStream(); | |
119 } | |
120 | |
121 // Creates AudioInputStream object using non-default parameters where the | |
122 // frame size is modified. | |
123 AudioInputStream* Create(int samples_per_packet) { | |
124 samples_per_packet_ = samples_per_packet; | |
125 return CreateInputStream(); | |
126 } | |
127 | |
128 AudioParameters::Format format() const { return format_; } | |
129 int channels() const { | |
130 return ChannelLayoutToChannelCount(channel_layout_); | |
131 } | |
132 int bits_per_sample() const { return bits_per_sample_; } | |
133 int sample_rate() const { return sample_rate_; } | |
134 int samples_per_packet() const { return samples_per_packet_; } | |
135 | |
136 private: | |
137 AudioInputStream* CreateInputStream() { | |
138 AudioInputStream* ais = audio_man_->MakeAudioInputStream( | |
139 AudioParameters(format_, channel_layout_, sample_rate_, | |
140 bits_per_sample_, samples_per_packet_)); | |
141 EXPECT_TRUE(ais); | |
142 return ais; | |
143 } | |
144 | |
145 ScopedCOMInitializerMTA com_init_; | |
146 AudioManager* audio_man_; | |
147 AudioParameters::Format format_; | |
148 ChannelLayout channel_layout_; | |
149 int bits_per_sample_; | |
150 int sample_rate_; | |
151 int samples_per_packet_; | |
152 }; | |
153 | |
154 // Convenience method which creates a default AudioInputStream object. | |
155 static AudioInputStream* CreateDefaultAudioInputStream() { | |
156 AudioInputStreamWrapper aisw; | |
157 AudioInputStream* ais = aisw.Create(); | |
158 return ais; | |
159 } | |
160 | |
161 // Verify that we can retrieve the current hardware/mixing sample rate | |
162 // for all supported device roles. The ERole enumeration defines constants | |
163 // that indicate the role that the system/user has assigned to an audio | |
164 // endpoint device. | |
165 // TODO(henrika): modify this test when we suport full device enumeration. | |
166 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { | |
167 if (!CanRunAudioTests()) | |
168 return; | |
169 | |
170 ScopedCOMInitializerMTA com_init; | |
171 | |
172 // Default device intended for games, system notification sounds, | |
173 // and voice commands. | |
174 int fs = static_cast<int>( | |
175 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | |
176 EXPECT_GE(fs, 0); | |
177 | |
178 // Default communication device intended for e.g. VoIP communication. | |
179 fs = static_cast<int>( | |
180 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); | |
181 EXPECT_GE(fs, 0); | |
182 | |
183 // Multimedia device for music, movies and live music recording. | |
184 fs = static_cast<int>( | |
185 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); | |
186 EXPECT_GE(fs, 0); | |
187 } | |
188 | |
189 // Test Create(), Close() calling sequence. | |
190 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { | |
191 if (!CanRunAudioTests()) | |
192 return; | |
193 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
194 ais->Close(); | |
195 } | |
196 | |
197 // Test Open(), Close() calling sequence. | |
198 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { | |
199 if (!CanRunAudioTests()) | |
200 return; | |
201 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
202 EXPECT_TRUE(ais->Open()); | |
203 ais->Close(); | |
204 } | |
205 | |
206 // Test Open(), Start(), Close() calling sequence. | |
207 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { | |
208 if (!CanRunAudioTests()) | |
209 return; | |
210 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
211 EXPECT_TRUE(ais->Open()); | |
212 MockAudioInputCallback sink; | |
213 ais->Start(&sink); | |
214 EXPECT_CALL(sink, OnClose(ais)) | |
215 .Times(1); | |
216 ais->Close(); | |
217 } | |
218 | |
219 // Test Open(), Start(), Stop(), Close() calling sequence. | |
220 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { | |
221 if (!CanRunAudioTests()) | |
222 return; | |
223 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
224 EXPECT_TRUE(ais->Open()); | |
225 MockAudioInputCallback sink; | |
226 ais->Start(&sink); | |
227 ais->Stop(); | |
228 EXPECT_CALL(sink, OnClose(ais)) | |
229 .Times(1); | |
230 ais->Close(); | |
231 } | |
232 | |
233 // Test some additional calling sequences. | |
234 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { | |
235 if (!CanRunAudioTests()) | |
236 return; | |
237 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
238 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); | |
239 | |
240 // Open(), Open() should fail the second time. | |
241 EXPECT_TRUE(ais->Open()); | |
242 EXPECT_FALSE(ais->Open()); | |
243 | |
244 MockAudioInputCallback sink; | |
245 | |
246 // Start(), Start() is a valid calling sequence (second call does nothing). | |
247 ais->Start(&sink); | |
248 EXPECT_TRUE(wais->started()); | |
249 ais->Start(&sink); | |
250 EXPECT_TRUE(wais->started()); | |
251 | |
252 // Stop(), Stop() is a valid calling sequence (second call does nothing). | |
253 ais->Stop(); | |
254 EXPECT_FALSE(wais->started()); | |
255 ais->Stop(); | |
256 EXPECT_FALSE(wais->started()); | |
257 | |
258 EXPECT_CALL(sink, OnClose(ais)) | |
259 .Times(1); | |
260 ais->Close(); | |
261 } | |
262 | |
263 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { | |
264 if (!CanRunAudioTests()) | |
265 return; | |
266 | |
267 // 10 ms packet size. | |
268 | |
269 // Create default WASAPI input stream which records in stereo using | |
270 // the shared mixing rate. The default buffer size is 10ms. | |
271 AudioInputStreamWrapper aisw; | |
272 AudioInputStream* ais = aisw.Create(); | |
273 EXPECT_TRUE(ais->Open()); | |
274 | |
275 MockAudioInputCallback sink; | |
276 | |
277 // Derive the expected size in bytes of each recorded packet. | |
278 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
279 (aisw.bits_per_sample() / 8); | |
280 | |
281 // We use 10ms packets and will run the test for ~100ms. Given that the | |
282 // startup sequence takes some time, it is reasonable to expect 5-12 | |
283 // callbacks in this time period. All should contain valid packets of | |
284 // the same size and a valid delay estimate. | |
285 EXPECT_CALL(sink, OnData( | |
286 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
287 .Times(Between(5, 10)); | |
288 | |
289 ais->Start(&sink); | |
290 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | |
291 ais->Stop(); | |
292 | |
293 // Store current packet size (to be used in the subsequent tests). | |
294 int samples_per_packet_10ms = aisw.samples_per_packet(); | |
295 | |
296 EXPECT_CALL(sink, OnClose(ais)) | |
297 .Times(1); | |
298 ais->Close(); | |
299 | |
300 // 20 ms packet size. | |
301 | |
302 ais = aisw.Create(2 * samples_per_packet_10ms); | |
303 EXPECT_TRUE(ais->Open()); | |
304 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
305 (aisw.bits_per_sample() / 8); | |
306 | |
307 EXPECT_CALL(sink, OnData( | |
308 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
309 .Times(Between(5, 10)); | |
310 ais->Start(&sink); | |
311 base::PlatformThread::Sleep(2 * TestTimeouts::tiny_timeout_ms()); | |
312 ais->Stop(); | |
313 | |
314 EXPECT_CALL(sink, OnClose(ais)) | |
315 .Times(1); | |
316 ais->Close(); | |
317 | |
318 // 5 ms packet size. | |
319 | |
320 ais = aisw.Create(samples_per_packet_10ms / 2); | |
321 EXPECT_TRUE(ais->Open()); | |
322 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
323 (aisw.bits_per_sample() / 8); | |
324 | |
325 EXPECT_CALL(sink, OnData( | |
326 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
327 .Times(Between(2 * 5, 2 * 10)); | |
328 ais->Start(&sink); | |
329 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | |
330 ais->Stop(); | |
331 | |
332 EXPECT_CALL(sink, OnClose(ais)) | |
333 .Times(1); | |
334 ais->Close(); | |
335 } | |
336 | |
337 // This test is intended for manual tests and should only be enabled | |
338 // when it is required to store the captured data on a local file. | |
339 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | |
340 // To include disabled tests in test execution, just invoke the test program | |
341 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS | |
342 // environment variable to a value greater than 0. | |
343 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { | |
344 if (!CanRunAudioTests()) | |
345 return; | |
346 | |
347 const char* file_name = "out_stereo_10sec.pcm"; | |
348 | |
349 AudioInputStreamWrapper aisw; | |
350 AudioInputStream* ais = aisw.Create(); | |
351 EXPECT_TRUE(ais->Open()); | |
352 | |
353 fprintf(stderr, " File name : %s\n", file_name); | |
354 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); | |
355 WriteToFileAudioSink file_sink(file_name); | |
356 fprintf(stderr, " >> Speak into the mic while recording...\n"); | |
357 ais->Start(&file_sink); | |
358 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | |
359 ais->Stop(); | |
360 fprintf(stderr, " >> Recording has stopped.\n"); | |
361 ais->Close(); | |
362 } | |
OLD | NEW |