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