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