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