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