blob: 157ec79831f98ace9c057b6798b5aba93ed210d2 [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
136// Processing method running in EffectWorker thread.
137IEffect::Status EraserSw::effectProcessImpl(float* in, float* out, int samples) {
138 RETURN_VALUE_IF(!mContext, (IEffect::Status{EX_NULL_POINTER, 0, 0}), "nullContext");
139 return mContext->process(in, out, samples);
140}
141
142EraserSwContext::EraserSwContext(int statusDepth, const Parameter::Common& common)
143 : EffectContext(statusDepth, common) {
144 LOG(DEBUG) << __func__;
145}
146
147EraserSwContext::~EraserSwContext() {
148 LOG(DEBUG) << __func__;
149}
150
151template <typename TAG>
152std::optional<Eraser> EraserSwContext::getParam(TAG tag) {
153 if (mParamsMap.find(tag) != mParamsMap.end()) {
154 return mParamsMap.at(tag);
155 }
156 return std::nullopt;
157}
158
159template <typename TAG>
160ndk::ScopedAStatus EraserSwContext::setParam(TAG tag, Eraser eraser) {
161 mParamsMap[tag] = eraser;
162 return ndk::ScopedAStatus::ok();
163}
164
165IEffect::Status EraserSwContext::process(float* in, float* out, int samples) {
166 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
167 IEffect::Status status = {EX_ILLEGAL_ARGUMENT, 0, 0};
168
169 const auto inputChannelCount = getChannelCount(mCommon.input.base.channelMask);
170 const auto outputChannelCount = getChannelCount(mCommon.output.base.channelMask);
171 if (inputChannelCount < outputChannelCount) {
172 LOG(ERROR) << __func__ << " invalid channel count, in: " << inputChannelCount
173 << " out: " << outputChannelCount;
174 return status;
175 }
176
177 int iFrames = samples / inputChannelCount;
178 for (int i = 0; i < iFrames; i++) {
179 std::memcpy(out, in, outputChannelCount);
180 in += inputChannelCount;
181 out += outputChannelCount;
182 }
183 return {STATUS_OK, static_cast<int32_t>(iFrames * inputChannelCount),
184 static_cast<int32_t>(iFrames * outputChannelCount)};
185}
186
187} // namespace aidl::android::hardware::audio::effect