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/mock_audio_manager.h" | 14 #include "media/audio/mock_audio_manager.h" |
15 #include "media/base/test_helpers.h" | 15 #include "media/base/test_helpers.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace { | |
20 const char* kNonDefaultDeviceId = "non-default-device-id"; | |
21 } | |
22 namespace media { | 19 namespace media { |
23 | 20 |
24 class AudioSystemImplTest : public testing::TestWithParam<bool> { | 21 class AudioSystemImplTest : public testing::TestWithParam<bool> { |
25 public: | 22 public: |
26 AudioSystemImplTest() | 23 AudioSystemImplTest() |
27 : use_audio_thread_(GetParam()), | 24 : use_audio_thread_(GetParam()), audio_thread_("AudioSystemThread") { |
28 audio_thread_("AudioSystemThread"), | |
29 input_params_(AudioParameters::AUDIO_PCM_LINEAR, | |
30 CHANNEL_LAYOUT_MONO, | |
31 AudioParameters::kTelephoneSampleRate, | |
32 16, | |
33 AudioParameters::kTelephoneSampleRate / 10), | |
34 output_params_(AudioParameters::AUDIO_PCM_LINEAR, | |
35 CHANNEL_LAYOUT_MONO, | |
36 AudioParameters::kTelephoneSampleRate, | |
37 16, | |
38 AudioParameters::kTelephoneSampleRate / 20), | |
39 default_output_params_(AudioParameters::AUDIO_PCM_LINEAR, | |
40 CHANNEL_LAYOUT_MONO, | |
41 AudioParameters::kTelephoneSampleRate, | |
42 16, | |
43 AudioParameters::kTelephoneSampleRate / 30) { | |
44 if (use_audio_thread_) { | 25 if (use_audio_thread_) { |
45 audio_thread_.StartAndWaitForTesting(); | 26 audio_thread_.StartAndWaitForTesting(); |
46 audio_manager_.reset( | 27 audio_manager_.reset( |
47 new media::MockAudioManager(audio_thread_.task_runner())); | 28 new media::MockAudioManager(audio_thread_.task_runner())); |
48 } else { | 29 } else { |
49 audio_manager_.reset(new media::MockAudioManager( | 30 audio_manager_.reset(new media::MockAudioManager( |
50 base::ThreadTaskRunnerHandle::Get().get())); | 31 base::ThreadTaskRunnerHandle::Get().get())); |
51 } | 32 } |
52 | 33 audio_manager_->SetInputStreamParameters( |
53 audio_manager_->SetInputStreamParameters(input_params_); | 34 media::AudioParameters::UnavailableDeviceParams()); |
54 audio_manager_->SetOutputStreamParameters(output_params_); | |
55 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); | |
56 | |
57 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 35 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
58 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); | 36 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); |
59 } | 37 } |
60 | 38 |
61 ~AudioSystemImplTest() override { | 39 ~AudioSystemImplTest() override { |
62 // Deleting |audio_manager_| on its thread. | 40 // Deleting |audio_manager_| on its thread. |
63 audio_system_.reset(); | 41 audio_system_.reset(); |
64 EXPECT_EQ(AudioSystem::Get(), nullptr); | 42 EXPECT_EQ(AudioSystem::Get(), nullptr); |
65 audio_manager_.reset(); | 43 audio_manager_.reset(); |
66 audio_thread_.Stop(); | 44 audio_thread_.Stop(); |
67 } | 45 } |
68 | 46 |
69 void OnAudioParams(const AudioParameters& expected, | 47 void OnAudioParams(const AudioParameters& expected, |
70 const AudioParameters& received) { | 48 const AudioParameters& received) { |
71 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 49 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
72 EXPECT_EQ(expected.AsHumanReadableString(), | 50 EXPECT_EQ(expected.AsHumanReadableString(), |
73 received.AsHumanReadableString()); | 51 received.AsHumanReadableString()); |
74 AudioParametersReceived(); | 52 AudioParametersReceived(); |
75 } | 53 } |
76 | 54 |
77 void OnHasInputDevices(bool result) { | |
78 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | |
79 HasInputDevicesCallback(result); | |
80 } | |
81 | |
82 void WaitForCallback() { | 55 void WaitForCallback() { |
83 if (!use_audio_thread_) { | 56 if (!use_audio_thread_) { |
84 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
85 return; | 58 return; |
86 } | 59 } |
87 WaitableMessageLoopEvent event; | 60 media::WaitableMessageLoopEvent event; |
88 audio_thread_.task_runner()->PostTaskAndReply( | 61 audio_thread_.task_runner()->PostTaskAndReply( |
89 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | 62 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
90 // Runs the loop and waits for the |audio_thread_| to call event's closure, | 63 // Runs the loop and waits for the |audio_thread_| to call event's closure, |
91 // which means AudioSystem reply containing device parameters is already | 64 // which means AudioSystem reply containing device parameters is already |
92 // queued on the main thread. | 65 // queued on the main thread. |
93 event.RunAndWait(); | 66 event.RunAndWait(); |
94 base::RunLoop().RunUntilIdle(); | 67 base::RunLoop().RunUntilIdle(); |
95 } | 68 } |
96 | 69 |
97 MOCK_METHOD0(AudioParametersReceived, void(void)); | 70 MOCK_METHOD0(AudioParametersReceived, void(void)); |
98 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); | 71 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); |
99 | 72 |
100 protected: | 73 protected: |
101 base::MessageLoop message_loop_; | 74 base::MessageLoop message_loop_; |
102 base::ThreadChecker thread_checker_; | 75 base::ThreadChecker thread_checker_; |
103 bool use_audio_thread_; | 76 bool use_audio_thread_; |
104 base::Thread audio_thread_; | 77 base::Thread audio_thread_; |
105 MockAudioManager::UniquePtr audio_manager_; | 78 MockAudioManager::UniquePtr audio_manager_; |
106 std::unique_ptr<media::AudioSystem> audio_system_; | 79 std::unique_ptr<media::AudioSystem> audio_system_; |
107 AudioParameters input_params_; | |
108 AudioParameters output_params_; | |
109 AudioParameters default_output_params_; | |
110 }; | 80 }; |
111 | 81 |
112 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { | 82 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { |
113 EXPECT_CALL(*this, AudioParametersReceived()); | 83 EXPECT_CALL(*this, AudioParametersReceived()); |
114 audio_system_->GetInputStreamParameters( | 84 audio_system_->GetInputStreamParameters( |
115 media::AudioDeviceDescription::kDefaultDeviceId, | 85 media::AudioDeviceDescription::kDefaultDeviceId, |
116 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | 86 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
117 input_params_)); | 87 media::AudioParameters::UnavailableDeviceParams())); |
118 WaitForCallback(); | 88 WaitForCallback(); |
119 } | 89 } |
120 | 90 |
121 TEST_P(AudioSystemImplTest, GetInputStreamParametersNoDevice) { | 91 TEST_P(AudioSystemImplTest, GetInputStreamParametersNoDevice) { |
122 audio_manager_->SetHasInputDevices(false); | 92 audio_manager_->SetHasInputDevices(false); |
123 EXPECT_CALL(*this, AudioParametersReceived()); | 93 EXPECT_CALL(*this, AudioParametersReceived()); |
124 audio_system_->GetInputStreamParameters( | 94 audio_system_->GetInputStreamParameters( |
125 media::AudioDeviceDescription::kDefaultDeviceId, | 95 media::AudioDeviceDescription::kDefaultDeviceId, |
126 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | 96 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
127 media::AudioParameters())); | 97 media::AudioParameters())); |
128 WaitForCallback(); | 98 WaitForCallback(); |
129 } | 99 } |
130 | 100 |
131 TEST_P(AudioSystemImplTest, GetStreamParameters) { | |
132 EXPECT_CALL(*this, AudioParametersReceived()); | |
133 audio_system_->GetOutputStreamParameters( | |
134 kNonDefaultDeviceId, base::Bind(&AudioSystemImplTest::OnAudioParams, | |
135 base::Unretained(this), output_params_)); | |
136 WaitForCallback(); | |
137 } | |
138 | |
139 TEST_P(AudioSystemImplTest, GetDefaultOutputStreamParameters) { | |
140 EXPECT_CALL(*this, AudioParametersReceived()); | |
141 audio_system_->GetOutputStreamParameters( | |
142 media::AudioDeviceDescription::kDefaultDeviceId, | |
143 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | |
144 default_output_params_)); | |
145 WaitForCallback(); | |
146 } | |
147 | |
148 TEST_P(AudioSystemImplTest, GetOutputStreamParametersNoDevice) { | |
149 audio_manager_->SetHasOutputDevices(false); | |
150 EXPECT_CALL(*this, AudioParametersReceived()).Times(2); | |
151 | |
152 audio_system_->GetOutputStreamParameters( | |
153 media::AudioDeviceDescription::kDefaultDeviceId, | |
154 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | |
155 media::AudioParameters())); | |
156 WaitForCallback(); | |
157 | |
158 audio_system_->GetOutputStreamParameters( | |
159 kNonDefaultDeviceId, | |
160 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | |
161 media::AudioParameters())); | |
162 WaitForCallback(); | |
163 } | |
164 | |
165 TEST_P(AudioSystemImplTest, HasInputDevices) { | 101 TEST_P(AudioSystemImplTest, HasInputDevices) { |
166 EXPECT_CALL(*this, HasInputDevicesCallback(true)); | 102 EXPECT_CALL(*this, HasInputDevicesCallback(true)); |
167 audio_system_->HasInputDevices(base::Bind( | 103 audio_system_->HasInputDevices(base::Bind( |
168 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); | 104 &AudioSystemImplTest::HasInputDevicesCallback, base::Unretained(this))); |
169 WaitForCallback(); | 105 WaitForCallback(); |
170 } | 106 } |
171 | 107 |
172 TEST_P(AudioSystemImplTest, HasNoInputDevices) { | 108 TEST_P(AudioSystemImplTest, HasNoInputDevices) { |
173 audio_manager_->SetHasInputDevices(false); | 109 audio_manager_->SetHasInputDevices(false); |
174 EXPECT_CALL(*this, HasInputDevicesCallback(false)); | 110 EXPECT_CALL(*this, HasInputDevicesCallback(false)); |
175 audio_system_->HasInputDevices(base::Bind( | 111 audio_system_->HasInputDevices(base::Bind( |
176 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); | 112 &AudioSystemImplTest::HasInputDevicesCallback, base::Unretained(this))); |
177 WaitForCallback(); | 113 WaitForCallback(); |
178 } | 114 } |
179 | 115 |
180 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); | 116 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); |
181 | 117 |
182 } // namespace media | 118 } // namespace media |
OLD | NEW |