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> |
| 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
| 25 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 26 | #include <aidl/android/hardware/audio/effect/IFactory.h> |
| 27 | #include <aidl/android/media/audio/common/AudioChannelLayout.h> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 28 | #include <android/binder_auto_utils.h> |
| 29 | #include <fmq/AidlMessageQueue.h> |
| 30 | |
| 31 | #include "AudioHalBinderServiceUtil.h" |
| 32 | #include "EffectFactoryHelper.h" |
| 33 | #include "TestUtils.h" |
| 34 | |
| 35 | using namespace android; |
| 36 | using aidl::android::hardware::audio::effect::CommandId; |
| 37 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 38 | using aidl::android::hardware::audio::effect::IEffect; |
| 39 | using aidl::android::hardware::audio::effect::Parameter; |
| 40 | using aidl::android::hardware::audio::effect::State; |
| 41 | using aidl::android::hardware::common::fmq::SynchronizedReadWrite; |
| 42 | using aidl::android::media::audio::common::AudioChannelLayout; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 43 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 44 | using aidl::android::media::audio::common::AudioFormatType; |
| 45 | using aidl::android::media::audio::common::AudioUuid; |
| 46 | using aidl::android::media::audio::common::PcmType; |
| 47 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 48 | const AudioFormatDescription kDefaultFormatDescription = { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 49 | .type = AudioFormatType::PCM, .pcm = PcmType::FLOAT_32_BIT, .encoding = ""}; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 50 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 51 | typedef ::android::AidlMessageQueue<IEffect::Status, |
| 52 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
| 53 | StatusMQ; |
| 54 | typedef ::android::AidlMessageQueue<float, |
| 55 | ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
| 56 | DataMQ; |
| 57 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 58 | class EffectHelper { |
| 59 | public: |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 60 | 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] | 61 | Descriptor& desc, binder_status_t status = EX_NONE) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 62 | ASSERT_NE(factory, nullptr); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 63 | auto& id = desc.common.id; |
| 64 | ASSERT_STATUS(status, factory->createEffect(id.uuid, &effect)); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 65 | if (status == EX_NONE) { |
| 66 | ASSERT_NE(effect, nullptr) << id.uuid.toString(); |
| 67 | } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 70 | static void destroyIgnoreRet(std::shared_ptr<IFactory> factory, |
| 71 | std::shared_ptr<IEffect> effect) { |
| 72 | if (factory && effect) { |
| 73 | factory->destroyEffect(effect); |
| 74 | } |
| 75 | } |
| 76 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 77 | static void destroy(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect> effect, |
| 78 | binder_status_t status = EX_NONE) { |
| 79 | ASSERT_NE(factory, nullptr); |
| 80 | ASSERT_NE(effect, nullptr); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 81 | ASSERT_STATUS(status, factory->destroyEffect(effect)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 84 | static void open(std::shared_ptr<IEffect> effect, const Parameter::Common& common, |
| 85 | const std::optional<Parameter::Specific>& specific, |
| 86 | IEffect::OpenEffectReturn* ret, binder_status_t status = EX_NONE) { |
| 87 | ASSERT_NE(effect, nullptr); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 88 | ASSERT_STATUS(status, effect->open(common, specific, ret)); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void open(std::shared_ptr<IEffect> effect, int session = 0, |
| 92 | binder_status_t status = EX_NONE) { |
| 93 | ASSERT_NE(effect, nullptr); |
| 94 | Parameter::Common common = EffectHelper::createParamCommon(session); |
| 95 | IEffect::OpenEffectReturn ret; |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 96 | ASSERT_NO_FATAL_FAILURE(open(effect, common, std::nullopt /* specific */, &ret, status)); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 99 | static void closeIgnoreRet(std::shared_ptr<IEffect> effect) { |
| 100 | if (effect) { |
| 101 | effect->close(); |
| 102 | } |
| 103 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 104 | static void close(std::shared_ptr<IEffect> effect, binder_status_t status = EX_NONE) { |
| 105 | if (effect) { |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 106 | ASSERT_STATUS(status, effect->close()); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 109 | static void getDescriptor(std::shared_ptr<IEffect> effect, Descriptor& desc, |
| 110 | binder_status_t status = EX_NONE) { |
| 111 | ASSERT_NE(effect, nullptr); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 112 | ASSERT_STATUS(status, effect->getDescriptor(&desc)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 113 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 114 | static void expectState(std::shared_ptr<IEffect> effect, State expectState, |
| 115 | binder_status_t status = EX_NONE) { |
| 116 | ASSERT_NE(effect, nullptr); |
| 117 | State state; |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 118 | ASSERT_STATUS(status, effect->getState(&state)); |
| 119 | ASSERT_EQ(expectState, state); |
| 120 | } |
| 121 | static void commandIgnoreRet(std::shared_ptr<IEffect> effect, CommandId command) { |
| 122 | if (effect) { |
| 123 | effect->command(command); |
| 124 | } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 125 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 126 | static void command(std::shared_ptr<IEffect> effect, CommandId command, |
| 127 | binder_status_t status = EX_NONE) { |
| 128 | ASSERT_NE(effect, nullptr); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 129 | ASSERT_STATUS(status, effect->command(command)); |
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 allocateInputData(const Parameter::Common common, std::unique_ptr<DataMQ>& mq, |
| 132 | std::vector<float>& buffer) { |
| 133 | ASSERT_NE(mq, nullptr); |
| 134 | auto frameSize = android::hardware::audio::common::getFrameSizeInBytes( |
| 135 | common.input.base.format, common.input.base.channelMask); |
| 136 | const size_t floatsToWrite = mq->availableToWrite(); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 137 | ASSERT_NE(0UL, floatsToWrite); |
| 138 | ASSERT_EQ(frameSize * common.input.frameCount, floatsToWrite * sizeof(float)); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 139 | buffer.resize(floatsToWrite); |
| 140 | std::fill(buffer.begin(), buffer.end(), 0x5a); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 141 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 142 | static void writeToFmq(std::unique_ptr<DataMQ>& mq, const std::vector<float>& buffer) { |
| 143 | const size_t available = mq->availableToWrite(); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 144 | ASSERT_NE(0Ul, available); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 145 | auto bufferFloats = buffer.size(); |
| 146 | auto floatsToWrite = std::min(available, bufferFloats); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 147 | ASSERT_TRUE(mq->write(buffer.data(), floatsToWrite)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 148 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 149 | static void readFromFmq(std::unique_ptr<StatusMQ>& statusMq, size_t statusNum, |
| 150 | std::unique_ptr<DataMQ>& dataMq, size_t expectFloats, |
| 151 | std::vector<float>& buffer) { |
| 152 | IEffect::Status status{}; |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 153 | ASSERT_TRUE(statusMq->readBlocking(&status, statusNum)); |
| 154 | ASSERT_EQ(STATUS_OK, status.status); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 155 | if (statusNum != 0) { |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 156 | ASSERT_EQ(expectFloats, (unsigned)status.fmqProduced); |
| 157 | ASSERT_EQ(expectFloats, dataMq->availableToRead()); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 158 | if (expectFloats != 0) { |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 159 | ASSERT_TRUE(dataMq->read(buffer.data(), expectFloats)); |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 160 | } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 163 | static Parameter::Common createParamCommon( |
| 164 | int session = 0, int ioHandle = -1, int iSampleRate = 48000, int oSampleRate = 48000, |
| 165 | long iFrameCount = 0x100, long oFrameCount = 0x100, |
| 166 | AudioChannelLayout inputChannelLayout = |
| 167 | AudioChannelLayout::make<AudioChannelLayout::layoutMask>( |
| 168 | AudioChannelLayout::LAYOUT_STEREO), |
| 169 | AudioChannelLayout outputChannelLayout = |
| 170 | AudioChannelLayout::make<AudioChannelLayout::layoutMask>( |
| 171 | AudioChannelLayout::LAYOUT_STEREO)) { |
| 172 | Parameter::Common common; |
| 173 | common.session = session; |
| 174 | common.ioHandle = ioHandle; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 175 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 176 | auto& input = common.input; |
| 177 | auto& output = common.output; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 178 | input.base.sampleRate = iSampleRate; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 179 | input.base.channelMask = inputChannelLayout; |
| 180 | input.base.format = kDefaultFormatDescription; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 181 | input.frameCount = iFrameCount; |
| 182 | output.base.sampleRate = oSampleRate; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 183 | output.base.channelMask = outputChannelLayout; |
| 184 | output.base.format = kDefaultFormatDescription; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 185 | output.frameCount = oFrameCount; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 186 | return common; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 189 | typedef ::android::AidlMessageQueue< |
| 190 | IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
| 191 | StatusMQ; |
| 192 | typedef ::android::AidlMessageQueue< |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 193 | float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 194 | DataMQ; |
| 195 | |
| 196 | class EffectParam { |
| 197 | public: |
| 198 | std::unique_ptr<StatusMQ> statusMQ; |
| 199 | std::unique_ptr<DataMQ> inputMQ; |
| 200 | std::unique_ptr<DataMQ> outputMQ; |
| 201 | }; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 202 | }; |