Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <set> |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 19 | #include <sys/wait.h> |
| 20 | #include <unistd.h> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 21 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 24 | #define LOG_TAG "APM_Test" |
| 25 | #include <log/log.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 26 | #include <media/PatchBuilder.h> |
| 27 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 28 | #include "AudioPolicyTestClient.h" |
| 29 | #include "AudioPolicyTestManager.h" |
| 30 | |
| 31 | using namespace android; |
| 32 | |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 33 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 34 | AudioPolicyTestClient client; |
| 35 | AudioPolicyTestManager manager(&client); |
| 36 | manager.getConfig().setDefault(); |
| 37 | manager.getConfig().setEngineLibraryNameSuffix("non-existent"); |
| 38 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 39 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 40 | } |
| 41 | |
| 42 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 43 | AudioPolicyTestClient client; |
| 44 | AudioPolicyTestManager manager(&client); |
| 45 | manager.getConfig().setDefault(); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 46 | // Since the default client fails to open anything, |
| 47 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 48 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 49 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 50 | } |
| 51 | |
| 52 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 53 | class AudioPolicyManagerTestClient : public AudioPolicyTestClient { |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 54 | public: |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 55 | // AudioPolicyClientInterface implementation |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 56 | audio_module_handle_t loadHwModule(const char* /*name*/) override { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 57 | return mNextModuleHandle++; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 58 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 59 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 60 | status_t openOutput(audio_module_handle_t module, |
| 61 | audio_io_handle_t* output, |
| 62 | audio_config_t* /*config*/, |
| 63 | audio_devices_t* /*devices*/, |
| 64 | const String8& /*address*/, |
| 65 | uint32_t* /*latencyMs*/, |
| 66 | audio_output_flags_t /*flags*/) override { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 67 | if (module >= mNextModuleHandle) { |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 68 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 69 | __func__, module, mNextModuleHandle); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 70 | return BAD_VALUE; |
| 71 | } |
| 72 | *output = mNextIoHandle++; |
| 73 | return NO_ERROR; |
| 74 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 75 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 76 | status_t openInput(audio_module_handle_t module, |
| 77 | audio_io_handle_t* input, |
| 78 | audio_config_t* /*config*/, |
| 79 | audio_devices_t* /*device*/, |
| 80 | const String8& /*address*/, |
| 81 | audio_source_t /*source*/, |
| 82 | audio_input_flags_t /*flags*/) override { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 83 | if (module >= mNextModuleHandle) { |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 84 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 85 | __func__, module, mNextModuleHandle); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 86 | return BAD_VALUE; |
| 87 | } |
| 88 | *input = mNextIoHandle++; |
| 89 | return NO_ERROR; |
| 90 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 91 | |
| 92 | status_t createAudioPatch(const struct audio_patch* /*patch*/, |
| 93 | audio_patch_handle_t* handle, |
| 94 | int /*delayMs*/) override { |
| 95 | *handle = mNextPatchHandle++; |
| 96 | mActivePatches.insert(*handle); |
| 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
| 100 | status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 101 | int /*delayMs*/) override { |
| 102 | if (mActivePatches.erase(handle) != 1) { |
| 103 | if (handle >= mNextPatchHandle) { |
| 104 | ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", |
| 105 | __func__, handle, mNextPatchHandle); |
| 106 | } else { |
| 107 | ALOGE("%s: Attempt to release patch %d twice", __func__, handle); |
| 108 | } |
| 109 | return BAD_VALUE; |
| 110 | } |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
| 114 | // Helper methods for tests |
| 115 | size_t getActivePatchesCount() const { return mActivePatches.size(); } |
| 116 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 117 | private: |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 118 | audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 119 | audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 120 | audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; |
| 121 | std::set<audio_patch_handle_t> mActivePatches; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 124 | class PatchCountCheck { |
| 125 | public: |
| 126 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 127 | : mClient{client}, |
| 128 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 129 | int deltaFromSnapshot() const { |
| 130 | size_t currentCount = mClient->getActivePatchesCount(); |
| 131 | if (mInitialCount <= currentCount) { |
| 132 | return currentCount - mInitialCount; |
| 133 | } else { |
| 134 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 135 | } |
| 136 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 137 | private: |
| 138 | const AudioPolicyManagerTestClient *mClient; |
| 139 | const size_t mInitialCount; |
| 140 | }; |
| 141 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 142 | class AudioPolicyManagerTest : public testing::Test { |
| 143 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 144 | void SetUp() override; |
| 145 | void TearDown() override; |
| 146 | virtual void SetUpConfig(AudioPolicyConfig *config) { (void)config; } |
| 147 | |
| 148 | void dumpToLog(); |
| 149 | void getOutputForAttr( |
| 150 | audio_port_handle_t *selectedDeviceId, |
| 151 | audio_format_t format, |
| 152 | int channelMask, |
| 153 | int sampleRate, |
| 154 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
| 155 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 156 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 157 | |
| 158 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 159 | std::unique_ptr<AudioPolicyTestManager> mManager; |
| 160 | }; |
| 161 | |
| 162 | void AudioPolicyManagerTest::SetUp() { |
| 163 | mClient.reset(new AudioPolicyManagerTestClient); |
| 164 | mManager.reset(new AudioPolicyTestManager(mClient.get())); |
| 165 | mManager->getConfig().setDefault(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 166 | SetUpConfig(&mManager->getConfig()); // Subclasses may want to customize the config. |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 167 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 168 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 169 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 170 | |
| 171 | void AudioPolicyManagerTest::TearDown() { |
| 172 | mManager.reset(); |
| 173 | mClient.reset(); |
| 174 | } |
| 175 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 176 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 177 | int pipefd[2]; |
| 178 | ASSERT_NE(-1, pipe(pipefd)); |
| 179 | pid_t cpid = fork(); |
| 180 | ASSERT_NE(-1, cpid); |
| 181 | if (cpid == 0) { |
| 182 | // Child process reads from the pipe and logs. |
| 183 | close(pipefd[1]); |
| 184 | std::string line; |
| 185 | char buf; |
| 186 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 187 | if (buf != '\n') { |
| 188 | line += buf; |
| 189 | } else { |
| 190 | ALOGI("%s", line.c_str()); |
| 191 | line = ""; |
| 192 | } |
| 193 | } |
| 194 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 195 | close(pipefd[0]); |
| 196 | _exit(EXIT_SUCCESS); |
| 197 | } else { |
| 198 | // Parent does the dump and checks the status code. |
| 199 | close(pipefd[0]); |
| 200 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 201 | close(pipefd[1]); |
| 202 | wait(NULL); // Wait for the child to exit. |
| 203 | } |
| 204 | } |
| 205 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 206 | void AudioPolicyManagerTest::getOutputForAttr( |
| 207 | audio_port_handle_t *selectedDeviceId, |
| 208 | audio_format_t format, |
| 209 | int channelMask, |
| 210 | int sampleRate, |
| 211 | audio_output_flags_t flags, |
| 212 | audio_port_handle_t *portId) { |
| 213 | audio_attributes_t attr = {}; |
| 214 | audio_io_handle_t output = AUDIO_PORT_HANDLE_NONE; |
| 215 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 216 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 217 | config.sample_rate = sampleRate; |
| 218 | config.channel_mask = channelMask; |
| 219 | config.format = format; |
| 220 | *selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 221 | audio_port_handle_t localPortId; |
| 222 | if (!portId) portId = &localPortId; |
| 223 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 224 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
| 225 | &attr, &output, AUDIO_SESSION_NONE, &stream, 0 /*uid*/, &config, &flags, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 226 | selectedDeviceId, portId, {})); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 227 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 232 | // SetUp must finish with no assertions. |
| 233 | } |
| 234 | |
| 235 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 236 | dumpToLog(); |
| 237 | } |
| 238 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 239 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 240 | audio_patch patch{}; |
| 241 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 242 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 243 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 244 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 245 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 246 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 247 | patch.num_sinks = 1; |
| 248 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 249 | patch.num_sources = 1; |
| 250 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 251 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 252 | patch.num_sources = 2; |
| 253 | patch.num_sinks = 1; |
| 254 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 255 | patch = {}; |
| 256 | patch.num_sources = 1; |
| 257 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 258 | patch.num_sinks = 1; |
| 259 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 260 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 261 | patch = {}; |
| 262 | patch.num_sources = 1; |
| 263 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 264 | patch.num_sinks = 1; |
| 265 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 266 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 267 | // Verify that the handle is left unchanged. |
| 268 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 269 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 273 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 274 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 275 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 276 | ASSERT_FALSE(mManager->getConfig().getAvailableInputDevices().isEmpty()); |
| 277 | PatchBuilder patchBuilder; |
| 278 | patchBuilder.addSource(mManager->getConfig().getAvailableInputDevices()[0]). |
| 279 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 280 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 281 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 282 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // TODO: Add patch creation tests that involve already existing patch |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 286 | |
| 287 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest { |
| 288 | protected: |
| 289 | void SetUpConfig(AudioPolicyConfig *config) override; |
| 290 | void TearDown() override; |
| 291 | |
| 292 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 293 | sp<DeviceDescriptor> mMsdInputDevice; |
| 294 | }; |
| 295 | |
| 296 | void AudioPolicyManagerTestMsd::SetUpConfig(AudioPolicyConfig *config) { |
| 297 | // TODO: Consider using Serializer to load part of the config from a string. |
| 298 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 299 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 300 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 301 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 302 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 303 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 304 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
| 305 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 306 | // Match output profile from AudioPolicyConfig::setDefault. |
| 307 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 308 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 309 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
| 310 | config->addAvailableDevice(mMsdOutputDevice); |
| 311 | config->addAvailableDevice(mMsdInputDevice); |
| 312 | |
| 313 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
| 314 | HwModuleCollection modules = config->getHwModules(); |
| 315 | modules.add(msdModule); |
| 316 | config->setHwModules(modules); |
| 317 | mMsdOutputDevice->attach(msdModule); |
| 318 | mMsdInputDevice->attach(msdModule); |
| 319 | |
| 320 | sp<OutputProfile> msdOutputProfile = new OutputProfile(String8("msd input")); |
| 321 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 322 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 323 | msdModule->addOutputProfile(msdOutputProfile); |
| 324 | sp<OutputProfile> msdCompressedOutputProfile = |
| 325 | new OutputProfile(String8("msd compressed input")); |
| 326 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 327 | msdCompressedOutputProfile->setFlags( |
| 328 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 329 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 330 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 331 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
| 332 | |
| 333 | sp<InputProfile> msdInputProfile = new InputProfile(String8("msd output")); |
| 334 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 335 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 336 | msdModule->addInputProfile(msdInputProfile); |
| 337 | |
| 338 | // Add a profile with another encoding to the default device to test routing |
| 339 | // of streams that are not supported by MSD. |
| 340 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 341 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 342 | config->getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
| 343 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile(String8("encoded")); |
| 344 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 345 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
| 346 | primaryEncodedOutputProfile->addSupportedDevice(config->getDefaultOutputDevice()); |
| 347 | config->getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
| 348 | addOutputProfile(primaryEncodedOutputProfile); |
| 349 | } |
| 350 | |
| 351 | void AudioPolicyManagerTestMsd::TearDown() { |
| 352 | mMsdOutputDevice.clear(); |
| 353 | mMsdInputDevice.clear(); |
| 354 | AudioPolicyManagerTest::TearDown(); |
| 355 | } |
| 356 | |
| 357 | TEST_F(AudioPolicyManagerTestMsd, InitSuccess) { |
| 358 | ASSERT_TRUE(mMsdOutputDevice); |
| 359 | ASSERT_TRUE(mMsdInputDevice); |
| 360 | } |
| 361 | |
| 362 | TEST_F(AudioPolicyManagerTestMsd, Dump) { |
| 363 | dumpToLog(); |
| 364 | } |
| 365 | |
| 366 | TEST_F(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 367 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 368 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 369 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 370 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 374 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 375 | audio_port_handle_t selectedDeviceId; |
| 376 | getOutputForAttr(&selectedDeviceId, |
| 377 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 378 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 379 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 383 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 384 | audio_port_handle_t selectedDeviceId; |
| 385 | getOutputForAttr(&selectedDeviceId, |
| 386 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 387 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 388 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 392 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 393 | audio_port_handle_t selectedDeviceId; |
| 394 | getOutputForAttr(&selectedDeviceId, |
| 395 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 396 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 397 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 398 | getOutputForAttr(&selectedDeviceId, |
| 399 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 400 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 401 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 405 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 406 | audio_port_handle_t selectedDeviceId; |
| 407 | getOutputForAttr(&selectedDeviceId, |
| 408 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 409 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 410 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
| 414 | // Switch between formats that are supported and not supported by MSD. |
| 415 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 416 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 417 | audio_port_handle_t selectedDeviceId, portId; |
| 418 | getOutputForAttr(&selectedDeviceId, |
| 419 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 420 | &portId); |
| 421 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 422 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 423 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 424 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 425 | } |
| 426 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 427 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 428 | audio_port_handle_t selectedDeviceId, portId; |
| 429 | getOutputForAttr(&selectedDeviceId, |
| 430 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 431 | &portId); |
| 432 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 433 | ASSERT_EQ(-1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 434 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 435 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 436 | } |
| 437 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 438 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 439 | audio_port_handle_t selectedDeviceId; |
| 440 | getOutputForAttr(&selectedDeviceId, |
| 441 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 442 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 443 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 444 | } |
| 445 | } |