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

Side by Side Diff: media/audio/win/audio_low_latency_input_win_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698