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