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 | 723ed13 | 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> |
| 23 | |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 24 | #define LOG_TAG "APM_Test" |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 25 | #include <Serializer.h> |
| 26 | #include <android-base/file.h> |
| 27 | #include <media/AudioPolicy.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 28 | #include <media/PatchBuilder.h> |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 29 | #include <media/RecordingActivityTracker.h> |
| 30 | #include <utils/Log.h> |
| 31 | #include <utils/Vector.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 32 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 33 | #include "AudioPolicyInterface.h" |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame^] | 34 | #include "AudioPolicyManagerTestClient.h" |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 35 | #include "AudioPolicyTestClient.h" |
| 36 | #include "AudioPolicyTestManager.h" |
| 37 | |
| 38 | using namespace android; |
| 39 | |
Mikhail Naganov | e13c679 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 40 | TEST(AudioPolicyManagerTestInit, EngineFailure) { |
| 41 | AudioPolicyTestClient client; |
| 42 | AudioPolicyTestManager manager(&client); |
| 43 | manager.getConfig().setDefault(); |
| 44 | manager.getConfig().setEngineLibraryNameSuffix("non-existent"); |
| 45 | ASSERT_EQ(NO_INIT, manager.initialize()); |
| 46 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 47 | } |
| 48 | |
| 49 | TEST(AudioPolicyManagerTestInit, ClientFailure) { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 50 | AudioPolicyTestClient client; |
| 51 | AudioPolicyTestManager manager(&client); |
| 52 | manager.getConfig().setDefault(); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 53 | // Since the default client fails to open anything, |
| 54 | // APM should indicate that the initialization didn't succeed. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 55 | ASSERT_EQ(NO_INIT, manager.initialize()); |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 56 | ASSERT_EQ(NO_INIT, manager.initCheck()); |
| 57 | } |
| 58 | |
| 59 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 60 | class PatchCountCheck { |
| 61 | public: |
| 62 | explicit PatchCountCheck(AudioPolicyManagerTestClient *client) |
| 63 | : mClient{client}, |
| 64 | mInitialCount{mClient->getActivePatchesCount()} {} |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 65 | int deltaFromSnapshot() const { |
| 66 | size_t currentCount = mClient->getActivePatchesCount(); |
| 67 | if (mInitialCount <= currentCount) { |
| 68 | return currentCount - mInitialCount; |
| 69 | } else { |
| 70 | return -(static_cast<int>(mInitialCount - currentCount)); |
| 71 | } |
| 72 | } |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 73 | private: |
| 74 | const AudioPolicyManagerTestClient *mClient; |
| 75 | const size_t mInitialCount; |
| 76 | }; |
| 77 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 78 | class AudioPolicyManagerTest : public testing::Test { |
| 79 | protected: |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 80 | void SetUp() override; |
| 81 | void TearDown() override; |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 82 | virtual void SetUpManagerConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 83 | |
| 84 | void dumpToLog(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 85 | // When explicitly routing is needed, selectedDeviceId need to be set as the wanted port |
| 86 | // id. Otherwise, selectedDeviceId need to be initialized as AUDIO_PORT_HANDLE_NONE. |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 87 | void getOutputForAttr( |
| 88 | audio_port_handle_t *selectedDeviceId, |
| 89 | audio_format_t format, |
| 90 | int channelMask, |
| 91 | int sampleRate, |
| 92 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE, |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 93 | audio_port_handle_t *portId = nullptr, |
| 94 | audio_attributes_t attr = {}); |
| 95 | void getInputForAttr( |
| 96 | const audio_attributes_t &attr, |
| 97 | audio_unique_id_t riid, |
| 98 | audio_port_handle_t *selectedDeviceId, |
| 99 | audio_format_t format, |
| 100 | int channelMask, |
| 101 | int sampleRate, |
| 102 | audio_input_flags_t flags = AUDIO_INPUT_FLAG_NONE, |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 103 | audio_port_handle_t *portId = nullptr); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 104 | PatchCountCheck snapshotPatchCount() { return PatchCountCheck(mClient.get()); } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 105 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 106 | void findDevicePort(audio_port_role_t role, audio_devices_t deviceType, |
| 107 | const std::string &address, audio_port &foundPort); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 108 | static audio_port_handle_t getDeviceIdFromPatch(const struct audio_patch* patch); |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 109 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 110 | std::unique_ptr<AudioPolicyManagerTestClient> mClient; |
| 111 | std::unique_ptr<AudioPolicyTestManager> mManager; |
| 112 | }; |
| 113 | |
| 114 | void AudioPolicyManagerTest::SetUp() { |
| 115 | mClient.reset(new AudioPolicyManagerTestClient); |
| 116 | mManager.reset(new AudioPolicyTestManager(mClient.get())); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 117 | SetUpManagerConfig(); // Subclasses may want to customize the config. |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 118 | ASSERT_EQ(NO_ERROR, mManager->initialize()); |
| 119 | ASSERT_EQ(NO_ERROR, mManager->initCheck()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 120 | } |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 121 | |
| 122 | void AudioPolicyManagerTest::TearDown() { |
| 123 | mManager.reset(); |
| 124 | mClient.reset(); |
| 125 | } |
| 126 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 127 | void AudioPolicyManagerTest::SetUpManagerConfig() { |
| 128 | mManager->getConfig().setDefault(); |
| 129 | } |
| 130 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 131 | void AudioPolicyManagerTest::dumpToLog() { |
Mikhail Naganov | 21b4336 | 2018-06-04 10:37:09 -0700 | [diff] [blame] | 132 | int pipefd[2]; |
| 133 | ASSERT_NE(-1, pipe(pipefd)); |
| 134 | pid_t cpid = fork(); |
| 135 | ASSERT_NE(-1, cpid); |
| 136 | if (cpid == 0) { |
| 137 | // Child process reads from the pipe and logs. |
| 138 | close(pipefd[1]); |
| 139 | std::string line; |
| 140 | char buf; |
| 141 | while (read(pipefd[0], &buf, sizeof(buf)) > 0) { |
| 142 | if (buf != '\n') { |
| 143 | line += buf; |
| 144 | } else { |
| 145 | ALOGI("%s", line.c_str()); |
| 146 | line = ""; |
| 147 | } |
| 148 | } |
| 149 | if (!line.empty()) ALOGI("%s", line.c_str()); |
| 150 | close(pipefd[0]); |
| 151 | _exit(EXIT_SUCCESS); |
| 152 | } else { |
| 153 | // Parent does the dump and checks the status code. |
| 154 | close(pipefd[0]); |
| 155 | ASSERT_EQ(NO_ERROR, mManager->dump(pipefd[1])); |
| 156 | close(pipefd[1]); |
| 157 | wait(NULL); // Wait for the child to exit. |
| 158 | } |
| 159 | } |
| 160 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 161 | void AudioPolicyManagerTest::getOutputForAttr( |
| 162 | audio_port_handle_t *selectedDeviceId, |
| 163 | audio_format_t format, |
| 164 | int channelMask, |
| 165 | int sampleRate, |
| 166 | audio_output_flags_t flags, |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 167 | audio_port_handle_t *portId, |
| 168 | audio_attributes_t attr) { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 169 | audio_io_handle_t output = AUDIO_PORT_HANDLE_NONE; |
| 170 | audio_stream_type_t stream = AUDIO_STREAM_DEFAULT; |
| 171 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 172 | config.sample_rate = sampleRate; |
| 173 | config.channel_mask = channelMask; |
| 174 | config.format = format; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 175 | audio_port_handle_t localPortId; |
| 176 | if (!portId) portId = &localPortId; |
| 177 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 178 | ASSERT_EQ(OK, mManager->getOutputForAttr( |
| 179 | &attr, &output, AUDIO_SESSION_NONE, &stream, 0 /*uid*/, &config, &flags, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 180 | selectedDeviceId, portId, {})); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 181 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 182 | } |
| 183 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 184 | void AudioPolicyManagerTest::getInputForAttr( |
| 185 | const audio_attributes_t &attr, |
| 186 | audio_unique_id_t riid, |
| 187 | audio_port_handle_t *selectedDeviceId, |
| 188 | audio_format_t format, |
| 189 | int channelMask, |
| 190 | int sampleRate, |
| 191 | audio_input_flags_t flags, |
| 192 | audio_port_handle_t *portId) { |
| 193 | audio_io_handle_t input = AUDIO_PORT_HANDLE_NONE; |
| 194 | audio_config_base_t config = AUDIO_CONFIG_BASE_INITIALIZER; |
| 195 | config.sample_rate = sampleRate; |
| 196 | config.channel_mask = channelMask; |
| 197 | config.format = format; |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 198 | audio_port_handle_t localPortId; |
| 199 | if (!portId) portId = &localPortId; |
| 200 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 201 | AudioPolicyInterface::input_type_t inputType; |
| 202 | ASSERT_EQ(OK, mManager->getInputForAttr( |
| 203 | &attr, &input, riid, AUDIO_SESSION_NONE, 0 /*uid*/, &config, flags, |
| 204 | selectedDeviceId, &inputType, portId)); |
| 205 | ASSERT_NE(AUDIO_PORT_HANDLE_NONE, *portId); |
| 206 | } |
| 207 | |
| 208 | void AudioPolicyManagerTest::findDevicePort(audio_port_role_t role, |
| 209 | audio_devices_t deviceType, const std::string &address, audio_port &foundPort) { |
| 210 | uint32_t numPorts = 0; |
| 211 | uint32_t generation1; |
| 212 | status_t ret; |
| 213 | |
| 214 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, nullptr, &generation1); |
| 215 | ASSERT_EQ(NO_ERROR, ret); |
| 216 | |
| 217 | uint32_t generation2; |
| 218 | struct audio_port ports[numPorts]; |
| 219 | ret = mManager->listAudioPorts(role, AUDIO_PORT_TYPE_DEVICE, &numPorts, ports, &generation2); |
| 220 | ASSERT_EQ(NO_ERROR, ret); |
| 221 | ASSERT_EQ(generation1, generation2); |
| 222 | |
| 223 | for (const auto &port : ports) { |
| 224 | if (port.role == role && port.ext.device.type == deviceType && |
| 225 | (strncmp(port.ext.device.address, address.c_str(), |
| 226 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 227 | foundPort = port; |
| 228 | return; |
| 229 | } |
| 230 | } |
| 231 | GTEST_FAIL(); |
| 232 | } |
| 233 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 234 | audio_port_handle_t AudioPolicyManagerTest::getDeviceIdFromPatch( |
| 235 | const struct audio_patch* patch) { |
| 236 | // The logic here is the same as the one in AudioIoDescriptor. |
| 237 | // Note this function is aim to get routed device id for test. |
| 238 | // In that case, device to device patch is not expected here. |
| 239 | if (patch->num_sources != 0 && patch->num_sinks != 0) { |
| 240 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
| 241 | return patch->sinks[0].id; |
| 242 | } else { |
| 243 | return patch->sources[0].id; |
| 244 | } |
| 245 | } |
| 246 | return AUDIO_PORT_HANDLE_NONE; |
| 247 | } |
| 248 | |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 249 | |
| 250 | TEST_F(AudioPolicyManagerTest, InitSuccess) { |
| 251 | // SetUp must finish with no assertions. |
| 252 | } |
| 253 | |
| 254 | TEST_F(AudioPolicyManagerTest, Dump) { |
| 255 | dumpToLog(); |
| 256 | } |
| 257 | |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 258 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFailure) { |
| 259 | audio_patch patch{}; |
| 260 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 261 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 262 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(nullptr, &handle, 0)); |
| 263 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, nullptr, 0)); |
| 264 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 265 | patch.num_sources = AUDIO_PATCH_PORTS_MAX + 1; |
| 266 | patch.num_sinks = 1; |
| 267 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 268 | patch.num_sources = 1; |
| 269 | patch.num_sinks = AUDIO_PATCH_PORTS_MAX + 1; |
| 270 | ASSERT_EQ(BAD_VALUE, mManager->createAudioPatch(&patch, &handle, 0)); |
| 271 | patch.num_sources = 2; |
| 272 | patch.num_sinks = 1; |
| 273 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 274 | patch = {}; |
| 275 | patch.num_sources = 1; |
| 276 | patch.sources[0].role = AUDIO_PORT_ROLE_SINK; |
| 277 | patch.num_sinks = 1; |
| 278 | patch.sinks[0].role = AUDIO_PORT_ROLE_SINK; |
| 279 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 280 | patch = {}; |
| 281 | patch.num_sources = 1; |
| 282 | patch.sources[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 283 | patch.num_sinks = 1; |
| 284 | patch.sinks[0].role = AUDIO_PORT_ROLE_SOURCE; |
| 285 | ASSERT_EQ(INVALID_OPERATION, mManager->createAudioPatch(&patch, &handle, 0)); |
| 286 | // Verify that the handle is left unchanged. |
| 287 | ASSERT_EQ(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 288 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | TEST_F(AudioPolicyManagerTest, CreateAudioPatchFromMix) { |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 292 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 293 | uid_t uid = 42; |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 294 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 295 | ASSERT_FALSE(mManager->getConfig().getAvailableInputDevices().isEmpty()); |
| 296 | PatchBuilder patchBuilder; |
| 297 | patchBuilder.addSource(mManager->getConfig().getAvailableInputDevices()[0]). |
| 298 | addSink(mManager->getConfig().getDefaultOutputDevice()); |
| 299 | ASSERT_EQ(NO_ERROR, mManager->createAudioPatch(patchBuilder.patch(), &handle, uid)); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 300 | ASSERT_NE(AUDIO_PATCH_HANDLE_NONE, handle); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 301 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | 04a8663 | 2017-12-15 18:01:42 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // TODO: Add patch creation tests that involve already existing patch |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 305 | |
| 306 | class AudioPolicyManagerTestMsd : public AudioPolicyManagerTest { |
| 307 | protected: |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 308 | void SetUpManagerConfig() override; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 309 | void TearDown() override; |
| 310 | |
| 311 | sp<DeviceDescriptor> mMsdOutputDevice; |
| 312 | sp<DeviceDescriptor> mMsdInputDevice; |
| 313 | }; |
| 314 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 315 | void AudioPolicyManagerTestMsd::SetUpManagerConfig() { |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 316 | // TODO: Consider using Serializer to load part of the config from a string. |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 317 | AudioPolicyManagerTest::SetUpManagerConfig(); |
| 318 | AudioPolicyConfig& config = mManager->getConfig(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 319 | mMsdOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_BUS); |
| 320 | sp<AudioProfile> pcmOutputProfile = new AudioProfile( |
| 321 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 322 | sp<AudioProfile> ac3OutputProfile = new AudioProfile( |
| 323 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
| 324 | mMsdOutputDevice->addAudioProfile(pcmOutputProfile); |
| 325 | mMsdOutputDevice->addAudioProfile(ac3OutputProfile); |
| 326 | mMsdInputDevice = new DeviceDescriptor(AUDIO_DEVICE_IN_BUS); |
| 327 | // Match output profile from AudioPolicyConfig::setDefault. |
| 328 | sp<AudioProfile> pcmInputProfile = new AudioProfile( |
| 329 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_STEREO, 44100); |
| 330 | mMsdInputDevice->addAudioProfile(pcmInputProfile); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 331 | config.addAvailableDevice(mMsdOutputDevice); |
| 332 | config.addAvailableDevice(mMsdInputDevice); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 333 | |
| 334 | sp<HwModule> msdModule = new HwModule(AUDIO_HARDWARE_MODULE_ID_MSD, 2 /*halVersionMajor*/); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 335 | HwModuleCollection modules = config.getHwModules(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 336 | modules.add(msdModule); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 337 | config.setHwModules(modules); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 338 | mMsdOutputDevice->attach(msdModule); |
| 339 | mMsdInputDevice->attach(msdModule); |
| 340 | |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 341 | sp<OutputProfile> msdOutputProfile = new OutputProfile("msd input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 342 | msdOutputProfile->addAudioProfile(pcmOutputProfile); |
| 343 | msdOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 344 | msdModule->addOutputProfile(msdOutputProfile); |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 345 | sp<OutputProfile> msdCompressedOutputProfile = new OutputProfile("msd compressed input"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 346 | msdCompressedOutputProfile->addAudioProfile(ac3OutputProfile); |
| 347 | msdCompressedOutputProfile->setFlags( |
| 348 | AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 349 | AUDIO_OUTPUT_FLAG_NON_BLOCKING); |
| 350 | msdCompressedOutputProfile->addSupportedDevice(mMsdOutputDevice); |
| 351 | msdModule->addOutputProfile(msdCompressedOutputProfile); |
| 352 | |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 353 | sp<InputProfile> msdInputProfile = new InputProfile("msd output"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 354 | msdInputProfile->addAudioProfile(pcmInputProfile); |
| 355 | msdInputProfile->addSupportedDevice(mMsdInputDevice); |
| 356 | msdModule->addInputProfile(msdInputProfile); |
| 357 | |
| 358 | // Add a profile with another encoding to the default device to test routing |
| 359 | // of streams that are not supported by MSD. |
| 360 | sp<AudioProfile> dtsOutputProfile = new AudioProfile( |
| 361 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 362 | config.getDefaultOutputDevice()->addAudioProfile(dtsOutputProfile); |
jiabin | eaf09f0 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 363 | sp<OutputProfile> primaryEncodedOutputProfile = new OutputProfile("encoded"); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 364 | primaryEncodedOutputProfile->addAudioProfile(dtsOutputProfile); |
| 365 | primaryEncodedOutputProfile->setFlags(AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 366 | primaryEncodedOutputProfile->addSupportedDevice(config.getDefaultOutputDevice()); |
| 367 | config.getHwModules().getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)-> |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 368 | addOutputProfile(primaryEncodedOutputProfile); |
| 369 | } |
| 370 | |
| 371 | void AudioPolicyManagerTestMsd::TearDown() { |
| 372 | mMsdOutputDevice.clear(); |
| 373 | mMsdInputDevice.clear(); |
| 374 | AudioPolicyManagerTest::TearDown(); |
| 375 | } |
| 376 | |
| 377 | TEST_F(AudioPolicyManagerTestMsd, InitSuccess) { |
| 378 | ASSERT_TRUE(mMsdOutputDevice); |
| 379 | ASSERT_TRUE(mMsdInputDevice); |
| 380 | } |
| 381 | |
| 382 | TEST_F(AudioPolicyManagerTestMsd, Dump) { |
| 383 | dumpToLog(); |
| 384 | } |
| 385 | |
| 386 | TEST_F(AudioPolicyManagerTestMsd, PatchCreationOnSetForceUse) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 387 | const PatchCountCheck patchCount = snapshotPatchCount(); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 388 | mManager->setForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND, |
| 389 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 390 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 394 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 395 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 396 | getOutputForAttr(&selectedDeviceId, |
| 397 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 398 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 399 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 403 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 404 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 405 | getOutputForAttr(&selectedDeviceId, |
| 406 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 407 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 408 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrEncodedPlusPcmRoutesToMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 412 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 413 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 414 | getOutputForAttr(&selectedDeviceId, |
| 415 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 416 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 417 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 418 | getOutputForAttr(&selectedDeviceId, |
| 419 | AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, 48000); |
| 420 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 421 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrUnsupportedFormatBypassesMsd) { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 425 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 426 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 427 | getOutputForAttr(&selectedDeviceId, |
| 428 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 429 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 430 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | TEST_F(AudioPolicyManagerTestMsd, GetOutputForAttrFormatSwitching) { |
| 434 | // Switch between formats that are supported and not supported by MSD. |
| 435 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 436 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 437 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 438 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 439 | getOutputForAttr(&selectedDeviceId, |
| 440 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 441 | &portId); |
| 442 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 443 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 444 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 445 | ASSERT_EQ(1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 446 | } |
| 447 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 448 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 449 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 450 | audio_port_handle_t portId; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 451 | getOutputForAttr(&selectedDeviceId, |
| 452 | AUDIO_FORMAT_DTS, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT, |
| 453 | &portId); |
| 454 | ASSERT_NE(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 455 | ASSERT_EQ(-1, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 456 | mManager->releaseOutput(portId); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 457 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 458 | } |
| 459 | { |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 460 | const PatchCountCheck patchCount = snapshotPatchCount(); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 461 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 462 | getOutputForAttr(&selectedDeviceId, |
| 463 | AUDIO_FORMAT_AC3, AUDIO_CHANNEL_OUT_5POINT1, 48000, AUDIO_OUTPUT_FLAG_DIRECT); |
| 464 | ASSERT_EQ(selectedDeviceId, mMsdOutputDevice->getId()); |
Mikhail Naganov | d53525f | 2019-01-24 13:15:46 -0800 | [diff] [blame] | 465 | ASSERT_EQ(0, patchCount.deltaFromSnapshot()); |
Mikhail Naganov | df2e359 | 2018-12-19 14:27:42 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 468 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 469 | class AudioPolicyManagerTestWithConfigurationFile : public AudioPolicyManagerTest { |
| 470 | protected: |
| 471 | void SetUpManagerConfig() override; |
| 472 | virtual std::string getConfigFile() { return sDefaultConfig; } |
| 473 | |
| 474 | static const std::string sExecutableDir; |
| 475 | static const std::string sDefaultConfig; |
| 476 | }; |
| 477 | |
| 478 | const std::string AudioPolicyManagerTestWithConfigurationFile::sExecutableDir = |
| 479 | base::GetExecutableDirectory() + "/"; |
| 480 | |
| 481 | const std::string AudioPolicyManagerTestWithConfigurationFile::sDefaultConfig = |
| 482 | sExecutableDir + "test_audio_policy_configuration.xml"; |
| 483 | |
| 484 | void AudioPolicyManagerTestWithConfigurationFile::SetUpManagerConfig() { |
| 485 | status_t status = deserializeAudioPolicyFile(getConfigFile().c_str(), &mManager->getConfig()); |
| 486 | ASSERT_EQ(NO_ERROR, status); |
| 487 | } |
| 488 | |
| 489 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, InitSuccess) { |
| 490 | // SetUp must finish with no assertions. |
| 491 | } |
| 492 | |
| 493 | TEST_F(AudioPolicyManagerTestWithConfigurationFile, Dump) { |
| 494 | dumpToLog(); |
| 495 | } |
| 496 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 497 | using PolicyMixTuple = std::tuple<audio_usage_t, audio_source_t, uint32_t>; |
| 498 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 499 | class AudioPolicyManagerTestDynamicPolicy : public AudioPolicyManagerTestWithConfigurationFile { |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 500 | protected: |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 501 | void TearDown() override; |
| 502 | |
| 503 | status_t addPolicyMix(int mixType, int mixFlag, audio_devices_t deviceType, |
| 504 | std::string mixAddress, const audio_config_t& audioConfig, |
| 505 | const std::vector<PolicyMixTuple>& rules); |
| 506 | void clearPolicyMix(); |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 507 | |
| 508 | Vector<AudioMix> mAudioMixes; |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 509 | const std::string mMixAddress = "remote_submix_media"; |
| 510 | }; |
| 511 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 512 | void AudioPolicyManagerTestDynamicPolicy::TearDown() { |
| 513 | mManager->unregisterPolicyMixes(mAudioMixes); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 514 | AudioPolicyManagerTestWithConfigurationFile::TearDown(); |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | status_t AudioPolicyManagerTestDynamicPolicy::addPolicyMix(int mixType, int mixFlag, |
| 518 | audio_devices_t deviceType, std::string mixAddress, const audio_config_t& audioConfig, |
| 519 | const std::vector<PolicyMixTuple>& rules) { |
| 520 | Vector<AudioMixMatchCriterion> myMixMatchCriteria; |
| 521 | |
| 522 | for(const auto &rule: rules) { |
| 523 | myMixMatchCriteria.add(AudioMixMatchCriterion( |
| 524 | std::get<0>(rule), std::get<1>(rule), std::get<2>(rule))); |
| 525 | } |
| 526 | |
| 527 | AudioMix myAudioMix(myMixMatchCriteria, mixType, audioConfig, mixFlag, |
| 528 | String8(mixAddress.c_str()), 0); |
| 529 | myAudioMix.mDeviceType = deviceType; |
| 530 | // Clear mAudioMix before add new one to make sure we don't add already exist mixes. |
| 531 | mAudioMixes.clear(); |
| 532 | mAudioMixes.add(myAudioMix); |
| 533 | |
| 534 | // As the policy mixes registration may fail at some case, |
| 535 | // caller need to check the returned status. |
| 536 | status_t ret = mManager->registerPolicyMixes(mAudioMixes); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | void AudioPolicyManagerTestDynamicPolicy::clearPolicyMix() { |
| 541 | if (mManager != nullptr) { |
| 542 | mManager->unregisterPolicyMixes(mAudioMixes); |
| 543 | } |
| 544 | mAudioMixes.clear(); |
| 545 | } |
| 546 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 547 | TEST_F(AudioPolicyManagerTestDynamicPolicy, InitSuccess) { |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 548 | // SetUp must finish with no assertions |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | TEST_F(AudioPolicyManagerTestDynamicPolicy, Dump) { |
| 552 | dumpToLog(); |
| 553 | } |
| 554 | |
| 555 | TEST_F(AudioPolicyManagerTestDynamicPolicy, RegisterPolicyMixes) { |
| 556 | status_t ret; |
| 557 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 558 | |
| 559 | // Only capture of playback is allowed in LOOP_BACK &RENDER mode |
| 560 | ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER, |
| 561 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 562 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 563 | |
| 564 | // Fail due to the device is already connected. |
| 565 | clearPolicyMix(); |
| 566 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 567 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 568 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 569 | |
| 570 | // The first time to register policy mixes with valid parameter should succeed. |
| 571 | clearPolicyMix(); |
| 572 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 573 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 574 | audioConfig.sample_rate = 48000; |
| 575 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 576 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 577 | std::vector<PolicyMixTuple>()); |
| 578 | ASSERT_EQ(NO_ERROR, ret); |
| 579 | // Registering the same policy mixes should fail. |
| 580 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 581 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 582 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 583 | // Registration should fail due to device not found. |
| 584 | // Note that earpiece is not present in the test configuration file. |
| 585 | // This will need to be updated if earpiece is added in the test configuration file. |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 586 | clearPolicyMix(); |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 587 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 588 | AUDIO_DEVICE_OUT_EARPIECE, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 589 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 590 | |
| 591 | // Registration should fail due to output not found. |
| 592 | clearPolicyMix(); |
| 593 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 594 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 595 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 596 | |
| 597 | // The first time to register valid policy mixes should succeed. |
| 598 | clearPolicyMix(); |
| 599 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_RENDER, |
| 600 | AUDIO_DEVICE_OUT_SPEAKER, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 601 | ASSERT_EQ(NO_ERROR, ret); |
| 602 | // Registering the same policy mixes should fail. |
| 603 | ret = mManager->registerPolicyMixes(mAudioMixes); |
| 604 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 605 | } |
| 606 | |
| 607 | TEST_F(AudioPolicyManagerTestDynamicPolicy, UnregisterPolicyMixes) { |
| 608 | status_t ret; |
| 609 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 610 | |
| 611 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 612 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 613 | audioConfig.sample_rate = 48000; |
| 614 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 615 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, |
| 616 | std::vector<PolicyMixTuple>()); |
| 617 | ASSERT_EQ(NO_ERROR, ret); |
| 618 | |
| 619 | // After successfully registering policy mixes, it should be able to unregister. |
| 620 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 621 | ASSERT_EQ(NO_ERROR, ret); |
| 622 | |
| 623 | // After unregistering policy mixes successfully, it should fail unregistering |
| 624 | // the same policy mixes as they are not registered. |
| 625 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 626 | ASSERT_EQ(INVALID_OPERATION, ret); |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 627 | } |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 628 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 629 | class AudioPolicyManagerTestDPNoRemoteSubmixModule : public AudioPolicyManagerTestDynamicPolicy { |
| 630 | protected: |
| 631 | std::string getConfigFile() override { return sPrimaryOnlyConfig; } |
| 632 | |
| 633 | static const std::string sPrimaryOnlyConfig; |
| 634 | }; |
| 635 | |
| 636 | const std::string AudioPolicyManagerTestDPNoRemoteSubmixModule::sPrimaryOnlyConfig = |
| 637 | sExecutableDir + "test_audio_policy_primary_only_configuration.xml"; |
| 638 | |
| 639 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, InitSuccess) { |
| 640 | // SetUp must finish with no assertions. |
| 641 | } |
| 642 | |
| 643 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, Dump) { |
| 644 | dumpToLog(); |
| 645 | } |
| 646 | |
| 647 | TEST_F(AudioPolicyManagerTestDPNoRemoteSubmixModule, RegistrationFailure) { |
| 648 | // Registration/Unregistration should fail due to module for remote submix not found. |
| 649 | status_t ret; |
| 650 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 651 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 652 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 653 | audioConfig.sample_rate = 48000; |
| 654 | ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 655 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, "", audioConfig, std::vector<PolicyMixTuple>()); |
| 656 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 657 | |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 658 | ret = mManager->unregisterPolicyMixes(mAudioMixes); |
| 659 | ASSERT_EQ(INVALID_OPERATION, ret); |
| 660 | } |
| 661 | |
| 662 | class AudioPolicyManagerTestDPPlaybackReRouting : public AudioPolicyManagerTestDynamicPolicy, |
| 663 | public testing::WithParamInterface<audio_attributes_t> { |
| 664 | protected: |
| 665 | void SetUp() override; |
| 666 | void TearDown() override; |
| 667 | |
| 668 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 669 | |
| 670 | std::vector<PolicyMixTuple> mUsageRules = { |
| 671 | {AUDIO_USAGE_MEDIA, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE}, |
| 672 | {AUDIO_USAGE_ALARM, AUDIO_SOURCE_DEFAULT, RULE_MATCH_ATTRIBUTE_USAGE} |
| 673 | }; |
| 674 | |
| 675 | struct audio_port mInjectionPort; |
| 676 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 677 | }; |
| 678 | |
| 679 | void AudioPolicyManagerTestDPPlaybackReRouting::SetUp() { |
| 680 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 681 | |
| 682 | mTracker.reset(new RecordingActivityTracker()); |
| 683 | |
| 684 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 685 | audioConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 686 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 687 | audioConfig.sample_rate = 48000; |
| 688 | status_t ret = addPolicyMix(MIX_TYPE_PLAYERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 689 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, mMixAddress, audioConfig, mUsageRules); |
| 690 | ASSERT_EQ(NO_ERROR, ret); |
| 691 | |
| 692 | struct audio_port extractionPort; |
| 693 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 694 | mMixAddress, extractionPort); |
| 695 | |
| 696 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 697 | audio_source_t source = AUDIO_SOURCE_REMOTE_SUBMIX; |
| 698 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, source, 0, ""}; |
| 699 | std::string tags = "addr=" + mMixAddress; |
| 700 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 701 | getInputForAttr(attr, mTracker->getRiid(), &selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, |
| 702 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &mPortId); |
| 703 | ASSERT_EQ(NO_ERROR, mManager->startInput(mPortId)); |
| 704 | ASSERT_EQ(extractionPort.id, selectedDeviceId); |
| 705 | |
| 706 | findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 707 | mMixAddress, mInjectionPort); |
| 708 | } |
| 709 | |
| 710 | void AudioPolicyManagerTestDPPlaybackReRouting::TearDown() { |
| 711 | mManager->stopInput(mPortId); |
| 712 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 713 | } |
| 714 | |
| 715 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, InitSuccess) { |
| 716 | // SetUp must finish with no assertions |
| 717 | } |
| 718 | |
| 719 | TEST_F(AudioPolicyManagerTestDPPlaybackReRouting, Dump) { |
| 720 | dumpToLog(); |
| 721 | } |
| 722 | |
| 723 | TEST_P(AudioPolicyManagerTestDPPlaybackReRouting, PlaybackReRouting) { |
| 724 | const audio_attributes_t attr = GetParam(); |
| 725 | const audio_usage_t usage = attr.usage; |
| 726 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 727 | audio_port_handle_t playbackRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 728 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 729 | getOutputForAttr(&playbackRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 730 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, &portId, attr); |
| 731 | if (std::find_if(begin(mUsageRules), end(mUsageRules), [&usage](const auto &usageRule) { |
| 732 | return (std::get<0>(usageRule) == usage) && |
| 733 | (std::get<2>(usageRule) == RULE_MATCH_ATTRIBUTE_USAGE);}) != end(mUsageRules) || |
| 734 | (strncmp(attr.tags, "addr=", strlen("addr=")) == 0 && |
| 735 | strncmp(attr.tags + strlen("addr="), mMixAddress.c_str(), |
| 736 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0)) { |
| 737 | EXPECT_EQ(mInjectionPort.id, playbackRoutedPortId); |
| 738 | } else { |
| 739 | EXPECT_NE(mInjectionPort.id, playbackRoutedPortId); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | INSTANTIATE_TEST_CASE_P( |
| 744 | PlaybackReroutingUsageMatch, |
| 745 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 746 | testing::Values( |
| 747 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
| 748 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 749 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
| 750 | AUDIO_SOURCE_DEFAULT, 0, ""} |
| 751 | ) |
| 752 | ); |
| 753 | |
| 754 | INSTANTIATE_TEST_CASE_P( |
| 755 | PlaybackReroutingAddressPriorityMatch, |
| 756 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 757 | testing::Values( |
| 758 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_MEDIA, |
| 759 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 760 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
| 761 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 762 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 763 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 764 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 765 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ALARM, |
| 766 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 767 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
| 768 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 769 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 770 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 771 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 772 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 773 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 774 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 775 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 776 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 777 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 778 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 779 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 780 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 781 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
| 782 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 783 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 784 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 785 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 786 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 787 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 788 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 789 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 790 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 791 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 792 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
| 793 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 794 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VIRTUAL_SOURCE, |
| 795 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"}, |
| 796 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
| 797 | AUDIO_SOURCE_DEFAULT, 0, "addr=remote_submix_media"} |
| 798 | ) |
| 799 | ); |
| 800 | |
| 801 | INSTANTIATE_TEST_CASE_P( |
| 802 | PlaybackReroutingUnHandledUsages, |
| 803 | AudioPolicyManagerTestDPPlaybackReRouting, |
| 804 | testing::Values( |
| 805 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_VOICE_COMMUNICATION, |
| 806 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 807 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 808 | AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING, |
| 809 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 810 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION, |
| 811 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 812 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 813 | AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE, |
| 814 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 815 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 816 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST, |
| 817 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 818 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 819 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT, |
| 820 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 821 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 822 | AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED, |
| 823 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 824 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_NOTIFICATION_EVENT, |
| 825 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 826 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 827 | AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY, |
| 828 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 829 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 830 | AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, |
| 831 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 832 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, |
| 833 | AUDIO_USAGE_ASSISTANCE_SONIFICATION, |
| 834 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 835 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_GAME, |
| 836 | AUDIO_SOURCE_DEFAULT, 0, ""}, |
| 837 | (audio_attributes_t){AUDIO_CONTENT_TYPE_MUSIC, AUDIO_USAGE_ASSISTANT, |
| 838 | AUDIO_SOURCE_DEFAULT, 0, ""} |
| 839 | ) |
| 840 | ); |
| 841 | |
| 842 | class AudioPolicyManagerTestDPMixRecordInjection : public AudioPolicyManagerTestDynamicPolicy, |
| 843 | public testing::WithParamInterface<audio_attributes_t> { |
| 844 | protected: |
| 845 | void SetUp() override; |
| 846 | void TearDown() override; |
| 847 | |
| 848 | std::unique_ptr<RecordingActivityTracker> mTracker; |
| 849 | |
| 850 | std::vector<PolicyMixTuple> mSourceRules = { |
| 851 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_CAMCORDER, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 852 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_MIC, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}, |
| 853 | {AUDIO_USAGE_UNKNOWN, AUDIO_SOURCE_VOICE_COMMUNICATION, RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} |
| 854 | }; |
| 855 | |
| 856 | struct audio_port mExtractionPort; |
| 857 | audio_port_handle_t mPortId = AUDIO_PORT_HANDLE_NONE; |
| 858 | }; |
| 859 | |
| 860 | void AudioPolicyManagerTestDPMixRecordInjection::SetUp() { |
| 861 | AudioPolicyManagerTestDynamicPolicy::SetUp(); |
| 862 | |
| 863 | mTracker.reset(new RecordingActivityTracker()); |
| 864 | |
| 865 | audio_config_t audioConfig = AUDIO_CONFIG_INITIALIZER; |
| 866 | audioConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 867 | audioConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 868 | audioConfig.sample_rate = 48000; |
| 869 | status_t ret = addPolicyMix(MIX_TYPE_RECORDERS, MIX_ROUTE_FLAG_LOOP_BACK, |
| 870 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, mMixAddress, audioConfig, mSourceRules); |
| 871 | ASSERT_EQ(NO_ERROR, ret); |
| 872 | |
| 873 | struct audio_port injectionPort; |
| 874 | findDevicePort(AUDIO_PORT_ROLE_SINK, AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 875 | mMixAddress, injectionPort); |
| 876 | |
| 877 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 878 | audio_usage_t usage = AUDIO_USAGE_VIRTUAL_SOURCE; |
| 879 | audio_attributes_t attr = {AUDIO_CONTENT_TYPE_UNKNOWN, usage, AUDIO_SOURCE_DEFAULT, 0, ""}; |
| 880 | std::string tags = std::string("addr=") + mMixAddress; |
| 881 | strncpy(attr.tags, tags.c_str(), AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1); |
| 882 | getOutputForAttr(&selectedDeviceId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 883 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, &mPortId, attr); |
| 884 | ASSERT_EQ(NO_ERROR, mManager->startOutput(mPortId)); |
| 885 | ASSERT_EQ(injectionPort.id, getDeviceIdFromPatch(mClient->getLastAddedPatch())); |
| 886 | |
| 887 | findDevicePort(AUDIO_PORT_ROLE_SOURCE, AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 888 | mMixAddress, mExtractionPort); |
| 889 | } |
| 890 | |
| 891 | void AudioPolicyManagerTestDPMixRecordInjection::TearDown() { |
| 892 | mManager->stopOutput(mPortId); |
| 893 | AudioPolicyManagerTestDynamicPolicy::TearDown(); |
| 894 | } |
| 895 | |
| 896 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, InitSuccess) { |
| 897 | // SetUp mush finish with no assertions. |
| 898 | } |
| 899 | |
| 900 | TEST_F(AudioPolicyManagerTestDPMixRecordInjection, Dump) { |
| 901 | dumpToLog(); |
| 902 | } |
| 903 | |
| 904 | TEST_P(AudioPolicyManagerTestDPMixRecordInjection, RecordingInjection) { |
| 905 | const audio_attributes_t attr = GetParam(); |
| 906 | const audio_source_t source = attr.source; |
| 907 | |
jiabin | 890df85 | 2019-09-05 10:26:04 -0700 | [diff] [blame] | 908 | audio_port_handle_t captureRoutedPortId = AUDIO_PORT_HANDLE_NONE; |
jiabin | 723ed13 | 2019-08-28 15:31:47 -0700 | [diff] [blame] | 909 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 910 | getInputForAttr(attr, mTracker->getRiid(), &captureRoutedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 911 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 912 | if (std::find_if(begin(mSourceRules), end(mSourceRules), [&source](const auto &sourceRule) { |
| 913 | return (std::get<1>(sourceRule) == source) && |
| 914 | (std::get<2>(sourceRule) == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET);}) |
| 915 | != end(mSourceRules)) { |
| 916 | EXPECT_EQ(mExtractionPort.id, captureRoutedPortId); |
| 917 | } else { |
| 918 | EXPECT_NE(mExtractionPort.id, captureRoutedPortId); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // No address priority rule for remote recording, address is a "don't care" |
| 923 | INSTANTIATE_TEST_CASE_P( |
| 924 | RecordInjectionSourceMatch, |
| 925 | AudioPolicyManagerTestDPMixRecordInjection, |
| 926 | testing::Values( |
| 927 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 928 | AUDIO_SOURCE_CAMCORDER, 0, ""}, |
| 929 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 930 | AUDIO_SOURCE_CAMCORDER, 0, "addr=remote_submix_media"}, |
| 931 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 932 | AUDIO_SOURCE_MIC, 0, "addr=remote_submix_media"}, |
| 933 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 934 | AUDIO_SOURCE_MIC, 0, ""}, |
| 935 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 936 | AUDIO_SOURCE_VOICE_COMMUNICATION, 0, ""}, |
| 937 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 938 | AUDIO_SOURCE_VOICE_COMMUNICATION, 0, |
| 939 | "addr=remote_submix_media"} |
| 940 | ) |
| 941 | ); |
| 942 | |
| 943 | // No address priority rule for remote recording |
| 944 | INSTANTIATE_TEST_CASE_P( |
| 945 | RecordInjectionSourceNotMatch, |
| 946 | AudioPolicyManagerTestDPMixRecordInjection, |
| 947 | testing::Values( |
| 948 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 949 | AUDIO_SOURCE_VOICE_RECOGNITION, 0, ""}, |
| 950 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 951 | AUDIO_SOURCE_HOTWORD, 0, ""}, |
| 952 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 953 | AUDIO_SOURCE_VOICE_RECOGNITION, 0, |
| 954 | "addr=remote_submix_media"}, |
| 955 | (audio_attributes_t){AUDIO_CONTENT_TYPE_UNKNOWN, AUDIO_USAGE_UNKNOWN, |
| 956 | AUDIO_SOURCE_HOTWORD, 0, "addr=remote_submix_media"} |
| 957 | ) |
| 958 | ); |
jiabin | f9bd900 | 2019-09-05 14:07:25 -0700 | [diff] [blame] | 959 | |
| 960 | using DeviceConnectionTestParams = |
| 961 | std::tuple<audio_devices_t /*type*/, std::string /*name*/, std::string /*address*/>; |
| 962 | |
| 963 | class AudioPolicyManagerTestDeviceConnection : public AudioPolicyManagerTestWithConfigurationFile, |
| 964 | public testing::WithParamInterface<DeviceConnectionTestParams> { |
| 965 | }; |
| 966 | |
| 967 | TEST_F(AudioPolicyManagerTestDeviceConnection, InitSuccess) { |
| 968 | // SetUp must finish with no assertions. |
| 969 | } |
| 970 | |
| 971 | TEST_F(AudioPolicyManagerTestDeviceConnection, Dump) { |
| 972 | dumpToLog(); |
| 973 | } |
| 974 | |
| 975 | TEST_P(AudioPolicyManagerTestDeviceConnection, SetDeviceConnectionState) { |
| 976 | const audio_devices_t type = std::get<0>(GetParam()); |
| 977 | const std::string name = std::get<1>(GetParam()); |
| 978 | const std::string address = std::get<2>(GetParam()); |
| 979 | |
| 980 | if (type == AUDIO_DEVICE_OUT_HDMI) { |
| 981 | // Set device connection state failed due to no device descriptor found |
| 982 | // For HDMI case, it is easier to simulate device descriptor not found error |
| 983 | // by using a undeclared encoded format. |
| 984 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 985 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 986 | address.c_str(), name.c_str(), AUDIO_FORMAT_MAT_2_1)); |
| 987 | } |
| 988 | // Connect with valid parameters should succeed |
| 989 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 990 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 991 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 992 | // Try to connect with the same device again should fail |
| 993 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 994 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 995 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 996 | // Disconnect the connected device should succeed |
| 997 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 998 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 999 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1000 | // Disconnect device that is not connected should fail |
| 1001 | ASSERT_EQ(INVALID_OPERATION, mManager->setDeviceConnectionState( |
| 1002 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1003 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1004 | // Try to set device connection state with a invalid connection state should fail |
| 1005 | ASSERT_EQ(BAD_VALUE, mManager->setDeviceConnectionState( |
| 1006 | type, AUDIO_POLICY_DEVICE_STATE_CNT, |
| 1007 | "", "", AUDIO_FORMAT_DEFAULT)); |
| 1008 | } |
| 1009 | |
| 1010 | TEST_P(AudioPolicyManagerTestDeviceConnection, ExplicitlyRoutingAfterConnection) { |
| 1011 | const audio_devices_t type = std::get<0>(GetParam()); |
| 1012 | const std::string name = std::get<1>(GetParam()); |
| 1013 | const std::string address = std::get<2>(GetParam()); |
| 1014 | |
| 1015 | // Connect device to do explicitly routing test |
| 1016 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1017 | type, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1018 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1019 | |
| 1020 | audio_port devicePort; |
| 1021 | const audio_port_role_t role = audio_is_output_device(type) |
| 1022 | ? AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE; |
| 1023 | findDevicePort(role, type, address, devicePort); |
| 1024 | |
| 1025 | audio_port_handle_t routedPortId = devicePort.id; |
| 1026 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 1027 | // Try start input or output according to the device type |
| 1028 | if (audio_is_output_devices(type)) { |
| 1029 | getOutputForAttr(&routedPortId, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_OUT_STEREO, |
| 1030 | 48000 /*sampleRate*/, AUDIO_OUTPUT_FLAG_NONE, &portId); |
| 1031 | } else if (audio_is_input_device(type)) { |
| 1032 | RecordingActivityTracker tracker; |
| 1033 | getInputForAttr({}, tracker.getRiid(), &routedPortId, AUDIO_FORMAT_PCM_16_BIT, |
| 1034 | AUDIO_CHANNEL_IN_STEREO, 48000 /*sampleRate*/, AUDIO_INPUT_FLAG_NONE, &portId); |
| 1035 | } |
| 1036 | ASSERT_EQ(devicePort.id, routedPortId); |
| 1037 | |
| 1038 | ASSERT_EQ(NO_ERROR, mManager->setDeviceConnectionState( |
| 1039 | type, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1040 | address.c_str(), name.c_str(), AUDIO_FORMAT_DEFAULT)); |
| 1041 | } |
| 1042 | |
| 1043 | INSTANTIATE_TEST_CASE_P( |
| 1044 | DeviceConnectionState, |
| 1045 | AudioPolicyManagerTestDeviceConnection, |
| 1046 | testing::Values( |
| 1047 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_HDMI, "test_in_hdmi", |
| 1048 | "audio_policy_test_in_hdmi"}), |
| 1049 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_HDMI, "test_out_hdmi", |
| 1050 | "audio_policy_test_out_hdmi"}), |
| 1051 | DeviceConnectionTestParams({AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, "bt_hfp_in", |
| 1052 | "hfp_client_in"}), |
| 1053 | DeviceConnectionTestParams({AUDIO_DEVICE_OUT_BLUETOOTH_SCO, "bt_hfp_out", |
| 1054 | "hfp_client_out"}) |
| 1055 | ) |
| 1056 | ); |