| 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 <windows.h> | 5 #include <windows.h> |
| 6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 virtual void OnError(AudioInputStream* stream, int code) {} | 81 virtual void OnError(AudioInputStream* stream, int code) {} |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 media::SeekableBuffer buffer_; | 84 media::SeekableBuffer buffer_; |
| 85 FILE* file_; | 85 FILE* file_; |
| 86 size_t bytes_to_write_; | 86 size_t bytes_to_write_; |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 // Convenience method which ensures that we are not running on the build | 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. | 90 // bots and that at least one valid input device can be found. |
| 91 static bool CanRunAudioTests() { | 91 static bool CanRunAudioTests(AudioManager* audio_man) { |
| 92 if (NULL == audio_man) |
| 93 return false; |
| 94 |
| 92 scoped_ptr<base::Environment> env(base::Environment::Create()); | 95 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 93 if (env->HasVar("CHROME_HEADLESS")) | 96 if (env->HasVar("CHROME_HEADLESS")) |
| 94 return false; | 97 return false; |
| 95 AudioManager* audio_man = AudioManager::GetAudioManager(); | 98 |
| 96 if (NULL == audio_man) | |
| 97 return false; | |
| 98 // TODO(henrika): note that we use Wave today to query the number of | 99 // TODO(henrika): note that we use Wave today to query the number of |
| 99 // existing input devices. | 100 // existing input devices. |
| 100 return audio_man->HasAudioInputDevices(); | 101 return audio_man->HasAudioInputDevices(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 // Convenience method which creates a default AudioInputStream object but | 104 // Convenience method which creates a default AudioInputStream object but |
| 104 // also allows the user to modify the default settings. | 105 // also allows the user to modify the default settings. |
| 105 class AudioInputStreamWrapper { | 106 class AudioInputStreamWrapper { |
| 106 public: | 107 public: |
| 107 AudioInputStreamWrapper() | 108 explicit AudioInputStreamWrapper(AudioManager* audio_manager) |
| 108 : com_init_(ScopedCOMInitializer::kMTA), | 109 : com_init_(ScopedCOMInitializer::kMTA), |
| 109 audio_man_(AudioManager::GetAudioManager()), | 110 audio_man_(audio_manager), |
| 110 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), | 111 format_(AudioParameters::AUDIO_PCM_LOW_LATENCY), |
| 111 channel_layout_(CHANNEL_LAYOUT_STEREO), | 112 channel_layout_(CHANNEL_LAYOUT_STEREO), |
| 112 bits_per_sample_(16) { | 113 bits_per_sample_(16) { |
| 113 // Use native/mixing sample rate and 10ms frame size as default. | 114 // Use native/mixing sample rate and 10ms frame size as default. |
| 114 sample_rate_ = static_cast<int>( | 115 sample_rate_ = static_cast<int>( |
| 115 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | 116 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); |
| 116 samples_per_packet_ = sample_rate_ / 100; | 117 samples_per_packet_ = sample_rate_ / 100; |
| 117 } | 118 } |
| 118 | 119 |
| 119 ~AudioInputStreamWrapper() {} | 120 ~AudioInputStreamWrapper() {} |
| (...skipping 22 matching lines...) Expand all Loading... |
| 142 AudioInputStream* CreateInputStream() { | 143 AudioInputStream* CreateInputStream() { |
| 143 AudioInputStream* ais = audio_man_->MakeAudioInputStream( | 144 AudioInputStream* ais = audio_man_->MakeAudioInputStream( |
| 144 AudioParameters(format_, channel_layout_, sample_rate_, | 145 AudioParameters(format_, channel_layout_, sample_rate_, |
| 145 bits_per_sample_, samples_per_packet_), | 146 bits_per_sample_, samples_per_packet_), |
| 146 AudioManagerBase::kDefaultDeviceId); | 147 AudioManagerBase::kDefaultDeviceId); |
| 147 EXPECT_TRUE(ais); | 148 EXPECT_TRUE(ais); |
| 148 return ais; | 149 return ais; |
| 149 } | 150 } |
| 150 | 151 |
| 151 ScopedCOMInitializer com_init_; | 152 ScopedCOMInitializer com_init_; |
| 152 AudioManager* audio_man_; | 153 scoped_refptr<AudioManager> audio_man_; |
| 153 AudioParameters::Format format_; | 154 AudioParameters::Format format_; |
| 154 ChannelLayout channel_layout_; | 155 ChannelLayout channel_layout_; |
| 155 int bits_per_sample_; | 156 int bits_per_sample_; |
| 156 int sample_rate_; | 157 int sample_rate_; |
| 157 int samples_per_packet_; | 158 int samples_per_packet_; |
| 158 }; | 159 }; |
| 159 | 160 |
| 160 // Convenience method which creates a default AudioInputStream object. | 161 // Convenience method which creates a default AudioInputStream object. |
| 161 static AudioInputStream* CreateDefaultAudioInputStream() { | 162 static AudioInputStream* CreateDefaultAudioInputStream( |
| 162 AudioInputStreamWrapper aisw; | 163 AudioManager* audio_manager) { |
| 164 AudioInputStreamWrapper aisw(audio_manager); |
| 163 AudioInputStream* ais = aisw.Create(); | 165 AudioInputStream* ais = aisw.Create(); |
| 164 return ais; | 166 return ais; |
| 165 } | 167 } |
| 166 | 168 |
| 167 // Verify that we can retrieve the current hardware/mixing sample rate | 169 // Verify that we can retrieve the current hardware/mixing sample rate |
| 168 // for all supported device roles. The ERole enumeration defines constants | 170 // for all supported device roles. The ERole enumeration defines constants |
| 169 // that indicate the role that the system/user has assigned to an audio | 171 // that indicate the role that the system/user has assigned to an audio |
| 170 // endpoint device. | 172 // endpoint device. |
| 171 // TODO(henrika): modify this test when we suport full device enumeration. | 173 // TODO(henrika): modify this test when we suport full device enumeration. |
| 172 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { | 174 TEST(WinAudioInputTest, WASAPIAudioInputStreamHardwareSampleRate) { |
| 173 if (!CanRunAudioTests()) | 175 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 176 if (!CanRunAudioTests(audio_manager.get())) |
| 174 return; | 177 return; |
| 175 | 178 |
| 176 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); | 179 ScopedCOMInitializer com_init(ScopedCOMInitializer::kMTA); |
| 177 | 180 |
| 178 // Default device intended for games, system notification sounds, | 181 // Default device intended for games, system notification sounds, |
| 179 // and voice commands. | 182 // and voice commands. |
| 180 int fs = static_cast<int>( | 183 int fs = static_cast<int>( |
| 181 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); | 184 WASAPIAudioInputStream::HardwareSampleRate(eConsole)); |
| 182 EXPECT_GE(fs, 0); | 185 EXPECT_GE(fs, 0); |
| 183 | 186 |
| 184 // Default communication device intended for e.g. VoIP communication. | 187 // Default communication device intended for e.g. VoIP communication. |
| 185 fs = static_cast<int>( | 188 fs = static_cast<int>( |
| 186 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); | 189 WASAPIAudioInputStream::HardwareSampleRate(eCommunications)); |
| 187 EXPECT_GE(fs, 0); | 190 EXPECT_GE(fs, 0); |
| 188 | 191 |
| 189 // Multimedia device for music, movies and live music recording. | 192 // Multimedia device for music, movies and live music recording. |
| 190 fs = static_cast<int>( | 193 fs = static_cast<int>( |
| 191 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); | 194 WASAPIAudioInputStream::HardwareSampleRate(eMultimedia)); |
| 192 EXPECT_GE(fs, 0); | 195 EXPECT_GE(fs, 0); |
| 193 } | 196 } |
| 194 | 197 |
| 195 // Test Create(), Close() calling sequence. | 198 // Test Create(), Close() calling sequence. |
| 196 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { | 199 TEST(WinAudioInputTest, WASAPIAudioInputStreamCreateAndClose) { |
| 197 if (!CanRunAudioTests()) | 200 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 201 if (!CanRunAudioTests(audio_manager.get())) |
| 198 return; | 202 return; |
| 199 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 203 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager.get()); |
| 200 ais->Close(); | 204 ais->Close(); |
| 201 } | 205 } |
| 202 | 206 |
| 203 // Test Open(), Close() calling sequence. | 207 // Test Open(), Close() calling sequence. |
| 204 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { | 208 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenAndClose) { |
| 205 if (!CanRunAudioTests()) | 209 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 210 if (!CanRunAudioTests(audio_manager.get())) |
| 206 return; | 211 return; |
| 207 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 212 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager.get()); |
| 208 EXPECT_TRUE(ais->Open()); | 213 EXPECT_TRUE(ais->Open()); |
| 209 ais->Close(); | 214 ais->Close(); |
| 210 } | 215 } |
| 211 | 216 |
| 212 // Test Open(), Start(), Close() calling sequence. | 217 // Test Open(), Start(), Close() calling sequence. |
| 213 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { | 218 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartAndClose) { |
| 214 if (!CanRunAudioTests()) | 219 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 220 if (!CanRunAudioTests(audio_manager.get())) |
| 215 return; | 221 return; |
| 216 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 222 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager.get()); |
| 217 EXPECT_TRUE(ais->Open()); | 223 EXPECT_TRUE(ais->Open()); |
| 218 MockAudioInputCallback sink; | 224 MockAudioInputCallback sink; |
| 219 ais->Start(&sink); | 225 ais->Start(&sink); |
| 220 EXPECT_CALL(sink, OnClose(ais)) | 226 EXPECT_CALL(sink, OnClose(ais)) |
| 221 .Times(1); | 227 .Times(1); |
| 222 ais->Close(); | 228 ais->Close(); |
| 223 } | 229 } |
| 224 | 230 |
| 225 // Test Open(), Start(), Stop(), Close() calling sequence. | 231 // Test Open(), Start(), Stop(), Close() calling sequence. |
| 226 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { | 232 TEST(WinAudioInputTest, WASAPIAudioInputStreamOpenStartStopAndClose) { |
| 227 if (!CanRunAudioTests()) | 233 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 234 if (!CanRunAudioTests(audio_manager.get())) |
| 228 return; | 235 return; |
| 229 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 236 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager.get()); |
| 230 EXPECT_TRUE(ais->Open()); | 237 EXPECT_TRUE(ais->Open()); |
| 231 MockAudioInputCallback sink; | 238 MockAudioInputCallback sink; |
| 232 ais->Start(&sink); | 239 ais->Start(&sink); |
| 233 ais->Stop(); | 240 ais->Stop(); |
| 234 EXPECT_CALL(sink, OnClose(ais)) | 241 EXPECT_CALL(sink, OnClose(ais)) |
| 235 .Times(1); | 242 .Times(1); |
| 236 ais->Close(); | 243 ais->Close(); |
| 237 } | 244 } |
| 238 | 245 |
| 239 // Test some additional calling sequences. | 246 // Test some additional calling sequences. |
| 240 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { | 247 TEST(MacAudioInputTest, WASAPIAudioInputStreamMiscCallingSequences) { |
| 241 if (!CanRunAudioTests()) | 248 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 249 if (!CanRunAudioTests(audio_manager.get())) |
| 242 return; | 250 return; |
| 243 AudioInputStream* ais = CreateDefaultAudioInputStream(); | 251 AudioInputStream* ais = CreateDefaultAudioInputStream(audio_manager.get()); |
| 244 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); | 252 WASAPIAudioInputStream* wais = static_cast<WASAPIAudioInputStream*>(ais); |
| 245 | 253 |
| 246 // Open(), Open() should fail the second time. | 254 // Open(), Open() should fail the second time. |
| 247 EXPECT_TRUE(ais->Open()); | 255 EXPECT_TRUE(ais->Open()); |
| 248 EXPECT_FALSE(ais->Open()); | 256 EXPECT_FALSE(ais->Open()); |
| 249 | 257 |
| 250 MockAudioInputCallback sink; | 258 MockAudioInputCallback sink; |
| 251 | 259 |
| 252 // Start(), Start() is a valid calling sequence (second call does nothing). | 260 // Start(), Start() is a valid calling sequence (second call does nothing). |
| 253 ais->Start(&sink); | 261 ais->Start(&sink); |
| 254 EXPECT_TRUE(wais->started()); | 262 EXPECT_TRUE(wais->started()); |
| 255 ais->Start(&sink); | 263 ais->Start(&sink); |
| 256 EXPECT_TRUE(wais->started()); | 264 EXPECT_TRUE(wais->started()); |
| 257 | 265 |
| 258 // Stop(), Stop() is a valid calling sequence (second call does nothing). | 266 // Stop(), Stop() is a valid calling sequence (second call does nothing). |
| 259 ais->Stop(); | 267 ais->Stop(); |
| 260 EXPECT_FALSE(wais->started()); | 268 EXPECT_FALSE(wais->started()); |
| 261 ais->Stop(); | 269 ais->Stop(); |
| 262 EXPECT_FALSE(wais->started()); | 270 EXPECT_FALSE(wais->started()); |
| 263 | 271 |
| 264 EXPECT_CALL(sink, OnClose(ais)) | 272 EXPECT_CALL(sink, OnClose(ais)) |
| 265 .Times(1); | 273 .Times(1); |
| 266 ais->Close(); | 274 ais->Close(); |
| 267 } | 275 } |
| 268 | 276 |
| 269 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { | 277 TEST(WinAudioInputTest, WASAPIAudioInputStreamTestPacketSizes) { |
| 270 if (!CanRunAudioTests()) | 278 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 279 if (!CanRunAudioTests(audio_manager.get())) |
| 271 return; | 280 return; |
| 272 | 281 |
| 273 // 10 ms packet size. | 282 // 10 ms packet size. |
| 274 | 283 |
| 275 // Create default WASAPI input stream which records in stereo using | 284 // Create default WASAPI input stream which records in stereo using |
| 276 // the shared mixing rate. The default buffer size is 10ms. | 285 // the shared mixing rate. The default buffer size is 10ms. |
| 277 AudioInputStreamWrapper aisw; | 286 AudioInputStreamWrapper aisw(audio_manager.get()); |
| 278 AudioInputStream* ais = aisw.Create(); | 287 AudioInputStream* ais = aisw.Create(); |
| 279 EXPECT_TRUE(ais->Open()); | 288 EXPECT_TRUE(ais->Open()); |
| 280 | 289 |
| 281 MockAudioInputCallback sink; | 290 MockAudioInputCallback sink; |
| 282 | 291 |
| 283 // Derive the expected size in bytes of each recorded packet. | 292 // Derive the expected size in bytes of each recorded packet. |
| 284 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * | 293 uint32 bytes_per_packet = aisw.channels() * aisw.samples_per_packet() * |
| 285 (aisw.bits_per_sample() / 8); | 294 (aisw.bits_per_sample() / 8); |
| 286 | 295 |
| 287 // We use 10ms packets and will run the test for ~100ms. Given that the | 296 // We use 10ms packets and will run the test for ~100ms. Given that the |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 ais->Close(); | 349 ais->Close(); |
| 341 } | 350 } |
| 342 | 351 |
| 343 // This test is intended for manual tests and should only be enabled | 352 // 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. | 353 // 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. | 354 // By default, GTest will print out YOU HAVE 1 DISABLED TEST. |
| 346 // To include disabled tests in test execution, just invoke the test program | 355 // 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 | 356 // with --gtest_also_run_disabled_tests or set the GTEST_ALSO_RUN_DISABLED_TESTS |
| 348 // environment variable to a value greater than 0. | 357 // environment variable to a value greater than 0. |
| 349 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { | 358 TEST(WinAudioInputTest, DISABLED_WASAPIAudioInputStreamRecordToFile) { |
| 350 if (!CanRunAudioTests()) | 359 scoped_refptr<AudioManager> audio_manager(AudioManager::Create()); |
| 360 if (!CanRunAudioTests(audio_manager.get())) |
| 351 return; | 361 return; |
| 352 | 362 |
| 353 const char* file_name = "out_stereo_10sec.pcm"; | 363 const char* file_name = "out_stereo_10sec.pcm"; |
| 354 | 364 |
| 355 AudioInputStreamWrapper aisw; | 365 AudioInputStreamWrapper aisw(audio_manager.get()); |
| 356 AudioInputStream* ais = aisw.Create(); | 366 AudioInputStream* ais = aisw.Create(); |
| 357 EXPECT_TRUE(ais->Open()); | 367 EXPECT_TRUE(ais->Open()); |
| 358 | 368 |
| 359 fprintf(stderr, " File name : %s\n", file_name); | 369 fprintf(stderr, " File name : %s\n", file_name); |
| 360 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); | 370 fprintf(stderr, " Sample rate: %d\n", aisw.sample_rate()); |
| 361 WriteToFileAudioSink file_sink(file_name); | 371 WriteToFileAudioSink file_sink(file_name); |
| 362 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 372 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
| 363 ais->Start(&file_sink); | 373 ais->Start(&file_sink); |
| 364 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); | 374 base::PlatformThread::Sleep(TestTimeouts::action_timeout_ms()); |
| 365 ais->Stop(); | 375 ais->Stop(); |
| 366 fprintf(stderr, " >> Recording has stopped.\n"); | 376 fprintf(stderr, " >> Recording has stopped.\n"); |
| 367 ais->Close(); | 377 ais->Close(); |
| 368 } | 378 } |
| OLD | NEW |