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