| 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/environment.h" | 5 #include "base/environment.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "media/audio/audio_manager.h" | 9 #include "media/audio/audio_manager.h" |
| 10 #include "media/audio/audio_manager_base.h" | 10 #include "media/audio/audio_manager_base.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #if defined(USE_PULSEAUDIO) | 24 #if defined(USE_PULSEAUDIO) |
| 25 #include "media/audio/pulse/audio_manager_pulse.h" | 25 #include "media/audio/pulse/audio_manager_pulse.h" |
| 26 #endif // defined(USE_PULSEAUDIO) | 26 #endif // defined(USE_PULSEAUDIO) |
| 27 | 27 |
| 28 namespace media { | 28 namespace media { |
| 29 | 29 |
| 30 // Test fixture which allows us to override the default enumeration API on | 30 // Test fixture which allows us to override the default enumeration API on |
| 31 // Windows. | 31 // Windows. |
| 32 class AudioManagerTest | 32 class AudioManagerTest : public ::testing::Test { |
| 33 : public ::testing::Test { | |
| 34 protected: | 33 protected: |
| 35 AudioManagerTest() | 34 AudioManagerTest() |
| 36 : audio_manager_(AudioManager::CreateForTesting()) | 35 : audio_manager_(AudioManager::CreateForTesting()) |
| 37 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
| 38 , com_init_(base::win::ScopedCOMInitializer::kMTA) | 37 , com_init_(base::win::ScopedCOMInitializer::kMTA) |
| 39 #endif | 38 #endif |
| 40 { | 39 { |
| 41 // Wait for audio thread initialization to complete. Otherwise the | 40 // Wait for audio thread initialization to complete. Otherwise the |
| 42 // enumeration type may not have been set yet. | 41 // enumeration type may not have been set yet. |
| 43 base::WaitableEvent event(false, false); | 42 base::WaitableEvent event(false, false); |
| 44 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( | 43 audio_manager_->GetTaskRunner()->PostTask(FROM_HERE, base::Bind( |
| 45 &base::WaitableEvent::Signal, base::Unretained(&event))); | 44 &base::WaitableEvent::Signal, base::Unretained(&event))); |
| 46 event.Wait(); | 45 event.Wait(); |
| 47 } | 46 } |
| 48 | 47 |
| 48 AudioManager* audio_manager() { return audio_manager_.get(); }; |
| 49 |
| 49 #if defined(OS_WIN) | 50 #if defined(OS_WIN) |
| 50 bool SetMMDeviceEnumeration() { | 51 bool SetMMDeviceEnumeration() { |
| 51 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); | 52 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); |
| 52 // Windows Wave is used as default if Windows XP was detected => | 53 // Windows Wave is used as default if Windows XP was detected => |
| 53 // return false since MMDevice is not supported on XP. | 54 // return false since MMDevice is not supported on XP. |
| 54 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) | 55 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) |
| 55 return false; | 56 return false; |
| 56 | 57 |
| 57 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); | 58 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); |
| 58 return true; | 59 return true; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 #if defined(USE_ALSA) || defined(USE_PULSEAUDIO) | 123 #if defined(USE_ALSA) || defined(USE_PULSEAUDIO) |
| 123 template <class T> | 124 template <class T> |
| 124 void CreateAudioManagerForTesting() { | 125 void CreateAudioManagerForTesting() { |
| 125 // Only one AudioManager may exist at a time, so destroy the one we're | 126 // Only one AudioManager may exist at a time, so destroy the one we're |
| 126 // currently holding before creating a new one. | 127 // currently holding before creating a new one. |
| 127 audio_manager_.reset(); | 128 audio_manager_.reset(); |
| 128 audio_manager_.reset(T::Create(&fake_audio_log_factory_)); | 129 audio_manager_.reset(T::Create(&fake_audio_log_factory_)); |
| 129 } | 130 } |
| 130 #endif | 131 #endif |
| 131 | 132 |
| 133 // Synchronously runs the provided callback/closure on the audio thread. |
| 134 void RunOnAudioThread(const base::Closure& closure) { |
| 135 if (!audio_manager()->GetTaskRunner()->BelongsToCurrentThread()) { |
| 136 base::WaitableEvent event(false, false); |
| 137 audio_manager_->GetTaskRunner()->PostTask( |
| 138 FROM_HERE, |
| 139 base::Bind(&AudioManagerTest::RunOnAudioThreadImpl, |
| 140 base::Unretained(this), |
| 141 closure, |
| 142 &event)); |
| 143 event.Wait(); |
| 144 } else { |
| 145 closure.Run(); |
| 146 } |
| 147 } |
| 148 |
| 149 void RunOnAudioThreadImpl(const base::Closure& closure, |
| 150 base::WaitableEvent* event) { |
| 151 DCHECK(audio_manager()->GetTaskRunner()->BelongsToCurrentThread()); |
| 152 closure.Run(); |
| 153 event->Signal(); |
| 154 } |
| 155 |
| 132 FakeAudioLogFactory fake_audio_log_factory_; | 156 FakeAudioLogFactory fake_audio_log_factory_; |
| 133 scoped_ptr<AudioManager> audio_manager_; | 157 scoped_ptr<AudioManager> audio_manager_; |
| 134 | 158 |
| 135 #if defined(OS_WIN) | 159 #if defined(OS_WIN) |
| 136 // The MMDevice API requires COM to be initialized on the current thread. | 160 // The MMDevice API requires COM to be initialized on the current thread. |
| 137 base::win::ScopedCOMInitializer com_init_; | 161 base::win::ScopedCOMInitializer com_init_; |
| 138 #endif | 162 #endif |
| 139 }; | 163 }; |
| 140 | 164 |
| 141 // Test that devices can be enumerated. | 165 // Test that devices can be enumerated. |
| 142 TEST_F(AudioManagerTest, EnumerateInputDevices) { | 166 TEST_F(AudioManagerTest, EnumerateInputDevices) { |
| 143 if (!CanRunInputTest()) | 167 if (!CanRunInputTest()) |
| 144 return; | 168 return; |
| 145 | 169 |
| 146 AudioDeviceNames device_names; | 170 AudioDeviceNames device_names; |
| 147 audio_manager_->GetAudioInputDeviceNames(&device_names); | 171 RunOnAudioThread( |
| 172 base::Bind(&AudioManager::GetAudioInputDeviceNames, |
| 173 base::Unretained(audio_manager()), |
| 174 &device_names)); |
| 148 CheckDeviceNames(device_names); | 175 CheckDeviceNames(device_names); |
| 149 } | 176 } |
| 150 | 177 |
| 151 // Test that devices can be enumerated. | 178 // Test that devices can be enumerated. |
| 152 TEST_F(AudioManagerTest, EnumerateOutputDevices) { | 179 TEST_F(AudioManagerTest, EnumerateOutputDevices) { |
| 153 if (!CanRunOutputTest()) | 180 if (!CanRunOutputTest()) |
| 154 return; | 181 return; |
| 155 | 182 |
| 156 AudioDeviceNames device_names; | 183 AudioDeviceNames device_names; |
| 157 audio_manager_->GetAudioOutputDeviceNames(&device_names); | 184 RunOnAudioThread( |
| 185 base::Bind(&AudioManager::GetAudioOutputDeviceNames, |
| 186 base::Unretained(audio_manager()), |
| 187 &device_names)); |
| 158 CheckDeviceNames(device_names); | 188 CheckDeviceNames(device_names); |
| 159 } | 189 } |
| 160 | 190 |
| 161 // Run additional tests for Windows since enumeration can be done using | 191 // Run additional tests for Windows since enumeration can be done using |
| 162 // two different APIs. MMDevice is default for Vista and higher and Wave | 192 // two different APIs. MMDevice is default for Vista and higher and Wave |
| 163 // is default for XP and lower. | 193 // is default for XP and lower. |
| 164 #if defined(OS_WIN) | 194 #if defined(OS_WIN) |
| 165 | 195 |
| 166 // Override default enumeration API and force usage of Windows MMDevice. | 196 // Override default enumeration API and force usage of Windows MMDevice. |
| 167 // This test will only run on Windows Vista and higher. | 197 // This test will only run on Windows Vista and higher. |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 VLOG(2) << it->unique_id << " matches with " << output_device_id; | 385 VLOG(2) << it->unique_id << " matches with " << output_device_id; |
| 356 found_an_associated_device = true; | 386 found_an_associated_device = true; |
| 357 } | 387 } |
| 358 } | 388 } |
| 359 | 389 |
| 360 EXPECT_TRUE(found_an_associated_device); | 390 EXPECT_TRUE(found_an_associated_device); |
| 361 #endif // defined(OS_WIN) || defined(OS_MACOSX) | 391 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 362 } | 392 } |
| 363 | 393 |
| 364 } // namespace media | 394 } // namespace media |
| OLD | NEW |