blob: 59cc9a2dc75f2de5f33e68fa28bb5c39e71a4583 [file] [log] [blame]
Shunkai Yao922576f2024-10-17 00:05:58 +00001/*
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
26using aidl::android::hardware::audio::common::getChannelCount;
27using aidl::android::hardware::audio::effect::Descriptor;
28using aidl::android::hardware::audio::effect::EraserSw;
29using aidl::android::hardware::audio::effect::getEffectImplUuidEraserSw;
30using aidl::android::hardware::audio::effect::getEffectTypeUuidEraser;
31using aidl::android::hardware::audio::effect::IEffect;
32using aidl::android::hardware::audio::effect::State;
33using aidl::android::media::audio::common::AudioChannelLayout;
34using aidl::android::media::audio::common::AudioUuid;
35
36extern "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
52extern "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
61namespace aidl::android::hardware::audio::effect {
62
63const std::string EraserSw::kEffectName = "EraserSw";
64const 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
72ndk::ScopedAStatus EraserSw::getDescriptor(Descriptor* _aidl_return) {
73 LOG(DEBUG) << __func__ << kDescriptor.toString();
74 *_aidl_return = kDescriptor;
75 return ndk::ScopedAStatus::ok();
76}
77
78ndk::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
87ndk::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
115std::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
124RetCode EraserSw::releaseContext() {
125 if (mContext) {
126 mContext.reset();
127 }
128 return RetCode::SUCCESS;
129}
130
131EraserSw::~EraserSw() {
132 cleanUp();
133 LOG(DEBUG) << __func__;
134}
135
Shunkai Yao240c1d42024-11-24 18:40:58 +0000136ndk::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
Shunkai Yao922576f2024-10-17 00:05:58 +0000182// Processing method running in EffectWorker thread.
183IEffect::Status EraserSw::effectProcessImpl(float* in, float* out, int samples) {
184 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
Shunkai Yao240c1d42024-11-24 18:40:58 +0000185 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
194void EraserSw::drainingComplete_l() {
195 if (mState != State::DRAINING) return;
196
197 LOG(DEBUG) << getEffectNameWithVersion() << __func__;
198 finishDraining();
199 mState = State::IDLE;
Shunkai Yao922576f2024-10-17 00:05:58 +0000200}
201
202EraserSwContext::EraserSwContext(int statusDepth, const Parameter::Common& common)
203 : EffectContext(statusDepth, common) {
204 LOG(DEBUG) << __func__;
205}
206
207EraserSwContext::~EraserSwContext() {
208 LOG(DEBUG) << __func__;
209}
210
211template <typename TAG>
212std::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
219template <typename TAG>
220ndk::ScopedAStatus EraserSwContext::setParam(TAG tag, Eraser eraser) {
221 mParamsMap[tag] = eraser;
222 return ndk::ScopedAStatus::ok();
223}
224
225IEffect::Status EraserSwContext::process(float* in, float* out, int samples) {
226 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
Shunkai Yao240c1d42024-11-24 18:40:58 +0000227 IEffect::Status procStatus = {EX_ILLEGAL_ARGUMENT, 0, 0};
Shunkai Yao922576f2024-10-17 00:05:58 +0000228 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;
Shunkai Yao240c1d42024-11-24 18:40:58 +0000233 return procStatus;
Shunkai Yao922576f2024-10-17 00:05:58 +0000234 }
235
Shunkai Yao240c1d42024-11-24 18:40:58 +0000236 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;
Shunkai Yao922576f2024-10-17 00:05:58 +0000243 for (int i = 0; i < iFrames; i++) {
Shunkai Yao240c1d42024-11-24 18:40:58 +0000244 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
Shunkai Yao922576f2024-10-17 00:05:58 +0000253 in += inputChannelCount;
254 out += outputChannelCount;
255 }
Shunkai Yao240c1d42024-11-24 18:40:58 +0000256
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;
Shunkai Yao922576f2024-10-17 00:05:58 +0000268}
269
270} // namespace aidl::android::hardware::audio::effect