| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 11 #include "base/memory/aligned_memory.h" | 11 #include "base/memory/aligned_memory.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/sync_socket.h" | 13 #include "base/sync_socket.h" |
| 13 #include "base/win/scoped_com_initializer.h" | 14 #include "base/win/scoped_com_initializer.h" |
| 14 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
| 15 #include "media/audio/audio_io.h" | 16 #include "media/audio/audio_io.h" |
| 16 #include "media/audio/audio_manager.h" | 17 #include "media/audio/audio_manager.h" |
| 17 #include "media/audio/audio_unittest_util.h" | 18 #include "media/audio/audio_unittest_util.h" |
| 18 #include "media/audio/mock_audio_source_callback.h" | 19 #include "media/audio/mock_audio_source_callback.h" |
| 19 #include "media/audio/simple_sources.h" | 20 #include "media/audio/simple_sources.h" |
| 20 #include "media/base/limits.h" | 21 #include "media/base/limits.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 uint32_t size() const { return size_; } | 137 uint32_t size() const { return size_; } |
| 137 // Returns the memory backing the file. | 138 // Returns the memory backing the file. |
| 138 const void* GetChunkAt(uint32_t offset) { return &start_[offset]; } | 139 const void* GetChunkAt(uint32_t offset) { return &start_[offset]; } |
| 139 | 140 |
| 140 private: | 141 private: |
| 141 HANDLE fmap_; | 142 HANDLE fmap_; |
| 142 char* start_; | 143 char* start_; |
| 143 uint32_t size_; | 144 uint32_t size_; |
| 144 }; | 145 }; |
| 145 | 146 |
| 147 class WinAudioTest : public ::testing::Test { |
| 148 public: |
| 149 WinAudioTest() { |
| 150 audio_manager_ = |
| 151 AudioManager::CreateForTesting(message_loop_.task_runner()); |
| 152 message_loop_.RunUntilIdle(); |
| 153 } |
| 154 ~WinAudioTest() override { |
| 155 audio_manager_.reset(); |
| 156 message_loop_.RunUntilIdle(); |
| 157 } |
| 158 |
| 159 protected: |
| 160 base::MessageLoop message_loop_; |
| 161 ScopedAudioManagerPtr audio_manager_; |
| 162 }; |
| 163 |
| 146 // =========================================================================== | 164 // =========================================================================== |
| 147 // Validation of AudioManager::AUDIO_PCM_LINEAR | 165 // Validation of AudioManager::AUDIO_PCM_LINEAR |
| 148 // | 166 // |
| 149 // NOTE: | 167 // NOTE: |
| 150 // The tests can fail on the build bots when somebody connects to them via | 168 // The tests can fail on the build bots when somebody connects to them via |
| 151 // remote-desktop and the rdp client installs an audio device that fails to open | 169 // remote-desktop and the rdp client installs an audio device that fails to open |
| 152 // at some point, possibly when the connection goes idle. | 170 // at some point, possibly when the connection goes idle. |
| 153 | 171 |
| 154 // Test that can it be created and closed. | 172 // Test that can it be created and closed. |
| 155 TEST(WinAudioTest, PCMWaveStreamGetAndClose) { | 173 TEST_F(WinAudioTest, PCMWaveStreamGetAndClose) { |
| 156 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 174 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 157 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 158 | 175 |
| 159 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 176 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 160 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 177 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
| 161 8000, 16, 256), | 178 8000, 16, 256), |
| 162 std::string()); | 179 std::string()); |
| 163 ASSERT_TRUE(NULL != oas); | 180 ASSERT_TRUE(NULL != oas); |
| 164 oas->Close(); | 181 oas->Close(); |
| 165 } | 182 } |
| 166 | 183 |
| 167 // Test that can it be cannot be created with invalid parameters. | 184 // Test that can it be cannot be created with invalid parameters. |
| 168 TEST(WinAudioTest, SanityOnMakeParams) { | 185 TEST_F(WinAudioTest, SanityOnMakeParams) { |
| 169 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 186 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 170 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 171 | 187 |
| 172 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; | 188 AudioParameters::Format fmt = AudioParameters::AUDIO_PCM_LINEAR; |
| 173 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 189 EXPECT_TRUE( |
| 174 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), | 190 NULL == |
| 175 std::string())); | 191 audio_manager_->MakeAudioOutputStream( |
| 176 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 192 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), |
| 177 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, 256), | 193 std::string())); |
| 178 std::string())); | 194 EXPECT_TRUE( |
| 179 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 195 NULL == |
| 180 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 256), | 196 audio_manager_->MakeAudioOutputStream( |
| 181 std::string())); | 197 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 1024 * 1024, 16, 256), |
| 182 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 198 std::string())); |
| 183 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), | 199 EXPECT_TRUE(NULL == |
| 184 std::string())); | 200 audio_manager_->MakeAudioOutputStream( |
| 185 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 201 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, 8000, 80, 256), |
| 186 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, 256), | 202 std::string())); |
| 187 std::string())); | 203 EXPECT_TRUE( |
| 188 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 204 NULL == |
| 189 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, -100), | 205 audio_manager_->MakeAudioOutputStream( |
| 190 std::string())); | 206 AudioParameters(fmt, CHANNEL_LAYOUT_UNSUPPORTED, 8000, 16, 256), |
| 191 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 207 std::string())); |
| 192 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, 0), | 208 EXPECT_TRUE(NULL == |
| 193 std::string())); | 209 audio_manager_->MakeAudioOutputStream( |
| 194 EXPECT_TRUE(NULL == audio_man->MakeAudioOutputStream( | 210 AudioParameters(fmt, CHANNEL_LAYOUT_STEREO, -8000, 16, 256), |
| 195 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, | 211 std::string())); |
| 196 media::limits::kMaxSamplesPerPacket + 1), | 212 EXPECT_TRUE(NULL == |
| 197 std::string())); | 213 audio_manager_->MakeAudioOutputStream( |
| 214 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, -100), |
| 215 std::string())); |
| 216 EXPECT_TRUE(NULL == |
| 217 audio_manager_->MakeAudioOutputStream( |
| 218 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, 0), |
| 219 std::string())); |
| 220 EXPECT_TRUE(NULL == |
| 221 audio_manager_->MakeAudioOutputStream( |
| 222 AudioParameters(fmt, CHANNEL_LAYOUT_MONO, 8000, 16, |
| 223 media::limits::kMaxSamplesPerPacket + 1), |
| 224 std::string())); |
| 198 } | 225 } |
| 199 | 226 |
| 200 // Test that it can be opened and closed. | 227 // Test that it can be opened and closed. |
| 201 TEST(WinAudioTest, PCMWaveStreamOpenAndClose) { | 228 TEST_F(WinAudioTest, PCMWaveStreamOpenAndClose) { |
| 202 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 229 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 203 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 204 | 230 |
| 205 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 231 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 206 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 232 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
| 207 8000, 16, 256), | 233 8000, 16, 256), |
| 208 std::string()); | 234 std::string()); |
| 209 ASSERT_TRUE(NULL != oas); | 235 ASSERT_TRUE(NULL != oas); |
| 210 EXPECT_TRUE(oas->Open()); | 236 EXPECT_TRUE(oas->Open()); |
| 211 oas->Close(); | 237 oas->Close(); |
| 212 } | 238 } |
| 213 | 239 |
| 214 // Test that it has a maximum packet size. | 240 // Test that it has a maximum packet size. |
| 215 TEST(WinAudioTest, PCMWaveStreamOpenLimit) { | 241 TEST_F(WinAudioTest, PCMWaveStreamOpenLimit) { |
| 216 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 242 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 217 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 218 | 243 |
| 219 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 244 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 220 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, | 245 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_STEREO, |
| 221 8000, 16, 1024 * 1024 * 1024), | 246 8000, 16, 1024 * 1024 * 1024), |
| 222 std::string()); | 247 std::string()); |
| 223 EXPECT_TRUE(NULL == oas); | 248 EXPECT_TRUE(NULL == oas); |
| 224 if (oas) | 249 if (oas) |
| 225 oas->Close(); | 250 oas->Close(); |
| 226 } | 251 } |
| 227 | 252 |
| 228 // Test potential deadlock situation if the source is slow or blocks for some | 253 // Test potential deadlock situation if the source is slow or blocks for some |
| 229 // time. The actual EXPECT_GT are mostly meaningless and the real test is that | 254 // time. The actual EXPECT_GT are mostly meaningless and the real test is that |
| 230 // the test completes in reasonable time. | 255 // the test completes in reasonable time. |
| 231 TEST(WinAudioTest, PCMWaveSlowSource) { | 256 TEST_F(WinAudioTest, PCMWaveSlowSource) { |
| 232 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 257 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 233 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 234 | 258 |
| 235 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 259 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 236 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 260 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 237 16000, 16, 256), | 261 16000, 16, 256), |
| 238 std::string()); | 262 std::string()); |
| 239 ASSERT_TRUE(NULL != oas); | 263 ASSERT_TRUE(NULL != oas); |
| 240 TestSourceLaggy test_laggy(90); | 264 TestSourceLaggy test_laggy(90); |
| 241 EXPECT_TRUE(oas->Open()); | 265 EXPECT_TRUE(oas->Open()); |
| 242 // The test parameters cause a callback every 32 ms and the source is | 266 // The test parameters cause a callback every 32 ms and the source is |
| 243 // sleeping for 90 ms, so it is guaranteed that we run out of ready buffers. | 267 // sleeping for 90 ms, so it is guaranteed that we run out of ready buffers. |
| 244 oas->Start(&test_laggy); | 268 oas->Start(&test_laggy); |
| 245 ::Sleep(500); | 269 ::Sleep(500); |
| 246 EXPECT_GT(test_laggy.callback_count(), 2); | 270 EXPECT_GT(test_laggy.callback_count(), 2); |
| 247 EXPECT_FALSE(test_laggy.had_error()); | 271 EXPECT_FALSE(test_laggy.had_error()); |
| 248 oas->Stop(); | 272 oas->Stop(); |
| 249 ::Sleep(500); | 273 ::Sleep(500); |
| 250 oas->Close(); | 274 oas->Close(); |
| 251 } | 275 } |
| 252 | 276 |
| 253 // Test another potential deadlock situation if the thread that calls Start() | 277 // Test another potential deadlock situation if the thread that calls Start() |
| 254 // gets paused. This test is best when run over RDP with audio enabled. See | 278 // gets paused. This test is best when run over RDP with audio enabled. See |
| 255 // bug 19276 for more details. | 279 // bug 19276 for more details. |
| 256 TEST(WinAudioTest, PCMWaveStreamPlaySlowLoop) { | 280 TEST_F(WinAudioTest, PCMWaveStreamPlaySlowLoop) { |
| 257 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 281 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 258 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 259 | 282 |
| 260 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 283 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
| 261 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 284 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 262 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 285 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 263 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 286 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
| 264 std::string()); | 287 std::string()); |
| 265 ASSERT_TRUE(NULL != oas); | 288 ASSERT_TRUE(NULL != oas); |
| 266 | 289 |
| 267 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 290 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
| 268 | 291 |
| 269 EXPECT_TRUE(oas->Open()); | 292 EXPECT_TRUE(oas->Open()); |
| 270 oas->SetVolume(1.0); | 293 oas->SetVolume(1.0); |
| 271 | 294 |
| 272 for (int ix = 0; ix != 5; ++ix) { | 295 for (int ix = 0; ix != 5; ++ix) { |
| 273 oas->Start(&source); | 296 oas->Start(&source); |
| 274 ::Sleep(10); | 297 ::Sleep(10); |
| 275 oas->Stop(); | 298 oas->Stop(); |
| 276 } | 299 } |
| 277 oas->Close(); | 300 oas->Close(); |
| 278 } | 301 } |
| 279 | 302 |
| 280 | 303 |
| 281 // This test produces actual audio for .5 seconds on the default wave | 304 // This test produces actual audio for .5 seconds on the default wave |
| 282 // device at 44.1K s/sec. Parameters have been chosen carefully so you should | 305 // device at 44.1K s/sec. Parameters have been chosen carefully so you should |
| 283 // not hear pops or noises while the sound is playing. | 306 // not hear pops or noises while the sound is playing. |
| 284 TEST(WinAudioTest, PCMWaveStreamPlay200HzTone44Kss) { | 307 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzTone44Kss) { |
| 285 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 308 if (!audio_manager_->HasAudioOutputDevices()) { |
| 286 if (!audio_man->HasAudioOutputDevices()) { | |
| 287 LOG(WARNING) << "No output device detected."; | 309 LOG(WARNING) << "No output device detected."; |
| 288 return; | 310 return; |
| 289 } | 311 } |
| 290 | 312 |
| 291 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 313 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
| 292 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 314 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 293 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 315 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 294 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 316 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
| 295 std::string()); | 317 std::string()); |
| 296 ASSERT_TRUE(NULL != oas); | 318 ASSERT_TRUE(NULL != oas); |
| 297 | 319 |
| 298 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 320 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
| 299 | 321 |
| 300 EXPECT_TRUE(oas->Open()); | 322 EXPECT_TRUE(oas->Open()); |
| 301 oas->SetVolume(1.0); | 323 oas->SetVolume(1.0); |
| 302 oas->Start(&source); | 324 oas->Start(&source); |
| 303 ::Sleep(500); | 325 ::Sleep(500); |
| 304 oas->Stop(); | 326 oas->Stop(); |
| 305 oas->Close(); | 327 oas->Close(); |
| 306 } | 328 } |
| 307 | 329 |
| 308 // This test produces actual audio for for .5 seconds on the default wave | 330 // This test produces actual audio for for .5 seconds on the default wave |
| 309 // device at 22K s/sec. Parameters have been chosen carefully so you should | 331 // device at 22K s/sec. Parameters have been chosen carefully so you should |
| 310 // not hear pops or noises while the sound is playing. The audio also should | 332 // not hear pops or noises while the sound is playing. The audio also should |
| 311 // sound with a lower volume than PCMWaveStreamPlay200HzTone44Kss. | 333 // sound with a lower volume than PCMWaveStreamPlay200HzTone44Kss. |
| 312 TEST(WinAudioTest, PCMWaveStreamPlay200HzTone22Kss) { | 334 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzTone22Kss) { |
| 313 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 335 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 314 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 315 | 336 |
| 316 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 20; | 337 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 20; |
| 317 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 338 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 318 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 339 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 319 AudioParameters::kAudioCDSampleRate / 2, 16, | 340 AudioParameters::kAudioCDSampleRate / 2, 16, |
| 320 samples_100_ms), | 341 samples_100_ms), |
| 321 std::string()); | 342 std::string()); |
| 322 ASSERT_TRUE(NULL != oas); | 343 ASSERT_TRUE(NULL != oas); |
| 323 | 344 |
| 324 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate/2); | 345 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate/2); |
| 325 | 346 |
| 326 EXPECT_TRUE(oas->Open()); | 347 EXPECT_TRUE(oas->Open()); |
| 327 | 348 |
| 328 oas->SetVolume(0.5); | 349 oas->SetVolume(0.5); |
| 329 oas->Start(&source); | 350 oas->Start(&source); |
| 330 ::Sleep(500); | 351 ::Sleep(500); |
| 331 | 352 |
| 332 // Test that the volume is within the set limits. | 353 // Test that the volume is within the set limits. |
| 333 double volume = 0.0; | 354 double volume = 0.0; |
| 334 oas->GetVolume(&volume); | 355 oas->GetVolume(&volume); |
| 335 EXPECT_LT(volume, 0.51); | 356 EXPECT_LT(volume, 0.51); |
| 336 EXPECT_GT(volume, 0.49); | 357 EXPECT_GT(volume, 0.49); |
| 337 oas->Stop(); | 358 oas->Stop(); |
| 338 oas->Close(); | 359 oas->Close(); |
| 339 } | 360 } |
| 340 | 361 |
| 341 // Uses a restricted source to play ~2 seconds of audio for about 5 seconds. We | 362 // Uses a restricted source to play ~2 seconds of audio for about 5 seconds. We |
| 342 // try hard to generate situation where the two threads are accessing the | 363 // try hard to generate situation where the two threads are accessing the |
| 343 // object roughly at the same time. | 364 // object roughly at the same time. |
| 344 TEST(WinAudioTest, PushSourceFile16KHz) { | 365 TEST_F(WinAudioTest, PushSourceFile16KHz) { |
| 345 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 366 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 346 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 347 | 367 |
| 348 static const int kSampleRate = 16000; | 368 static const int kSampleRate = 16000; |
| 349 SineWaveAudioSource source(1, 200.0, kSampleRate); | 369 SineWaveAudioSource source(1, 200.0, kSampleRate); |
| 350 // Compute buffer size for 100ms of audio. | 370 // Compute buffer size for 100ms of audio. |
| 351 const uint32_t kSamples100ms = (kSampleRate / 1000) * 100; | 371 const uint32_t kSamples100ms = (kSampleRate / 1000) * 100; |
| 352 // Restrict SineWaveAudioSource to 100ms of samples. | 372 // Restrict SineWaveAudioSource to 100ms of samples. |
| 353 source.CapSamples(kSamples100ms); | 373 source.CapSamples(kSamples100ms); |
| 354 | 374 |
| 355 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 375 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 356 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 376 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 357 kSampleRate, 16, kSamples100ms), | 377 kSampleRate, 16, kSamples100ms), |
| 358 std::string()); | 378 std::string()); |
| 359 ASSERT_TRUE(NULL != oas); | 379 ASSERT_TRUE(NULL != oas); |
| 360 | 380 |
| 361 EXPECT_TRUE(oas->Open()); | 381 EXPECT_TRUE(oas->Open()); |
| 362 | 382 |
| 363 oas->SetVolume(1.0); | 383 oas->SetVolume(1.0); |
| 364 oas->Start(&source); | 384 oas->Start(&source); |
| 365 | 385 |
| 366 // We buffer and play at the same time, buffering happens every ~10ms and the | 386 // We buffer and play at the same time, buffering happens every ~10ms and the |
| 367 // consuming of the buffer happens every ~100ms. We do 100 buffers which | 387 // consuming of the buffer happens every ~100ms. We do 100 buffers which |
| 368 // effectively wrap around the file more than once. | 388 // effectively wrap around the file more than once. |
| 369 for (uint32_t ix = 0; ix != 100; ++ix) { | 389 for (uint32_t ix = 0; ix != 100; ++ix) { |
| 370 ::Sleep(10); | 390 ::Sleep(10); |
| 371 source.Reset(); | 391 source.Reset(); |
| 372 } | 392 } |
| 373 | 393 |
| 374 // Play a little bit more of the file. | 394 // Play a little bit more of the file. |
| 375 ::Sleep(500); | 395 ::Sleep(500); |
| 376 | 396 |
| 377 oas->Stop(); | 397 oas->Stop(); |
| 378 oas->Close(); | 398 oas->Close(); |
| 379 } | 399 } |
| 380 | 400 |
| 381 // This test is to make sure an AudioOutputStream can be started after it was | 401 // This test is to make sure an AudioOutputStream can be started after it was |
| 382 // stopped. You will here two .5 seconds wave signal separated by 0.5 seconds | 402 // stopped. You will here two .5 seconds wave signal separated by 0.5 seconds |
| 383 // of silence. | 403 // of silence. |
| 384 TEST(WinAudioTest, PCMWaveStreamPlayTwice200HzTone44Kss) { | 404 TEST_F(WinAudioTest, PCMWaveStreamPlayTwice200HzTone44Kss) { |
| 385 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 405 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 386 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 387 | 406 |
| 388 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 407 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
| 389 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 408 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 390 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 409 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 391 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 410 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
| 392 std::string()); | 411 std::string()); |
| 393 ASSERT_TRUE(NULL != oas); | 412 ASSERT_TRUE(NULL != oas); |
| 394 | 413 |
| 395 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); | 414 SineWaveAudioSource source(1, 200.0, AudioParameters::kAudioCDSampleRate); |
| 396 EXPECT_TRUE(oas->Open()); | 415 EXPECT_TRUE(oas->Open()); |
| 397 oas->SetVolume(1.0); | 416 oas->SetVolume(1.0); |
| 398 | 417 |
| 399 // Play the wave for .5 seconds. | 418 // Play the wave for .5 seconds. |
| 400 oas->Start(&source); | 419 oas->Start(&source); |
| 401 ::Sleep(500); | 420 ::Sleep(500); |
| 402 oas->Stop(); | 421 oas->Stop(); |
| 403 | 422 |
| 404 // Sleep to give silence after stopping the AudioOutputStream. | 423 // Sleep to give silence after stopping the AudioOutputStream. |
| 405 ::Sleep(250); | 424 ::Sleep(250); |
| 406 | 425 |
| 407 // Start again and play for .5 seconds. | 426 // Start again and play for .5 seconds. |
| 408 oas->Start(&source); | 427 oas->Start(&source); |
| 409 ::Sleep(500); | 428 ::Sleep(500); |
| 410 oas->Stop(); | 429 oas->Stop(); |
| 411 | 430 |
| 412 oas->Close(); | 431 oas->Close(); |
| 413 } | 432 } |
| 414 | 433 |
| 415 // With the low latency mode, WASAPI is utilized by default for Vista and | 434 // With the low latency mode, WASAPI is utilized by default for Vista and |
| 416 // higher and Wave is used for XP and lower. It is possible to utilize a | 435 // higher and Wave is used for XP and lower. It is possible to utilize a |
| 417 // smaller buffer size for WASAPI than for Wave. | 436 // smaller buffer size for WASAPI than for Wave. |
| 418 TEST(WinAudioTest, PCMWaveStreamPlay200HzToneLowLatency) { | 437 TEST_F(WinAudioTest, PCMWaveStreamPlay200HzToneLowLatency) { |
| 419 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 438 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 420 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 421 | 439 |
| 422 // Use 10 ms buffer size for WASAPI and 50 ms buffer size for Wave. | 440 // Use 10 ms buffer size for WASAPI and 50 ms buffer size for Wave. |
| 423 // Take the existing native sample rate into account. | 441 // Take the existing native sample rate into account. |
| 424 const AudioParameters params = audio_man->GetDefaultOutputStreamParameters(); | 442 const AudioParameters params = |
| 443 audio_manager_->GetDefaultOutputStreamParameters(); |
| 425 int sample_rate = params.sample_rate(); | 444 int sample_rate = params.sample_rate(); |
| 426 uint32_t samples_10_ms = sample_rate / 100; | 445 uint32_t samples_10_ms = sample_rate / 100; |
| 427 int n = 1; | 446 int n = 1; |
| 428 (base::win::GetVersion() <= base::win::VERSION_XP) ? n = 5 : n = 1; | 447 (base::win::GetVersion() <= base::win::VERSION_XP) ? n = 5 : n = 1; |
| 429 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 448 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 430 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 449 AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 431 CHANNEL_LAYOUT_MONO, sample_rate, | 450 CHANNEL_LAYOUT_MONO, sample_rate, 16, n * samples_10_ms), |
| 432 16, n * samples_10_ms), | |
| 433 std::string()); | 451 std::string()); |
| 434 ASSERT_TRUE(NULL != oas); | 452 ASSERT_TRUE(NULL != oas); |
| 435 | 453 |
| 436 SineWaveAudioSource source(1, 200, sample_rate); | 454 SineWaveAudioSource source(1, 200, sample_rate); |
| 437 | 455 |
| 438 bool opened = oas->Open(); | 456 bool opened = oas->Open(); |
| 439 if (!opened) { | 457 if (!opened) { |
| 440 // It was not possible to open this audio device in mono. | 458 // It was not possible to open this audio device in mono. |
| 441 // No point in continuing the test so let's break here. | 459 // No point in continuing the test so let's break here. |
| 442 LOG(WARNING) << "Mono is not supported. Skipping test."; | 460 LOG(WARNING) << "Mono is not supported. Skipping test."; |
| 443 oas->Close(); | 461 oas->Close(); |
| 444 return; | 462 return; |
| 445 } | 463 } |
| 446 oas->SetVolume(1.0); | 464 oas->SetVolume(1.0); |
| 447 | 465 |
| 448 // Play the wave for .8 seconds. | 466 // Play the wave for .8 seconds. |
| 449 oas->Start(&source); | 467 oas->Start(&source); |
| 450 ::Sleep(800); | 468 ::Sleep(800); |
| 451 oas->Stop(); | 469 oas->Stop(); |
| 452 oas->Close(); | 470 oas->Close(); |
| 453 } | 471 } |
| 454 | 472 |
| 455 // Check that the pending bytes value is correct what the stream starts. | 473 // Check that the pending bytes value is correct what the stream starts. |
| 456 TEST(WinAudioTest, PCMWaveStreamPendingBytes) { | 474 TEST_F(WinAudioTest, PCMWaveStreamPendingBytes) { |
| 457 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 475 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 458 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 459 | 476 |
| 460 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; | 477 uint32_t samples_100_ms = AudioParameters::kAudioCDSampleRate / 10; |
| 461 AudioOutputStream* oas = audio_man->MakeAudioOutputStream( | 478 AudioOutputStream* oas = audio_manager_->MakeAudioOutputStream( |
| 462 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, | 479 AudioParameters(AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, |
| 463 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), | 480 AudioParameters::kAudioCDSampleRate, 16, samples_100_ms), |
| 464 std::string()); | 481 std::string()); |
| 465 ASSERT_TRUE(NULL != oas); | 482 ASSERT_TRUE(NULL != oas); |
| 466 | 483 |
| 467 NiceMock<MockAudioSourceCallback> source; | 484 NiceMock<MockAudioSourceCallback> source; |
| 468 EXPECT_TRUE(oas->Open()); | 485 EXPECT_TRUE(oas->Open()); |
| 469 | 486 |
| 470 uint32_t bytes_100_ms = samples_100_ms * 2; | 487 uint32_t bytes_100_ms = samples_100_ms * 2; |
| 471 | 488 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 } | 595 } |
| 579 | 596 |
| 580 // Test the basic operation of AudioOutputStream used with a SyncSocket. | 597 // Test the basic operation of AudioOutputStream used with a SyncSocket. |
| 581 // The emphasis is to verify that it is possible to feed data to the audio | 598 // The emphasis is to verify that it is possible to feed data to the audio |
| 582 // layer using a source based on SyncSocket. In a real situation we would | 599 // layer using a source based on SyncSocket. In a real situation we would |
| 583 // go for the low-latency version in combination with SyncSocket, but to keep | 600 // go for the low-latency version in combination with SyncSocket, but to keep |
| 584 // the test more simple, AUDIO_PCM_LINEAR is utilized instead. The main | 601 // the test more simple, AUDIO_PCM_LINEAR is utilized instead. The main |
| 585 // principle of the test still remains and we avoid the additional complexity | 602 // principle of the test still remains and we avoid the additional complexity |
| 586 // related to the two different audio-layers for AUDIO_PCM_LOW_LATENCY. | 603 // related to the two different audio-layers for AUDIO_PCM_LOW_LATENCY. |
| 587 // In this test you should hear a continuous 200Hz tone for 2 seconds. | 604 // In this test you should hear a continuous 200Hz tone for 2 seconds. |
| 588 TEST(WinAudioTest, SyncSocketBasic) { | 605 TEST_F(WinAudioTest, SyncSocketBasic) { |
| 589 scoped_ptr<AudioManager> audio_man(AudioManager::CreateForTesting()); | 606 ABORT_AUDIO_TEST_IF_NOT(audio_manager_->HasAudioOutputDevices()); |
| 590 ABORT_AUDIO_TEST_IF_NOT(audio_man->HasAudioOutputDevices()); | |
| 591 | 607 |
| 592 static const int sample_rate = AudioParameters::kAudioCDSampleRate; | 608 static const int sample_rate = AudioParameters::kAudioCDSampleRate; |
| 593 static const uint32_t kSamples20ms = sample_rate / 50; | 609 static const uint32_t kSamples20ms = sample_rate / 50; |
| 594 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, | 610 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, |
| 595 CHANNEL_LAYOUT_MONO, sample_rate, 16, kSamples20ms); | 611 CHANNEL_LAYOUT_MONO, sample_rate, 16, kSamples20ms); |
| 596 | 612 |
| 597 | 613 AudioOutputStream* oas = |
| 598 AudioOutputStream* oas = audio_man->MakeAudioOutputStream(params, | 614 audio_manager_->MakeAudioOutputStream(params, std::string()); |
| 599 std::string()); | |
| 600 ASSERT_TRUE(NULL != oas); | 615 ASSERT_TRUE(NULL != oas); |
| 601 | 616 |
| 602 ASSERT_TRUE(oas->Open()); | 617 ASSERT_TRUE(oas->Open()); |
| 603 | 618 |
| 604 base::SyncSocket sockets[2]; | 619 base::SyncSocket sockets[2]; |
| 605 ASSERT_TRUE(base::SyncSocket::CreatePair(&sockets[0], &sockets[1])); | 620 ASSERT_TRUE(base::SyncSocket::CreatePair(&sockets[0], &sockets[1])); |
| 606 | 621 |
| 607 SyncSocketSource source(&sockets[0], params); | 622 SyncSocketSource source(&sockets[0], params); |
| 608 | 623 |
| 609 SyncThreadContext thread_context; | 624 SyncThreadContext thread_context; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 620 oas->Start(&source); | 635 oas->Start(&source); |
| 621 | 636 |
| 622 ::WaitForSingleObject(thread, INFINITE); | 637 ::WaitForSingleObject(thread, INFINITE); |
| 623 ::CloseHandle(thread); | 638 ::CloseHandle(thread); |
| 624 | 639 |
| 625 oas->Stop(); | 640 oas->Stop(); |
| 626 oas->Close(); | 641 oas->Close(); |
| 627 } | 642 } |
| 628 | 643 |
| 629 } // namespace media | 644 } // namespace media |
| OLD | NEW |