1 /*
2 * Copyright (C) 2024 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 #define LOG_TAG "AHAL_Eraser"
18
19 #include "Eraser.h"
20
21 #include <android-base/logging.h>
22 #include <system/audio_effects/effect_uuid.h>
23
24 #include <optional>
25
26 using aidl::android::hardware::audio::common::getChannelCount;
27 using aidl::android::hardware::audio::effect::Descriptor;
28 using aidl::android::hardware::audio::effect::EraserSw;
29 using aidl::android::hardware::audio::effect::getEffectImplUuidEraserSw;
30 using aidl::android::hardware::audio::effect::getEffectTypeUuidEraser;
31 using aidl::android::hardware::audio::effect::IEffect;
32 using aidl::android::hardware::audio::effect::State;
33 using aidl::android::media::audio::common::AudioChannelLayout;
34 using aidl::android::media::audio::common::AudioUuid;
35
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)36 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37 std::shared_ptr<IEffect>* instanceSpp) {
38 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEraserSw()) {
39 LOG(ERROR) << __func__ << "uuid not supported";
40 return EX_ILLEGAL_ARGUMENT;
41 }
42 if (!instanceSpp) {
43 LOG(ERROR) << __func__ << " invalid input parameter!";
44 return EX_ILLEGAL_ARGUMENT;
45 }
46
47 *instanceSpp = ndk::SharedRefBase::make<EraserSw>();
48 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
49 return EX_NONE;
50 }
51
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)52 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
53 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidEraserSw()) {
54 LOG(ERROR) << __func__ << "uuid not supported";
55 return EX_ILLEGAL_ARGUMENT;
56 }
57 *_aidl_return = EraserSw::kDescriptor;
58 return EX_NONE;
59 }
60
61 namespace aidl::android::hardware::audio::effect {
62
63 const std::string EraserSw::kEffectName = "EraserSw";
64 const Descriptor EraserSw::kDescriptor = {
65 .common = {.id = {.type = getEffectTypeUuidEraser(), .uuid = getEffectImplUuidEraserSw()},
66 .flags = {.type = Flags::Type::INSERT,
67 .insert = Flags::Insert::FIRST,
68 .hwAcceleratorMode = Flags::HardwareAccelerator::NONE},
69 .name = EraserSw::kEffectName,
70 .implementor = "The Android Open Source Project"}};
71
getDescriptor(Descriptor * _aidl_return)72 ndk::ScopedAStatus EraserSw::getDescriptor(Descriptor* _aidl_return) {
73 LOG(DEBUG) << __func__ << kDescriptor.toString();
74 *_aidl_return = kDescriptor;
75 return ndk::ScopedAStatus::ok();
76 }
77
setParameterSpecific(const Parameter::Specific & specific)78 ndk::ScopedAStatus EraserSw::setParameterSpecific(const Parameter::Specific& specific) {
79 RETURN_IF(Parameter::Specific::eraser != specific.getTag(), EX_ILLEGAL_ARGUMENT,
80 "EffectNotSupported");
81 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
82
83 auto& param = specific.get<Parameter::Specific::eraser>();
84 return mContext->setParam(param.getTag(), param);
85 }
86
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)87 ndk::ScopedAStatus EraserSw::getParameterSpecific(const Parameter::Id& id,
88 Parameter::Specific* specific) {
89 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
90
91 auto tag = id.getTag();
92 RETURN_IF(Parameter::Id::eraserTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
93 auto eraserId = id.get<Parameter::Id::eraserTag>();
94 auto eraserTag = eraserId.getTag();
95 switch (eraserTag) {
96 case Eraser::Id::commonTag: {
97 auto specificTag = eraserId.get<Eraser::Id::commonTag>();
98 std::optional<Eraser> param = mContext->getParam(specificTag);
99 if (!param.has_value()) {
100 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
101 "EraserTagNotSupported");
102 }
103 specific->set<Parameter::Specific::eraser>(param.value());
104 break;
105 }
106 default: {
107 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
108 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
109 "EraserTagNotSupported");
110 }
111 }
112 return ndk::ScopedAStatus::ok();
113 }
114
createContext(const Parameter::Common & common)115 std::shared_ptr<EffectContext> EraserSw::createContext(const Parameter::Common& common) {
116 if (mContext) {
117 LOG(DEBUG) << __func__ << " context already exist";
118 } else {
119 mContext = std::make_shared<EraserSwContext>(1 /* statusFmqDepth */, common);
120 }
121 return mContext;
122 }
123
releaseContext()124 RetCode EraserSw::releaseContext() {
125 if (mContext) {
126 mContext.reset();
127 }
128 return RetCode::SUCCESS;
129 }
130
~EraserSw()131 EraserSw::~EraserSw() {
132 cleanUp();
133 LOG(DEBUG) << __func__;
134 }
135
command(CommandId command)136 ndk::ScopedAStatus EraserSw::command(CommandId command) {
137 std::lock_guard lg(mImplMutex);
138 RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "instanceNotOpen");
139
140 switch (command) {
141 case CommandId::START:
142 RETURN_OK_IF(mState == State::PROCESSING);
143 mState = State::PROCESSING;
144 mContext->enable();
145 startThread();
146 RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
147 "notifyEventFlagNotEmptyFailed");
148 break;
149 case CommandId::STOP:
150 RETURN_OK_IF(mState == State::IDLE || mState == State::DRAINING);
151 if (mVersion < kDrainSupportedVersion) {
152 mState = State::IDLE;
153 stopThread();
154 mContext->disable();
155 } else {
156 mState = State::DRAINING;
157 startDraining();
158 mContext->startDraining();
159 }
160 RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
161 "notifyEventFlagNotEmptyFailed");
162 break;
163 case CommandId::RESET:
164 mState = State::IDLE;
165 RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
166 "notifyEventFlagNotEmptyFailed");
167 stopThread();
168 mImplContext->disable();
169 mImplContext->reset();
170 mImplContext->resetBuffer();
171 break;
172 default:
173 LOG(ERROR) << getEffectNameWithVersion() << __func__ << " instance still processing";
174 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
175 "CommandIdNotSupported");
176 }
177 LOG(VERBOSE) << getEffectNameWithVersion() << __func__
178 << " transfer to state: " << toString(mState);
179 return ndk::ScopedAStatus::ok();
180 }
181
182 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)183 IEffect::Status EraserSw::effectProcessImpl(float* in, float* out, int samples) {
184 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
185 IEffect::Status procStatus{STATUS_NOT_ENOUGH_DATA, 0, 0};
186 procStatus = mContext->process(in, out, samples);
187 if (mState == State::DRAINING && procStatus.status == STATUS_NOT_ENOUGH_DATA) {
188 drainingComplete_l();
189 }
190
191 return procStatus;
192 }
193
drainingComplete_l()194 void EraserSw::drainingComplete_l() {
195 if (mState != State::DRAINING) return;
196
197 LOG(DEBUG) << getEffectNameWithVersion() << __func__;
198 finishDraining();
199 mState = State::IDLE;
200 }
201
EraserSwContext(int statusDepth,const Parameter::Common & common)202 EraserSwContext::EraserSwContext(int statusDepth, const Parameter::Common& common)
203 : EffectContext(statusDepth, common) {
204 LOG(DEBUG) << __func__;
205 }
206
~EraserSwContext()207 EraserSwContext::~EraserSwContext() {
208 LOG(DEBUG) << __func__;
209 }
210
211 template <typename TAG>
getParam(TAG tag)212 std::optional<Eraser> EraserSwContext::getParam(TAG tag) {
213 if (mParamsMap.find(tag) != mParamsMap.end()) {
214 return mParamsMap.at(tag);
215 }
216 return std::nullopt;
217 }
218
219 template <typename TAG>
setParam(TAG tag,Eraser eraser)220 ndk::ScopedAStatus EraserSwContext::setParam(TAG tag, Eraser eraser) {
221 mParamsMap[tag] = eraser;
222 return ndk::ScopedAStatus::ok();
223 }
224
process(float * in,float * out,int samples)225 IEffect::Status EraserSwContext::process(float* in, float* out, int samples) {
226 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
227 IEffect::Status procStatus = {EX_ILLEGAL_ARGUMENT, 0, 0};
228 const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask);
229 const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask);
230 if (inputChannelCount < outputChannelCount) {
231 LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount
232 << " out: " << outputChannelCount;
233 return procStatus;
234 }
235
236 if (samples <= 0 || 0 != samples % inputChannelCount) {
237 LOG(ERROR) << __func__ << " invalid samples: " << samples;
238 return procStatus;
239 }
240
241 const int iFrames = samples / inputChannelCount;
242 const float gainPerSample = 1.f / iFrames;
243 for (int i = 0; i < iFrames; i++) {
244 if (isDraining()) {
245 const float gain = (iFrames - i - 1) * gainPerSample;
246 for (size_t c = 0; c < outputChannelCount; c++) {
247 out[c] = in[c] * gain;
248 }
249 } else {
250 std::memcpy(out, in, outputChannelCount * sizeof(float));
251 }
252
253 in += inputChannelCount;
254 out += outputChannelCount;
255 }
256
257 // drain for one cycle
258 if (isDraining()) {
259 procStatus.status = STATUS_NOT_ENOUGH_DATA;
260 finishDraining();
261 } else {
262 procStatus.status = STATUS_OK;
263 }
264 procStatus.fmqConsumed = static_cast<int32_t>(iFrames * inputChannelCount);
265 procStatus.fmqProduced = static_cast<int32_t>(iFrames * outputChannelCount);
266
267 return procStatus;
268 }
269
270 } // namespace aidl::android::hardware::audio::effect
271