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>
20 #define LOG_TAG "AHAL_AutomaticGainControlV2Sw"
21
22 #include <android-base/logging.h>
23 #include <fmq/AidlMessageQueue.h>
24 #include <system/audio_effects/effect_uuid.h>
25
26 #include "AutomaticGainControlV2Sw.h"
27
28 using aidl::android::hardware::audio::effect::AutomaticGainControlV2Sw;
29 using aidl::android::hardware::audio::effect::Descriptor;
30 using aidl::android::hardware::audio::effect::getEffectImplUuidAutomaticGainControlV2Sw;
31 using aidl::android::hardware::audio::effect::getEffectTypeUuidAutomaticGainControlV2;
32 using aidl::android::hardware::audio::effect::IEffect;
33 using aidl::android::media::audio::common::AudioUuid;
34
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)35 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
37 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV2Sw()) {
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
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)51 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
52 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAutomaticGainControlV2Sw()) {
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 = {
72 .common = {.id = {.type = getEffectTypeUuidAutomaticGainControlV2(),
73 .uuid = getEffectImplUuidAutomaticGainControlV2Sw(),
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
getDescriptor(Descriptor * _aidl_return)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
setParameterSpecific(const Parameter::Specific & specific)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
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)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
getParameterAutomaticGainControlV2(const AutomaticGainControlV2::Tag & tag,Parameter::Specific * specific)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
createContext(const Parameter::Common & common)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
releaseContext()183 RetCode AutomaticGainControlV2Sw::releaseContext() {
184 if (mContext) {
185 mContext.reset();
186 }
187 return RetCode::SUCCESS;
188 }
189
190 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)191 IEffect::Status AutomaticGainControlV2Sw::effectProcessImpl(float* in, float* out, int samples) {
192 // TODO: get data buffer and process.
193 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
194 for (int i = 0; i < samples; i++) {
195 *out++ = *in++;
196 }
197 return {STATUS_OK, samples, samples};
198 }
199
setDigitalGain(int gain)200 RetCode AutomaticGainControlV2SwContext::setDigitalGain(int gain) {
201 mDigitalGain = gain;
202 return RetCode::SUCCESS;
203 }
204
getDigitalGain()205 int AutomaticGainControlV2SwContext::getDigitalGain() {
206 return mDigitalGain;
207 }
208
setLevelEstimator(AutomaticGainControlV2::LevelEstimator levelEstimator)209 RetCode AutomaticGainControlV2SwContext::setLevelEstimator(
210 AutomaticGainControlV2::LevelEstimator levelEstimator) {
211 mLevelEstimator = levelEstimator;
212 return RetCode::SUCCESS;
213 }
214
getLevelEstimator()215 AutomaticGainControlV2::LevelEstimator AutomaticGainControlV2SwContext::getLevelEstimator() {
216 return mLevelEstimator;
217 }
218
setSaturationMargin(int margin)219 RetCode AutomaticGainControlV2SwContext::setSaturationMargin(int margin) {
220 mSaturationMargin = margin;
221 return RetCode::SUCCESS;
222 }
223
getSaturationMargin()224 int AutomaticGainControlV2SwContext::getSaturationMargin() {
225 return mSaturationMargin;
226 }
227
228 } // namespace aidl::android::hardware::audio::effect
229