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