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, |
| 61 | Descriptor::Identity id, binder_status_t status = EX_NONE) { |
| 62 | ASSERT_NE(factory, nullptr); |
| 63 | EXPECT_STATUS(status, factory->createEffect(id.uuid, &effect)); |
| 64 | if (status == EX_NONE) { |
| 65 | ASSERT_NE(effect, nullptr) << id.uuid.toString(); |
| 66 | } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 69 | static void destroy(std::shared_ptr<IFactory> factory, std::shared_ptr<IEffect> effect, |
| 70 | binder_status_t status = EX_NONE) { |
| 71 | ASSERT_NE(factory, nullptr); |
| 72 | ASSERT_NE(effect, nullptr); |
| 73 | EXPECT_STATUS(status, factory->destroyEffect(effect)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 76 | static void open(std::shared_ptr<IEffect> effect, const Parameter::Common& common, |
| 77 | const std::optional<Parameter::Specific>& specific, |
| 78 | IEffect::OpenEffectReturn* ret, binder_status_t status = EX_NONE) { |
| 79 | ASSERT_NE(effect, nullptr); |
| 80 | EXPECT_STATUS(status, effect->open(common, specific, ret)); |
| 81 | } |
| 82 | |
| 83 | static void open(std::shared_ptr<IEffect> effect, int session = 0, |
| 84 | binder_status_t status = EX_NONE) { |
| 85 | ASSERT_NE(effect, nullptr); |
| 86 | Parameter::Common common = EffectHelper::createParamCommon(session); |
| 87 | IEffect::OpenEffectReturn ret; |
| 88 | open(effect, common, std::nullopt /* specific */, &ret, status); |
| 89 | } |
| 90 | |
| 91 | static void close(std::shared_ptr<IEffect> effect, binder_status_t status = EX_NONE) { |
| 92 | if (effect) { |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 93 | EXPECT_STATUS(status, effect->close()); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 96 | static void getDescriptor(std::shared_ptr<IEffect> effect, Descriptor& desc, |
| 97 | binder_status_t status = EX_NONE) { |
| 98 | ASSERT_NE(effect, nullptr); |
| 99 | EXPECT_STATUS(status, effect->getDescriptor(&desc)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 100 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 101 | static void expectState(std::shared_ptr<IEffect> effect, State expectState, |
| 102 | binder_status_t status = EX_NONE) { |
| 103 | ASSERT_NE(effect, nullptr); |
| 104 | State state; |
| 105 | EXPECT_STATUS(status, effect->getState(&state)); |
| 106 | EXPECT_EQ(expectState, state); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 107 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 108 | static void command(std::shared_ptr<IEffect> effect, CommandId command, |
| 109 | binder_status_t status = EX_NONE) { |
| 110 | ASSERT_NE(effect, nullptr); |
| 111 | EXPECT_STATUS(status, effect->command(command)); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 112 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 113 | static void allocateInputData(const Parameter::Common common, std::unique_ptr<DataMQ>& mq, |
| 114 | std::vector<float>& buffer) { |
| 115 | ASSERT_NE(mq, nullptr); |
| 116 | auto frameSize = android::hardware::audio::common::getFrameSizeInBytes( |
| 117 | common.input.base.format, common.input.base.channelMask); |
| 118 | const size_t floatsToWrite = mq->availableToWrite(); |
| 119 | EXPECT_NE(0UL, floatsToWrite); |
| 120 | EXPECT_EQ(frameSize * common.input.frameCount, floatsToWrite * sizeof(float)); |
| 121 | buffer.resize(floatsToWrite); |
| 122 | std::fill(buffer.begin(), buffer.end(), 0x5a); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 123 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 124 | static void writeToFmq(std::unique_ptr<DataMQ>& mq, const std::vector<float>& buffer) { |
| 125 | const size_t available = mq->availableToWrite(); |
| 126 | EXPECT_NE(0Ul, available); |
| 127 | auto bufferFloats = buffer.size(); |
| 128 | auto floatsToWrite = std::min(available, bufferFloats); |
| 129 | EXPECT_TRUE(mq->write(buffer.data(), floatsToWrite)); |
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 readFromFmq(std::unique_ptr<StatusMQ>& statusMq, size_t statusNum, |
| 132 | std::unique_ptr<DataMQ>& dataMq, size_t expectFloats, |
| 133 | std::vector<float>& buffer) { |
| 134 | IEffect::Status status{}; |
| 135 | EXPECT_TRUE(statusMq->readBlocking(&status, statusNum)); |
| 136 | EXPECT_EQ(STATUS_OK, status.status); |
| 137 | if (statusNum != 0) { |
| 138 | EXPECT_EQ(expectFloats, (unsigned)status.fmqProduced); |
| 139 | EXPECT_EQ(expectFloats, dataMq->availableToRead()); |
| 140 | if (expectFloats != 0) { |
| 141 | EXPECT_TRUE(dataMq->read(buffer.data(), expectFloats)); |
| 142 | } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 145 | static Parameter::Common createParamCommon( |
| 146 | int session = 0, int ioHandle = -1, int iSampleRate = 48000, int oSampleRate = 48000, |
| 147 | long iFrameCount = 0x100, long oFrameCount = 0x100, |
| 148 | AudioChannelLayout inputChannelLayout = |
| 149 | AudioChannelLayout::make<AudioChannelLayout::layoutMask>( |
| 150 | AudioChannelLayout::LAYOUT_STEREO), |
| 151 | AudioChannelLayout outputChannelLayout = |
| 152 | AudioChannelLayout::make<AudioChannelLayout::layoutMask>( |
| 153 | AudioChannelLayout::LAYOUT_STEREO)) { |
| 154 | Parameter::Common common; |
| 155 | common.session = session; |
| 156 | common.ioHandle = ioHandle; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 157 | |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 158 | auto& input = common.input; |
| 159 | auto& output = common.output; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 160 | input.base.sampleRate = iSampleRate; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 161 | input.base.channelMask = inputChannelLayout; |
| 162 | input.base.format = kDefaultFormatDescription; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 163 | input.frameCount = iFrameCount; |
| 164 | output.base.sampleRate = oSampleRate; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 165 | output.base.channelMask = outputChannelLayout; |
| 166 | output.base.format = kDefaultFormatDescription; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 167 | output.frameCount = oFrameCount; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 168 | return common; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 171 | typedef ::android::AidlMessageQueue< |
| 172 | IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
| 173 | StatusMQ; |
| 174 | typedef ::android::AidlMessageQueue< |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 175 | float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 176 | DataMQ; |
| 177 | |
| 178 | class EffectParam { |
| 179 | public: |
| 180 | std::unique_ptr<StatusMQ> statusMQ; |
| 181 | std::unique_ptr<DataMQ> inputMQ; |
| 182 | std::unique_ptr<DataMQ> outputMQ; |
| 183 | }; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 184 | }; |