| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/environment.h" | 6 #include "base/environment.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/run_loop.h" |
| 9 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 10 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 11 #include "media/audio/audio_manager.h" | 13 #include "media/audio/audio_manager.h" |
| 12 #include "media/audio/audio_manager_base.h" | 14 #include "media/audio/audio_manager_base.h" |
| 13 #include "media/audio/audio_output_proxy.h" | 15 #include "media/audio/audio_output_proxy.h" |
| 14 #include "media/audio/audio_unittest_util.h" | 16 #include "media/audio/audio_unittest_util.h" |
| 15 #include "media/audio/fake_audio_log_factory.h" | 17 #include "media/audio/fake_audio_log_factory.h" |
| 16 #include "media/audio/fake_audio_manager.h" | 18 #include "media/audio/fake_audio_manager.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 #if defined(USE_ALSA) | 21 #if defined(USE_ALSA) |
| 20 #include "media/audio/alsa/audio_manager_alsa.h" | 22 #include "media/audio/alsa/audio_manager_alsa.h" |
| 21 #endif // defined(USE_ALSA) | 23 #endif // defined(USE_ALSA) |
| 22 | 24 |
| 23 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 24 #include "base/win/scoped_com_initializer.h" | 26 #include "base/win/scoped_com_initializer.h" |
| 25 #include "media/audio/win/audio_manager_win.h" | 27 #include "media/audio/win/audio_manager_win.h" |
| 26 #include "media/audio/win/wavein_input_win.h" | 28 #include "media/audio/win/wavein_input_win.h" |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 #if defined(USE_PULSEAUDIO) | 31 #if defined(USE_PULSEAUDIO) |
| 30 #include "media/audio/pulse/audio_manager_pulse.h" | 32 #include "media/audio/pulse/audio_manager_pulse.h" |
| 31 #endif // defined(USE_PULSEAUDIO) | 33 #endif // defined(USE_PULSEAUDIO) |
| 32 | 34 |
| 33 namespace media { | 35 namespace media { |
| 34 | 36 |
| 37 namespace { |
| 38 template <typename T> |
| 39 struct TestAudioManagerFactory { |
| 40 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { |
| 41 return ScopedAudioManagerPtr(new T(base::ThreadTaskRunnerHandle::Get(), |
| 42 base::ThreadTaskRunnerHandle::Get(), |
| 43 audio_log_factory)); |
| 44 }; |
| 45 }; |
| 46 |
| 47 #if defined(USE_PULSEAUDIO) |
| 48 template <> |
| 49 struct TestAudioManagerFactory<AudioManagerPulse> { |
| 50 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { |
| 51 scoped_ptr<AudioManagerPulse, AudioManagerDeleter> manager( |
| 52 new AudioManagerPulse(base::ThreadTaskRunnerHandle::Get(), |
| 53 base::ThreadTaskRunnerHandle::Get(), |
| 54 audio_log_factory)); |
| 55 if (!manager->Init()) |
| 56 manager.reset(); |
| 57 return std::move(manager); |
| 58 }; |
| 59 }; |
| 60 #endif // defined(USE_PULSEAUDIO) |
| 61 |
| 62 template <> |
| 63 struct TestAudioManagerFactory<std::nullptr_t> { |
| 64 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { |
| 65 return AudioManager::Create(base::ThreadTaskRunnerHandle::Get(), |
| 66 base::ThreadTaskRunnerHandle::Get(), nullptr, |
| 67 audio_log_factory); |
| 68 }; |
| 69 }; |
| 70 } // namespace |
| 71 |
| 35 // Test fixture which allows us to override the default enumeration API on | 72 // Test fixture which allows us to override the default enumeration API on |
| 36 // Windows. | 73 // Windows. |
| 37 class AudioManagerTest : public ::testing::Test { | 74 class AudioManagerTest : public ::testing::Test { |
| 38 public: | 75 public: |
| 39 void HandleDefaultDeviceIDsTest() { | 76 void HandleDefaultDeviceIDsTest() { |
| 40 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, | 77 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, |
| 41 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048); | 78 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048); |
| 42 | 79 |
| 43 // Create a stream with the default device id "". | 80 // Create a stream with the default device id "". |
| 44 AudioOutputStream* stream = | 81 AudioOutputStream* stream = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 70 *params = audio_manager_->GetDefaultOutputStreamParameters(); | 107 *params = audio_manager_->GetDefaultOutputStreamParameters(); |
| 71 } | 108 } |
| 72 | 109 |
| 73 void GetAssociatedOutputDeviceID(const std::string& input_device_id, | 110 void GetAssociatedOutputDeviceID(const std::string& input_device_id, |
| 74 std::string* output_device_id) { | 111 std::string* output_device_id) { |
| 75 *output_device_id = | 112 *output_device_id = |
| 76 audio_manager_->GetAssociatedOutputDeviceID(input_device_id); | 113 audio_manager_->GetAssociatedOutputDeviceID(input_device_id); |
| 77 } | 114 } |
| 78 | 115 |
| 79 protected: | 116 protected: |
| 80 AudioManagerTest() : audio_manager_(AudioManager::CreateForTesting()) { | 117 AudioManagerTest() { CreateAudioManagerForTesting(); } |
| 81 // Wait for audio thread initialization to complete. Otherwise the | 118 ~AudioManagerTest() override {} |
| 82 // enumeration type may not have been set yet. | |
| 83 base::WaitableEvent event(false, false); | |
| 84 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( | |
| 85 &base::WaitableEvent::Signal, base::Unretained(&event))); | |
| 86 event.Wait(); | |
| 87 } | |
| 88 | 119 |
| 89 #if defined(OS_WIN) | 120 #if defined(OS_WIN) |
| 90 bool SetMMDeviceEnumeration() { | 121 bool SetMMDeviceEnumeration() { |
| 91 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); | 122 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); |
| 92 // Windows Wave is used as default if Windows XP was detected => | 123 // Windows Wave is used as default if Windows XP was detected => |
| 93 // return false since MMDevice is not supported on XP. | 124 // return false since MMDevice is not supported on XP. |
| 94 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) | 125 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) |
| 95 return false; | 126 return false; |
| 96 | 127 |
| 97 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); | 128 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 ++it; | 173 ++it; |
| 143 } | 174 } |
| 144 } else { | 175 } else { |
| 145 // Log a warning so we can see the status on the build bots. No need to | 176 // Log a warning so we can see the status on the build bots. No need to |
| 146 // break the test though since this does successfully test the code and | 177 // break the test though since this does successfully test the code and |
| 147 // some failure cases. | 178 // some failure cases. |
| 148 LOG(WARNING) << "No input devices detected"; | 179 LOG(WARNING) << "No input devices detected"; |
| 149 } | 180 } |
| 150 } | 181 } |
| 151 | 182 |
| 152 void HasInputDevicesAvailable(bool* has_devices) { | 183 bool InputDevicesAvailable() { |
| 153 *has_devices = audio_manager_->HasAudioInputDevices(); | 184 return audio_manager_->HasAudioInputDevices(); |
| 185 } |
| 186 bool OutputDevicesAvailable() { |
| 187 return audio_manager_->HasAudioOutputDevices(); |
| 154 } | 188 } |
| 155 | 189 |
| 156 void HasOutputDevicesAvailable(bool* has_devices) { | 190 template <typename T = std::nullptr_t> |
| 157 *has_devices = audio_manager_->HasAudioOutputDevices(); | |
| 158 } | |
| 159 | |
| 160 bool InputDevicesAvailable() { | |
| 161 bool has_devices = false; | |
| 162 RunOnAudioThread(base::Bind(&AudioManagerTest::HasInputDevicesAvailable, | |
| 163 base::Unretained(this), &has_devices)); | |
| 164 return has_devices; | |
| 165 } | |
| 166 | |
| 167 bool OutputDevicesAvailable() { | |
| 168 bool has_devices = false; | |
| 169 RunOnAudioThread(base::Bind(&AudioManagerTest::HasOutputDevicesAvailable, | |
| 170 base::Unretained(this), &has_devices)); | |
| 171 return has_devices; | |
| 172 } | |
| 173 | |
| 174 #if defined(USE_ALSA) || defined(USE_PULSEAUDIO) | |
| 175 template <class T> | |
| 176 void CreateAudioManagerForTesting() { | 191 void CreateAudioManagerForTesting() { |
| 177 // Only one AudioManager may exist at a time, so destroy the one we're | 192 // Only one AudioManager may exist at a time, so destroy the one we're |
| 178 // currently holding before creating a new one. | 193 // currently holding before creating a new one. |
| 179 audio_manager_.reset(); | 194 audio_manager_.reset(); |
| 180 audio_manager_.reset(T::Create(&fake_audio_log_factory_)); | 195 audio_manager_ = |
| 181 } | 196 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); |
| 182 #endif | 197 // A few AudioManager implementations post initialization tasks to |
| 183 | 198 // audio thread. Flush the thread to ensure that |audio_manager_| is |
| 184 // Synchronously runs the provided callback/closure on the audio thread. | 199 // initialized and ready to use before returning from this function. |
| 185 void RunOnAudioThread(const base::Closure& closure) { | 200 // TODO(alokp): We should perhaps do this in AudioManager::Create(). |
| 186 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) { | 201 base::RunLoop().RunUntilIdle(); |
| 187 base::WaitableEvent event(false, false); | |
| 188 audio_manager_->GetTaskRunner()->PostTask( | |
| 189 FROM_HERE, | |
| 190 base::Bind(&AudioManagerTest::RunOnAudioThreadImpl, | |
| 191 base::Unretained(this), | |
| 192 closure, | |
| 193 &event)); | |
| 194 event.Wait(); | |
| 195 } else { | |
| 196 closure.Run(); | |
| 197 } | |
| 198 } | 202 } |
| 199 | 203 |
| 200 void RunOnAudioThreadImpl(const base::Closure& closure, | 204 base::MessageLoop message_loop_; |
| 201 base::WaitableEvent* event) { | |
| 202 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); | |
| 203 closure.Run(); | |
| 204 event->Signal(); | |
| 205 } | |
| 206 | |
| 207 FakeAudioLogFactory fake_audio_log_factory_; | 205 FakeAudioLogFactory fake_audio_log_factory_; |
| 208 scoped_ptr<AudioManager> audio_manager_; | 206 ScopedAudioManagerPtr audio_manager_; |
| 209 }; | 207 }; |
| 210 | 208 |
| 211 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) { | 209 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) { |
| 212 // Use a fake manager so we can makeup device ids, this will still use the | 210 // Use a fake manager so we can makeup device ids, this will still use the |
| 213 // AudioManagerBase code. | 211 // AudioManagerBase code. |
| 214 audio_manager_.reset(new FakeAudioManager(&fake_audio_log_factory_)); | 212 CreateAudioManagerForTesting<FakeAudioManager>(); |
| 215 RunOnAudioThread(base::Bind(&AudioManagerTest::HandleDefaultDeviceIDsTest, | 213 HandleDefaultDeviceIDsTest(); |
| 216 base::Unretained(this))); | 214 base::RunLoop().RunUntilIdle(); |
| 217 } | 215 } |
| 218 | 216 |
| 219 // Test that devices can be enumerated. | 217 // Test that devices can be enumerated. |
| 220 TEST_F(AudioManagerTest, EnumerateInputDevices) { | 218 TEST_F(AudioManagerTest, EnumerateInputDevices) { |
| 221 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); | 219 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
| 222 | 220 |
| 223 AudioDeviceNames device_names; | 221 AudioDeviceNames device_names; |
| 224 RunOnAudioThread( | 222 audio_manager_->GetAudioInputDeviceNames(&device_names); |
| 225 base::Bind(&AudioManager::GetAudioInputDeviceNames, | |
| 226 base::Unretained(audio_manager_.get()), | |
| 227 &device_names)); | |
| 228 CheckDeviceNames(device_names); | 223 CheckDeviceNames(device_names); |
| 229 } | 224 } |
| 230 | 225 |
| 231 // Test that devices can be enumerated. | 226 // Test that devices can be enumerated. |
| 232 TEST_F(AudioManagerTest, EnumerateOutputDevices) { | 227 TEST_F(AudioManagerTest, EnumerateOutputDevices) { |
| 233 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); | 228 ABORT_AUDIO_TEST_IF_NOT(OutputDevicesAvailable()); |
| 234 | 229 |
| 235 AudioDeviceNames device_names; | 230 AudioDeviceNames device_names; |
| 236 RunOnAudioThread( | 231 audio_manager_->GetAudioOutputDeviceNames(&device_names); |
| 237 base::Bind(&AudioManager::GetAudioOutputDeviceNames, | |
| 238 base::Unretained(audio_manager_.get()), | |
| 239 &device_names)); | |
| 240 CheckDeviceNames(device_names); | 232 CheckDeviceNames(device_names); |
| 241 } | 233 } |
| 242 | 234 |
| 243 // Run additional tests for Windows since enumeration can be done using | 235 // Run additional tests for Windows since enumeration can be done using |
| 244 // two different APIs. MMDevice is default for Vista and higher and Wave | 236 // two different APIs. MMDevice is default for Vista and higher and Wave |
| 245 // is default for XP and lower. | 237 // is default for XP and lower. |
| 246 #if defined(OS_WIN) | 238 #if defined(OS_WIN) |
| 247 | 239 |
| 248 // Override default enumeration API and force usage of Windows MMDevice. | 240 // Override default enumeration API and force usage of Windows MMDevice. |
| 249 // This test will only run on Windows Vista and higher. | 241 // This test will only run on Windows Vista and higher. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 audio_manager_->GetAudioOutputDeviceNames(&device_names); | 388 audio_manager_->GetAudioOutputDeviceNames(&device_names); |
| 397 CheckDeviceNames(device_names); | 389 CheckDeviceNames(device_names); |
| 398 } | 390 } |
| 399 #endif // defined(USE_ALSA) | 391 #endif // defined(USE_ALSA) |
| 400 | 392 |
| 401 TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) { | 393 TEST_F(AudioManagerTest, GetDefaultOutputStreamParameters) { |
| 402 #if defined(OS_WIN) || defined(OS_MACOSX) | 394 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 403 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); | 395 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); |
| 404 | 396 |
| 405 AudioParameters params; | 397 AudioParameters params; |
| 406 RunOnAudioThread( | 398 GetDefaultOutputStreamParameters(¶ms); |
| 407 base::Bind(&AudioManagerTest::GetDefaultOutputStreamParameters, | |
| 408 base::Unretained(this), ¶ms)); | |
| 409 EXPECT_TRUE(params.IsValid()); | 399 EXPECT_TRUE(params.IsValid()); |
| 410 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 400 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 411 } | 401 } |
| 412 | 402 |
| 413 TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) { | 403 TEST_F(AudioManagerTest, GetAssociatedOutputDeviceID) { |
| 414 #if defined(OS_WIN) || defined(OS_MACOSX) | 404 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 415 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable() && OutputDevicesAvailable()); | 405 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable() && OutputDevicesAvailable()); |
| 416 | 406 |
| 417 AudioDeviceNames device_names; | 407 AudioDeviceNames device_names; |
| 418 RunOnAudioThread(base::Bind(&AudioManager::GetAudioInputDeviceNames, | 408 audio_manager_->GetAudioInputDeviceNames(&device_names); |
| 419 base::Unretained(audio_manager_.get()), | |
| 420 &device_names)); | |
| 421 bool found_an_associated_device = false; | 409 bool found_an_associated_device = false; |
| 422 for (AudioDeviceNames::iterator it = device_names.begin(); | 410 for (AudioDeviceNames::iterator it = device_names.begin(); |
| 423 it != device_names.end(); | 411 it != device_names.end(); |
| 424 ++it) { | 412 ++it) { |
| 425 EXPECT_FALSE(it->unique_id.empty()); | 413 EXPECT_FALSE(it->unique_id.empty()); |
| 426 EXPECT_FALSE(it->device_name.empty()); | 414 EXPECT_FALSE(it->device_name.empty()); |
| 427 std::string output_device_id; | 415 std::string output_device_id; |
| 428 RunOnAudioThread(base::Bind(&AudioManagerTest::GetAssociatedOutputDeviceID, | 416 GetAssociatedOutputDeviceID(it->unique_id, &output_device_id); |
| 429 base::Unretained(this), it->unique_id, | |
| 430 &output_device_id)); | |
| 431 if (!output_device_id.empty()) { | 417 if (!output_device_id.empty()) { |
| 432 DVLOG(2) << it->unique_id << " matches with " << output_device_id; | 418 DVLOG(2) << it->unique_id << " matches with " << output_device_id; |
| 433 found_an_associated_device = true; | 419 found_an_associated_device = true; |
| 434 } | 420 } |
| 435 } | 421 } |
| 436 | 422 |
| 437 EXPECT_TRUE(found_an_associated_device); | 423 EXPECT_TRUE(found_an_associated_device); |
| 438 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 424 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 439 } | 425 } |
| 440 | 426 |
| 441 } // namespace media | 427 } // namespace media |
| OLD | NEW |