blob: 89386189c9ee1d45bf030ce1c0bfaae50a38aaf6 [file] [log] [blame]
Shunkai Yao67b1be62022-07-13 05:01:42 +00001/*
2 * Copyright (C) 2022 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
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000017#define LOG_TAG "VtsHalAudioEffectTargetTest"
18
Shunkai Yao812d5b42022-11-16 18:08:50 +000019#include <chrono>
Shunkai Yao45905172022-08-24 18:14:02 +000020#include <memory>
Shunkai Yao67b1be62022-07-13 05:01:42 +000021#include <string>
Shunkai Yao45905172022-08-24 18:14:02 +000022#include <vector>
Shunkai Yao67b1be62022-07-13 05:01:42 +000023
Shunkai Yao812d5b42022-11-16 18:08:50 +000024#include <Utils.h>
Shunkai Yao67b1be62022-07-13 05:01:42 +000025#include <aidl/Gtest.h>
26#include <aidl/Vintf.h>
Shunkai Yao812d5b42022-11-16 18:08:50 +000027#include <aidl/android/hardware/audio/effect/IEffect.h>
28#include <aidl/android/hardware/audio/effect/IFactory.h>
Shunkai Yao67b1be62022-07-13 05:01:42 +000029#include <android-base/logging.h>
Shunkai Yao67b1be62022-07-13 05:01:42 +000030#include <android/binder_interface_utils.h>
31#include <android/binder_manager.h>
32#include <android/binder_process.h>
Shunkai Yao812d5b42022-11-16 18:08:50 +000033#include <fmq/AidlMessageQueue.h>
Shunkai Yao67b1be62022-07-13 05:01:42 +000034
35#include "AudioHalBinderServiceUtil.h"
Shunkai Yao812d5b42022-11-16 18:08:50 +000036#include "EffectFactoryHelper.h"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000037#include "EffectHelper.h"
Shunkai Yao121c6dd2022-09-21 23:42:08 +000038#include "TestUtils.h"
Shunkai Yao67b1be62022-07-13 05:01:42 +000039
40using namespace android;
41
42using ndk::ScopedAStatus;
43
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000044using aidl::android::hardware::audio::effect::CommandId;
Shunkai Yao67b1be62022-07-13 05:01:42 +000045using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao45905172022-08-24 18:14:02 +000046using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao67b1be62022-07-13 05:01:42 +000047using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000048using aidl::android::hardware::audio::effect::Parameter;
49using aidl::android::hardware::audio::effect::State;
Shunkai Yao67b1be62022-07-13 05:01:42 +000050
Shunkai Yao812d5b42022-11-16 18:08:50 +000051enum ParamName { PARAM_INSTANCE_NAME };
Shunkai Yaocb0fc412022-12-15 20:34:32 +000052using EffectTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>>;
Shunkai Yao812d5b42022-11-16 18:08:50 +000053
54class AudioEffectTest : public testing::TestWithParam<EffectTestParam>, public EffectHelper {
Shunkai Yao45905172022-08-24 18:14:02 +000055 public:
Shunkai Yaocb0fc412022-12-15 20:34:32 +000056 AudioEffectTest() {
57 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
58 }
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000059
Shunkai Yao812d5b42022-11-16 18:08:50 +000060 void SetUp() override {}
Shunkai Yaocb0fc412022-12-15 20:34:32 +000061
62 void TearDown() override {
63 // Do the cleanup for every test case
64 if (mEffect) {
65 ASSERT_NO_FATAL_FAILURE(commandIgnoreRet(mEffect, CommandId::STOP));
66 ASSERT_NO_FATAL_FAILURE(closeIgnoreRet(mEffect));
67 ASSERT_NO_FATAL_FAILURE(destroyIgnoreRet(mFactory, mEffect));
68 mEffect.reset();
69 }
70 }
Shunkai Yao45905172022-08-24 18:14:02 +000071
Shunkai Yao812d5b42022-11-16 18:08:50 +000072 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
73 std::shared_ptr<IFactory> mFactory;
Shunkai Yaocb0fc412022-12-15 20:34:32 +000074 std::shared_ptr<IEffect> mEffect;
75 Descriptor mDescriptor;
Shunkai Yao45905172022-08-24 18:14:02 +000076};
77
Shunkai Yao812d5b42022-11-16 18:08:50 +000078TEST_P(AudioEffectTest, SetupAndTearDown) {
79 // Intentionally empty test body.
Shunkai Yao45905172022-08-24 18:14:02 +000080}
81
Shunkai Yao812d5b42022-11-16 18:08:50 +000082TEST_P(AudioEffectTest, CreateAndDestroy) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000083 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
84 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao45905172022-08-24 18:14:02 +000085}
86
Shunkai Yao812d5b42022-11-16 18:08:50 +000087TEST_P(AudioEffectTest, OpenAndClose) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000088 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
89 ASSERT_NO_FATAL_FAILURE(open(mEffect));
90 ASSERT_NO_FATAL_FAILURE(close(mEffect));
91 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao45905172022-08-24 18:14:02 +000092}
93
Shunkai Yao812d5b42022-11-16 18:08:50 +000094TEST_P(AudioEffectTest, CloseUnopenedEffect) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000095 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
96 ASSERT_NO_FATAL_FAILURE(close(mEffect));
97 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao45905172022-08-24 18:14:02 +000098}
99
Shunkai Yao812d5b42022-11-16 18:08:50 +0000100TEST_P(AudioEffectTest, DoubleOpenAndClose) {
101 std::shared_ptr<IEffect> effect1, effect2;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000102 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
103 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000104 ASSERT_NO_FATAL_FAILURE(open(effect1));
105 ASSERT_NO_FATAL_FAILURE(open(effect2, 1 /* session */));
106 ASSERT_NO_FATAL_FAILURE(close(effect1));
107 ASSERT_NO_FATAL_FAILURE(close(effect2));
108 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
109 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
Shunkai Yao45905172022-08-24 18:14:02 +0000110}
111
Shunkai Yao812d5b42022-11-16 18:08:50 +0000112TEST_P(AudioEffectTest, TripleOpenAndClose) {
113 std::shared_ptr<IEffect> effect1, effect2, effect3;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000114 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
115 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
116 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect3, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000117 ASSERT_NO_FATAL_FAILURE(open(effect1));
118 ASSERT_NO_FATAL_FAILURE(open(effect2, 1 /* session */));
119 ASSERT_NO_FATAL_FAILURE(open(effect3, 2 /* session */));
120 ASSERT_NO_FATAL_FAILURE(close(effect1));
121 ASSERT_NO_FATAL_FAILURE(close(effect2));
122 ASSERT_NO_FATAL_FAILURE(close(effect3));
123 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
124 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
125 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect3));
126}
Shunkai Yao45905172022-08-24 18:14:02 +0000127
Shunkai Yao812d5b42022-11-16 18:08:50 +0000128TEST_P(AudioEffectTest, GetDescritorBeforeOpen) {
Shunkai Yao812d5b42022-11-16 18:08:50 +0000129 Descriptor desc;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000130 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
131 ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, desc));
132 EXPECT_EQ(mDescriptor.common, desc.common);
133 // Effect implementation Must fill in implementor and name
Shunkai Yao812d5b42022-11-16 18:08:50 +0000134 EXPECT_NE("", desc.common.name);
135 EXPECT_NE("", desc.common.implementor);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000136 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000137}
138
139TEST_P(AudioEffectTest, GetDescritorAfterOpen) {
Shunkai Yao812d5b42022-11-16 18:08:50 +0000140 Descriptor beforeOpen, afterOpen, afterClose;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000141 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
142 ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, beforeOpen));
143 ASSERT_NO_FATAL_FAILURE(open(mEffect));
144 ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, afterOpen));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000145 EXPECT_EQ(beforeOpen.toString(), afterOpen.toString()) << "\n"
146 << beforeOpen.toString() << "\n"
147 << afterOpen.toString();
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000148 ASSERT_NO_FATAL_FAILURE(close(mEffect));
149 ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, afterClose));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000150 EXPECT_EQ(beforeOpen.toString(), afterClose.toString()) << "\n"
151 << beforeOpen.toString() << "\n"
152 << afterClose.toString();
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000153 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000154}
155
156TEST_P(AudioEffectTest, DescriptorExistAndUnique) {
Shunkai Yao812d5b42022-11-16 18:08:50 +0000157 Descriptor desc;
158
159 auto descList = EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor);
160 std::set<Descriptor::Identity> idSet;
161 for (const auto& it : descList) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000162 auto& id = it.second.common.id;
Shunkai Yao812d5b42022-11-16 18:08:50 +0000163 EXPECT_EQ(0ul, idSet.count(id));
164 idSet.insert(id);
Shunkai Yao45905172022-08-24 18:14:02 +0000165 }
Shunkai Yao812d5b42022-11-16 18:08:50 +0000166
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000167 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
168 ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, desc));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000169 EXPECT_EQ(1ul, idSet.count(desc.common.id));
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000170 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao45905172022-08-24 18:14:02 +0000171}
172
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000173/// State testing.
174// An effect instance is in INIT state by default after it was created.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000175TEST_P(AudioEffectTest, InitStateAfterCreation) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000176 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
177 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
178 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000179}
180
Shunkai Yao812d5b42022-11-16 18:08:50 +0000181// An effect instance transfer to IDLE state after IEffect.ASSERT_NO_FATAL_FAILURE(open().
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000182TEST_P(AudioEffectTest, IdleStateAfterOpen) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000183 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
184 ASSERT_NO_FATAL_FAILURE(open(mEffect));
185 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
186 ASSERT_NO_FATAL_FAILURE(close(mEffect));
187 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000188}
189
190// An effect instance is in PROCESSING state after it receive an START command.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000191TEST_P(AudioEffectTest, ProcessingStateAfterStart) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000192 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
193 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
194 ASSERT_NO_FATAL_FAILURE(open(mEffect));
195 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
196 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
197 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
198 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
199 ASSERT_NO_FATAL_FAILURE(close(mEffect));
200 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000201}
202
203// An effect instance transfer to IDLE state after Command.Id.STOP in PROCESSING state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000204TEST_P(AudioEffectTest, IdleStateAfterStop) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000205 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
206 ASSERT_NO_FATAL_FAILURE(open(mEffect));
207 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
208 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
209 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
210 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
211 ASSERT_NO_FATAL_FAILURE(close(mEffect));
212 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000213}
214
215// An effect instance transfer to IDLE state after Command.Id.RESET in PROCESSING state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000216TEST_P(AudioEffectTest, IdleStateAfterReset) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000217 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
218 ASSERT_NO_FATAL_FAILURE(open(mEffect));
219 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
220 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
221 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
222 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
223 ASSERT_NO_FATAL_FAILURE(close(mEffect));
224 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000225}
226
Shunkai Yao812d5b42022-11-16 18:08:50 +0000227// An effect instance transfer to INIT after IEffect.ASSERT_NO_FATAL_FAILURE(close().
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000228TEST_P(AudioEffectTest, InitStateAfterClose) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000229 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
230 ASSERT_NO_FATAL_FAILURE(open(mEffect));
231 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
232 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
233 ASSERT_NO_FATAL_FAILURE(close(mEffect));
234 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
235 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000236}
237
238// An effect instance shouldn't accept any command before open.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000239TEST_P(AudioEffectTest, NoCommandAcceptedBeforeOpen) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000240 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
241 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START, EX_ILLEGAL_STATE));
242 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP, EX_ILLEGAL_STATE));
243 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET, EX_ILLEGAL_STATE));
244 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000245}
246
247// No-op when receive STOP command in IDLE state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000248TEST_P(AudioEffectTest, StopCommandInIdleStateNoOp) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000249 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
250 ASSERT_NO_FATAL_FAILURE(open(mEffect));
251 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
252 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
253 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
254 ASSERT_NO_FATAL_FAILURE(close(mEffect));
255 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000256}
257
Shunkai Yao812d5b42022-11-16 18:08:50 +0000258// No-op when receive RESET command in IDLE state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000259TEST_P(AudioEffectTest, ResetCommandInIdleStateNoOp) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000260 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
261 ASSERT_NO_FATAL_FAILURE(open(mEffect));
262 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
263 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
264 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
265 ASSERT_NO_FATAL_FAILURE(close(mEffect));
266 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000267}
268
269// Repeat START and STOP command.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000270TEST_P(AudioEffectTest, RepeatStartAndStop) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000271 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
272 ASSERT_NO_FATAL_FAILURE(open(mEffect));
273 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
274 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
275 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
276 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
277 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000278
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000279 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
280 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
281 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
282 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
283 ASSERT_NO_FATAL_FAILURE(close(mEffect));
284 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000285}
286
287// Repeat START and RESET command.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000288TEST_P(AudioEffectTest, RepeatStartAndReset) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000289 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
290 ASSERT_NO_FATAL_FAILURE(open(mEffect));
291 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
292 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
293 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
294 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
295 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000296
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000297 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
298 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
299 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
300 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
301 ASSERT_NO_FATAL_FAILURE(close(mEffect));
302 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000303}
304
Shunkai Yao812d5b42022-11-16 18:08:50 +0000305// Try to close an effect instance at PROCESSING state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000306TEST_P(AudioEffectTest, CloseProcessingStateEffects) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000307 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
308 ASSERT_NO_FATAL_FAILURE(open(mEffect));
309 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
310 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
311 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000312
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000313 ASSERT_NO_FATAL_FAILURE(close(mEffect, EX_ILLEGAL_STATE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000314
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000315 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
316 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
317 ASSERT_NO_FATAL_FAILURE(close(mEffect));
318 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000319}
320
321// Expect EX_ILLEGAL_STATE if the effect instance is not in a proper state to be destroyed.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000322TEST_P(AudioEffectTest, DestroyOpenEffects) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000323 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
324 ASSERT_NO_FATAL_FAILURE(open(mEffect));
325 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000326
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000327 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect, EX_ILLEGAL_STATE));
328
329 // cleanup
330 ASSERT_NO_FATAL_FAILURE(close(mEffect));
331 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000332}
333
334// Expect EX_ILLEGAL_STATE if the effect instance is not in a proper state to be destroyed.
335TEST_P(AudioEffectTest, DestroyProcessingEffects) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000336 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
337 ASSERT_NO_FATAL_FAILURE(open(mEffect));
338 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
339 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
340 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000341
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000342 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect, EX_ILLEGAL_STATE));
343
344 // cleanup
345 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
346 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
347 ASSERT_NO_FATAL_FAILURE(close(mEffect));
348 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000349}
350
351TEST_P(AudioEffectTest, NormalSequenceStates) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000352 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
353 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
354 ASSERT_NO_FATAL_FAILURE(open(mEffect));
355 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
356 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
357 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
358 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
359 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
360 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
361 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
362 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
363 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
364 ASSERT_NO_FATAL_FAILURE(close(mEffect));
365 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000366}
367
368/// Parameter testing.
369// Verify parameters pass in open can be successfully get.
Shunkai Yao812d5b42022-11-16 18:08:50 +0000370TEST_P(AudioEffectTest, VerifyCommonParametersAfterOpen) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000371 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000372
373 Parameter::Common common = EffectHelper::createParamCommon();
374 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000375 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000376
377 Parameter get = Parameter(), expect = Parameter();
378 expect.set<Parameter::common>(common);
379 Parameter::Id id;
380 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000381 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000382 EXPECT_EQ(expect, get) << expect.toString() << " vs " << get.toString();
383
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000384 ASSERT_NO_FATAL_FAILURE(close(mEffect));
385 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000386}
387
388// Verify parameters pass in set can be successfully get.
Shunkai Yao812d5b42022-11-16 18:08:50 +0000389TEST_P(AudioEffectTest, SetAndGetCommonParameter) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000390 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
391 ASSERT_NO_FATAL_FAILURE(open(mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000392
393 Parameter::Common common = EffectHelper::createParamCommon(
394 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */);
395 Parameter get = Parameter(), set = Parameter();
396 set.set<Parameter::common>(common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000397 EXPECT_IS_OK(mEffect->setParameter(set));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000398
399 Parameter::Id id;
400 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000401 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000402 EXPECT_EQ(set, get) << set.toString() << " vs " << get.toString();
403
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000404 ASSERT_NO_FATAL_FAILURE(close(mEffect));
405 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000406}
407
Shunkai Yao812d5b42022-11-16 18:08:50 +0000408// Verify parameters set and get in PROCESSING state.
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000409TEST_P(AudioEffectTest, SetAndGetParameterInProcessing) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000410 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
411 ASSERT_NO_FATAL_FAILURE(open(mEffect));
412 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
413 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000414
415 Parameter::Common common = EffectHelper::createParamCommon(
416 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */);
417 Parameter get = Parameter(), set = Parameter();
418 set.set<Parameter::common>(common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000419 EXPECT_IS_OK(mEffect->setParameter(set));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000420
421 Parameter::Id id;
422 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000423 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000424 EXPECT_EQ(set, get) << set.toString() << " vs " << get.toString();
425
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000426 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
427 ASSERT_NO_FATAL_FAILURE(close(mEffect));
428 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000429}
430
Shunkai Yao812d5b42022-11-16 18:08:50 +0000431// Verify parameters set and get in IDLE state.
432TEST_P(AudioEffectTest, SetAndGetParameterInIdle) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000433 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
434 ASSERT_NO_FATAL_FAILURE(open(mEffect));
435 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
436 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
437 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
438 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000439
440 Parameter::Common common = EffectHelper::createParamCommon(
441 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */);
442 Parameter get = Parameter(), set = Parameter();
443 set.set<Parameter::common>(common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000444 EXPECT_IS_OK(mEffect->setParameter(set));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000445
446 Parameter::Id id;
447 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000448 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000449 EXPECT_EQ(set, get) << set.toString() << " vs " << get.toString();
450
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000451 ASSERT_NO_FATAL_FAILURE(close(mEffect));
452 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000453}
454
Shunkai Yao812d5b42022-11-16 18:08:50 +0000455// Verify Parameters kept after stop.
456TEST_P(AudioEffectTest, SetAndGetParameterAfterStop) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000457 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
458 ASSERT_NO_FATAL_FAILURE(open(mEffect));
459 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
460 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000461
Shunkai Yao812d5b42022-11-16 18:08:50 +0000462 Parameter::Common common = EffectHelper::createParamCommon(
463 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */);
464 Parameter get = Parameter(), set = Parameter();
465 set.set<Parameter::common>(common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000466 EXPECT_IS_OK(mEffect->setParameter(set));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000467
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000468 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
469 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000470
Shunkai Yao812d5b42022-11-16 18:08:50 +0000471 Parameter::Id id;
472 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000473 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000474 EXPECT_EQ(set, get) << set.toString() << " vs " << get.toString();
475
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000476 ASSERT_NO_FATAL_FAILURE(close(mEffect));
477 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000478}
479
Shunkai Yao812d5b42022-11-16 18:08:50 +0000480// Verify Parameters kept after reset.
481TEST_P(AudioEffectTest, SetAndGetParameterAfterReset) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000482 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
483 ASSERT_NO_FATAL_FAILURE(open(mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000484
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000485 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
486 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000487
488 Parameter::Common common = EffectHelper::createParamCommon(
489 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */);
490 Parameter get = Parameter(), set = Parameter();
491 set.set<Parameter::common>(common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000492 EXPECT_IS_OK(mEffect->setParameter(set));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000493
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000494 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
495 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000496
497 Parameter::Id id;
498 id.set<Parameter::Id::commonTag>(Parameter::common);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000499 EXPECT_IS_OK(mEffect->getParameter(id, &get));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000500 EXPECT_EQ(set, get) << set.toString() << " vs " << get.toString();
501
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000502 ASSERT_NO_FATAL_FAILURE(close(mEffect));
503 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000504}
Shunkai Yao812d5b42022-11-16 18:08:50 +0000505/// Data processing test
506// Send data to effects and expect it to be consumed by checking statusMQ.
507TEST_P(AudioEffectTest, ConsumeDataInProcessingState) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000508 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000509
510 Parameter::Common common = EffectHelper::createParamCommon(
511 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
512 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
513 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000514 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000515 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000516 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000517 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000518 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000519 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000520 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000521
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000522 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
523 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000524
525 std::vector<float> buffer;
526 EffectHelper::allocateInputData(common, inputMQ, buffer);
527 EffectHelper::writeToFmq(inputMQ, buffer);
528 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
529
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000530 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
531 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000532
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000533 ASSERT_NO_FATAL_FAILURE(close(mEffect));
534 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000535}
536
537// Send data to effects and expect it to be consumed after effect restart.
538TEST_P(AudioEffectTest, ConsumeDataAfterRestart) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000539 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000540
541 Parameter::Common common = EffectHelper::createParamCommon(
542 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
543 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
544 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000545 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000546 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000547 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000548 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000549 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000550 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000551 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000552
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000553 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
554 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
555 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
556 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
557 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
558 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000559
560 std::vector<float> buffer;
561 EffectHelper::allocateInputData(common, inputMQ, buffer);
562 EffectHelper::writeToFmq(inputMQ, buffer);
563 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
564
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000565 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
566 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000567
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000568 ASSERT_NO_FATAL_FAILURE(close(mEffect));
569 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000570}
571
572// Send data to IDLE effects and expect it to be consumed after effect start.
573TEST_P(AudioEffectTest, SendDataAtIdleAndConsumeDataInProcessing) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000574 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000575
576 Parameter::Common common = EffectHelper::createParamCommon(
577 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
578 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
579 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000580 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000581 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000582 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000583 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000584 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000585 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000586 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000587
588 std::vector<float> buffer;
589 EffectHelper::allocateInputData(common, inputMQ, buffer);
590 EffectHelper::writeToFmq(inputMQ, buffer);
591 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
592
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000593 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
594 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000595
596 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
597
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000598 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
599 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000600
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000601 ASSERT_NO_FATAL_FAILURE(close(mEffect));
602 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000603}
604
605// Send data multiple times.
606TEST_P(AudioEffectTest, ProcessDataMultipleTimes) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000607 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000608
609 Parameter::Common common = EffectHelper::createParamCommon(
610 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
611 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
612 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000613 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000614 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000615 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000616 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000617 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000618 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000619 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000620
621 std::vector<float> buffer;
622 EffectHelper::allocateInputData(common, inputMQ, buffer);
623 EffectHelper::writeToFmq(inputMQ, buffer);
624 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
625
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000626 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
627 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000628
629 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
630 // expect no status and data after consume
631 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
632
633 EffectHelper::writeToFmq(inputMQ, buffer);
634 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
635 // expect no status and data after consume
636 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
637
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000638 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
639 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000640
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000641 ASSERT_NO_FATAL_FAILURE(close(mEffect));
642 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000643}
644
645// Send data to IDLE state effects and expect it not be consumed.
646TEST_P(AudioEffectTest, NotConsumeDataInIdleState) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000647 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000648
649 Parameter::Common common = EffectHelper::createParamCommon(
650 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
651 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
652 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000653 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000654 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000655 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000656 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000657 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000658 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000659 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000660
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000661 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
662 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
663 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
664 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000665
666 std::vector<float> buffer;
667 EffectHelper::allocateInputData(common, inputMQ, buffer);
668 EffectHelper::writeToFmq(inputMQ, buffer);
669 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
670
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000671 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
672 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000673 EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer);
674
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000675 ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
676 ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000677
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000678 ASSERT_NO_FATAL_FAILURE(close(mEffect));
679 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000680}
681
682// Send data to closed effects and expect it not be consumed.
683TEST_P(AudioEffectTest, NotConsumeDataByClosedEffect) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000684 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000685
686 Parameter::Common common = EffectHelper::createParamCommon(
687 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
688 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
689 IEffect::OpenEffectReturn ret;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000690 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
691 ASSERT_NO_FATAL_FAILURE(close(mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000692
693 auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000694 ASSERT_TRUE(statusMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000695 auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000696 ASSERT_TRUE(inputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000697 auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000698 ASSERT_TRUE(outputMQ->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000699
700 std::vector<float> buffer;
701 EffectHelper::allocateInputData(common, inputMQ, buffer);
702 EffectHelper::writeToFmq(inputMQ, buffer);
703 EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer);
704
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000705 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000706}
707
708// Send data to multiple effects.
709TEST_P(AudioEffectTest, ConsumeDataMultipleEffects) {
710 std::shared_ptr<IEffect> effect1, effect2;
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000711 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
712 ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
Shunkai Yao812d5b42022-11-16 18:08:50 +0000713
714 Parameter::Common common1 = EffectHelper::createParamCommon(
715 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
716 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
717 Parameter::Common common2 = EffectHelper::createParamCommon(
718 1 /* session */, 1 /* ioHandle */, 48000 /* iSampleRate */, 48000 /* oSampleRate */,
719 2 * kInputFrameCount /* iFrameCount */, 2 * kOutputFrameCount /* oFrameCount */);
720 IEffect::OpenEffectReturn ret1, ret2;
721 ASSERT_NO_FATAL_FAILURE(open(effect1, common1, std::nullopt /* specific */, &ret1, EX_NONE));
722 ASSERT_NO_FATAL_FAILURE(open(effect2, common2, std::nullopt /* specific */, &ret2, EX_NONE));
723 ASSERT_NO_FATAL_FAILURE(command(effect1, CommandId::START));
724 ASSERT_NO_FATAL_FAILURE(expectState(effect1, State::PROCESSING));
725 ASSERT_NO_FATAL_FAILURE(command(effect2, CommandId::START));
726 ASSERT_NO_FATAL_FAILURE(expectState(effect2, State::PROCESSING));
727
728 auto statusMQ1 = std::make_unique<EffectHelper::StatusMQ>(ret1.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000729 ASSERT_TRUE(statusMQ1->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000730 auto inputMQ1 = std::make_unique<EffectHelper::DataMQ>(ret1.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000731 ASSERT_TRUE(inputMQ1->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000732 auto outputMQ1 = std::make_unique<EffectHelper::DataMQ>(ret1.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000733 ASSERT_TRUE(outputMQ1->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000734
735 std::vector<float> buffer1, buffer2;
736 EffectHelper::allocateInputData(common1, inputMQ1, buffer1);
737 EffectHelper::writeToFmq(inputMQ1, buffer1);
738 EffectHelper::readFromFmq(statusMQ1, 1, outputMQ1, buffer1.size(), buffer1);
739
740 auto statusMQ2 = std::make_unique<EffectHelper::StatusMQ>(ret2.statusMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000741 ASSERT_TRUE(statusMQ2->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000742 auto inputMQ2 = std::make_unique<EffectHelper::DataMQ>(ret2.inputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000743 ASSERT_TRUE(inputMQ2->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000744 auto outputMQ2 = std::make_unique<EffectHelper::DataMQ>(ret2.outputDataMQ);
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000745 ASSERT_TRUE(outputMQ2->isValid());
Shunkai Yao812d5b42022-11-16 18:08:50 +0000746 EffectHelper::allocateInputData(common2, inputMQ2, buffer2);
747 EffectHelper::writeToFmq(inputMQ2, buffer2);
748 EffectHelper::readFromFmq(statusMQ2, 1, outputMQ2, buffer2.size(), buffer2);
749
750 ASSERT_NO_FATAL_FAILURE(command(effect1, CommandId::STOP));
751 ASSERT_NO_FATAL_FAILURE(expectState(effect1, State::IDLE));
752 ASSERT_NO_FATAL_FAILURE(close(effect1));
753 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
754
755 ASSERT_NO_FATAL_FAILURE(command(effect2, CommandId::STOP));
756 ASSERT_NO_FATAL_FAILURE(expectState(effect2, State::IDLE));
757 ASSERT_NO_FATAL_FAILURE(close(effect2));
758 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
759}
760
761INSTANTIATE_TEST_SUITE_P(
762 SingleEffectInstanceTest, AudioEffectTest,
763 ::testing::Combine(testing::ValuesIn(
764 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor))),
765 [](const testing::TestParamInfo<AudioEffectTest::ParamType>& info) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000766 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
767 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
768 descriptor.common.name + "_TYPE_" +
769 descriptor.common.id.type.toString() + "_UUID_" +
770 descriptor.common.id.uuid.toString();
Shunkai Yao812d5b42022-11-16 18:08:50 +0000771 std::replace_if(
772 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
773 return name;
774 });
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000775GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectTest);
Shunkai Yao67b1be62022-07-13 05:01:42 +0000776
777int main(int argc, char** argv) {
778 ::testing::InitGoogleTest(&argc, argv);
779 ABinderProcess_setThreadPoolMaxThreadCount(1);
780 ABinderProcess_startThreadPool();
781 return RUN_ALL_TESTS();
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000782}