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