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, |
| 141 | const std::string &address, audio_port *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, |
| 249 | audio_devices_t deviceType, const std::string &address, audio_port *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; |
| 259 | struct audio_port ports[numPorts]; |
| 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 | |
| 352 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest { |
| 353 | protected: |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 354 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 355 | void TearDown() override; |
| 356 | |
| 357 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 358 | sp<DeviceDescriptor> mMsdInputDevice; |
| 359 | }; |
| 360 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 361 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 362 | // TODO: Consider using Serializer to load part of the config from a string. |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 363 | AudioPolicyManagerTest::SetUpManagerConfig(); |
| 364 | AudioPolicyConfig& config = mManager->getConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 365 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 366 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 367 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 368 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 369 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 370 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 371 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
| 372 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 373 | // Match output profile from AudioPolicyConfig::setDefault. |
| 374 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 375 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 376 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 377 | config.addDevice(mMsdOutputDevice); |
| 378 | config.addDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 379 | |
| 380 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 381 | HwModuleCollection modules = config.getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 382 | modules.add(msdModule); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 383 | config.setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 384 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 385 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 386 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 387 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 388 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 389 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 390 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 391 | msdCompressedOutputProfile->setFlags( |
| 392 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 393 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 394 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 395 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
| 396 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 397 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 398 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 399 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 400 | msdModule->addInputProfile(msdInputProfile); |
| 401 | |
| 402 | // Add a profile with another encoding to the default device to test routing |
| 403 | // of streams that are not supported by MSD. |
| 404 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 405 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 406 | config.getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 407 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 408 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 409 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 410 | primaryEncodedOutputProfile->addSupportedDevice(config.getDefaultOutputDevice()); |
| 411 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 412 | addOutputProfile(primaryEncodedOutputProfile); |
| 413 | } |
| 414 | |
| 415 | void AudioPolicyManagerTestMsd::TearDown() { |
| 416 | mMsdOutputDevice.clear(); |
| 417 | mMsdInputDevice.clear(); |
| 418 | AudioPolicyManagerTest::TearDown(); |
| 419 | } |
| 420 | |
| 421 | TEST_F(AudioPolicyManagerTestMsd, InitSuccess) { |
| 422 | ASSERT_TRUE(mMsdOutputDevice); |
| 423 | ASSERT_TRUE(mMsdInputDevice); |
| 424 | } |
| 425 | |
| 426 | TEST_F(AudioPolicyManagerTestMsd, Dump) { |
| 427 | dumpToLog(); |
| 428 | } |
| 429 | |
| 430 | TEST_F(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 431 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 432 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 433 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 434 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 438 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 439 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 440 | getOutputForAttr(&selectedDeviceId, |
| 441 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 442 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 443 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 447 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 448 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 449 | getOutputForAttr(&selectedDeviceId, |
| 450 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 451 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 452 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 456 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 457 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 458 | getOutputForAttr(&selectedDeviceId, |
| 459 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 460 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 461 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 462 | getOutputForAttr(&selectedDeviceId, |
| 463 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 464 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 465 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 469 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 470 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 471 | getOutputForAttr(&selectedDeviceId, |
| 472 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 473 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 474 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
| 478 | // Switch between formats that are supported and not supported by MSD. |
| 479 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 480 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 481 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 482 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 483 | getOutputForAttr(&selectedDeviceId, |
| 484 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 485 | nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 486 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 487 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 488 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 489 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 490 | } |
| 491 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 492 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 493 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 494 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 495 | getOutputForAttr(&selectedDeviceId, |
| 496 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 497 | nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 498 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 499 | ASSERT_EQ(-1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 500 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 501 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 502 | } |
| 503 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 504 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 505 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 506 | getOutputForAttr(&selectedDeviceId, |
| 507 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 508 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 509 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 510 | } |
| 511 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 512 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 513 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 514 | protected: |
| 515 | void SetUpManagerConfig() override; |
| 516 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 517 | |
| 518 | static const std::string sExecutableDir; |
| 519 | static const std::string sDefaultConfig; |
| 520 | }; |
| 521 | |
| 522 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 523 | base::GetExecutableDirectory() + "/"; |
| 524 | |
| 525 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 526 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 527 | |
| 528 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
| 529 | status_t status = deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 530 | ASSERT_EQ(NO_ERROR, status); |
| 531 | } |
| 532 | |
| 533 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 534 | // SetUp must finish with no assertions. |
| 535 | } |
| 536 | |
| 537 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 538 | dumpToLog(); |
| 539 | } |
| 540 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 541 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 542 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 543 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 544 | protected: |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 545 | void TearDown() override; |
| 546 | |
| 547 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 548 | std::string mixAddress, const audio_config_t& audioConfig, |
| 549 | const std::vector<PolicyMixTuple>& rules); |
| 550 | void clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 551 | |
| 552 | Vector<AudioMix> mAudioMixes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 553 | const std::string mMixAddress = "remote_submix_media"; |
| 554 | }; |
| 555 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 556 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 557 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 558 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 562 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
| 563 | const std::vector<PolicyMixTuple>& rules) { |
| 564 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 565 | |
| 566 | for(const auto &rule: rules) { |
| 567 | myMixMatchCriteria.add(AudioMixMatchCriterion( |
| 568 | std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 569 | } |
| 570 | |
| 571 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 572 | String8(mixAddress.c_str()), 0); |
| 573 | myAudioMix.mDeviceType = deviceType; |
| 574 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 575 | mAudioMixes.clear(); |
| 576 | mAudioMixes.add(myAudioMix); |
| 577 | |
| 578 | // As the policy mixes registration may fail at some case, |
| 579 | // caller need to check the returned status. |
| 580 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 585 | if (mManager != nullptr) { |
| 586 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 587 | } |
| 588 | mAudioMixes.clear(); |
| 589 | } |
| 590 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 591 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 592 | // SetUp must finish with no assertions |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 596 | dumpToLog(); |
| 597 | } |
| 598 | |
| 599 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 600 | status_t ret; |
| 601 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 602 | |
| 603 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 604 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 605 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 606 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 607 | |
| 608 | // Fail due to the device is already connected. |
| 609 | clearPolicyMix(); |
| 610 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 611 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 612 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 613 | |
| 614 | // The first time to register policy mixes with valid parameter should succeed. |
| 615 | clearPolicyMix(); |
| 616 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 617 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 618 | audioConfig.sample_rate = 48000; |
| 619 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 620 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 621 | std::vector<PolicyMixTuple>()); |
| 622 | ASSERT_EQ(NO_ERROR, ret); |
| 623 | // Registering the same policy mixes should fail. |
| 624 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 625 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 626 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 627 | // Registration should fail due to device not found. |
| 628 | // Note that earpiece is not present in the test configuration file. |
| 629 | // 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] | 630 | clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 631 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 632 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 633 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 634 | |
| 635 | // Registration should fail due to output not found. |
| 636 | clearPolicyMix(); |
| 637 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 638 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 639 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 640 | |
| 641 | // The first time to register valid policy mixes should succeed. |
| 642 | clearPolicyMix(); |
| 643 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 644 | AUDIO_DEVICE_OUT_SPEAKER, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 645 | ASSERT_EQ(NO_ERROR, ret); |
| 646 | // Registering the same policy mixes should fail. |
| 647 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 648 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 649 | } |
| 650 | |
| 651 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 652 | status_t ret; |
| 653 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 654 | |
| 655 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 656 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 657 | audioConfig.sample_rate = 48000; |
| 658 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 659 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 660 | std::vector<PolicyMixTuple>()); |
| 661 | ASSERT_EQ(NO_ERROR, ret); |
| 662 | |
| 663 | // After successfully registering policy mixes, it should be able to unregister. |
| 664 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 665 | ASSERT_EQ(NO_ERROR, ret); |
| 666 | |
| 667 | // After unregistering policy mixes successfully, it should fail unregistering |
| 668 | // the same policy mixes as they are not registered. |
| 669 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 670 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 671 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 672 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 673 | class AudioPolicyManagerTestForHdmi |
| 674 | : public AudioPolicyManagerTestWithConfigurationFile { |
| 675 | protected: |
| 676 | void SetUp() override; |
| 677 | std::string getConfigFile() override { return sTvConfig; } |
| 678 | std::map<audio_format_t, bool> getSurroundFormatsHelper(bool reported); |
| 679 | std::unordered_set<audio_format_t> getFormatsFromPorts(); |
| 680 | AudioPolicyManagerTestClient* getClient() override { |
| 681 | return new AudioPolicyManagerTestClientForHdmi; |
| 682 | } |
| 683 | void TearDown() override; |
| 684 | |
| 685 | static const std::string sTvConfig; |
| 686 | |
| 687 | }; |
| 688 | |
| 689 | const std::string AudioPolicyManagerTestForHdmi::sTvConfig = |
| 690 | AudioPolicyManagerTestForHdmi::sExecutableDir + |
| 691 | "test_settop_box_surround_configuration.xml"; |
| 692 | |
| 693 | void AudioPolicyManagerTestForHdmi::SetUp() { |
| 694 | AudioPolicyManagerTest::SetUp(); |
| 695 | mClient->addSupportedFormat(AUDIO_FORMAT_E_AC3); |
| 696 | mManager->setDeviceConnectionState( |
| 697 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 698 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 699 | } |
| 700 | |
| 701 | void AudioPolicyManagerTestForHdmi::TearDown() { |
| 702 | mManager->setDeviceConnectionState( |
| 703 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 704 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 705 | AudioPolicyManagerTest::TearDown(); |
| 706 | } |
| 707 | |
| 708 | std::map<audio_format_t, bool> |
| 709 | AudioPolicyManagerTestForHdmi::getSurroundFormatsHelper(bool reported) { |
| 710 | unsigned int numSurroundFormats = 0; |
| 711 | std::map<audio_format_t, bool> surroundFormatsMap; |
| 712 | status_t ret = mManager->getSurroundFormats( |
| 713 | &numSurroundFormats, nullptr /* surroundFormats */, |
| 714 | nullptr /* surroundFormatsEnabled */, reported); |
| 715 | EXPECT_EQ(NO_ERROR, ret); |
| 716 | if (ret != NO_ERROR) { |
| 717 | return surroundFormatsMap; |
| 718 | } |
| 719 | audio_format_t surroundFormats[numSurroundFormats]; |
| 720 | memset(surroundFormats, 0, sizeof(audio_format_t) * numSurroundFormats); |
| 721 | bool surroundFormatsEnabled[numSurroundFormats]; |
| 722 | memset(surroundFormatsEnabled, 0, sizeof(bool) * numSurroundFormats); |
| 723 | ret = mManager->getSurroundFormats( |
| 724 | &numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
| 725 | EXPECT_EQ(NO_ERROR, ret); |
| 726 | if (ret != NO_ERROR) { |
| 727 | return surroundFormatsMap; |
| 728 | } |
| 729 | for (int i = 0; i< numSurroundFormats; i++) { |
| 730 | surroundFormatsMap[surroundFormats[i]] = surroundFormatsEnabled[i]; |
| 731 | } |
| 732 | return surroundFormatsMap; |
| 733 | } |
| 734 | |
| 735 | std::unordered_set<audio_format_t> |
| 736 | AudioPolicyManagerTestForHdmi::getFormatsFromPorts() { |
| 737 | uint32_t numPorts = 0; |
| 738 | uint32_t generation1; |
| 739 | status_t ret; |
| 740 | std::unordered_set<audio_format_t> formats; |
| 741 | ret = mManager->listAudioPorts( |
| 742 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 743 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 744 | if (ret != NO_ERROR) { |
| 745 | return formats; |
| 746 | } |
| 747 | struct audio_port ports[numPorts]; |
| 748 | ret = mManager->listAudioPorts( |
| 749 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation1); |
| 750 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 751 | if (ret != NO_ERROR) { |
| 752 | return formats; |
| 753 | } |
| 754 | for (const auto &port : ports) { |
| 755 | formats.insert(port.formats, port.formats + port.num_formats); |
| 756 | } |
| 757 | return formats; |
| 758 | } |
| 759 | |
| 760 | TEST_F(AudioPolicyManagerTestForHdmi, GetSurroundFormatsReturnsSupportedFormats) { |
| 761 | mManager->setForceUse( |
| 762 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
| 763 | auto surroundFormats = getSurroundFormatsHelper(false /*reported*/); |
| 764 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 765 | } |
| 766 | |
| 767 | TEST_F(AudioPolicyManagerTestForHdmi, |
| 768 | GetSurroundFormatsReturnsManipulatedFormats) { |
| 769 | mManager->setForceUse( |
| 770 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 771 | |
| 772 | status_t ret = |
| 773 | mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, false /*enabled*/); |
| 774 | ASSERT_EQ(NO_ERROR, ret); |
| 775 | auto surroundFormats = getSurroundFormatsHelper(false /*reported*/); |
| 776 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 777 | ASSERT_FALSE(surroundFormats[AUDIO_FORMAT_E_AC3]); |
| 778 | |
| 779 | ret = mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, true /*enabled*/); |
| 780 | ASSERT_EQ(NO_ERROR, ret); |
| 781 | surroundFormats = getSurroundFormatsHelper(false /*reported*/); |
| 782 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 783 | ASSERT_TRUE(surroundFormats[AUDIO_FORMAT_E_AC3]); |
| 784 | |
| 785 | ret = mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, false /*enabled*/); |
| 786 | ASSERT_EQ(NO_ERROR, ret); |
| 787 | surroundFormats = getSurroundFormatsHelper(false /*reported*/); |
| 788 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 789 | ASSERT_FALSE(surroundFormats[AUDIO_FORMAT_E_AC3]); |
| 790 | } |
| 791 | |
| 792 | TEST_F(AudioPolicyManagerTestForHdmi, |
| 793 | ListAudioPortsReturnManipulatedHdmiFormats) { |
| 794 | mManager->setForceUse( |
| 795 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 796 | |
| 797 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, false /*enabled*/)); |
| 798 | auto formats = getFormatsFromPorts(); |
| 799 | ASSERT_EQ(0, formats.count(AUDIO_FORMAT_E_AC3)); |
| 800 | |
| 801 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, true /*enabled*/)); |
| 802 | formats = getFormatsFromPorts(); |
| 803 | ASSERT_EQ(1, formats.count(AUDIO_FORMAT_E_AC3)); |
| 804 | } |
| 805 | |
| 806 | TEST_F(AudioPolicyManagerTestForHdmi, |
| 807 | GetReportedSurroundFormatsReturnsHdmiReportedFormats) { |
| 808 | mManager->setForceUse( |
| 809 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
| 810 | auto surroundFormats = getSurroundFormatsHelper(true /*reported*/); |
| 811 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 812 | } |
| 813 | |
| 814 | TEST_F(AudioPolicyManagerTestForHdmi, |
| 815 | GetReportedSurroundFormatsReturnsNonManipulatedHdmiReportedFormats) { |
| 816 | mManager->setForceUse( |
| 817 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 818 | |
| 819 | status_t ret = mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, false /*enabled*/); |
| 820 | ASSERT_EQ(NO_ERROR, ret); |
| 821 | auto surroundFormats = getSurroundFormatsHelper(true /*reported*/); |
| 822 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 823 | |
| 824 | ret = mManager->setSurroundFormatEnabled(AUDIO_FORMAT_E_AC3, true /*enabled*/); |
| 825 | ASSERT_EQ(NO_ERROR, ret); |
| 826 | surroundFormats = getSurroundFormatsHelper(true /*reported*/); |
| 827 | ASSERT_EQ(1, surroundFormats.count(AUDIO_FORMAT_E_AC3)); |
| 828 | } |
| 829 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 830 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 831 | protected: |
| 832 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 833 | |
| 834 | static const std::string sPrimaryOnlyConfig; |
| 835 | }; |
| 836 | |
| 837 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 838 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 839 | |
| 840 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 841 | // SetUp must finish with no assertions. |
| 842 | } |
| 843 | |
| 844 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 845 | dumpToLog(); |
| 846 | } |
| 847 | |
| 848 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 849 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 850 | status_t ret; |
| 851 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 852 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 853 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 854 | audioConfig.sample_rate = 48000; |
| 855 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 856 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 857 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 858 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 859 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 860 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 861 | } |
| 862 | |
| 863 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
| 864 | public testing::WithParamInterface<audio_attributes_t> { |
| 865 | protected: |
| 866 | void SetUp() override; |
| 867 | void TearDown() override; |
| 868 | |
| 869 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 870 | |
| 871 | std::vector<PolicyMixTuple> mUsageRules = { |
| 872 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 873 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE} |
| 874 | }; |
| 875 | |
| 876 | struct audio_port mInjectionPort; |
| 877 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 878 | }; |
| 879 | |
| 880 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
| 881 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 882 | |
| 883 | mTracker.reset(new RecordingActivityTracker()); |
| 884 | |
| 885 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 886 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 887 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 888 | audioConfig.sample_rate = 48000; |
| 889 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 890 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, mUsageRules); |
| 891 | ASSERT_EQ(NO_ERROR, ret); |
| 892 | |
| 893 | struct audio_port extractionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 894 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 895 | mMixAddress, &extractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 896 | |
| 897 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 898 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 899 | audio_attributes_t attr = { |
| 900 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 901 | std::string tags = "addr=" + mMixAddress; |
| 902 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 903 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, |
| 904 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &mPortId); |
| 905 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 906 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 907 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 908 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 909 | mMixAddress, &mInjectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 913 | mManager->stopInput(mPortId); |
| 914 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 915 | } |
| 916 | |
| 917 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, InitSuccess) { |
| 918 | // SetUp must finish with no assertions |
| 919 | } |
| 920 | |
| 921 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, Dump) { |
| 922 | dumpToLog(); |
| 923 | } |
| 924 | |
| 925 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
| 926 | const audio_attributes_t attr = GetParam(); |
| 927 | const audio_usage_t usage = attr.usage; |
| 928 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 929 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 930 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 931 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, |
| 932 | nullptr /*output*/, nullptr /*portId*/, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 933 | if (std::find_if(begin(mUsageRules), end(mUsageRules), [&usage](const auto &usageRule) { |
| 934 | return (std::get<0>(usageRule) == usage) && |
| 935 | (std::get<2>(usageRule) == RULE_MATCH_ATTRIBUTE_USAGE);}) != end(mUsageRules) || |
| 936 | (strncmp(attr.tags, "addr=", strlen("addr=")) == 0 && |
| 937 | strncmp(attr.tags + strlen("addr="), mMixAddress.c_str(), |
| 938 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0)) { |
| 939 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 940 | } else { |
| 941 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | INSTANTIATE_TEST_CASE_P( |
| 946 | PlaybackReroutingUsageMatch, |
| 947 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 948 | testing::Values( |
| 949 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 950 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 951 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 952 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 953 | ) |
| 954 | ); |
| 955 | |
| 956 | INSTANTIATE_TEST_CASE_P( |
| 957 | PlaybackReroutingAddressPriorityMatch, |
| 958 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 959 | testing::Values( |
| 960 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 961 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 962 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 963 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 964 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 965 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 966 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 967 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 968 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 969 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 970 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 971 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 972 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 973 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 974 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 975 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 976 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 977 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 978 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 979 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 980 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 981 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 982 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 983 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 984 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 985 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 986 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 987 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 988 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 989 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 990 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 991 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 992 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 993 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 994 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 995 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 996 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VIRTUAL_SOURCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 997 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 998 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 999 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 1000 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1001 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1002 | ) |
| 1003 | ); |
| 1004 | |
| 1005 | INSTANTIATE_TEST_CASE_P( |
| 1006 | PlaybackReroutingUnHandledUsages, |
| 1007 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1008 | testing::Values( |
| 1009 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1010 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1011 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1012 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1013 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1014 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1015 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1016 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1017 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1018 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1019 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1020 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1021 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1022 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1023 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1024 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1025 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1026 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1027 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1028 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1029 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1030 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1031 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1032 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1033 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1034 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1035 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1036 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1037 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1038 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1039 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1040 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1041 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1042 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 1043 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1044 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1045 | ) |
| 1046 | ); |
| 1047 | |
| 1048 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
| 1049 | public testing::WithParamInterface<audio_attributes_t> { |
| 1050 | protected: |
| 1051 | void SetUp() override; |
| 1052 | void TearDown() override; |
| 1053 | |
| 1054 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 1055 | |
| 1056 | std::vector<PolicyMixTuple> mSourceRules = { |
| 1057 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 1058 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 1059 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} |
| 1060 | }; |
| 1061 | |
| 1062 | struct audio_port mExtractionPort; |
| 1063 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 1064 | }; |
| 1065 | |
| 1066 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
| 1067 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 1068 | |
| 1069 | mTracker.reset(new RecordingActivityTracker()); |
| 1070 | |
| 1071 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1072 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1073 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1074 | audioConfig.sample_rate = 48000; |
| 1075 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1076 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, mSourceRules); |
| 1077 | ASSERT_EQ(NO_ERROR, ret); |
| 1078 | |
| 1079 | struct audio_port injectionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1080 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1081 | mMixAddress, &injectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1082 | |
| 1083 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1084 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1085 | audio_attributes_t attr = |
| 1086 | {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1087 | std::string tags = std::string("addr=") + mMixAddress; |
| 1088 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 1089 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1090 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, &mPortId, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1091 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 1092 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 1093 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1094 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1095 | mMixAddress, &mExtractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 1099 | mManager->stopOutput(mPortId); |
| 1100 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 1101 | } |
| 1102 | |
| 1103 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, InitSuccess) { |
| 1104 | // SetUp mush finish with no assertions. |
| 1105 | } |
| 1106 | |
| 1107 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, Dump) { |
| 1108 | dumpToLog(); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
| 1112 | const audio_attributes_t attr = GetParam(); |
| 1113 | const audio_source_t source = attr.source; |
| 1114 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1115 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1116 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 1117 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 1118 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 1119 | if (std::find_if(begin(mSourceRules), end(mSourceRules), [&source](const auto &sourceRule) { |
| 1120 | return (std::get<1>(sourceRule) == source) && |
| 1121 | (std::get<2>(sourceRule) == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET);}) |
| 1122 | != end(mSourceRules)) { |
| 1123 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 1124 | } else { |
| 1125 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | // No address priority rule for remote recording, address is a "don't care" |
| 1130 | INSTANTIATE_TEST_CASE_P( |
| 1131 | RecordInjectionSourceMatch, |
| 1132 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1133 | testing::Values( |
| 1134 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1135 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1136 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1137 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, |
| 1138 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1139 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1140 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, |
| 1141 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1142 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1143 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1144 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1145 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1146 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1147 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1148 | "addr=remote_submix_media"} |
| 1149 | ) |
| 1150 | ); |
| 1151 | |
| 1152 | // No address priority rule for remote recording |
| 1153 | INSTANTIATE_TEST_CASE_P( |
| 1154 | RecordInjectionSourceNotMatch, |
| 1155 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1156 | testing::Values( |
| 1157 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1158 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1159 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1160 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1161 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1162 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1163 | "addr=remote_submix_media"}, |
| 1164 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1165 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, |
| 1166 | "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1167 | ) |
| 1168 | ); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1169 | |
| 1170 | using DeviceConnectionTestParams = |
| 1171 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 1172 | |
| 1173 | class AudioPolicyManagerTestDeviceConnection : public AudioPolicyManagerTestWithConfigurationFile, |
| 1174 | public testing::WithParamInterface<DeviceConnectionTestParams> { |
| 1175 | }; |
| 1176 | |
| 1177 | TEST_F(AudioPolicyManagerTestDeviceConnection, InitSuccess) { |
| 1178 | // SetUp must finish with no assertions. |
| 1179 | } |
| 1180 | |
| 1181 | TEST_F(AudioPolicyManagerTestDeviceConnection, Dump) { |
| 1182 | dumpToLog(); |
| 1183 | } |
| 1184 | |
| 1185 | TEST_P(AudioPolicyManagerTestDeviceConnection, SetDeviceConnectionState) { |
| 1186 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1187 | const std::string name = std::get<1>(GetParam()); |
| 1188 | const std::string address = std::get<2>(GetParam()); |
| 1189 | |
| 1190 | if (type == AUDIO_DEVICE_OUT_HDMI) { |
| 1191 | // Set device connection state failed due to no device descriptor found |
| 1192 | // For HDMI case, it is easier to simulate device descriptor not found error |
| 1193 | // by using a undeclared encoded format. |
| 1194 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1195 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1196 | address.c_str(), name.c_str(), AUDIO_FORMAT_MAT_2_1)); |
| 1197 | } |
| 1198 | // Connect with valid parameters should succeed |
| 1199 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1200 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1201 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1202 | // Try to connect with the same device again should fail |
| 1203 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1204 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1205 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1206 | // Disconnect the connected device should succeed |
| 1207 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1208 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1209 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1210 | // Disconnect device that is not connected should fail |
| 1211 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1212 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1213 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1214 | // Try to set device connection state with a invalid connection state should fail |
| 1215 | ASSERT_EQ(BAD_VALUE, mManager->setDeviceConnectionState( |
| 1216 | type, AUDIO_POLICY_DEVICE_STATE_CNT, |
| 1217 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1218 | } |
| 1219 | |
| 1220 | TEST_P(AudioPolicyManagerTestDeviceConnection, ExplicitlyRoutingAfterConnection) { |
| 1221 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1222 | const std::string name = std::get<1>(GetParam()); |
| 1223 | const std::string address = std::get<2>(GetParam()); |
| 1224 | |
| 1225 | // Connect device to do explicitly routing test |
| 1226 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1227 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1228 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1229 | |
| 1230 | audio_port devicePort; |
| 1231 | const audio_port_role_t role = audio_is_output_device(type) |
| 1232 | ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1233 | ASSERT_TRUE(findDevicePort(role, type, address, &devicePort)); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1234 | |
| 1235 | audio_port_handle_t routedPortId = devicePort.id; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1236 | // Try start input or output according to the device type |
| 1237 | if (audio_is_output_devices(type)) { |
| 1238 | getOutputForAttr(&routedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1239 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1240 | } else if (audio_is_input_device(type)) { |
| 1241 | RecordingActivityTracker tracker; |
| 1242 | getInputForAttr({}, tracker.getRiid(), &routedPortId, AUDIO_FORMAT_PCM_16_BIT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1243 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1244 | } |
| 1245 | ASSERT_EQ(devicePort.id, routedPortId); |
| 1246 | |
| 1247 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1248 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1249 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1250 | } |
| 1251 | |
| 1252 | INSTANTIATE_TEST_CASE_P( |
| 1253 | DeviceConnectionState, |
| 1254 | AudioPolicyManagerTestDeviceConnection, |
| 1255 | testing::Values( |
| 1256 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_HDMI, "test_in_hdmi", |
| 1257 | "audio_policy_test_in_hdmi"}), |
| 1258 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_HDMI, "test_out_hdmi", |
| 1259 | "audio_policy_test_out_hdmi"}), |
| 1260 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "bt_hfp_in", |
| 1261 | "hfp_client_in"}), |
| 1262 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "bt_hfp_out", |
| 1263 | "hfp_client_out"}) |
| 1264 | ) |
| 1265 | ); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1266 | |
| 1267 | class AudioPolicyManagerTVTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1268 | protected: |
| 1269 | std::string getConfigFile() override { return sTvConfig; } |
| 1270 | void testHDMIPortSelection(audio_output_flags_t flags, const char* expectedMixPortName); |
| 1271 | |
| 1272 | static const std::string sTvConfig; |
| 1273 | }; |
| 1274 | |
| 1275 | const std::string AudioPolicyManagerTVTest::sTvConfig = |
| 1276 | AudioPolicyManagerTVTest::sExecutableDir + "test_tv_apm_configuration.xml"; |
| 1277 | |
| 1278 | // SwAudioOutputDescriptor doesn't populate flags so check against the port name. |
| 1279 | void AudioPolicyManagerTVTest::testHDMIPortSelection( |
| 1280 | audio_output_flags_t flags, const char* expectedMixPortName) { |
| 1281 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1282 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1283 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1284 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1285 | audio_io_handle_t output; |
| 1286 | audio_port_handle_t portId; |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1287 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1288 | flags, &output, &portId); |
| 1289 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 1290 | ASSERT_NE(nullptr, outDesc.get()); |
| 1291 | audio_port port = {}; |
| 1292 | outDesc->toAudioPort(&port); |
| 1293 | mManager->releaseOutput(portId); |
| 1294 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1295 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1296 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1297 | ASSERT_EQ(AUDIO_PORT_TYPE_MIX, port.type); |
| 1298 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, port.role); |
| 1299 | ASSERT_STREQ(expectedMixPortName, port.name); |
| 1300 | } |
| 1301 | |
| 1302 | TEST_F(AudioPolicyManagerTVTest, InitSuccess) { |
| 1303 | // SetUp must finish with no assertions. |
| 1304 | } |
| 1305 | |
| 1306 | TEST_F(AudioPolicyManagerTVTest, Dump) { |
| 1307 | dumpToLog(); |
| 1308 | } |
| 1309 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1310 | TEST_F(AudioPolicyManagerTVTest, MatchNoFlags) { |
| 1311 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_NONE, "primary output"); |
| 1312 | } |
| 1313 | |
| 1314 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectNoHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1315 | // b/140447125: The selected port must not have HW AV Sync flag (see the config file). |
| 1316 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_DIRECT, "direct"); |
| 1317 | } |
| 1318 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1319 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1320 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1321 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
| 1322 | "tunnel"); |
| 1323 | } |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1324 | |
| 1325 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectMMapNoIrq) { |
| 1326 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1327 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_MMAP_NOIRQ), |
| 1328 | "low latency"); |
| 1329 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 1330 | |
| 1331 | class AudioPolicyManagerDynamicHwModulesTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1332 | protected: |
| 1333 | void SetUpManagerConfig() override; |
| 1334 | }; |
| 1335 | |
| 1336 | void AudioPolicyManagerDynamicHwModulesTest::SetUpManagerConfig() { |
| 1337 | AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig(); |
| 1338 | // Only allow successful opening of "primary" hw module during APM initialization. |
| 1339 | mClient->swapAllowedModuleNames({"primary"}); |
| 1340 | } |
| 1341 | |
| 1342 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, InitSuccess) { |
| 1343 | // SetUp must finish with no assertions. |
| 1344 | } |
| 1345 | |
| 1346 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, DynamicAddition) { |
| 1347 | const auto handleBefore = mClient->peekNextModuleHandle(); |
| 1348 | mManager->onNewAudioModulesAvailable(); |
| 1349 | ASSERT_EQ(handleBefore, mClient->peekNextModuleHandle()); |
| 1350 | // Reset module loading restrictions. |
| 1351 | mClient->swapAllowedModuleNames(); |
| 1352 | mManager->onNewAudioModulesAvailable(); |
| 1353 | const auto handleAfter = mClient->peekNextModuleHandle(); |
| 1354 | ASSERT_GT(handleAfter, handleBefore); |
| 1355 | mManager->onNewAudioModulesAvailable(); |
| 1356 | ASSERT_EQ(handleAfter, mClient->peekNextModuleHandle()); |
| 1357 | } |
| 1358 | |
| 1359 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, AddedDeviceAvailable) { |
| 1360 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, mManager->getDeviceConnectionState( |
| 1361 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1362 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1363 | mManager->onNewAudioModulesAvailable(); |
| 1364 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_AVAILABLE, mManager->getDeviceConnectionState( |
| 1365 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1366 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1367 | |
| 1368 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ListAddedAudioPorts) { |
| 1369 | ASSERT_FALSE( |
| 1370 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", nullptr)); |
| 1371 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1372 | mManager->onNewAudioModulesAvailable(); |
| 1373 | struct audio_port port; |
| 1374 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", &port)); |
| 1375 | } |
| 1376 | |
| 1377 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ClientIsUpdated) { |
| 1378 | const size_t prevAudioPortListUpdateCount = mClient->getAudioPortListUpdateCount(); |
| 1379 | const uint32_t prevAudioPortGeneration = mManager->getAudioPortGeneration(); |
| 1380 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1381 | mManager->onNewAudioModulesAvailable(); |
| 1382 | EXPECT_GT(mClient->getAudioPortListUpdateCount(), prevAudioPortListUpdateCount); |
| 1383 | EXPECT_GT(mManager->getAudioPortGeneration(), prevAudioPortGeneration); |
| 1384 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 1385 | |
| 1386 | using DevicesRoleForCapturePresetParam = std::tuple<audio_source_t, device_role_t>; |
| 1387 | |
| 1388 | class AudioPolicyManagerDevicesRoleForCapturePresetTest |
| 1389 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 1390 | public testing::WithParamInterface<DevicesRoleForCapturePresetParam> { |
| 1391 | protected: |
| 1392 | // The `inputDevice` and `inputDevice2` indicate the audio devices type to be used for setting |
| 1393 | // device role. They must be declared in the test_audio_policy_configuration.xml |
| 1394 | AudioDeviceTypeAddr inputDevice = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_BUILTIN_MIC, ""); |
| 1395 | AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_HDMI, ""); |
| 1396 | }; |
| 1397 | |
| 1398 | TEST_P(AudioPolicyManagerDevicesRoleForCapturePresetTest, DevicesRoleForCapturePreset) { |
| 1399 | const audio_source_t audioSource = std::get<0>(GetParam()); |
| 1400 | const device_role_t role = std::get<1>(GetParam()); |
| 1401 | |
| 1402 | // Test invalid device when setting |
| 1403 | const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, ""); |
| 1404 | const AudioDeviceTypeAddrVector outputDevices = {outputDevice}; |
| 1405 | ASSERT_EQ(BAD_VALUE, |
| 1406 | mManager->setDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1407 | ASSERT_EQ(BAD_VALUE, |
| 1408 | mManager->addDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1409 | AudioDeviceTypeAddrVector devices; |
| 1410 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1411 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1412 | ASSERT_TRUE(devices.empty()); |
| 1413 | ASSERT_EQ(BAD_VALUE, |
| 1414 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1415 | |
| 1416 | // Without setting, call get/remove/clear must fail |
| 1417 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1418 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1419 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1420 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, devices)); |
| 1421 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1422 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1423 | |
| 1424 | // Test set/get devices role |
| 1425 | const AudioDeviceTypeAddrVector inputDevices = {inputDevice}; |
| 1426 | ASSERT_EQ(NO_ERROR, |
| 1427 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1428 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1429 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice)); |
| 1430 | |
| 1431 | // Test setting will change the previously set devices |
| 1432 | const AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2}; |
| 1433 | ASSERT_EQ(NO_ERROR, |
| 1434 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices2)); |
| 1435 | devices.clear(); |
| 1436 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1437 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1438 | |
| 1439 | // Test add devices |
| 1440 | ASSERT_EQ(NO_ERROR, |
| 1441 | mManager->addDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1442 | devices.clear(); |
| 1443 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1444 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice, inputDevice2)); |
| 1445 | |
| 1446 | // Test remove devices |
| 1447 | ASSERT_EQ(NO_ERROR, |
| 1448 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1449 | devices.clear(); |
| 1450 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1451 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1452 | |
| 1453 | // Test remove devices that are not set as the device role |
| 1454 | ASSERT_EQ(BAD_VALUE, |
| 1455 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1456 | |
| 1457 | // Test clear devices |
| 1458 | ASSERT_EQ(NO_ERROR, |
| 1459 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1460 | devices.clear(); |
| 1461 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1462 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1463 | } |
| 1464 | |
| 1465 | INSTANTIATE_TEST_CASE_P( |
| 1466 | DevicesRoleForCapturePresetOperation, |
| 1467 | AudioPolicyManagerDevicesRoleForCapturePresetTest, |
| 1468 | testing::Values( |
| 1469 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_MIC, DEVICE_ROLE_PREFERRED}), |
| 1470 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_UPLINK, |
| 1471 | DEVICE_ROLE_PREFERRED}), |
| 1472 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_DOWNLINK, |
| 1473 | DEVICE_ROLE_PREFERRED}), |
| 1474 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_CALL, DEVICE_ROLE_PREFERRED}), |
| 1475 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_CAMCORDER, DEVICE_ROLE_PREFERRED}), |
| 1476 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_RECOGNITION, |
| 1477 | DEVICE_ROLE_PREFERRED}), |
| 1478 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_COMMUNICATION, |
| 1479 | DEVICE_ROLE_PREFERRED}), |
| 1480 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_REMOTE_SUBMIX, |
| 1481 | DEVICE_ROLE_PREFERRED}), |
| 1482 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_UNPROCESSED, DEVICE_ROLE_PREFERRED}), |
| 1483 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_PERFORMANCE, |
| 1484 | DEVICE_ROLE_PREFERRED}), |
| 1485 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_ECHO_REFERENCE, |
| 1486 | DEVICE_ROLE_PREFERRED}), |
| 1487 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_FM_TUNER, DEVICE_ROLE_PREFERRED}), |
| 1488 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_HOTWORD, DEVICE_ROLE_PREFERRED}) |
| 1489 | ) |
| 1490 | ); |