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 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 17 | #include <cstring> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 18 | #include <memory> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 19 | #include <string> |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 20 | #include <sys/wait.h> |
| 21 | #include <unistd.h> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 22 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 23 | #include <gtest/gtest.h> |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 24 | #include <gmock/gmock.h> |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 25 | |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 26 | #define LOG_TAG "APM_Test" |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 27 | #include <Serializer.h> |
| 28 | #include <android-base/file.h> |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 29 | #include <android/content/AttributionSourceState.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 30 | #include <media/AudioPolicy.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 31 | #include <media/PatchBuilder.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 32 | #include <media/RecordingActivityTracker.h> |
| 33 | #include <utils/Log.h> |
| 34 | #include <utils/Vector.h> |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 35 | #include <cutils/multiuser.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 36 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 37 | #include "AudioPolicyInterface.h" |
jiabin | b3f9804 | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 38 | #include "AudioPolicyManagerTestClient.h" |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 39 | #include "AudioPolicyTestClient.h" |
| 40 | #include "AudioPolicyTestManager.h" |
| 41 | |
| 42 | using namespace android; |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 43 | using testing::UnorderedElementsAre; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 44 | using android::content::AttributionSourceState; |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 45 | |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | AudioMixMatchCriterion createUidCriterion(uint32_t uid, bool exclude = false) { |
| 49 | AudioMixMatchCriterion criterion; |
| 50 | criterion.mValue.mUid = uid; |
| 51 | criterion.mRule = exclude ? RULE_EXCLUDE_UID : RULE_MATCH_UID; |
| 52 | return criterion; |
| 53 | } |
| 54 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 55 | AudioMixMatchCriterion createUserIdCriterion(int userId, bool exclude = false) { |
| 56 | AudioMixMatchCriterion criterion; |
| 57 | criterion.mValue.mUserId = userId; |
| 58 | criterion.mRule = exclude ? RULE_EXCLUDE_USERID : RULE_MATCH_USERID; |
| 59 | return criterion; |
| 60 | } |
| 61 | |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 62 | AudioMixMatchCriterion createUsageCriterion(audio_usage_t usage, bool exclude = false) { |
| 63 | AudioMixMatchCriterion criterion; |
| 64 | criterion.mValue.mUsage = usage; |
| 65 | criterion.mRule = exclude ? RULE_EXCLUDE_ATTRIBUTE_USAGE : RULE_MATCH_ATTRIBUTE_USAGE; |
| 66 | return criterion; |
| 67 | } |
| 68 | |
| 69 | AudioMixMatchCriterion createCapturePresetCriterion(audio_source_t source, bool exclude = false) { |
| 70 | AudioMixMatchCriterion criterion; |
| 71 | criterion.mValue.mSource = source; |
| 72 | criterion.mRule = exclude ? |
| 73 | RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET : RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET; |
| 74 | return criterion; |
| 75 | } |
| 76 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 77 | AudioMixMatchCriterion createSessionIdCriterion(audio_session_t session, bool exclude = false) { |
| 78 | AudioMixMatchCriterion criterion; |
| 79 | criterion.mValue.mAudioSessionId = session; |
| 80 | criterion.mRule = exclude ? |
| 81 | RULE_EXCLUDE_AUDIO_SESSION_ID : RULE_MATCH_AUDIO_SESSION_ID; |
| 82 | return criterion; |
| 83 | } |
| 84 | |
Jan Sebechlebsky | 8e10cea | 2023-03-29 11:43:31 +0200 | [diff] [blame] | 85 | // TODO b/182392769: use attribution source util |
| 86 | AttributionSourceState createAttributionSourceState(uid_t uid) { |
| 87 | AttributionSourceState attributionSourceState; |
| 88 | attributionSourceState.uid = uid; |
| 89 | attributionSourceState.token = sp<BBinder>::make(); |
| 90 | return attributionSourceState; |
| 91 | } |
| 92 | |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 95 | TEST(AudioPolicyConfigTest, DefaultConfigForTestsIsEmpty) { |
| 96 | auto config = AudioPolicyConfig::createWritableForTests(); |
| 97 | EXPECT_TRUE(config->getSource().empty()); |
| 98 | EXPECT_TRUE(config->getHwModules().isEmpty()); |
| 99 | EXPECT_TRUE(config->getInputDevices().isEmpty()); |
| 100 | EXPECT_TRUE(config->getOutputDevices().isEmpty()); |
| 101 | } |
| 102 | |
| 103 | TEST(AudioPolicyConfigTest, FallbackToDefault) { |
| 104 | auto config = AudioPolicyConfig::loadFromApmXmlConfigWithFallback( |
| 105 | base::GetExecutableDirectory() + "/test_invalid_audio_policy_configuration.xml"); |
| 106 | EXPECT_EQ(AudioPolicyConfig::kDefaultConfigSource, config->getSource()); |
| 107 | } |
| 108 | |
| 109 | TEST(AudioPolicyConfigTest, LoadForTests) { |
| 110 | { |
| 111 | auto result = AudioPolicyConfig::loadFromCustomXmlConfigForTests( |
| 112 | base::GetExecutableDirectory() + "/test_invalid_audio_policy_configuration.xml"); |
| 113 | EXPECT_FALSE(result.ok()); |
| 114 | } |
| 115 | { |
| 116 | const std::string source = |
| 117 | base::GetExecutableDirectory() + "/test_audio_policy_configuration.xml"; |
| 118 | auto result = AudioPolicyConfig::loadFromCustomXmlConfigForTests(source); |
| 119 | ASSERT_TRUE(result.ok()); |
| 120 | EXPECT_EQ(source, result.value()->getSource()); |
| 121 | EXPECT_FALSE(result.value()->getHwModules().isEmpty()); |
| 122 | EXPECT_FALSE(result.value()->getInputDevices().isEmpty()); |
| 123 | EXPECT_FALSE(result.value()->getOutputDevices().isEmpty()); |
| 124 | } |
| 125 | } |
| 126 | |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 127 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 128 | AudioPolicyTestClient client; |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 129 | auto config = AudioPolicyConfig::createWritableForTests(); |
| 130 | config->setDefault(); |
| 131 | config->setEngineLibraryNameSuffix("non-existent"); |
| 132 | AudioPolicyTestManager manager(config, &client); |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 133 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 134 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 135 | } |
| 136 | |
| 137 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 138 | AudioPolicyTestClient client; |
| 139 | AudioPolicyTestManager manager(&client); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 140 | // Since the default client fails to open anything, |
| 141 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 142 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 143 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 144 | } |
| 145 | |
| 146 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 147 | class PatchCountCheck { |
| 148 | public: |
| 149 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 150 | : mClient{client}, |
| 151 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 152 | int deltaFromSnapshot() const { |
| 153 | size_t currentCount = mClient->getActivePatchesCount(); |
| 154 | if (mInitialCount <= currentCount) { |
| 155 | return currentCount - mInitialCount; |
| 156 | } else { |
| 157 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 158 | } |
| 159 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 160 | private: |
| 161 | const AudioPolicyManagerTestClient *mClient; |
| 162 | const size_t mInitialCount; |
| 163 | }; |
| 164 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 165 | class AudioPolicyManagerTest : public testing::Test { |
| 166 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 167 | void SetUp() override; |
| 168 | void TearDown() override; |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 169 | virtual void SetUpManagerConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 170 | |
| 171 | void dumpToLog(); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 172 | // When explicit routing is needed, selectedDeviceId needs to be set as the wanted port |
| 173 | // id. Otherwise, selectedDeviceId needs to be initialized as AUDIO_PORT_HANDLE_NONE. |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 174 | void getOutputForAttr( |
| 175 | audio_port_handle_t *selectedDeviceId, |
| 176 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 177 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 178 | int sampleRate, |
| 179 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 180 | audio_io_handle_t *output = nullptr, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 181 | audio_port_handle_t *portId = nullptr, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 182 | audio_attributes_t attr = {}, |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 183 | audio_session_t session = AUDIO_SESSION_NONE, |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 184 | int uid = 0, |
| 185 | bool* isBitPerfect = nullptr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 186 | void getInputForAttr( |
| 187 | const audio_attributes_t &attr, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 188 | audio_session_t session, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 189 | audio_unique_id_t riid, |
| 190 | audio_port_handle_t *selectedDeviceId, |
| 191 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 192 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 193 | int sampleRate, |
| 194 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 195 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 196 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 197 | |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 198 | void getAudioPorts(audio_port_type_t type, audio_port_role_t role, |
| 199 | std::vector<audio_port_v7>* ports); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 200 | // Tries to find a device port. If 'foundPort' isn't nullptr, |
| 201 | // will generate a failure if the port hasn't been found. |
| 202 | bool findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 203 | const std::string &address, audio_port_v7 *foundPort); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 204 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch* patch); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 205 | virtual AudioPolicyManagerTestClient* getClient() { return new AudioPolicyManagerTestClient; } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 206 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 207 | sp<AudioPolicyConfig> mConfig; |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 208 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 209 | std::unique_ptr<AudioPolicyTestManager> mManager; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 210 | |
| 211 | const uint32_t k48000SamplingRate = 48000; |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | void AudioPolicyManagerTest::SetUp() { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 215 | mClient.reset(getClient()); |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 216 | ASSERT_NO_FATAL_FAILURE(SetUpManagerConfig()); // Subclasses may want to customize the config. |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 217 | mManager.reset(new AudioPolicyTestManager(mConfig, mClient.get())); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 218 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 219 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 220 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 221 | |
| 222 | void AudioPolicyManagerTest::TearDown() { |
| 223 | mManager.reset(); |
| 224 | mClient.reset(); |
| 225 | } |
| 226 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 227 | void AudioPolicyManagerTest::SetUpManagerConfig() { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 228 | mConfig = AudioPolicyConfig::createWritableForTests(); |
| 229 | mConfig->setDefault(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 232 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 233 | int pipefd[2]; |
| 234 | ASSERT_NE(-1, pipe(pipefd)); |
| 235 | pid_t cpid = fork(); |
| 236 | ASSERT_NE(-1, cpid); |
| 237 | if (cpid == 0) { |
| 238 | // Child process reads from the pipe and logs. |
| 239 | close(pipefd[1]); |
| 240 | std::string line; |
| 241 | char buf; |
| 242 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 243 | if (buf != '\n') { |
| 244 | line += buf; |
| 245 | } else { |
| 246 | ALOGI("%s", line.c_str()); |
| 247 | line = ""; |
| 248 | } |
| 249 | } |
| 250 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 251 | close(pipefd[0]); |
| 252 | _exit(EXIT_SUCCESS); |
| 253 | } else { |
| 254 | // Parent does the dump and checks the status code. |
| 255 | close(pipefd[0]); |
| 256 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 257 | close(pipefd[1]); |
| 258 | wait(NULL); // Wait for the child to exit. |
| 259 | } |
| 260 | } |
| 261 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 262 | void AudioPolicyManagerTest::getOutputForAttr( |
| 263 | audio_port_handle_t *selectedDeviceId, |
| 264 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 265 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 266 | int sampleRate, |
| 267 | audio_output_flags_t flags, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 268 | audio_io_handle_t *output, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 269 | audio_port_handle_t *portId, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 270 | audio_attributes_t attr, |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 271 | audio_session_t session, |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 272 | int uid, |
| 273 | bool* isBitPerfect) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 274 | audio_io_handle_t localOutput; |
| 275 | if (!output) output = &localOutput; |
| 276 | *output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 277 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 278 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 279 | config.sample_rate = sampleRate; |
| 280 | config.channel_mask = channelMask; |
| 281 | config.format = format; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 282 | audio_port_handle_t localPortId; |
| 283 | if (!portId) portId = &localPortId; |
| 284 | *portId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 285 | AudioPolicyInterface::output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 286 | bool isSpatialized; |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 287 | bool isBitPerfectInternal; |
Jan Sebechlebsky | 8e10cea | 2023-03-29 11:43:31 +0200 | [diff] [blame] | 288 | AttributionSourceState attributionSource = createAttributionSourceState(uid); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 289 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 290 | &attr, output, session, &stream, attributionSource, &config, &flags, |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 291 | selectedDeviceId, portId, {}, &outputType, &isSpatialized, |
| 292 | isBitPerfect == nullptr ? &isBitPerfectInternal : isBitPerfect)); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 293 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 294 | ASSERT_NE(AUDIO_IO_HANDLE_NONE, *output); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 295 | } |
| 296 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 297 | void AudioPolicyManagerTest::getInputForAttr( |
| 298 | const audio_attributes_t &attr, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 299 | const audio_session_t session, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 300 | audio_unique_id_t riid, |
| 301 | audio_port_handle_t *selectedDeviceId, |
| 302 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 303 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 304 | int sampleRate, |
| 305 | audio_input_flags_t flags, |
| 306 | audio_port_handle_t *portId) { |
| 307 | audio_io_handle_t input = AUDIO_PORT_HANDLE_NONE; |
| 308 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 309 | config.sample_rate = sampleRate; |
| 310 | config.channel_mask = channelMask; |
| 311 | config.format = format; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 312 | audio_port_handle_t localPortId; |
| 313 | if (!portId) portId = &localPortId; |
| 314 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 315 | AudioPolicyInterface::input_type_t inputType; |
Jan Sebechlebsky | 8e10cea | 2023-03-29 11:43:31 +0200 | [diff] [blame] | 316 | AttributionSourceState attributionSource = createAttributionSourceState(/*uid=*/ 0); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 317 | ASSERT_EQ(OK, mManager->getInputForAttr( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 318 | &attr, &input, riid, session, attributionSource, &config, flags, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 319 | selectedDeviceId, &inputType, portId)); |
| 320 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 321 | } |
| 322 | |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 323 | void AudioPolicyManagerTest::getAudioPorts(audio_port_type_t type, audio_port_role_t role, |
| 324 | std::vector<audio_port_v7>* ports) { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 325 | uint32_t numPorts = 0; |
| 326 | uint32_t generation1; |
| 327 | status_t ret; |
| 328 | |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 329 | ret = mManager->listAudioPorts(role, type, &numPorts, nullptr, &generation1); |
| 330 | ASSERT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 331 | |
| 332 | uint32_t generation2; |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 333 | ports->resize(numPorts); |
| 334 | ret = mManager->listAudioPorts(role, type, &numPorts, ports->data(), &generation2); |
| 335 | ASSERT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 336 | ASSERT_EQ(generation1, generation2) << "Generations changed during ports retrieval"; |
| 337 | } |
| 338 | |
| 339 | bool AudioPolicyManagerTest::findDevicePort(audio_port_role_t role, |
| 340 | audio_devices_t deviceType, const std::string &address, audio_port_v7 *foundPort) { |
| 341 | std::vector<audio_port_v7> ports; |
| 342 | getAudioPorts(AUDIO_PORT_TYPE_DEVICE, role, &ports); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 343 | if (HasFailure()) return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 344 | |
| 345 | for (const auto &port : ports) { |
| 346 | if (port.role == role && port.ext.device.type == deviceType && |
| 347 | (strncmp(port.ext.device.address, address.c_str(), |
| 348 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 349 | if (foundPort) *foundPort = port; |
| 350 | return true; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 353 | if (foundPort) { |
| 354 | ADD_FAILURE() << "Device port with role " << role << " and address " |
| 355 | << address << " not found"; |
| 356 | } |
| 357 | return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 358 | } |
| 359 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 360 | audio_port_handle_t AudioPolicyManagerTest::getDeviceIdFromPatch( |
| 361 | const struct audio_patch* patch) { |
| 362 | // The logic here is the same as the one in AudioIoDescriptor. |
| 363 | // Note this function is aim to get routed device id for test. |
| 364 | // In that case, device to device patch is not expected here. |
| 365 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 366 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 367 | return patch->sinks[0].id; |
| 368 | } else { |
| 369 | return patch->sources[0].id; |
| 370 | } |
| 371 | } |
| 372 | return AUDIO_PORT_HANDLE_NONE; |
| 373 | } |
| 374 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 375 | |
| 376 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 377 | // SetUp must finish with no assertions. |
| 378 | } |
| 379 | |
| 380 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 381 | dumpToLog(); |
| 382 | } |
| 383 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 384 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 385 | audio_patch patch{}; |
| 386 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 387 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 388 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 389 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 390 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 391 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 392 | patch.num_sinks = 1; |
| 393 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 394 | patch.num_sources = 1; |
| 395 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 396 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 397 | patch.num_sources = 2; |
| 398 | patch.num_sinks = 1; |
| 399 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 400 | patch = {}; |
| 401 | patch.num_sources = 1; |
| 402 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 403 | patch.num_sinks = 1; |
| 404 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 405 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 406 | patch = {}; |
| 407 | patch.num_sources = 1; |
| 408 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 409 | patch.num_sinks = 1; |
| 410 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 411 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 412 | // Verify that the handle is left unchanged. |
| 413 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 414 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 418 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 419 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 420 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 421 | ASSERT_FALSE(mManager->getAvailableInputDevices().isEmpty()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 422 | PatchBuilder patchBuilder; |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 423 | patchBuilder.addSource(mManager->getAvailableInputDevices()[0]). |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 424 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 425 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 426 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 427 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | // TODO: Add patch creation tests that involve already existing patch |
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 | enum |
| 433 | { |
| 434 | MSD_AUDIO_PATCH_COUNT_NUM_AUDIO_PATCHES_INDEX = 0, |
| 435 | MSD_AUDIO_PATCH_COUNT_NAME_INDEX = 1 |
| 436 | }; |
| 437 | using MsdAudioPatchCountSpecification = std::tuple<size_t, std::string>; |
| 438 | |
| 439 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest, |
| 440 | public ::testing::WithParamInterface<MsdAudioPatchCountSpecification> { |
| 441 | public: |
| 442 | AudioPolicyManagerTestMsd(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 443 | protected: |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 444 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 445 | void TearDown() override; |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 446 | AudioProfileVector getDirectProfilesForAttributes(const audio_attributes_t& attr); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 447 | |
| 448 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 449 | sp<DeviceDescriptor> mMsdInputDevice; |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 450 | sp<DeviceDescriptor> mDefaultOutputDevice; |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 451 | |
| 452 | const size_t mExpectedAudioPatchCount; |
| 453 | sp<DeviceDescriptor> mSpdifDevice; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 454 | |
| 455 | sp<DeviceDescriptor> mHdmiInputDevice; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 456 | }; |
| 457 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 458 | AudioPolicyManagerTestMsd::AudioPolicyManagerTestMsd() |
| 459 | : mExpectedAudioPatchCount(std::get<MSD_AUDIO_PATCH_COUNT_NUM_AUDIO_PATCHES_INDEX>( |
| 460 | GetParam())) {} |
| 461 | |
| 462 | INSTANTIATE_TEST_CASE_P( |
| 463 | MsdAudioPatchCount, |
| 464 | AudioPolicyManagerTestMsd, |
| 465 | ::testing::Values( |
| 466 | MsdAudioPatchCountSpecification(1u, "single"), |
| 467 | MsdAudioPatchCountSpecification(2u, "dual") |
| 468 | ), |
| 469 | [](const ::testing::TestParamInfo<MsdAudioPatchCountSpecification> &info) { |
| 470 | return std::get<MSD_AUDIO_PATCH_COUNT_NAME_INDEX>(info.param); } |
| 471 | ); |
| 472 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 473 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 474 | // 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] | 475 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTest::SetUpManagerConfig()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 476 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 477 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 478 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, k48000SamplingRate); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 479 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 480 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, k48000SamplingRate); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 481 | sp<AudioProfile> iec958OutputProfile = new AudioProfile( |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 482 | AUDIO_FORMAT_IEC60958, AUDIO_CHANNEL_INDEX_MASK_24, k48000SamplingRate); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 483 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 484 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 485 | mMsdOutputDevice->addAudioProfile(iec958OutputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 486 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 487 | // Match output profile from AudioPolicyConfig::setDefault. |
| 488 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 489 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 490 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 491 | mConfig->addDevice(mMsdOutputDevice); |
| 492 | mConfig->addDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 493 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 494 | if (mExpectedAudioPatchCount == 2) { |
| 495 | // Add SPDIF device with PCM output profile as a second device for dual MSD audio patching. |
| 496 | mSpdifDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPDIF); |
| 497 | mSpdifDevice->addAudioProfile(pcmOutputProfile); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 498 | mConfig->addDevice(mSpdifDevice); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 499 | |
| 500 | sp<OutputProfile> spdifOutputProfile = new OutputProfile("spdif output"); |
| 501 | spdifOutputProfile->addAudioProfile(pcmOutputProfile); |
| 502 | spdifOutputProfile->addSupportedDevice(mSpdifDevice); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 503 | mConfig->getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 504 | addOutputProfile(spdifOutputProfile); |
| 505 | } |
| 506 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 507 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 508 | HwModuleCollection modules = mConfig->getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 509 | modules.add(msdModule); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 510 | mConfig->setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 511 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 512 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 513 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 514 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 515 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 516 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 517 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 518 | msdCompressedOutputProfile->setFlags( |
| 519 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 520 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 521 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 522 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 523 | sp<OutputProfile> msdIec958OutputProfile = new OutputProfile("msd iec958 input"); |
| 524 | msdIec958OutputProfile->addAudioProfile(iec958OutputProfile); |
| 525 | msdIec958OutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
| 526 | msdIec958OutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 527 | msdModule->addOutputProfile(msdIec958OutputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 528 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 529 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 530 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 531 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 532 | msdModule->addInputProfile(msdInputProfile); |
| 533 | |
| 534 | // Add a profile with another encoding to the default device to test routing |
| 535 | // of streams that are not supported by MSD. |
| 536 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 537 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, k48000SamplingRate); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 538 | mConfig->getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 539 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 540 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 541 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 542 | primaryEncodedOutputProfile->addSupportedDevice(mConfig->getDefaultOutputDevice()); |
| 543 | mConfig->getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 544 | addOutputProfile(primaryEncodedOutputProfile); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 545 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 546 | mDefaultOutputDevice = mConfig->getDefaultOutputDevice(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 547 | if (mExpectedAudioPatchCount == 2) { |
| 548 | mSpdifDevice->addAudioProfile(dtsOutputProfile); |
| 549 | primaryEncodedOutputProfile->addSupportedDevice(mSpdifDevice); |
| 550 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 551 | |
| 552 | // Add HDMI input device with IEC60958 profile for HDMI in -> MSD patching. |
| 553 | mHdmiInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_HDMI); |
| 554 | sp<AudioProfile> iec958InputProfile = new AudioProfile( |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 555 | AUDIO_FORMAT_IEC60958, AUDIO_CHANNEL_INDEX_MASK_24, k48000SamplingRate); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 556 | mHdmiInputDevice->addAudioProfile(iec958InputProfile); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 557 | mConfig->addDevice(mHdmiInputDevice); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 558 | sp<InputProfile> hdmiInputProfile = new InputProfile("hdmi input"); |
| 559 | hdmiInputProfile->addAudioProfile(iec958InputProfile); |
| 560 | hdmiInputProfile->setFlags(AUDIO_INPUT_FLAG_DIRECT); |
| 561 | hdmiInputProfile->addSupportedDevice(mHdmiInputDevice); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 562 | mConfig->getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 563 | addInputProfile(hdmiInputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void AudioPolicyManagerTestMsd::TearDown() { |
| 567 | mMsdOutputDevice.clear(); |
| 568 | mMsdInputDevice.clear(); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 569 | mDefaultOutputDevice.clear(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 570 | mSpdifDevice.clear(); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 571 | mHdmiInputDevice.clear(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 572 | AudioPolicyManagerTest::TearDown(); |
| 573 | } |
| 574 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 575 | AudioProfileVector AudioPolicyManagerTestMsd::getDirectProfilesForAttributes( |
| 576 | const audio_attributes_t& attr) { |
| 577 | AudioProfileVector audioProfilesVector; |
| 578 | mManager->getDirectProfilesForAttributes(&attr, audioProfilesVector); |
| 579 | return audioProfilesVector; |
| 580 | } |
| 581 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 582 | TEST_P(AudioPolicyManagerTestMsd, InitSuccess) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 583 | ASSERT_TRUE(mMsdOutputDevice); |
| 584 | ASSERT_TRUE(mMsdInputDevice); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 585 | ASSERT_TRUE(mDefaultOutputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 588 | TEST_P(AudioPolicyManagerTestMsd, Dump) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 589 | dumpToLog(); |
| 590 | } |
| 591 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 592 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 593 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 594 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 595 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 596 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 599 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationSetReleaseMsdOutputPatches) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 600 | const PatchCountCheck patchCount = snapshotPatchCount(); |
| 601 | DeviceVector devices = mManager->getAvailableOutputDevices(); |
| 602 | // Remove MSD output device to avoid patching to itself |
| 603 | devices.remove(mMsdOutputDevice); |
| 604 | ASSERT_EQ(mExpectedAudioPatchCount, devices.size()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 605 | mManager->setMsdOutputPatches(&devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 606 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
| 607 | // Dual patch: exercise creating one new audio patch and reusing another existing audio patch. |
| 608 | DeviceVector singleDevice(devices[0]); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 609 | mManager->releaseMsdOutputPatches(singleDevice); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 610 | ASSERT_EQ(mExpectedAudioPatchCount - 1, patchCount.deltaFromSnapshot()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 611 | mManager->setMsdOutputPatches(&devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 612 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 613 | mManager->releaseMsdOutputPatches(devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 614 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
| 615 | } |
| 616 | |
| 617 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 618 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 619 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 620 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, |
| 621 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 622 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 623 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 626 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 627 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 628 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 629 | getOutputForAttr(&selectedDeviceId, |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 630 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, k48000SamplingRate); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 631 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 632 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 635 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 636 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 637 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 638 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, |
| 639 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 640 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 641 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
| 642 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 643 | getOutputForAttr(&selectedDeviceId, |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 644 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, k48000SamplingRate); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 645 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 646 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 647 | } |
| 648 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 649 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 650 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 651 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 652 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, |
| 653 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 654 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 655 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 658 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 659 | // Switch between formats that are supported and not supported by MSD. |
| 660 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 661 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 662 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 663 | audio_port_handle_t portId; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 664 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, |
| 665 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, nullptr /*output*/, &portId); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 666 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 667 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 668 | mManager->releaseOutput(portId); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 669 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 670 | } |
| 671 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 672 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 673 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 674 | audio_port_handle_t portId; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 675 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, |
| 676 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 677 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 678 | ASSERT_EQ(-static_cast<int>(mExpectedAudioPatchCount), patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 679 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 680 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 681 | } |
| 682 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 683 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 684 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 685 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, |
| 686 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 687 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 688 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 691 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 692 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationFromHdmiInToMsd) { |
| 693 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 694 | uid_t uid = 42; |
| 695 | const PatchCountCheck patchCount = snapshotPatchCount(); |
| 696 | ASSERT_FALSE(mManager->getAvailableInputDevices().isEmpty()); |
| 697 | PatchBuilder patchBuilder; |
| 698 | patchBuilder. |
| 699 | addSource(mManager->getAvailableInputDevices(). |
| 700 | getDevice(AUDIO_DEVICE_IN_HDMI, String8(""), AUDIO_FORMAT_DEFAULT)). |
| 701 | addSink(mManager->getAvailableOutputDevices(). |
| 702 | getDevice(AUDIO_DEVICE_OUT_BUS, String8(""), AUDIO_FORMAT_DEFAULT)); |
| 703 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
| 704 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
| 705 | AudioPatchCollection patches = mManager->getAudioPatches(); |
| 706 | sp<AudioPatch> patch = patches.valueFor(handle); |
| 707 | ASSERT_EQ(1, patch->mPatch.num_sources); |
| 708 | ASSERT_EQ(1, patch->mPatch.num_sinks); |
| 709 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, patch->mPatch.sources[0].role); |
| 710 | ASSERT_EQ(AUDIO_PORT_ROLE_SINK, patch->mPatch.sinks[0].role); |
| 711 | ASSERT_EQ(AUDIO_FORMAT_IEC60958, patch->mPatch.sources[0].format); |
| 712 | ASSERT_EQ(AUDIO_FORMAT_IEC60958, patch->mPatch.sinks[0].format); |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 713 | ASSERT_EQ(AUDIO_CHANNEL_INDEX_MASK_24, patch->mPatch.sources[0].channel_mask); |
| 714 | ASSERT_EQ(AUDIO_CHANNEL_INDEX_MASK_24, patch->mPatch.sinks[0].channel_mask); |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 715 | ASSERT_EQ(k48000SamplingRate, patch->mPatch.sources[0].sample_rate); |
| 716 | ASSERT_EQ(k48000SamplingRate, patch->mPatch.sinks[0].sample_rate); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 717 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
| 718 | } |
| 719 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 720 | TEST_P(AudioPolicyManagerTestMsd, GetDirectProfilesForAttributesWithMsd) { |
| 721 | const audio_attributes_t attr = { |
| 722 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 723 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 724 | |
| 725 | // count expected direct profiles for the default device |
| 726 | int countDirectProfilesPrimary = 0; |
| 727 | const auto& primary = mManager->getConfig().getHwModules() |
| 728 | .getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 729 | for (const auto& outputProfile : primary->getOutputProfiles()) { |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 730 | if (outputProfile->asAudioPort()->isDirectOutput()) { |
| 731 | countDirectProfilesPrimary += outputProfile->asAudioPort()->getAudioProfiles().size(); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | // count expected direct profiles for the msd device |
| 736 | int countDirectProfilesMsd = 0; |
| 737 | const auto& msd = mManager->getConfig().getHwModules() |
| 738 | .getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 739 | for (const auto& outputProfile : msd->getOutputProfiles()) { |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 740 | if (outputProfile->asAudioPort()->isDirectOutput()) { |
| 741 | countDirectProfilesMsd += outputProfile->asAudioPort()->getAudioProfiles().size(); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | // before setting up MSD audio patches we only have the primary hal direct profiles |
| 746 | ASSERT_EQ(countDirectProfilesPrimary, getDirectProfilesForAttributes(attr).size()); |
| 747 | |
| 748 | DeviceVector outputDevices = mManager->getAvailableOutputDevices(); |
| 749 | // Remove MSD output device to avoid patching to itself |
| 750 | outputDevices.remove(mMsdOutputDevice); |
| 751 | mManager->setMsdOutputPatches(&outputDevices); |
| 752 | |
| 753 | // after setting up MSD audio patches the MSD direct profiles are added |
| 754 | ASSERT_EQ(countDirectProfilesPrimary + countDirectProfilesMsd, |
| 755 | getDirectProfilesForAttributes(attr).size()); |
| 756 | |
| 757 | mManager->releaseMsdOutputPatches(outputDevices); |
| 758 | // releasing the MSD audio patches gets us back to the primary hal direct profiles only |
| 759 | ASSERT_EQ(countDirectProfilesPrimary, getDirectProfilesForAttributes(attr).size()); |
| 760 | } |
| 761 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 762 | TEST_P(AudioPolicyManagerTestMsd, IsDirectPlaybackSupportedWithMsd) { |
| 763 | const audio_attributes_t attr = { |
| 764 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 765 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 766 | |
| 767 | audio_config_base_t directConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
| 768 | directConfig.format = AUDIO_FORMAT_DTS; |
| 769 | directConfig.sample_rate = 48000; |
| 770 | directConfig.channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 771 | |
| 772 | audio_config_base_t nonDirectConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
| 773 | nonDirectConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 774 | nonDirectConfig.sample_rate = 48000; |
| 775 | nonDirectConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 776 | |
| 777 | audio_config_base_t nonExistentConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
| 778 | nonExistentConfig.format = AUDIO_FORMAT_E_AC3; |
| 779 | nonExistentConfig.sample_rate = 48000; |
| 780 | nonExistentConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 781 | |
| 782 | audio_config_base_t msdDirectConfig1 = AUDIO_CONFIG_BASE_INITIALIZER; |
| 783 | msdDirectConfig1.format = AUDIO_FORMAT_AC3; |
| 784 | msdDirectConfig1.sample_rate = 48000; |
| 785 | msdDirectConfig1.channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 786 | |
| 787 | audio_config_base_t msdDirectConfig2 = AUDIO_CONFIG_BASE_INITIALIZER; |
| 788 | msdDirectConfig2.format = AUDIO_FORMAT_IEC60958; |
| 789 | msdDirectConfig2.sample_rate = 48000; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 790 | msdDirectConfig2.channel_mask = AUDIO_CHANNEL_INDEX_MASK_24; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 791 | |
| 792 | audio_config_base_t msdNonDirectConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
| 793 | msdNonDirectConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 794 | msdNonDirectConfig.sample_rate = 96000; |
| 795 | msdNonDirectConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 796 | |
| 797 | ASSERT_TRUE(mManager->isDirectOutputSupported(directConfig, attr)); |
| 798 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonDirectConfig, attr)); |
| 799 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonExistentConfig, attr)); |
| 800 | // before setting MSD patches the direct MSD configs return false |
| 801 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdDirectConfig1, attr)); |
| 802 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdDirectConfig2, attr)); |
| 803 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdNonDirectConfig, attr)); |
| 804 | |
| 805 | DeviceVector outputDevices = mManager->getAvailableOutputDevices(); |
| 806 | // Remove MSD output device to avoid patching to itself |
| 807 | outputDevices.remove(mMsdOutputDevice); |
| 808 | mManager->setMsdOutputPatches(&outputDevices); |
| 809 | |
| 810 | ASSERT_TRUE(mManager->isDirectOutputSupported(directConfig, attr)); |
| 811 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonDirectConfig, attr)); |
| 812 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonExistentConfig, attr)); |
| 813 | // after setting MSD patches the direct MSD configs return true |
| 814 | ASSERT_TRUE(mManager->isDirectOutputSupported(msdDirectConfig1, attr)); |
| 815 | ASSERT_TRUE(mManager->isDirectOutputSupported(msdDirectConfig2, attr)); |
| 816 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdNonDirectConfig, attr)); |
| 817 | |
| 818 | mManager->releaseMsdOutputPatches(outputDevices); |
| 819 | |
| 820 | ASSERT_TRUE(mManager->isDirectOutputSupported(directConfig, attr)); |
| 821 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonDirectConfig, attr)); |
| 822 | ASSERT_FALSE(mManager->isDirectOutputSupported(nonExistentConfig, attr)); |
| 823 | // AFTER releasing MSD patches the direct MSD configs return false |
| 824 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdDirectConfig1, attr)); |
| 825 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdDirectConfig2, attr)); |
| 826 | ASSERT_FALSE(mManager->isDirectOutputSupported(msdNonDirectConfig, attr)); |
| 827 | } |
| 828 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 829 | TEST_P(AudioPolicyManagerTestMsd, GetDirectPlaybackSupportWithMsd) { |
| 830 | const audio_attributes_t attr = { |
| 831 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 832 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 833 | |
| 834 | audio_config_t directConfig = AUDIO_CONFIG_INITIALIZER; |
| 835 | directConfig.format = AUDIO_FORMAT_DTS; |
| 836 | directConfig.sample_rate = 48000; |
| 837 | directConfig.channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 838 | |
| 839 | audio_config_t nonDirectConfig = AUDIO_CONFIG_INITIALIZER; |
| 840 | nonDirectConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 841 | nonDirectConfig.sample_rate = 48000; |
| 842 | nonDirectConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 843 | |
| 844 | audio_config_t nonExistentConfig = AUDIO_CONFIG_INITIALIZER; |
| 845 | nonExistentConfig.format = AUDIO_FORMAT_E_AC3; |
| 846 | nonExistentConfig.sample_rate = 48000; |
| 847 | nonExistentConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 848 | |
| 849 | audio_config_t msdDirectConfig1 = AUDIO_CONFIG_INITIALIZER; |
| 850 | msdDirectConfig1.format = AUDIO_FORMAT_AC3; |
| 851 | msdDirectConfig1.sample_rate = 48000; |
| 852 | msdDirectConfig1.channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 853 | |
| 854 | audio_config_t msdDirectConfig2 = AUDIO_CONFIG_INITIALIZER; |
| 855 | msdDirectConfig2.format = AUDIO_FORMAT_IEC60958; |
| 856 | msdDirectConfig2.sample_rate = 48000; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 857 | msdDirectConfig2.channel_mask = AUDIO_CHANNEL_INDEX_MASK_24; |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 858 | |
| 859 | audio_config_t msdNonDirectConfig = AUDIO_CONFIG_INITIALIZER; |
| 860 | msdNonDirectConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 861 | msdNonDirectConfig.sample_rate = 96000; |
| 862 | msdNonDirectConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 863 | |
| 864 | ASSERT_EQ(AUDIO_DIRECT_BITSTREAM_SUPPORTED, |
| 865 | mManager->getDirectPlaybackSupport(&attr, &directConfig)); |
| 866 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 867 | mManager->getDirectPlaybackSupport(&attr, &nonDirectConfig)); |
| 868 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 869 | mManager->getDirectPlaybackSupport(&attr, &nonExistentConfig)); |
| 870 | // before setting MSD patches the direct MSD configs return AUDIO_DIRECT_NOT_SUPPORTED |
| 871 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 872 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig1)); |
| 873 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 874 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig2)); |
| 875 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 876 | mManager->getDirectPlaybackSupport(&attr, &msdNonDirectConfig)); |
| 877 | |
| 878 | DeviceVector outputDevices = mManager->getAvailableOutputDevices(); |
| 879 | // Remove MSD output device to avoid patching to itself |
| 880 | outputDevices.remove(mMsdOutputDevice); |
| 881 | mManager->setMsdOutputPatches(&outputDevices); |
| 882 | |
| 883 | ASSERT_EQ(AUDIO_DIRECT_BITSTREAM_SUPPORTED, |
| 884 | mManager->getDirectPlaybackSupport(&attr, &directConfig)); |
| 885 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 886 | mManager->getDirectPlaybackSupport(&attr, &nonDirectConfig)); |
| 887 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 888 | mManager->getDirectPlaybackSupport(&attr, &nonExistentConfig)); |
| 889 | // after setting MSD patches the direct MSD configs return values according to their flags |
| 890 | ASSERT_EQ(AUDIO_DIRECT_OFFLOAD_SUPPORTED, |
| 891 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig1)); |
| 892 | ASSERT_EQ(AUDIO_DIRECT_BITSTREAM_SUPPORTED, |
| 893 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig2)); |
| 894 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 895 | mManager->getDirectPlaybackSupport(&attr, &msdNonDirectConfig)); |
| 896 | |
| 897 | mManager->releaseMsdOutputPatches(outputDevices); |
| 898 | |
| 899 | ASSERT_EQ(AUDIO_DIRECT_BITSTREAM_SUPPORTED, |
| 900 | mManager->getDirectPlaybackSupport(&attr, &directConfig)); |
| 901 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 902 | mManager->getDirectPlaybackSupport(&attr, &nonDirectConfig)); |
| 903 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 904 | mManager->getDirectPlaybackSupport(&attr, &nonExistentConfig)); |
| 905 | // after releasing MSD patches the direct MSD configs return AUDIO_DIRECT_NOT_SUPPORTED |
| 906 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 907 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig1)); |
| 908 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 909 | mManager->getDirectPlaybackSupport(&attr, &msdDirectConfig2)); |
| 910 | ASSERT_EQ(AUDIO_DIRECT_NOT_SUPPORTED, |
| 911 | mManager->getDirectPlaybackSupport(&attr, &msdNonDirectConfig)); |
| 912 | } |
| 913 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 914 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 915 | protected: |
| 916 | void SetUpManagerConfig() override; |
| 917 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 918 | |
| 919 | static const std::string sExecutableDir; |
| 920 | static const std::string sDefaultConfig; |
| 921 | }; |
| 922 | |
| 923 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 924 | base::GetExecutableDirectory() + "/"; |
| 925 | |
| 926 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 927 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 928 | |
| 929 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 930 | auto result = AudioPolicyConfig::loadFromCustomXmlConfigForTests(getConfigFile()); |
| 931 | ASSERT_TRUE(result.ok()); |
| 932 | mConfig = result.value(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 936 | // SetUp must finish with no assertions. |
| 937 | } |
| 938 | |
| 939 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 940 | dumpToLog(); |
| 941 | } |
| 942 | |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 943 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, ListAudioPortsHasFlags) { |
| 944 | // Create an input for VOIP TX because it's not opened automatically like outputs are. |
| 945 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 946 | audio_port_handle_t mixPortId = AUDIO_PORT_HANDLE_NONE; |
| 947 | audio_source_t source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 948 | audio_attributes_t attr = { |
| 949 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, AUDIO_FLAG_NONE, ""}; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 950 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 951 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO, 8000, AUDIO_INPUT_FLAG_VOIP_TX, &mixPortId)); |
Mikhail Naganov | 0805de1 | 2022-02-15 23:00:07 +0000 | [diff] [blame] | 952 | |
| 953 | std::vector<audio_port_v7> ports; |
| 954 | ASSERT_NO_FATAL_FAILURE( |
| 955 | getAudioPorts(AUDIO_PORT_TYPE_MIX, AUDIO_PORT_ROLE_NONE, &ports)); |
| 956 | EXPECT_NE(0, ports.size()); |
| 957 | bool hasFlags = false, foundPrimary = false, foundVoipRx = false, foundVoipTx = false; |
| 958 | for (const auto& port : ports) { |
| 959 | if ((port.active_config.config_mask & AUDIO_PORT_CONFIG_FLAGS) != 0) { |
| 960 | hasFlags = true; |
| 961 | if (port.role == AUDIO_PORT_ROLE_SOURCE) { |
| 962 | if ((port.active_config.flags.output & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
| 963 | foundPrimary = true; |
| 964 | } |
| 965 | if ((port.active_config.flags.output & AUDIO_OUTPUT_FLAG_VOIP_RX) != 0) { |
| 966 | foundVoipRx = true; |
| 967 | } |
| 968 | } else if (port.role == AUDIO_PORT_ROLE_SINK) { |
| 969 | if ((port.active_config.flags.input & AUDIO_INPUT_FLAG_VOIP_TX) != 0) { |
| 970 | foundVoipTx = true; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | EXPECT_TRUE(hasFlags); |
| 976 | EXPECT_TRUE(foundPrimary); |
| 977 | EXPECT_TRUE(foundVoipRx); |
| 978 | EXPECT_TRUE(foundVoipTx); |
| 979 | } |
| 980 | |
Ram Mohan M | 594558d | 2022-06-14 14:42:44 +0530 | [diff] [blame] | 981 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, HandleDeviceConfigChange) { |
| 982 | { |
| 983 | const auto prevCounter = mClient->getRoutingUpdatedCounter(); |
| 984 | |
| 985 | EXPECT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 986 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 987 | "", "", AUDIO_FORMAT_LDAC)); |
| 988 | const auto currCounter = mClient->getRoutingUpdatedCounter(); |
| 989 | EXPECT_GT(currCounter, prevCounter); |
| 990 | } |
| 991 | { |
| 992 | const auto prevCounter = mClient->getRoutingUpdatedCounter(); |
| 993 | // Update device configuration |
| 994 | EXPECT_EQ(NO_ERROR, mManager->handleDeviceConfigChange(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 995 | "" /*address*/, "" /*name*/, |
| 996 | AUDIO_FORMAT_AAC)); |
| 997 | |
| 998 | // As mClient marks isReconfigA2dpSupported to false, device state needs to be toggled for |
| 999 | // config changes to take effect |
| 1000 | const auto currCounter = mClient->getRoutingUpdatedCounter(); |
| 1001 | EXPECT_GT(currCounter, prevCounter); |
| 1002 | } |
| 1003 | } |
| 1004 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1005 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, PreferredMixerAttributes) { |
| 1006 | mClient->addSupportedFormat(AUDIO_FORMAT_PCM_16_BIT); |
| 1007 | mClient->addSupportedChannelMask(AUDIO_CHANNEL_OUT_STEREO); |
| 1008 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1009 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1010 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1011 | auto devices = mManager->getAvailableOutputDevices(); |
| 1012 | audio_port_handle_t maxPortId = 0; |
| 1013 | audio_port_handle_t speakerPortId; |
| 1014 | audio_port_handle_t usbPortId; |
| 1015 | for (auto device : devices) { |
| 1016 | maxPortId = std::max(maxPortId, device->getId()); |
| 1017 | if (device->type() == AUDIO_DEVICE_OUT_SPEAKER) { |
| 1018 | speakerPortId = device->getId(); |
| 1019 | } else if (device->type() == AUDIO_DEVICE_OUT_USB_DEVICE) { |
| 1020 | usbPortId = device->getId(); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | const uid_t uid = 1234; |
| 1025 | const uid_t otherUid = 4321; |
| 1026 | const audio_attributes_t mediaAttr = { |
| 1027 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 1028 | .usage = AUDIO_USAGE_MEDIA, |
| 1029 | }; |
| 1030 | const audio_attributes_t alarmAttr = { |
| 1031 | .content_type = AUDIO_CONTENT_TYPE_SONIFICATION, |
| 1032 | .usage = AUDIO_USAGE_ALARM, |
| 1033 | }; |
| 1034 | |
| 1035 | std::vector<audio_mixer_attributes_t> mixerAttributes; |
| 1036 | EXPECT_EQ(NO_ERROR, mManager->getSupportedMixerAttributes(usbPortId, mixerAttributes)); |
| 1037 | for (const auto attrToSet : mixerAttributes) { |
| 1038 | audio_mixer_attributes_t attrFromQuery = AUDIO_MIXER_ATTRIBUTES_INITIALIZER; |
| 1039 | |
| 1040 | // The given device is not available |
| 1041 | EXPECT_EQ(BAD_VALUE, |
| 1042 | mManager->setPreferredMixerAttributes( |
| 1043 | &mediaAttr, maxPortId + 1, uid, &attrToSet)); |
| 1044 | // The only allowed device is USB |
| 1045 | EXPECT_EQ(BAD_VALUE, |
| 1046 | mManager->setPreferredMixerAttributes( |
| 1047 | &mediaAttr, speakerPortId, uid, &attrToSet)); |
| 1048 | // The only allowed usage is media |
| 1049 | EXPECT_EQ(BAD_VALUE, |
| 1050 | mManager->setPreferredMixerAttributes(&alarmAttr, usbPortId, uid, &attrToSet)); |
| 1051 | // Nothing set yet, must get null when query |
| 1052 | EXPECT_EQ(NAME_NOT_FOUND, |
| 1053 | mManager->getPreferredMixerAttributes(&mediaAttr, usbPortId, &attrFromQuery)); |
| 1054 | EXPECT_EQ(NO_ERROR, |
| 1055 | mManager->setPreferredMixerAttributes( |
| 1056 | &mediaAttr, usbPortId, uid, &attrToSet)); |
| 1057 | EXPECT_EQ(NO_ERROR, |
| 1058 | mManager->getPreferredMixerAttributes(&mediaAttr, usbPortId, &attrFromQuery)); |
| 1059 | EXPECT_EQ(attrToSet.config.format, attrFromQuery.config.format); |
| 1060 | EXPECT_EQ(attrToSet.config.sample_rate, attrFromQuery.config.sample_rate); |
| 1061 | EXPECT_EQ(attrToSet.config.channel_mask, attrFromQuery.config.channel_mask); |
| 1062 | EXPECT_EQ(attrToSet.mixer_behavior, attrFromQuery.mixer_behavior); |
| 1063 | EXPECT_EQ(NAME_NOT_FOUND, |
| 1064 | mManager->clearPreferredMixerAttributes(&mediaAttr, speakerPortId, uid)); |
| 1065 | EXPECT_EQ(PERMISSION_DENIED, |
| 1066 | mManager->clearPreferredMixerAttributes(&mediaAttr, usbPortId, otherUid)); |
| 1067 | EXPECT_EQ(NO_ERROR, |
| 1068 | mManager->clearPreferredMixerAttributes(&mediaAttr, usbPortId, uid)); |
| 1069 | } |
| 1070 | |
| 1071 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1072 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1073 | "", "", AUDIO_FORMAT_LDAC)); |
| 1074 | } |
| 1075 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 1076 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, RoutingChangedWithPreferredMixerAttributes) { |
| 1077 | mClient->addSupportedFormat(AUDIO_FORMAT_PCM_16_BIT); |
| 1078 | mClient->addSupportedChannelMask(AUDIO_CHANNEL_OUT_STEREO); |
| 1079 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1080 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1081 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1082 | auto devices = mManager->getAvailableOutputDevices(); |
| 1083 | audio_port_handle_t usbPortId = AUDIO_PORT_HANDLE_NONE; |
| 1084 | for (auto device : devices) { |
| 1085 | if (device->type() == AUDIO_DEVICE_OUT_USB_DEVICE) { |
| 1086 | usbPortId = device->getId(); |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | EXPECT_NE(AUDIO_PORT_HANDLE_NONE, usbPortId); |
| 1091 | |
| 1092 | const uid_t uid = 1234; |
| 1093 | const audio_attributes_t mediaAttr = { |
| 1094 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 1095 | .usage = AUDIO_USAGE_MEDIA, |
| 1096 | }; |
| 1097 | |
| 1098 | std::vector<audio_mixer_attributes_t> mixerAttributes; |
| 1099 | EXPECT_EQ(NO_ERROR, mManager->getSupportedMixerAttributes(usbPortId, mixerAttributes)); |
| 1100 | EXPECT_GT(mixerAttributes.size(), 0); |
| 1101 | EXPECT_EQ(NO_ERROR, |
| 1102 | mManager->setPreferredMixerAttributes( |
| 1103 | &mediaAttr, usbPortId, uid, &mixerAttributes[0])); |
| 1104 | |
| 1105 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 1106 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1107 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 1108 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1109 | 48000, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, |
| 1110 | AUDIO_SESSION_NONE, uid); |
| 1111 | status_t status = mManager->startOutput(portId); |
| 1112 | if (status == DEAD_OBJECT) { |
| 1113 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1114 | 48000, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, |
| 1115 | AUDIO_SESSION_NONE, uid); |
| 1116 | status = mManager->startOutput(portId); |
| 1117 | } |
| 1118 | EXPECT_EQ(NO_ERROR, status); |
| 1119 | EXPECT_NE(AUDIO_IO_HANDLE_NONE, output); |
| 1120 | EXPECT_NE(nullptr, mManager->getOutputs().valueFor(output)); |
| 1121 | EXPECT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 1122 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1123 | "", "", AUDIO_FORMAT_LDAC)); |
| 1124 | // When BT device is connected, it will be selected as media device and trigger routing changed. |
| 1125 | // When this happens, existing output that is opened with preferred mixer attributes will be |
| 1126 | // closed and reopened with default config. |
| 1127 | EXPECT_EQ(nullptr, mManager->getOutputs().valueFor(output)); |
| 1128 | |
| 1129 | EXPECT_EQ(NO_ERROR, |
| 1130 | mManager->clearPreferredMixerAttributes(&mediaAttr, usbPortId, uid)); |
| 1131 | |
| 1132 | EXPECT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 1133 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1134 | "", "", AUDIO_FORMAT_LDAC)); |
| 1135 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1136 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1137 | "", "", AUDIO_FORMAT_LDAC)); |
| 1138 | } |
| 1139 | |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1140 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, BitPerfectPlayback) { |
| 1141 | const audio_format_t bitPerfectFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 1142 | const audio_channel_mask_t bitPerfectChannelMask = AUDIO_CHANNEL_OUT_QUAD; |
| 1143 | const uint32_t bitPerfectSampleRate = 48000; |
| 1144 | mClient->addSupportedFormat(bitPerfectFormat); |
| 1145 | mClient->addSupportedChannelMask(bitPerfectChannelMask); |
| 1146 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1147 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1148 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1149 | auto devices = mManager->getAvailableOutputDevices(); |
| 1150 | audio_port_handle_t usbPortId = AUDIO_PORT_HANDLE_NONE; |
| 1151 | for (auto device : devices) { |
| 1152 | if (device->type() == AUDIO_DEVICE_OUT_USB_DEVICE) { |
| 1153 | usbPortId = device->getId(); |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | EXPECT_NE(AUDIO_PORT_HANDLE_NONE, usbPortId); |
| 1158 | |
| 1159 | const uid_t uid = 1234; |
| 1160 | const uid_t anotherUid = 5678; |
| 1161 | const audio_attributes_t mediaAttr = { |
| 1162 | .content_type = AUDIO_CONTENT_TYPE_MUSIC, |
| 1163 | .usage = AUDIO_USAGE_MEDIA, |
| 1164 | }; |
| 1165 | |
| 1166 | std::vector<audio_mixer_attributes_t> mixerAttributes; |
| 1167 | EXPECT_EQ(NO_ERROR, mManager->getSupportedMixerAttributes(usbPortId, mixerAttributes)); |
| 1168 | EXPECT_GT(mixerAttributes.size(), 0); |
| 1169 | size_t bitPerfectIndex = 0; |
| 1170 | for (; bitPerfectIndex < mixerAttributes.size(); ++bitPerfectIndex) { |
| 1171 | if (mixerAttributes[bitPerfectIndex].mixer_behavior == AUDIO_MIXER_BEHAVIOR_BIT_PERFECT) { |
| 1172 | break; |
| 1173 | } |
| 1174 | } |
| 1175 | EXPECT_LT(bitPerfectIndex, mixerAttributes.size()); |
| 1176 | EXPECT_EQ(bitPerfectFormat, mixerAttributes[bitPerfectIndex].config.format); |
| 1177 | EXPECT_EQ(bitPerfectChannelMask, mixerAttributes[bitPerfectIndex].config.channel_mask); |
| 1178 | EXPECT_EQ(bitPerfectSampleRate, mixerAttributes[bitPerfectIndex].config.sample_rate); |
| 1179 | EXPECT_EQ(NO_ERROR, |
| 1180 | mManager->setPreferredMixerAttributes( |
| 1181 | &mediaAttr, usbPortId, uid, &mixerAttributes[bitPerfectIndex])); |
| 1182 | |
| 1183 | audio_io_handle_t bitPerfectOutput = AUDIO_IO_HANDLE_NONE; |
| 1184 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 1185 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1186 | audio_port_handle_t bitPerfectPortId = AUDIO_PORT_HANDLE_NONE; |
| 1187 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 1188 | bool isBitPerfect; |
| 1189 | |
| 1190 | // When there is no active bit-perfect playback, the output selection will follow default |
| 1191 | // routing strategy. |
| 1192 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1193 | 48000, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, AUDIO_SESSION_NONE, |
| 1194 | uid, &isBitPerfect); |
| 1195 | EXPECT_FALSE(isBitPerfect); |
| 1196 | EXPECT_NE(AUDIO_IO_HANDLE_NONE, output); |
| 1197 | const auto outputDesc = mManager->getOutputs().valueFor(output); |
| 1198 | EXPECT_NE(nullptr, outputDesc); |
| 1199 | EXPECT_NE(AUDIO_OUTPUT_FLAG_BIT_PERFECT, outputDesc->mFlags & AUDIO_OUTPUT_FLAG_BIT_PERFECT); |
| 1200 | |
| 1201 | // Start bit-perfect playback |
| 1202 | getOutputForAttr(&selectedDeviceId, bitPerfectFormat, bitPerfectChannelMask, |
| 1203 | bitPerfectSampleRate, AUDIO_OUTPUT_FLAG_NONE, &bitPerfectOutput, &bitPerfectPortId, |
| 1204 | mediaAttr, AUDIO_SESSION_NONE, uid, &isBitPerfect); |
| 1205 | status_t status = mManager->startOutput(bitPerfectPortId); |
| 1206 | if (status == DEAD_OBJECT) { |
| 1207 | getOutputForAttr(&selectedDeviceId, bitPerfectFormat, bitPerfectChannelMask, |
| 1208 | bitPerfectSampleRate, AUDIO_OUTPUT_FLAG_NONE, &bitPerfectOutput, &bitPerfectPortId, |
| 1209 | mediaAttr, AUDIO_SESSION_NONE, uid, &isBitPerfect); |
| 1210 | status = mManager->startOutput(bitPerfectPortId); |
| 1211 | } |
| 1212 | EXPECT_EQ(NO_ERROR, status); |
| 1213 | EXPECT_TRUE(isBitPerfect); |
| 1214 | EXPECT_NE(AUDIO_IO_HANDLE_NONE, bitPerfectOutput); |
| 1215 | const auto bitPerfectOutputDesc = mManager->getOutputs().valueFor(bitPerfectOutput); |
| 1216 | EXPECT_NE(nullptr, bitPerfectOutputDesc); |
| 1217 | EXPECT_EQ(AUDIO_OUTPUT_FLAG_BIT_PERFECT, |
| 1218 | bitPerfectOutputDesc->mFlags & AUDIO_OUTPUT_FLAG_BIT_PERFECT); |
| 1219 | |
| 1220 | // If the playback is from preferred mixer attributes owner but the request doesn't match |
| 1221 | // preferred mixer attributes, it will not be bit-perfect. |
| 1222 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1223 | 48000, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, AUDIO_SESSION_NONE, |
| 1224 | uid, &isBitPerfect); |
| 1225 | EXPECT_FALSE(isBitPerfect); |
| 1226 | EXPECT_EQ(bitPerfectOutput, output); |
| 1227 | |
| 1228 | // When bit-perfect playback is active, all other playback will be routed to bit-perfect output. |
| 1229 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1230 | 48000, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, AUDIO_SESSION_NONE, |
| 1231 | anotherUid, &isBitPerfect); |
| 1232 | EXPECT_FALSE(isBitPerfect); |
| 1233 | EXPECT_EQ(bitPerfectOutput, output); |
| 1234 | |
| 1235 | // When configuration matches preferred mixer attributes, which is bit-perfect, but the client |
| 1236 | // is not the owner of preferred mixer attributes, the playback will not be bit-perfect. |
| 1237 | getOutputForAttr(&selectedDeviceId, bitPerfectFormat, bitPerfectChannelMask, |
| 1238 | bitPerfectSampleRate, AUDIO_OUTPUT_FLAG_NONE, &output, &portId, mediaAttr, |
| 1239 | AUDIO_SESSION_NONE, anotherUid, &isBitPerfect); |
| 1240 | EXPECT_FALSE(isBitPerfect); |
| 1241 | EXPECT_EQ(bitPerfectOutput, output); |
| 1242 | |
| 1243 | EXPECT_EQ(NO_ERROR, |
| 1244 | mManager->clearPreferredMixerAttributes(&mediaAttr, usbPortId, uid)); |
| 1245 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState(AUDIO_DEVICE_OUT_USB_DEVICE, |
| 1246 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1247 | "", "", AUDIO_FORMAT_LDAC)); |
| 1248 | } |
| 1249 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1250 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1251 | protected: |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1252 | void TearDown() override; |
| 1253 | |
| 1254 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 1255 | std::string mixAddress, const audio_config_t& audioConfig, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1256 | const std::vector<AudioMixMatchCriterion>& matchCriteria); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1257 | void clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1258 | |
| 1259 | Vector<AudioMix> mAudioMixes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1260 | const std::string mMixAddress = "remote_submix_media"; |
| 1261 | }; |
| 1262 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1263 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 1264 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1265 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 1269 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1270 | const std::vector<AudioMixMatchCriterion>& matchCriteria = {}) { |
| 1271 | AudioMix myAudioMix(matchCriteria, mixType, audioConfig, mixFlag, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1272 | String8(mixAddress.c_str()), 0); |
| 1273 | myAudioMix.mDeviceType = deviceType; |
| 1274 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 1275 | mAudioMixes.clear(); |
| 1276 | mAudioMixes.add(myAudioMix); |
| 1277 | |
| 1278 | // As the policy mixes registration may fail at some case, |
| 1279 | // caller need to check the returned status. |
| 1280 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 1285 | if (mManager != nullptr) { |
| 1286 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 1287 | } |
| 1288 | mAudioMixes.clear(); |
| 1289 | } |
| 1290 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1291 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1292 | // SetUp must finish with no assertions |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 1296 | dumpToLog(); |
| 1297 | } |
| 1298 | |
| 1299 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 1300 | status_t ret; |
| 1301 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1302 | |
| 1303 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 1304 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1305 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1306 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1307 | |
| 1308 | // Fail due to the device is already connected. |
| 1309 | clearPolicyMix(); |
| 1310 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1311 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1312 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1313 | |
| 1314 | // The first time to register policy mixes with valid parameter should succeed. |
| 1315 | clearPolicyMix(); |
| 1316 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1317 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1318 | audioConfig.sample_rate = k48000SamplingRate; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1319 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1320 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1321 | ASSERT_EQ(NO_ERROR, ret); |
| 1322 | // Registering the same policy mixes should fail. |
| 1323 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 1324 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1325 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1326 | // Registration should fail due to device not found. |
| 1327 | // Note that earpiece is not present in the test configuration file. |
| 1328 | // 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] | 1329 | clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1330 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1331 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1332 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1333 | |
| 1334 | // Registration should fail due to output not found. |
| 1335 | clearPolicyMix(); |
| 1336 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1337 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1338 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1339 | |
Jan Sebechlebsky | cd33d8d | 2023-06-07 10:45:50 +0200 | [diff] [blame^] | 1340 | // The first time to register valid loopback policy mix should succeed. |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1341 | clearPolicyMix(); |
Jan Sebechlebsky | cd33d8d | 2023-06-07 10:45:50 +0200 | [diff] [blame^] | 1342 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1343 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "addr", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1344 | ASSERT_EQ(NO_ERROR, ret); |
Jan Sebechlebsky | cd33d8d | 2023-06-07 10:45:50 +0200 | [diff] [blame^] | 1345 | // Registering the render policy for the loopback address should succeed. |
| 1346 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 1347 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "addr", audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1348 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1349 | } |
| 1350 | |
| 1351 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 1352 | status_t ret; |
| 1353 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1354 | |
| 1355 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1356 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1357 | audioConfig.sample_rate = k48000SamplingRate; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1358 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1359 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1360 | ASSERT_EQ(NO_ERROR, ret); |
| 1361 | |
| 1362 | // After successfully registering policy mixes, it should be able to unregister. |
| 1363 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 1364 | ASSERT_EQ(NO_ERROR, ret); |
| 1365 | |
| 1366 | // After unregistering policy mixes successfully, it should fail unregistering |
| 1367 | // the same policy mixes as they are not registered. |
| 1368 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 1369 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1370 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1371 | |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1372 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyWithConsistentMixSucceeds) { |
| 1373 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1374 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1375 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1376 | audioConfig.sample_rate = k48000SamplingRate; |
| 1377 | |
| 1378 | std::vector<AudioMixMatchCriterion> mixMatchCriteria = { |
| 1379 | createUidCriterion(/*uid=*/42), |
| 1380 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/true)}; |
| 1381 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1382 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 1383 | mixMatchCriteria); |
| 1384 | ASSERT_EQ(NO_ERROR, ret); |
| 1385 | } |
| 1386 | |
| 1387 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyWithInconsistentMixFails) { |
| 1388 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1389 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1390 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1391 | audioConfig.sample_rate = k48000SamplingRate; |
| 1392 | |
| 1393 | std::vector<AudioMixMatchCriterion> mixMatchCriteria = { |
| 1394 | createUidCriterion(/*uid=*/42), |
| 1395 | createUidCriterion(/*uid=*/1235, /*exclude=*/true), |
| 1396 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/true)}; |
| 1397 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1398 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 1399 | mixMatchCriteria); |
| 1400 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1401 | } |
| 1402 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1403 | class AudioPolicyManagerTestForHdmi |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1404 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 1405 | public testing::WithParamInterface<audio_format_t> { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1406 | protected: |
| 1407 | void SetUp() override; |
| 1408 | std::string getConfigFile() override { return sTvConfig; } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1409 | std::map<audio_format_t, bool> getSurroundFormatsHelper(); |
| 1410 | std::vector<audio_format_t> getReportedSurroundFormatsHelper(); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1411 | std::unordered_set<audio_format_t> getFormatsFromPorts(); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1412 | void TearDown() override; |
| 1413 | |
| 1414 | static const std::string sTvConfig; |
| 1415 | |
| 1416 | }; |
| 1417 | |
| 1418 | const std::string AudioPolicyManagerTestForHdmi::sTvConfig = |
| 1419 | AudioPolicyManagerTestForHdmi::sExecutableDir + |
| 1420 | "test_settop_box_surround_configuration.xml"; |
| 1421 | |
| 1422 | void AudioPolicyManagerTestForHdmi::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1423 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTest::SetUp()); |
Mikhail Naganov | 83caee0 | 2021-10-05 15:52:01 -0700 | [diff] [blame] | 1424 | mClient->addSupportedFormat(AUDIO_FORMAT_AC3); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1425 | mClient->addSupportedFormat(AUDIO_FORMAT_E_AC3); |
| 1426 | mManager->setDeviceConnectionState( |
| 1427 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1428 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 1429 | } |
| 1430 | |
| 1431 | void AudioPolicyManagerTestForHdmi::TearDown() { |
| 1432 | mManager->setDeviceConnectionState( |
| 1433 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1434 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 1435 | AudioPolicyManagerTest::TearDown(); |
| 1436 | } |
| 1437 | |
| 1438 | std::map<audio_format_t, bool> |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1439 | AudioPolicyManagerTestForHdmi::getSurroundFormatsHelper() { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1440 | unsigned int numSurroundFormats = 0; |
| 1441 | std::map<audio_format_t, bool> surroundFormatsMap; |
| 1442 | status_t ret = mManager->getSurroundFormats( |
| 1443 | &numSurroundFormats, nullptr /* surroundFormats */, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1444 | nullptr /* surroundFormatsEnabled */); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1445 | EXPECT_EQ(NO_ERROR, ret); |
| 1446 | if (ret != NO_ERROR) { |
| 1447 | return surroundFormatsMap; |
| 1448 | } |
| 1449 | audio_format_t surroundFormats[numSurroundFormats]; |
| 1450 | memset(surroundFormats, 0, sizeof(audio_format_t) * numSurroundFormats); |
| 1451 | bool surroundFormatsEnabled[numSurroundFormats]; |
| 1452 | memset(surroundFormatsEnabled, 0, sizeof(bool) * numSurroundFormats); |
| 1453 | ret = mManager->getSurroundFormats( |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1454 | &numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1455 | EXPECT_EQ(NO_ERROR, ret); |
| 1456 | if (ret != NO_ERROR) { |
| 1457 | return surroundFormatsMap; |
| 1458 | } |
| 1459 | for (int i = 0; i< numSurroundFormats; i++) { |
| 1460 | surroundFormatsMap[surroundFormats[i]] = surroundFormatsEnabled[i]; |
| 1461 | } |
| 1462 | return surroundFormatsMap; |
| 1463 | } |
| 1464 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1465 | std::vector<audio_format_t> AudioPolicyManagerTestForHdmi::getReportedSurroundFormatsHelper() { |
| 1466 | unsigned int numSurroundFormats = 0; |
| 1467 | std::vector<audio_format_t> surroundFormatsVector; |
| 1468 | status_t ret = mManager->getReportedSurroundFormats( |
| 1469 | &numSurroundFormats, nullptr /* surroundFormats */); |
| 1470 | EXPECT_EQ(NO_ERROR, ret); |
| 1471 | if (ret != NO_ERROR) { |
| 1472 | return surroundFormatsVector; |
| 1473 | } |
| 1474 | audio_format_t surroundFormats[numSurroundFormats]; |
| 1475 | memset(surroundFormats, 0, sizeof(audio_format_t) * numSurroundFormats); |
| 1476 | ret = mManager->getReportedSurroundFormats(&numSurroundFormats, surroundFormats); |
| 1477 | EXPECT_EQ(NO_ERROR, ret); |
| 1478 | if (ret != NO_ERROR) { |
| 1479 | return surroundFormatsVector; |
| 1480 | } |
| 1481 | for (const auto &surroundFormat : surroundFormats) { |
| 1482 | surroundFormatsVector.push_back(surroundFormat); |
| 1483 | } |
| 1484 | return surroundFormatsVector; |
| 1485 | } |
| 1486 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1487 | std::unordered_set<audio_format_t> |
| 1488 | AudioPolicyManagerTestForHdmi::getFormatsFromPorts() { |
| 1489 | uint32_t numPorts = 0; |
| 1490 | uint32_t generation1; |
| 1491 | status_t ret; |
| 1492 | std::unordered_set<audio_format_t> formats; |
| 1493 | ret = mManager->listAudioPorts( |
| 1494 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 1495 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 1496 | if (ret != NO_ERROR) { |
| 1497 | return formats; |
| 1498 | } |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1499 | struct audio_port_v7 ports[numPorts]; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1500 | ret = mManager->listAudioPorts( |
| 1501 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation1); |
| 1502 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 1503 | if (ret != NO_ERROR) { |
| 1504 | return formats; |
| 1505 | } |
| 1506 | for (const auto &port : ports) { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1507 | for (size_t i = 0; i < port.num_audio_profiles; ++i) { |
| 1508 | formats.insert(port.audio_profiles[i].format); |
| 1509 | } |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1510 | } |
| 1511 | return formats; |
| 1512 | } |
| 1513 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1514 | TEST_P(AudioPolicyManagerTestForHdmi, GetSurroundFormatsReturnsSupportedFormats) { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1515 | mManager->setForceUse( |
| 1516 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1517 | auto surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1518 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1521 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1522 | GetSurroundFormatsReturnsManipulatedFormats) { |
| 1523 | mManager->setForceUse( |
| 1524 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 1525 | |
| 1526 | status_t ret = |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1527 | mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1528 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1529 | auto surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1530 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 1531 | ASSERT_FALSE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1532 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1533 | ret = mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1534 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1535 | surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1536 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 1537 | ASSERT_TRUE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1538 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1539 | ret = mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1540 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1541 | surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1542 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 1543 | ASSERT_FALSE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1544 | } |
| 1545 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1546 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1547 | ListAudioPortsReturnManipulatedHdmiFormats) { |
| 1548 | mManager->setForceUse( |
| 1549 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 1550 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1551 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/)); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1552 | auto formats = getFormatsFromPorts(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1553 | ASSERT_EQ(0, formats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1554 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1555 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/)); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1556 | formats = getFormatsFromPorts(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1557 | ASSERT_EQ(1, formats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1560 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1561 | GetReportedSurroundFormatsReturnsHdmiReportedFormats) { |
| 1562 | mManager->setForceUse( |
| 1563 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1564 | auto surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1565 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1568 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1569 | GetReportedSurroundFormatsReturnsNonManipulatedHdmiReportedFormats) { |
| 1570 | mManager->setForceUse( |
| 1571 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 1572 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1573 | status_t ret = mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1574 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1575 | auto surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1576 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1577 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1578 | ret = mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1579 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1580 | surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1581 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1582 | } |
| 1583 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1584 | TEST_P(AudioPolicyManagerTestForHdmi, GetSurroundFormatsIgnoresSupportedFormats) { |
| 1585 | mManager->setForceUse( |
| 1586 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER); |
| 1587 | auto surroundFormats = getSurroundFormatsHelper(); |
| 1588 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 1589 | ASSERT_FALSE(surroundFormats[GetParam()]); |
| 1590 | } |
| 1591 | |
| 1592 | INSTANTIATE_TEST_SUITE_P(SurroundFormatSupport, AudioPolicyManagerTestForHdmi, |
| 1593 | testing::Values(AUDIO_FORMAT_AC3, AUDIO_FORMAT_E_AC3), |
| 1594 | [](const ::testing::TestParamInfo<AudioPolicyManagerTestForHdmi::ParamType>& info) { |
| 1595 | return audio_format_to_string(info.param); |
| 1596 | }); |
| 1597 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1598 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 1599 | protected: |
| 1600 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 1601 | |
| 1602 | static const std::string sPrimaryOnlyConfig; |
| 1603 | }; |
| 1604 | |
| 1605 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 1606 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 1607 | |
| 1608 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 1609 | // SetUp must finish with no assertions. |
| 1610 | } |
| 1611 | |
| 1612 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 1613 | dumpToLog(); |
| 1614 | } |
| 1615 | |
| 1616 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 1617 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 1618 | status_t ret; |
| 1619 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1620 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1621 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1622 | audioConfig.sample_rate = k48000SamplingRate; |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1623 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 0e7b2f1 | 2022-08-18 14:40:37 +0200 | [diff] [blame] | 1624 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1625 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1626 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1627 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 1628 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1629 | } |
| 1630 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1631 | struct DPTestParam { |
| 1632 | DPTestParam(const std::vector<AudioMixMatchCriterion>& mixCriteria, |
| 1633 | bool expected_match = false) |
| 1634 | : mixCriteria(mixCriteria), attributes(defaultAttr), session(AUDIO_SESSION_NONE), |
| 1635 | expected_match(expected_match) {} |
| 1636 | |
| 1637 | DPTestParam& withUsage(audio_usage_t usage) { |
| 1638 | attributes.usage = usage; |
| 1639 | return *this; |
| 1640 | } |
| 1641 | |
| 1642 | DPTestParam& withTags(const char *tags) { |
| 1643 | std::strncpy(attributes.tags, tags, sizeof(attributes.tags)); |
| 1644 | return *this; |
| 1645 | } |
| 1646 | |
| 1647 | DPTestParam& withSource(audio_source_t source) { |
| 1648 | attributes.source = source; |
| 1649 | return *this; |
| 1650 | } |
| 1651 | |
| 1652 | DPTestParam& withSessionId(audio_session_t sessionId) { |
| 1653 | session = sessionId; |
| 1654 | return *this; |
| 1655 | } |
| 1656 | |
| 1657 | std::vector<AudioMixMatchCriterion> mixCriteria; |
| 1658 | audio_attributes_t attributes; |
| 1659 | audio_session_t session; |
| 1660 | bool expected_match; |
| 1661 | }; |
| 1662 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1663 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1664 | public testing::WithParamInterface<DPTestParam> { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1665 | protected: |
| 1666 | void SetUp() override; |
| 1667 | void TearDown() override; |
| 1668 | |
| 1669 | std::unique_ptr<RecordingActivityTracker> mTracker; |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1670 | struct audio_port_v7 mInjectionPort; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1671 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 1672 | }; |
| 1673 | |
| 1674 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1675 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestDynamicPolicy::SetUp()); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1676 | |
| 1677 | mTracker.reset(new RecordingActivityTracker()); |
| 1678 | |
| 1679 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1680 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1681 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1682 | audioConfig.sample_rate = k48000SamplingRate; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1683 | |
| 1684 | DPTestParam param = GetParam(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1685 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1686 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, param.mixCriteria); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1687 | ASSERT_EQ(NO_ERROR, ret); |
| 1688 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1689 | struct audio_port_v7 extractionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1690 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1691 | mMixAddress, &extractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1692 | |
| 1693 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1694 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1695 | audio_attributes_t attr = { |
| 1696 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1697 | std::string tags = "addr=" + mMixAddress; |
| 1698 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1699 | getInputForAttr(attr, param.session, mTracker->getRiid(), &selectedDeviceId, |
| 1700 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, k48000SamplingRate, |
| 1701 | AUDIO_INPUT_FLAG_NONE, &mPortId); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1702 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 1703 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 1704 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1705 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1706 | mMixAddress, &mInjectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 1710 | mManager->stopInput(mPortId); |
| 1711 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 1712 | } |
| 1713 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1714 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1715 | const DPTestParam param = GetParam(); |
| 1716 | const audio_attributes_t& attr = param.attributes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1717 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1718 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1719 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1720 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, nullptr /*portId*/, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1721 | attr, param.session); |
| 1722 | if (param.expected_match) { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1723 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 1724 | } else { |
| 1725 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 1726 | } |
| 1727 | } |
| 1728 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1729 | const std::vector<AudioMixMatchCriterion> USAGE_MEDIA_ALARM_CRITERIA = { |
| 1730 | createUsageCriterion(AUDIO_USAGE_MEDIA), |
| 1731 | createUsageCriterion(AUDIO_USAGE_ALARM) |
| 1732 | }; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1733 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1734 | INSTANTIATE_TEST_SUITE_P( |
| 1735 | PlaybackReroutingUsageMatch, |
| 1736 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1737 | testing::Values( |
| 1738 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1739 | .withUsage(AUDIO_USAGE_MEDIA), |
| 1740 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1741 | .withUsage(AUDIO_USAGE_MEDIA).withTags("addr=other"), |
| 1742 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1743 | .withUsage(AUDIO_USAGE_ALARM), |
| 1744 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1745 | .withUsage(AUDIO_USAGE_VOICE_COMMUNICATION), |
| 1746 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1747 | .withUsage(AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING), |
| 1748 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1749 | .withUsage(AUDIO_USAGE_NOTIFICATION), |
| 1750 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1751 | .withUsage(AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE), |
| 1752 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1753 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST), |
| 1754 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1755 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT), |
| 1756 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1757 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED), |
| 1758 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1759 | .withUsage(AUDIO_USAGE_NOTIFICATION_EVENT), |
| 1760 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1761 | .withUsage(AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY), |
| 1762 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1763 | .withUsage(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE), |
| 1764 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1765 | .withUsage(AUDIO_USAGE_ASSISTANCE_SONIFICATION), |
| 1766 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1767 | .withUsage(AUDIO_USAGE_GAME), |
| 1768 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ false) |
| 1769 | .withUsage(AUDIO_USAGE_ASSISTANT))); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1770 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1771 | INSTANTIATE_TEST_SUITE_P( |
| 1772 | PlaybackReroutingAddressPriorityMatch, |
| 1773 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1774 | testing::Values( |
| 1775 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1776 | .withUsage(AUDIO_USAGE_MEDIA).withTags("addr=remote_submix_media"), |
| 1777 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1778 | .withUsage(AUDIO_USAGE_VOICE_COMMUNICATION).withTags("addr=remote_submix_media"), |
| 1779 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1780 | .withUsage(AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING) |
| 1781 | .withTags("addr=remote_submix_media"), |
| 1782 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1783 | .withUsage(AUDIO_USAGE_ALARM) |
| 1784 | .withTags("addr=remote_submix_media"), |
| 1785 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1786 | .withUsage(AUDIO_USAGE_NOTIFICATION) |
| 1787 | .withTags("addr=remote_submix_media"), |
| 1788 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1789 | .withUsage(AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE) |
| 1790 | .withTags("addr=remote_submix_media"), |
| 1791 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1792 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST) |
| 1793 | .withTags("addr=remote_submix_media"), |
| 1794 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1795 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT) |
| 1796 | .withTags("addr=remote_submix_media"), |
| 1797 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1798 | .withUsage(AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED) |
| 1799 | .withTags("addr=remote_submix_media"), |
| 1800 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1801 | .withUsage(AUDIO_USAGE_NOTIFICATION_EVENT) |
| 1802 | .withTags("addr=remote_submix_media"), |
| 1803 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1804 | .withUsage(AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) |
| 1805 | .withTags("addr=remote_submix_media"), |
| 1806 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1807 | .withUsage(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE) |
| 1808 | .withTags("addr=remote_submix_media"), |
| 1809 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1810 | .withUsage(AUDIO_USAGE_ASSISTANCE_SONIFICATION) |
| 1811 | .withTags("addr=remote_submix_media"), |
| 1812 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1813 | .withUsage(AUDIO_USAGE_GAME) |
| 1814 | .withTags("addr=remote_submix_media"), |
| 1815 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1816 | .withUsage(AUDIO_USAGE_VIRTUAL_SOURCE) |
| 1817 | .withTags("addr=remote_submix_media"), |
| 1818 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1819 | .withUsage(AUDIO_USAGE_ASSISTANT) |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 1820 | .withTags("addr=remote_submix_media"), |
| 1821 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1822 | .withUsage(AUDIO_USAGE_ASSISTANT) |
| 1823 | .withTags("sometag;addr=remote_submix_media;othertag=somevalue"), |
| 1824 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1825 | .withUsage(AUDIO_USAGE_ASSISTANT) |
| 1826 | .withTags("addr=remote_submix_media;othertag"), |
| 1827 | DPTestParam(USAGE_MEDIA_ALARM_CRITERIA, /*expected_match=*/ true) |
| 1828 | .withUsage(AUDIO_USAGE_ASSISTANT) |
| 1829 | .withTags("sometag;othertag;addr=remote_submix_media"))); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1830 | |
| 1831 | static constexpr audio_session_t TEST_SESSION_ID = static_cast<audio_session_t>(42); |
| 1832 | static constexpr audio_session_t OTHER_SESSION_ID = static_cast<audio_session_t>(77); |
| 1833 | |
| 1834 | INSTANTIATE_TEST_SUITE_P( |
| 1835 | PlaybackReRoutingWithSessionId, |
| 1836 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1837 | testing::Values( |
| 1838 | // Mix is matched because the session id matches the one specified by the mix rule. |
| 1839 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 1840 | /*expected_match=*/ true) |
| 1841 | .withSessionId(TEST_SESSION_ID), |
| 1842 | // Mix is not matched because the session id doesn't match the one specified |
| 1843 | // by the mix rule. |
| 1844 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 1845 | /*expected_match=*/ false) |
| 1846 | .withSessionId(OTHER_SESSION_ID), |
| 1847 | // Mix is matched, the session id doesn't match the one specified by rule, |
| 1848 | // but there's address specified in the tags which takes precedence. |
| 1849 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 1850 | /*expected_match=*/ true) |
| 1851 | .withSessionId(OTHER_SESSION_ID).withTags("addr=remote_submix_media"), |
| 1852 | // Mix is matched, both the session id and the usage match ones specified by mix rule. |
| 1853 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID), |
| 1854 | createUsageCriterion(AUDIO_USAGE_MEDIA)}, |
| 1855 | /*expected_match=*/ true) |
| 1856 | .withSessionId(TEST_SESSION_ID).withUsage(AUDIO_USAGE_MEDIA), |
| 1857 | // Mix is not matched, the session id matches the one specified by mix rule, |
| 1858 | // but usage does not. |
| 1859 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID), |
| 1860 | createUsageCriterion(AUDIO_USAGE_MEDIA)}, |
| 1861 | /*expected_match=*/ false) |
| 1862 | .withSessionId(TEST_SESSION_ID).withUsage(AUDIO_USAGE_GAME), |
| 1863 | // Mix is not matched, the usage matches the one specified by mix rule, |
| 1864 | // but the session id is excluded. |
| 1865 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID, /*exclude=*/ true), |
| 1866 | createUsageCriterion(AUDIO_USAGE_MEDIA)}, |
| 1867 | /*expected_match=*/ false) |
| 1868 | .withSessionId(TEST_SESSION_ID).withUsage(AUDIO_USAGE_MEDIA))); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1869 | |
Jan Sebechlebsky | 8e10cea | 2023-03-29 11:43:31 +0200 | [diff] [blame] | 1870 | struct DPMmapTestParam { |
| 1871 | DPMmapTestParam(int mixRouteFlags, audio_devices_t deviceType, const std::string& deviceAddress) |
| 1872 | : mixRouteFlags(mixRouteFlags), deviceType(deviceType), deviceAddress(deviceAddress) {} |
| 1873 | |
| 1874 | int mixRouteFlags; |
| 1875 | audio_devices_t deviceType; |
| 1876 | std::string deviceAddress; |
| 1877 | }; |
| 1878 | |
| 1879 | class AudioPolicyManagerTestMMapPlaybackRerouting |
| 1880 | : public AudioPolicyManagerTestDynamicPolicy, |
| 1881 | public ::testing::WithParamInterface<DPMmapTestParam> { |
| 1882 | protected: |
| 1883 | void SetUp() override { |
| 1884 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 1885 | audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1886 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1887 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1888 | audioConfig.sample_rate = k48000SamplingRate; |
| 1889 | } |
| 1890 | |
| 1891 | audio_config_t audioConfig; |
| 1892 | audio_io_handle_t mOutput; |
| 1893 | audio_stream_type_t mStream = AUDIO_STREAM_DEFAULT; |
| 1894 | audio_port_handle_t mSelectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1895 | audio_port_handle_t mPortId; |
| 1896 | AudioPolicyInterface::output_type_t mOutputType; |
| 1897 | audio_attributes_t attr = AUDIO_ATTRIBUTES_INITIALIZER; |
| 1898 | bool mIsSpatialized; |
| 1899 | bool mIsBitPerfect; |
| 1900 | }; |
| 1901 | |
| 1902 | TEST_P(AudioPolicyManagerTestMMapPlaybackRerouting, MmapPlaybackStreamMatchingDapMixFails) { |
| 1903 | // Add mix matching the test uid. |
| 1904 | const int testUid = 12345; |
| 1905 | const auto param = GetParam(); |
| 1906 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, param.mixRouteFlags, param.deviceType, |
| 1907 | param.deviceAddress, audioConfig, {createUidCriterion(testUid)}); |
| 1908 | ASSERT_EQ(NO_ERROR, ret); |
| 1909 | |
| 1910 | // Geting output for matching uid and mmap-ed stream should fail. |
| 1911 | audio_output_flags_t outputFlags = AUDIO_OUTPUT_FLAG_MMAP_NOIRQ; |
| 1912 | ASSERT_EQ(INVALID_OPERATION, |
| 1913 | mManager->getOutputForAttr(&attr, &mOutput, AUDIO_SESSION_NONE, &mStream, |
| 1914 | createAttributionSourceState(testUid), &audioConfig, |
| 1915 | &outputFlags, &mSelectedDeviceId, &mPortId, {}, |
| 1916 | &mOutputType, &mIsSpatialized, &mIsBitPerfect)); |
| 1917 | } |
| 1918 | |
| 1919 | TEST_P(AudioPolicyManagerTestMMapPlaybackRerouting, NonMmapPlaybackStreamMatchingDapMixSucceeds) { |
| 1920 | // Add mix matching the test uid. |
| 1921 | const int testUid = 12345; |
| 1922 | const auto param = GetParam(); |
| 1923 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, param.mixRouteFlags, param.deviceType, |
| 1924 | param.deviceAddress, audioConfig, {createUidCriterion(testUid)}); |
| 1925 | ASSERT_EQ(NO_ERROR, ret); |
| 1926 | |
| 1927 | // Geting output for matching uid should succeed for non-mmaped stream. |
| 1928 | audio_output_flags_t outputFlags = AUDIO_OUTPUT_FLAG_NONE; |
| 1929 | ASSERT_EQ(NO_ERROR, |
| 1930 | mManager->getOutputForAttr(&attr, &mOutput, AUDIO_SESSION_NONE, &mStream, |
| 1931 | createAttributionSourceState(testUid), &audioConfig, |
| 1932 | &outputFlags, &mSelectedDeviceId, &mPortId, {}, |
| 1933 | &mOutputType, &mIsSpatialized, &mIsBitPerfect)); |
| 1934 | } |
| 1935 | |
| 1936 | INSTANTIATE_TEST_SUITE_P( |
| 1937 | MmapPlaybackRerouting, AudioPolicyManagerTestMMapPlaybackRerouting, |
| 1938 | testing::Values(DPMmapTestParam(MIX_ROUTE_FLAG_LOOP_BACK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1939 | /*deviceAddress=*/"remote_submix_media"), |
| 1940 | DPMmapTestParam(MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 1941 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1942 | /*deviceAddress=*/"remote_submix_media"), |
| 1943 | DPMmapTestParam(MIX_ROUTE_FLAG_RENDER, AUDIO_DEVICE_OUT_SPEAKER, |
| 1944 | /*deviceAddress=*/""))); |
| 1945 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1946 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1947 | public testing::WithParamInterface<DPTestParam> { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1948 | protected: |
| 1949 | void SetUp() override; |
| 1950 | void TearDown() override; |
| 1951 | |
| 1952 | std::unique_ptr<RecordingActivityTracker> mTracker; |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1953 | struct audio_port_v7 mExtractionPort; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1954 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 1955 | }; |
| 1956 | |
| 1957 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1958 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestDynamicPolicy::SetUp()); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1959 | |
| 1960 | mTracker.reset(new RecordingActivityTracker()); |
| 1961 | |
| 1962 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1963 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1964 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1965 | audioConfig.sample_rate = k48000SamplingRate; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1966 | |
| 1967 | DPTestParam param = GetParam(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1968 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1969 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, param.mixCriteria); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1970 | ASSERT_EQ(NO_ERROR, ret); |
| 1971 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1972 | struct audio_port_v7 injectionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1973 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1974 | mMixAddress, &injectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1975 | |
| 1976 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1977 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1978 | audio_attributes_t attr = |
| 1979 | {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1980 | std::string tags = std::string("addr=") + mMixAddress; |
| 1981 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 1982 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1983 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, &mPortId, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1984 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 1985 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 1986 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1987 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1988 | mMixAddress, &mExtractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 1992 | mManager->stopOutput(mPortId); |
| 1993 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 1994 | } |
| 1995 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1996 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1997 | const DPTestParam param = GetParam(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1998 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1999 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2000 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2001 | getInputForAttr(param.attributes, param.session, mTracker->getRiid(), &captureRoutedPortId, |
| 2002 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, k48000SamplingRate, |
| 2003 | AUDIO_INPUT_FLAG_NONE, &portId); |
| 2004 | if (param.expected_match) { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2005 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 2006 | } else { |
| 2007 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 2008 | } |
| 2009 | } |
| 2010 | |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2011 | const std::vector<AudioMixMatchCriterion> SOURCE_CAM_MIC_VOICE_CRITERIA = { |
| 2012 | createCapturePresetCriterion(AUDIO_SOURCE_CAMCORDER), |
| 2013 | createCapturePresetCriterion(AUDIO_SOURCE_MIC), |
| 2014 | createCapturePresetCriterion(AUDIO_SOURCE_VOICE_COMMUNICATION) |
| 2015 | }; |
| 2016 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2017 | // No address priority rule for remote recording, address is a "don't care" |
| 2018 | INSTANTIATE_TEST_CASE_P( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2019 | RecordInjectionSource, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2020 | AudioPolicyManagerTestDPMixRecordInjection, |
| 2021 | testing::Values( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2022 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2023 | .withSource(AUDIO_SOURCE_CAMCORDER), |
| 2024 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2025 | .withSource(AUDIO_SOURCE_CAMCORDER) |
| 2026 | .withTags("addr=remote_submix_media"), |
| 2027 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2028 | .withSource(AUDIO_SOURCE_MIC), |
| 2029 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2030 | .withSource(AUDIO_SOURCE_MIC) |
| 2031 | .withTags("addr=remote_submix_media"), |
| 2032 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2033 | .withSource(AUDIO_SOURCE_VOICE_COMMUNICATION), |
| 2034 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ true) |
| 2035 | .withSource(AUDIO_SOURCE_VOICE_COMMUNICATION) |
| 2036 | .withTags("addr=remote_submix_media"), |
| 2037 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ false) |
| 2038 | .withSource(AUDIO_SOURCE_VOICE_RECOGNITION), |
| 2039 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ false) |
| 2040 | .withSource(AUDIO_SOURCE_VOICE_RECOGNITION) |
| 2041 | .withTags("addr=remote_submix_media"), |
| 2042 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ false) |
| 2043 | .withSource(AUDIO_SOURCE_HOTWORD), |
| 2044 | DPTestParam(SOURCE_CAM_MIC_VOICE_CRITERIA, /*expected_match=*/ false) |
| 2045 | .withSource(AUDIO_SOURCE_HOTWORD) |
| 2046 | .withTags("addr=remote_submix_media"))); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2047 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2048 | INSTANTIATE_TEST_CASE_P( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2049 | RecordInjectionWithSessionId, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 2050 | AudioPolicyManagerTestDPMixRecordInjection, |
| 2051 | testing::Values( |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2052 | // Mix is matched because the session id matches the one specified by the mix rule. |
| 2053 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 2054 | /*expected_match=*/ true) |
| 2055 | .withSessionId(TEST_SESSION_ID), |
| 2056 | // Mix is not matched because the session id doesn't match the one specified |
| 2057 | // by the mix rule. |
| 2058 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 2059 | /*expected_match=*/ false) |
| 2060 | .withSessionId(OTHER_SESSION_ID), |
| 2061 | // Mix is not matched, the session id doesn't match the one specified by rule, |
| 2062 | // but tand address specified in the tags is ignored for recorder mix. |
| 2063 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID)}, |
| 2064 | /*expected_match=*/ false) |
| 2065 | .withSessionId(OTHER_SESSION_ID).withTags("addr=remote_submix_media"), |
| 2066 | // Mix is matched, both the session id and the source match ones specified by mix rule |
| 2067 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID), |
| 2068 | createCapturePresetCriterion(AUDIO_SOURCE_CAMCORDER)}, |
| 2069 | /*expected_match=*/ true) |
| 2070 | .withSessionId(TEST_SESSION_ID).withSource(AUDIO_SOURCE_CAMCORDER), |
| 2071 | // Mix is not matched, the session id matches the one specified by mix rule, |
| 2072 | // but source does not. |
| 2073 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID), |
| 2074 | createCapturePresetCriterion(AUDIO_SOURCE_CAMCORDER)}, |
| 2075 | /*expected_match=*/ false) |
| 2076 | .withSessionId(TEST_SESSION_ID).withSource(AUDIO_SOURCE_MIC), |
| 2077 | // Mix is not matched, the source matches the one specified by mix rule, |
| 2078 | // but the session id is excluded. |
| 2079 | DPTestParam(/*mixCriteria=*/ {createSessionIdCriterion(TEST_SESSION_ID, |
| 2080 | /*exclude=*/ true), |
| 2081 | createCapturePresetCriterion(AUDIO_SOURCE_MIC)}, |
| 2082 | /*expected_match=*/ false) |
| 2083 | .withSessionId(TEST_SESSION_ID).withSource(AUDIO_SOURCE_MIC))); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2084 | |
| 2085 | using DeviceConnectionTestParams = |
| 2086 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 2087 | |
| 2088 | class AudioPolicyManagerTestDeviceConnection : public AudioPolicyManagerTestWithConfigurationFile, |
| 2089 | public testing::WithParamInterface<DeviceConnectionTestParams> { |
| 2090 | }; |
| 2091 | |
| 2092 | TEST_F(AudioPolicyManagerTestDeviceConnection, InitSuccess) { |
| 2093 | // SetUp must finish with no assertions. |
| 2094 | } |
| 2095 | |
| 2096 | TEST_F(AudioPolicyManagerTestDeviceConnection, Dump) { |
| 2097 | dumpToLog(); |
| 2098 | } |
| 2099 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2100 | TEST_F(AudioPolicyManagerTestDeviceConnection, RoutingUpdate) { |
| 2101 | mClient->resetRoutingUpdatedCounter(); |
| 2102 | // Connecting a valid output device with valid parameters should trigger a routing update |
| 2103 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2104 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Mikhail Naganov | b1ddbb0 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 2105 | "00:11:22:33:44:55", "b", AUDIO_FORMAT_DEFAULT)); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2106 | ASSERT_EQ(1, mClient->getRoutingUpdatedCounter()); |
| 2107 | |
| 2108 | // Disconnecting a connected device should succeed and trigger a routing update |
| 2109 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2110 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | b1ddbb0 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 2111 | "00:11:22:33:44:55", "b", AUDIO_FORMAT_DEFAULT)); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2112 | ASSERT_EQ(2, mClient->getRoutingUpdatedCounter()); |
| 2113 | |
| 2114 | // Disconnecting a disconnected device should fail and not trigger a routing update |
| 2115 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 2116 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | b1ddbb0 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 2117 | "00:11:22:33:44:55", "b", AUDIO_FORMAT_DEFAULT)); |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 2118 | ASSERT_EQ(2, mClient->getRoutingUpdatedCounter()); |
| 2119 | |
| 2120 | // Changing force use should trigger an update |
| 2121 | auto config = mManager->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA); |
| 2122 | auto newConfig = config == AUDIO_POLICY_FORCE_BT_A2DP ? |
| 2123 | AUDIO_POLICY_FORCE_NONE : AUDIO_POLICY_FORCE_BT_A2DP; |
| 2124 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA, newConfig); |
| 2125 | ASSERT_EQ(3, mClient->getRoutingUpdatedCounter()); |
| 2126 | } |
| 2127 | |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2128 | TEST_P(AudioPolicyManagerTestDeviceConnection, SetDeviceConnectionState) { |
| 2129 | const audio_devices_t type = std::get<0>(GetParam()); |
| 2130 | const std::string name = std::get<1>(GetParam()); |
| 2131 | const std::string address = std::get<2>(GetParam()); |
| 2132 | |
| 2133 | if (type == AUDIO_DEVICE_OUT_HDMI) { |
| 2134 | // Set device connection state failed due to no device descriptor found |
| 2135 | // 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] | 2136 | // by using an encoded format which isn't listed in the 'encodedFormats' |
| 2137 | // attribute for this devicePort. |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2138 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 2139 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2140 | address.c_str(), name.c_str(), AUDIO_FORMAT_MAT_2_1)); |
| 2141 | } |
| 2142 | // Connect with valid parameters should succeed |
| 2143 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2144 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2145 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2146 | // Try to connect with the same device again should fail |
| 2147 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 2148 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2149 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2150 | // Disconnect the connected device should succeed |
| 2151 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2152 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2153 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2154 | // Disconnect device that is not connected should fail |
| 2155 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 2156 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2157 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2158 | // Try to set device connection state with a invalid connection state should fail |
| 2159 | ASSERT_EQ(BAD_VALUE, mManager->setDeviceConnectionState( |
| 2160 | type, AUDIO_POLICY_DEVICE_STATE_CNT, |
| 2161 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 2162 | } |
| 2163 | |
| 2164 | TEST_P(AudioPolicyManagerTestDeviceConnection, ExplicitlyRoutingAfterConnection) { |
| 2165 | const audio_devices_t type = std::get<0>(GetParam()); |
| 2166 | const std::string name = std::get<1>(GetParam()); |
| 2167 | const std::string address = std::get<2>(GetParam()); |
| 2168 | |
| 2169 | // Connect device to do explicitly routing test |
| 2170 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2171 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2172 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2173 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 2174 | audio_port_v7 devicePort; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2175 | const audio_port_role_t role = audio_is_output_device(type) |
| 2176 | ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 2177 | ASSERT_TRUE(findDevicePort(role, type, address, &devicePort)); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2178 | |
| 2179 | audio_port_handle_t routedPortId = devicePort.id; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2180 | // Try start input or output according to the device type |
| 2181 | if (audio_is_output_devices(type)) { |
| 2182 | getOutputForAttr(&routedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2183 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2184 | } else if (audio_is_input_device(type)) { |
| 2185 | RecordingActivityTracker tracker; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2186 | getInputForAttr({}, AUDIO_SESSION_NONE, tracker.getRiid(), &routedPortId, |
| 2187 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, k48000SamplingRate, |
| 2188 | AUDIO_INPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2189 | } |
| 2190 | ASSERT_EQ(devicePort.id, routedPortId); |
| 2191 | |
| 2192 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2193 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2194 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2195 | } |
| 2196 | |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 2197 | android::media::audio::common::ExtraAudioDescriptor make_ExtraAudioDescriptor( |
| 2198 | android::media::audio::common::AudioStandard audioStandard, |
| 2199 | android::media::audio::common::AudioEncapsulationType audioEncapsulationType) { |
| 2200 | android::media::audio::common::ExtraAudioDescriptor result; |
| 2201 | result.standard = audioStandard; |
| 2202 | result.audioDescriptor = {0xb4, 0xaf, 0x98, 0x1a}; |
| 2203 | result.encapsulationType = audioEncapsulationType; |
| 2204 | return result; |
| 2205 | } |
| 2206 | |
| 2207 | TEST_P(AudioPolicyManagerTestDeviceConnection, PassingExtraAudioDescriptors) { |
| 2208 | const audio_devices_t type = std::get<0>(GetParam()); |
| 2209 | if (!audio_device_is_digital(type)) { |
| 2210 | // EADs are used only for HDMI devices. |
| 2211 | GTEST_SKIP() << "Not a digital device type: " << audio_device_to_string(type); |
| 2212 | } |
| 2213 | const std::string name = std::get<1>(GetParam()); |
| 2214 | const std::string address = std::get<2>(GetParam()); |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 2215 | android::media::AudioPortFw audioPort; |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 2216 | ASSERT_EQ(NO_ERROR, |
| 2217 | mManager->deviceToAudioPort(type, address.c_str(), name.c_str(), &audioPort)); |
| 2218 | android::media::audio::common::AudioPort& port = audioPort.hal; |
| 2219 | port.extraAudioDescriptors.push_back(make_ExtraAudioDescriptor( |
| 2220 | android::media::audio::common::AudioStandard::EDID, |
| 2221 | android::media::audio::common::AudioEncapsulationType::IEC61937)); |
| 2222 | const size_t lastConnectedDevicePortCount = mClient->getConnectedDevicePortCount(); |
| 2223 | const size_t lastDisconnectedDevicePortCount = mClient->getDisconnectedDevicePortCount(); |
| 2224 | EXPECT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2225 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, port, AUDIO_FORMAT_DEFAULT)); |
| 2226 | EXPECT_EQ(lastConnectedDevicePortCount + 1, mClient->getConnectedDevicePortCount()); |
| 2227 | EXPECT_EQ(lastDisconnectedDevicePortCount, mClient->getDisconnectedDevicePortCount()); |
| 2228 | const audio_port_v7* devicePort = mClient->getLastConnectedDevicePort(); |
| 2229 | EXPECT_EQ(port.extraAudioDescriptors.size(), devicePort->num_extra_audio_descriptors); |
| 2230 | EXPECT_EQ(AUDIO_STANDARD_EDID, devicePort->extra_audio_descriptors[0].standard); |
| 2231 | EXPECT_EQ(AUDIO_ENCAPSULATION_TYPE_IEC61937, |
| 2232 | devicePort->extra_audio_descriptors[0].encapsulation_type); |
| 2233 | EXPECT_NE(0, devicePort->extra_audio_descriptors[0].descriptor[0]); |
| 2234 | } |
| 2235 | |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2236 | INSTANTIATE_TEST_CASE_P( |
| 2237 | DeviceConnectionState, |
| 2238 | AudioPolicyManagerTestDeviceConnection, |
| 2239 | testing::Values( |
| 2240 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_HDMI, "test_in_hdmi", |
| 2241 | "audio_policy_test_in_hdmi"}), |
| 2242 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_HDMI, "test_out_hdmi", |
| 2243 | "audio_policy_test_out_hdmi"}), |
| 2244 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "bt_hfp_in", |
Mikhail Naganov | b1ddbb0 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 2245 | "00:11:22:33:44:55"}), |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2246 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "bt_hfp_out", |
Mikhail Naganov | b1ddbb0 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 2247 | "00:11:22:33:44:55"}) |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 2248 | ) |
| 2249 | ); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2250 | |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2251 | class AudioPolicyManagerCarTest : public AudioPolicyManagerTestDynamicPolicy { |
| 2252 | protected: |
| 2253 | std::string getConfigFile() override { return sCarConfig; } |
| 2254 | |
| 2255 | static const std::string sCarConfig; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2256 | static const std::string sCarBusMediaOutput; |
| 2257 | static const std::string sCarBusNavigationOutput; |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2258 | static const std::string sCarRearZoneOneOutput; |
| 2259 | static const std::string sCarRearZoneTwoOutput; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2260 | }; |
| 2261 | |
| 2262 | const std::string AudioPolicyManagerCarTest::sCarConfig = |
| 2263 | AudioPolicyManagerCarTest::sExecutableDir + "test_car_ap_atmos_offload_configuration.xml"; |
| 2264 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2265 | const std::string AudioPolicyManagerCarTest::sCarBusMediaOutput = "bus0_media_out"; |
| 2266 | |
| 2267 | const std::string AudioPolicyManagerCarTest::sCarBusNavigationOutput = "bus1_navigation_out"; |
| 2268 | |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2269 | const std::string AudioPolicyManagerCarTest::sCarRearZoneOneOutput = "bus100_audio_zone_1"; |
| 2270 | |
| 2271 | const std::string AudioPolicyManagerCarTest::sCarRearZoneTwoOutput = "bus200_audio_zone_2"; |
| 2272 | |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2273 | TEST_F(AudioPolicyManagerCarTest, InitSuccess) { |
| 2274 | // SetUp must finish with no assertions. |
| 2275 | } |
| 2276 | |
| 2277 | TEST_F(AudioPolicyManagerCarTest, Dump) { |
| 2278 | dumpToLog(); |
| 2279 | } |
| 2280 | |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 2281 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrAtmosOutputAfterRegisteringPolicyMix) { |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2282 | status_t ret; |
| 2283 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2284 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2285 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig); |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2286 | ASSERT_EQ(NO_ERROR, ret); |
| 2287 | |
| 2288 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2289 | audio_io_handle_t output; |
| 2290 | audio_port_handle_t portId; |
| 2291 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_E_AC3_JOC, AUDIO_CHANNEL_OUT_5POINT1, |
| 2292 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId); |
| 2293 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, selectedDeviceId); |
| 2294 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 2295 | ASSERT_NE(nullptr, outDesc.get()); |
| 2296 | ASSERT_EQ(AUDIO_FORMAT_E_AC3_JOC, outDesc->getFormat()); |
| 2297 | ASSERT_EQ(AUDIO_CHANNEL_OUT_5POINT1, outDesc->getChannelMask()); |
| 2298 | ASSERT_EQ(k48000SamplingRate, outDesc->getSamplingRate()); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 2299 | |
| 2300 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2301 | output = AUDIO_IO_HANDLE_NONE; |
| 2302 | portId = AUDIO_PORT_HANDLE_NONE; |
| 2303 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_7POINT1POINT4, |
| 2304 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId); |
| 2305 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, selectedDeviceId); |
| 2306 | outDesc = mManager->getOutputs().valueFor(output); |
| 2307 | ASSERT_NE(nullptr, outDesc.get()); |
| 2308 | ASSERT_EQ(AUDIO_FORMAT_PCM_16_BIT, outDesc->getFormat()); |
| 2309 | ASSERT_EQ(AUDIO_CHANNEL_OUT_7POINT1POINT4, outDesc->getChannelMask()); |
| 2310 | ASSERT_EQ(k48000SamplingRate, outDesc->getSamplingRate()); |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2311 | } |
| 2312 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2313 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrAfterRegisteringPolicyMix) { |
| 2314 | status_t ret; |
| 2315 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2316 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2317 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2318 | audioConfig.sample_rate = k48000SamplingRate; |
| 2319 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2320 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2321 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2322 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2323 | ASSERT_EQ(NO_ERROR, ret); |
| 2324 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2325 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2326 | /*exclude=*/ false)}; |
| 2327 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2328 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2329 | ASSERT_EQ(NO_ERROR, ret); |
| 2330 | audio_port_v7 mediaDevicePort; |
| 2331 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2332 | sCarBusMediaOutput, &mediaDevicePort)); |
| 2333 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2334 | audio_io_handle_t output; |
| 2335 | audio_port_handle_t portId; |
| 2336 | const audio_attributes_t mediaAttribute = { |
| 2337 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2338 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2339 | |
| 2340 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2341 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute); |
| 2342 | |
| 2343 | ASSERT_EQ(mediaDevicePort.id, selectedDeviceId); |
| 2344 | } |
| 2345 | |
| 2346 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrWithSelectedOutputAfterRegisteringPolicyMix) { |
| 2347 | status_t ret; |
| 2348 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2349 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2350 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2351 | audioConfig.sample_rate = k48000SamplingRate; |
| 2352 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2353 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2354 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2355 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2356 | ASSERT_EQ(NO_ERROR, ret); |
| 2357 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2358 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2359 | /*exclude=*/ false)}; |
| 2360 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2361 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2362 | ASSERT_EQ(NO_ERROR, ret); |
| 2363 | audio_port_v7 navDevicePort; |
| 2364 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2365 | sCarBusNavigationOutput, &navDevicePort)); |
| 2366 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2367 | audio_io_handle_t output; |
| 2368 | audio_port_handle_t portId; |
| 2369 | const audio_attributes_t mediaAttribute = { |
| 2370 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2371 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2372 | |
| 2373 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2374 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute); |
| 2375 | |
| 2376 | ASSERT_EQ(navDevicePort.id, selectedDeviceId); |
| 2377 | } |
| 2378 | |
| 2379 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrWithSelectedOutputAfterUserAffinities) { |
| 2380 | status_t ret; |
| 2381 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2382 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2383 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2384 | audioConfig.sample_rate = k48000SamplingRate; |
| 2385 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2386 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2387 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2388 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2389 | ASSERT_EQ(NO_ERROR, ret); |
| 2390 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2391 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2392 | /*exclude=*/ false)}; |
| 2393 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2394 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2395 | ASSERT_EQ(NO_ERROR, ret); |
| 2396 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2397 | const AudioDeviceTypeAddrVector outputDevices = {mediaOutputDevice}; |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2398 | mManager->setUserIdDeviceAffinities(/* userId */ 0, outputDevices); |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2399 | audio_port_v7 navDevicePort; |
| 2400 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2401 | sCarBusNavigationOutput, &navDevicePort)); |
| 2402 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2403 | audio_io_handle_t output; |
| 2404 | audio_port_handle_t portId; |
| 2405 | const audio_attributes_t mediaAttribute = { |
| 2406 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2407 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2408 | |
| 2409 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2410 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute); |
| 2411 | |
| 2412 | ASSERT_NE(navDevicePort.id, selectedDeviceId); |
| 2413 | } |
| 2414 | |
| 2415 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrWithExcludeUserIdCriteria) { |
| 2416 | status_t ret; |
| 2417 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2418 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2419 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2420 | audioConfig.sample_rate = k48000SamplingRate; |
| 2421 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2422 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2423 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2424 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2425 | ASSERT_EQ(NO_ERROR, ret); |
| 2426 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2427 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2428 | /*exclude=*/ false), |
| 2429 | createUserIdCriterion(/* userId */ 0, /* exclude */ true)}; |
| 2430 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2431 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2432 | ASSERT_EQ(NO_ERROR, ret); |
| 2433 | audio_port_v7 navDevicePort; |
| 2434 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2435 | sCarBusNavigationOutput, &navDevicePort)); |
| 2436 | audio_io_handle_t output; |
| 2437 | audio_port_handle_t portId; |
| 2438 | const audio_attributes_t navigationAttribute = { |
| 2439 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2440 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2441 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2442 | |
| 2443 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2444 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, navigationAttribute); |
| 2445 | |
| 2446 | ASSERT_NE(navDevicePort.id, selectedDeviceId); |
| 2447 | } |
| 2448 | |
| 2449 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrWithSelectedOutputExcludeUserIdCriteria) { |
| 2450 | status_t ret; |
| 2451 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2452 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2453 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2454 | audioConfig.sample_rate = k48000SamplingRate; |
| 2455 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2456 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2457 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2458 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2459 | ASSERT_EQ(NO_ERROR, ret); |
| 2460 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2461 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2462 | /*exclude=*/ false), |
| 2463 | createUserIdCriterion(0 /* userId */, /* exclude */ true)}; |
| 2464 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2465 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2466 | ASSERT_EQ(NO_ERROR, ret); |
| 2467 | audio_port_v7 navDevicePort; |
| 2468 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2469 | sCarBusNavigationOutput, &navDevicePort)); |
| 2470 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2471 | audio_io_handle_t output; |
| 2472 | audio_port_handle_t portId; |
| 2473 | const audio_attributes_t mediaAttribute = { |
| 2474 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2475 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2476 | |
| 2477 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2478 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute); |
| 2479 | |
| 2480 | ASSERT_EQ(navDevicePort.id, selectedDeviceId); |
| 2481 | } |
| 2482 | |
| 2483 | TEST_F(AudioPolicyManagerCarTest, |
| 2484 | GetOutputForAttrWithMatchingMixAndSelectedOutputAfterUserAffinities) { |
| 2485 | status_t ret; |
| 2486 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2487 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2488 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2489 | audioConfig.sample_rate = k48000SamplingRate; |
| 2490 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2491 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2492 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2493 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2494 | ASSERT_EQ(NO_ERROR, ret); |
| 2495 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2496 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2497 | /*exclude=*/ false)}; |
| 2498 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2499 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2500 | ASSERT_EQ(NO_ERROR, ret); |
| 2501 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2502 | const AudioDeviceTypeAddr navOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput); |
| 2503 | const AudioDeviceTypeAddrVector outputDevices = {mediaOutputDevice, navOutputDevice}; |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2504 | mManager->setUserIdDeviceAffinities(/* userId */ 0, outputDevices); |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2505 | audio_port_v7 navDevicePort; |
| 2506 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2507 | sCarBusNavigationOutput, &navDevicePort)); |
| 2508 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2509 | audio_io_handle_t output; |
| 2510 | audio_port_handle_t portId; |
| 2511 | const audio_attributes_t mediaAttribute = { |
| 2512 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2513 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2514 | |
| 2515 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2516 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute); |
| 2517 | |
| 2518 | ASSERT_EQ(navDevicePort.id, selectedDeviceId); |
| 2519 | } |
| 2520 | |
| 2521 | TEST_F(AudioPolicyManagerCarTest, |
| 2522 | GetOutputForAttrWithNoMatchingMaxAndSelectedOutputAfterUserAffinities) { |
| 2523 | status_t ret; |
| 2524 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2525 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2526 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2527 | audioConfig.sample_rate = k48000SamplingRate; |
| 2528 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2529 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2530 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2531 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2532 | ASSERT_EQ(NO_ERROR, ret); |
| 2533 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2534 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2535 | /*exclude=*/ false)}; |
| 2536 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2537 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2538 | ASSERT_EQ(NO_ERROR, ret); |
| 2539 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2540 | const AudioDeviceTypeAddr navOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput); |
| 2541 | const AudioDeviceTypeAddrVector outputDevices = {mediaOutputDevice, navOutputDevice}; |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2542 | mManager->setUserIdDeviceAffinities(/* userId */ 0, outputDevices); |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2543 | audio_port_v7 navDevicePort; |
| 2544 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2545 | sCarBusNavigationOutput, &navDevicePort)); |
| 2546 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2547 | audio_io_handle_t output; |
| 2548 | audio_port_handle_t portId; |
| 2549 | const audio_attributes_t alarmAttribute = { |
| 2550 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_ALARM, |
| 2551 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2552 | |
| 2553 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2554 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, alarmAttribute); |
| 2555 | |
| 2556 | ASSERT_EQ(navDevicePort.id, selectedDeviceId); |
| 2557 | } |
| 2558 | |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2559 | TEST_F(AudioPolicyManagerCarTest, |
| 2560 | GetOutputForAttrWithMatMixAfterUserAffinitiesForOneUser) { |
| 2561 | status_t ret; |
| 2562 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2563 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2564 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2565 | audioConfig.sample_rate = k48000SamplingRate; |
| 2566 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2567 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2568 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2569 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2570 | ASSERT_EQ(NO_ERROR, ret); |
| 2571 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2572 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneOneOutput, audioConfig, mediaMatchCriteria); |
| 2573 | ASSERT_EQ(NO_ERROR, ret); |
| 2574 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2575 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneTwoOutput, audioConfig, mediaMatchCriteria); |
| 2576 | ASSERT_EQ(NO_ERROR, ret); |
| 2577 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2578 | const AudioDeviceTypeAddrVector primaryZoneDevices = {mediaOutputDevice}; |
| 2579 | mManager->setUserIdDeviceAffinities(/* userId */ 0, primaryZoneDevices); |
| 2580 | audio_port_v7 primaryZoneDevicePort; |
| 2581 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2582 | sCarBusMediaOutput, &primaryZoneDevicePort)); |
| 2583 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2584 | audio_io_handle_t output; |
| 2585 | audio_port_handle_t portId; |
| 2586 | const audio_attributes_t mediaAttribute = { |
| 2587 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2588 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2589 | uid_t user11AppUid = multiuser_get_uid(/* user_id */ 11, /* app_id */ 12345); |
| 2590 | |
| 2591 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2592 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute, |
| 2593 | AUDIO_SESSION_NONE, user11AppUid); |
| 2594 | |
| 2595 | ASSERT_EQ(primaryZoneDevicePort.id, selectedDeviceId); |
| 2596 | } |
| 2597 | |
| 2598 | TEST_F(AudioPolicyManagerCarTest, |
| 2599 | GetOutputForAttrWithMatMixAfterUserAffinitiesForTwoUsers) { |
| 2600 | status_t ret; |
| 2601 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2602 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2603 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2604 | audioConfig.sample_rate = k48000SamplingRate; |
| 2605 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2606 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2607 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2608 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2609 | ASSERT_EQ(NO_ERROR, ret); |
| 2610 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2611 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneOneOutput, audioConfig, mediaMatchCriteria); |
| 2612 | ASSERT_EQ(NO_ERROR, ret); |
| 2613 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2614 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneTwoOutput, audioConfig, mediaMatchCriteria); |
| 2615 | ASSERT_EQ(NO_ERROR, ret); |
| 2616 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2617 | const AudioDeviceTypeAddrVector primaryZoneDevices = {mediaOutputDevice}; |
| 2618 | mManager->setUserIdDeviceAffinities(/* userId */ 0, primaryZoneDevices); |
| 2619 | const AudioDeviceTypeAddr secondaryOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarRearZoneOneOutput); |
| 2620 | const AudioDeviceTypeAddrVector secondaryZoneDevices = {secondaryOutputDevice}; |
| 2621 | mManager->setUserIdDeviceAffinities(/* userId */ 11, secondaryZoneDevices); |
| 2622 | audio_port_v7 secondaryZoneDevicePort; |
| 2623 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2624 | sCarRearZoneOneOutput, &secondaryZoneDevicePort)); |
| 2625 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2626 | audio_io_handle_t output; |
| 2627 | audio_port_handle_t portId; |
| 2628 | const audio_attributes_t mediaAttribute = { |
| 2629 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2630 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2631 | uid_t user11AppUid = multiuser_get_uid(/* user_id */ 11, /* app_id */ 12345); |
| 2632 | |
| 2633 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2634 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute, |
| 2635 | AUDIO_SESSION_NONE, user11AppUid); |
| 2636 | |
| 2637 | ASSERT_EQ(secondaryZoneDevicePort.id, selectedDeviceId); |
| 2638 | } |
| 2639 | |
| 2640 | TEST_F(AudioPolicyManagerCarTest, |
| 2641 | GetOutputForAttrWithMatMixAfterUserAffinitiesForThreeUsers) { |
| 2642 | status_t ret; |
| 2643 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2644 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2645 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2646 | audioConfig.sample_rate = k48000SamplingRate; |
| 2647 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2648 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2649 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2650 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2651 | ASSERT_EQ(NO_ERROR, ret); |
| 2652 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2653 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneOneOutput, audioConfig, mediaMatchCriteria); |
| 2654 | ASSERT_EQ(NO_ERROR, ret); |
| 2655 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2656 | AUDIO_DEVICE_OUT_BUS, sCarRearZoneTwoOutput, audioConfig, mediaMatchCriteria); |
| 2657 | ASSERT_EQ(NO_ERROR, ret); |
| 2658 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2659 | const AudioDeviceTypeAddrVector primaryZoneDevices = {mediaOutputDevice}; |
| 2660 | mManager->setUserIdDeviceAffinities(/* userId */ 0, primaryZoneDevices); |
| 2661 | const AudioDeviceTypeAddr secondaryOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarRearZoneOneOutput); |
| 2662 | const AudioDeviceTypeAddrVector secondaryZoneDevices = {secondaryOutputDevice}; |
| 2663 | mManager->setUserIdDeviceAffinities(/* userId */ 11, secondaryZoneDevices); |
| 2664 | const AudioDeviceTypeAddr tertiaryOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarRearZoneTwoOutput); |
| 2665 | const AudioDeviceTypeAddrVector tertiaryZoneDevices = {tertiaryOutputDevice}; |
| 2666 | mManager->setUserIdDeviceAffinities(/* userId */ 15, tertiaryZoneDevices); |
| 2667 | audio_port_v7 tertiaryZoneDevicePort; |
| 2668 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2669 | sCarRearZoneTwoOutput, &tertiaryZoneDevicePort)); |
| 2670 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2671 | audio_io_handle_t output; |
| 2672 | audio_port_handle_t portId; |
| 2673 | const audio_attributes_t mediaAttribute = { |
| 2674 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_MEDIA, |
| 2675 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2676 | uid_t user15AppUid = multiuser_get_uid(/* user_id */ 15, /* app_id */ 12345); |
| 2677 | |
| 2678 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2679 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, mediaAttribute, |
| 2680 | AUDIO_SESSION_NONE, user15AppUid); |
| 2681 | |
| 2682 | ASSERT_EQ(tertiaryZoneDevicePort.id, selectedDeviceId); |
| 2683 | } |
| 2684 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2685 | TEST_F(AudioPolicyManagerCarTest, GetOutputForAttrWithNoMatchingMix) { |
| 2686 | status_t ret; |
| 2687 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 2688 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2689 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 2690 | audioConfig.sample_rate = k48000SamplingRate; |
| 2691 | std::vector<AudioMixMatchCriterion> mediaMatchCriteria = { |
| 2692 | createUsageCriterion(AUDIO_USAGE_MEDIA, /*exclude=*/ false)}; |
| 2693 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2694 | AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput, audioConfig, mediaMatchCriteria); |
| 2695 | ASSERT_EQ(NO_ERROR, ret); |
| 2696 | std::vector<AudioMixMatchCriterion> navMatchCriteria = { |
| 2697 | createUsageCriterion(AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 2698 | /*exclude=*/ false)}; |
| 2699 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 2700 | AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput, audioConfig, navMatchCriteria); |
| 2701 | ASSERT_EQ(NO_ERROR, ret); |
| 2702 | const AudioDeviceTypeAddr mediaOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusMediaOutput); |
| 2703 | const AudioDeviceTypeAddr navOutputDevice(AUDIO_DEVICE_OUT_BUS, sCarBusNavigationOutput); |
| 2704 | const AudioDeviceTypeAddrVector outputDevices = {mediaOutputDevice, navOutputDevice}; |
Oscar Azucena | 4f49ef6 | 2023-01-25 23:32:13 -0800 | [diff] [blame] | 2705 | mManager->setUserIdDeviceAffinities(/* userId */ 0, outputDevices); |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 2706 | audio_port_v7 navDevicePort; |
| 2707 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_BUS, |
| 2708 | sCarBusNavigationOutput, &navDevicePort)); |
| 2709 | audio_port_handle_t selectedDeviceId = navDevicePort.id; |
| 2710 | audio_io_handle_t output; |
| 2711 | audio_port_handle_t portId; |
| 2712 | const audio_attributes_t alarmAttribute = { |
| 2713 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_ALARM, |
| 2714 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 2715 | |
| 2716 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2717 | k48000SamplingRate, AUDIO_OUTPUT_FLAG_DIRECT, &output, &portId, alarmAttribute); |
| 2718 | |
| 2719 | ASSERT_EQ(navDevicePort.id, selectedDeviceId); |
| 2720 | } |
| 2721 | |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2722 | class AudioPolicyManagerTVTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 2723 | protected: |
| 2724 | std::string getConfigFile() override { return sTvConfig; } |
| 2725 | void testHDMIPortSelection(audio_output_flags_t flags, const char* expectedMixPortName); |
| 2726 | |
| 2727 | static const std::string sTvConfig; |
| 2728 | }; |
| 2729 | |
| 2730 | const std::string AudioPolicyManagerTVTest::sTvConfig = |
| 2731 | AudioPolicyManagerTVTest::sExecutableDir + "test_tv_apm_configuration.xml"; |
| 2732 | |
| 2733 | // SwAudioOutputDescriptor doesn't populate flags so check against the port name. |
| 2734 | void AudioPolicyManagerTVTest::testHDMIPortSelection( |
| 2735 | audio_output_flags_t flags, const char* expectedMixPortName) { |
| 2736 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2737 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2738 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 2739 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2740 | audio_io_handle_t output; |
| 2741 | audio_port_handle_t portId; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 2742 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 2743 | k48000SamplingRate, flags, &output, &portId); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2744 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 2745 | ASSERT_NE(nullptr, outDesc.get()); |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 2746 | audio_port_v7 port = {}; |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2747 | outDesc->toAudioPort(&port); |
| 2748 | mManager->releaseOutput(portId); |
| 2749 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2750 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2751 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 2752 | ASSERT_EQ(AUDIO_PORT_TYPE_MIX, port.type); |
| 2753 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, port.role); |
| 2754 | ASSERT_STREQ(expectedMixPortName, port.name); |
| 2755 | } |
| 2756 | |
| 2757 | TEST_F(AudioPolicyManagerTVTest, InitSuccess) { |
| 2758 | // SetUp must finish with no assertions. |
| 2759 | } |
| 2760 | |
| 2761 | TEST_F(AudioPolicyManagerTVTest, Dump) { |
| 2762 | dumpToLog(); |
| 2763 | } |
| 2764 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 2765 | TEST_F(AudioPolicyManagerTVTest, MatchNoFlags) { |
| 2766 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_NONE, "primary output"); |
| 2767 | } |
| 2768 | |
| 2769 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectNoHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2770 | // b/140447125: The selected port must not have HW AV Sync flag (see the config file). |
| 2771 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_DIRECT, "direct"); |
| 2772 | } |
| 2773 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 2774 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 2775 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 2776 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
| 2777 | "tunnel"); |
| 2778 | } |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 2779 | |
| 2780 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectMMapNoIrq) { |
| 2781 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 2782 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_MMAP_NOIRQ), |
| 2783 | "low latency"); |
| 2784 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 2785 | |
| 2786 | class AudioPolicyManagerDynamicHwModulesTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 2787 | protected: |
| 2788 | void SetUpManagerConfig() override; |
| 2789 | }; |
| 2790 | |
| 2791 | void AudioPolicyManagerDynamicHwModulesTest::SetUpManagerConfig() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 2792 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 2793 | // Only allow successful opening of "primary" hw module during APM initialization. |
| 2794 | mClient->swapAllowedModuleNames({"primary"}); |
| 2795 | } |
| 2796 | |
| 2797 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, InitSuccess) { |
| 2798 | // SetUp must finish with no assertions. |
| 2799 | } |
| 2800 | |
| 2801 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, DynamicAddition) { |
| 2802 | const auto handleBefore = mClient->peekNextModuleHandle(); |
| 2803 | mManager->onNewAudioModulesAvailable(); |
| 2804 | ASSERT_EQ(handleBefore, mClient->peekNextModuleHandle()); |
| 2805 | // Reset module loading restrictions. |
| 2806 | mClient->swapAllowedModuleNames(); |
| 2807 | mManager->onNewAudioModulesAvailable(); |
| 2808 | const auto handleAfter = mClient->peekNextModuleHandle(); |
| 2809 | ASSERT_GT(handleAfter, handleBefore); |
| 2810 | mManager->onNewAudioModulesAvailable(); |
| 2811 | ASSERT_EQ(handleAfter, mClient->peekNextModuleHandle()); |
| 2812 | } |
| 2813 | |
| 2814 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, AddedDeviceAvailable) { |
| 2815 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, mManager->getDeviceConnectionState( |
| 2816 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 2817 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 2818 | mManager->onNewAudioModulesAvailable(); |
| 2819 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_AVAILABLE, mManager->getDeviceConnectionState( |
| 2820 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 2821 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 2822 | |
| 2823 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ListAddedAudioPorts) { |
| 2824 | ASSERT_FALSE( |
| 2825 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", nullptr)); |
| 2826 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 2827 | mManager->onNewAudioModulesAvailable(); |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 2828 | struct audio_port_v7 port; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 2829 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", &port)); |
| 2830 | } |
| 2831 | |
| 2832 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ClientIsUpdated) { |
| 2833 | const size_t prevAudioPortListUpdateCount = mClient->getAudioPortListUpdateCount(); |
| 2834 | const uint32_t prevAudioPortGeneration = mManager->getAudioPortGeneration(); |
| 2835 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 2836 | mManager->onNewAudioModulesAvailable(); |
| 2837 | EXPECT_GT(mClient->getAudioPortListUpdateCount(), prevAudioPortListUpdateCount); |
| 2838 | EXPECT_GT(mManager->getAudioPortGeneration(), prevAudioPortGeneration); |
| 2839 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2840 | |
| 2841 | using DevicesRoleForCapturePresetParam = std::tuple<audio_source_t, device_role_t>; |
| 2842 | |
| 2843 | class AudioPolicyManagerDevicesRoleForCapturePresetTest |
| 2844 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 2845 | public testing::WithParamInterface<DevicesRoleForCapturePresetParam> { |
| 2846 | protected: |
| 2847 | // The `inputDevice` and `inputDevice2` indicate the audio devices type to be used for setting |
| 2848 | // device role. They must be declared in the test_audio_policy_configuration.xml |
| 2849 | AudioDeviceTypeAddr inputDevice = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_BUILTIN_MIC, ""); |
| 2850 | AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_HDMI, ""); |
| 2851 | }; |
| 2852 | |
| 2853 | TEST_P(AudioPolicyManagerDevicesRoleForCapturePresetTest, DevicesRoleForCapturePreset) { |
| 2854 | const audio_source_t audioSource = std::get<0>(GetParam()); |
| 2855 | const device_role_t role = std::get<1>(GetParam()); |
| 2856 | |
| 2857 | // Test invalid device when setting |
| 2858 | const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, ""); |
| 2859 | const AudioDeviceTypeAddrVector outputDevices = {outputDevice}; |
| 2860 | ASSERT_EQ(BAD_VALUE, |
| 2861 | mManager->setDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 2862 | ASSERT_EQ(BAD_VALUE, |
| 2863 | mManager->addDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 2864 | AudioDeviceTypeAddrVector devices; |
| 2865 | ASSERT_EQ(NAME_NOT_FOUND, |
| 2866 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2867 | ASSERT_TRUE(devices.empty()); |
| 2868 | ASSERT_EQ(BAD_VALUE, |
| 2869 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 2870 | |
| 2871 | // Without setting, call get/remove/clear must fail |
| 2872 | ASSERT_EQ(NAME_NOT_FOUND, |
| 2873 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2874 | ASSERT_EQ(NAME_NOT_FOUND, |
| 2875 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, devices)); |
| 2876 | ASSERT_EQ(NAME_NOT_FOUND, |
| 2877 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 2878 | |
| 2879 | // Test set/get devices role |
| 2880 | const AudioDeviceTypeAddrVector inputDevices = {inputDevice}; |
| 2881 | ASSERT_EQ(NO_ERROR, |
| 2882 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 2883 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2884 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice)); |
| 2885 | |
| 2886 | // Test setting will change the previously set devices |
| 2887 | const AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2}; |
| 2888 | ASSERT_EQ(NO_ERROR, |
| 2889 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices2)); |
| 2890 | devices.clear(); |
| 2891 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2892 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 2893 | |
| 2894 | // Test add devices |
| 2895 | ASSERT_EQ(NO_ERROR, |
| 2896 | mManager->addDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 2897 | devices.clear(); |
| 2898 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2899 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice, inputDevice2)); |
| 2900 | |
| 2901 | // Test remove devices |
| 2902 | ASSERT_EQ(NO_ERROR, |
| 2903 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 2904 | devices.clear(); |
| 2905 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2906 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 2907 | |
| 2908 | // Test remove devices that are not set as the device role |
| 2909 | ASSERT_EQ(BAD_VALUE, |
| 2910 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 2911 | |
| 2912 | // Test clear devices |
| 2913 | ASSERT_EQ(NO_ERROR, |
| 2914 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 2915 | devices.clear(); |
| 2916 | ASSERT_EQ(NAME_NOT_FOUND, |
| 2917 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 2918 | } |
| 2919 | |
jiabin | 14662b5 | 2023-03-06 21:35:29 +0000 | [diff] [blame] | 2920 | TEST_F(AudioPolicyManagerDevicesRoleForCapturePresetTest, PreferredDeviceUsedForInput) { |
| 2921 | const audio_source_t source = AUDIO_SOURCE_MIC; |
| 2922 | const device_role_t role = DEVICE_ROLE_PREFERRED; |
| 2923 | const std::string address = "card=1;device=0"; |
| 2924 | const std::string deviceName = "randomName"; |
| 2925 | |
| 2926 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2927 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2928 | address.c_str(), deviceName.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2929 | auto availableDevices = mManager->getAvailableInputDevices(); |
| 2930 | ASSERT_GT(availableDevices.size(), 1); |
| 2931 | |
| 2932 | audio_attributes_t attr = AUDIO_ATTRIBUTES_INITIALIZER; |
| 2933 | attr.source = source; |
| 2934 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2935 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 2936 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 2937 | 48000)); |
| 2938 | auto selectedDevice = availableDevices.getDeviceFromId(selectedDeviceId); |
| 2939 | ASSERT_NE(nullptr, selectedDevice); |
| 2940 | |
| 2941 | sp<DeviceDescriptor> preferredDevice = nullptr; |
| 2942 | for (const auto& device : availableDevices) { |
| 2943 | if (device != selectedDevice) { |
| 2944 | preferredDevice = device; |
| 2945 | break; |
| 2946 | } |
| 2947 | } |
| 2948 | ASSERT_NE(nullptr, preferredDevice); |
| 2949 | // After setting preferred device for capture preset, the selected device for input should be |
| 2950 | // the preferred device. |
| 2951 | ASSERT_EQ(NO_ERROR, |
| 2952 | mManager->setDevicesRoleForCapturePreset(source, role, |
| 2953 | {preferredDevice->getDeviceTypeAddr()})); |
| 2954 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2955 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 2956 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 2957 | 48000)); |
| 2958 | ASSERT_EQ(preferredDevice, availableDevices.getDeviceFromId(selectedDeviceId)); |
| 2959 | |
| 2960 | // After clearing preferred device for capture preset, the selected device for input should be |
| 2961 | // the same as original one. |
| 2962 | ASSERT_EQ(NO_ERROR, |
| 2963 | mManager->clearDevicesRoleForCapturePreset(source, role)); |
| 2964 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2965 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 2966 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 2967 | 48000)); |
| 2968 | ASSERT_EQ(selectedDevice, availableDevices.getDeviceFromId(selectedDeviceId)); |
| 2969 | |
| 2970 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2971 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2972 | address.c_str(), deviceName.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2973 | } |
| 2974 | |
| 2975 | TEST_F(AudioPolicyManagerDevicesRoleForCapturePresetTest, DisabledDeviceNotUsedForInput) { |
| 2976 | const audio_source_t source = AUDIO_SOURCE_MIC; |
| 2977 | const device_role_t role = DEVICE_ROLE_DISABLED; |
| 2978 | const std::string address = "card=1;device=0"; |
| 2979 | const std::string deviceName = "randomName"; |
| 2980 | |
| 2981 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 2982 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2983 | address.c_str(), deviceName.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 2984 | auto availableDevices = mManager->getAvailableInputDevices(); |
| 2985 | ASSERT_GT(availableDevices.size(), 1); |
| 2986 | |
| 2987 | audio_attributes_t attr = AUDIO_ATTRIBUTES_INITIALIZER; |
| 2988 | attr.source = source; |
| 2989 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 2990 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 2991 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 2992 | 48000)); |
| 2993 | auto selectedDevice = availableDevices.getDeviceFromId(selectedDeviceId); |
| 2994 | ASSERT_NE(nullptr, selectedDevice); |
| 2995 | |
| 2996 | // After setting disabled device for capture preset, the disabled device must not be |
| 2997 | // selected for input. |
| 2998 | ASSERT_EQ(NO_ERROR, |
| 2999 | mManager->setDevicesRoleForCapturePreset(source, role, |
| 3000 | {selectedDevice->getDeviceTypeAddr()})); |
| 3001 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3002 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 3003 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 3004 | 48000)); |
| 3005 | ASSERT_NE(selectedDevice, availableDevices.getDeviceFromId(selectedDeviceId)); |
| 3006 | |
| 3007 | // After clearing disabled device for capture preset, the selected device for input should be |
| 3008 | // the original one. |
| 3009 | ASSERT_EQ(NO_ERROR, |
| 3010 | mManager->clearDevicesRoleForCapturePreset(source, role)); |
| 3011 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3012 | ASSERT_NO_FATAL_FAILURE(getInputForAttr(attr, AUDIO_SESSION_NONE, 1, &selectedDeviceId, |
| 3013 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, |
| 3014 | 48000)); |
| 3015 | ASSERT_EQ(selectedDevice, availableDevices.getDeviceFromId(selectedDeviceId)); |
| 3016 | |
| 3017 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 3018 | AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3019 | address.c_str(), deviceName.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 3020 | } |
| 3021 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3022 | INSTANTIATE_TEST_CASE_P( |
| 3023 | DevicesRoleForCapturePresetOperation, |
| 3024 | AudioPolicyManagerDevicesRoleForCapturePresetTest, |
| 3025 | testing::Values( |
| 3026 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_MIC, DEVICE_ROLE_PREFERRED}), |
| 3027 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_UPLINK, |
| 3028 | DEVICE_ROLE_PREFERRED}), |
| 3029 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_DOWNLINK, |
| 3030 | DEVICE_ROLE_PREFERRED}), |
| 3031 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_CALL, DEVICE_ROLE_PREFERRED}), |
| 3032 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_CAMCORDER, DEVICE_ROLE_PREFERRED}), |
| 3033 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_RECOGNITION, |
| 3034 | DEVICE_ROLE_PREFERRED}), |
| 3035 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_COMMUNICATION, |
| 3036 | DEVICE_ROLE_PREFERRED}), |
| 3037 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_REMOTE_SUBMIX, |
| 3038 | DEVICE_ROLE_PREFERRED}), |
| 3039 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_UNPROCESSED, DEVICE_ROLE_PREFERRED}), |
| 3040 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_PERFORMANCE, |
| 3041 | DEVICE_ROLE_PREFERRED}), |
| 3042 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_ECHO_REFERENCE, |
| 3043 | DEVICE_ROLE_PREFERRED}), |
| 3044 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_FM_TUNER, DEVICE_ROLE_PREFERRED}), |
| 3045 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_HOTWORD, DEVICE_ROLE_PREFERRED}) |
| 3046 | ) |
| 3047 | ); |