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