| 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_name.h" | 21 #include "media/audio/audio_device_name.h" |
| 22 #include "media/audio/audio_output_proxy.h" | 22 #include "media/audio/audio_output_proxy.h" |
| 23 #include "media/audio/audio_unittest_util.h" | 23 #include "media/audio/audio_unittest_util.h" |
| 24 #include "media/audio/fake_audio_log_factory.h" | 24 #include "media/audio/fake_audio_log_factory.h" |
| 25 #include "media/audio/fake_audio_manager.h" | 25 #include "media/audio/fake_audio_manager.h" |
| 26 #include "media/audio/test_audio_thread.h" |
| 26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 29 |
| 29 #if defined(USE_ALSA) | 30 #if defined(USE_ALSA) |
| 30 #include "media/audio/alsa/audio_manager_alsa.h" | 31 #include "media/audio/alsa/audio_manager_alsa.h" |
| 31 #endif // defined(USE_ALSA) | 32 #endif // defined(USE_ALSA) |
| 32 | 33 |
| 33 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 34 #include "base/win/scoped_com_initializer.h" | 35 #include "base/win/scoped_com_initializer.h" |
| 35 #include "media/audio/win/audio_manager_win.h" | 36 #include "media/audio/win/audio_manager_win.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 46 #include "chromeos/dbus/fake_cras_audio_client.h" | 47 #include "chromeos/dbus/fake_cras_audio_client.h" |
| 47 #include "media/audio/cras/audio_manager_cras.h" | 48 #include "media/audio/cras/audio_manager_cras.h" |
| 48 #endif // defined(USE_CRAS) | 49 #endif // defined(USE_CRAS) |
| 49 | 50 |
| 50 namespace media { | 51 namespace media { |
| 51 | 52 |
| 52 namespace { | 53 namespace { |
| 53 | 54 |
| 54 template <typename T> | 55 template <typename T> |
| 55 struct TestAudioManagerFactory { | 56 struct TestAudioManagerFactory { |
| 56 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 57 static std::unique_ptr<AudioManager> Create( |
| 57 return ScopedAudioManagerPtr(new T(base::ThreadTaskRunnerHandle::Get(), | 58 AudioLogFactory* audio_log_factory) { |
| 58 base::ThreadTaskRunnerHandle::Get(), | 59 return base::MakeUnique<T>(base::MakeUnique<TestAudioThread>(), |
| 59 audio_log_factory)); | 60 audio_log_factory); |
| 60 } | 61 } |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 #if defined(USE_PULSEAUDIO) | 64 #if defined(USE_PULSEAUDIO) |
| 64 template <> | 65 template <> |
| 65 struct TestAudioManagerFactory<AudioManagerPulse> { | 66 struct TestAudioManagerFactory<AudioManagerPulse> { |
| 66 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 67 static std::unique_ptr<AudioManager> Create( |
| 67 std::unique_ptr<AudioManagerPulse, AudioManagerDeleter> manager( | 68 AudioLogFactory* audio_log_factory) { |
| 68 new AudioManagerPulse(base::ThreadTaskRunnerHandle::Get(), | 69 auto manager = base::MakeUnique<AudioManagerPulse>( |
| 69 base::ThreadTaskRunnerHandle::Get(), | 70 base::MakeUnique<TestAudioThread>(), audio_log_factory); |
| 70 audio_log_factory)); | |
| 71 if (!manager->Init()) | 71 if (!manager->Init()) |
| 72 manager.reset(); | 72 manager.reset(); |
| 73 return std::move(manager); | 73 return manager; |
| 74 } | 74 } |
| 75 }; | 75 }; |
| 76 #endif // defined(USE_PULSEAUDIO) | 76 #endif // defined(USE_PULSEAUDIO) |
| 77 | 77 |
| 78 template <> | 78 template <> |
| 79 struct TestAudioManagerFactory<std::nullptr_t> { | 79 struct TestAudioManagerFactory<std::nullptr_t> { |
| 80 static ScopedAudioManagerPtr Create(AudioLogFactory* audio_log_factory) { | 80 static std::unique_ptr<AudioManager> Create( |
| 81 return AudioManager::CreateForTesting(base::ThreadTaskRunnerHandle::Get()); | 81 AudioLogFactory* audio_log_factory) { |
| 82 return AudioManager::CreateForTesting(base::MakeUnique<TestAudioThread>()); |
| 82 } | 83 } |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 #if defined(USE_CRAS) | 86 #if defined(USE_CRAS) |
| 86 using chromeos::AudioNode; | 87 using chromeos::AudioNode; |
| 87 using chromeos::AudioNodeList; | 88 using chromeos::AudioNodeList; |
| 88 | 89 |
| 89 const uint64_t kJabraSpeaker1Id = 30001; | 90 const uint64_t kJabraSpeaker1Id = 30001; |
| 90 const uint64_t kJabraSpeaker1StableDeviceId = 80001; | 91 const uint64_t kJabraSpeaker1StableDeviceId = 80001; |
| 91 const uint64_t kJabraSpeaker2Id = 30002; | 92 const uint64_t kJabraSpeaker2Id = 30002; |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 audio_client_->SetAudioNodesForTesting(audio_nodes); | 227 audio_client_->SetAudioNodesForTesting(audio_nodes); |
| 227 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub(); | 228 audio_pref_handler_ = new chromeos::AudioDevicesPrefHandlerStub(); |
| 228 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_); | 229 chromeos::CrasAudioHandler::Initialize(audio_pref_handler_); |
| 229 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); | 230 cras_audio_handler_ = chromeos::CrasAudioHandler::Get(); |
| 230 base::RunLoop().RunUntilIdle(); | 231 base::RunLoop().RunUntilIdle(); |
| 231 } | 232 } |
| 232 #endif // defined(USE_CRAS) | 233 #endif // defined(USE_CRAS) |
| 233 | 234 |
| 234 protected: | 235 protected: |
| 235 AudioManagerTest() { CreateAudioManagerForTesting(); } | 236 AudioManagerTest() { CreateAudioManagerForTesting(); } |
| 236 ~AudioManagerTest() override {} | 237 ~AudioManagerTest() override { audio_manager_->Shutdown(); } |
| 237 | 238 |
| 238 // Helper method which verifies that the device list starts with a valid | 239 // Helper method which verifies that the device list starts with a valid |
| 239 // default record followed by non-default device names. | 240 // default record followed by non-default device names. |
| 240 static void CheckDeviceDescriptions( | 241 static void CheckDeviceDescriptions( |
| 241 const AudioDeviceDescriptions& device_descriptions) { | 242 const AudioDeviceDescriptions& device_descriptions) { |
| 242 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices."; | 243 DVLOG(2) << "Got " << device_descriptions.size() << " audio devices."; |
| 243 if (!device_descriptions.empty()) { | 244 if (!device_descriptions.empty()) { |
| 244 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin(); | 245 AudioDeviceDescriptions::const_iterator it = device_descriptions.begin(); |
| 245 | 246 |
| 246 // The first device in the list should always be the default device. | 247 // 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... |
| 323 } | 324 } |
| 324 bool OutputDevicesAvailable() { | 325 bool OutputDevicesAvailable() { |
| 325 return audio_manager_->HasAudioOutputDevices(); | 326 return audio_manager_->HasAudioOutputDevices(); |
| 326 } | 327 } |
| 327 | 328 |
| 328 template <typename T = std::nullptr_t> | 329 template <typename T = std::nullptr_t> |
| 329 void CreateAudioManagerForTesting() { | 330 void CreateAudioManagerForTesting() { |
| 330 // Only one AudioManager may exist at a time, so destroy the one we're | 331 // Only one AudioManager may exist at a time, so destroy the one we're |
| 331 // currently holding before creating a new one. | 332 // currently holding before creating a new one. |
| 332 // Flush the message loop to run any shutdown tasks posted by AudioManager. | 333 // Flush the message loop to run any shutdown tasks posted by AudioManager. |
| 333 audio_manager_.reset(); | 334 if (audio_manager_) { |
| 334 base::RunLoop().RunUntilIdle(); | 335 audio_manager_->Shutdown(); |
| 336 audio_manager_.reset(); |
| 337 } |
| 335 | 338 |
| 336 audio_manager_ = | 339 audio_manager_ = |
| 337 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); | 340 TestAudioManagerFactory<T>::Create(&fake_audio_log_factory_); |
| 338 // A few AudioManager implementations post initialization tasks to | 341 // A few AudioManager implementations post initialization tasks to |
| 339 // audio thread. Flush the thread to ensure that |audio_manager_| is | 342 // audio thread. Flush the thread to ensure that |audio_manager_| is |
| 340 // initialized and ready to use before returning from this function. | 343 // initialized and ready to use before returning from this function. |
| 341 // TODO(alokp): We should perhaps do this in AudioManager::Create(). | 344 // TODO(alokp): We should perhaps do this in AudioManager::Create(). |
| 342 base::RunLoop().RunUntilIdle(); | 345 base::RunLoop().RunUntilIdle(); |
| 343 } | 346 } |
| 344 | 347 |
| 345 base::TestMessageLoop message_loop_; | 348 base::TestMessageLoop message_loop_; |
| 346 FakeAudioLogFactory fake_audio_log_factory_; | 349 FakeAudioLogFactory fake_audio_log_factory_; |
| 347 ScopedAudioManagerPtr audio_manager_; | 350 std::unique_ptr<AudioManager> audio_manager_; |
| 348 | 351 |
| 349 #if defined(USE_CRAS) | 352 #if defined(USE_CRAS) |
| 350 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. | 353 chromeos::CrasAudioHandler* cras_audio_handler_ = nullptr; // Not owned. |
| 351 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned. | 354 chromeos::FakeCrasAudioClient* audio_client_ = nullptr; // Not owned. |
| 352 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_; | 355 scoped_refptr<chromeos::AudioDevicesPrefHandlerStub> audio_pref_handler_; |
| 353 #endif // defined(USE_CRAS) | 356 #endif // defined(USE_CRAS) |
| 354 }; | 357 }; |
| 355 | 358 |
| 356 #if defined(USE_CRAS) | 359 #if defined(USE_CRAS) |
| 357 TEST_F(AudioManagerTest, EnumerateInputDevicesCras) { | 360 TEST_F(AudioManagerTest, EnumerateInputDevicesCras) { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 private: | 573 private: |
| 571 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager); | 574 DISALLOW_COPY_AND_ASSIGN(MockAudioDebugRecordingManager); |
| 572 }; | 575 }; |
| 573 | 576 |
| 574 class TestAudioManager : public FakeAudioManager { | 577 class TestAudioManager : public FakeAudioManager { |
| 575 // For testing the default implementation of GetGroupId(Input|Output) | 578 // For testing the default implementation of GetGroupId(Input|Output) |
| 576 // input$i is associated to output$i, if both exist. | 579 // input$i is associated to output$i, if both exist. |
| 577 // Default input is input1. | 580 // Default input is input1. |
| 578 // Default output is output2. | 581 // Default output is output2. |
| 579 public: | 582 public: |
| 580 TestAudioManager( | 583 TestAudioManager(std::unique_ptr<AudioThread> audio_thread, |
| 581 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 584 AudioLogFactory* audio_log_factory) |
| 582 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | 585 : FakeAudioManager(std::move(audio_thread), audio_log_factory) {} |
| 583 AudioLogFactory* audio_log_factory) | |
| 584 : FakeAudioManager(task_runner, worker_task_runner, audio_log_factory) {} | |
| 585 | 586 |
| 586 std::string GetDefaultOutputDeviceID() override { return "output4"; } | 587 std::string GetDefaultOutputDeviceID() override { return "output4"; } |
| 587 | 588 |
| 588 std::string GetAssociatedOutputDeviceID( | 589 std::string GetAssociatedOutputDeviceID( |
| 589 const std::string& input_id) override { | 590 const std::string& input_id) override { |
| 590 if (input_id == "input1") | 591 if (input_id == "input1") |
| 591 return "output1"; | 592 return "output1"; |
| 592 if (input_id == "input2") | 593 if (input_id == "input2") |
| 593 return "output2"; | 594 return "output2"; |
| 594 if (input_id == "default") | 595 if (input_id == "default") |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 | 665 |
| 665 base::FilePath file_path(FILE_PATH_LITERAL("path")); | 666 base::FilePath file_path(FILE_PATH_LITERAL("path")); |
| 666 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); | 667 EXPECT_CALL(*mock_debug_recording_manager, EnableDebugRecording(file_path)); |
| 667 audio_manager_->EnableOutputDebugRecording(file_path); | 668 audio_manager_->EnableOutputDebugRecording(file_path); |
| 668 | 669 |
| 669 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); | 670 EXPECT_CALL(*mock_debug_recording_manager, DisableDebugRecording()); |
| 670 audio_manager_->DisableOutputDebugRecording(); | 671 audio_manager_->DisableOutputDebugRecording(); |
| 671 } | 672 } |
| 672 | 673 |
| 673 } // namespace media | 674 } // namespace media |
| OLD | NEW |