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 "media/audio/audio_manager.h" | 5 #include "media/audio/audio_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/test/test_message_loop.h" | 17 #include "base/test/test_message_loop.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "media/audio/audio_device_description.h" | 20 #include "media/audio/audio_device_description.h" |
21 #include "media/audio/audio_device_info_accessor_for_tests.h" | 21 #include "media/audio/audio_device_info_accessor_for_tests.h" |
22 #include "media/audio/audio_device_name.h" | 22 #include "media/audio/audio_device_name.h" |
23 #include "media/audio/audio_output_proxy.h" | 23 #include "media/audio/audio_output_proxy.h" |
24 #include "media/audio/audio_unittest_util.h" | 24 #include "media/audio/audio_unittest_util.h" |
25 #include "media/audio/fake_audio_log_factory.h" | 25 #include "media/audio/fake_audio_log_factory.h" |
26 #include "media/audio/fake_audio_manager.h" | 26 #include "media/audio/fake_audio_manager.h" |
| 27 #include "media/audio/test_audio_thread.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 28 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
29 | 30 |
30 #if defined(USE_ALSA) | 31 #if defined(USE_ALSA) |
31 #include "media/audio/alsa/audio_manager_alsa.h" | 32 #include "media/audio/alsa/audio_manager_alsa.h" |
32 #endif // defined(USE_ALSA) | 33 #endif // defined(USE_ALSA) |
33 | 34 |
34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
35 #include "base/win/scoped_com_initializer.h" | 36 #include "base/win/scoped_com_initializer.h" |
36 #include "media/audio/win/audio_manager_win.h" | 37 #include "media/audio/win/audio_manager_win.h" |
37 #endif | 38 #endif |
38 | 39 |
39 #if defined(USE_PULSEAUDIO) | 40 #if defined(USE_PULSEAUDIO) |
40 #include "media/audio/pulse/audio_manager_pulse.h" | 41 #include "media/audio/pulse/audio_manager_pulse.h" |
| 42 #include "media/audio/pulse/pulse_util.h" |
41 #endif // defined(USE_PULSEAUDIO) | 43 #endif // defined(USE_PULSEAUDIO) |
42 | 44 |
43 #if defined(USE_CRAS) | 45 #if defined(USE_CRAS) |
44 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 46 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
45 #include "chromeos/audio/cras_audio_handler.h" | 47 #include "chromeos/audio/cras_audio_handler.h" |
46 #include "chromeos/dbus/dbus_thread_manager.h" | 48 #include "chromeos/dbus/dbus_thread_manager.h" |
47 #include "chromeos/dbus/fake_cras_audio_client.h" | 49 #include "chromeos/dbus/fake_cras_audio_client.h" |
48 #include "media/audio/cras/audio_manager_cras.h" | 50 #include "media/audio/cras/audio_manager_cras.h" |
49 #endif // defined(USE_CRAS) | 51 #endif // defined(USE_CRAS) |
50 | 52 |
51 namespace media { | 53 namespace media { |
52 | 54 |
53 namespace { | 55 namespace { |
54 | 56 |
55 template <typename T> | 57 template <typename T> |
56 struct TestAudioManagerFactory { | 58 struct TestAudioManagerFactory { |
57 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 59 static std::unique_ptr<AudioManager> Create( |
58 return ScopedAudioManagerPtr(new T(base::ThreadTaskRunnerHandle::Get(), | 60 AudioLogFactory* audio_log_factory) { |
59 base::ThreadTaskRunnerHandle::Get(), | 61 return base::MakeUnique<T>(base::MakeUnique<TestAudioThread>(), |
60 audio_log_factory)); | 62 audio_log_factory); |
61 } | 63 } |
62 }; | 64 }; |
63 | 65 |
64 #if defined(USE_PULSEAUDIO) | 66 #if defined(USE_PULSEAUDIO) |
65 template <> | 67 template <> |
66 struct TestAudioManagerFactory<AudioManagerPulse> { | 68 struct TestAudioManagerFactory<AudioManagerPulse> { |
67 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 69 static std::unique_ptr<AudioManager> Create( |
68 std::unique_ptr<AudioManagerPulse, AudioManagerDeleter> manager( | 70 AudioLogFactory* audio_log_factory) { |
69 new AudioManagerPulse(base::ThreadTaskRunnerHandle::Get(), | 71 pa_threaded_mainloop* pa_mainloop = nullptr; |
70 base::ThreadTaskRunnerHandle::Get(), | 72 pa_context* pa_context = nullptr; |
71 audio_log_factory)); | 73 if (!pulse::InitPulse(&pa_mainloop, &pa_context)) |
72 if (!manager->Init()) | 74 return nullptr; |
73 manager.reset(); | 75 return base::MakeUnique<AudioManagerPulse>( |
74 return std::move(manager); | 76 base::MakeUnique<TestAudioThread>(), audio_log_factory, pa_mainloop, |
| 77 pa_context); |
75 } | 78 } |
76 }; | 79 }; |
77 #endif // defined(USE_PULSEAUDIO) | 80 #endif // defined(USE_PULSEAUDIO) |
78 | 81 |
79 template <> | 82 template <> |
80 struct TestAudioManagerFactory<std::nullptr_t> { | 83 struct TestAudioManagerFactory<std::nullptr_t> { |
81 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 84 static std::unique_ptr<AudioManager> Create( |
82 return AudioManager::CreateForTesting(base::ThreadTaskRunnerHandle::Get()); | 85 AudioLogFactory* audio_log_factory) { |
| 86 return AudioManager::CreateForTesting(base::MakeUnique<TestAudioThread>()); |
83 } | 87 } |
84 }; | 88 }; |
85 | 89 |
86 #if defined(USE_CRAS) | 90 #if defined(USE_CRAS) |
87 using chromeos::AudioNode; | 91 using chromeos::AudioNode; |
88 using chromeos::AudioNodeList; | 92 using chromeos::AudioNodeList; |
89 | 93 |
90 const uint64_t kJabraSpeaker1Id = 30001; | 94 const uint64_t kJabraSpeaker1Id = 30001; |
91 const uint64_t kJabraSpeaker1StableDeviceId = 80001; | 95 const uint64_t kJabraSpeaker1StableDeviceId = 80001; |
92 const uint64_t kJabraSpeaker2Id = 30002; | 96 const uint64_t kJabraSpeaker2Id = 30002; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 audio_client_->SetAudioNodesForTesting(audio_nodes); | 231 audio_client_->SetAudioNodesForTesting(audio_nodes); |
228 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub(); | 232 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub(); |
229 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_); | 233 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_); |
230 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); | 234 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); |
231 base::RunLoop().RunUntilIdle(); | 235 base::RunLoop().RunUntilIdle(); |
232 } | 236 } |
233 #endif // defined(USE_CRAS) | 237 #endif // defined(USE_CRAS) |
234 | 238 |
235 protected: | 239 protected: |
236 AudioManagerTest() { CreateAudioManagerForTesting(); } | 240 AudioManagerTest() { CreateAudioManagerForTesting(); } |
237 ~AudioManagerTest() override {} | 241 ~AudioManagerTest() override { audio_manager_->Shutdown(); } |
238 | 242 |
239 // Helper method which verifies that the device list starts with a valid | 243 // Helper method which verifies that the device list starts with a valid |
240 // default record followed by non-default device names. | 244 // default record followed by non-default device names. |
241 static void CheckDeviceDescriptions( | 245 static void CheckDeviceDescriptions( |
242 const AudioDeviceDescriptions& device_descriptions) { | 246 const AudioDeviceDescriptions& device_descriptions) { |
243 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices."; | 247 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices."; |
244 if (!device_descriptions.empty()) { | 248 if (!device_descriptions.empty()) { |
245 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin(); | 249 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin(); |
246 | 250 |
247 // The first device in the list should always be the default device. | 251 // The first device in the list should always be the default device. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 } | 328 } |
325 bool OutputDevicesAvailable() { | 329 bool OutputDevicesAvailable() { |
326 return device_info_accessor_->HasAudioOutputDevices(); | 330 return device_info_accessor_->HasAudioOutputDevices(); |
327 } | 331 } |
328 | 332 |
329 template <typename T = std::nullptr_t> | 333 template <typename T = std::nullptr_t> |
330 void CreateAudioManagerForTesting() { | 334 void CreateAudioManagerForTesting() { |
331 // Only one AudioManager may exist at a time, so destroy the one we're | 335 // Only one AudioManager may exist at a time, so destroy the one we're |
332 // currently holding before creating a new one. | 336 // currently holding before creating a new one. |
333 // Flush the message loop to run any shutdown tasks posted by AudioManager. | 337 // Flush the message loop to run any shutdown tasks posted by AudioManager. |
334 audio_manager_.reset(); | 338 if (audio_manager_) { |
335 base::RunLoop().RunUntilIdle(); | 339 audio_manager_->Shutdown(); |
| 340 audio_manager_.reset(); |
| 341 } |
336 | 342 |
337 audio_manager_ = | 343 audio_manager_ = |
338 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); | 344 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); |
339 // A few AudioManager implementations post initialization tasks to | 345 // A few AudioManager implementations post initialization tasks to |
340 // audio thread. Flush the thread to ensure that |audio_manager_| is | 346 // audio thread. Flush the thread to ensure that |audio_manager_| is |
341 // initialized and ready to use before returning from this function. | 347 // initialized and ready to use before returning from this function. |
342 // TODO(alokp): We should perhaps do this in AudioManager::Create(). | 348 // TODO(alokp): We should perhaps do this in AudioManager::Create(). |
343 base::RunLoop().RunUntilIdle(); | 349 base::RunLoop().RunUntilIdle(); |
344 device_info_accessor_ = | 350 device_info_accessor_ = |
345 base::MakeUnique<AudioDeviceInfoAccessorForTests>(audio_manager_.get()); | 351 base::MakeUnique<AudioDeviceInfoAccessorForTests>(audio_manager_.get()); |
346 } | 352 } |
347 | 353 |
348 base::TestMessageLoop message_loop_; | 354 base::TestMessageLoop message_loop_; |
349 FakeAudioLogFactory fake_audio_log_factory_; | 355 FakeAudioLogFactory fake_audio_log_factory_; |
350 ScopedAudioManagerPtr audio_manager_; | 356 std::unique_ptr<AudioManager> audio_manager_; |
351 std::unique_ptr<AudioDeviceInfoAccessorForTests> device_info_accessor_; | 357 std::unique_ptr<AudioDeviceInfoAccessorForTests> device_info_accessor_; |
352 | 358 |
353 #if defined(USE_CRAS) | 359 #if defined(USE_CRAS) |
354 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. | 360 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. |
355 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned. | 361 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned. |
356 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_; | 362 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_; |
357 #endif // defined(USE_CRAS) | 363 #endif // defined(USE_CRAS) |
358 }; | 364 }; |
359 | 365 |
360 #if defined(USE_CRAS) | 366 #if defined(USE_CRAS) |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 private: | 582 private: |
577 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager); | 583 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager); |
578 }; | 584 }; |
579 | 585 |
580 class TestAudioManager : public FakeAudioManager { | 586 class TestAudioManager : public FakeAudioManager { |
581 // For testing the default implementation of GetGroupId(Input|Output) | 587 // For testing the default implementation of GetGroupId(Input|Output) |
582 // input$i is associated to output$i, if both exist. | 588 // input$i is associated to output$i, if both exist. |
583 // Default input is input1. | 589 // Default input is input1. |
584 // Default output is output2. | 590 // Default output is output2. |
585 public: | 591 public: |
586 TestAudioManager( | 592 TestAudioManager(std::unique_ptr<AudioThread> audio_thread, |
587 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 593 AudioLogFactory* audio_log_factory) |
588 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 594 : FakeAudioManager(std::move(audio_thread), audio_log_factory) {} |
589 AudioLogFactory* audio_log_factory) | |
590 : FakeAudioManager(task_runner, worker_task_runner, audio_log_factory) {} | |
591 | 595 |
592 std::string GetDefaultOutputDeviceID() override { return "output4"; } | 596 std::string GetDefaultOutputDeviceID() override { return "output4"; } |
593 | 597 |
594 std::string GetAssociatedOutputDeviceID( | 598 std::string GetAssociatedOutputDeviceID( |
595 const std::string& input_id) override { | 599 const std::string& input_id) override { |
596 if (input_id == "input1") | 600 if (input_id == "input1") |
597 return "output1"; | 601 return "output1"; |
598 if (input_id == "input2") | 602 if (input_id == "input2") |
599 return "output2"; | 603 return "output2"; |
600 if (input_id == "default") | 604 if (input_id == "default") |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 | 674 |
671 base::FilePath file_path(FILE_PATH_LITERAL("path")); | 675 base::FilePath file_path(FILE_PATH_LITERAL("path")); |
672 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); | 676 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); |
673 audio_manager_->EnableOutputDebugRecording(file_path); | 677 audio_manager_->EnableOutputDebugRecording(file_path); |
674 | 678 |
675 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); | 679 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); |
676 audio_manager_->DisableOutputDebugRecording(); | 680 audio_manager_->DisableOutputDebugRecording(); |
677 } | 681 } |
678 | 682 |
679 } // namespace media | 683 } // namespace media |
OLD | NEW |