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