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