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