Chromium Code Reviews
|
| 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; | |
| 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, | |
|
tommi (sloooow) - chröme
2011/10/14 14:31:04
indent
henrika (OOO until Aug 14)
2011/10/17 12:08:24
Done.
| |
| 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), | |
| 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, | |
|
tommi (sloooow) - chröme
2011/10/14 14:31:04
strange indentation in this function
henrika (OOO until Aug 14)
2011/10/17 12:08:24
Done.
| |
| 67 const uint8* src, uint32 size, | |
| 68 uint32 hardware_delay_bytes) { | |
| 69 // Store data data in a temporary buffer to avoid making blocking | |
| 70 // fwrite() calls in the audio callback. The complete buffer will be | |
| 71 // written to file in the destructor. | |
| 72 if (buffer_.Append(src, size)) { | |
| 73 bytes_to_write_ += size; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 virtual void OnClose(AudioInputStream* stream) {} | |
| 78 virtual void OnError(AudioInputStream* stream, int code) {} | |
| 79 | |
| 80 private: | |
| 81 media::SeekableBuffer buffer_; | |
| 82 FILE* file_; | |
| 83 size_t bytes_to_write_; | |
| 84 }; | |
| 85 | |
| 86 // Convenience method which ensures that we are not running on the build | |
| 87 // bots and that at least one valid input device can be found. | |
| 88 static bool CanRunAudioTests() { | |
| 89 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 90 if (env->HasVar("CHROME_HEADLESS")) | |
| 91 return false; | |
| 92 AudioManager* audio_man = AudioManager::GetAudioManager(); | |
| 93 if (NULL == audio_man) | |
| 94 return false; | |
| 95 // TODO(henrika): note that we use Wave today to query the number of | |
| 96 // existing input devices. | |
| 97 return audio_man->HasAudioInputDevices(); | |
| 98 } | |
| 99 | |
| 100 // Convenience method which creates a default AudioInputStream object but | |
| 101 // also allows the user to modify the default settings. | |
| 102 class AudioInputStreamWrapper { | |
| 103 public: | |
| 104 AudioInputStreamWrapper() | |
| 105 : audio_man_(AudioManager::GetAudioManager()), | |
| 106 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | |
| 107 channel_layout_(CHANNEL_LAYOUT_STEREO), | |
| 108 bits_per_sample_(16) { | |
| 109 // Use native/mixing sample rate and 10ms frame size as default. | |
| 110 sample_rate_ = static_cast<int>( | |
| 111 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | |
| 112 samples_per_packet_ = sample_rate_ / 100; | |
| 113 } | |
| 114 | |
| 115 ~AudioInputStreamWrapper() { } | |
| 116 | |
| 117 // Creates AudioInputStream object using default parameters. | |
| 118 AudioInputStream* Create() { | |
| 119 return CreateInputStream(); | |
| 120 } | |
| 121 | |
| 122 // Creates AudioInputStream object using non-default parameters where the | |
| 123 // frame size is modified. | |
| 124 AudioInputStream* Create(int samples_per_packet) { | |
| 125 samples_per_packet_ = samples_per_packet; | |
| 126 return CreateInputStream(); | |
| 127 } | |
| 128 | |
| 129 AudioParameters::Format format() const { return format_; } | |
| 130 int channels() const { | |
| 131 return ChannelLayoutToChannelCount(channel_layout_); | |
| 132 } | |
| 133 int bits_per_sample() const { return bits_per_sample_; } | |
| 134 int sample_rate() const { return sample_rate_; } | |
| 135 int samples_per_packet() const { return samples_per_packet_; } | |
| 136 | |
| 137 private: | |
| 138 AudioInputStream* CreateInputStream() { | |
| 139 AudioInputStream* ais = audio_man_->MakeAudioInputStream( | |
| 140 AudioParameters(format_, channel_layout_, sample_rate_, | |
| 141 bits_per_sample_, samples_per_packet_)); | |
| 142 EXPECT_TRUE(ais); | |
| 143 return ais; | |
| 144 } | |
| 145 | |
| 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 // Default device intended for games, system notification sounds, | |
| 171 // and voice commands. | |
| 172 int fs = static_cast<int>( | |
| 173 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | |
| 174 EXPECT_GE(fs, 0); | |
| 175 | |
| 176 // Default communication device intended for e.g. VoIP communication. | |
| 177 fs = static_cast<int>( | |
| 178 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); | |
| 179 EXPECT_GE(fs, 0); | |
| 180 | |
| 181 // Multimedia device for music, movies and live music recording. | |
| 182 fs = static_cast<int>( | |
| 183 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); | |
| 184 EXPECT_GE(fs, 0); | |
| 185 } | |
| 186 | |
| 187 // Test Create(), Close() calling sequence. | |
| 188 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { | |
| 189 if (!CanRunAudioTests()) | |
| 190 return; | |
| 191 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
| 192 ais->Close(); | |
| 193 } | |
| 194 | |
| 195 // Test Open(), Close() calling sequence. | |
| 196 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { | |
| 197 if (!CanRunAudioTests()) | |
| 198 return; | |
| 199 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
| 200 EXPECT_TRUE(ais->Open()); | |
| 201 ais->Close(); | |
| 202 } | |
| 203 | |
| 204 // Test Open(), Start(), Close() calling sequence. | |
| 205 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { | |
| 206 if (!CanRunAudioTests()) | |
| 207 return; | |
| 208 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
| 209 EXPECT_TRUE(ais->Open()); | |
| 210 MockAudioInputCallback sink; | |
| 211 ais->Start(&sink); | |
| 212 EXPECT_CALL(sink, OnClose(ais)) | |
| 213 .Times(1); | |
| 214 ais->Close(); | |
| 215 } | |
| 216 | |
| 217 // Test Open(), Start(), Stop(), Close() calling sequence. | |
| 218 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { | |
| 219 if (!CanRunAudioTests()) | |
| 220 return; | |
| 221 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
| 222 EXPECT_TRUE(ais->Open()); | |
| 223 MockAudioInputCallback sink; | |
| 224 ais->Start(&sink); | |
| 225 ais->Stop(); | |
| 226 EXPECT_CALL(sink, OnClose(ais)) | |
| 227 .Times(1); | |
| 228 ais->Close(); | |
| 229 } | |
| 230 | |
| 231 // Test some additional calling sequences. | |
| 232 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { | |
| 233 if (!CanRunAudioTests()) | |
| 234 return; | |
| 235 AudioInputStream* ais = CreateDefaultAudioInputStream(); | |
| 236 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); | |
| 237 | |
| 238 // Open(), Open() should fail the second time. | |
| 239 EXPECT_TRUE(ais->Open()); | |
| 240 EXPECT_FALSE(ais->Open()); | |
| 241 | |
| 242 MockAudioInputCallback sink; | |
| 243 | |
| 244 // Start(), Start() is a valid calling sequence (second call does nothing). | |
| 245 ais->Start(&sink); | |
| 246 EXPECT_TRUE(wais->started()); | |
| 247 ais->Start(&sink); | |
| 248 EXPECT_TRUE(wais->started()); | |
| 249 | |
| 250 // Stop(), Stop() is a valid calling sequence (second call does nothing). | |
| 251 ais->Stop(); | |
| 252 EXPECT_FALSE(wais->started()); | |
| 253 ais->Stop(); | |
| 254 EXPECT_FALSE(wais->started()); | |
| 255 | |
| 256 EXPECT_CALL(sink, OnClose(ais)) | |
| 257 .Times(1); | |
| 258 ais->Close(); | |
| 259 } | |
| 260 | |
| 261 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { | |
| 262 if (!CanRunAudioTests()) | |
| 263 return; | |
| 264 | |
| 265 // 10 ms packet size. | |
| 266 | |
| 267 // Create default WASAPI input stream which records in stereo using | |
| 268 // the shared mixing rate. The default buffer size is 10ms. | |
| 269 AudioInputStreamWrapper aisw; | |
| 270 AudioInputStream* ais = aisw.Create(); | |
| 271 EXPECT_TRUE(ais->Open()); | |
| 272 | |
| 273 MockAudioInputCallback sink; | |
| 274 | |
| 275 // Derive the expected size in bytes of each recorded packet. | |
| 276 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
| 277 (aisw.bits_per_sample() / 8); | |
|
tommi (sloooow) - chröme
2011/10/14 14:31:04
indent
henrika (OOO until Aug 14)
2011/10/17 12:08:24
Done.
| |
| 278 | |
| 279 // We use 10ms packets and will run the test for ~100ms. Given that the | |
| 280 // startup sequence takes some time, it is reasonable to expect 5-12 | |
| 281 // callbacks in this time period. All should contain valid packets of | |
| 282 // the same size and a valid delay estimate. | |
| 283 EXPECT_CALL(sink, OnData( | |
| 284 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
| 285 .Times(Between(5, 10)); | |
| 286 | |
| 287 ais->Start(&sink); | |
| 288 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | |
| 289 ais->Stop(); | |
| 290 | |
| 291 // Store current packet size (to be used in the subsequent tests). | |
| 292 int samples_per_packet_10ms = aisw.samples_per_packet(); | |
| 293 | |
| 294 EXPECT_CALL(sink, OnClose(ais)) | |
| 295 .Times(1); | |
| 296 ais->Close(); | |
| 297 | |
| 298 // 20 ms packet size. | |
| 299 | |
| 300 ais = aisw.Create(2 * samples_per_packet_10ms); | |
| 301 EXPECT_TRUE(ais->Open()); | |
| 302 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
| 303 (aisw.bits_per_sample() / 8); | |
| 304 | |
| 305 EXPECT_CALL(sink, OnData( | |
| 306 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
| 307 .Times(Between(5, 10)); | |
| 308 ais->Start(&sink); | |
| 309 base::PlatformThread::Sleep(2 * TestTimeouts::tiny_timeout_ms()); | |
| 310 ais->Stop(); | |
| 311 | |
| 312 EXPECT_CALL(sink, OnClose(ais)) | |
| 313 .Times(1); | |
| 314 ais->Close(); | |
| 315 | |
| 316 // 5 ms packet size. | |
| 317 | |
| 318 ais = aisw.Create(samples_per_packet_10ms / 2); | |
| 319 EXPECT_TRUE(ais->Open()); | |
| 320 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | |
| 321 (aisw.bits_per_sample() / 8); | |
| 322 | |
| 323 EXPECT_CALL(sink, OnData( | |
| 324 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) | |
| 325 .Times(Between(2 * 5, 2 * 10)); | |
| 326 ais->Start(&sink); | |
| 327 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); | |
| 328 ais->Stop(); | |
| 329 | |
| 330 EXPECT_CALL(sink, OnClose(ais)) | |
| 331 .Times(1); | |
|
tommi (sloooow) - chröme
2011/10/14 14:31:04
indent
henrika (OOO until Aug 14)
2011/10/17 12:08:24
Done.
| |
| 332 ais->Close(); | |
| 333 } | |
| 334 | |
| 335 // This test is intended for manual tests and should only be enabled | |
| 336 // when it is required to store the captured data on a local file. | |
| 337 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. | |
| 338 // To include disabled tests in test execution, just invoke the test program | |
| 339 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS | |
| 340 // environment variable to a value greater than 0. | |
| 341 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { | |
| 342 if (!CanRunAudioTests()) | |
| 343 return; | |
| 344 | |
| 345 const char* file_name = "out_stereo_10sec.pcm"; | |
| 346 | |
| 347 AudioInputStreamWrapper aisw; | |
| 348 AudioInputStream* ais = aisw.Create(); | |
| 349 EXPECT_TRUE(ais->Open()); | |
| 350 | |
| 351 fprintf(stderr, " File name : %s\n", file_name); | |
| 352 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); | |
| 353 WriteToFileAudioSink file_sink(file_name); | |
| 354 fprintf(stderr, " >> Speak into the mic while recording...\n"); | |
| 355 ais->Start(&file_sink); | |
| 356 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | |
| 357 ais->Stop(); | |
| 358 fprintf(stderr, " >> Recording has stopped.\n"); | |
| 359 ais->Close(); | |
| 360 } | |
| OLD | NEW |