Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: media/audio/audio_manager_unittest.cc

Issue 1806313003: Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: implemented ScopedAudioManagerPtr Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "media/audio/audio_manager.h" 11 #include "media/audio/audio_manager.h"
12 #include "media/audio/audio_manager_base.h" 12 #include "media/audio/audio_manager_base.h"
13 #include "media/audio/audio_output_proxy.h" 13 #include "media/audio/audio_output_proxy.h"
14 #include "media/audio/audio_thread.h"
14 #include "media/audio/audio_unittest_util.h" 15 #include "media/audio/audio_unittest_util.h"
15 #include "media/audio/fake_audio_log_factory.h" 16 #include "media/audio/fake_audio_log_factory.h"
16 #include "media/audio/fake_audio_manager.h" 17 #include "media/audio/fake_audio_manager.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 19
19 #if defined(USE_ALSA) 20 #if defined(USE_ALSA)
20 #include "media/audio/alsa/audio_manager_alsa.h" 21 #include "media/audio/alsa/audio_manager_alsa.h"
21 #endif // defined(USE_ALSA) 22 #endif // defined(USE_ALSA)
22 23
23 #if defined(OS_WIN) 24 #if defined(OS_WIN)
24 #include "base/win/scoped_com_initializer.h" 25 #include "base/win/scoped_com_initializer.h"
25 #include "media/audio/win/audio_manager_win.h" 26 #include "media/audio/win/audio_manager_win.h"
26 #include "media/audio/win/wavein_input_win.h" 27 #include "media/audio/win/wavein_input_win.h"
27 #endif 28 #endif
28 29
29 #if defined(USE_PULSEAUDIO) 30 #if defined(USE_PULSEAUDIO)
30 #include "media/audio/pulse/audio_manager_pulse.h" 31 #include "media/audio/pulse/audio_manager_pulse.h"
31 #endif // defined(USE_PULSEAUDIO) 32 #endif // defined(USE_PULSEAUDIO)
32 33
33 namespace media { 34 namespace media {
34 35
36 namespace {
37 template <typename T>
38 struct AudioManagerFactory {
39 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
40 base::Thread* audio_thread = AudioThread::Get();
41 return ScopedAudioManagerPtr(new T(audio_thread->task_runner(),
42 audio_thread->task_runner(),
43 audio_log_factory));
44 };
45 };
46
47 #if defined(USE_PULSEAUDIO)
48 template <>
49 struct AudioManagerFactory<AudioManagerPulse> {
50 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
51 base::Thread* audio_thread = AudioThread::Get();
52 scoped_ptr<AudioManagerPulse, AudioManagerDeleter> manager(
53 new AudioManagerPulse(audio_thread->task_runner(),
54 audio_thread->task_runner(), 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 AudioManagerFactory<std::nullptr_t> {
64 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) {
65 return AudioManager::Create(nullptr, nullptr, nullptr, audio_log_factory);
66 };
67 };
68 } // namespace
69
35 // Test fixture which allows us to override the default enumeration API on 70 // Test fixture which allows us to override the default enumeration API on
36 // Windows. 71 // Windows.
37 class AudioManagerTest : public ::testing::Test { 72 class AudioManagerTest : public ::testing::Test {
38 public: 73 public:
39 void HandleDefaultDeviceIDsTest() { 74 void HandleDefaultDeviceIDsTest() {
40 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY, 75 AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
41 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048); 76 CHANNEL_LAYOUT_STEREO, 48000, 16, 2048);
42 77
43 // Create a stream with the default device id "". 78 // Create a stream with the default device id "".
44 AudioOutputStream* stream = 79 AudioOutputStream* stream =
(...skipping 25 matching lines...) Expand all
70 *params = audio_manager_->GetDefaultOutputStreamParameters(); 105 *params = audio_manager_->GetDefaultOutputStreamParameters();
71 } 106 }
72 107
73 void GetAssociatedOutputDeviceID(const std::string& input_device_id, 108 void GetAssociatedOutputDeviceID(const std::string& input_device_id,
74 std::string* output_device_id) { 109 std::string* output_device_id) {
75 *output_device_id = 110 *output_device_id =
76 audio_manager_->GetAssociatedOutputDeviceID(input_device_id); 111 audio_manager_->GetAssociatedOutputDeviceID(input_device_id);
77 } 112 }
78 113
79 protected: 114 protected:
80 AudioManagerTest() : audio_manager_(AudioManager::CreateForTesting()) { 115 AudioManagerTest() { CreateAudioManagerForTesting(); }
81 // Wait for audio thread initialization to complete. Otherwise the 116 ~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 117
89 #if defined(OS_WIN) 118 #if defined(OS_WIN)
90 bool SetMMDeviceEnumeration() { 119 bool SetMMDeviceEnumeration() {
91 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get()); 120 AudioManagerWin* amw = static_cast<AudioManagerWin*>(audio_manager_.get());
92 // Windows Wave is used as default if Windows XP was detected => 121 // Windows Wave is used as default if Windows XP was detected =>
93 // return false since MMDevice is not supported on XP. 122 // return false since MMDevice is not supported on XP.
94 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration) 123 if (amw->enumeration_type() == AudioManagerWin::kWaveEnumeration)
95 return false; 124 return false;
96 125
97 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration); 126 amw->SetEnumerationType(AudioManagerWin::kMMDeviceEnumeration);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 return has_devices; 193 return has_devices;
165 } 194 }
166 195
167 bool OutputDevicesAvailable() { 196 bool OutputDevicesAvailable() {
168 bool has_devices = false; 197 bool has_devices = false;
169 RunOnAudioThread(base::Bind(&AudioManagerTest::HasOutputDevicesAvailable, 198 RunOnAudioThread(base::Bind(&AudioManagerTest::HasOutputDevicesAvailable,
170 base::Unretained(this), &has_devices)); 199 base::Unretained(this), &has_devices));
171 return has_devices; 200 return has_devices;
172 } 201 }
173 202
174 #if defined(USE_ALSA) || defined(USE_PULSEAUDIO) 203 template <typename T = std::nullptr_t>
175 template <class T>
176 void CreateAudioManagerForTesting() { 204 void CreateAudioManagerForTesting() {
177 // Only one AudioManager may exist at a time, so destroy the one we're 205 // Only one AudioManager may exist at a time, so destroy the one we're
178 // currently holding before creating a new one. 206 // currently holding before creating a new one.
179 audio_manager_.reset(); 207 audio_manager_.reset();
180 audio_manager_.reset(T::Create(&fake_audio_log_factory_)); 208 audio_manager_ = AudioManagerFactory<T>::Create(&fake_audio_log_factory_);
181 } 209 }
182 #endif
183 210
184 // Synchronously runs the provided callback/closure on the audio thread. 211 // Synchronously runs the provided callback/closure on the audio thread.
185 void RunOnAudioThread(const base::Closure& closure) { 212 void RunOnAudioThread(const base::Closure& closure) {
186 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) { 213 if (!audio_manager_->GetTaskRunner()->BelongsToCurrentThread()) {
187 base::WaitableEvent event(false, false); 214 base::WaitableEvent event(false, false);
188 audio_manager_->GetTaskRunner()->PostTask( 215 audio_manager_->GetTaskRunner()->PostTask(
189 FROM_HERE, 216 FROM_HERE,
190 base::Bind(&AudioManagerTest::RunOnAudioThreadImpl, 217 base::Bind(&AudioManagerTest::RunOnAudioThreadImpl,
191 base::Unretained(this), 218 base::Unretained(this),
192 closure, 219 closure,
193 &event)); 220 &event));
194 event.Wait(); 221 event.Wait();
195 } else { 222 } else {
196 closure.Run(); 223 closure.Run();
197 } 224 }
198 } 225 }
199 226
200 void RunOnAudioThreadImpl(const base::Closure& closure, 227 void RunOnAudioThreadImpl(const base::Closure& closure,
201 base::WaitableEvent* event) { 228 base::WaitableEvent* event) {
202 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread()); 229 DCHECK(audio_manager_->GetTaskRunner()->BelongsToCurrentThread());
203 closure.Run(); 230 closure.Run();
204 event->Signal(); 231 event->Signal();
205 } 232 }
206 233
207 FakeAudioLogFactory fake_audio_log_factory_; 234 FakeAudioLogFactory fake_audio_log_factory_;
208 scoped_ptr<AudioManager> audio_manager_; 235 ScopedAudioManagerPtr audio_manager_;
209 }; 236 };
210 237
211 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) { 238 TEST_F(AudioManagerTest, HandleDefaultDeviceIDs) {
212 // Use a fake manager so we can makeup device ids, this will still use the 239 // Use a fake manager so we can makeup device ids, this will still use the
213 // AudioManagerBase code. 240 // AudioManagerBase code.
214 audio_manager_.reset(new FakeAudioManager(&fake_audio_log_factory_)); 241 CreateAudioManagerForTesting<FakeAudioManager>();
215 RunOnAudioThread(base::Bind(&AudioManagerTest::HandleDefaultDeviceIDsTest, 242 RunOnAudioThread(base::Bind(&AudioManagerTest::HandleDefaultDeviceIDsTest,
216 base::Unretained(this))); 243 base::Unretained(this)));
217 } 244 }
218 245
219 // Test that devices can be enumerated. 246 // Test that devices can be enumerated.
220 TEST_F(AudioManagerTest, EnumerateInputDevices) { 247 TEST_F(AudioManagerTest, EnumerateInputDevices) {
221 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable()); 248 ABORT_AUDIO_TEST_IF_NOT(InputDevicesAvailable());
222 249
223 AudioDeviceNames device_names; 250 AudioDeviceNames device_names;
224 RunOnAudioThread( 251 RunOnAudioThread(
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 DVLOG(2) << it->unique_id << " matches with " << output_device_id; 459 DVLOG(2) << it->unique_id << " matches with " << output_device_id;
433 found_an_associated_device = true; 460 found_an_associated_device = true;
434 } 461 }
435 } 462 }
436 463
437 EXPECT_TRUE(found_an_associated_device); 464 EXPECT_TRUE(found_an_associated_device);
438 #endif // defined(OS_WIN) || defined(OS_MACOSX) 465 #endif // defined(OS_WIN) || defined(OS_MACOSX)
439 } 466 }
440 467
441 } // namespace media 468 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698