Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [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 | #include <algorithm> |
| 18 | #include <cstddef> |
| 19 | #include <memory> |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 20 | #define LOG_TAG "AHAL_AutomaticGainControlV2Sw" |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 21 | |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <fmq/AidlMessageQueue.h> |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 24 | #include <system/audio_effects/effect_uuid.h> |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 25 | |
| 26 | #include "AutomaticGainControlV2Sw.h" |
| 27 | |
| 28 | using aidl::android::hardware::audio::effect::AutomaticGainControlV2Sw; |
| 29 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::getEffectImplUuidAutomaticGainControlV2Sw; |
| 31 | using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV2; |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::IEffect; |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 36 | std::shared_ptr<IEffect>* instanceSpp) { |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 37 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV2Sw()) { |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 38 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 39 | return EX_ILLEGAL_ARGUMENT; |
| 40 | } |
| 41 | if (instanceSpp) { |
| 42 | *instanceSpp = ndk::SharedRefBase::make<AutomaticGainControlV2Sw>(); |
| 43 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 44 | return EX_NONE; |
| 45 | } else { |
| 46 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 47 | return EX_ILLEGAL_ARGUMENT; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 52 | if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV2Sw()) { |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 53 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 54 | return EX_ILLEGAL_ARGUMENT; |
| 55 | } |
| 56 | *_aidl_return = AutomaticGainControlV2Sw::kDescriptor; |
| 57 | return EX_NONE; |
| 58 | } |
| 59 | |
| 60 | namespace aidl::android::hardware::audio::effect { |
| 61 | |
| 62 | const std::string AutomaticGainControlV2Sw::kEffectName = "AutomaticGainControlV2Sw"; |
| 63 | |
| 64 | const std::vector<Range::AutomaticGainControlV2Range> AutomaticGainControlV2Sw::kRanges = { |
| 65 | MAKE_RANGE(AutomaticGainControlV2, fixedDigitalGainMb, 0, 50000), |
| 66 | MAKE_RANGE(AutomaticGainControlV2, saturationMarginMb, 0, 10000)}; |
| 67 | |
| 68 | const Capability AutomaticGainControlV2Sw::kCapability = { |
| 69 | .range = AutomaticGainControlV2Sw::kRanges}; |
| 70 | |
| 71 | const Descriptor AutomaticGainControlV2Sw::kDescriptor = { |
Shunkai Yao | 4b3434f | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 72 | .common = {.id = {.type = getEffectTypeUuidAutomaticGainControlV2(), |
| 73 | .uuid = getEffectImplUuidAutomaticGainControlV2Sw(), |
Shraddha Basantwani | 84ea32e | 2023-02-01 16:22:37 +0530 | [diff] [blame] | 74 | .proxy = std::nullopt}, |
| 75 | .flags = {.type = Flags::Type::INSERT, |
| 76 | .insert = Flags::Insert::FIRST, |
| 77 | .volume = Flags::Volume::CTRL}, |
| 78 | .name = AutomaticGainControlV2Sw::kEffectName, |
| 79 | .implementor = "The Android Open Source Project"}, |
| 80 | .capability = AutomaticGainControlV2Sw::kCapability}; |
| 81 | |
| 82 | ndk::ScopedAStatus AutomaticGainControlV2Sw::getDescriptor(Descriptor* _aidl_return) { |
| 83 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 84 | *_aidl_return = kDescriptor; |
| 85 | return ndk::ScopedAStatus::ok(); |
| 86 | } |
| 87 | |
| 88 | ndk::ScopedAStatus AutomaticGainControlV2Sw::setParameterSpecific( |
| 89 | const Parameter::Specific& specific) { |
| 90 | RETURN_IF(Parameter::Specific::automaticGainControlV2 != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 91 | "EffectNotSupported"); |
| 92 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 93 | |
| 94 | auto& param = specific.get<Parameter::Specific::automaticGainControlV2>(); |
| 95 | RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange"); |
| 96 | auto tag = param.getTag(); |
| 97 | switch (tag) { |
| 98 | case AutomaticGainControlV2::fixedDigitalGainMb: { |
| 99 | RETURN_IF(mContext->setDigitalGain( |
| 100 | param.get<AutomaticGainControlV2::fixedDigitalGainMb>()) != |
| 101 | RetCode::SUCCESS, |
| 102 | EX_ILLEGAL_ARGUMENT, "digitalGainNotSupported"); |
| 103 | return ndk::ScopedAStatus::ok(); |
| 104 | } |
| 105 | case AutomaticGainControlV2::levelEstimator: { |
| 106 | RETURN_IF(mContext->setLevelEstimator( |
| 107 | param.get<AutomaticGainControlV2::levelEstimator>()) != |
| 108 | RetCode::SUCCESS, |
| 109 | EX_ILLEGAL_ARGUMENT, "levelEstimatorNotSupported"); |
| 110 | return ndk::ScopedAStatus::ok(); |
| 111 | } |
| 112 | case AutomaticGainControlV2::saturationMarginMb: { |
| 113 | RETURN_IF(mContext->setSaturationMargin( |
| 114 | param.get<AutomaticGainControlV2::saturationMarginMb>()) != |
| 115 | RetCode::SUCCESS, |
| 116 | EX_ILLEGAL_ARGUMENT, "saturationMarginNotSupported"); |
| 117 | return ndk::ScopedAStatus::ok(); |
| 118 | } |
| 119 | default: { |
| 120 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 121 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 122 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV2TagNotSupported"); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ndk::ScopedAStatus AutomaticGainControlV2Sw::getParameterSpecific(const Parameter::Id& id, |
| 128 | Parameter::Specific* specific) { |
| 129 | auto tag = id.getTag(); |
| 130 | RETURN_IF(Parameter::Id::automaticGainControlV2Tag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 131 | auto specificId = id.get<Parameter::Id::automaticGainControlV2Tag>(); |
| 132 | auto specificIdTag = specificId.getTag(); |
| 133 | switch (specificIdTag) { |
| 134 | case AutomaticGainControlV2::Id::commonTag: |
| 135 | return getParameterAutomaticGainControlV2( |
| 136 | specificId.get<AutomaticGainControlV2::Id::commonTag>(), specific); |
| 137 | default: |
| 138 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 139 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 140 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV2TagNotSupported"); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | ndk::ScopedAStatus AutomaticGainControlV2Sw::getParameterAutomaticGainControlV2( |
| 145 | const AutomaticGainControlV2::Tag& tag, Parameter::Specific* specific) { |
| 146 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 147 | AutomaticGainControlV2 param; |
| 148 | switch (tag) { |
| 149 | case AutomaticGainControlV2::fixedDigitalGainMb: { |
| 150 | param.set<AutomaticGainControlV2::fixedDigitalGainMb>(mContext->getDigitalGain()); |
| 151 | break; |
| 152 | } |
| 153 | case AutomaticGainControlV2::levelEstimator: { |
| 154 | param.set<AutomaticGainControlV2::levelEstimator>(mContext->getLevelEstimator()); |
| 155 | break; |
| 156 | } |
| 157 | case AutomaticGainControlV2::saturationMarginMb: { |
| 158 | param.set<AutomaticGainControlV2::saturationMarginMb>(mContext->getSaturationMargin()); |
| 159 | break; |
| 160 | } |
| 161 | default: { |
| 162 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 163 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 164 | EX_ILLEGAL_ARGUMENT, "AutomaticGainControlV2TagNotSupported"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | specific->set<Parameter::Specific::automaticGainControlV2>(param); |
| 169 | return ndk::ScopedAStatus::ok(); |
| 170 | } |
| 171 | |
| 172 | std::shared_ptr<EffectContext> AutomaticGainControlV2Sw::createContext( |
| 173 | const Parameter::Common& common) { |
| 174 | if (mContext) { |
| 175 | LOG(DEBUG) << __func__ << " context already exist"; |
| 176 | } else { |
| 177 | mContext = |
| 178 | std::make_shared<AutomaticGainControlV2SwContext>(1 /* statusFmqDepth */, common); |
| 179 | } |
| 180 | return mContext; |
| 181 | } |
| 182 | |
| 183 | std::shared_ptr<EffectContext> AutomaticGainControlV2Sw::getContext() { |
| 184 | return mContext; |
| 185 | } |
| 186 | |
| 187 | RetCode AutomaticGainControlV2Sw::releaseContext() { |
| 188 | if (mContext) { |
| 189 | mContext.reset(); |
| 190 | } |
| 191 | return RetCode::SUCCESS; |
| 192 | } |
| 193 | |
| 194 | // Processing method running in EffectWorker thread. |
| 195 | IEffect::Status AutomaticGainControlV2Sw::effectProcessImpl(float* in, float* out, int samples) { |
| 196 | // TODO: get data buffer and process. |
| 197 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 198 | for (int i = 0; i < samples; i++) { |
| 199 | *out++ = *in++; |
| 200 | } |
| 201 | return {STATUS_OK, samples, samples}; |
| 202 | } |
| 203 | |
| 204 | RetCode AutomaticGainControlV2SwContext::setDigitalGain(int gain) { |
| 205 | mDigitalGain = gain; |
| 206 | return RetCode::SUCCESS; |
| 207 | } |
| 208 | |
| 209 | int AutomaticGainControlV2SwContext::getDigitalGain() { |
| 210 | return mDigitalGain; |
| 211 | } |
| 212 | |
| 213 | RetCode AutomaticGainControlV2SwContext::setLevelEstimator( |
| 214 | AutomaticGainControlV2::LevelEstimator levelEstimator) { |
| 215 | mLevelEstimator = levelEstimator; |
| 216 | return RetCode::SUCCESS; |
| 217 | } |
| 218 | |
| 219 | AutomaticGainControlV2::LevelEstimator AutomaticGainControlV2SwContext::getLevelEstimator() { |
| 220 | return mLevelEstimator; |
| 221 | } |
| 222 | |
| 223 | RetCode AutomaticGainControlV2SwContext::setSaturationMargin(int margin) { |
| 224 | mSaturationMargin = margin; |
| 225 | return RetCode::SUCCESS; |
| 226 | } |
| 227 | |
| 228 | int AutomaticGainControlV2SwContext::getSaturationMargin() { |
| 229 | return mSaturationMargin; |
| 230 | } |
| 231 | |
| 232 | } // namespace aidl::android::hardware::audio::effect |