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