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