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