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