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