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