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