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