OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_system_impl.h" | 5 #include "media/audio/audio_system_impl.h" |
6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "media/audio/audio_device_description.h" | 13 #include "media/audio/audio_device_description.h" |
| 14 #include "media/audio/audio_thread_impl.h" |
14 #include "media/audio/mock_audio_manager.h" | 15 #include "media/audio/mock_audio_manager.h" |
| 16 #include "media/audio/test_audio_thread.h" |
15 #include "media/base/test_helpers.h" | 17 #include "media/base/test_helpers.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 const char* kNonDefaultDeviceId = "non-default-device-id"; | 22 const char* kNonDefaultDeviceId = "non-default-device-id"; |
21 } | 23 } |
22 | 24 |
23 namespace media { | 25 namespace media { |
24 | 26 |
25 bool operator==(const media::AudioDeviceDescription& lhs, | 27 bool operator==(const media::AudioDeviceDescription& lhs, |
26 const media::AudioDeviceDescription& rhs) { | 28 const media::AudioDeviceDescription& rhs) { |
27 return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id && | 29 return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id && |
28 lhs.group_id == rhs.group_id; | 30 lhs.group_id == rhs.group_id; |
29 } | 31 } |
30 | 32 |
31 class AudioSystemImplTest : public testing::TestWithParam<bool> { | 33 class AudioSystemImplTest : public testing::TestWithParam<bool> { |
32 public: | 34 public: |
33 AudioSystemImplTest() | 35 AudioSystemImplTest() |
34 : use_audio_thread_(GetParam()), | 36 : use_audio_thread_(GetParam()), |
35 audio_thread_("AudioSystemThread"), | |
36 input_params_(AudioParameters::AUDIO_PCM_LINEAR, | 37 input_params_(AudioParameters::AUDIO_PCM_LINEAR, |
37 CHANNEL_LAYOUT_MONO, | 38 CHANNEL_LAYOUT_MONO, |
38 AudioParameters::kTelephoneSampleRate, | 39 AudioParameters::kTelephoneSampleRate, |
39 16, | 40 16, |
40 AudioParameters::kTelephoneSampleRate / 10), | 41 AudioParameters::kTelephoneSampleRate / 10), |
41 output_params_(AudioParameters::AUDIO_PCM_LINEAR, | 42 output_params_(AudioParameters::AUDIO_PCM_LINEAR, |
42 CHANNEL_LAYOUT_MONO, | 43 CHANNEL_LAYOUT_MONO, |
43 AudioParameters::kTelephoneSampleRate, | 44 AudioParameters::kTelephoneSampleRate, |
44 16, | 45 16, |
45 AudioParameters::kTelephoneSampleRate / 20), | 46 AudioParameters::kTelephoneSampleRate / 20), |
46 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR, | 47 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR, |
47 CHANNEL_LAYOUT_MONO, | 48 CHANNEL_LAYOUT_MONO, |
48 AudioParameters::kTelephoneSampleRate, | 49 AudioParameters::kTelephoneSampleRate, |
49 16, | 50 16, |
50 AudioParameters::kTelephoneSampleRate / 30) { | 51 AudioParameters::kTelephoneSampleRate / 30) { |
51 if (use_audio_thread_) { | 52 audio_manager_ = base::MakeUnique<MockAudioManager>( |
52 audio_thread_.StartAndWaitForTesting(); | 53 base::MakeUnique<TestAudioThread>(use_audio_thread_)); |
53 audio_manager_.reset( | |
54 new media::MockAudioManager(audio_thread_.task_runner())); | |
55 } else { | |
56 audio_manager_.reset(new media::MockAudioManager( | |
57 base::ThreadTaskRunnerHandle::Get().get())); | |
58 } | |
59 | |
60 audio_manager_->SetInputStreamParameters(input_params_); | 54 audio_manager_->SetInputStreamParameters(input_params_); |
61 audio_manager_->SetOutputStreamParameters(output_params_); | 55 audio_manager_->SetOutputStreamParameters(output_params_); |
62 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); | 56 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); |
63 | 57 |
64 auto get_device_descriptions = [](const AudioDeviceDescriptions* source, | 58 auto get_device_descriptions = [](const AudioDeviceDescriptions* source, |
65 AudioDeviceDescriptions* destination) { | 59 AudioDeviceDescriptions* destination) { |
66 destination->insert(destination->end(), source->begin(), source->end()); | 60 destination->insert(destination->end(), source->begin(), source->end()); |
67 }; | 61 }; |
68 | 62 |
69 audio_manager_->SetInputDeviceDescriptionsCallback( | 63 audio_manager_->SetInputDeviceDescriptionsCallback( |
70 base::Bind(get_device_descriptions, | 64 base::Bind(get_device_descriptions, |
71 base::Unretained(&input_device_descriptions_))); | 65 base::Unretained(&input_device_descriptions_))); |
72 audio_manager_->SetOutputDeviceDescriptionsCallback( | 66 audio_manager_->SetOutputDeviceDescriptionsCallback( |
73 base::Bind(get_device_descriptions, | 67 base::Bind(get_device_descriptions, |
74 base::Unretained(&output_device_descriptions_))); | 68 base::Unretained(&output_device_descriptions_))); |
75 | 69 |
76 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 70 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
77 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); | 71 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); |
78 } | 72 } |
79 | 73 |
80 ~AudioSystemImplTest() override { | 74 ~AudioSystemImplTest() override { audio_manager_->Shutdown(); } |
81 // Deleting |audio_manager_| on its thread. | |
82 audio_system_.reset(); | |
83 EXPECT_EQ(AudioSystem::Get(), nullptr); | |
84 audio_manager_.reset(); | |
85 audio_thread_.Stop(); | |
86 } | |
87 | 75 |
88 void OnAudioParams(const AudioParameters& expected, | 76 void OnAudioParams(const AudioParameters& expected, |
89 const AudioParameters& received) { | 77 const AudioParameters& received) { |
90 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 78 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
91 EXPECT_EQ(expected.AsHumanReadableString(), | 79 EXPECT_EQ(expected.AsHumanReadableString(), |
92 received.AsHumanReadableString()); | 80 received.AsHumanReadableString()); |
93 AudioParametersReceived(); | 81 AudioParametersReceived(); |
94 } | 82 } |
95 | 83 |
96 void OnHasInputDevices(bool result) { | 84 void OnHasInputDevices(bool result) { |
(...skipping 28 matching lines...) Expand all Loading... |
125 EXPECT_EQ(expected_associated_device_id, associated_device_id); | 113 EXPECT_EQ(expected_associated_device_id, associated_device_id); |
126 InputDeviceInfoReceived(); | 114 InputDeviceInfoReceived(); |
127 } | 115 } |
128 | 116 |
129 void WaitForCallback() { | 117 void WaitForCallback() { |
130 if (!use_audio_thread_) { | 118 if (!use_audio_thread_) { |
131 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
132 return; | 120 return; |
133 } | 121 } |
134 WaitableMessageLoopEvent event; | 122 WaitableMessageLoopEvent event; |
135 audio_thread_.task_runner()->PostTaskAndReply( | 123 audio_manager_->GetTaskRunner()->PostTaskAndReply( |
136 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | 124 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
137 // Runs the loop and waits for the |audio_thread_| to call event's closure, | 125 // Runs the loop and waits for the |audio_thread_| to call event's closure, |
138 // which means AudioSystem reply containing device parameters is already | 126 // which means AudioSystem reply containing device parameters is already |
139 // queued on the main thread. | 127 // queued on the main thread. |
140 event.RunAndWait(); | 128 event.RunAndWait(); |
141 base::RunLoop().RunUntilIdle(); | 129 base::RunLoop().RunUntilIdle(); |
142 } | 130 } |
143 | 131 |
144 // Mocks to verify that AudioSystem replied with an expected callback. | 132 // Mocks to verify that AudioSystem replied with an expected callback. |
145 MOCK_METHOD0(AudioParametersReceived, void(void)); | 133 MOCK_METHOD0(AudioParametersReceived, void(void)); |
146 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); | 134 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); |
147 MOCK_METHOD1(HasOutputDevicesCallback, void(bool)); | 135 MOCK_METHOD1(HasOutputDevicesCallback, void(bool)); |
148 MOCK_METHOD0(DeviceDescriptionsReceived, void(void)); | 136 MOCK_METHOD0(DeviceDescriptionsReceived, void(void)); |
149 MOCK_METHOD1(AssociatedOutputDeviceIDReceived, void(const std::string&)); | 137 MOCK_METHOD1(AssociatedOutputDeviceIDReceived, void(const std::string&)); |
150 MOCK_METHOD0(InputDeviceInfoReceived, void(void)); | 138 MOCK_METHOD0(InputDeviceInfoReceived, void(void)); |
151 | 139 |
152 protected: | 140 protected: |
153 base::MessageLoop message_loop_; | 141 base::MessageLoop message_loop_; |
154 base::ThreadChecker thread_checker_; | 142 base::ThreadChecker thread_checker_; |
155 bool use_audio_thread_; | 143 bool use_audio_thread_; |
156 base::Thread audio_thread_; | 144 std::unique_ptr<media::MockAudioManager> audio_manager_; |
157 MockAudioManager::UniquePtr audio_manager_; | |
158 std::unique_ptr<media::AudioSystem> audio_system_; | 145 std::unique_ptr<media::AudioSystem> audio_system_; |
159 AudioParameters input_params_; | 146 AudioParameters input_params_; |
160 AudioParameters output_params_; | 147 AudioParameters output_params_; |
161 AudioParameters default_output_params_; | 148 AudioParameters default_output_params_; |
162 AudioDeviceDescriptions input_device_descriptions_; | 149 AudioDeviceDescriptions input_device_descriptions_; |
163 AudioDeviceDescriptions output_device_descriptions_; | 150 AudioDeviceDescriptions output_device_descriptions_; |
164 }; | 151 }; |
165 | 152 |
166 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { | 153 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { |
167 EXPECT_CALL(*this, AudioParametersReceived()); | 154 EXPECT_CALL(*this, AudioParametersReceived()); |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 audio_system_->GetInputDeviceInfo( | 331 audio_system_->GetInputDeviceInfo( |
345 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnInputDeviceInfo, | 332 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnInputDeviceInfo, |
346 base::Unretained(this), input_params_, | 333 base::Unretained(this), input_params_, |
347 output_params_, associated_id)); | 334 output_params_, associated_id)); |
348 WaitForCallback(); | 335 WaitForCallback(); |
349 } | 336 } |
350 | 337 |
351 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); | 338 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); |
352 | 339 |
353 } // namespace media | 340 } // namespace media |
OLD | NEW |