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