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