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