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