• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
18 #define ATRACE_TAG ATRACE_TAG_AUDIO
19 #define LOG_TAG "AHAL_EffectImpl"
20 #include <utils/Trace.h>
21 #include "effect-impl/EffectImpl.h"
22 #include "effect-impl/EffectTypes.h"
23 #include "include/effect-impl/EffectTypes.h"
24 
25 using aidl::android::hardware::audio::effect::IEffect;
26 using aidl::android::hardware::audio::effect::kEventFlagDataMqNotEmpty;
27 using aidl::android::hardware::audio::effect::kEventFlagNotEmpty;
28 using aidl::android::hardware::audio::effect::kReopenSupportedVersion;
29 using aidl::android::hardware::audio::effect::State;
30 using aidl::android::media::audio::common::PcmType;
31 using ::android::hardware::EventFlag;
32 
destroyEffect(const std::shared_ptr<IEffect> & instanceSp)33 extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) {
34     State state;
35     ndk::ScopedAStatus status = instanceSp->getState(&state);
36     if (!status.isOk() || State::INIT != state) {
37         LOG(ERROR) << __func__ << " instance " << instanceSp.get()
38                    << " in state: " << toString(state) << ", status: " << status.getDescription();
39         return EX_ILLEGAL_STATE;
40     }
41     return EX_NONE;
42 }
43 
44 namespace aidl::android::hardware::audio::effect {
45 
open(const Parameter::Common & common,const std::optional<Parameter::Specific> & specific,OpenEffectReturn * ret)46 ndk::ScopedAStatus EffectImpl::open(const Parameter::Common& common,
47                                     const std::optional<Parameter::Specific>& specific,
48                                     OpenEffectReturn* ret) {
49     // effect only support 32bits float
50     RETURN_IF(common.input.base.format.pcm != common.output.base.format.pcm ||
51                       common.input.base.format.pcm != PcmType::FLOAT_32_BIT,
52               EX_ILLEGAL_ARGUMENT, "dataMustBe32BitsFloat");
53 
54     std::lock_guard lg(mImplMutex);
55     RETURN_OK_IF(mState != State::INIT);
56     mImplContext = createContext(common);
57     RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
58 
59     RETURN_IF(!getInterfaceVersion(&mVersion).isOk(), EX_UNSUPPORTED_OPERATION,
60               "FailedToGetInterfaceVersion");
61     mImplContext->setVersion(mVersion);
62     mEventFlag = mImplContext->getStatusEventFlag();
63     mDataMqNotEmptyEf =
64             mVersion >= kReopenSupportedVersion ? kEventFlagDataMqNotEmpty : kEventFlagNotEmpty;
65 
66     if (specific.has_value()) {
67         RETURN_IF_ASTATUS_NOT_OK(setParameterSpecific(specific.value()), "setSpecParamErr");
68     }
69 
70     mState = State::IDLE;
71     mImplContext->dupeFmq(ret);
72     RETURN_IF(createThread(getEffectNameWithVersion()) != RetCode::SUCCESS,
73               EX_UNSUPPORTED_OPERATION, "FailedToCreateWorker");
74     LOG(INFO) << getEffectNameWithVersion() << __func__;
75     return ndk::ScopedAStatus::ok();
76 }
77 
reopen(OpenEffectReturn * ret)78 ndk::ScopedAStatus EffectImpl::reopen(OpenEffectReturn* ret) {
79     std::lock_guard lg(mImplMutex);
80     RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "alreadyClosed");
81 
82     // TODO: b/302036943 add reopen implementation
83     RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
84     mImplContext->dupeFmqWithReopen(ret);
85     return ndk::ScopedAStatus::ok();
86 }
87 
close()88 ndk::ScopedAStatus EffectImpl::close() {
89     {
90         std::lock_guard lg(mImplMutex);
91         RETURN_OK_IF(mState == State::INIT);
92         RETURN_IF(mState == State::PROCESSING, EX_ILLEGAL_STATE, "closeAtProcessing");
93         mState = State::INIT;
94     }
95 
96     RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
97               "notifyEventFlagNotEmptyFailed");
98     // stop the worker thread, ignore the return code
99     RETURN_IF(destroyThread() != RetCode::SUCCESS, EX_UNSUPPORTED_OPERATION,
100               "FailedToDestroyWorker");
101 
102     {
103         std::lock_guard lg(mImplMutex);
104         releaseContext();
105         mImplContext.reset();
106     }
107 
108     LOG(INFO) << getEffectNameWithVersion() << __func__;
109     return ndk::ScopedAStatus::ok();
110 }
111 
setParameter(const Parameter & param)112 ndk::ScopedAStatus EffectImpl::setParameter(const Parameter& param) {
113     std::lock_guard lg(mImplMutex);
114     LOG(VERBOSE) << getEffectNameWithVersion() << __func__ << " with: " << param.toString();
115 
116     const auto& tag = param.getTag();
117     switch (tag) {
118         case Parameter::common:
119         case Parameter::deviceDescription:
120         case Parameter::mode:
121         case Parameter::source:
122             FALLTHROUGH_INTENDED;
123         case Parameter::volumeStereo:
124             return setParameterCommon(param);
125         case Parameter::specific: {
126             return setParameterSpecific(param.get<Parameter::specific>());
127         }
128         default: {
129             LOG(ERROR) << getEffectNameWithVersion() << __func__ << " unsupportedParameterTag "
130                        << toString(tag);
131             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
132                                                                     "ParameterNotSupported");
133         }
134     }
135 }
136 
getParameter(const Parameter::Id & id,Parameter * param)137 ndk::ScopedAStatus EffectImpl::getParameter(const Parameter::Id& id, Parameter* param) {
138     std::lock_guard lg(mImplMutex);
139     switch (id.getTag()) {
140         case Parameter::Id::commonTag: {
141             RETURN_IF_ASTATUS_NOT_OK(getParameterCommon(id.get<Parameter::Id::commonTag>(), param),
142                                      "CommonParamNotSupported");
143             break;
144         }
145         case Parameter::Id::vendorEffectTag:
146             FALLTHROUGH_INTENDED;
147         default: {
148             Parameter::Specific specific;
149             RETURN_IF_ASTATUS_NOT_OK(getParameterSpecific(id, &specific), "SpecParamNotSupported");
150             param->set<Parameter::specific>(specific);
151             break;
152         }
153     }
154     LOG(VERBOSE) << getEffectNameWithVersion() << __func__ << id.toString() << param->toString();
155     return ndk::ScopedAStatus::ok();
156 }
157 
setParameterCommon(const Parameter & param)158 ndk::ScopedAStatus EffectImpl::setParameterCommon(const Parameter& param) {
159     RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
160 
161     const auto& tag = param.getTag();
162     switch (tag) {
163         case Parameter::common:
164             RETURN_IF(mImplContext->setCommon(param.get<Parameter::common>()) != RetCode::SUCCESS,
165                       EX_ILLEGAL_ARGUMENT, "setCommFailed");
166             break;
167         case Parameter::deviceDescription:
168             RETURN_IF(mImplContext->setOutputDevice(param.get<Parameter::deviceDescription>()) !=
169                               RetCode::SUCCESS,
170                       EX_ILLEGAL_ARGUMENT, "setDeviceFailed");
171             break;
172         case Parameter::mode:
173             RETURN_IF(mImplContext->setAudioMode(param.get<Parameter::mode>()) != RetCode::SUCCESS,
174                       EX_ILLEGAL_ARGUMENT, "setModeFailed");
175             break;
176         case Parameter::source:
177             RETURN_IF(mImplContext->setAudioSource(param.get<Parameter::source>()) !=
178                               RetCode::SUCCESS,
179                       EX_ILLEGAL_ARGUMENT, "setSourceFailed");
180             break;
181         case Parameter::volumeStereo:
182             RETURN_IF(mImplContext->setVolumeStereo(param.get<Parameter::volumeStereo>()) !=
183                               RetCode::SUCCESS,
184                       EX_ILLEGAL_ARGUMENT, "setVolumeStereoFailed");
185             break;
186         default: {
187             LOG(ERROR) << getEffectNameWithVersion() << __func__ << " unsupportedParameterTag "
188                        << toString(tag);
189             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
190                                                                     "commonParamNotSupported");
191         }
192     }
193     return ndk::ScopedAStatus::ok();
194 }
195 
getParameterCommon(const Parameter::Tag & tag,Parameter * param)196 ndk::ScopedAStatus EffectImpl::getParameterCommon(const Parameter::Tag& tag, Parameter* param) {
197     RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
198 
199     switch (tag) {
200         case Parameter::common: {
201             param->set<Parameter::common>(mImplContext->getCommon());
202             break;
203         }
204         case Parameter::deviceDescription: {
205             param->set<Parameter::deviceDescription>(mImplContext->getOutputDevice());
206             break;
207         }
208         case Parameter::mode: {
209             param->set<Parameter::mode>(mImplContext->getAudioMode());
210             break;
211         }
212         case Parameter::source: {
213             param->set<Parameter::source>(mImplContext->getAudioSource());
214             break;
215         }
216         case Parameter::volumeStereo: {
217             param->set<Parameter::volumeStereo>(mImplContext->getVolumeStereo());
218             break;
219         }
220         default: {
221             LOG(DEBUG) << getEffectNameWithVersion() << __func__ << " unsupported tag "
222                        << toString(tag);
223             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
224                                                                     "tagNotSupported");
225         }
226     }
227     return ndk::ScopedAStatus::ok();
228 }
229 
getState(State * state)230 ndk::ScopedAStatus EffectImpl::getState(State* state) NO_THREAD_SAFETY_ANALYSIS {
231     *state = mState;
232     return ndk::ScopedAStatus::ok();
233 }
234 
command(CommandId command)235 ndk::ScopedAStatus EffectImpl::command(CommandId command) {
236     std::lock_guard lg(mImplMutex);
237     RETURN_IF(mState == State::INIT, EX_ILLEGAL_STATE, "instanceNotOpen");
238 
239     switch (command) {
240         case CommandId::START:
241             RETURN_OK_IF(mState == State::PROCESSING);
242             RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
243             mState = State::PROCESSING;
244             RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
245                       "notifyEventFlagNotEmptyFailed");
246             startThread();
247             break;
248         case CommandId::STOP:
249         case CommandId::RESET:
250             RETURN_OK_IF(mState == State::IDLE);
251             mState = State::IDLE;
252             RETURN_IF(notifyEventFlag(mDataMqNotEmptyEf) != RetCode::SUCCESS, EX_ILLEGAL_STATE,
253                       "notifyEventFlagNotEmptyFailed");
254             stopThread();
255             RETURN_IF_ASTATUS_NOT_OK(commandImpl(command), "commandImplFailed");
256             break;
257         default:
258             LOG(ERROR) << getEffectNameWithVersion() << __func__ << " instance still processing";
259             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
260                                                                     "CommandIdNotSupported");
261     }
262     LOG(VERBOSE) << getEffectNameWithVersion() << __func__
263                  << " transfer to state: " << toString(mState);
264     return ndk::ScopedAStatus::ok();
265 }
266 
commandImpl(CommandId command)267 ndk::ScopedAStatus EffectImpl::commandImpl(CommandId command) {
268     RETURN_IF(!mImplContext, EX_NULL_POINTER, "nullContext");
269     if (command == CommandId::RESET) {
270         mImplContext->resetBuffer();
271     }
272     return ndk::ScopedAStatus::ok();
273 }
274 
createContext(const Parameter::Common & common)275 std::shared_ptr<EffectContext> EffectImpl::createContext(const Parameter::Common& common) {
276     return std::make_shared<EffectContext>(1 /* statusMqDepth */, common);
277 }
278 
releaseContext()279 RetCode EffectImpl::releaseContext() {
280     if (mImplContext) {
281         mImplContext.reset();
282     }
283     return RetCode::SUCCESS;
284 }
285 
cleanUp()286 void EffectImpl::cleanUp() {
287     command(CommandId::STOP);
288     close();
289 }
290 
notifyEventFlag(uint32_t flag)291 RetCode EffectImpl::notifyEventFlag(uint32_t flag) {
292     if (!mEventFlag) {
293         LOG(ERROR) << getEffectNameWithVersion() << __func__ << ": StatusEventFlag invalid";
294         return RetCode::ERROR_EVENT_FLAG_ERROR;
295     }
296     if (const auto ret = mEventFlag->wake(flag); ret != ::android::OK) {
297         LOG(ERROR) << getEffectNameWithVersion() << __func__ << ": wake failure with ret " << ret;
298         return RetCode::ERROR_EVENT_FLAG_ERROR;
299     }
300     LOG(VERBOSE) << getEffectNameWithVersion() << __func__ << ": " << std::hex << mEventFlag;
301     return RetCode::SUCCESS;
302 }
303 
status(binder_status_t status,size_t consumed,size_t produced)304 IEffect::Status EffectImpl::status(binder_status_t status, size_t consumed, size_t produced) {
305     IEffect::Status ret;
306     ret.status = status;
307     ret.fmqConsumed = consumed;
308     ret.fmqProduced = produced;
309     return ret;
310 }
311 
process()312 void EffectImpl::process() {
313     ATRACE_NAME(getEffectNameWithVersion().c_str());
314     /**
315      * wait for the EventFlag without lock, it's ok because the mEfGroup pointer will not change
316      * in the life cycle of workerThread (threadLoop).
317      */
318     uint32_t efState = 0;
319     if (!mEventFlag ||
320         ::android::OK != mEventFlag->wait(mDataMqNotEmptyEf, &efState, 0 /* no timeout */,
321                                           true /* retry */) ||
322         !(efState & mDataMqNotEmptyEf)) {
323         LOG(ERROR) << getEffectNameWithVersion() << __func__ << ": StatusEventFlag - " << mEventFlag
324                    << " efState - " << std::hex << efState;
325         return;
326     }
327 
328     {
329         std::lock_guard lg(mImplMutex);
330         if (mState != State::PROCESSING) {
331             LOG(DEBUG) << getEffectNameWithVersion()
332                        << " skip process in state: " << toString(mState);
333             return;
334         }
335         RETURN_VALUE_IF(!mImplContext, void(), "nullContext");
336         auto statusMQ = mImplContext->getStatusFmq();
337         auto inputMQ = mImplContext->getInputDataFmq();
338         auto outputMQ = mImplContext->getOutputDataFmq();
339         auto buffer = mImplContext->getWorkBuffer();
340         if (!inputMQ || !outputMQ) {
341             return;
342         }
343 
344         assert(mImplContext->getWorkBufferSize() >=
345                std::max(inputMQ->availableToRead(), outputMQ->availableToWrite()));
346         auto processSamples = std::min(inputMQ->availableToRead(), outputMQ->availableToWrite());
347         if (processSamples) {
348             inputMQ->read(buffer, processSamples);
349             IEffect::Status status = effectProcessImpl(buffer, buffer, processSamples);
350             outputMQ->write(buffer, status.fmqProduced);
351             statusMQ->writeBlocking(&status, 1);
352         }
353     }
354 }
355 
356 // A placeholder processing implementation to copy samples from input to output
effectProcessImpl(float * in,float * out,int samples)357 IEffect::Status EffectImpl::effectProcessImpl(float* in, float* out, int samples) {
358     for (int i = 0; i < samples; i++) {
359         *out++ = *in++;
360     }
361     return {STATUS_OK, samples, samples};
362 }
363 
364 }  // namespace aidl::android::hardware::audio::effect
365