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 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 17 | #include <map> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 18 | #include <memory> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 19 | #include <string> |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 20 | #include <sys/wait.h> |
| 21 | #include <unistd.h> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 22 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
| 24 | |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 25 | #define LOG_TAG "APM_Test" |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 26 | #include <Serializer.h> |
| 27 | #include <android-base/file.h> |
| 28 | #include <media/AudioPolicy.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 29 | #include <media/PatchBuilder.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 30 | #include <media/RecordingActivityTracker.h> |
| 31 | #include <utils/Log.h> |
| 32 | #include <utils/Vector.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 33 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 34 | #include "AudioPolicyInterface.h" |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 35 | #include "AudioPolicyTestClient.h" |
| 36 | #include "AudioPolicyTestManager.h" |
| 37 | |
| 38 | using namespace android; |
| 39 | |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 40 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 41 | AudioPolicyTestClient client; |
| 42 | AudioPolicyTestManager manager(&client); |
| 43 | manager.getConfig().setDefault(); |
| 44 | manager.getConfig().setEngineLibraryNameSuffix("non-existent"); |
| 45 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 46 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 47 | } |
| 48 | |
| 49 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 50 | AudioPolicyTestClient client; |
| 51 | AudioPolicyTestManager manager(&client); |
| 52 | manager.getConfig().setDefault(); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 53 | // Since the default client fails to open anything, |
| 54 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 55 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 56 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 57 | } |
| 58 | |
| 59 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 60 | class AudioPolicyManagerTestClient : public AudioPolicyTestClient { |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 61 | public: |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 62 | // AudioPolicyClientInterface implementation |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 63 | audio_module_handle_t loadHwModule(const char* /*name*/) override { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 64 | return mNextModuleHandle++; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 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 openOutput(audio_module_handle_t module, |
| 68 | audio_io_handle_t* output, |
| 69 | audio_config_t* /*config*/, |
| 70 | audio_devices_t* /*devices*/, |
| 71 | const String8& /*address*/, |
| 72 | uint32_t* /*latencyMs*/, |
| 73 | audio_output_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 | *output = mNextIoHandle++; |
| 80 | return NO_ERROR; |
| 81 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 82 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 83 | status_t openInput(audio_module_handle_t module, |
| 84 | audio_io_handle_t* input, |
| 85 | audio_config_t* /*config*/, |
| 86 | audio_devices_t* /*device*/, |
| 87 | const String8& /*address*/, |
| 88 | audio_source_t /*source*/, |
| 89 | audio_input_flags_t /*flags*/) override { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 90 | if (module >= mNextModuleHandle) { |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 91 | 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] | 92 | __func__, module, mNextModuleHandle); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 93 | return BAD_VALUE; |
| 94 | } |
| 95 | *input = mNextIoHandle++; |
| 96 | return NO_ERROR; |
| 97 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 98 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 99 | status_t createAudioPatch(const struct audio_patch* patch, |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 100 | audio_patch_handle_t* handle, |
| 101 | int /*delayMs*/) override { |
| 102 | *handle = mNextPatchHandle++; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 103 | mActivePatches.insert(std::make_pair(*handle, *patch)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 104 | return NO_ERROR; |
| 105 | } |
| 106 | |
| 107 | status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 108 | int /*delayMs*/) override { |
| 109 | if (mActivePatches.erase(handle) != 1) { |
| 110 | if (handle >= mNextPatchHandle) { |
| 111 | ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", |
| 112 | __func__, handle, mNextPatchHandle); |
| 113 | } else { |
| 114 | ALOGE("%s: Attempt to release patch %d twice", __func__, handle); |
| 115 | } |
| 116 | return BAD_VALUE; |
| 117 | } |
| 118 | return NO_ERROR; |
| 119 | } |
| 120 | |
| 121 | // Helper methods for tests |
| 122 | size_t getActivePatchesCount() const { return mActivePatches.size(); } |
| 123 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 124 | const struct audio_patch* getLastAddedPatch() const { |
| 125 | if (mActivePatches.empty()) { |
| 126 | return nullptr; |
| 127 | } |
| 128 | auto it = --mActivePatches.end(); |
| 129 | return &it->second; |
| 130 | }; |
| 131 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 132 | private: |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 133 | audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 134 | audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 135 | audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 136 | std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 139 | class PatchCountCheck { |
| 140 | public: |
| 141 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 142 | : mClient{client}, |
| 143 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 144 | int deltaFromSnapshot() const { |
| 145 | size_t currentCount = mClient->getActivePatchesCount(); |
| 146 | if (mInitialCount <= currentCount) { |
| 147 | return currentCount - mInitialCount; |
| 148 | } else { |
| 149 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 150 | } |
| 151 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 152 | private: |
| 153 | const AudioPolicyManagerTestClient *mClient; |
| 154 | const size_t mInitialCount; |
| 155 | }; |
| 156 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 157 | class AudioPolicyManagerTest : public testing::Test { |
| 158 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 159 | void SetUp() override; |
| 160 | void TearDown() override; |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 161 | virtual void SetUpManagerConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 162 | |
| 163 | void dumpToLog(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 164 | // When explicitly routing is needed, selectedDeviceId need to be set as the wanted port |
| 165 | // id. Otherwise, selectedDeviceId need to be initialized as AUDIO_PORT_HANDLE_NONE. |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 166 | void getOutputForAttr( |
| 167 | audio_port_handle_t *selectedDeviceId, |
| 168 | audio_format_t format, |
| 169 | int channelMask, |
| 170 | int sampleRate, |
| 171 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 172 | audio_port_handle_t *portId = nullptr, |
| 173 | audio_attributes_t attr = {}); |
| 174 | void getInputForAttr( |
| 175 | const audio_attributes_t &attr, |
| 176 | audio_unique_id_t riid, |
| 177 | audio_port_handle_t *selectedDeviceId, |
| 178 | audio_format_t format, |
| 179 | int channelMask, |
| 180 | int sampleRate, |
| 181 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 182 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 183 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 184 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 185 | void findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
| 186 | const std::string &address, audio_port &foundPort); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 187 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch* patch); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 188 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 189 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 190 | std::unique_ptr<AudioPolicyTestManager> mManager; |
| 191 | }; |
| 192 | |
| 193 | void AudioPolicyManagerTest::SetUp() { |
| 194 | mClient.reset(new AudioPolicyManagerTestClient); |
| 195 | mManager.reset(new AudioPolicyTestManager(mClient.get())); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 196 | SetUpManagerConfig(); // Subclasses may want to customize the config. |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 197 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 198 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 199 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 200 | |
| 201 | void AudioPolicyManagerTest::TearDown() { |
| 202 | mManager.reset(); |
| 203 | mClient.reset(); |
| 204 | } |
| 205 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 206 | void AudioPolicyManagerTest::SetUpManagerConfig() { |
| 207 | mManager->getConfig().setDefault(); |
| 208 | } |
| 209 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 210 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 211 | int pipefd[2]; |
| 212 | ASSERT_NE(-1, pipe(pipefd)); |
| 213 | pid_t cpid = fork(); |
| 214 | ASSERT_NE(-1, cpid); |
| 215 | if (cpid == 0) { |
| 216 | // Child process reads from the pipe and logs. |
| 217 | close(pipefd[1]); |
| 218 | std::string line; |
| 219 | char buf; |
| 220 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 221 | if (buf != '\n') { |
| 222 | line += buf; |
| 223 | } else { |
| 224 | ALOGI("%s", line.c_str()); |
| 225 | line = ""; |
| 226 | } |
| 227 | } |
| 228 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 229 | close(pipefd[0]); |
| 230 | _exit(EXIT_SUCCESS); |
| 231 | } else { |
| 232 | // Parent does the dump and checks the status code. |
| 233 | close(pipefd[0]); |
| 234 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 235 | close(pipefd[1]); |
| 236 | wait(NULL); // Wait for the child to exit. |
| 237 | } |
| 238 | } |
| 239 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 240 | void AudioPolicyManagerTest::getOutputForAttr( |
| 241 | audio_port_handle_t *selectedDeviceId, |
| 242 | audio_format_t format, |
| 243 | int channelMask, |
| 244 | int sampleRate, |
| 245 | audio_output_flags_t flags, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 246 | audio_port_handle_t *portId, |
| 247 | audio_attributes_t attr) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 248 | audio_io_handle_t output = AUDIO_PORT_HANDLE_NONE; |
| 249 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 250 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 251 | config.sample_rate = sampleRate; |
| 252 | config.channel_mask = channelMask; |
| 253 | config.format = format; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 254 | audio_port_handle_t localPortId; |
| 255 | if (!portId) portId = &localPortId; |
| 256 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 257 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
| 258 | &attr, &output, AUDIO_SESSION_NONE, &stream, 0 /*uid*/, &config, &flags, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 259 | selectedDeviceId, portId, {})); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 260 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 261 | } |
| 262 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 263 | void AudioPolicyManagerTest::getInputForAttr( |
| 264 | const audio_attributes_t &attr, |
| 265 | audio_unique_id_t riid, |
| 266 | audio_port_handle_t *selectedDeviceId, |
| 267 | audio_format_t format, |
| 268 | int channelMask, |
| 269 | int sampleRate, |
| 270 | audio_input_flags_t flags, |
| 271 | audio_port_handle_t *portId) { |
| 272 | audio_io_handle_t input = AUDIO_PORT_HANDLE_NONE; |
| 273 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 274 | config.sample_rate = sampleRate; |
| 275 | config.channel_mask = channelMask; |
| 276 | config.format = format; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 277 | audio_port_handle_t localPortId; |
| 278 | if (!portId) portId = &localPortId; |
| 279 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 280 | AudioPolicyInterface::input_type_t inputType; |
| 281 | ASSERT_EQ(OK, mManager->getInputForAttr( |
| 282 | &attr, &input, riid, AUDIO_SESSION_NONE, 0 /*uid*/, &config, flags, |
| 283 | selectedDeviceId, &inputType, portId)); |
| 284 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 285 | } |
| 286 | |
| 287 | void AudioPolicyManagerTest::findDevicePort(audio_port_role_t role, |
| 288 | audio_devices_t deviceType, const std::string &address, audio_port &foundPort) { |
| 289 | uint32_t numPorts = 0; |
| 290 | uint32_t generation1; |
| 291 | status_t ret; |
| 292 | |
| 293 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 294 | ASSERT_EQ(NO_ERROR, ret); |
| 295 | |
| 296 | uint32_t generation2; |
| 297 | struct audio_port ports[numPorts]; |
| 298 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation2); |
| 299 | ASSERT_EQ(NO_ERROR, ret); |
| 300 | ASSERT_EQ(generation1, generation2); |
| 301 | |
| 302 | for (const auto &port : ports) { |
| 303 | if (port.role == role && port.ext.device.type == deviceType && |
| 304 | (strncmp(port.ext.device.address, address.c_str(), |
| 305 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 306 | foundPort = port; |
| 307 | return; |
| 308 | } |
| 309 | } |
| 310 | GTEST_FAIL(); |
| 311 | } |
| 312 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 313 | audio_port_handle_t AudioPolicyManagerTest::getDeviceIdFromPatch( |
| 314 | const struct audio_patch* patch) { |
| 315 | // The logic here is the same as the one in AudioIoDescriptor. |
| 316 | // Note this function is aim to get routed device id for test. |
| 317 | // In that case, device to device patch is not expected here. |
| 318 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 319 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 320 | return patch->sinks[0].id; |
| 321 | } else { |
| 322 | return patch->sources[0].id; |
| 323 | } |
| 324 | } |
| 325 | return AUDIO_PORT_HANDLE_NONE; |
| 326 | } |
| 327 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 328 | |
| 329 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 330 | // SetUp must finish with no assertions. |
| 331 | } |
| 332 | |
| 333 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 334 | dumpToLog(); |
| 335 | } |
| 336 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 337 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 338 | audio_patch patch{}; |
| 339 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 340 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 341 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 342 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 343 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 344 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 345 | patch.num_sinks = 1; |
| 346 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 347 | patch.num_sources = 1; |
| 348 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 349 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 350 | patch.num_sources = 2; |
| 351 | patch.num_sinks = 1; |
| 352 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 353 | patch = {}; |
| 354 | patch.num_sources = 1; |
| 355 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 356 | patch.num_sinks = 1; |
| 357 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 358 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 359 | patch = {}; |
| 360 | patch.num_sources = 1; |
| 361 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 362 | patch.num_sinks = 1; |
| 363 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 364 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 365 | // Verify that the handle is left unchanged. |
| 366 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 367 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 371 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 372 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 373 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 374 | ASSERT_FALSE(mManager->getConfig().getAvailableInputDevices().isEmpty()); |
| 375 | PatchBuilder patchBuilder; |
| 376 | patchBuilder.addSource(mManager->getConfig().getAvailableInputDevices()[0]). |
| 377 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 378 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 379 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 380 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // TODO: Add patch creation tests that involve already existing patch |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 384 | |
| 385 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest { |
| 386 | protected: |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 387 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 388 | void TearDown() override; |
| 389 | |
| 390 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 391 | sp<DeviceDescriptor> mMsdInputDevice; |
| 392 | }; |
| 393 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 394 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 395 | // TODO: Consider using Serializer to load part of the config from a string. |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 396 | AudioPolicyManagerTest::SetUpManagerConfig(); |
| 397 | AudioPolicyConfig& config = mManager->getConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 398 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 399 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 400 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 401 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 402 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 403 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 404 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
| 405 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 406 | // Match output profile from AudioPolicyConfig::setDefault. |
| 407 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 408 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 409 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 410 | config.addAvailableDevice(mMsdOutputDevice); |
| 411 | config.addAvailableDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 412 | |
| 413 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 414 | HwModuleCollection modules = config.getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 415 | modules.add(msdModule); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 416 | config.setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 417 | mMsdOutputDevice->attach(msdModule); |
| 418 | mMsdInputDevice->attach(msdModule); |
| 419 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 420 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 421 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 422 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 423 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 424 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 425 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 426 | msdCompressedOutputProfile->setFlags( |
| 427 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 428 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 429 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 430 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
| 431 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 432 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 433 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 434 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 435 | msdModule->addInputProfile(msdInputProfile); |
| 436 | |
| 437 | // Add a profile with another encoding to the default device to test routing |
| 438 | // of streams that are not supported by MSD. |
| 439 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 440 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 441 | config.getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 442 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 443 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 444 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 445 | primaryEncodedOutputProfile->addSupportedDevice(config.getDefaultOutputDevice()); |
| 446 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 447 | addOutputProfile(primaryEncodedOutputProfile); |
| 448 | } |
| 449 | |
| 450 | void AudioPolicyManagerTestMsd::TearDown() { |
| 451 | mMsdOutputDevice.clear(); |
| 452 | mMsdInputDevice.clear(); |
| 453 | AudioPolicyManagerTest::TearDown(); |
| 454 | } |
| 455 | |
| 456 | TEST_F(AudioPolicyManagerTestMsd, InitSuccess) { |
| 457 | ASSERT_TRUE(mMsdOutputDevice); |
| 458 | ASSERT_TRUE(mMsdInputDevice); |
| 459 | } |
| 460 | |
| 461 | TEST_F(AudioPolicyManagerTestMsd, Dump) { |
| 462 | dumpToLog(); |
| 463 | } |
| 464 | |
| 465 | TEST_F(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 466 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 467 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 468 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 469 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 473 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 474 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 475 | getOutputForAttr(&selectedDeviceId, |
| 476 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 477 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 478 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 482 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 483 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 484 | getOutputForAttr(&selectedDeviceId, |
| 485 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 486 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 487 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 491 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 492 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 493 | getOutputForAttr(&selectedDeviceId, |
| 494 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 495 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 496 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 497 | getOutputForAttr(&selectedDeviceId, |
| 498 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 499 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 500 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 504 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 505 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 506 | getOutputForAttr(&selectedDeviceId, |
| 507 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 508 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 509 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
| 513 | // Switch between formats that are supported and not supported by MSD. |
| 514 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 515 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 516 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 517 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 518 | getOutputForAttr(&selectedDeviceId, |
| 519 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 520 | &portId); |
| 521 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 522 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 523 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 524 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 525 | } |
| 526 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 527 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 528 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 529 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 530 | getOutputForAttr(&selectedDeviceId, |
| 531 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 532 | &portId); |
| 533 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 534 | ASSERT_EQ(-1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 535 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 536 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 537 | } |
| 538 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 539 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 540 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 541 | getOutputForAttr(&selectedDeviceId, |
| 542 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 543 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 544 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 545 | } |
| 546 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 547 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 548 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 549 | protected: |
| 550 | void SetUpManagerConfig() override; |
| 551 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 552 | |
| 553 | static const std::string sExecutableDir; |
| 554 | static const std::string sDefaultConfig; |
| 555 | }; |
| 556 | |
| 557 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 558 | base::GetExecutableDirectory() + "/"; |
| 559 | |
| 560 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 561 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 562 | |
| 563 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
| 564 | status_t status = deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 565 | ASSERT_EQ(NO_ERROR, status); |
| 566 | } |
| 567 | |
| 568 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 569 | // SetUp must finish with no assertions. |
| 570 | } |
| 571 | |
| 572 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 573 | dumpToLog(); |
| 574 | } |
| 575 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 576 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 577 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 578 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 579 | protected: |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 580 | void TearDown() override; |
| 581 | |
| 582 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 583 | std::string mixAddress, const audio_config_t& audioConfig, |
| 584 | const std::vector<PolicyMixTuple>& rules); |
| 585 | void clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 586 | |
| 587 | Vector<AudioMix> mAudioMixes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 588 | const std::string mMixAddress = "remote_submix_media"; |
| 589 | }; |
| 590 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 591 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 592 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 593 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 597 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
| 598 | const std::vector<PolicyMixTuple>& rules) { |
| 599 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 600 | |
| 601 | for(const auto &rule: rules) { |
| 602 | myMixMatchCriteria.add(AudioMixMatchCriterion( |
| 603 | std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 604 | } |
| 605 | |
| 606 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 607 | String8(mixAddress.c_str()), 0); |
| 608 | myAudioMix.mDeviceType = deviceType; |
| 609 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 610 | mAudioMixes.clear(); |
| 611 | mAudioMixes.add(myAudioMix); |
| 612 | |
| 613 | // As the policy mixes registration may fail at some case, |
| 614 | // caller need to check the returned status. |
| 615 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 620 | if (mManager != nullptr) { |
| 621 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 622 | } |
| 623 | mAudioMixes.clear(); |
| 624 | } |
| 625 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 626 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 627 | // SetUp must finish with no assertions |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 631 | dumpToLog(); |
| 632 | } |
| 633 | |
| 634 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 635 | status_t ret; |
| 636 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 637 | |
| 638 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 639 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 640 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 641 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 642 | |
| 643 | // Fail due to the device is already connected. |
| 644 | clearPolicyMix(); |
| 645 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 646 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 647 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 648 | |
| 649 | // The first time to register policy mixes with valid parameter should succeed. |
| 650 | clearPolicyMix(); |
| 651 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 652 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 653 | audioConfig.sample_rate = 48000; |
| 654 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 655 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 656 | std::vector<PolicyMixTuple>()); |
| 657 | ASSERT_EQ(NO_ERROR, ret); |
| 658 | // Registering the same policy mixes should fail. |
| 659 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 660 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 661 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 662 | // Registration should fail due to device not found. |
| 663 | // Note that earpiece is not present in the test configuration file. |
| 664 | // This will need to be updated if earpiece is added in the test configuration file. |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 665 | clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 666 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 667 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 668 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 669 | |
| 670 | // Registration should fail due to output not found. |
| 671 | clearPolicyMix(); |
| 672 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 673 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 674 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 675 | |
| 676 | // The first time to register valid policy mixes should succeed. |
| 677 | clearPolicyMix(); |
| 678 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 679 | AUDIO_DEVICE_OUT_SPEAKER, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 680 | ASSERT_EQ(NO_ERROR, ret); |
| 681 | // Registering the same policy mixes should fail. |
| 682 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 683 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 684 | } |
| 685 | |
| 686 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 687 | status_t ret; |
| 688 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 689 | |
| 690 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 691 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 692 | audioConfig.sample_rate = 48000; |
| 693 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 694 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 695 | std::vector<PolicyMixTuple>()); |
| 696 | ASSERT_EQ(NO_ERROR, ret); |
| 697 | |
| 698 | // After successfully registering policy mixes, it should be able to unregister. |
| 699 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 700 | ASSERT_EQ(NO_ERROR, ret); |
| 701 | |
| 702 | // After unregistering policy mixes successfully, it should fail unregistering |
| 703 | // the same policy mixes as they are not registered. |
| 704 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 705 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 706 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 707 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 708 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 709 | protected: |
| 710 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 711 | |
| 712 | static const std::string sPrimaryOnlyConfig; |
| 713 | }; |
| 714 | |
| 715 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 716 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 717 | |
| 718 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 719 | // SetUp must finish with no assertions. |
| 720 | } |
| 721 | |
| 722 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 723 | dumpToLog(); |
| 724 | } |
| 725 | |
| 726 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 727 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 728 | status_t ret; |
| 729 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 730 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 731 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 732 | audioConfig.sample_rate = 48000; |
| 733 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 734 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 735 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 736 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 737 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 738 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 739 | } |
| 740 | |
| 741 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
| 742 | public testing::WithParamInterface<audio_attributes_t> { |
| 743 | protected: |
| 744 | void SetUp() override; |
| 745 | void TearDown() override; |
| 746 | |
| 747 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 748 | |
| 749 | std::vector<PolicyMixTuple> mUsageRules = { |
| 750 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 751 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE} |
| 752 | }; |
| 753 | |
| 754 | struct audio_port mInjectionPort; |
| 755 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 756 | }; |
| 757 | |
| 758 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
| 759 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 760 | |
| 761 | mTracker.reset(new RecordingActivityTracker()); |
| 762 | |
| 763 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 764 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 765 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 766 | audioConfig.sample_rate = 48000; |
| 767 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 768 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, mUsageRules); |
| 769 | ASSERT_EQ(NO_ERROR, ret); |
| 770 | |
| 771 | struct audio_port extractionPort; |
| 772 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 773 | mMixAddress, extractionPort); |
| 774 | |
| 775 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 776 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
| 777 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, 0, ""}; |
| 778 | std::string tags = "addr=" + mMixAddress; |
| 779 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 780 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, |
| 781 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &mPortId); |
| 782 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 783 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 784 | |
| 785 | findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 786 | mMixAddress, mInjectionPort); |
| 787 | } |
| 788 | |
| 789 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 790 | mManager->stopInput(mPortId); |
| 791 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 792 | } |
| 793 | |
| 794 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, InitSuccess) { |
| 795 | // SetUp must finish with no assertions |
| 796 | } |
| 797 | |
| 798 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, Dump) { |
| 799 | dumpToLog(); |
| 800 | } |
| 801 | |
| 802 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
| 803 | const audio_attributes_t attr = GetParam(); |
| 804 | const audio_usage_t usage = attr.usage; |
| 805 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 806 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 807 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 808 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 809 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, &portId, attr); |
| 810 | if (std::find_if(begin(mUsageRules), end(mUsageRules), [&usage](const auto &usageRule) { |
| 811 | return (std::get<0>(usageRule) == usage) && |
| 812 | (std::get<2>(usageRule) == RULE_MATCH_ATTRIBUTE_USAGE);}) != end(mUsageRules) || |
| 813 | (strncmp(attr.tags, "addr=", strlen("addr=")) == 0 && |
| 814 | strncmp(attr.tags + strlen("addr="), mMixAddress.c_str(), |
| 815 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0)) { |
| 816 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 817 | } else { |
| 818 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | INSTANTIATE_TEST_CASE_P( |
| 823 | PlaybackReroutingUsageMatch, |
| 824 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 825 | testing::Values( |
| 826 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
| 827 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 828 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
| 829 | AUDIO_SOURCE_DEFAULT, 0, ""} |
| 830 | ) |
| 831 | ); |
| 832 | |
| 833 | INSTANTIATE_TEST_CASE_P( |
| 834 | PlaybackReroutingAddressPriorityMatch, |
| 835 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 836 | testing::Values( |
| 837 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
| 838 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 839 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
| 840 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 841 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 842 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 843 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 844 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
| 845 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 846 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
| 847 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 848 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 849 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 850 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 851 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 852 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 853 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 854 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 855 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 856 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 857 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 858 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 859 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 860 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
| 861 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 862 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 863 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 864 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 865 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 866 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 867 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 868 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 869 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 870 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 871 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
| 872 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 873 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VIRTUAL_SOURCE, |
| 874 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 875 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
| 876 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"} |
| 877 | ) |
| 878 | ); |
| 879 | |
| 880 | INSTANTIATE_TEST_CASE_P( |
| 881 | PlaybackReroutingUnHandledUsages, |
| 882 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 883 | testing::Values( |
| 884 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
| 885 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 886 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 887 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 888 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 889 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
| 890 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 891 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 892 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 893 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 894 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 895 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 896 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 897 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 898 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 899 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 900 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 901 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 902 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 903 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
| 904 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 905 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 906 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 907 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 908 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 909 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 910 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 911 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 912 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 913 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 914 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
| 915 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 916 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
| 917 | AUDIO_SOURCE_DEFAULT, 0, ""} |
| 918 | ) |
| 919 | ); |
| 920 | |
| 921 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
| 922 | public testing::WithParamInterface<audio_attributes_t> { |
| 923 | protected: |
| 924 | void SetUp() override; |
| 925 | void TearDown() override; |
| 926 | |
| 927 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 928 | |
| 929 | std::vector<PolicyMixTuple> mSourceRules = { |
| 930 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 931 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 932 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} |
| 933 | }; |
| 934 | |
| 935 | struct audio_port mExtractionPort; |
| 936 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 937 | }; |
| 938 | |
| 939 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
| 940 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 941 | |
| 942 | mTracker.reset(new RecordingActivityTracker()); |
| 943 | |
| 944 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 945 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 946 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 947 | audioConfig.sample_rate = 48000; |
| 948 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 949 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, mSourceRules); |
| 950 | ASSERT_EQ(NO_ERROR, ret); |
| 951 | |
| 952 | struct audio_port injectionPort; |
| 953 | findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 954 | mMixAddress, injectionPort); |
| 955 | |
| 956 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 957 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
| 958 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, 0, ""}; |
| 959 | std::string tags = std::string("addr=") + mMixAddress; |
| 960 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 961 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 962 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, &mPortId, attr); |
| 963 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 964 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 965 | |
| 966 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 967 | mMixAddress, mExtractionPort); |
| 968 | } |
| 969 | |
| 970 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 971 | mManager->stopOutput(mPortId); |
| 972 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 973 | } |
| 974 | |
| 975 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, InitSuccess) { |
| 976 | // SetUp mush finish with no assertions. |
| 977 | } |
| 978 | |
| 979 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, Dump) { |
| 980 | dumpToLog(); |
| 981 | } |
| 982 | |
| 983 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
| 984 | const audio_attributes_t attr = GetParam(); |
| 985 | const audio_source_t source = attr.source; |
| 986 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame^] | 987 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 988 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 989 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 990 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 991 | if (std::find_if(begin(mSourceRules), end(mSourceRules), [&source](const auto &sourceRule) { |
| 992 | return (std::get<1>(sourceRule) == source) && |
| 993 | (std::get<2>(sourceRule) == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET);}) |
| 994 | != end(mSourceRules)) { |
| 995 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 996 | } else { |
| 997 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | // No address priority rule for remote recording, address is a "don't care" |
| 1002 | INSTANTIATE_TEST_CASE_P( |
| 1003 | RecordInjectionSourceMatch, |
| 1004 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1005 | testing::Values( |
| 1006 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1007 | AUDIO_SOURCE_CAMCORDER, 0, ""}, |
| 1008 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1009 | AUDIO_SOURCE_CAMCORDER, 0, "addr=remote_submix_media"}, |
| 1010 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1011 | AUDIO_SOURCE_MIC, 0, "addr=remote_submix_media"}, |
| 1012 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1013 | AUDIO_SOURCE_MIC, 0, ""}, |
| 1014 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1015 | AUDIO_SOURCE_VOICE_COMMUNICATION, 0, ""}, |
| 1016 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1017 | AUDIO_SOURCE_VOICE_COMMUNICATION, 0, |
| 1018 | "addr=remote_submix_media"} |
| 1019 | ) |
| 1020 | ); |
| 1021 | |
| 1022 | // No address priority rule for remote recording |
| 1023 | INSTANTIATE_TEST_CASE_P( |
| 1024 | RecordInjectionSourceNotMatch, |
| 1025 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1026 | testing::Values( |
| 1027 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1028 | AUDIO_SOURCE_VOICE_RECOGNITION, 0, ""}, |
| 1029 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1030 | AUDIO_SOURCE_HOTWORD, 0, ""}, |
| 1031 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1032 | AUDIO_SOURCE_VOICE_RECOGNITION, 0, |
| 1033 | "addr=remote_submix_media"}, |
| 1034 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 1035 | AUDIO_SOURCE_HOTWORD, 0, "addr=remote_submix_media"} |
| 1036 | ) |
| 1037 | ); |