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