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 #pragma once
18
19 #include <algorithm>
20 #include <memory>
21 #include <string>
22 #include <type_traits>
23 #include <unordered_map>
24 #include <vector>
25
26 #include <Utils.h>
27 #include <aidl/android/hardware/audio/effect/IEffect.h>
28 #include <aidl/android/hardware/audio/effect/IFactory.h>
29 #include <aidl/android/media/audio/common/AudioChannelLayout.h>
30 #include <android/binder_auto_utils.h>
31 #include <fmq/AidlMessageQueue.h>
32 #include <gtest/gtest.h>
33 #include <system/audio_aidl_utils.h>
34 #include <system/audio_effects/aidl_effects_utils.h>
35 #include <system/audio_effects/effect_uuid.h>
36
37 #include "AudioHalBinderServiceUtil.h"
38 #include "EffectFactoryHelper.h"
39 #include "TestUtils.h"
40
41 using namespace android;
42 using aidl::android::hardware::audio::effect::CommandId;
43 using aidl::android::hardware::audio::effect::Descriptor;
44 using aidl::android::hardware::audio::effect::IEffect;
45 using aidl::android::hardware::audio::effect::kEventFlagNotEmpty;
46 using aidl::android::hardware::audio::effect::Parameter;
47 using aidl::android::hardware::audio::effect::Range;
48 using aidl::android::hardware::audio::effect::State;
49 using aidl::android::hardware::common::fmq::SynchronizedReadWrite;
50 using aidl::android::media::audio::common::AudioChannelLayout;
51 using aidl::android::media::audio::common::AudioFormatDescription;
52 using aidl::android::media::audio::common::AudioFormatType;
53 using aidl::android::media::audio::common::AudioUuid;
54 using aidl::android::media::audio::common::PcmType;
55 using ::android::audio::utils::toString;
56 using ::android::hardware::EventFlag;
57
58 const AudioFormatDescription kDefaultFormatDescription = {
59 .type = AudioFormatType::PCM, .pcm = PcmType::FLOAT_32_BIT, .encoding = ""};
60
61 typedef ::android::AidlMessageQueue<IEffect::Status,
62 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
63 StatusMQ;
64 typedef ::android::AidlMessageQueue<float,
65 ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
66 DataMQ;
67
getPrefix(Descriptor & descriptor)68 static inline std::string getPrefix(Descriptor& descriptor) {
69 std::string prefix = "Implementor_" + descriptor.common.implementor + "_name_" +
70 descriptor.common.name + "_UUID_" + toString(descriptor.common.id.uuid);
71 return prefix;
72 }
73
74 class EffectHelper {
75 public:
76 static void create(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect>& effect,
77 Descriptor& desc, binder_status_t status = EX_NONE) {
78 ASSERT_NE(factory, nullptr);
79 auto& id = desc.common.id;
80 ASSERT_STATUS(status, factory->createEffect(id.uuid, &effect));
81 if (status == EX_NONE) {
82 ASSERT_NE(effect, nullptr) << toString(id.uuid);
83 }
84 }
85
destroyIgnoreRet(std::shared_ptr<IFactory> factory,std::shared_ptr<IEffect> effect)86 static void destroyIgnoreRet(std::shared_ptr<IFactory> factory,
87 std::shared_ptr<IEffect> effect) {
88 if (factory && effect) {
89 factory->destroyEffect(effect);
90 }
91 }
92
93 static void destroy(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect> effect,
94 binder_status_t status = EX_NONE) {
95 ASSERT_NE(factory, nullptr);
96 ASSERT_NE(effect, nullptr);
97 ASSERT_STATUS(status, factory->destroyEffect(effect));
98 }
99
100 static void open(std::shared_ptr<IEffect> effect, const Parameter::Common& common,
101 const std::optional<Parameter::Specific>& specific,
102 IEffect::OpenEffectReturn* ret, binder_status_t status = EX_NONE) {
103 ASSERT_NE(effect, nullptr);
104 ASSERT_STATUS(status, effect->open(common, specific, ret));
105 }
106
107 static void open(std::shared_ptr<IEffect> effect, int session = 0,
108 binder_status_t status = EX_NONE) {
109 ASSERT_NE(effect, nullptr);
110 Parameter::Common common = EffectHelper::createParamCommon(session);
111 IEffect::OpenEffectReturn ret;
112 ASSERT_NO_FATAL_FAILURE(open(effect, common, std::nullopt /* specific */, &ret, status));
113 }
114
closeIgnoreRet(std::shared_ptr<IEffect> effect)115 static void closeIgnoreRet(std::shared_ptr<IEffect> effect) {
116 if (effect) {
117 effect->close();
118 }
119 }
120 static void close(std::shared_ptr<IEffect> effect, binder_status_t status = EX_NONE) {
121 if (effect) {
122 ASSERT_STATUS(status, effect->close());
123 }
124 }
125 static void getDescriptor(std::shared_ptr<IEffect> effect, Descriptor& desc,
126 binder_status_t status = EX_NONE) {
127 ASSERT_NE(effect, nullptr);
128 ASSERT_STATUS(status, effect->getDescriptor(&desc));
129 }
130 static void expectState(std::shared_ptr<IEffect> effect, State expectState,
131 binder_status_t status = EX_NONE) {
132 ASSERT_NE(effect, nullptr);
133 State state;
134 ASSERT_STATUS(status, effect->getState(&state));
135 ASSERT_EQ(expectState, state);
136 }
commandIgnoreRet(std::shared_ptr<IEffect> effect,CommandId command)137 static void commandIgnoreRet(std::shared_ptr<IEffect> effect, CommandId command) {
138 if (effect) {
139 effect->command(command);
140 }
141 }
142 static void command(std::shared_ptr<IEffect> effect, CommandId command,
143 binder_status_t status = EX_NONE) {
144 ASSERT_NE(effect, nullptr);
145 ASSERT_STATUS(status, effect->command(command));
146 }
allocateInputData(const Parameter::Common common,std::unique_ptr<DataMQ> & mq,std::vector<float> & buffer)147 static void allocateInputData(const Parameter::Common common, std::unique_ptr<DataMQ>& mq,
148 std::vector<float>& buffer) {
149 ASSERT_NE(mq, nullptr);
150 auto frameSize = ::aidl::android::hardware::audio::common::getFrameSizeInBytes(
151 common.input.base.format, common.input.base.channelMask);
152 const size_t floatsToWrite = mq->availableToWrite();
153 ASSERT_NE(0UL, floatsToWrite);
154 ASSERT_EQ(frameSize * common.input.frameCount, floatsToWrite * sizeof(float));
155 buffer.resize(floatsToWrite);
156 std::fill(buffer.begin(), buffer.end(), 0x5a);
157 }
writeToFmq(std::unique_ptr<StatusMQ> & statusMq,std::unique_ptr<DataMQ> & dataMq,const std::vector<float> & buffer)158 static void writeToFmq(std::unique_ptr<StatusMQ>& statusMq, std::unique_ptr<DataMQ>& dataMq,
159 const std::vector<float>& buffer) {
160 const size_t available = dataMq->availableToWrite();
161 ASSERT_NE(0Ul, available);
162 auto bufferFloats = buffer.size();
163 auto floatsToWrite = std::min(available, bufferFloats);
164 ASSERT_TRUE(dataMq->write(buffer.data(), floatsToWrite));
165
166 EventFlag* efGroup;
167 ASSERT_EQ(::android::OK,
168 EventFlag::createEventFlag(statusMq->getEventFlagWord(), &efGroup));
169 ASSERT_NE(nullptr, efGroup);
170 efGroup->wake(kEventFlagNotEmpty);
171 ASSERT_EQ(::android::OK, EventFlag::deleteEventFlag(&efGroup));
172 }
173 static void readFromFmq(std::unique_ptr<StatusMQ>& statusMq, size_t statusNum,
174 std::unique_ptr<DataMQ>& dataMq, size_t expectFloats,
175 std::vector<float>& buffer,
176 std::optional<int> expectStatus = STATUS_OK) {
177 if (0 == statusNum) {
178 ASSERT_EQ(0ul, statusMq->availableToRead());
179 return;
180 }
181 IEffect::Status status{};
182 ASSERT_TRUE(statusMq->readBlocking(&status, statusNum));
183 if (expectStatus.has_value()) {
184 ASSERT_EQ(expectStatus.value(), status.status);
185 }
186
187 ASSERT_EQ(expectFloats, (unsigned)status.fmqProduced);
188 ASSERT_EQ(expectFloats, dataMq->availableToRead());
189 if (expectFloats != 0) {
190 ASSERT_TRUE(dataMq->read(buffer.data(), expectFloats));
191 }
192 }
193 static Parameter::Common createParamCommon(
194 int session = 0, int ioHandle = -1, int iSampleRate = 48000, int oSampleRate = 48000,
195 long iFrameCount = 0x100, long oFrameCount = 0x100,
196 AudioChannelLayout inputChannelLayout =
197 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
198 AudioChannelLayout::LAYOUT_STEREO),
199 AudioChannelLayout outputChannelLayout =
200 AudioChannelLayout::make<AudioChannelLayout::layoutMask>(
201 AudioChannelLayout::LAYOUT_STEREO)) {
202 Parameter::Common common;
203 common.session = session;
204 common.ioHandle = ioHandle;
205
206 auto& input = common.input;
207 auto& output = common.output;
208 input.base.sampleRate = iSampleRate;
209 input.base.channelMask = inputChannelLayout;
210 input.base.format = kDefaultFormatDescription;
211 input.frameCount = iFrameCount;
212 output.base.sampleRate = oSampleRate;
213 output.base.channelMask = outputChannelLayout;
214 output.base.format = kDefaultFormatDescription;
215 output.frameCount = oFrameCount;
216 return common;
217 }
218
219 typedef ::android::AidlMessageQueue<
220 IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
221 StatusMQ;
222 typedef ::android::AidlMessageQueue<
223 float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
224 DataMQ;
225
226 class EffectParam {
227 public:
228 std::unique_ptr<StatusMQ> statusMQ;
229 std::unique_ptr<DataMQ> inputMQ;
230 std::unique_ptr<DataMQ> outputMQ;
231 };
232
233 template <typename T, Range::Tag tag>
isParameterValid(const T & target,const Descriptor & desc)234 static bool isParameterValid(const T& target, const Descriptor& desc) {
235 if (desc.capability.range.getTag() != tag) {
236 return true;
237 }
238 const auto& ranges = desc.capability.range.get<tag>();
239 return inRange(target, ranges);
240 }
241
242 /**
243 * Add to test value set: (min+max)/2, minimum/maximum numeric limits, and min-1/max+1 if
244 * result still in numeric limits after -1/+1.
245 * Only use this when the type of test value is basic type (std::is_arithmetic return true).
246 */
247 template <typename S, typename = std::enable_if_t<std::is_arithmetic_v<S>>>
expandTestValueBasic(std::set<S> & s)248 static std::set<S> expandTestValueBasic(std::set<S>& s) {
249 const auto minLimit = std::numeric_limits<S>::min(),
250 maxLimit = std::numeric_limits<S>::max();
251 if (s.size()) {
252 const auto min = *s.begin(), max = *s.rbegin();
253 s.insert((min & max) + ((min ^ max) >> 1));
254 if (min > minLimit + 1) {
255 s.insert(min - 1);
256 }
257 if (max < maxLimit - 1) {
258 s.insert(max + 1);
259 }
260 }
261 s.insert(minLimit);
262 s.insert(maxLimit);
263 return s;
264 }
265
266 template <typename T, typename S, Range::Tag R, typename T::Tag tag, typename Functor>
getTestValueSet(std::vector<std::pair<std::shared_ptr<IFactory>,Descriptor>> kFactoryDescList,Functor functor)267 static std::set<S> getTestValueSet(
268 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList,
269 Functor functor) {
270 std::set<S> result;
271 for (const auto& [_, desc] : kFactoryDescList) {
272 if (desc.capability.range.getTag() == R) {
273 const auto& ranges = desc.capability.range.get<R>();
274 for (const auto& range : ranges) {
275 if (range.min.getTag() == tag) {
276 result.insert(range.min.template get<tag>());
277 }
278 if (range.max.getTag() == tag) {
279 result.insert(range.max.template get<tag>());
280 }
281 }
282 }
283 }
284 return functor(result);
285 }
286 };
287