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