1 /*
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 
17 #define LOG_TAG "VtsHalAudioEffectTargetTest"
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include <aidl/Gtest.h>
24 #include <aidl/android/hardware/audio/effect/IEffect.h>
25 #include <aidl/android/hardware/audio/effect/IFactory.h>
26 #include <android-base/logging.h>
27 #include <android/binder_interface_utils.h>
28 #include <android/binder_manager.h>
29 #include <android/binder_process.h>
30 #include <fmq/AidlMessageQueue.h>
31 
32 #include "EffectFactoryHelper.h"
33 #include "EffectHelper.h"
34 #include "TestUtils.h"
35 
36 using namespace android;
37 
38 using ndk::ScopedAStatus;
39 
40 using aidl::android::hardware::audio::effect::CommandId;
41 using aidl::android::hardware::audio::effect::Descriptor;
42 using aidl::android::hardware::audio::effect::Flags;
43 using aidl::android::hardware::audio::effect::IEffect;
44 using aidl::android::hardware::audio::effect::IFactory;
45 using aidl::android::hardware::audio::effect::Parameter;
46 using aidl::android::hardware::audio::effect::State;
47 using aidl::android::media::audio::common::AudioDeviceDescription;
48 using aidl::android::media::audio::common::AudioDeviceType;
49 using aidl::android::media::audio::common::AudioMode;
50 using aidl::android::media::audio::common::AudioSource;
51 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
52 
53 enum ParamName { PARAM_INSTANCE_NAME };
54 using EffectTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>>;
55 
56 class AudioEffectTest : public testing::TestWithParam<EffectTestParam>, public EffectHelper {
57   public:
AudioEffectTest()58     AudioEffectTest() {
59         std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
60         mVersion = EffectFactoryHelper::getHalVersion(mFactory);
61     }
62 
SetUp()63     void SetUp() override {}
64 
TearDown()65     void TearDown() override {
66         // Do the cleanup for every test case
67         if (mEffect) {
68             ASSERT_NO_FATAL_FAILURE(commandIgnoreRet(mEffect, CommandId::STOP));
69             ASSERT_NO_FATAL_FAILURE(closeIgnoreRet(mEffect));
70             ASSERT_NO_FATAL_FAILURE(destroyIgnoreRet(mFactory, mEffect));
71             mEffect.reset();
72         }
73     }
74 
75     static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
76     std::shared_ptr<IFactory> mFactory;
77     std::shared_ptr<IEffect> mEffect;
78     Descriptor mDescriptor;
79     int mVersion = 0;
80 
setAndGetParameter(Parameter::Id id,const Parameter & set)81     void setAndGetParameter(Parameter::Id id, const Parameter& set) {
82         Parameter get;
83         EXPECT_IS_OK(mEffect->setParameter(set));
84         EXPECT_IS_OK(mEffect->getParameter(id, &get));
85         EXPECT_EQ(set, get) << set.toString() << "\n vs \n" << get.toString();
86     }
87 };
88 
89 class AudioEffectDataPathTest : public AudioEffectTest {
90   public:
SetUp()91     void SetUp() override {
92         AudioEffectTest::SetUp();
93         SKIP_TEST_IF_DATA_UNSUPPORTED(mDescriptor.common.flags);
94     }
95 };
96 
TEST_P(AudioEffectTest,SetupAndTearDown)97 TEST_P(AudioEffectTest, SetupAndTearDown) {
98     // Intentionally empty test body.
99 }
100 
TEST_P(AudioEffectTest,CreateAndDestroy)101 TEST_P(AudioEffectTest, CreateAndDestroy) {
102     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
103     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
104 }
105 
TEST_P(AudioEffectTest,OpenAndClose)106 TEST_P(AudioEffectTest, OpenAndClose) {
107     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
108     ASSERT_NO_FATAL_FAILURE(open(mEffect));
109     ASSERT_NO_FATAL_FAILURE(close(mEffect));
110     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
111 }
112 
TEST_P(AudioEffectTest,CloseUnopenedEffect)113 TEST_P(AudioEffectTest, CloseUnopenedEffect) {
114     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
115     ASSERT_NO_FATAL_FAILURE(close(mEffect));
116     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
117 }
118 
TEST_P(AudioEffectTest,DoubleOpenAndClose)119 TEST_P(AudioEffectTest, DoubleOpenAndClose) {
120     std::shared_ptr<IEffect> effect1, effect2;
121     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
122     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
123     ASSERT_NO_FATAL_FAILURE(open(effect1));
124     ASSERT_NO_FATAL_FAILURE(open(effect2, 1 /* session */));
125     ASSERT_NO_FATAL_FAILURE(close(effect1));
126     ASSERT_NO_FATAL_FAILURE(close(effect2));
127     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
128     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
129 }
130 
TEST_P(AudioEffectTest,TripleOpenAndClose)131 TEST_P(AudioEffectTest, TripleOpenAndClose) {
132     std::shared_ptr<IEffect> effect1, effect2, effect3;
133     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
134     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
135     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect3, mDescriptor));
136     ASSERT_NO_FATAL_FAILURE(open(effect1));
137     ASSERT_NO_FATAL_FAILURE(open(effect2, 1 /* session */));
138     ASSERT_NO_FATAL_FAILURE(open(effect3, 2 /* session */));
139     ASSERT_NO_FATAL_FAILURE(close(effect1));
140     ASSERT_NO_FATAL_FAILURE(close(effect2));
141     ASSERT_NO_FATAL_FAILURE(close(effect3));
142     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
143     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
144     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect3));
145 }
146 
TEST_P(AudioEffectTest,GetDescritorBeforeOpen)147 TEST_P(AudioEffectTest, GetDescritorBeforeOpen) {
148     Descriptor desc;
149     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
150     ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, desc));
151     EXPECT_EQ(mDescriptor.common.id.type, desc.common.id.type);
152     EXPECT_EQ(mDescriptor.common.id.uuid, desc.common.id.uuid);
153     EXPECT_EQ(mDescriptor.common.name, desc.common.name);
154     EXPECT_EQ(mDescriptor.common.implementor, desc.common.implementor);
155     // Effect implementation Must fill in implementor and name
156     EXPECT_NE("", desc.common.name);
157     EXPECT_NE("", desc.common.implementor);
158     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
159 }
160 
TEST_P(AudioEffectTest,GetDescritorAfterOpen)161 TEST_P(AudioEffectTest, GetDescritorAfterOpen) {
162     Descriptor beforeOpen, afterOpen, afterClose;
163     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
164     ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, beforeOpen));
165     ASSERT_NO_FATAL_FAILURE(open(mEffect));
166     ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, afterOpen));
167     EXPECT_EQ(beforeOpen.toString(), afterOpen.toString()) << "\n"
168                                                            << beforeOpen.toString() << "\n"
169                                                            << afterOpen.toString();
170     ASSERT_NO_FATAL_FAILURE(close(mEffect));
171     ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, afterClose));
172     EXPECT_EQ(beforeOpen.toString(), afterClose.toString()) << "\n"
173                                                             << beforeOpen.toString() << "\n"
174                                                             << afterClose.toString();
175     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
176 }
177 
TEST_P(AudioEffectTest,DescriptorExistAndUnique)178 TEST_P(AudioEffectTest, DescriptorExistAndUnique) {
179     Descriptor desc;
180 
181     auto descList = EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor);
182     std::set<Descriptor::Identity> idSet;
183     for (const auto& it : descList) {
184         auto& id = it.second.common.id;
185         EXPECT_EQ(0ul, idSet.count(id));
186         idSet.insert(id);
187     }
188 
189     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
190     ASSERT_NO_FATAL_FAILURE(getDescriptor(mEffect, desc));
191     int uuidCount = std::count_if(idSet.begin(), idSet.end(), [&](const auto& id) {
192         return id.uuid == desc.common.id.uuid && id.type == desc.common.id.type;
193     });
194 
195     EXPECT_EQ(1, uuidCount);
196     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
197 }
198 
199 /// State testing.
200 // An effect instance is in INIT state by default after it was created.
TEST_P(AudioEffectTest,InitStateAfterCreation)201 TEST_P(AudioEffectTest, InitStateAfterCreation) {
202     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
203     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
204     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
205 }
206 
207 // An effect instance transfer to IDLE state after IEffect.ASSERT_NO_FATAL_FAILURE(open().
TEST_P(AudioEffectTest,IdleStateAfterOpen)208 TEST_P(AudioEffectTest, IdleStateAfterOpen) {
209     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
210     ASSERT_NO_FATAL_FAILURE(open(mEffect));
211     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
212     ASSERT_NO_FATAL_FAILURE(close(mEffect));
213     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
214 }
215 
216 // An effect instance is in PROCESSING state after it receive an START command.
TEST_P(AudioEffectTest,ProcessingStateAfterStart)217 TEST_P(AudioEffectTest, ProcessingStateAfterStart) {
218     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
219     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
220     ASSERT_NO_FATAL_FAILURE(open(mEffect));
221     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
222     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
223     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
224     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
225     ASSERT_NO_FATAL_FAILURE(close(mEffect));
226     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
227 }
228 
229 // An effect instance transfer to IDLE state after Command.Id.STOP in PROCESSING state.
TEST_P(AudioEffectTest,IdleStateAfterStop)230 TEST_P(AudioEffectTest, IdleStateAfterStop) {
231     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
232     ASSERT_NO_FATAL_FAILURE(open(mEffect));
233     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
234     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
235     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
236     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
237     ASSERT_NO_FATAL_FAILURE(close(mEffect));
238     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
239 }
240 
241 // An effect instance transfer to IDLE state after Command.Id.RESET in PROCESSING state.
TEST_P(AudioEffectTest,IdleStateAfterReset)242 TEST_P(AudioEffectTest, IdleStateAfterReset) {
243     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
244     ASSERT_NO_FATAL_FAILURE(open(mEffect));
245     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
246     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
247     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
248     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
249     ASSERT_NO_FATAL_FAILURE(close(mEffect));
250     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
251 }
252 
253 // An effect instance transfer to INIT after IEffect.ASSERT_NO_FATAL_FAILURE(close().
TEST_P(AudioEffectTest,InitStateAfterClose)254 TEST_P(AudioEffectTest, InitStateAfterClose) {
255     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
256     ASSERT_NO_FATAL_FAILURE(open(mEffect));
257     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
258     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
259     ASSERT_NO_FATAL_FAILURE(close(mEffect));
260     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
261     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
262 }
263 
264 // An effect instance shouldn't accept any command before open.
TEST_P(AudioEffectTest,NoCommandAcceptedBeforeOpen)265 TEST_P(AudioEffectTest, NoCommandAcceptedBeforeOpen) {
266     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
267     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START, EX_ILLEGAL_STATE));
268     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP, EX_ILLEGAL_STATE));
269     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET, EX_ILLEGAL_STATE));
270     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
271 }
272 
273 // No-op when receive STOP command in IDLE state.
TEST_P(AudioEffectTest,StopCommandInIdleStateNoOp)274 TEST_P(AudioEffectTest, StopCommandInIdleStateNoOp) {
275     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
276     ASSERT_NO_FATAL_FAILURE(open(mEffect));
277     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
278     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
279     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
280     ASSERT_NO_FATAL_FAILURE(close(mEffect));
281     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
282 }
283 
284 // No-op when receive RESET command in IDLE state.
TEST_P(AudioEffectTest,ResetCommandInIdleStateNoOp)285 TEST_P(AudioEffectTest, ResetCommandInIdleStateNoOp) {
286     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
287     ASSERT_NO_FATAL_FAILURE(open(mEffect));
288     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
289     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
290     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
291     ASSERT_NO_FATAL_FAILURE(close(mEffect));
292     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
293 }
294 
295 // Repeat START and STOP command.
TEST_P(AudioEffectTest,RepeatStartAndStop)296 TEST_P(AudioEffectTest, RepeatStartAndStop) {
297     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
298     ASSERT_NO_FATAL_FAILURE(open(mEffect));
299     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
300     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
301     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
302     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
303     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
304 
305     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
306     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
307     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
308     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
309     ASSERT_NO_FATAL_FAILURE(close(mEffect));
310     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
311 }
312 
313 // Repeat START and RESET command.
TEST_P(AudioEffectTest,RepeatStartAndReset)314 TEST_P(AudioEffectTest, RepeatStartAndReset) {
315     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
316     ASSERT_NO_FATAL_FAILURE(open(mEffect));
317     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
318     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
319     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
320     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
321     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
322 
323     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
324     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
325     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
326     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
327     ASSERT_NO_FATAL_FAILURE(close(mEffect));
328     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
329 }
330 
331 // Try to close an effect instance at PROCESSING state.
TEST_P(AudioEffectTest,CloseProcessingStateEffects)332 TEST_P(AudioEffectTest, CloseProcessingStateEffects) {
333     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
334     ASSERT_NO_FATAL_FAILURE(open(mEffect));
335     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
336     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
337     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
338 
339     ASSERT_NO_FATAL_FAILURE(close(mEffect, EX_ILLEGAL_STATE));
340 
341     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
342     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
343     ASSERT_NO_FATAL_FAILURE(close(mEffect));
344     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
345 }
346 
347 // Expect EX_ILLEGAL_STATE if the effect instance is not in a proper state to be destroyed.
TEST_P(AudioEffectTest,DestroyOpenEffects)348 TEST_P(AudioEffectTest, DestroyOpenEffects) {
349     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
350     ASSERT_NO_FATAL_FAILURE(open(mEffect));
351     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
352 
353     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect, EX_ILLEGAL_STATE));
354 
355     // cleanup
356     ASSERT_NO_FATAL_FAILURE(close(mEffect));
357     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
358 }
359 
360 // Expect EX_ILLEGAL_STATE if the effect instance is not in a proper state to be destroyed.
TEST_P(AudioEffectTest,DestroyProcessingEffects)361 TEST_P(AudioEffectTest, DestroyProcessingEffects) {
362     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
363     ASSERT_NO_FATAL_FAILURE(open(mEffect));
364     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
365     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
366     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
367 
368     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect, EX_ILLEGAL_STATE));
369 
370     // cleanup
371     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
372     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
373     ASSERT_NO_FATAL_FAILURE(close(mEffect));
374     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
375 }
376 
TEST_P(AudioEffectTest,NormalSequenceStates)377 TEST_P(AudioEffectTest, NormalSequenceStates) {
378     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
379     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::INIT));
380     ASSERT_NO_FATAL_FAILURE(open(mEffect));
381     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
382     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
383     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
384     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
385     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
386     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
387     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
388     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
389     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
390     ASSERT_NO_FATAL_FAILURE(close(mEffect));
391     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
392 }
393 
394 /// Parameter testing.
395 // Verify parameters pass in open can be successfully get.
TEST_P(AudioEffectTest,VerifyCommonParametersAfterOpen)396 TEST_P(AudioEffectTest, VerifyCommonParametersAfterOpen) {
397     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
398 
399     Parameter::Common common = createParamCommon();
400     IEffect::OpenEffectReturn ret;
401     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
402 
403     Parameter get = Parameter(), expect = Parameter();
404     expect.set<Parameter::common>(common);
405     Parameter::Id id;
406     id.set<Parameter::Id::commonTag>(Parameter::common);
407     EXPECT_IS_OK(mEffect->getParameter(id, &get));
408     EXPECT_EQ(expect, get) << expect.toString() << "\n vs \n" << get.toString();
409 
410     ASSERT_NO_FATAL_FAILURE(close(mEffect));
411     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
412 }
413 
414 // Verify parameters pass in set can be successfully get.
TEST_P(AudioEffectTest,SetAndGetCommonParameter)415 TEST_P(AudioEffectTest, SetAndGetCommonParameter) {
416     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
417     ASSERT_NO_FATAL_FAILURE(open(mEffect));
418 
419     Parameter::Common common = createParamCommon(0 /* session */, 1 /* ioHandle */,
420                                                  44100 /* iSampleRate */, 44100 /* oSampleRate */);
421     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
422     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
423 
424     ASSERT_NO_FATAL_FAILURE(close(mEffect));
425     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
426 }
427 
428 // Verify parameters set and get in PROCESSING state.
TEST_P(AudioEffectTest,SetAndGetParameterInProcessing)429 TEST_P(AudioEffectTest, SetAndGetParameterInProcessing) {
430     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
431     ASSERT_NO_FATAL_FAILURE(open(mEffect));
432     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
433     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
434 
435     Parameter::Common common = createParamCommon(0 /* session */, 1 /* ioHandle */,
436                                                  44100 /* iSampleRate */, 44100 /* oSampleRate */);
437     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
438     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
439 
440     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
441     ASSERT_NO_FATAL_FAILURE(close(mEffect));
442     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
443 }
444 
445 // Verify parameters set and get in IDLE state.
TEST_P(AudioEffectTest,SetAndGetParameterInIdle)446 TEST_P(AudioEffectTest, SetAndGetParameterInIdle) {
447     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
448     ASSERT_NO_FATAL_FAILURE(open(mEffect));
449     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
450     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
451     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
452     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
453 
454     Parameter::Common common = createParamCommon(0 /* session */, 1 /* ioHandle */,
455                                                  44100 /* iSampleRate */, 44100 /* oSampleRate */);
456     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
457     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
458 
459     ASSERT_NO_FATAL_FAILURE(close(mEffect));
460     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
461 }
462 
463 // Verify Parameters kept after stop.
TEST_P(AudioEffectTest,SetAndGetParameterAfterStop)464 TEST_P(AudioEffectTest, SetAndGetParameterAfterStop) {
465     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
466     ASSERT_NO_FATAL_FAILURE(open(mEffect));
467     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
468     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
469 
470     Parameter::Common common = createParamCommon(0 /* session */, 1 /* ioHandle */,
471                                                  44100 /* iSampleRate */, 44100 /* oSampleRate */);
472     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
473     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
474 
475     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
476     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
477     ASSERT_NO_FATAL_FAILURE(close(mEffect));
478     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
479 }
480 
481 // Verify Parameters kept after reset.
TEST_P(AudioEffectTest,SetAndGetParameterAfterReset)482 TEST_P(AudioEffectTest, SetAndGetParameterAfterReset) {
483     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
484     ASSERT_NO_FATAL_FAILURE(open(mEffect));
485 
486     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
487     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
488 
489     Parameter::Common common = createParamCommon(0 /* session */, 1 /* ioHandle */,
490                                                  44100 /* iSampleRate */, 44100 /* oSampleRate */);
491     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
492     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
493 
494     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::RESET));
495     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
496 
497     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
498 
499     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
500     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
501     ASSERT_NO_FATAL_FAILURE(close(mEffect));
502     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
503 }
504 
505 // Set and get AudioDeviceDescription in Parameter
TEST_P(AudioEffectTest,SetAndGetParameterDeviceDescription)506 TEST_P(AudioEffectTest, SetAndGetParameterDeviceDescription) {
507     if (!mDescriptor.common.flags.deviceIndication) {
508         GTEST_SKIP() << "Skipping test as effect does not support deviceIndication"
509                      << mDescriptor.common.flags.toString();
510     }
511 
512     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
513     ASSERT_NO_FATAL_FAILURE(open(mEffect));
514 
515     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
516     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
517 
518     std::vector<AudioDeviceDescription> deviceDescs = {
519             {.type = AudioDeviceType::IN_DEFAULT,
520              .connection = AudioDeviceDescription::CONNECTION_ANALOG},
521             {.type = AudioDeviceType::IN_DEVICE,
522              .connection = AudioDeviceDescription::CONNECTION_BT_A2DP}};
523     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::deviceDescription);
524     ASSERT_NO_FATAL_FAILURE(
525             setAndGetParameter(id, Parameter::make<Parameter::deviceDescription>(deviceDescs)));
526 
527     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
528     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
529     ASSERT_NO_FATAL_FAILURE(close(mEffect));
530     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
531 }
532 
533 // Set and get AudioMode in Parameter
TEST_P(AudioEffectTest,SetAndGetParameterAudioMode)534 TEST_P(AudioEffectTest, SetAndGetParameterAudioMode) {
535     if (!mDescriptor.common.flags.audioModeIndication) {
536         GTEST_SKIP() << "Skipping test as effect does not support audioModeIndication"
537                      << mDescriptor.common.flags.toString();
538     }
539 
540     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
541     ASSERT_NO_FATAL_FAILURE(open(mEffect));
542 
543     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
544     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
545 
546     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::mode);
547     ASSERT_NO_FATAL_FAILURE(
548             setAndGetParameter(id, Parameter::make<Parameter::mode>(AudioMode::NORMAL)));
549     ASSERT_NO_FATAL_FAILURE(
550             setAndGetParameter(id, Parameter::make<Parameter::mode>(AudioMode::IN_COMMUNICATION)));
551 
552     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
553     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
554     ASSERT_NO_FATAL_FAILURE(close(mEffect));
555     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
556 }
557 
558 // Set and get AudioSource in Parameter
TEST_P(AudioEffectTest,SetAndGetParameterAudioSource)559 TEST_P(AudioEffectTest, SetAndGetParameterAudioSource) {
560     if (!mDescriptor.common.flags.audioSourceIndication) {
561         GTEST_SKIP() << "Skipping test as effect does not support audioSourceIndication"
562                      << mDescriptor.common.flags.toString();
563     }
564 
565     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
566     ASSERT_NO_FATAL_FAILURE(open(mEffect));
567 
568     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
569     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
570 
571     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::source);
572     ASSERT_NO_FATAL_FAILURE(
573             setAndGetParameter(id, Parameter::make<Parameter::source>(AudioSource::DEFAULT)));
574     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(
575             id, Parameter::make<Parameter::source>(AudioSource::VOICE_RECOGNITION)));
576 
577     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
578     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
579     ASSERT_NO_FATAL_FAILURE(close(mEffect));
580     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
581 }
582 
583 // Set and get VolumeStereo in Parameter
TEST_P(AudioEffectTest,SetAndGetParameterVolume)584 TEST_P(AudioEffectTest, SetAndGetParameterVolume) {
585     if (mDescriptor.common.flags.volume == Flags::Volume::NONE) {
586         GTEST_SKIP() << "Skipping test as effect does not support volume"
587                      << mDescriptor.common.flags.toString();
588     }
589 
590     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
591     ASSERT_NO_FATAL_FAILURE(open(mEffect));
592 
593     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
594     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
595 
596     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::volumeStereo);
597     Parameter::VolumeStereo volume = {.left = 10.0, .right = 10.0};
598     if (mDescriptor.common.flags.volume == Flags::Volume::CTRL) {
599         Parameter get;
600         EXPECT_IS_OK(mEffect->setParameter(volume));
601         EXPECT_IS_OK(mEffect->getParameter(id, &get));
602     } else {
603         ASSERT_NO_FATAL_FAILURE(
604                 setAndGetParameter(id, Parameter::make<Parameter::volumeStereo>(volume)));
605     }
606 
607     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
608     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
609     ASSERT_NO_FATAL_FAILURE(close(mEffect));
610     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
611 }
612 
613 /**
614  * Verify DataMqUpdateEventFlag after common parameter setting.
615  * verify reopen sequence.
616  */
TEST_P(AudioEffectDataPathTest,SetCommonParameterAndReopen)617 TEST_P(AudioEffectDataPathTest, SetCommonParameterAndReopen) {
618     if (!EffectFactoryHelper::isReopenSupported(mFactory)) {
619         GTEST_SKIP() << "Skipping test as effect does not support reopen";
620     }
621 
622     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
623 
624     Parameter::Common common = createParamCommon(
625             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
626             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
627     IEffect::OpenEffectReturn ret;
628     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
629     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
630     ASSERT_TRUE(statusMQ->isValid());
631     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
632     ASSERT_TRUE(inputMQ->isValid());
633     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
634     ASSERT_TRUE(outputMQ->isValid());
635 
636     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
637     common.input.frameCount++;
638     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
639     ASSERT_TRUE(statusMQ->isValid());
640     expectDataMqUpdateEventFlag(statusMQ);
641     EXPECT_IS_OK(mEffect->reopen(&ret));
642     inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
643     outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
644     ASSERT_TRUE(statusMQ->isValid());
645     ASSERT_TRUE(inputMQ->isValid());
646     ASSERT_TRUE(outputMQ->isValid());
647 
648     common.output.frameCount++;
649     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
650     ASSERT_TRUE(statusMQ->isValid());
651     expectDataMqUpdateEventFlag(statusMQ);
652     EXPECT_IS_OK(mEffect->reopen(&ret));
653     inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
654     outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
655     ASSERT_TRUE(statusMQ->isValid());
656     ASSERT_TRUE(inputMQ->isValid());
657     ASSERT_TRUE(outputMQ->isValid());
658 
659     ASSERT_NO_FATAL_FAILURE(close(mEffect));
660     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
661 }
662 
663 /// Data processing test
664 // Send data to effects and expect it to be consumed by checking statusMQ.
665 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ConsumeDataInProcessingState)666 TEST_P(AudioEffectDataPathTest, ConsumeDataInProcessingState) {
667     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
668 
669     Parameter::Common common = createParamCommon(
670             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
671             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
672     IEffect::OpenEffectReturn ret;
673     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
674     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
675     ASSERT_TRUE(statusMQ->isValid());
676     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
677     ASSERT_TRUE(inputMQ->isValid());
678     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
679     ASSERT_TRUE(outputMQ->isValid());
680 
681     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
682     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
683 
684     std::vector<float> buffer;
685     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
686     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
687     EXPECT_NO_FATAL_FAILURE(
688             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
689 
690     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
691     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
692     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
693 
694     ASSERT_NO_FATAL_FAILURE(close(mEffect));
695     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
696 }
697 
698 // Send data to effects and expect it to be consumed after effect restart.
699 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ConsumeDataAfterRestart)700 TEST_P(AudioEffectDataPathTest, ConsumeDataAfterRestart) {
701     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
702 
703     Parameter::Common common = createParamCommon(
704             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
705             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
706     IEffect::OpenEffectReturn ret;
707     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
708     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
709     ASSERT_TRUE(statusMQ->isValid());
710     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
711     ASSERT_TRUE(inputMQ->isValid());
712     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
713     ASSERT_TRUE(outputMQ->isValid());
714 
715     std::vector<float> buffer;
716     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
717     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
718     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
719     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
720     EXPECT_NO_FATAL_FAILURE(
721             EffectHelper::readFromFmq(statusMQ, 0, outputMQ, buffer.size(), buffer));
722     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
723     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
724 
725     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
726     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
727     EXPECT_NO_FATAL_FAILURE(
728             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
729 
730     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
731     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
732     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
733 
734     ASSERT_NO_FATAL_FAILURE(close(mEffect));
735     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
736 }
737 
738 // Send data to effects and expect it to be consumed after effect reopen (IO AudioConfig change).
739 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ConsumeDataAfterReopen)740 TEST_P(AudioEffectDataPathTest, ConsumeDataAfterReopen) {
741     if (!EffectFactoryHelper::isReopenSupported(mFactory)) {
742         GTEST_SKIP() << "Skipping test as effect does not support reopen";
743     }
744 
745     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
746 
747     Parameter::Common common = createParamCommon(
748             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
749             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
750     IEffect::OpenEffectReturn ret;
751     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
752     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
753     ASSERT_TRUE(statusMQ->isValid());
754     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
755     ASSERT_TRUE(inputMQ->isValid());
756     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
757     ASSERT_TRUE(outputMQ->isValid());
758 
759     std::vector<float> buffer;
760     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
761     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
762     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
763     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
764     EXPECT_NO_FATAL_FAILURE(
765             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
766 
767     // set a new common parameter with different IO frameCount, reopen
768     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common);
769     common.input.frameCount += 4;
770     common.output.frameCount += 4;
771     ASSERT_NO_FATAL_FAILURE(setAndGetParameter(id, Parameter::make<Parameter::common>(common)));
772     ASSERT_TRUE(statusMQ->isValid());
773     expectDataMqUpdateEventFlag(statusMQ);
774     EXPECT_IS_OK(mEffect->reopen(&ret));
775     inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
776     outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
777     ASSERT_TRUE(statusMQ->isValid());
778     ASSERT_TRUE(inputMQ->isValid());
779     ASSERT_TRUE(outputMQ->isValid());
780 
781     // verify data consume again
782     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
783     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
784     EXPECT_NO_FATAL_FAILURE(
785             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
786 
787     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
788     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
789     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
790 
791     ASSERT_NO_FATAL_FAILURE(close(mEffect));
792     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
793 }
794 
795 // Send data to IDLE effects and expect it to be consumed after effect start.
796 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,SendDataAtIdleAndConsumeDataInProcessing)797 TEST_P(AudioEffectDataPathTest, SendDataAtIdleAndConsumeDataInProcessing) {
798     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
799 
800     Parameter::Common common = createParamCommon(
801             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
802             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
803     IEffect::OpenEffectReturn ret;
804     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
805     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
806     ASSERT_TRUE(statusMQ->isValid());
807     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
808     ASSERT_TRUE(inputMQ->isValid());
809     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
810     ASSERT_TRUE(outputMQ->isValid());
811 
812     std::vector<float> buffer;
813     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
814     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
815 
816     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
817     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
818 
819     EXPECT_NO_FATAL_FAILURE(
820             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
821 
822     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
823     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
824 
825     ASSERT_NO_FATAL_FAILURE(close(mEffect));
826     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
827 }
828 
829 // Send data multiple times.
830 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ProcessDataMultipleTimes)831 TEST_P(AudioEffectDataPathTest, ProcessDataMultipleTimes) {
832     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
833 
834     Parameter::Common common = createParamCommon(
835             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
836             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
837     IEffect::OpenEffectReturn ret;
838     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
839     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
840     ASSERT_TRUE(statusMQ->isValid());
841     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
842     ASSERT_TRUE(inputMQ->isValid());
843     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
844     ASSERT_TRUE(outputMQ->isValid());
845 
846     std::vector<float> buffer;
847     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
848     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
849     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
850 
851     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
852     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
853 
854     EXPECT_NO_FATAL_FAILURE(
855             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
856 
857     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
858     EXPECT_NO_FATAL_FAILURE(
859             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
860 
861     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
862     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
863     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
864 
865     ASSERT_NO_FATAL_FAILURE(close(mEffect));
866     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
867 }
868 
869 // Send data to processing state effects, stop, and restart.
870 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ConsumeDataAndRestart)871 TEST_P(AudioEffectDataPathTest, ConsumeDataAndRestart) {
872     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
873 
874     Parameter::Common common = createParamCommon(
875             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
876             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
877     IEffect::OpenEffectReturn ret;
878     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
879     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
880     ASSERT_TRUE(statusMQ->isValid());
881     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
882     ASSERT_TRUE(inputMQ->isValid());
883     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
884     ASSERT_TRUE(outputMQ->isValid());
885 
886     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
887     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
888     std::vector<float> buffer;
889     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
890     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
891     EXPECT_NO_FATAL_FAILURE(
892             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
893 
894     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
895     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
896     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
897     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
898 
899     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::START));
900     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::PROCESSING));
901     EXPECT_NO_FATAL_FAILURE(
902             EffectHelper::readFromFmq(statusMQ, 1, outputMQ, buffer.size(), buffer));
903 
904     ASSERT_NO_FATAL_FAILURE(command(mEffect, CommandId::STOP));
905     ASSERT_NO_FATAL_FAILURE(expectState(mEffect, State::IDLE));
906 
907     ASSERT_NO_FATAL_FAILURE(close(mEffect));
908     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
909 }
910 
911 // Send data to closed effects and expect it not be consumed.
912 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,NotConsumeDataByClosedEffect)913 TEST_P(AudioEffectDataPathTest, NotConsumeDataByClosedEffect) {
914     ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
915 
916     Parameter::Common common = createParamCommon(
917             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
918             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
919     IEffect::OpenEffectReturn ret;
920     ASSERT_NO_FATAL_FAILURE(open(mEffect, common, std::nullopt /* specific */, &ret, EX_NONE));
921     ASSERT_NO_FATAL_FAILURE(close(mEffect));
922 
923     auto statusMQ = std::make_unique<EffectHelper::StatusMQ>(ret.statusMQ);
924     ASSERT_TRUE(statusMQ->isValid());
925     auto inputMQ = std::make_unique<EffectHelper::DataMQ>(ret.inputDataMQ);
926     ASSERT_TRUE(inputMQ->isValid());
927     auto outputMQ = std::make_unique<EffectHelper::DataMQ>(ret.outputDataMQ);
928     ASSERT_TRUE(outputMQ->isValid());
929 
930     std::vector<float> buffer;
931     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common, inputMQ, buffer));
932     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ, inputMQ, buffer, mVersion));
933     EXPECT_NO_FATAL_FAILURE(EffectHelper::readFromFmq(statusMQ, 0, outputMQ, 0, buffer));
934 
935     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
936 }
937 
938 // Send data to multiple effects.
939 // Effects exposing bypass flags or operating in offload mode will be skipped.
TEST_P(AudioEffectDataPathTest,ConsumeDataMultipleEffects)940 TEST_P(AudioEffectDataPathTest, ConsumeDataMultipleEffects) {
941     std::shared_ptr<IEffect> effect1, effect2;
942     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect1, mDescriptor));
943     ASSERT_NO_FATAL_FAILURE(create(mFactory, effect2, mDescriptor));
944 
945     Parameter::Common common1 = createParamCommon(
946             0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
947             kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
948     Parameter::Common common2 = createParamCommon(
949             1 /* session */, 1 /* ioHandle */, 48000 /* iSampleRate */, 48000 /* oSampleRate */,
950             2 * kInputFrameCount /* iFrameCount */, 2 * kOutputFrameCount /* oFrameCount */);
951     IEffect::OpenEffectReturn ret1, ret2;
952     ASSERT_NO_FATAL_FAILURE(open(effect1, common1, std::nullopt /* specific */, &ret1, EX_NONE));
953     ASSERT_NO_FATAL_FAILURE(open(effect2, common2, std::nullopt /* specific */, &ret2, EX_NONE));
954     ASSERT_NO_FATAL_FAILURE(command(effect1, CommandId::START));
955     ASSERT_NO_FATAL_FAILURE(expectState(effect1, State::PROCESSING));
956     ASSERT_NO_FATAL_FAILURE(command(effect2, CommandId::START));
957     ASSERT_NO_FATAL_FAILURE(expectState(effect2, State::PROCESSING));
958 
959     auto statusMQ1 = std::make_unique<EffectHelper::StatusMQ>(ret1.statusMQ);
960     ASSERT_TRUE(statusMQ1->isValid());
961     auto inputMQ1 = std::make_unique<EffectHelper::DataMQ>(ret1.inputDataMQ);
962     ASSERT_TRUE(inputMQ1->isValid());
963     auto outputMQ1 = std::make_unique<EffectHelper::DataMQ>(ret1.outputDataMQ);
964     ASSERT_TRUE(outputMQ1->isValid());
965 
966     std::vector<float> buffer1, buffer2;
967     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common1, inputMQ1, buffer1));
968     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ1, inputMQ1, buffer1, mVersion));
969     EXPECT_NO_FATAL_FAILURE(
970             EffectHelper::readFromFmq(statusMQ1, 1, outputMQ1, buffer1.size(), buffer1));
971 
972     auto statusMQ2 = std::make_unique<EffectHelper::StatusMQ>(ret2.statusMQ);
973     ASSERT_TRUE(statusMQ2->isValid());
974     auto inputMQ2 = std::make_unique<EffectHelper::DataMQ>(ret2.inputDataMQ);
975     ASSERT_TRUE(inputMQ2->isValid());
976     auto outputMQ2 = std::make_unique<EffectHelper::DataMQ>(ret2.outputDataMQ);
977     ASSERT_TRUE(outputMQ2->isValid());
978     EXPECT_NO_FATAL_FAILURE(EffectHelper::allocateInputData(common2, inputMQ2, buffer2));
979     EXPECT_NO_FATAL_FAILURE(EffectHelper::writeToFmq(statusMQ2, inputMQ2, buffer2, mVersion));
980     EXPECT_NO_FATAL_FAILURE(
981             EffectHelper::readFromFmq(statusMQ2, 1, outputMQ2, buffer2.size(), buffer2));
982 
983     ASSERT_NO_FATAL_FAILURE(command(effect1, CommandId::STOP));
984     ASSERT_NO_FATAL_FAILURE(expectState(effect1, State::IDLE));
985     ASSERT_NO_FATAL_FAILURE(close(effect1));
986     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect1));
987 
988     ASSERT_NO_FATAL_FAILURE(command(effect2, CommandId::STOP));
989     ASSERT_NO_FATAL_FAILURE(expectState(effect2, State::IDLE));
990     ASSERT_NO_FATAL_FAILURE(close(effect2));
991     ASSERT_NO_FATAL_FAILURE(destroy(mFactory, effect2));
992 }
993 
994 INSTANTIATE_TEST_SUITE_P(
995         SingleEffectInstanceTest, AudioEffectTest,
996         ::testing::Combine(testing::ValuesIn(
997                 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor))),
__anon1877866e0202(const testing::TestParamInfo<AudioEffectTest::ParamType>& info) 998         [](const testing::TestParamInfo<AudioEffectTest::ParamType>& info) {
999             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
1000             std::string name = getPrefix(descriptor);
1001             std::replace_if(
1002                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
1003             return name;
1004         });
1005 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectTest);
1006 
1007 INSTANTIATE_TEST_SUITE_P(
1008         SingleEffectInstanceTest, AudioEffectDataPathTest,
1009         ::testing::Combine(testing::ValuesIn(
1010                 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor))),
__anon1877866e0402(const testing::TestParamInfo<AudioEffectDataPathTest::ParamType>& info) 1011         [](const testing::TestParamInfo<AudioEffectDataPathTest::ParamType>& info) {
1012             auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
1013             std::string name = getPrefix(descriptor);
1014             std::replace_if(
1015                     name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
1016             return name;
1017         });
1018 
1019 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(AudioEffectDataPathTest);
1020 
main(int argc,char ** argv)1021 int main(int argc, char** argv) {
1022     ::testing::InitGoogleTest(&argc, argv);
1023     ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
1024     ABinderProcess_setThreadPoolMaxThreadCount(1);
1025     ABinderProcess_startThreadPool();
1026     return RUN_ALL_TESTS();
1027 }
1028