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 #define LOG_TAG "AHAL_EffectImpl"
18 #include "effect-impl/EffectImpl.h"
19 #include "effect-impl/EffectTypes.h"
20 #include "include/effect-impl/EffectTypes.h"
21
22 using aidl::android::hardware::audio::effect::IEffect;
23 using aidl::android::hardware::audio::effect::State;
24 using aidl::android::media::audio::common::PcmType;
25
destroyEffect(const std::shared_ptr<IEffect> & instanceSp)26 extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) {
27 State state;
28 ndk::ScopedAStatus status = instanceSp->getState(&state);
29 if (!status.isOk() || State::INIT != state) {
30 LOG(ERROR) << __func__ << " instance " << instanceSp.get()
31 << " in state: " << toString(state) << ", status: " << status.getDescription();
32 return EX_ILLEGAL_STATE;
33 }
34 LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed";
35 return EX_NONE;
36 }
37
38 namespace aidl::android::hardware::audio::effect {
39
open(const Parameter::Common & common,const std::optional<Parameter::Specific> & specific,OpenEffectReturn * ret)40 ndk::ScopedAStatus EffectImpl::open(const Parameter::Common& common,
41 const std::optional<Parameter::Specific>& specific,
42 OpenEffectReturn* ret) {
43 LOG(DEBUG) << getEffectName() << __func__;
44 // effect only support 32bits float
45 RETURN_IF(common.input.base.format.pcm != common.output.base.format.pcm ||
46 common.input.base.format.pcm != PcmType::FLOAT_32_BIT,
47 EX_ILLEGAL_ARGUMENT, "dataMustBe32BitsFloat");
48 RETURN_OK_IF(mState != State::INIT);
49 auto context = createContext(common);
50 RETURN_IF(!context, EX_NULL_POINTER, "createContextFailed");
51
52 if (specific.has_value()) {
53 RETURN_IF_ASTATUS_NOT_OK(setParameterSpecific(specific.value()), "setSpecParamErr");
54 }
55
56 mState = State::IDLE;
57 context->dupeFmq(ret);
58 RETURN_IF(createThread(context, getEffectName()) != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
59 "FailedToCreateWorker");
60 return ndk::ScopedAStatus::ok();
61 }
62
close()63 ndk::ScopedAStatus EffectImpl::close() {
64 RETURN_OK_IF(mState == State::INIT);
65 RETURN_IF(mState == State::PROCESSING, EX_ILLEGAL_STATE, "closeAtProcessing");
66
67 // stop the worker thread, ignore the return code
68 RETURN_IF(destroyThread() != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
69 "FailedToDestroyWorker");
70 mState = State::INIT;
71 RETURN_IF(releaseContext() != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
72 "FailedToCreateWorker");
73
74 LOG(DEBUG) << getEffectName() << __func__;
75 return ndk::ScopedAStatus::ok();
76 }
77
setParameter(const Parameter & param)78 ndk::ScopedAStatus EffectImpl::setParameter(const Parameter& param) {
79 LOG(VERBOSE) << getEffectName() << __func__ << " with: " << param.toString();
80
81 const auto tag = param.getTag();
82 switch (tag) {
83 case Parameter::common:
84 case Parameter::deviceDescription:
85 case Parameter::mode:
86 case Parameter::source:
87 FALLTHROUGH_INTENDED;
88 case Parameter::volumeStereo:
89 return setParameterCommon(param);
90 case Parameter::specific: {
91 return setParameterSpecific(param.get<Parameter::specific>());
92 }
93 default: {
94 LOG(ERROR) << getEffectName() << __func__ << " unsupportedParameterTag "
95 << toString(tag);
96 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
97 "ParameterNotSupported");
98 }
99 }
100 }
101
getParameter(const Parameter::Id & id,Parameter * param)102 ndk::ScopedAStatus EffectImpl::getParameter(const Parameter::Id& id, Parameter* param) {
103 auto tag = id.getTag();
104 switch (tag) {
105 case Parameter::Id::commonTag: {
106 RETURN_IF_ASTATUS_NOT_OK(getParameterCommon(id.get<Parameter::Id::commonTag>(), param),
107 "CommonParamNotSupported");
108 break;
109 }
110 case Parameter::Id::vendorEffectTag:
111 FALLTHROUGH_INTENDED;
112 default: {
113 Parameter::Specific specific;
114 RETURN_IF_ASTATUS_NOT_OK(getParameterSpecific(id, &specific), "SpecParamNotSupported");
115 param->set<Parameter::specific>(specific);
116 break;
117 }
118 }
119 LOG(VERBOSE) << getEffectName() << __func__ << id.toString() << param->toString();
120 return ndk::ScopedAStatus::ok();
121 }
122
setParameterCommon(const Parameter & param)123 ndk::ScopedAStatus EffectImpl::setParameterCommon(const Parameter& param) {
124 auto context = getContext();
125 RETURN_IF(!context, EX_NULL_POINTER, "nullContext");
126
127 auto tag = param.getTag();
128 switch (tag) {
129 case Parameter::common:
130 RETURN_IF(context->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS,
131 EX_ILLEGAL_ARGUMENT, "setCommFailed");
132 break;
133 case Parameter::deviceDescription:
134 RETURN_IF(context->setOutputDevice(param.get<Parameter::deviceDescription>()) !=
135 RetCode::SUCCESS,
136 EX_ILLEGAL_ARGUMENT, "setDeviceFailed");
137 break;
138 case Parameter::mode:
139 RETURN_IF(context->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS,
140 EX_ILLEGAL_ARGUMENT, "setModeFailed");
141 break;
142 case Parameter::source:
143 RETURN_IF(context->setAudioSource(param.get<Parameter::source>()) != RetCode::SUCCESS,
144 EX_ILLEGAL_ARGUMENT, "setSourceFailed");
145 break;
146 case Parameter::volumeStereo:
147 RETURN_IF(context->setVolumeStereo(param.get<Parameter::volumeStereo>()) !=
148 RetCode::SUCCESS,
149 EX_ILLEGAL_ARGUMENT, "setVolumeStereoFailed");
150 break;
151 default: {
152 LOG(ERROR) << getEffectName() << __func__ << " unsupportedParameterTag "
153 << toString(tag);
154 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
155 "commonParamNotSupported");
156 }
157 }
158 return ndk::ScopedAStatus::ok();
159 }
160
getParameterCommon(const Parameter::Tag & tag,Parameter * param)161 ndk::ScopedAStatus EffectImpl::getParameterCommon(const Parameter::Tag& tag, Parameter* param) {
162 auto context = getContext();
163 RETURN_IF(!context, EX_NULL_POINTER, "nullContext");
164
165 switch (tag) {
166 case Parameter::common: {
167 param->set<Parameter::common>(context->getCommon());
168 break;
169 }
170 case Parameter::deviceDescription: {
171 param->set<Parameter::deviceDescription>(context->getOutputDevice());
172 break;
173 }
174 case Parameter::mode: {
175 param->set<Parameter::mode>(context->getAudioMode());
176 break;
177 }
178 case Parameter::source: {
179 param->set<Parameter::source>(context->getAudioSource());
180 break;
181 }
182 case Parameter::volumeStereo: {
183 param->set<Parameter::volumeStereo>(context->getVolumeStereo());
184 break;
185 }
186 default: {
187 LOG(DEBUG) << getEffectName() << __func__ << " unsupported tag " << toString(tag);
188 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
189 "tagNotSupported");
190 }
191 }
192 return ndk::ScopedAStatus::ok();
193 }
194
getState(State * state)195 ndk::ScopedAStatus EffectImpl::getState(State* state) {
196 *state = mState;
197 return ndk::ScopedAStatus::ok();
198 }
199
command(CommandId command)200 ndk::ScopedAStatus EffectImpl::command(CommandId command) {
201 RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "CommandStateError");
202 LOG(DEBUG) << getEffectName() << __func__ << ": receive command: " << toString(command)
203 << " at state " << toString(mState);
204
205 switch (command) {
206 case CommandId::START:
207 RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "instanceNotOpen");
208 RETURN_OK_IF(mState == State::PROCESSING);
209 RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
210 startThread();
211 mState = State::PROCESSING;
212 break;
213 case CommandId::STOP:
214 case CommandId::RESET:
215 RETURN_OK_IF(mState == State::IDLE);
216 stopThread();
217 RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
218 mState = State::IDLE;
219 break;
220 default:
221 LOG(ERROR) << getEffectName() << __func__ << " instance still processing";
222 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
223 "CommandIdNotSupported");
224 }
225 LOG(DEBUG) << getEffectName() << __func__ << " transfer to state: " << toString(mState);
226 return ndk::ScopedAStatus::ok();
227 }
228
commandImpl(CommandId command)229 ndk::ScopedAStatus EffectImpl::commandImpl(CommandId command) {
230 auto context = getContext();
231 RETURN_IF(!context, EX_NULL_POINTER, "nullContext");
232 if (command == CommandId::RESET) {
233 context->resetBuffer();
234 }
235 return ndk::ScopedAStatus::ok();
236 }
237
cleanUp()238 void EffectImpl::cleanUp() {
239 command(CommandId::STOP);
240 close();
241 }
242
status(binder_status_t status,size_t consumed,size_t produced)243 IEffect::Status EffectImpl::status(binder_status_t status, size_t consumed, size_t produced) {
244 IEffect::Status ret;
245 ret.status = status;
246 ret.fmqConsumed = consumed;
247 ret.fmqProduced = produced;
248 return ret;
249 }
250
251 // A placeholder processing implementation to copy samples from input to output
effectProcessImpl(float * in,float * out,int samples)252 IEffect::Status EffectImpl::effectProcessImpl(float* in, float* out, int samples) {
253 for (int i = 0; i < samples; i++) {
254 *out++ = *in++;
255 }
256 LOG(VERBOSE) << getEffectName() << __func__ << " done processing " << samples << " samples";
257 return {STATUS_OK, samples, samples};
258 }
259
260 } // namespace aidl::android::hardware::audio::effect
261