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