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 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 17 | #include <memory> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 18 | #include <string> |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 19 | #include <sys/wait.h> |
| 20 | #include <unistd.h> |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 21 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 23 | #include <gmock/gmock.h> |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 24 | |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 25 | #define LOG_TAG "APM_Test" |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 26 | #include <Serializer.h> |
| 27 | #include <android-base/file.h> |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 28 | #include <android/content/AttributionSourceState.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 29 | #include <media/AudioPolicy.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 30 | #include <media/PatchBuilder.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 31 | #include <media/RecordingActivityTracker.h> |
| 32 | #include <utils/Log.h> |
| 33 | #include <utils/Vector.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 34 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 35 | #include "AudioPolicyInterface.h" |
jiabin | b3f9804 | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 36 | #include "AudioPolicyManagerTestClient.h" |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 37 | #include "AudioPolicyManagerTestClientForHdmi.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 | |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 45 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 46 | AudioPolicyTestClient client; |
| 47 | AudioPolicyTestManager manager(&client); |
| 48 | manager.getConfig().setDefault(); |
| 49 | manager.getConfig().setEngineLibraryNameSuffix("non-existent"); |
| 50 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 51 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 52 | } |
| 53 | |
| 54 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 55 | AudioPolicyTestClient client; |
| 56 | AudioPolicyTestManager manager(&client); |
| 57 | manager.getConfig().setDefault(); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 58 | // Since the default client fails to open anything, |
| 59 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 60 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 61 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 62 | } |
| 63 | |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 64 | // Verifies that a failure while loading a config doesn't leave |
| 65 | // APM config in a "dirty" state. Since AudioPolicyConfig object |
| 66 | // is a proxy for the data hosted by APM, it isn't possible |
| 67 | // to "deep copy" it, and thus we have to test its elements |
| 68 | // individually. |
| 69 | TEST(AudioPolicyManagerTestInit, ConfigLoadingIsTransactional) { |
| 70 | AudioPolicyTestClient client; |
| 71 | AudioPolicyTestManager manager(&client); |
| 72 | ASSERT_TRUE(manager.getConfig().getHwModules().isEmpty()); |
| 73 | ASSERT_TRUE(manager.getConfig().getInputDevices().isEmpty()); |
| 74 | ASSERT_TRUE(manager.getConfig().getOutputDevices().isEmpty()); |
| 75 | status_t status = deserializeAudioPolicyFile( |
| 76 | (base::GetExecutableDirectory() + |
| 77 | "/test_invalid_audio_policy_configuration.xml").c_str(), |
| 78 | &manager.getConfig()); |
| 79 | ASSERT_NE(NO_ERROR, status); |
| 80 | EXPECT_TRUE(manager.getConfig().getHwModules().isEmpty()); |
| 81 | EXPECT_TRUE(manager.getConfig().getInputDevices().isEmpty()); |
| 82 | EXPECT_TRUE(manager.getConfig().getOutputDevices().isEmpty()); |
| 83 | status = deserializeAudioPolicyFile( |
| 84 | (base::GetExecutableDirectory() + "/test_audio_policy_configuration.xml").c_str(), |
| 85 | &manager.getConfig()); |
| 86 | ASSERT_EQ(NO_ERROR, status); |
| 87 | EXPECT_FALSE(manager.getConfig().getHwModules().isEmpty()); |
| 88 | EXPECT_FALSE(manager.getConfig().getInputDevices().isEmpty()); |
| 89 | EXPECT_FALSE(manager.getConfig().getOutputDevices().isEmpty()); |
| 90 | } |
| 91 | |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 92 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 93 | class PatchCountCheck { |
| 94 | public: |
| 95 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 96 | : mClient{client}, |
| 97 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 98 | int deltaFromSnapshot() const { |
| 99 | size_t currentCount = mClient->getActivePatchesCount(); |
| 100 | if (mInitialCount <= currentCount) { |
| 101 | return currentCount - mInitialCount; |
| 102 | } else { |
| 103 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 104 | } |
| 105 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 106 | private: |
| 107 | const AudioPolicyManagerTestClient *mClient; |
| 108 | const size_t mInitialCount; |
| 109 | }; |
| 110 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 111 | class AudioPolicyManagerTest : public testing::Test { |
| 112 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 113 | void SetUp() override; |
| 114 | void TearDown() override; |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 115 | virtual void SetUpManagerConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 116 | |
| 117 | void dumpToLog(); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 118 | // When explicit routing is needed, selectedDeviceId needs to be set as the wanted port |
| 119 | // id. Otherwise, selectedDeviceId needs to be initialized as AUDIO_PORT_HANDLE_NONE. |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 120 | void getOutputForAttr( |
| 121 | audio_port_handle_t *selectedDeviceId, |
| 122 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 123 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 124 | int sampleRate, |
| 125 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 126 | audio_io_handle_t *output = nullptr, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 127 | audio_port_handle_t *portId = nullptr, |
| 128 | audio_attributes_t attr = {}); |
| 129 | void getInputForAttr( |
| 130 | const audio_attributes_t &attr, |
| 131 | audio_unique_id_t riid, |
| 132 | audio_port_handle_t *selectedDeviceId, |
| 133 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 134 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 135 | int sampleRate, |
| 136 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 137 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 138 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 139 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 140 | // Tries to find a device port. If 'foundPort' isn't nullptr, |
| 141 | // will generate a failure if the port hasn't been found. |
| 142 | bool findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 143 | const std::string &address, audio_port_v7 *foundPort); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 144 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch* patch); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 145 | virtual AudioPolicyManagerTestClient* getClient() { return new AudioPolicyManagerTestClient; } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 146 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 147 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 148 | std::unique_ptr<AudioPolicyTestManager> mManager; |
| 149 | }; |
| 150 | |
| 151 | void AudioPolicyManagerTest::SetUp() { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 152 | mClient.reset(getClient()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 153 | mManager.reset(new AudioPolicyTestManager(mClient.get())); |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 154 | ASSERT_NO_FATAL_FAILURE(SetUpManagerConfig()); // Subclasses may want to customize the config. |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 155 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 156 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 157 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 158 | |
| 159 | void AudioPolicyManagerTest::TearDown() { |
| 160 | mManager.reset(); |
| 161 | mClient.reset(); |
| 162 | } |
| 163 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 164 | void AudioPolicyManagerTest::SetUpManagerConfig() { |
| 165 | mManager->getConfig().setDefault(); |
| 166 | } |
| 167 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 168 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 169 | int pipefd[2]; |
| 170 | ASSERT_NE(-1, pipe(pipefd)); |
| 171 | pid_t cpid = fork(); |
| 172 | ASSERT_NE(-1, cpid); |
| 173 | if (cpid == 0) { |
| 174 | // Child process reads from the pipe and logs. |
| 175 | close(pipefd[1]); |
| 176 | std::string line; |
| 177 | char buf; |
| 178 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 179 | if (buf != '\n') { |
| 180 | line += buf; |
| 181 | } else { |
| 182 | ALOGI("%s", line.c_str()); |
| 183 | line = ""; |
| 184 | } |
| 185 | } |
| 186 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 187 | close(pipefd[0]); |
| 188 | _exit(EXIT_SUCCESS); |
| 189 | } else { |
| 190 | // Parent does the dump and checks the status code. |
| 191 | close(pipefd[0]); |
| 192 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 193 | close(pipefd[1]); |
| 194 | wait(NULL); // Wait for the child to exit. |
| 195 | } |
| 196 | } |
| 197 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 198 | void AudioPolicyManagerTest::getOutputForAttr( |
| 199 | audio_port_handle_t *selectedDeviceId, |
| 200 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 201 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 202 | int sampleRate, |
| 203 | audio_output_flags_t flags, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 204 | audio_io_handle_t *output, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 205 | audio_port_handle_t *portId, |
| 206 | audio_attributes_t attr) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 207 | audio_io_handle_t localOutput; |
| 208 | if (!output) output = &localOutput; |
| 209 | *output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 210 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 211 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 212 | config.sample_rate = sampleRate; |
| 213 | config.channel_mask = channelMask; |
| 214 | config.format = format; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 215 | audio_port_handle_t localPortId; |
| 216 | if (!portId) portId = &localPortId; |
| 217 | *portId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 218 | AudioPolicyInterface::output_type_t outputType; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 219 | // TODO b/182392769: use attribution source util |
| 220 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 221 | attributionSource.uid = 0; |
| 222 | attributionSource.token = sp<BBinder>::make(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 223 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 224 | &attr, output, AUDIO_SESSION_NONE, &stream, attributionSource, &config, &flags, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 225 | selectedDeviceId, portId, {}, &outputType)); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 226 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 227 | ASSERT_NE(AUDIO_IO_HANDLE_NONE, *output); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 228 | } |
| 229 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 230 | void AudioPolicyManagerTest::getInputForAttr( |
| 231 | const audio_attributes_t &attr, |
| 232 | audio_unique_id_t riid, |
| 233 | audio_port_handle_t *selectedDeviceId, |
| 234 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 235 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 236 | int sampleRate, |
| 237 | audio_input_flags_t flags, |
| 238 | audio_port_handle_t *portId) { |
| 239 | audio_io_handle_t input = AUDIO_PORT_HANDLE_NONE; |
| 240 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 241 | config.sample_rate = sampleRate; |
| 242 | config.channel_mask = channelMask; |
| 243 | config.format = format; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 244 | audio_port_handle_t localPortId; |
| 245 | if (!portId) portId = &localPortId; |
| 246 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 247 | AudioPolicyInterface::input_type_t inputType; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 248 | // TODO b/182392769: use attribution source util |
| 249 | AttributionSourceState attributionSource = AttributionSourceState(); |
| 250 | attributionSource.uid = 0; |
| 251 | attributionSource.token = sp<BBinder>::make(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 252 | ASSERT_EQ(OK, mManager->getInputForAttr( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 253 | &attr, &input, riid, AUDIO_SESSION_NONE, attributionSource, &config, flags, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 254 | selectedDeviceId, &inputType, portId)); |
| 255 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 256 | } |
| 257 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 258 | bool AudioPolicyManagerTest::findDevicePort(audio_port_role_t role, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 259 | audio_devices_t deviceType, const std::string &address, audio_port_v7 *foundPort) { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 260 | uint32_t numPorts = 0; |
| 261 | uint32_t generation1; |
| 262 | status_t ret; |
| 263 | |
| 264 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 265 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 266 | if (HasFailure()) return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 267 | |
| 268 | uint32_t generation2; |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 269 | struct audio_port_v7 ports[numPorts]; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 270 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation2); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 271 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 272 | EXPECT_EQ(generation1, generation2) << "Generations changed during ports retrieval"; |
| 273 | if (HasFailure()) return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 274 | |
| 275 | for (const auto &port : ports) { |
| 276 | if (port.role == role && port.ext.device.type == deviceType && |
| 277 | (strncmp(port.ext.device.address, address.c_str(), |
| 278 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 279 | if (foundPort) *foundPort = port; |
| 280 | return true; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 283 | if (foundPort) { |
| 284 | ADD_FAILURE() << "Device port with role " << role << " and address " |
| 285 | << address << " not found"; |
| 286 | } |
| 287 | return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 288 | } |
| 289 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 290 | audio_port_handle_t AudioPolicyManagerTest::getDeviceIdFromPatch( |
| 291 | const struct audio_patch* patch) { |
| 292 | // The logic here is the same as the one in AudioIoDescriptor. |
| 293 | // Note this function is aim to get routed device id for test. |
| 294 | // In that case, device to device patch is not expected here. |
| 295 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 296 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 297 | return patch->sinks[0].id; |
| 298 | } else { |
| 299 | return patch->sources[0].id; |
| 300 | } |
| 301 | } |
| 302 | return AUDIO_PORT_HANDLE_NONE; |
| 303 | } |
| 304 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 305 | |
| 306 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 307 | // SetUp must finish with no assertions. |
| 308 | } |
| 309 | |
| 310 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 311 | dumpToLog(); |
| 312 | } |
| 313 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 314 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 315 | audio_patch patch{}; |
| 316 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 317 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 318 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 319 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 320 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 321 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 322 | patch.num_sinks = 1; |
| 323 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 324 | patch.num_sources = 1; |
| 325 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 326 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 327 | patch.num_sources = 2; |
| 328 | patch.num_sinks = 1; |
| 329 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 330 | patch = {}; |
| 331 | patch.num_sources = 1; |
| 332 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 333 | patch.num_sinks = 1; |
| 334 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 335 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 336 | patch = {}; |
| 337 | patch.num_sources = 1; |
| 338 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 339 | patch.num_sinks = 1; |
| 340 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 341 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 342 | // Verify that the handle is left unchanged. |
| 343 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 344 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 348 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 349 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 350 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 351 | ASSERT_FALSE(mManager->getAvailableInputDevices().isEmpty()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 352 | PatchBuilder patchBuilder; |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 353 | patchBuilder.addSource(mManager->getAvailableInputDevices()[0]). |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 354 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 355 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 356 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 357 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // TODO: Add patch creation tests that involve already existing patch |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 361 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 362 | enum |
| 363 | { |
| 364 | MSD_AUDIO_PATCH_COUNT_NUM_AUDIO_PATCHES_INDEX = 0, |
| 365 | MSD_AUDIO_PATCH_COUNT_NAME_INDEX = 1 |
| 366 | }; |
| 367 | using MsdAudioPatchCountSpecification = std::tuple<size_t, std::string>; |
| 368 | |
| 369 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest, |
| 370 | public ::testing::WithParamInterface<MsdAudioPatchCountSpecification> { |
| 371 | public: |
| 372 | AudioPolicyManagerTestMsd(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 373 | protected: |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 374 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 375 | void TearDown() override; |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame^] | 376 | AudioProfileVector getDirectProfilesForAttributes(const audio_attributes_t& attr); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 377 | |
| 378 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 379 | sp<DeviceDescriptor> mMsdInputDevice; |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 380 | sp<DeviceDescriptor> mDefaultOutputDevice; |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 381 | |
| 382 | const size_t mExpectedAudioPatchCount; |
| 383 | sp<DeviceDescriptor> mSpdifDevice; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 384 | |
| 385 | sp<DeviceDescriptor> mHdmiInputDevice; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 386 | }; |
| 387 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 388 | AudioPolicyManagerTestMsd::AudioPolicyManagerTestMsd() |
| 389 | : mExpectedAudioPatchCount(std::get<MSD_AUDIO_PATCH_COUNT_NUM_AUDIO_PATCHES_INDEX>( |
| 390 | GetParam())) {} |
| 391 | |
| 392 | INSTANTIATE_TEST_CASE_P( |
| 393 | MsdAudioPatchCount, |
| 394 | AudioPolicyManagerTestMsd, |
| 395 | ::testing::Values( |
| 396 | MsdAudioPatchCountSpecification(1u, "single"), |
| 397 | MsdAudioPatchCountSpecification(2u, "dual") |
| 398 | ), |
| 399 | [](const ::testing::TestParamInfo<MsdAudioPatchCountSpecification> &info) { |
| 400 | return std::get<MSD_AUDIO_PATCH_COUNT_NAME_INDEX>(info.param); } |
| 401 | ); |
| 402 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 403 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 404 | // 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] | 405 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTest::SetUpManagerConfig()); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 406 | AudioPolicyConfig& config = mManager->getConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 407 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 408 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 409 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 410 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 411 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 412 | sp<AudioProfile> iec958OutputProfile = new AudioProfile( |
| 413 | AUDIO_FORMAT_IEC60958, AUDIO_CHANNEL_OUT_STEREO, 48000); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 414 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 415 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 416 | mMsdOutputDevice->addAudioProfile(iec958OutputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 417 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 418 | // Match output profile from AudioPolicyConfig::setDefault. |
| 419 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 420 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 421 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 422 | config.addDevice(mMsdOutputDevice); |
| 423 | config.addDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 424 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 425 | if (mExpectedAudioPatchCount == 2) { |
| 426 | // Add SPDIF device with PCM output profile as a second device for dual MSD audio patching. |
| 427 | mSpdifDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPDIF); |
| 428 | mSpdifDevice->addAudioProfile(pcmOutputProfile); |
| 429 | config.addDevice(mSpdifDevice); |
| 430 | |
| 431 | sp<OutputProfile> spdifOutputProfile = new OutputProfile("spdif output"); |
| 432 | spdifOutputProfile->addAudioProfile(pcmOutputProfile); |
| 433 | spdifOutputProfile->addSupportedDevice(mSpdifDevice); |
| 434 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
| 435 | addOutputProfile(spdifOutputProfile); |
| 436 | } |
| 437 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 438 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 439 | HwModuleCollection modules = config.getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 440 | modules.add(msdModule); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 441 | config.setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 442 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 443 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 444 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 445 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 446 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 447 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 448 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 449 | msdCompressedOutputProfile->setFlags( |
| 450 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 451 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 452 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 453 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 454 | sp<OutputProfile> msdIec958OutputProfile = new OutputProfile("msd iec958 input"); |
| 455 | msdIec958OutputProfile->addAudioProfile(iec958OutputProfile); |
| 456 | msdIec958OutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
| 457 | msdIec958OutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 458 | msdModule->addOutputProfile(msdIec958OutputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 459 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 460 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 461 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 462 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 463 | msdModule->addInputProfile(msdInputProfile); |
| 464 | |
| 465 | // Add a profile with another encoding to the default device to test routing |
| 466 | // of streams that are not supported by MSD. |
| 467 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 468 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 469 | config.getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 470 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 471 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 472 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 473 | primaryEncodedOutputProfile->addSupportedDevice(config.getDefaultOutputDevice()); |
| 474 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 475 | addOutputProfile(primaryEncodedOutputProfile); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 476 | |
| 477 | mDefaultOutputDevice = config.getDefaultOutputDevice(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 478 | if (mExpectedAudioPatchCount == 2) { |
| 479 | mSpdifDevice->addAudioProfile(dtsOutputProfile); |
| 480 | primaryEncodedOutputProfile->addSupportedDevice(mSpdifDevice); |
| 481 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 482 | |
| 483 | // Add HDMI input device with IEC60958 profile for HDMI in -> MSD patching. |
| 484 | mHdmiInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_HDMI); |
| 485 | sp<AudioProfile> iec958InputProfile = new AudioProfile( |
| 486 | AUDIO_FORMAT_IEC60958, AUDIO_CHANNEL_IN_STEREO, 48000); |
| 487 | mHdmiInputDevice->addAudioProfile(iec958InputProfile); |
| 488 | config.addDevice(mHdmiInputDevice); |
| 489 | sp<InputProfile> hdmiInputProfile = new InputProfile("hdmi input"); |
| 490 | hdmiInputProfile->addAudioProfile(iec958InputProfile); |
| 491 | hdmiInputProfile->setFlags(AUDIO_INPUT_FLAG_DIRECT); |
| 492 | hdmiInputProfile->addSupportedDevice(mHdmiInputDevice); |
| 493 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
| 494 | addInputProfile(hdmiInputProfile); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void AudioPolicyManagerTestMsd::TearDown() { |
| 498 | mMsdOutputDevice.clear(); |
| 499 | mMsdInputDevice.clear(); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 500 | mDefaultOutputDevice.clear(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 501 | mSpdifDevice.clear(); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 502 | mHdmiInputDevice.clear(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 503 | AudioPolicyManagerTest::TearDown(); |
| 504 | } |
| 505 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame^] | 506 | AudioProfileVector AudioPolicyManagerTestMsd::getDirectProfilesForAttributes( |
| 507 | const audio_attributes_t& attr) { |
| 508 | AudioProfileVector audioProfilesVector; |
| 509 | mManager->getDirectProfilesForAttributes(&attr, audioProfilesVector); |
| 510 | return audioProfilesVector; |
| 511 | } |
| 512 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 513 | TEST_P(AudioPolicyManagerTestMsd, InitSuccess) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 514 | ASSERT_TRUE(mMsdOutputDevice); |
| 515 | ASSERT_TRUE(mMsdInputDevice); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 516 | ASSERT_TRUE(mDefaultOutputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 519 | TEST_P(AudioPolicyManagerTestMsd, Dump) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 520 | dumpToLog(); |
| 521 | } |
| 522 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 523 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 524 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 525 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 526 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 527 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 530 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationSetReleaseMsdOutputPatches) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 531 | const PatchCountCheck patchCount = snapshotPatchCount(); |
| 532 | DeviceVector devices = mManager->getAvailableOutputDevices(); |
| 533 | // Remove MSD output device to avoid patching to itself |
| 534 | devices.remove(mMsdOutputDevice); |
| 535 | ASSERT_EQ(mExpectedAudioPatchCount, devices.size()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 536 | mManager->setMsdOutputPatches(&devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 537 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
| 538 | // Dual patch: exercise creating one new audio patch and reusing another existing audio patch. |
| 539 | DeviceVector singleDevice(devices[0]); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 540 | mManager->releaseMsdOutputPatches(singleDevice); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 541 | ASSERT_EQ(mExpectedAudioPatchCount - 1, patchCount.deltaFromSnapshot()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 542 | mManager->setMsdOutputPatches(&devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 543 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 544 | mManager->releaseMsdOutputPatches(devices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 545 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
| 546 | } |
| 547 | |
| 548 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 549 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 550 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 551 | getOutputForAttr(&selectedDeviceId, |
| 552 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 553 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 554 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 557 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 558 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 559 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 560 | getOutputForAttr(&selectedDeviceId, |
| 561 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 562 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 563 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 566 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 567 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 568 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 569 | getOutputForAttr(&selectedDeviceId, |
| 570 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 571 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 572 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
| 573 | selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 574 | getOutputForAttr(&selectedDeviceId, |
| 575 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 576 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 577 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 580 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 581 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 582 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 583 | getOutputForAttr(&selectedDeviceId, |
| 584 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 585 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 586 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 589 | TEST_P(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 590 | // Switch between formats that are supported and not supported by MSD. |
| 591 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 592 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 593 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 594 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 595 | getOutputForAttr(&selectedDeviceId, |
| 596 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 597 | nullptr /*output*/, &portId); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 598 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 599 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 600 | mManager->releaseOutput(portId); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 601 | ASSERT_EQ(mExpectedAudioPatchCount, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 602 | } |
| 603 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 604 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 605 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 606 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 607 | getOutputForAttr(&selectedDeviceId, |
| 608 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 609 | nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 610 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 611 | ASSERT_EQ(-static_cast<int>(mExpectedAudioPatchCount), patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 612 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 613 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 614 | } |
| 615 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 616 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 617 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 618 | getOutputForAttr(&selectedDeviceId, |
| 619 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 620 | ASSERT_EQ(selectedDeviceId, mDefaultOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 621 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 624 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 625 | TEST_P(AudioPolicyManagerTestMsd, PatchCreationFromHdmiInToMsd) { |
| 626 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 627 | uid_t uid = 42; |
| 628 | const PatchCountCheck patchCount = snapshotPatchCount(); |
| 629 | ASSERT_FALSE(mManager->getAvailableInputDevices().isEmpty()); |
| 630 | PatchBuilder patchBuilder; |
| 631 | patchBuilder. |
| 632 | addSource(mManager->getAvailableInputDevices(). |
| 633 | getDevice(AUDIO_DEVICE_IN_HDMI, String8(""), AUDIO_FORMAT_DEFAULT)). |
| 634 | addSink(mManager->getAvailableOutputDevices(). |
| 635 | getDevice(AUDIO_DEVICE_OUT_BUS, String8(""), AUDIO_FORMAT_DEFAULT)); |
| 636 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
| 637 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
| 638 | AudioPatchCollection patches = mManager->getAudioPatches(); |
| 639 | sp<AudioPatch> patch = patches.valueFor(handle); |
| 640 | ASSERT_EQ(1, patch->mPatch.num_sources); |
| 641 | ASSERT_EQ(1, patch->mPatch.num_sinks); |
| 642 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, patch->mPatch.sources[0].role); |
| 643 | ASSERT_EQ(AUDIO_PORT_ROLE_SINK, patch->mPatch.sinks[0].role); |
| 644 | ASSERT_EQ(AUDIO_FORMAT_IEC60958, patch->mPatch.sources[0].format); |
| 645 | ASSERT_EQ(AUDIO_FORMAT_IEC60958, patch->mPatch.sinks[0].format); |
| 646 | ASSERT_EQ(AUDIO_CHANNEL_IN_STEREO, patch->mPatch.sources[0].channel_mask); |
| 647 | ASSERT_EQ(AUDIO_CHANNEL_OUT_STEREO, patch->mPatch.sinks[0].channel_mask); |
| 648 | ASSERT_EQ(48000, patch->mPatch.sources[0].sample_rate); |
| 649 | ASSERT_EQ(48000, patch->mPatch.sinks[0].sample_rate); |
| 650 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
| 651 | } |
| 652 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame^] | 653 | TEST_P(AudioPolicyManagerTestMsd, GetDirectProfilesForAttributesWithMsd) { |
| 654 | const audio_attributes_t attr = { |
| 655 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 656 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
| 657 | |
| 658 | // count expected direct profiles for the default device |
| 659 | int countDirectProfilesPrimary = 0; |
| 660 | const auto& primary = mManager->getConfig().getHwModules() |
| 661 | .getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
| 662 | for (const auto outputProfile : primary->getOutputProfiles()) { |
| 663 | if (outputProfile->asAudioPort()->isDirectOutput()) { |
| 664 | countDirectProfilesPrimary += outputProfile->asAudioPort()->getAudioProfiles().size(); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | // count expected direct profiles for the msd device |
| 669 | int countDirectProfilesMsd = 0; |
| 670 | const auto& msd = mManager->getConfig().getHwModules() |
| 671 | .getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 672 | for (const auto outputProfile : msd->getOutputProfiles()) { |
| 673 | if (outputProfile->asAudioPort()->isDirectOutput()) { |
| 674 | countDirectProfilesMsd += outputProfile->asAudioPort()->getAudioProfiles().size(); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // before setting up MSD audio patches we only have the primary hal direct profiles |
| 679 | ASSERT_EQ(countDirectProfilesPrimary, getDirectProfilesForAttributes(attr).size()); |
| 680 | |
| 681 | DeviceVector outputDevices = mManager->getAvailableOutputDevices(); |
| 682 | // Remove MSD output device to avoid patching to itself |
| 683 | outputDevices.remove(mMsdOutputDevice); |
| 684 | mManager->setMsdOutputPatches(&outputDevices); |
| 685 | |
| 686 | // after setting up MSD audio patches the MSD direct profiles are added |
| 687 | ASSERT_EQ(countDirectProfilesPrimary + countDirectProfilesMsd, |
| 688 | getDirectProfilesForAttributes(attr).size()); |
| 689 | |
| 690 | mManager->releaseMsdOutputPatches(outputDevices); |
| 691 | // releasing the MSD audio patches gets us back to the primary hal direct profiles only |
| 692 | ASSERT_EQ(countDirectProfilesPrimary, getDirectProfilesForAttributes(attr).size()); |
| 693 | } |
| 694 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 695 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 696 | protected: |
| 697 | void SetUpManagerConfig() override; |
| 698 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 699 | |
| 700 | static const std::string sExecutableDir; |
| 701 | static const std::string sDefaultConfig; |
| 702 | }; |
| 703 | |
| 704 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 705 | base::GetExecutableDirectory() + "/"; |
| 706 | |
| 707 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 708 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 709 | |
| 710 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
| 711 | status_t status = deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 712 | ASSERT_EQ(NO_ERROR, status); |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 713 | mManager->getConfig().setSource(getConfigFile()); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 717 | // SetUp must finish with no assertions. |
| 718 | } |
| 719 | |
| 720 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 721 | dumpToLog(); |
| 722 | } |
| 723 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 724 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 725 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 726 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 727 | protected: |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 728 | void TearDown() override; |
| 729 | |
| 730 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 731 | std::string mixAddress, const audio_config_t& audioConfig, |
| 732 | const std::vector<PolicyMixTuple>& rules); |
| 733 | void clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 734 | |
| 735 | Vector<AudioMix> mAudioMixes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 736 | const std::string mMixAddress = "remote_submix_media"; |
| 737 | }; |
| 738 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 739 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 740 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 741 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 745 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
| 746 | const std::vector<PolicyMixTuple>& rules) { |
| 747 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 748 | |
| 749 | for(const auto &rule: rules) { |
| 750 | myMixMatchCriteria.add(AudioMixMatchCriterion( |
| 751 | std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 752 | } |
| 753 | |
| 754 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 755 | String8(mixAddress.c_str()), 0); |
| 756 | myAudioMix.mDeviceType = deviceType; |
| 757 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 758 | mAudioMixes.clear(); |
| 759 | mAudioMixes.add(myAudioMix); |
| 760 | |
| 761 | // As the policy mixes registration may fail at some case, |
| 762 | // caller need to check the returned status. |
| 763 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 768 | if (mManager != nullptr) { |
| 769 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 770 | } |
| 771 | mAudioMixes.clear(); |
| 772 | } |
| 773 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 774 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 775 | // SetUp must finish with no assertions |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 779 | dumpToLog(); |
| 780 | } |
| 781 | |
| 782 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 783 | status_t ret; |
| 784 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 785 | |
| 786 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 787 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 788 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 789 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 790 | |
| 791 | // Fail due to the device is already connected. |
| 792 | clearPolicyMix(); |
| 793 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 794 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 795 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 796 | |
| 797 | // The first time to register policy mixes with valid parameter should succeed. |
| 798 | clearPolicyMix(); |
| 799 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 800 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 801 | audioConfig.sample_rate = 48000; |
| 802 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 803 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 804 | std::vector<PolicyMixTuple>()); |
| 805 | ASSERT_EQ(NO_ERROR, ret); |
| 806 | // Registering the same policy mixes should fail. |
| 807 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 808 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 809 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 810 | // Registration should fail due to device not found. |
| 811 | // Note that earpiece is not present in the test configuration file. |
| 812 | // 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] | 813 | clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 814 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 815 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 816 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 817 | |
| 818 | // Registration should fail due to output not found. |
| 819 | clearPolicyMix(); |
| 820 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 821 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 822 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 823 | |
| 824 | // The first time to register valid policy mixes should succeed. |
| 825 | clearPolicyMix(); |
| 826 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 827 | AUDIO_DEVICE_OUT_SPEAKER, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 828 | ASSERT_EQ(NO_ERROR, ret); |
| 829 | // Registering the same policy mixes should fail. |
| 830 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 831 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 832 | } |
| 833 | |
| 834 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 835 | status_t ret; |
| 836 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 837 | |
| 838 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 839 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 840 | audioConfig.sample_rate = 48000; |
| 841 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 842 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 843 | std::vector<PolicyMixTuple>()); |
| 844 | ASSERT_EQ(NO_ERROR, ret); |
| 845 | |
| 846 | // After successfully registering policy mixes, it should be able to unregister. |
| 847 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 848 | ASSERT_EQ(NO_ERROR, ret); |
| 849 | |
| 850 | // After unregistering policy mixes successfully, it should fail unregistering |
| 851 | // the same policy mixes as they are not registered. |
| 852 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 853 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 854 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 855 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 856 | class AudioPolicyManagerTestForHdmi |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 857 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 858 | public testing::WithParamInterface<audio_format_t> { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 859 | protected: |
| 860 | void SetUp() override; |
| 861 | std::string getConfigFile() override { return sTvConfig; } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 862 | std::map<audio_format_t, bool> getSurroundFormatsHelper(); |
| 863 | std::vector<audio_format_t> getReportedSurroundFormatsHelper(); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 864 | std::unordered_set<audio_format_t> getFormatsFromPorts(); |
| 865 | AudioPolicyManagerTestClient* getClient() override { |
| 866 | return new AudioPolicyManagerTestClientForHdmi; |
| 867 | } |
| 868 | void TearDown() override; |
| 869 | |
| 870 | static const std::string sTvConfig; |
| 871 | |
| 872 | }; |
| 873 | |
| 874 | const std::string AudioPolicyManagerTestForHdmi::sTvConfig = |
| 875 | AudioPolicyManagerTestForHdmi::sExecutableDir + |
| 876 | "test_settop_box_surround_configuration.xml"; |
| 877 | |
| 878 | void AudioPolicyManagerTestForHdmi::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 879 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTest::SetUp()); |
Mikhail Naganov | 83caee0 | 2021-10-05 15:52:01 -0700 | [diff] [blame] | 880 | mClient->addSupportedFormat(AUDIO_FORMAT_AC3); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 881 | mClient->addSupportedFormat(AUDIO_FORMAT_E_AC3); |
| 882 | mManager->setDeviceConnectionState( |
| 883 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 884 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 885 | } |
| 886 | |
| 887 | void AudioPolicyManagerTestForHdmi::TearDown() { |
| 888 | mManager->setDeviceConnectionState( |
| 889 | AUDIO_DEVICE_OUT_HDMI, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 890 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT); |
| 891 | AudioPolicyManagerTest::TearDown(); |
| 892 | } |
| 893 | |
| 894 | std::map<audio_format_t, bool> |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 895 | AudioPolicyManagerTestForHdmi::getSurroundFormatsHelper() { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 896 | unsigned int numSurroundFormats = 0; |
| 897 | std::map<audio_format_t, bool> surroundFormatsMap; |
| 898 | status_t ret = mManager->getSurroundFormats( |
| 899 | &numSurroundFormats, nullptr /* surroundFormats */, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 900 | nullptr /* surroundFormatsEnabled */); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 901 | EXPECT_EQ(NO_ERROR, ret); |
| 902 | if (ret != NO_ERROR) { |
| 903 | return surroundFormatsMap; |
| 904 | } |
| 905 | audio_format_t surroundFormats[numSurroundFormats]; |
| 906 | memset(surroundFormats, 0, sizeof(audio_format_t) * numSurroundFormats); |
| 907 | bool surroundFormatsEnabled[numSurroundFormats]; |
| 908 | memset(surroundFormatsEnabled, 0, sizeof(bool) * numSurroundFormats); |
| 909 | ret = mManager->getSurroundFormats( |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 910 | &numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 911 | EXPECT_EQ(NO_ERROR, ret); |
| 912 | if (ret != NO_ERROR) { |
| 913 | return surroundFormatsMap; |
| 914 | } |
| 915 | for (int i = 0; i< numSurroundFormats; i++) { |
| 916 | surroundFormatsMap[surroundFormats[i]] = surroundFormatsEnabled[i]; |
| 917 | } |
| 918 | return surroundFormatsMap; |
| 919 | } |
| 920 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 921 | std::vector<audio_format_t> AudioPolicyManagerTestForHdmi::getReportedSurroundFormatsHelper() { |
| 922 | unsigned int numSurroundFormats = 0; |
| 923 | std::vector<audio_format_t> surroundFormatsVector; |
| 924 | status_t ret = mManager->getReportedSurroundFormats( |
| 925 | &numSurroundFormats, nullptr /* surroundFormats */); |
| 926 | EXPECT_EQ(NO_ERROR, ret); |
| 927 | if (ret != NO_ERROR) { |
| 928 | return surroundFormatsVector; |
| 929 | } |
| 930 | audio_format_t surroundFormats[numSurroundFormats]; |
| 931 | memset(surroundFormats, 0, sizeof(audio_format_t) * numSurroundFormats); |
| 932 | ret = mManager->getReportedSurroundFormats(&numSurroundFormats, surroundFormats); |
| 933 | EXPECT_EQ(NO_ERROR, ret); |
| 934 | if (ret != NO_ERROR) { |
| 935 | return surroundFormatsVector; |
| 936 | } |
| 937 | for (const auto &surroundFormat : surroundFormats) { |
| 938 | surroundFormatsVector.push_back(surroundFormat); |
| 939 | } |
| 940 | return surroundFormatsVector; |
| 941 | } |
| 942 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 943 | std::unordered_set<audio_format_t> |
| 944 | AudioPolicyManagerTestForHdmi::getFormatsFromPorts() { |
| 945 | uint32_t numPorts = 0; |
| 946 | uint32_t generation1; |
| 947 | status_t ret; |
| 948 | std::unordered_set<audio_format_t> formats; |
| 949 | ret = mManager->listAudioPorts( |
| 950 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 951 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 952 | if (ret != NO_ERROR) { |
| 953 | return formats; |
| 954 | } |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 955 | struct audio_port_v7 ports[numPorts]; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 956 | ret = mManager->listAudioPorts( |
| 957 | AUDIO_PORT_ROLE_SINK, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation1); |
| 958 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 959 | if (ret != NO_ERROR) { |
| 960 | return formats; |
| 961 | } |
| 962 | for (const auto &port : ports) { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 963 | for (size_t i = 0; i < port.num_audio_profiles; ++i) { |
| 964 | formats.insert(port.audio_profiles[i].format); |
| 965 | } |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 966 | } |
| 967 | return formats; |
| 968 | } |
| 969 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 970 | TEST_P(AudioPolicyManagerTestForHdmi, GetSurroundFormatsReturnsSupportedFormats) { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 971 | mManager->setForceUse( |
| 972 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 973 | auto surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 974 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 975 | } |
| 976 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 977 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 978 | GetSurroundFormatsReturnsManipulatedFormats) { |
| 979 | mManager->setForceUse( |
| 980 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 981 | |
| 982 | status_t ret = |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 983 | mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 984 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 985 | auto surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 986 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 987 | ASSERT_FALSE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 988 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 989 | ret = mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 990 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 991 | surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 992 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 993 | ASSERT_TRUE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 994 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 995 | ret = mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 996 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 997 | surroundFormats = getSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 998 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 999 | ASSERT_FALSE(surroundFormats[GetParam()]); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1002 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1003 | ListAudioPortsReturnManipulatedHdmiFormats) { |
| 1004 | mManager->setForceUse( |
| 1005 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 1006 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1007 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/)); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1008 | auto formats = getFormatsFromPorts(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1009 | ASSERT_EQ(0, formats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1010 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1011 | ASSERT_EQ(NO_ERROR, mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/)); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1012 | formats = getFormatsFromPorts(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1013 | ASSERT_EQ(1, formats.count(GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1014 | } |
| 1015 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1016 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1017 | GetReportedSurroundFormatsReturnsHdmiReportedFormats) { |
| 1018 | mManager->setForceUse( |
| 1019 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1020 | auto surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1021 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1024 | TEST_P(AudioPolicyManagerTestForHdmi, |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1025 | GetReportedSurroundFormatsReturnsNonManipulatedHdmiReportedFormats) { |
| 1026 | mManager->setForceUse( |
| 1027 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL); |
| 1028 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1029 | status_t ret = mManager->setSurroundFormatEnabled(GetParam(), false /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1030 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1031 | auto surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1032 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1033 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1034 | ret = mManager->setSurroundFormatEnabled(GetParam(), true /*enabled*/); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1035 | ASSERT_EQ(NO_ERROR, ret); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 1036 | surroundFormats = getReportedSurroundFormatsHelper(); |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1037 | ASSERT_EQ(1, std::count(surroundFormats.begin(), surroundFormats.end(), GetParam())); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 1038 | } |
| 1039 | |
Mikhail Naganov | 18885d3 | 2021-10-01 13:03:09 -0700 | [diff] [blame] | 1040 | TEST_P(AudioPolicyManagerTestForHdmi, GetSurroundFormatsIgnoresSupportedFormats) { |
| 1041 | mManager->setForceUse( |
| 1042 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER); |
| 1043 | auto surroundFormats = getSurroundFormatsHelper(); |
| 1044 | ASSERT_EQ(1, surroundFormats.count(GetParam())); |
| 1045 | ASSERT_FALSE(surroundFormats[GetParam()]); |
| 1046 | } |
| 1047 | |
| 1048 | INSTANTIATE_TEST_SUITE_P(SurroundFormatSupport, AudioPolicyManagerTestForHdmi, |
| 1049 | testing::Values(AUDIO_FORMAT_AC3, AUDIO_FORMAT_E_AC3), |
| 1050 | [](const ::testing::TestParamInfo<AudioPolicyManagerTestForHdmi::ParamType>& info) { |
| 1051 | return audio_format_to_string(info.param); |
| 1052 | }); |
| 1053 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1054 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 1055 | protected: |
| 1056 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 1057 | |
| 1058 | static const std::string sPrimaryOnlyConfig; |
| 1059 | }; |
| 1060 | |
| 1061 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 1062 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 1063 | |
| 1064 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 1065 | // SetUp must finish with no assertions. |
| 1066 | } |
| 1067 | |
| 1068 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 1069 | dumpToLog(); |
| 1070 | } |
| 1071 | |
| 1072 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 1073 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 1074 | status_t ret; |
| 1075 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1076 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1077 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1078 | audioConfig.sample_rate = 48000; |
| 1079 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1080 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 1081 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1082 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1083 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 1084 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 1085 | } |
| 1086 | |
| 1087 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
| 1088 | public testing::WithParamInterface<audio_attributes_t> { |
| 1089 | protected: |
| 1090 | void SetUp() override; |
| 1091 | void TearDown() override; |
| 1092 | |
| 1093 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 1094 | |
| 1095 | std::vector<PolicyMixTuple> mUsageRules = { |
| 1096 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 1097 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE} |
| 1098 | }; |
| 1099 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1100 | struct audio_port_v7 mInjectionPort; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1101 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 1102 | }; |
| 1103 | |
| 1104 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1105 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestDynamicPolicy::SetUp()); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1106 | |
| 1107 | mTracker.reset(new RecordingActivityTracker()); |
| 1108 | |
| 1109 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1110 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1111 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1112 | audioConfig.sample_rate = 48000; |
| 1113 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1114 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, mUsageRules); |
| 1115 | ASSERT_EQ(NO_ERROR, ret); |
| 1116 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1117 | struct audio_port_v7 extractionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1118 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1119 | mMixAddress, &extractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1120 | |
| 1121 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1122 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1123 | audio_attributes_t attr = { |
| 1124 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1125 | std::string tags = "addr=" + mMixAddress; |
| 1126 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 1127 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, |
| 1128 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &mPortId); |
| 1129 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 1130 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 1131 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1132 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1133 | mMixAddress, &mInjectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 1137 | mManager->stopInput(mPortId); |
| 1138 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 1139 | } |
| 1140 | |
| 1141 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, InitSuccess) { |
| 1142 | // SetUp must finish with no assertions |
| 1143 | } |
| 1144 | |
| 1145 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, Dump) { |
| 1146 | dumpToLog(); |
| 1147 | } |
| 1148 | |
| 1149 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
| 1150 | const audio_attributes_t attr = GetParam(); |
| 1151 | const audio_usage_t usage = attr.usage; |
| 1152 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1153 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1154 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1155 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, |
| 1156 | nullptr /*output*/, nullptr /*portId*/, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1157 | if (std::find_if(begin(mUsageRules), end(mUsageRules), [&usage](const auto &usageRule) { |
| 1158 | return (std::get<0>(usageRule) == usage) && |
| 1159 | (std::get<2>(usageRule) == RULE_MATCH_ATTRIBUTE_USAGE);}) != end(mUsageRules) || |
| 1160 | (strncmp(attr.tags, "addr=", strlen("addr=")) == 0 && |
| 1161 | strncmp(attr.tags + strlen("addr="), mMixAddress.c_str(), |
| 1162 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0)) { |
| 1163 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 1164 | } else { |
| 1165 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | INSTANTIATE_TEST_CASE_P( |
| 1170 | PlaybackReroutingUsageMatch, |
| 1171 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1172 | testing::Values( |
| 1173 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1174 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1175 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1176 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1177 | ) |
| 1178 | ); |
| 1179 | |
| 1180 | INSTANTIATE_TEST_CASE_P( |
| 1181 | PlaybackReroutingAddressPriorityMatch, |
| 1182 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1183 | testing::Values( |
| 1184 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1185 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1186 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1187 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1188 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1189 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 1190 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1191 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1192 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1193 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1194 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1195 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1196 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 1197 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1198 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1199 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 1200 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1201 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1202 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 1203 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1204 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1205 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 1206 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1207 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1208 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1209 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1210 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 1211 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1212 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1213 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 1214 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1215 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1216 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 1217 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1218 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1219 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1220 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VIRTUAL_SOURCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1221 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1222 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1223 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 1224 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1225 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1226 | ) |
| 1227 | ); |
| 1228 | |
| 1229 | INSTANTIATE_TEST_CASE_P( |
| 1230 | PlaybackReroutingUnHandledUsages, |
| 1231 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 1232 | testing::Values( |
| 1233 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1234 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1235 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1236 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1237 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1238 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1239 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1240 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1241 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1242 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1243 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1244 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1245 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1246 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1247 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1248 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1249 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1250 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1251 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1252 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1253 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1254 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1255 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1256 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1257 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1258 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1259 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1260 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 1261 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1262 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1263 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1264 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1265 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1266 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 1267 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1268 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1269 | ) |
| 1270 | ); |
| 1271 | |
| 1272 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
| 1273 | public testing::WithParamInterface<audio_attributes_t> { |
| 1274 | protected: |
| 1275 | void SetUp() override; |
| 1276 | void TearDown() override; |
| 1277 | |
| 1278 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 1279 | |
| 1280 | std::vector<PolicyMixTuple> mSourceRules = { |
| 1281 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 1282 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 1283 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} |
| 1284 | }; |
| 1285 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1286 | struct audio_port_v7 mExtractionPort; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1287 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 1288 | }; |
| 1289 | |
| 1290 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1291 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestDynamicPolicy::SetUp()); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1292 | |
| 1293 | mTracker.reset(new RecordingActivityTracker()); |
| 1294 | |
| 1295 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 1296 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1297 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1298 | audioConfig.sample_rate = 48000; |
| 1299 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 1300 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, mSourceRules); |
| 1301 | ASSERT_EQ(NO_ERROR, ret); |
| 1302 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1303 | struct audio_port_v7 injectionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1304 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1305 | mMixAddress, &injectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1306 | |
| 1307 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1308 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1309 | audio_attributes_t attr = |
| 1310 | {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1311 | std::string tags = std::string("addr=") + mMixAddress; |
| 1312 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 1313 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1314 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, &mPortId, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1315 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 1316 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 1317 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1318 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1319 | mMixAddress, &mExtractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 1323 | mManager->stopOutput(mPortId); |
| 1324 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 1325 | } |
| 1326 | |
| 1327 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, InitSuccess) { |
| 1328 | // SetUp mush finish with no assertions. |
| 1329 | } |
| 1330 | |
| 1331 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, Dump) { |
| 1332 | dumpToLog(); |
| 1333 | } |
| 1334 | |
| 1335 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
| 1336 | const audio_attributes_t attr = GetParam(); |
| 1337 | const audio_source_t source = attr.source; |
| 1338 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 1339 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1340 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 1341 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 1342 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 1343 | if (std::find_if(begin(mSourceRules), end(mSourceRules), [&source](const auto &sourceRule) { |
| 1344 | return (std::get<1>(sourceRule) == source) && |
| 1345 | (std::get<2>(sourceRule) == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET);}) |
| 1346 | != end(mSourceRules)) { |
| 1347 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 1348 | } else { |
| 1349 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | // No address priority rule for remote recording, address is a "don't care" |
| 1354 | INSTANTIATE_TEST_CASE_P( |
| 1355 | RecordInjectionSourceMatch, |
| 1356 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1357 | testing::Values( |
| 1358 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1359 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1360 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1361 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, |
| 1362 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1363 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1364 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, |
| 1365 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1366 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1367 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1368 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1369 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1370 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1371 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1372 | "addr=remote_submix_media"} |
| 1373 | ) |
| 1374 | ); |
| 1375 | |
| 1376 | // No address priority rule for remote recording |
| 1377 | INSTANTIATE_TEST_CASE_P( |
| 1378 | RecordInjectionSourceNotMatch, |
| 1379 | AudioPolicyManagerTestDPMixRecordInjection, |
| 1380 | testing::Values( |
| 1381 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1382 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1383 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1384 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1385 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1386 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1387 | "addr=remote_submix_media"}, |
| 1388 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1389 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, |
| 1390 | "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 1391 | ) |
| 1392 | ); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1393 | |
| 1394 | using DeviceConnectionTestParams = |
| 1395 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 1396 | |
| 1397 | class AudioPolicyManagerTestDeviceConnection : public AudioPolicyManagerTestWithConfigurationFile, |
| 1398 | public testing::WithParamInterface<DeviceConnectionTestParams> { |
| 1399 | }; |
| 1400 | |
| 1401 | TEST_F(AudioPolicyManagerTestDeviceConnection, InitSuccess) { |
| 1402 | // SetUp must finish with no assertions. |
| 1403 | } |
| 1404 | |
| 1405 | TEST_F(AudioPolicyManagerTestDeviceConnection, Dump) { |
| 1406 | dumpToLog(); |
| 1407 | } |
| 1408 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 1409 | TEST_F(AudioPolicyManagerTestDeviceConnection, RoutingUpdate) { |
| 1410 | mClient->resetRoutingUpdatedCounter(); |
| 1411 | // Connecting a valid output device with valid parameters should trigger a routing update |
| 1412 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1413 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1414 | "a", "b", AUDIO_FORMAT_DEFAULT)); |
| 1415 | ASSERT_EQ(1, mClient->getRoutingUpdatedCounter()); |
| 1416 | |
| 1417 | // Disconnecting a connected device should succeed and trigger a routing update |
| 1418 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1419 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1420 | "a", "b", AUDIO_FORMAT_DEFAULT)); |
| 1421 | ASSERT_EQ(2, mClient->getRoutingUpdatedCounter()); |
| 1422 | |
| 1423 | // Disconnecting a disconnected device should fail and not trigger a routing update |
| 1424 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1425 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1426 | "a", "b", AUDIO_FORMAT_DEFAULT)); |
| 1427 | ASSERT_EQ(2, mClient->getRoutingUpdatedCounter()); |
| 1428 | |
| 1429 | // Changing force use should trigger an update |
| 1430 | auto config = mManager->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA); |
| 1431 | auto newConfig = config == AUDIO_POLICY_FORCE_BT_A2DP ? |
| 1432 | AUDIO_POLICY_FORCE_NONE : AUDIO_POLICY_FORCE_BT_A2DP; |
| 1433 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA, newConfig); |
| 1434 | ASSERT_EQ(3, mClient->getRoutingUpdatedCounter()); |
| 1435 | } |
| 1436 | |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1437 | TEST_P(AudioPolicyManagerTestDeviceConnection, SetDeviceConnectionState) { |
| 1438 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1439 | const std::string name = std::get<1>(GetParam()); |
| 1440 | const std::string address = std::get<2>(GetParam()); |
| 1441 | |
| 1442 | if (type == AUDIO_DEVICE_OUT_HDMI) { |
| 1443 | // Set device connection state failed due to no device descriptor found |
| 1444 | // 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] | 1445 | // by using an encoded format which isn't listed in the 'encodedFormats' |
| 1446 | // attribute for this devicePort. |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1447 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1448 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1449 | address.c_str(), name.c_str(), AUDIO_FORMAT_MAT_2_1)); |
| 1450 | } |
| 1451 | // Connect with valid parameters should succeed |
| 1452 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1453 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1454 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1455 | // Try to connect with the same device again should fail |
| 1456 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1457 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1458 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1459 | // Disconnect the connected device should succeed |
| 1460 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1461 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1462 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1463 | // Disconnect device that is not connected should fail |
| 1464 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1465 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1466 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1467 | // Try to set device connection state with a invalid connection state should fail |
| 1468 | ASSERT_EQ(BAD_VALUE, mManager->setDeviceConnectionState( |
| 1469 | type, AUDIO_POLICY_DEVICE_STATE_CNT, |
| 1470 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1471 | } |
| 1472 | |
| 1473 | TEST_P(AudioPolicyManagerTestDeviceConnection, ExplicitlyRoutingAfterConnection) { |
| 1474 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1475 | const std::string name = std::get<1>(GetParam()); |
| 1476 | const std::string address = std::get<2>(GetParam()); |
| 1477 | |
| 1478 | // Connect device to do explicitly routing test |
| 1479 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1480 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1481 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1482 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1483 | audio_port_v7 devicePort; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1484 | const audio_port_role_t role = audio_is_output_device(type) |
| 1485 | ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1486 | ASSERT_TRUE(findDevicePort(role, type, address, &devicePort)); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1487 | |
| 1488 | audio_port_handle_t routedPortId = devicePort.id; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1489 | // Try start input or output according to the device type |
| 1490 | if (audio_is_output_devices(type)) { |
| 1491 | getOutputForAttr(&routedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1492 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1493 | } else if (audio_is_input_device(type)) { |
| 1494 | RecordingActivityTracker tracker; |
| 1495 | getInputForAttr({}, tracker.getRiid(), &routedPortId, AUDIO_FORMAT_PCM_16_BIT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1496 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1497 | } |
| 1498 | ASSERT_EQ(devicePort.id, routedPortId); |
| 1499 | |
| 1500 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1501 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1502 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1503 | } |
| 1504 | |
| 1505 | INSTANTIATE_TEST_CASE_P( |
| 1506 | DeviceConnectionState, |
| 1507 | AudioPolicyManagerTestDeviceConnection, |
| 1508 | testing::Values( |
| 1509 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_HDMI, "test_in_hdmi", |
| 1510 | "audio_policy_test_in_hdmi"}), |
| 1511 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_HDMI, "test_out_hdmi", |
| 1512 | "audio_policy_test_out_hdmi"}), |
| 1513 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "bt_hfp_in", |
| 1514 | "hfp_client_in"}), |
| 1515 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "bt_hfp_out", |
| 1516 | "hfp_client_out"}) |
| 1517 | ) |
| 1518 | ); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1519 | |
| 1520 | class AudioPolicyManagerTVTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1521 | protected: |
| 1522 | std::string getConfigFile() override { return sTvConfig; } |
| 1523 | void testHDMIPortSelection(audio_output_flags_t flags, const char* expectedMixPortName); |
| 1524 | |
| 1525 | static const std::string sTvConfig; |
| 1526 | }; |
| 1527 | |
| 1528 | const std::string AudioPolicyManagerTVTest::sTvConfig = |
| 1529 | AudioPolicyManagerTVTest::sExecutableDir + "test_tv_apm_configuration.xml"; |
| 1530 | |
| 1531 | // SwAudioOutputDescriptor doesn't populate flags so check against the port name. |
| 1532 | void AudioPolicyManagerTVTest::testHDMIPortSelection( |
| 1533 | audio_output_flags_t flags, const char* expectedMixPortName) { |
| 1534 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1535 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1536 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1537 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1538 | audio_io_handle_t output; |
| 1539 | audio_port_handle_t portId; |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1540 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1541 | flags, &output, &portId); |
| 1542 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 1543 | ASSERT_NE(nullptr, outDesc.get()); |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1544 | audio_port_v7 port = {}; |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1545 | outDesc->toAudioPort(&port); |
| 1546 | mManager->releaseOutput(portId); |
| 1547 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1548 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1549 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1550 | ASSERT_EQ(AUDIO_PORT_TYPE_MIX, port.type); |
| 1551 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, port.role); |
| 1552 | ASSERT_STREQ(expectedMixPortName, port.name); |
| 1553 | } |
| 1554 | |
| 1555 | TEST_F(AudioPolicyManagerTVTest, InitSuccess) { |
| 1556 | // SetUp must finish with no assertions. |
| 1557 | } |
| 1558 | |
| 1559 | TEST_F(AudioPolicyManagerTVTest, Dump) { |
| 1560 | dumpToLog(); |
| 1561 | } |
| 1562 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1563 | TEST_F(AudioPolicyManagerTVTest, MatchNoFlags) { |
| 1564 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_NONE, "primary output"); |
| 1565 | } |
| 1566 | |
| 1567 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectNoHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1568 | // b/140447125: The selected port must not have HW AV Sync flag (see the config file). |
| 1569 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_DIRECT, "direct"); |
| 1570 | } |
| 1571 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1572 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1573 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1574 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
| 1575 | "tunnel"); |
| 1576 | } |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1577 | |
| 1578 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectMMapNoIrq) { |
| 1579 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1580 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_MMAP_NOIRQ), |
| 1581 | "low latency"); |
| 1582 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 1583 | |
| 1584 | class AudioPolicyManagerDynamicHwModulesTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1585 | protected: |
| 1586 | void SetUpManagerConfig() override; |
| 1587 | }; |
| 1588 | |
| 1589 | void AudioPolicyManagerDynamicHwModulesTest::SetUpManagerConfig() { |
Mikhail Naganov | d4c21aa | 2021-10-05 15:10:16 -0700 | [diff] [blame] | 1590 | ASSERT_NO_FATAL_FAILURE(AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 1591 | // Only allow successful opening of "primary" hw module during APM initialization. |
| 1592 | mClient->swapAllowedModuleNames({"primary"}); |
| 1593 | } |
| 1594 | |
| 1595 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, InitSuccess) { |
| 1596 | // SetUp must finish with no assertions. |
| 1597 | } |
| 1598 | |
| 1599 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, DynamicAddition) { |
| 1600 | const auto handleBefore = mClient->peekNextModuleHandle(); |
| 1601 | mManager->onNewAudioModulesAvailable(); |
| 1602 | ASSERT_EQ(handleBefore, mClient->peekNextModuleHandle()); |
| 1603 | // Reset module loading restrictions. |
| 1604 | mClient->swapAllowedModuleNames(); |
| 1605 | mManager->onNewAudioModulesAvailable(); |
| 1606 | const auto handleAfter = mClient->peekNextModuleHandle(); |
| 1607 | ASSERT_GT(handleAfter, handleBefore); |
| 1608 | mManager->onNewAudioModulesAvailable(); |
| 1609 | ASSERT_EQ(handleAfter, mClient->peekNextModuleHandle()); |
| 1610 | } |
| 1611 | |
| 1612 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, AddedDeviceAvailable) { |
| 1613 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, mManager->getDeviceConnectionState( |
| 1614 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1615 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1616 | mManager->onNewAudioModulesAvailable(); |
| 1617 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_AVAILABLE, mManager->getDeviceConnectionState( |
| 1618 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1619 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1620 | |
| 1621 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ListAddedAudioPorts) { |
| 1622 | ASSERT_FALSE( |
| 1623 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", nullptr)); |
| 1624 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1625 | mManager->onNewAudioModulesAvailable(); |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 1626 | struct audio_port_v7 port; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1627 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", &port)); |
| 1628 | } |
| 1629 | |
| 1630 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ClientIsUpdated) { |
| 1631 | const size_t prevAudioPortListUpdateCount = mClient->getAudioPortListUpdateCount(); |
| 1632 | const uint32_t prevAudioPortGeneration = mManager->getAudioPortGeneration(); |
| 1633 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1634 | mManager->onNewAudioModulesAvailable(); |
| 1635 | EXPECT_GT(mClient->getAudioPortListUpdateCount(), prevAudioPortListUpdateCount); |
| 1636 | EXPECT_GT(mManager->getAudioPortGeneration(), prevAudioPortGeneration); |
| 1637 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 1638 | |
| 1639 | using DevicesRoleForCapturePresetParam = std::tuple<audio_source_t, device_role_t>; |
| 1640 | |
| 1641 | class AudioPolicyManagerDevicesRoleForCapturePresetTest |
| 1642 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 1643 | public testing::WithParamInterface<DevicesRoleForCapturePresetParam> { |
| 1644 | protected: |
| 1645 | // The `inputDevice` and `inputDevice2` indicate the audio devices type to be used for setting |
| 1646 | // device role. They must be declared in the test_audio_policy_configuration.xml |
| 1647 | AudioDeviceTypeAddr inputDevice = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_BUILTIN_MIC, ""); |
| 1648 | AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_HDMI, ""); |
| 1649 | }; |
| 1650 | |
| 1651 | TEST_P(AudioPolicyManagerDevicesRoleForCapturePresetTest, DevicesRoleForCapturePreset) { |
| 1652 | const audio_source_t audioSource = std::get<0>(GetParam()); |
| 1653 | const device_role_t role = std::get<1>(GetParam()); |
| 1654 | |
| 1655 | // Test invalid device when setting |
| 1656 | const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, ""); |
| 1657 | const AudioDeviceTypeAddrVector outputDevices = {outputDevice}; |
| 1658 | ASSERT_EQ(BAD_VALUE, |
| 1659 | mManager->setDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1660 | ASSERT_EQ(BAD_VALUE, |
| 1661 | mManager->addDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1662 | AudioDeviceTypeAddrVector devices; |
| 1663 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1664 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1665 | ASSERT_TRUE(devices.empty()); |
| 1666 | ASSERT_EQ(BAD_VALUE, |
| 1667 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1668 | |
| 1669 | // Without setting, call get/remove/clear must fail |
| 1670 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1671 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1672 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1673 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, devices)); |
| 1674 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1675 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1676 | |
| 1677 | // Test set/get devices role |
| 1678 | const AudioDeviceTypeAddrVector inputDevices = {inputDevice}; |
| 1679 | ASSERT_EQ(NO_ERROR, |
| 1680 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1681 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1682 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice)); |
| 1683 | |
| 1684 | // Test setting will change the previously set devices |
| 1685 | const AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2}; |
| 1686 | ASSERT_EQ(NO_ERROR, |
| 1687 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices2)); |
| 1688 | devices.clear(); |
| 1689 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1690 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1691 | |
| 1692 | // Test add devices |
| 1693 | ASSERT_EQ(NO_ERROR, |
| 1694 | mManager->addDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1695 | devices.clear(); |
| 1696 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1697 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice, inputDevice2)); |
| 1698 | |
| 1699 | // Test remove devices |
| 1700 | ASSERT_EQ(NO_ERROR, |
| 1701 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1702 | devices.clear(); |
| 1703 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1704 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1705 | |
| 1706 | // Test remove devices that are not set as the device role |
| 1707 | ASSERT_EQ(BAD_VALUE, |
| 1708 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1709 | |
| 1710 | // Test clear devices |
| 1711 | ASSERT_EQ(NO_ERROR, |
| 1712 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1713 | devices.clear(); |
| 1714 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1715 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1716 | } |
| 1717 | |
| 1718 | INSTANTIATE_TEST_CASE_P( |
| 1719 | DevicesRoleForCapturePresetOperation, |
| 1720 | AudioPolicyManagerDevicesRoleForCapturePresetTest, |
| 1721 | testing::Values( |
| 1722 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_MIC, DEVICE_ROLE_PREFERRED}), |
| 1723 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_UPLINK, |
| 1724 | DEVICE_ROLE_PREFERRED}), |
| 1725 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_DOWNLINK, |
| 1726 | DEVICE_ROLE_PREFERRED}), |
| 1727 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_CALL, DEVICE_ROLE_PREFERRED}), |
| 1728 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_CAMCORDER, DEVICE_ROLE_PREFERRED}), |
| 1729 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_RECOGNITION, |
| 1730 | DEVICE_ROLE_PREFERRED}), |
| 1731 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_COMMUNICATION, |
| 1732 | DEVICE_ROLE_PREFERRED}), |
| 1733 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_REMOTE_SUBMIX, |
| 1734 | DEVICE_ROLE_PREFERRED}), |
| 1735 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_UNPROCESSED, DEVICE_ROLE_PREFERRED}), |
| 1736 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_PERFORMANCE, |
| 1737 | DEVICE_ROLE_PREFERRED}), |
| 1738 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_ECHO_REFERENCE, |
| 1739 | DEVICE_ROLE_PREFERRED}), |
| 1740 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_FM_TUNER, DEVICE_ROLE_PREFERRED}), |
| 1741 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_HOTWORD, DEVICE_ROLE_PREFERRED}) |
| 1742 | ) |
| 1743 | ); |