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 "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 sample_rate_ = 48000; |
| 117 samples_per_packet_ = sample_rate_ / 100; |
| 118 } |
| 119 |
| 120 ~AudioInputStreamWrapper() {} |
| 121 |
| 122 // Creates AudioInputStream object using default parameters. |
| 123 AudioInputStream* Create() { |
| 124 return CreateInputStream(); |
| 125 } |
| 126 |
| 127 // Creates AudioInputStream object using non-default parameters where the |
| 128 // frame size is modified. |
| 129 AudioInputStream* Create(int samples_per_packet) { |
| 130 samples_per_packet_ = samples_per_packet; |
| 131 return CreateInputStream(); |
| 132 } |
| 133 |
| 134 AudioParameters::Format format() const { return format_; } |
| 135 int channels() const { |
| 136 return ChannelLayoutToChannelCount(channel_layout_); |
| 137 } |
| 138 int bits_per_sample() const { return bits_per_sample_; } |
| 139 int sample_rate() const { return sample_rate_; } |
| 140 int samples_per_packet() const { return samples_per_packet_; } |
| 141 |
| 142 private: |
| 143 AudioInputStream* CreateInputStream() { |
| 144 AudioInputStream* ais = audio_man_->MakeAudioInputStream( |
| 145 AudioParameters(format_, channel_layout_, sample_rate_, |
| 146 bits_per_sample_, samples_per_packet_)); |
| 147 EXPECT_TRUE(ais); |
| 148 return ais; |
| 149 } |
| 150 |
| 151 ScopedCOMInitializer com_init_; |
| 152 AudioManager* audio_man_; |
| 153 AudioParameters::Format format_; |
| 154 ChannelLayout channel_layout_; |
| 155 int bits_per_sample_; |
| 156 int sample_rate_; |
| 157 int samples_per_packet_; |
| 158 }; |
| 159 |
| 160 // Convenience method which creates a default AudioInputStream object. |
| 161 static AudioInputStream* CreateDefaultAudioInputStream() { |
| 162 AudioInputStreamWrapper aisw; |
| 163 AudioInputStream* ais = aisw.Create(); |
| 164 return ais; |
| 165 } |
| 166 |
| 167 // Verify that we can retrieve the current hardware/mixing sample rate |
| 168 // for all supported device roles. The ERole enumeration defines constants |
| 169 // that indicate the role that the system/user has assigned to an audio |
| 170 // endpoint device. |
| 171 // TODO(henrika): modify this test when we suport full device enumeration. |
| 172 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { |
| 173 if (!CanRunAudioTests()) |
| 174 return; |
| 175 |
| 176 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); |
| 177 |
| 178 // Default device intended for games, system notification sounds, |
| 179 // and voice commands. |
| 180 int fs = static_cast<int>( |
| 181 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); |
| 182 EXPECT_GE(fs, 0); |
| 183 |
| 184 // Default communication device intended for e.g. VoIP communication. |
| 185 fs = static_cast<int>( |
| 186 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); |
| 187 EXPECT_GE(fs, 0); |
| 188 |
| 189 // Multimedia device for music, movies and live music recording. |
| 190 fs = static_cast<int>( |
| 191 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); |
| 192 EXPECT_GE(fs, 0); |
| 193 } |
| 194 |
| 195 // Test Create(), Close() calling sequence. |
| 196 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { |
| 197 if (!CanRunAudioTests()) |
| 198 return; |
| 199 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
| 200 ais->Close(); |
| 201 } |
| 202 |
| 203 // Test Open(), Close() calling sequence. |
| 204 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { |
| 205 if (!CanRunAudioTests()) |
| 206 return; |
| 207 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
| 208 EXPECT_TRUE(ais->Open()); |
| 209 ais->Close(); |
| 210 } |
| 211 |
| 212 // Test Open(), Start(), Close() calling sequence. |
| 213 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { |
| 214 if (!CanRunAudioTests()) |
| 215 return; |
| 216 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
| 217 EXPECT_TRUE(ais->Open()); |
| 218 MockAudioInputCallback sink; |
| 219 ais->Start(&sink); |
| 220 EXPECT_CALL(sink, OnClose(ais)) |
| 221 .Times(1); |
| 222 ais->Close(); |
| 223 } |
| 224 |
| 225 // Test Open(), Start(), Stop(), Close() calling sequence. |
| 226 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { |
| 227 if (!CanRunAudioTests()) |
| 228 return; |
| 229 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
| 230 EXPECT_TRUE(ais->Open()); |
| 231 MockAudioInputCallback sink; |
| 232 ais->Start(&sink); |
| 233 ais->Stop(); |
| 234 EXPECT_CALL(sink, OnClose(ais)) |
| 235 .Times(1); |
| 236 ais->Close(); |
| 237 } |
| 238 |
| 239 // Test some additional calling sequences. |
| 240 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { |
| 241 if (!CanRunAudioTests()) |
| 242 return; |
| 243 AudioInputStream* ais = CreateDefaultAudioInputStream(); |
| 244 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); |
| 245 |
| 246 // Open(), Open() should fail the second time. |
| 247 EXPECT_TRUE(ais->Open()); |
| 248 EXPECT_FALSE(ais->Open()); |
| 249 |
| 250 MockAudioInputCallback sink; |
| 251 |
| 252 // Start(), Start() is a valid calling sequence (second call does nothing). |
| 253 ais->Start(&sink); |
| 254 EXPECT_TRUE(wais->started()); |
| 255 ais->Start(&sink); |
| 256 EXPECT_TRUE(wais->started()); |
| 257 |
| 258 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
| 259 ais->Stop(); |
| 260 EXPECT_FALSE(wais->started()); |
| 261 ais->Stop(); |
| 262 EXPECT_FALSE(wais->started()); |
| 263 |
| 264 EXPECT_CALL(sink, OnClose(ais)) |
| 265 .Times(1); |
| 266 ais->Close(); |
| 267 } |
| 268 |
| 269 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { |
| 270 if (!CanRunAudioTests()) |
| 271 return; |
| 272 |
| 273 // 10 ms packet size. |
| 274 |
| 275 // Create default WASAPI input stream which records in stereo using |
| 276 // the shared mixing rate. The default buffer size is 10ms. |
| 277 AudioInputStreamWrapper aisw; |
| 278 AudioInputStream* ais = aisw.Create(); |
| 279 EXPECT_TRUE(ais->Open()); |
| 280 |
| 281 MockAudioInputCallback sink; |
| 282 |
| 283 // Derive the expected size in bytes of each recorded packet. |
| 284 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * |
| 285 (aisw.bits_per_sample() / 8); |
| 286 |
| 287 // We use 10ms packets and will run the test for ~100ms. Given that the |
| 288 // startup sequence takes some time, it is reasonable to expect 5-12 |
| 289 // callbacks in this time period. All should contain valid packets of |
| 290 // the same size and a valid delay estimate. |
| 291 EXPECT_CALL(sink, OnData( |
| 292 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) |
| 293 .Times(Between(5, 10)); |
| 294 |
| 295 ais->Start(&sink); |
| 296 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
| 297 ais->Stop(); |
| 298 |
| 299 // Store current packet size (to be used in the subsequent tests). |
| 300 int samples_per_packet_10ms = aisw.samples_per_packet(); |
| 301 |
| 302 EXPECT_CALL(sink, OnClose(ais)) |
| 303 .Times(1); |
| 304 ais->Close(); |
| 305 |
| 306 // 20 ms packet size. |
| 307 |
| 308 ais = aisw.Create(2 * samples_per_packet_10ms); |
| 309 EXPECT_TRUE(ais->Open()); |
| 310 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * |
| 311 (aisw.bits_per_sample() / 8); |
| 312 |
| 313 EXPECT_CALL(sink, OnData( |
| 314 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) |
| 315 .Times(Between(5, 10)); |
| 316 ais->Start(&sink); |
| 317 base::PlatformThread::Sleep(2 * TestTimeouts::tiny_timeout_ms()); |
| 318 ais->Stop(); |
| 319 |
| 320 EXPECT_CALL(sink, OnClose(ais)) |
| 321 .Times(1); |
| 322 ais->Close(); |
| 323 |
| 324 // 5 ms packet size. |
| 325 |
| 326 ais = aisw.Create(samples_per_packet_10ms / 2); |
| 327 EXPECT_TRUE(ais->Open()); |
| 328 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * |
| 329 (aisw.bits_per_sample() / 8); |
| 330 |
| 331 EXPECT_CALL(sink, OnData( |
| 332 ais, NotNull(), bytes_per_packet, Gt(bytes_per_packet))) |
| 333 .Times(Between(2 * 5, 2 * 10)); |
| 334 ais->Start(&sink); |
| 335 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout_ms()); |
| 336 ais->Stop(); |
| 337 |
| 338 EXPECT_CALL(sink, OnClose(ais)) |
| 339 .Times(1); |
| 340 ais->Close(); |
| 341 } |
| 342 |
| 343 // This test is intended for manual tests and should only be enabled |
| 344 // when it is required to store the captured data on a local file. |
| 345 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
| 346 // To include disabled tests in test execution, just invoke the test program |
| 347 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS |
| 348 // environment variable to a value greater than 0. |
| 349 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { |
| 350 if (!CanRunAudioTests()) |
| 351 return; |
| 352 |
| 353 const char* file_name = "out_stereo_10sec.pcm"; |
| 354 |
| 355 AudioInputStreamWrapper aisw; |
| 356 AudioInputStream* ais = aisw.Create(); |
| 357 EXPECT_TRUE(ais->Open()); |
| 358 |
| 359 fprintf(stderr, " File name : %s\n", file_name); |
| 360 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); |
| 361 WriteToFileAudioSink file_sink(file_name); |
| 362 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
| 363 ais->Start(&file_sink); |
| 364 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); |
| 365 ais->Stop(); |
| 366 fprintf(stderr, " >> Recording has stopped.\n"); |
| 367 ais->Close(); |
| 368 } |
OLD | NEW |