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