| 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 { | 19 namespace { |
| 20 const char* kNonDefaultDeviceId = "non-default-device-id"; | 20 const char* kNonDefaultDeviceId = "non-default-device-id"; |
| 21 } | 21 } |
| 22 |
| 22 namespace media { | 23 namespace media { |
| 23 | 24 |
| 25 bool operator==(const media::AudioDeviceDescription& lhs, |
| 26 const media::AudioDeviceDescription& rhs) { |
| 27 return lhs.device_name == rhs.device_name && lhs.unique_id == rhs.unique_id && |
| 28 lhs.group_id == rhs.group_id; |
| 29 } |
| 30 |
| 24 class AudioSystemImplTest : public testing::TestWithParam<bool> { | 31 class AudioSystemImplTest : public testing::TestWithParam<bool> { |
| 25 public: | 32 public: |
| 26 AudioSystemImplTest() | 33 AudioSystemImplTest() |
| 27 : use_audio_thread_(GetParam()), | 34 : use_audio_thread_(GetParam()), |
| 28 audio_thread_("AudioSystemThread"), | 35 audio_thread_("AudioSystemThread"), |
| 29 input_params_(AudioParameters::AUDIO_PCM_LINEAR, | 36 input_params_(AudioParameters::AUDIO_PCM_LINEAR, |
| 30 CHANNEL_LAYOUT_MONO, | 37 CHANNEL_LAYOUT_MONO, |
| 31 AudioParameters::kTelephoneSampleRate, | 38 AudioParameters::kTelephoneSampleRate, |
| 32 16, | 39 16, |
| 33 AudioParameters::kTelephoneSampleRate / 10), | 40 AudioParameters::kTelephoneSampleRate / 10), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 47 new media::MockAudioManager(audio_thread_.task_runner())); | 54 new media::MockAudioManager(audio_thread_.task_runner())); |
| 48 } else { | 55 } else { |
| 49 audio_manager_.reset(new media::MockAudioManager( | 56 audio_manager_.reset(new media::MockAudioManager( |
| 50 base::ThreadTaskRunnerHandle::Get().get())); | 57 base::ThreadTaskRunnerHandle::Get().get())); |
| 51 } | 58 } |
| 52 | 59 |
| 53 audio_manager_->SetInputStreamParameters(input_params_); | 60 audio_manager_->SetInputStreamParameters(input_params_); |
| 54 audio_manager_->SetOutputStreamParameters(output_params_); | 61 audio_manager_->SetOutputStreamParameters(output_params_); |
| 55 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); | 62 audio_manager_->SetDefaultOutputStreamParameters(default_output_params_); |
| 56 | 63 |
| 64 auto get_device_descriptions = [](const AudioDeviceDescriptions* source, |
| 65 AudioDeviceDescriptions* destination) { |
| 66 destination->insert(destination->end(), source->begin(), source->end()); |
| 67 }; |
| 68 |
| 69 audio_manager_->SetInputDeviceDescriptionsCallback( |
| 70 base::Bind(get_device_descriptions, |
| 71 base::Unretained(&input_device_descriptions_))); |
| 72 audio_manager_->SetOutputDeviceDescriptionsCallback( |
| 73 base::Bind(get_device_descriptions, |
| 74 base::Unretained(&output_device_descriptions_))); |
| 75 |
| 57 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 76 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
| 58 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); | 77 EXPECT_EQ(AudioSystem::Get(), audio_system_.get()); |
| 59 } | 78 } |
| 60 | 79 |
| 61 ~AudioSystemImplTest() override { | 80 ~AudioSystemImplTest() override { |
| 62 // Deleting |audio_manager_| on its thread. | 81 // Deleting |audio_manager_| on its thread. |
| 63 audio_system_.reset(); | 82 audio_system_.reset(); |
| 64 EXPECT_EQ(AudioSystem::Get(), nullptr); | 83 EXPECT_EQ(AudioSystem::Get(), nullptr); |
| 65 audio_manager_.reset(); | 84 audio_manager_.reset(); |
| 66 audio_thread_.Stop(); | 85 audio_thread_.Stop(); |
| 67 } | 86 } |
| 68 | 87 |
| 69 void OnAudioParams(const AudioParameters& expected, | 88 void OnAudioParams(const AudioParameters& expected, |
| 70 const AudioParameters& received) { | 89 const AudioParameters& received) { |
| 71 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 90 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 72 EXPECT_EQ(expected.AsHumanReadableString(), | 91 EXPECT_EQ(expected.AsHumanReadableString(), |
| 73 received.AsHumanReadableString()); | 92 received.AsHumanReadableString()); |
| 74 AudioParametersReceived(); | 93 AudioParametersReceived(); |
| 75 } | 94 } |
| 76 | 95 |
| 77 void OnHasInputDevices(bool result) { | 96 void OnHasInputDevices(bool result) { |
| 78 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); | 97 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 79 HasInputDevicesCallback(result); | 98 HasInputDevicesCallback(result); |
| 80 } | 99 } |
| 81 | 100 |
| 101 void OnGetDeviceDescriptions( |
| 102 const AudioDeviceDescriptions& expected_descriptions, |
| 103 AudioDeviceDescriptions descriptions) { |
| 104 EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 105 EXPECT_EQ(expected_descriptions, descriptions); |
| 106 DeviceDescriptionsReceived(); |
| 107 } |
| 108 |
| 82 void WaitForCallback() { | 109 void WaitForCallback() { |
| 83 if (!use_audio_thread_) { | 110 if (!use_audio_thread_) { |
| 84 base::RunLoop().RunUntilIdle(); | 111 base::RunLoop().RunUntilIdle(); |
| 85 return; | 112 return; |
| 86 } | 113 } |
| 87 WaitableMessageLoopEvent event; | 114 WaitableMessageLoopEvent event; |
| 88 audio_thread_.task_runner()->PostTaskAndReply( | 115 audio_thread_.task_runner()->PostTaskAndReply( |
| 89 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); | 116 FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
| 90 // Runs the loop and waits for the |audio_thread_| to call event's closure, | 117 // Runs the loop and waits for the |audio_thread_| to call event's closure, |
| 91 // which means AudioSystem reply containing device parameters is already | 118 // which means AudioSystem reply containing device parameters is already |
| 92 // queued on the main thread. | 119 // queued on the main thread. |
| 93 event.RunAndWait(); | 120 event.RunAndWait(); |
| 94 base::RunLoop().RunUntilIdle(); | 121 base::RunLoop().RunUntilIdle(); |
| 95 } | 122 } |
| 96 | 123 |
| 124 // Mocks to verify that AudioSystem replied with an expected callback. |
| 97 MOCK_METHOD0(AudioParametersReceived, void(void)); | 125 MOCK_METHOD0(AudioParametersReceived, void(void)); |
| 98 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); | 126 MOCK_METHOD1(HasInputDevicesCallback, void(bool)); |
| 127 MOCK_METHOD0(DeviceDescriptionsReceived, void(void)); |
| 99 | 128 |
| 100 protected: | 129 protected: |
| 101 base::MessageLoop message_loop_; | 130 base::MessageLoop message_loop_; |
| 102 base::ThreadChecker thread_checker_; | 131 base::ThreadChecker thread_checker_; |
| 103 bool use_audio_thread_; | 132 bool use_audio_thread_; |
| 104 base::Thread audio_thread_; | 133 base::Thread audio_thread_; |
| 105 MockAudioManager::UniquePtr audio_manager_; | 134 MockAudioManager::UniquePtr audio_manager_; |
| 106 std::unique_ptr<media::AudioSystem> audio_system_; | 135 std::unique_ptr<media::AudioSystem> audio_system_; |
| 107 AudioParameters input_params_; | 136 AudioParameters input_params_; |
| 108 AudioParameters output_params_; | 137 AudioParameters output_params_; |
| 109 AudioParameters default_output_params_; | 138 AudioParameters default_output_params_; |
| 139 AudioDeviceDescriptions input_device_descriptions_; |
| 140 AudioDeviceDescriptions output_device_descriptions_; |
| 110 }; | 141 }; |
| 111 | 142 |
| 112 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { | 143 TEST_P(AudioSystemImplTest, GetInputStreamParameters) { |
| 113 EXPECT_CALL(*this, AudioParametersReceived()); | 144 EXPECT_CALL(*this, AudioParametersReceived()); |
| 114 audio_system_->GetInputStreamParameters( | 145 audio_system_->GetInputStreamParameters( |
| 115 media::AudioDeviceDescription::kDefaultDeviceId, | 146 media::AudioDeviceDescription::kDefaultDeviceId, |
| 116 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), | 147 base::Bind(&AudioSystemImplTest::OnAudioParams, base::Unretained(this), |
| 117 input_params_)); | 148 input_params_)); |
| 118 WaitForCallback(); | 149 WaitForCallback(); |
| 119 } | 150 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 201 } |
| 171 | 202 |
| 172 TEST_P(AudioSystemImplTest, HasNoInputDevices) { | 203 TEST_P(AudioSystemImplTest, HasNoInputDevices) { |
| 173 audio_manager_->SetHasInputDevices(false); | 204 audio_manager_->SetHasInputDevices(false); |
| 174 EXPECT_CALL(*this, HasInputDevicesCallback(false)); | 205 EXPECT_CALL(*this, HasInputDevicesCallback(false)); |
| 175 audio_system_->HasInputDevices(base::Bind( | 206 audio_system_->HasInputDevices(base::Bind( |
| 176 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); | 207 &AudioSystemImplTest::OnHasInputDevices, base::Unretained(this))); |
| 177 WaitForCallback(); | 208 WaitForCallback(); |
| 178 } | 209 } |
| 179 | 210 |
| 211 TEST_P(AudioSystemImplTest, GetInputDeviceDescriptionsNoInputDevices) { |
| 212 output_device_descriptions_.emplace_back("output_device_name", |
| 213 "output_device_id", "group_id"); |
| 214 EXPECT_EQ(0, static_cast<int>(input_device_descriptions_.size())); |
| 215 EXPECT_EQ(1, static_cast<int>(output_device_descriptions_.size())); |
| 216 EXPECT_CALL(*this, DeviceDescriptionsReceived()); |
| 217 audio_system_->GetDeviceDescriptions( |
| 218 base::Bind(&AudioSystemImplTest::OnGetDeviceDescriptions, |
| 219 base::Unretained(this), input_device_descriptions_), |
| 220 true); |
| 221 WaitForCallback(); |
| 222 } |
| 223 |
| 224 TEST_P(AudioSystemImplTest, GetInputDeviceDescriptions) { |
| 225 output_device_descriptions_.emplace_back("output_device_name", |
| 226 "output_device_id", "group_id"); |
| 227 input_device_descriptions_.emplace_back("input_device_name1", |
| 228 "input_device_id1", "group_id1"); |
| 229 input_device_descriptions_.emplace_back("input_device_name2", |
| 230 "input_device_id2", "group_id2"); |
| 231 EXPECT_EQ(2, static_cast<int>(input_device_descriptions_.size())); |
| 232 EXPECT_EQ(1, static_cast<int>(output_device_descriptions_.size())); |
| 233 EXPECT_CALL(*this, DeviceDescriptionsReceived()); |
| 234 audio_system_->GetDeviceDescriptions( |
| 235 base::Bind(&AudioSystemImplTest::OnGetDeviceDescriptions, |
| 236 base::Unretained(this), input_device_descriptions_), |
| 237 true); |
| 238 WaitForCallback(); |
| 239 } |
| 240 |
| 241 TEST_P(AudioSystemImplTest, GetOutputDeviceDescriptionsNoInputDevices) { |
| 242 input_device_descriptions_.emplace_back("input_device_name", |
| 243 "input_device_id", "group_id"); |
| 244 EXPECT_EQ(0, static_cast<int>(output_device_descriptions_.size())); |
| 245 EXPECT_EQ(1, static_cast<int>(input_device_descriptions_.size())); |
| 246 EXPECT_CALL(*this, DeviceDescriptionsReceived()); |
| 247 audio_system_->GetDeviceDescriptions( |
| 248 base::Bind(&AudioSystemImplTest::OnGetDeviceDescriptions, |
| 249 base::Unretained(this), output_device_descriptions_), |
| 250 false); |
| 251 WaitForCallback(); |
| 252 } |
| 253 |
| 254 TEST_P(AudioSystemImplTest, GetOutputDeviceDescriptions) { |
| 255 input_device_descriptions_.emplace_back("input_device_name", |
| 256 "input_device_id", "group_id"); |
| 257 output_device_descriptions_.emplace_back("output_device_name1", |
| 258 "output_device_id1", "group_id1"); |
| 259 output_device_descriptions_.emplace_back("output_device_name2", |
| 260 "output_device_id2", "group_id2"); |
| 261 EXPECT_EQ(2, static_cast<int>(output_device_descriptions_.size())); |
| 262 EXPECT_EQ(1, static_cast<int>(input_device_descriptions_.size())); |
| 263 EXPECT_CALL(*this, DeviceDescriptionsReceived()); |
| 264 audio_system_->GetDeviceDescriptions( |
| 265 base::Bind(&AudioSystemImplTest::OnGetDeviceDescriptions, |
| 266 base::Unretained(this), output_device_descriptions_), |
| 267 false); |
| 268 WaitForCallback(); |
| 269 } |
| 270 |
| 180 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); | 271 INSTANTIATE_TEST_CASE_P(, AudioSystemImplTest, testing::Values(false, true)); |
| 181 | 272 |
| 182 } // namespace media | 273 } // namespace media |
| OLD | NEW |