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> |
| 28 | #include <media/AudioPolicy.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 29 | #include <media/PatchBuilder.h> |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 30 | #include <media/RecordingActivityTracker.h> |
| 31 | #include <utils/Log.h> |
| 32 | #include <utils/Vector.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 33 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 34 | #include "AudioPolicyInterface.h" |
jiabin | b3f9804 | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 35 | #include "AudioPolicyManagerTestClient.h" |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 36 | #include "AudioPolicyTestClient.h" |
| 37 | #include "AudioPolicyTestManager.h" |
| 38 | |
| 39 | using namespace android; |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 40 | using testing::UnorderedElementsAre; |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 41 | |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 42 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 43 | AudioPolicyTestClient client; |
| 44 | AudioPolicyTestManager manager(&client); |
| 45 | manager.getConfig().setDefault(); |
| 46 | manager.getConfig().setEngineLibraryNameSuffix("non-existent"); |
| 47 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 48 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 49 | } |
| 50 | |
| 51 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 52 | AudioPolicyTestClient client; |
| 53 | AudioPolicyTestManager manager(&client); |
| 54 | manager.getConfig().setDefault(); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 55 | // Since the default client fails to open anything, |
| 56 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 57 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 58 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 59 | } |
| 60 | |
| 61 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 62 | class PatchCountCheck { |
| 63 | public: |
| 64 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 65 | : mClient{client}, |
| 66 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 67 | int deltaFromSnapshot() const { |
| 68 | size_t currentCount = mClient->getActivePatchesCount(); |
| 69 | if (mInitialCount <= currentCount) { |
| 70 | return currentCount - mInitialCount; |
| 71 | } else { |
| 72 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 73 | } |
| 74 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 75 | private: |
| 76 | const AudioPolicyManagerTestClient *mClient; |
| 77 | const size_t mInitialCount; |
| 78 | }; |
| 79 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 80 | class AudioPolicyManagerTest : public testing::Test { |
| 81 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 82 | void SetUp() override; |
| 83 | void TearDown() override; |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 84 | virtual void SetUpManagerConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 85 | |
| 86 | void dumpToLog(); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 87 | // When explicit routing is needed, selectedDeviceId needs to be set as the wanted port |
| 88 | // id. Otherwise, selectedDeviceId needs to be initialized as AUDIO_PORT_HANDLE_NONE. |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 89 | void getOutputForAttr( |
| 90 | audio_port_handle_t *selectedDeviceId, |
| 91 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 92 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 93 | int sampleRate, |
| 94 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 95 | audio_io_handle_t *output = nullptr, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 96 | audio_port_handle_t *portId = nullptr, |
| 97 | audio_attributes_t attr = {}); |
| 98 | void getInputForAttr( |
| 99 | const audio_attributes_t &attr, |
| 100 | audio_unique_id_t riid, |
| 101 | audio_port_handle_t *selectedDeviceId, |
| 102 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 103 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 104 | int sampleRate, |
| 105 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 106 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 107 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 108 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 109 | // Tries to find a device port. If 'foundPort' isn't nullptr, |
| 110 | // will generate a failure if the port hasn't been found. |
| 111 | bool findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
| 112 | const std::string &address, audio_port *foundPort); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 113 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch* patch); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 114 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 115 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 116 | std::unique_ptr<AudioPolicyTestManager> mManager; |
| 117 | }; |
| 118 | |
| 119 | void AudioPolicyManagerTest::SetUp() { |
| 120 | mClient.reset(new AudioPolicyManagerTestClient); |
| 121 | mManager.reset(new AudioPolicyTestManager(mClient.get())); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 122 | SetUpManagerConfig(); // Subclasses may want to customize the config. |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 123 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 124 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 125 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 126 | |
| 127 | void AudioPolicyManagerTest::TearDown() { |
| 128 | mManager.reset(); |
| 129 | mClient.reset(); |
| 130 | } |
| 131 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 132 | void AudioPolicyManagerTest::SetUpManagerConfig() { |
| 133 | mManager->getConfig().setDefault(); |
| 134 | } |
| 135 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 136 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 137 | int pipefd[2]; |
| 138 | ASSERT_NE(-1, pipe(pipefd)); |
| 139 | pid_t cpid = fork(); |
| 140 | ASSERT_NE(-1, cpid); |
| 141 | if (cpid == 0) { |
| 142 | // Child process reads from the pipe and logs. |
| 143 | close(pipefd[1]); |
| 144 | std::string line; |
| 145 | char buf; |
| 146 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 147 | if (buf != '\n') { |
| 148 | line += buf; |
| 149 | } else { |
| 150 | ALOGI("%s", line.c_str()); |
| 151 | line = ""; |
| 152 | } |
| 153 | } |
| 154 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 155 | close(pipefd[0]); |
| 156 | _exit(EXIT_SUCCESS); |
| 157 | } else { |
| 158 | // Parent does the dump and checks the status code. |
| 159 | close(pipefd[0]); |
| 160 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 161 | close(pipefd[1]); |
| 162 | wait(NULL); // Wait for the child to exit. |
| 163 | } |
| 164 | } |
| 165 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 166 | void AudioPolicyManagerTest::getOutputForAttr( |
| 167 | audio_port_handle_t *selectedDeviceId, |
| 168 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 169 | audio_channel_mask_t channelMask, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 170 | int sampleRate, |
| 171 | audio_output_flags_t flags, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 172 | audio_io_handle_t *output, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 173 | audio_port_handle_t *portId, |
| 174 | audio_attributes_t attr) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 175 | audio_io_handle_t localOutput; |
| 176 | if (!output) output = &localOutput; |
| 177 | *output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 178 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 179 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 180 | config.sample_rate = sampleRate; |
| 181 | config.channel_mask = channelMask; |
| 182 | config.format = format; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 183 | audio_port_handle_t localPortId; |
| 184 | if (!portId) portId = &localPortId; |
| 185 | *portId = AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 186 | AudioPolicyInterface::output_type_t outputType; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 187 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 188 | &attr, output, AUDIO_SESSION_NONE, &stream, 0 /*uid*/, &config, &flags, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 189 | selectedDeviceId, portId, {}, &outputType)); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 190 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 191 | ASSERT_NE(AUDIO_IO_HANDLE_NONE, *output); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 192 | } |
| 193 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 194 | void AudioPolicyManagerTest::getInputForAttr( |
| 195 | const audio_attributes_t &attr, |
| 196 | audio_unique_id_t riid, |
| 197 | audio_port_handle_t *selectedDeviceId, |
| 198 | audio_format_t format, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 199 | audio_channel_mask_t channelMask, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 200 | int sampleRate, |
| 201 | audio_input_flags_t flags, |
| 202 | audio_port_handle_t *portId) { |
| 203 | audio_io_handle_t input = AUDIO_PORT_HANDLE_NONE; |
| 204 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 205 | config.sample_rate = sampleRate; |
| 206 | config.channel_mask = channelMask; |
| 207 | config.format = format; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 208 | audio_port_handle_t localPortId; |
| 209 | if (!portId) portId = &localPortId; |
| 210 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 211 | AudioPolicyInterface::input_type_t inputType; |
| 212 | ASSERT_EQ(OK, mManager->getInputForAttr( |
| 213 | &attr, &input, riid, AUDIO_SESSION_NONE, 0 /*uid*/, &config, flags, |
| 214 | selectedDeviceId, &inputType, portId)); |
| 215 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 216 | } |
| 217 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 218 | bool AudioPolicyManagerTest::findDevicePort(audio_port_role_t role, |
| 219 | audio_devices_t deviceType, const std::string &address, audio_port *foundPort) { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 220 | uint32_t numPorts = 0; |
| 221 | uint32_t generation1; |
| 222 | status_t ret; |
| 223 | |
| 224 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 225 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 226 | if (HasFailure()) return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 227 | |
| 228 | uint32_t generation2; |
| 229 | struct audio_port ports[numPorts]; |
| 230 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation2); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 231 | EXPECT_EQ(NO_ERROR, ret) << "mManager->listAudioPorts returned error"; |
| 232 | EXPECT_EQ(generation1, generation2) << "Generations changed during ports retrieval"; |
| 233 | if (HasFailure()) return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 234 | |
| 235 | for (const auto &port : ports) { |
| 236 | if (port.role == role && port.ext.device.type == deviceType && |
| 237 | (strncmp(port.ext.device.address, address.c_str(), |
| 238 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 239 | if (foundPort) *foundPort = port; |
| 240 | return true; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 243 | if (foundPort) { |
| 244 | ADD_FAILURE() << "Device port with role " << role << " and address " |
| 245 | << address << " not found"; |
| 246 | } |
| 247 | return false; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 248 | } |
| 249 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 250 | audio_port_handle_t AudioPolicyManagerTest::getDeviceIdFromPatch( |
| 251 | const struct audio_patch* patch) { |
| 252 | // The logic here is the same as the one in AudioIoDescriptor. |
| 253 | // Note this function is aim to get routed device id for test. |
| 254 | // In that case, device to device patch is not expected here. |
| 255 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 256 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 257 | return patch->sinks[0].id; |
| 258 | } else { |
| 259 | return patch->sources[0].id; |
| 260 | } |
| 261 | } |
| 262 | return AUDIO_PORT_HANDLE_NONE; |
| 263 | } |
| 264 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 265 | |
| 266 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 267 | // SetUp must finish with no assertions. |
| 268 | } |
| 269 | |
| 270 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 271 | dumpToLog(); |
| 272 | } |
| 273 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 274 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 275 | audio_patch patch{}; |
| 276 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 277 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 278 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 279 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 280 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 281 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 282 | patch.num_sinks = 1; |
| 283 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 284 | patch.num_sources = 1; |
| 285 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 286 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 287 | patch.num_sources = 2; |
| 288 | patch.num_sinks = 1; |
| 289 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 290 | patch = {}; |
| 291 | patch.num_sources = 1; |
| 292 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 293 | patch.num_sinks = 1; |
| 294 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 295 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 296 | patch = {}; |
| 297 | patch.num_sources = 1; |
| 298 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 299 | patch.num_sinks = 1; |
| 300 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 301 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 302 | // Verify that the handle is left unchanged. |
| 303 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 304 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 308 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 309 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 310 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 311 | ASSERT_FALSE(mManager->getAvailableInputDevices().isEmpty()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 312 | PatchBuilder patchBuilder; |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 313 | patchBuilder.addSource(mManager->getAvailableInputDevices()[0]). |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 314 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 315 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 316 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 317 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // TODO: Add patch creation tests that involve already existing patch |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 321 | |
| 322 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest { |
| 323 | protected: |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 324 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 325 | void TearDown() override; |
| 326 | |
| 327 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 328 | sp<DeviceDescriptor> mMsdInputDevice; |
| 329 | }; |
| 330 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 331 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 332 | // TODO: Consider using Serializer to load part of the config from a string. |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 333 | AudioPolicyManagerTest::SetUpManagerConfig(); |
| 334 | AudioPolicyConfig& config = mManager->getConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 335 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 336 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 337 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 338 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 339 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 340 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 341 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
| 342 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 343 | // Match output profile from AudioPolicyConfig::setDefault. |
| 344 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 345 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 346 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 347 | config.addDevice(mMsdOutputDevice); |
| 348 | config.addDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 349 | |
| 350 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 351 | HwModuleCollection modules = config.getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 352 | modules.add(msdModule); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 353 | config.setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 354 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 355 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 356 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 357 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 358 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 359 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 360 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 361 | msdCompressedOutputProfile->setFlags( |
| 362 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 363 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 364 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 365 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
| 366 | |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 367 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 368 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 369 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 370 | msdModule->addInputProfile(msdInputProfile); |
| 371 | |
| 372 | // Add a profile with another encoding to the default device to test routing |
| 373 | // of streams that are not supported by MSD. |
| 374 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 375 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 376 | config.getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 377 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 378 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 379 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 380 | primaryEncodedOutputProfile->addSupportedDevice(config.getDefaultOutputDevice()); |
| 381 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 382 | addOutputProfile(primaryEncodedOutputProfile); |
| 383 | } |
| 384 | |
| 385 | void AudioPolicyManagerTestMsd::TearDown() { |
| 386 | mMsdOutputDevice.clear(); |
| 387 | mMsdInputDevice.clear(); |
| 388 | AudioPolicyManagerTest::TearDown(); |
| 389 | } |
| 390 | |
| 391 | TEST_F(AudioPolicyManagerTestMsd, InitSuccess) { |
| 392 | ASSERT_TRUE(mMsdOutputDevice); |
| 393 | ASSERT_TRUE(mMsdInputDevice); |
| 394 | } |
| 395 | |
| 396 | TEST_F(AudioPolicyManagerTestMsd, Dump) { |
| 397 | dumpToLog(); |
| 398 | } |
| 399 | |
| 400 | TEST_F(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 401 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 402 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 403 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 404 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 408 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 409 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 410 | getOutputForAttr(&selectedDeviceId, |
| 411 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 412 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 413 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 417 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 418 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 419 | getOutputForAttr(&selectedDeviceId, |
| 420 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 421 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 422 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 426 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 427 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 428 | getOutputForAttr(&selectedDeviceId, |
| 429 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 430 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 431 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 432 | getOutputForAttr(&selectedDeviceId, |
| 433 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 434 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 435 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 439 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 440 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 441 | getOutputForAttr(&selectedDeviceId, |
| 442 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 443 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 444 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
| 448 | // Switch between formats that are supported and not supported by MSD. |
| 449 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 450 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 451 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 452 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 453 | getOutputForAttr(&selectedDeviceId, |
| 454 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 455 | nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 456 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 457 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 458 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 459 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 460 | } |
| 461 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 462 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 463 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 464 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 465 | getOutputForAttr(&selectedDeviceId, |
| 466 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 467 | nullptr /*output*/, &portId); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 468 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 469 | ASSERT_EQ(-1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 470 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 471 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 472 | } |
| 473 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 474 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 475 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 476 | getOutputForAttr(&selectedDeviceId, |
| 477 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 478 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 479 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 482 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 483 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 484 | protected: |
| 485 | void SetUpManagerConfig() override; |
| 486 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 487 | |
| 488 | static const std::string sExecutableDir; |
| 489 | static const std::string sDefaultConfig; |
| 490 | }; |
| 491 | |
| 492 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 493 | base::GetExecutableDirectory() + "/"; |
| 494 | |
| 495 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 496 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 497 | |
| 498 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
| 499 | status_t status = deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 500 | ASSERT_EQ(NO_ERROR, status); |
| 501 | } |
| 502 | |
| 503 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 504 | // SetUp must finish with no assertions. |
| 505 | } |
| 506 | |
| 507 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 508 | dumpToLog(); |
| 509 | } |
| 510 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 511 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 512 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 513 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 514 | protected: |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 515 | void TearDown() override; |
| 516 | |
| 517 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 518 | std::string mixAddress, const audio_config_t& audioConfig, |
| 519 | const std::vector<PolicyMixTuple>& rules); |
| 520 | void clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 521 | |
| 522 | Vector<AudioMix> mAudioMixes; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 523 | const std::string mMixAddress = "remote_submix_media"; |
| 524 | }; |
| 525 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 526 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 527 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 528 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 532 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
| 533 | const std::vector<PolicyMixTuple>& rules) { |
| 534 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 535 | |
| 536 | for(const auto &rule: rules) { |
| 537 | myMixMatchCriteria.add(AudioMixMatchCriterion( |
| 538 | std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 539 | } |
| 540 | |
| 541 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 542 | String8(mixAddress.c_str()), 0); |
| 543 | myAudioMix.mDeviceType = deviceType; |
| 544 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 545 | mAudioMixes.clear(); |
| 546 | mAudioMixes.add(myAudioMix); |
| 547 | |
| 548 | // As the policy mixes registration may fail at some case, |
| 549 | // caller need to check the returned status. |
| 550 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 555 | if (mManager != nullptr) { |
| 556 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 557 | } |
| 558 | mAudioMixes.clear(); |
| 559 | } |
| 560 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 561 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 562 | // SetUp must finish with no assertions |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 566 | dumpToLog(); |
| 567 | } |
| 568 | |
| 569 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 570 | status_t ret; |
| 571 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 572 | |
| 573 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 574 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 575 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 576 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 577 | |
| 578 | // Fail due to the device is already connected. |
| 579 | clearPolicyMix(); |
| 580 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 581 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 582 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 583 | |
| 584 | // The first time to register policy mixes with valid parameter should succeed. |
| 585 | clearPolicyMix(); |
| 586 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 587 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 588 | audioConfig.sample_rate = 48000; |
| 589 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 590 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 591 | std::vector<PolicyMixTuple>()); |
| 592 | ASSERT_EQ(NO_ERROR, ret); |
| 593 | // Registering the same policy mixes should fail. |
| 594 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 595 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 596 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 597 | // Registration should fail due to device not found. |
| 598 | // Note that earpiece is not present in the test configuration file. |
| 599 | // 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] | 600 | clearPolicyMix(); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 601 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 602 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 603 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 604 | |
| 605 | // Registration should fail due to output not found. |
| 606 | clearPolicyMix(); |
| 607 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 608 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 609 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 610 | |
| 611 | // The first time to register valid policy mixes should succeed. |
| 612 | clearPolicyMix(); |
| 613 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 614 | AUDIO_DEVICE_OUT_SPEAKER, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 615 | ASSERT_EQ(NO_ERROR, ret); |
| 616 | // Registering the same policy mixes should fail. |
| 617 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 618 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 619 | } |
| 620 | |
| 621 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 622 | status_t ret; |
| 623 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 624 | |
| 625 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 626 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 627 | audioConfig.sample_rate = 48000; |
| 628 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 629 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 630 | std::vector<PolicyMixTuple>()); |
| 631 | ASSERT_EQ(NO_ERROR, ret); |
| 632 | |
| 633 | // After successfully registering policy mixes, it should be able to unregister. |
| 634 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 635 | ASSERT_EQ(NO_ERROR, ret); |
| 636 | |
| 637 | // After unregistering policy mixes successfully, it should fail unregistering |
| 638 | // the same policy mixes as they are not registered. |
| 639 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 640 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 641 | } |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 642 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 643 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 644 | protected: |
| 645 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 646 | |
| 647 | static const std::string sPrimaryOnlyConfig; |
| 648 | }; |
| 649 | |
| 650 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 651 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 652 | |
| 653 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 654 | // SetUp must finish with no assertions. |
| 655 | } |
| 656 | |
| 657 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 658 | dumpToLog(); |
| 659 | } |
| 660 | |
| 661 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 662 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 663 | status_t ret; |
| 664 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 665 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 666 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 667 | audioConfig.sample_rate = 48000; |
| 668 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 669 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 670 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 671 | |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 672 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 673 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 674 | } |
| 675 | |
| 676 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
| 677 | public testing::WithParamInterface<audio_attributes_t> { |
| 678 | protected: |
| 679 | void SetUp() override; |
| 680 | void TearDown() override; |
| 681 | |
| 682 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 683 | |
| 684 | std::vector<PolicyMixTuple> mUsageRules = { |
| 685 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 686 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE} |
| 687 | }; |
| 688 | |
| 689 | struct audio_port mInjectionPort; |
| 690 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 691 | }; |
| 692 | |
| 693 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
| 694 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 695 | |
| 696 | mTracker.reset(new RecordingActivityTracker()); |
| 697 | |
| 698 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 699 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 700 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 701 | audioConfig.sample_rate = 48000; |
| 702 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 703 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, mUsageRules); |
| 704 | ASSERT_EQ(NO_ERROR, ret); |
| 705 | |
| 706 | struct audio_port extractionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 707 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 708 | mMixAddress, &extractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 709 | |
| 710 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 711 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 712 | audio_attributes_t attr = { |
| 713 | AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 714 | std::string tags = "addr=" + mMixAddress; |
| 715 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 716 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, |
| 717 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &mPortId); |
| 718 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 719 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 720 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 721 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 722 | mMixAddress, &mInjectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 726 | mManager->stopInput(mPortId); |
| 727 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 728 | } |
| 729 | |
| 730 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, InitSuccess) { |
| 731 | // SetUp must finish with no assertions |
| 732 | } |
| 733 | |
| 734 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, Dump) { |
| 735 | dumpToLog(); |
| 736 | } |
| 737 | |
| 738 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
| 739 | const audio_attributes_t attr = GetParam(); |
| 740 | const audio_usage_t usage = attr.usage; |
| 741 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 742 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 743 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 744 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, |
| 745 | nullptr /*output*/, nullptr /*portId*/, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 746 | if (std::find_if(begin(mUsageRules), end(mUsageRules), [&usage](const auto &usageRule) { |
| 747 | return (std::get<0>(usageRule) == usage) && |
| 748 | (std::get<2>(usageRule) == RULE_MATCH_ATTRIBUTE_USAGE);}) != end(mUsageRules) || |
| 749 | (strncmp(attr.tags, "addr=", strlen("addr=")) == 0 && |
| 750 | strncmp(attr.tags + strlen("addr="), mMixAddress.c_str(), |
| 751 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0)) { |
| 752 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 753 | } else { |
| 754 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | INSTANTIATE_TEST_CASE_P( |
| 759 | PlaybackReroutingUsageMatch, |
| 760 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 761 | testing::Values( |
| 762 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 763 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 764 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 765 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 766 | ) |
| 767 | ); |
| 768 | |
| 769 | INSTANTIATE_TEST_CASE_P( |
| 770 | PlaybackReroutingAddressPriorityMatch, |
| 771 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 772 | testing::Values( |
| 773 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 774 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 775 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 776 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 777 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 778 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 779 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 780 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 781 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 782 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 783 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 784 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 785 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 786 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 787 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 788 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 789 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 790 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 791 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 792 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 793 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 794 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 795 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 796 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 797 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 798 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 799 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 800 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 801 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 802 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 803 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 804 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 805 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 806 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 807 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 808 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 809 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VIRTUAL_SOURCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 810 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 811 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 812 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 813 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 814 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 815 | ) |
| 816 | ); |
| 817 | |
| 818 | INSTANTIATE_TEST_CASE_P( |
| 819 | PlaybackReroutingUnHandledUsages, |
| 820 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 821 | testing::Values( |
| 822 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 823 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 824 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 825 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 826 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 827 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 828 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 829 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 830 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 831 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 832 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 833 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 834 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 835 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 836 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 837 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 838 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 839 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 840 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 841 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 842 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 843 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 844 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 845 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 846 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 847 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 848 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 849 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 850 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 851 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 852 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 853 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 854 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 855 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}, |
Baekgyeong Kim | 47ea671 | 2019-10-30 20:29:41 +0900 | [diff] [blame] | 856 | (audio_attributes_t){AUDIO_CONTENT_TYPE_SPEECH, AUDIO_USAGE_ASSISTANT, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 857 | AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 858 | ) |
| 859 | ); |
| 860 | |
| 861 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
| 862 | public testing::WithParamInterface<audio_attributes_t> { |
| 863 | protected: |
| 864 | void SetUp() override; |
| 865 | void TearDown() override; |
| 866 | |
| 867 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 868 | |
| 869 | std::vector<PolicyMixTuple> mSourceRules = { |
| 870 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 871 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 872 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} |
| 873 | }; |
| 874 | |
| 875 | struct audio_port mExtractionPort; |
| 876 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 877 | }; |
| 878 | |
| 879 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
| 880 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 881 | |
| 882 | mTracker.reset(new RecordingActivityTracker()); |
| 883 | |
| 884 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 885 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 886 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 887 | audioConfig.sample_rate = 48000; |
| 888 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 889 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, mSourceRules); |
| 890 | ASSERT_EQ(NO_ERROR, ret); |
| 891 | |
| 892 | struct audio_port injectionPort; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 893 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 894 | mMixAddress, &injectionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 895 | |
| 896 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 897 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 898 | audio_attributes_t attr = |
| 899 | {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, AUDIO_FLAG_NONE, ""}; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 900 | std::string tags = std::string("addr=") + mMixAddress; |
| 901 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 902 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 903 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, nullptr /*output*/, &mPortId, attr); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 904 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 905 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 906 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 907 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 908 | mMixAddress, &mExtractionPort)); |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 912 | mManager->stopOutput(mPortId); |
| 913 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 914 | } |
| 915 | |
| 916 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, InitSuccess) { |
| 917 | // SetUp mush finish with no assertions. |
| 918 | } |
| 919 | |
| 920 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, Dump) { |
| 921 | dumpToLog(); |
| 922 | } |
| 923 | |
| 924 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
| 925 | const audio_attributes_t attr = GetParam(); |
| 926 | const audio_source_t source = attr.source; |
| 927 | |
jiabin | 7c0205e | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 928 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 929 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 930 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 931 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 932 | if (std::find_if(begin(mSourceRules), end(mSourceRules), [&source](const auto &sourceRule) { |
| 933 | return (std::get<1>(sourceRule) == source) && |
| 934 | (std::get<2>(sourceRule) == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET);}) |
| 935 | != end(mSourceRules)) { |
| 936 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 937 | } else { |
| 938 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | // No address priority rule for remote recording, address is a "don't care" |
| 943 | INSTANTIATE_TEST_CASE_P( |
| 944 | RecordInjectionSourceMatch, |
| 945 | AudioPolicyManagerTestDPMixRecordInjection, |
| 946 | testing::Values( |
| 947 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 948 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 949 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 950 | AUDIO_SOURCE_CAMCORDER, AUDIO_FLAG_NONE, |
| 951 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 952 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 953 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, |
| 954 | "addr=remote_submix_media"}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 955 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 956 | AUDIO_SOURCE_MIC, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 957 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 958 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 959 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 960 | AUDIO_SOURCE_VOICE_COMMUNICATION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 961 | "addr=remote_submix_media"} |
| 962 | ) |
| 963 | ); |
| 964 | |
| 965 | // No address priority rule for remote recording |
| 966 | INSTANTIATE_TEST_CASE_P( |
| 967 | RecordInjectionSourceNotMatch, |
| 968 | AudioPolicyManagerTestDPMixRecordInjection, |
| 969 | testing::Values( |
| 970 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 971 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 972 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 973 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, ""}, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 974 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 975 | AUDIO_SOURCE_VOICE_RECOGNITION, AUDIO_FLAG_NONE, |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 976 | "addr=remote_submix_media"}, |
| 977 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame^] | 978 | AUDIO_SOURCE_HOTWORD, AUDIO_FLAG_NONE, |
| 979 | "addr=remote_submix_media"} |
jiabin | f4eb15a | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 980 | ) |
| 981 | ); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 982 | |
| 983 | using DeviceConnectionTestParams = |
| 984 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 985 | |
| 986 | class AudioPolicyManagerTestDeviceConnection : public AudioPolicyManagerTestWithConfigurationFile, |
| 987 | public testing::WithParamInterface<DeviceConnectionTestParams> { |
| 988 | }; |
| 989 | |
| 990 | TEST_F(AudioPolicyManagerTestDeviceConnection, InitSuccess) { |
| 991 | // SetUp must finish with no assertions. |
| 992 | } |
| 993 | |
| 994 | TEST_F(AudioPolicyManagerTestDeviceConnection, Dump) { |
| 995 | dumpToLog(); |
| 996 | } |
| 997 | |
| 998 | TEST_P(AudioPolicyManagerTestDeviceConnection, SetDeviceConnectionState) { |
| 999 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1000 | const std::string name = std::get<1>(GetParam()); |
| 1001 | const std::string address = std::get<2>(GetParam()); |
| 1002 | |
| 1003 | if (type == AUDIO_DEVICE_OUT_HDMI) { |
| 1004 | // Set device connection state failed due to no device descriptor found |
| 1005 | // For HDMI case, it is easier to simulate device descriptor not found error |
| 1006 | // by using a undeclared encoded format. |
| 1007 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1008 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1009 | address.c_str(), name.c_str(), AUDIO_FORMAT_MAT_2_1)); |
| 1010 | } |
| 1011 | // Connect with valid parameters should succeed |
| 1012 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1013 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1014 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1015 | // Try to connect with the same device again should fail |
| 1016 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1017 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1018 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1019 | // Disconnect the connected device should succeed |
| 1020 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1021 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1022 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1023 | // Disconnect device that is not connected should fail |
| 1024 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1025 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1026 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1027 | // Try to set device connection state with a invalid connection state should fail |
| 1028 | ASSERT_EQ(BAD_VALUE, mManager->setDeviceConnectionState( |
| 1029 | type, AUDIO_POLICY_DEVICE_STATE_CNT, |
| 1030 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1031 | } |
| 1032 | |
| 1033 | TEST_P(AudioPolicyManagerTestDeviceConnection, ExplicitlyRoutingAfterConnection) { |
| 1034 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1035 | const std::string name = std::get<1>(GetParam()); |
| 1036 | const std::string address = std::get<2>(GetParam()); |
| 1037 | |
| 1038 | // Connect device to do explicitly routing test |
| 1039 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1040 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1041 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1042 | |
| 1043 | audio_port devicePort; |
| 1044 | const audio_port_role_t role = audio_is_output_device(type) |
| 1045 | ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1046 | ASSERT_TRUE(findDevicePort(role, type, address, &devicePort)); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1047 | |
| 1048 | audio_port_handle_t routedPortId = devicePort.id; |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1049 | // Try start input or output according to the device type |
| 1050 | if (audio_is_output_devices(type)) { |
| 1051 | getOutputForAttr(&routedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1052 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1053 | } else if (audio_is_input_device(type)) { |
| 1054 | RecordingActivityTracker tracker; |
| 1055 | getInputForAttr({}, tracker.getRiid(), &routedPortId, AUDIO_FORMAT_PCM_16_BIT, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1056 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE); |
jiabin | 43848a5 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 1057 | } |
| 1058 | ASSERT_EQ(devicePort.id, routedPortId); |
| 1059 | |
| 1060 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1061 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1062 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1063 | } |
| 1064 | |
| 1065 | INSTANTIATE_TEST_CASE_P( |
| 1066 | DeviceConnectionState, |
| 1067 | AudioPolicyManagerTestDeviceConnection, |
| 1068 | testing::Values( |
| 1069 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_HDMI, "test_in_hdmi", |
| 1070 | "audio_policy_test_in_hdmi"}), |
| 1071 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_HDMI, "test_out_hdmi", |
| 1072 | "audio_policy_test_out_hdmi"}), |
| 1073 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "bt_hfp_in", |
| 1074 | "hfp_client_in"}), |
| 1075 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "bt_hfp_out", |
| 1076 | "hfp_client_out"}) |
| 1077 | ) |
| 1078 | ); |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1079 | |
| 1080 | class AudioPolicyManagerTVTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1081 | protected: |
| 1082 | std::string getConfigFile() override { return sTvConfig; } |
| 1083 | void testHDMIPortSelection(audio_output_flags_t flags, const char* expectedMixPortName); |
| 1084 | |
| 1085 | static const std::string sTvConfig; |
| 1086 | }; |
| 1087 | |
| 1088 | const std::string AudioPolicyManagerTVTest::sTvConfig = |
| 1089 | AudioPolicyManagerTVTest::sExecutableDir + "test_tv_apm_configuration.xml"; |
| 1090 | |
| 1091 | // SwAudioOutputDescriptor doesn't populate flags so check against the port name. |
| 1092 | void AudioPolicyManagerTVTest::testHDMIPortSelection( |
| 1093 | audio_output_flags_t flags, const char* expectedMixPortName) { |
| 1094 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1095 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1096 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1097 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 1098 | audio_io_handle_t output; |
| 1099 | audio_port_handle_t portId; |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1100 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000, |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1101 | flags, &output, &portId); |
| 1102 | sp<SwAudioOutputDescriptor> outDesc = mManager->getOutputs().valueFor(output); |
| 1103 | ASSERT_NE(nullptr, outDesc.get()); |
| 1104 | audio_port port = {}; |
| 1105 | outDesc->toAudioPort(&port); |
| 1106 | mManager->releaseOutput(portId); |
| 1107 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1108 | AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1109 | "" /*address*/, "" /*name*/, AUDIO_FORMAT_DEFAULT)); |
| 1110 | ASSERT_EQ(AUDIO_PORT_TYPE_MIX, port.type); |
| 1111 | ASSERT_EQ(AUDIO_PORT_ROLE_SOURCE, port.role); |
| 1112 | ASSERT_STREQ(expectedMixPortName, port.name); |
| 1113 | } |
| 1114 | |
| 1115 | TEST_F(AudioPolicyManagerTVTest, InitSuccess) { |
| 1116 | // SetUp must finish with no assertions. |
| 1117 | } |
| 1118 | |
| 1119 | TEST_F(AudioPolicyManagerTVTest, Dump) { |
| 1120 | dumpToLog(); |
| 1121 | } |
| 1122 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1123 | TEST_F(AudioPolicyManagerTVTest, MatchNoFlags) { |
| 1124 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_NONE, "primary output"); |
| 1125 | } |
| 1126 | |
| 1127 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectNoHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1128 | // b/140447125: The selected port must not have HW AV Sync flag (see the config file). |
| 1129 | testHDMIPortSelection(AUDIO_OUTPUT_FLAG_DIRECT, "direct"); |
| 1130 | } |
| 1131 | |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1132 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectHwAvSync) { |
Mikhail Naganov | 0756bbf | 2019-09-06 13:53:26 -0700 | [diff] [blame] | 1133 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1134 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_HW_AV_SYNC), |
| 1135 | "tunnel"); |
| 1136 | } |
Mikhail Naganov | 57ac2ce | 2019-09-11 09:29:41 -0700 | [diff] [blame] | 1137 | |
| 1138 | TEST_F(AudioPolicyManagerTVTest, MatchOutputDirectMMapNoIrq) { |
| 1139 | testHDMIPortSelection(static_cast<audio_output_flags_t>( |
| 1140 | AUDIO_OUTPUT_FLAG_DIRECT|AUDIO_OUTPUT_FLAG_MMAP_NOIRQ), |
| 1141 | "low latency"); |
| 1142 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 1143 | |
| 1144 | class AudioPolicyManagerDynamicHwModulesTest : public AudioPolicyManagerTestWithConfigurationFile { |
| 1145 | protected: |
| 1146 | void SetUpManagerConfig() override; |
| 1147 | }; |
| 1148 | |
| 1149 | void AudioPolicyManagerDynamicHwModulesTest::SetUpManagerConfig() { |
| 1150 | AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig(); |
| 1151 | // Only allow successful opening of "primary" hw module during APM initialization. |
| 1152 | mClient->swapAllowedModuleNames({"primary"}); |
| 1153 | } |
| 1154 | |
| 1155 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, InitSuccess) { |
| 1156 | // SetUp must finish with no assertions. |
| 1157 | } |
| 1158 | |
| 1159 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, DynamicAddition) { |
| 1160 | const auto handleBefore = mClient->peekNextModuleHandle(); |
| 1161 | mManager->onNewAudioModulesAvailable(); |
| 1162 | ASSERT_EQ(handleBefore, mClient->peekNextModuleHandle()); |
| 1163 | // Reset module loading restrictions. |
| 1164 | mClient->swapAllowedModuleNames(); |
| 1165 | mManager->onNewAudioModulesAvailable(); |
| 1166 | const auto handleAfter = mClient->peekNextModuleHandle(); |
| 1167 | ASSERT_GT(handleAfter, handleBefore); |
| 1168 | mManager->onNewAudioModulesAvailable(); |
| 1169 | ASSERT_EQ(handleAfter, mClient->peekNextModuleHandle()); |
| 1170 | } |
| 1171 | |
| 1172 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, AddedDeviceAvailable) { |
| 1173 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, mManager->getDeviceConnectionState( |
| 1174 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1175 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1176 | mManager->onNewAudioModulesAvailable(); |
| 1177 | ASSERT_EQ(AUDIO_POLICY_DEVICE_STATE_AVAILABLE, mManager->getDeviceConnectionState( |
| 1178 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0")); |
| 1179 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 1180 | |
| 1181 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ListAddedAudioPorts) { |
| 1182 | ASSERT_FALSE( |
| 1183 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", nullptr)); |
| 1184 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1185 | mManager->onNewAudioModulesAvailable(); |
| 1186 | struct audio_port port; |
| 1187 | ASSERT_TRUE(findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, "0", &port)); |
| 1188 | } |
| 1189 | |
| 1190 | TEST_F(AudioPolicyManagerDynamicHwModulesTest, ClientIsUpdated) { |
| 1191 | const size_t prevAudioPortListUpdateCount = mClient->getAudioPortListUpdateCount(); |
| 1192 | const uint32_t prevAudioPortGeneration = mManager->getAudioPortGeneration(); |
| 1193 | mClient->swapAllowedModuleNames({"primary", "r_submix"}); |
| 1194 | mManager->onNewAudioModulesAvailable(); |
| 1195 | EXPECT_GT(mClient->getAudioPortListUpdateCount(), prevAudioPortListUpdateCount); |
| 1196 | EXPECT_GT(mManager->getAudioPortGeneration(), prevAudioPortGeneration); |
| 1197 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 1198 | |
| 1199 | using DevicesRoleForCapturePresetParam = std::tuple<audio_source_t, device_role_t>; |
| 1200 | |
| 1201 | class AudioPolicyManagerDevicesRoleForCapturePresetTest |
| 1202 | : public AudioPolicyManagerTestWithConfigurationFile, |
| 1203 | public testing::WithParamInterface<DevicesRoleForCapturePresetParam> { |
| 1204 | protected: |
| 1205 | // The `inputDevice` and `inputDevice2` indicate the audio devices type to be used for setting |
| 1206 | // device role. They must be declared in the test_audio_policy_configuration.xml |
| 1207 | AudioDeviceTypeAddr inputDevice = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_BUILTIN_MIC, ""); |
| 1208 | AudioDeviceTypeAddr inputDevice2 = AudioDeviceTypeAddr(AUDIO_DEVICE_IN_HDMI, ""); |
| 1209 | }; |
| 1210 | |
| 1211 | TEST_P(AudioPolicyManagerDevicesRoleForCapturePresetTest, DevicesRoleForCapturePreset) { |
| 1212 | const audio_source_t audioSource = std::get<0>(GetParam()); |
| 1213 | const device_role_t role = std::get<1>(GetParam()); |
| 1214 | |
| 1215 | // Test invalid device when setting |
| 1216 | const AudioDeviceTypeAddr outputDevice(AUDIO_DEVICE_OUT_SPEAKER, ""); |
| 1217 | const AudioDeviceTypeAddrVector outputDevices = {outputDevice}; |
| 1218 | ASSERT_EQ(BAD_VALUE, |
| 1219 | mManager->setDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1220 | ASSERT_EQ(BAD_VALUE, |
| 1221 | mManager->addDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1222 | AudioDeviceTypeAddrVector devices; |
| 1223 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1224 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1225 | ASSERT_TRUE(devices.empty()); |
| 1226 | ASSERT_EQ(BAD_VALUE, |
| 1227 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, outputDevices)); |
| 1228 | |
| 1229 | // Without setting, call get/remove/clear must fail |
| 1230 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1231 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1232 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1233 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, devices)); |
| 1234 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1235 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1236 | |
| 1237 | // Test set/get devices role |
| 1238 | const AudioDeviceTypeAddrVector inputDevices = {inputDevice}; |
| 1239 | ASSERT_EQ(NO_ERROR, |
| 1240 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1241 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1242 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice)); |
| 1243 | |
| 1244 | // Test setting will change the previously set devices |
| 1245 | const AudioDeviceTypeAddrVector inputDevices2 = {inputDevice2}; |
| 1246 | ASSERT_EQ(NO_ERROR, |
| 1247 | mManager->setDevicesRoleForCapturePreset(audioSource, role, inputDevices2)); |
| 1248 | devices.clear(); |
| 1249 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1250 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1251 | |
| 1252 | // Test add devices |
| 1253 | ASSERT_EQ(NO_ERROR, |
| 1254 | mManager->addDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1255 | devices.clear(); |
| 1256 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1257 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice, inputDevice2)); |
| 1258 | |
| 1259 | // Test remove devices |
| 1260 | ASSERT_EQ(NO_ERROR, |
| 1261 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1262 | devices.clear(); |
| 1263 | ASSERT_EQ(NO_ERROR, mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1264 | EXPECT_THAT(devices, UnorderedElementsAre(inputDevice2)); |
| 1265 | |
| 1266 | // Test remove devices that are not set as the device role |
| 1267 | ASSERT_EQ(BAD_VALUE, |
| 1268 | mManager->removeDevicesRoleForCapturePreset(audioSource, role, inputDevices)); |
| 1269 | |
| 1270 | // Test clear devices |
| 1271 | ASSERT_EQ(NO_ERROR, |
| 1272 | mManager->clearDevicesRoleForCapturePreset(audioSource, role)); |
| 1273 | devices.clear(); |
| 1274 | ASSERT_EQ(NAME_NOT_FOUND, |
| 1275 | mManager->getDevicesForRoleAndCapturePreset(audioSource, role, devices)); |
| 1276 | } |
| 1277 | |
| 1278 | INSTANTIATE_TEST_CASE_P( |
| 1279 | DevicesRoleForCapturePresetOperation, |
| 1280 | AudioPolicyManagerDevicesRoleForCapturePresetTest, |
| 1281 | testing::Values( |
| 1282 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_MIC, DEVICE_ROLE_PREFERRED}), |
| 1283 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_UPLINK, |
| 1284 | DEVICE_ROLE_PREFERRED}), |
| 1285 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_DOWNLINK, |
| 1286 | DEVICE_ROLE_PREFERRED}), |
| 1287 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_CALL, DEVICE_ROLE_PREFERRED}), |
| 1288 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_CAMCORDER, DEVICE_ROLE_PREFERRED}), |
| 1289 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_RECOGNITION, |
| 1290 | DEVICE_ROLE_PREFERRED}), |
| 1291 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_COMMUNICATION, |
| 1292 | DEVICE_ROLE_PREFERRED}), |
| 1293 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_REMOTE_SUBMIX, |
| 1294 | DEVICE_ROLE_PREFERRED}), |
| 1295 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_UNPROCESSED, DEVICE_ROLE_PREFERRED}), |
| 1296 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_VOICE_PERFORMANCE, |
| 1297 | DEVICE_ROLE_PREFERRED}), |
| 1298 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_ECHO_REFERENCE, |
| 1299 | DEVICE_ROLE_PREFERRED}), |
| 1300 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_FM_TUNER, DEVICE_ROLE_PREFERRED}), |
| 1301 | DevicesRoleForCapturePresetParam({AUDIO_SOURCE_HOTWORD, DEVICE_ROLE_PREFERRED}) |
| 1302 | ) |
| 1303 | ); |