1 /*
2  * Copyright (C) 2023 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 <cstddef>
18 #include <cstdint>
19 #include <cstring>
20 #include <optional>
21 #define LOG_TAG "EffectConversionHelperAidl"
22 //#define LOG_NDEBUG 0
23 
24 #include <error/expected_utils.h>
25 #include <media/AidlConversionCppNdk.h>
26 #include <media/AidlConversionNdk.h>
27 #include <media/AidlConversionEffect.h>
28 #include <media/AudioContainers.h>
29 #include <system/audio_effects/effect_visualizer.h>
30 
31 #include <utils/Log.h>
32 #include <Utils.h>
33 
34 #include "EffectConversionHelperAidl.h"
35 #include "EffectProxy.h"
36 
37 namespace android {
38 namespace effect {
39 
40 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
41 using ::aidl::android::hardware::audio::common::getChannelCount;
42 using ::aidl::android::hardware::audio::effect::CommandId;
43 using ::aidl::android::hardware::audio::effect::Descriptor;
44 using ::aidl::android::hardware::audio::effect::Flags;
45 using ::aidl::android::hardware::audio::effect::IEffect;
46 using ::aidl::android::hardware::audio::effect::Parameter;
47 using ::aidl::android::hardware::audio::effect::State;
48 using ::aidl::android::media::audio::common::AudioChannelLayout;
49 using ::aidl::android::media::audio::common::AudioDeviceDescription;
50 using ::aidl::android::media::audio::common::AudioMode;
51 using ::aidl::android::media::audio::common::AudioSource;
52 using android::effect::utils::EffectParamReader;
53 using android::effect::utils::EffectParamWriter;
54 using android::hardware::EventFlag;
55 
56 using ::android::status_t;
57 
58 const std::map<uint32_t /* effect_command_e */, EffectConversionHelperAidl::CommandHandler>
59         EffectConversionHelperAidl::mCommandHandlerMap = {
60                 {EFFECT_CMD_INIT, &EffectConversionHelperAidl::handleInit},
61                 {EFFECT_CMD_SET_PARAM, &EffectConversionHelperAidl::handleSetParameter},
62                 {EFFECT_CMD_GET_PARAM, &EffectConversionHelperAidl::handleGetParameter},
63                 {EFFECT_CMD_SET_CONFIG, &EffectConversionHelperAidl::handleSetConfig},
64                 {EFFECT_CMD_GET_CONFIG, &EffectConversionHelperAidl::handleGetConfig},
65                 {EFFECT_CMD_RESET, &EffectConversionHelperAidl::handleReset},
66                 {EFFECT_CMD_ENABLE, &EffectConversionHelperAidl::handleEnable},
67                 {EFFECT_CMD_DISABLE, &EffectConversionHelperAidl::handleDisable},
68                 {EFFECT_CMD_SET_AUDIO_MODE, &EffectConversionHelperAidl::handleSetAudioMode},
69                 {EFFECT_CMD_SET_AUDIO_SOURCE, &EffectConversionHelperAidl::handleSetAudioSource},
70                 {EFFECT_CMD_SET_DEVICE, &EffectConversionHelperAidl::handleSetDevice},
71                 {EFFECT_CMD_SET_INPUT_DEVICE, &EffectConversionHelperAidl::handleSetDevice},
72                 {EFFECT_CMD_SET_VOLUME, &EffectConversionHelperAidl::handleSetVolume},
73                 {EFFECT_CMD_OFFLOAD, &EffectConversionHelperAidl::handleSetOffload},
74                 // Only visualizer support these commands, reuse of EFFECT_CMD_FIRST_PROPRIETARY
75                 {VISUALIZER_CMD_CAPTURE, &EffectConversionHelperAidl::handleVisualizerCapture},
76                 {VISUALIZER_CMD_MEASURE, &EffectConversionHelperAidl::handleVisualizerMeasure}};
77 
EffectConversionHelperAidl(std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> effect,int32_t sessionId,int32_t ioId,const Descriptor & desc,bool isProxy)78 EffectConversionHelperAidl::EffectConversionHelperAidl(
79         std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> effect,
80         int32_t sessionId, int32_t ioId, const Descriptor& desc, bool isProxy)
81     : mSessionId(sessionId),
82       mIoId(ioId),
83       mDesc(desc),
84       mEffect(std::move(effect)),
85       mIsInputStream(mDesc.common.flags.type == Flags::Type::PRE_PROC),
86       mIsProxyEffect(isProxy) {
87     mCommon.session = sessionId;
88     mCommon.ioHandle = ioId;
89     mCommon.input = mCommon.output = kDefaultAudioConfig;
90 }
91 
handleCommand(uint32_t cmdCode,uint32_t cmdSize,void * pCmdData,uint32_t * replySize,void * pReplyData)92 status_t EffectConversionHelperAidl::handleCommand(uint32_t cmdCode, uint32_t cmdSize,
93                                                    void* pCmdData, uint32_t* replySize,
94                                                    void* pReplyData) {
95     const auto& handler = mCommandHandlerMap.find(cmdCode);
96     if (handler == mCommandHandlerMap.end() || !handler->second) {
97         ALOGE("%s handler for command %u doesn't exist", __func__, cmdCode);
98         return BAD_VALUE;
99     }
100     return (this->*handler->second)(cmdSize, pCmdData, replySize, pReplyData);
101 }
102 
handleInit(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)103 status_t EffectConversionHelperAidl::handleInit(uint32_t cmdSize __unused,
104                                                 const void* pCmdData __unused, uint32_t* replySize,
105                                                 void* pReplyData) {
106     if (!replySize || *replySize < sizeof(int) || !pReplyData) {
107         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
108               numericPointerToString(replySize).c_str(), pReplyData);
109         return BAD_VALUE;
110     }
111 
112     // Do nothing for EFFECT_CMD_INIT, call IEffect.open() with EFFECT_CMD_SET_CONFIG
113     return *(status_t*)pReplyData = OK;
114 }
115 
handleSetParameter(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)116 status_t EffectConversionHelperAidl::handleSetParameter(uint32_t cmdSize, const void* pCmdData,
117                                                         uint32_t* replySize, void* pReplyData) {
118     if (cmdSize < sizeof(effect_param_t) || !pCmdData || !replySize || *replySize < sizeof(int) ||
119         !pReplyData) {
120         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
121               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
122         return BAD_VALUE;
123     }
124 
125     auto reader = EffectParamReader(*(effect_param_t*)pCmdData);
126     if (!reader.validateCmdSize(cmdSize)) {
127         ALOGE("%s illegal param %s size %u", __func__, reader.toString().c_str(), cmdSize);
128         return BAD_VALUE;
129     }
130 
131     status_t ret = setParameter(reader);
132     EffectParamWriter writer(*(effect_param_t*)pReplyData);
133     writer.setStatus(ret);
134     return *(status_t*)pReplyData = ret;
135 }
136 
handleGetParameter(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)137 status_t EffectConversionHelperAidl::handleGetParameter(uint32_t cmdSize, const void* pCmdData,
138                                                         uint32_t* replySize, void* pReplyData) {
139     if (cmdSize < sizeof(effect_param_t) || !pCmdData || !replySize || !pReplyData) {
140         ALOGE("%s illegal cmdSize %u pCmdData %p replySize %s replyData %p", __func__, cmdSize,
141               pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
142         return BAD_VALUE;
143     }
144 
145     const auto reader = EffectParamReader(*(effect_param_t*)pCmdData);
146     if (*replySize < sizeof(effect_param_t) + reader.getParameterSize()) {
147         ALOGE("%s illegal param %s, replySize %u", __func__, reader.toString().c_str(), *replySize);
148         return BAD_VALUE;
149     }
150 
151     // copy effect_param_t and parameters
152     memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + reader.getParameterSize());
153     auto writer = EffectParamWriter(*(effect_param_t*)pReplyData);
154     status_t ret = getParameter(writer);
155     writer.finishValueWrite();
156     writer.setStatus(ret);
157     *replySize = writer.getTotalSize();
158     if (ret != OK) {
159         ALOGE("%s error ret %d, %s", __func__, ret, writer.toString().c_str());
160     }
161     return ret;
162 }
163 
handleSetConfig(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)164 status_t EffectConversionHelperAidl::handleSetConfig(uint32_t cmdSize, const void* pCmdData,
165                                                      uint32_t* replySize, void* pReplyData) {
166     if (!replySize || *replySize != sizeof(int) || !pReplyData ||
167         cmdSize != sizeof(effect_config_t)) {
168         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
169               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
170         return BAD_VALUE;
171     }
172 
173     effect_config_t* config = (effect_config_t*)pCmdData;
174     Parameter::Common common = {
175             .session = mCommon.session,
176             .ioHandle = mCommon.ioHandle,
177             .input =
178                     VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_buffer_config_t_AudioConfig(
179                             config->inputCfg, mIsInputStream)),
180             .output =
181                     VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_buffer_config_t_AudioConfig(
182                             config->outputCfg, mIsInputStream))};
183 
184     State state;
185     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getState(&state)));
186     if (state == State::INIT) {
187         ALOGD("%s at state %s, opening effect with input %s output %s", __func__,
188               android::internal::ToString(state).c_str(), common.input.toString().c_str(),
189               common.output.toString().c_str());
190         IEffect::OpenEffectReturn openReturn;
191         RETURN_STATUS_IF_ERROR(
192                 statusTFromBinderStatus(mEffect->open(common, std::nullopt, &openReturn)));
193         updateMqsAndEventFlags(openReturn);
194     } else if (mCommon != common) {
195         ALOGV("%s at state %s, setCommonParameter %s", __func__,
196               android::internal::ToString(state).c_str(), common.toString().c_str());
197         Parameter aidlParam = UNION_MAKE(Parameter, common, common);
198         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
199     }
200     mOutputAccessMode = config->outputCfg.accessMode;
201     mCommon = common;
202 
203     return *static_cast<int32_t*>(pReplyData) = OK;
204 }
205 
updateMqsAndEventFlags(const IEffect::OpenEffectReturn & ret)206 void EffectConversionHelperAidl::updateMqsAndEventFlags(const IEffect::OpenEffectReturn& ret) {
207     if (mIsProxyEffect) {
208         mStatusQ = std::static_pointer_cast<EffectProxy>(mEffect)->getStatusMQ();
209     } else {
210         mStatusQ = std::make_shared<StatusMQ>(ret.statusMQ);
211     }
212     updateEventFlags();
213     updateDataMqs(ret);
214 }
215 
updateDataMqs(const IEffect::OpenEffectReturn & ret)216 void EffectConversionHelperAidl::updateDataMqs(const IEffect::OpenEffectReturn& ret) {
217     if (mIsProxyEffect) {
218         mInputQ = std::static_pointer_cast<EffectProxy>(mEffect)->getInputMQ();
219         mOutputQ = std::static_pointer_cast<EffectProxy>(mEffect)->getOutputMQ();
220     } else {
221         mInputQ = std::make_shared<DataMQ>(ret.inputDataMQ);
222         mOutputQ = std::make_shared<DataMQ>(ret.outputDataMQ);
223     }
224 }
225 
handleGetConfig(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)226 status_t EffectConversionHelperAidl::handleGetConfig(uint32_t cmdSize __unused,
227                                                      const void* pCmdData __unused,
228                                                      uint32_t* replySize, void* pReplyData) {
229     if (!replySize || *replySize != sizeof(effect_config_t) || !pReplyData) {
230         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
231               numericPointerToString(replySize).c_str(), pReplyData);
232         return BAD_VALUE;
233     }
234 
235     Parameter param;
236     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(
237             Parameter::Id::make<Parameter::Id::commonTag>(Parameter::common), &param)));
238     if (param.getTag() != Parameter::common) {
239         *replySize = 0;
240         ALOGW("%s no valid common tag return from HAL: %s", __func__, param.toString().c_str());
241         return BAD_VALUE;
242     }
243 
244     const auto& common = param.get<Parameter::common>();
245     effect_config_t* pConfig = (effect_config_t*)pReplyData;
246     pConfig->inputCfg = VALUE_OR_RETURN_STATUS(
247             ::aidl::android::aidl2legacy_AudioConfig_buffer_config_t(common.input, true));
248     pConfig->outputCfg = VALUE_OR_RETURN_STATUS(
249             ::aidl::android::aidl2legacy_AudioConfig_buffer_config_t(common.output, false));
250     return OK;
251 }
252 
handleReset(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)253 status_t EffectConversionHelperAidl::handleReset(uint32_t cmdSize __unused,
254                                                  const void* pCmdData __unused, uint32_t* replySize,
255                                                  void* pReplyData) {
256     if (!replySize || *replySize != sizeof(int) || !pReplyData) {
257         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
258               numericPointerToString(replySize).c_str(), pReplyData);
259         return BAD_VALUE;
260     }
261 
262     return *(int *)pReplyData = statusTFromBinderStatus(mEffect->command(CommandId::RESET));
263 }
264 
handleEnable(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)265 status_t EffectConversionHelperAidl::handleEnable(uint32_t cmdSize __unused,
266                                                   const void* pCmdData __unused,
267                                                   uint32_t* replySize, void* pReplyData) {
268     if (!replySize || *replySize != sizeof(int) || !pReplyData) {
269         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
270               numericPointerToString(replySize).c_str(), pReplyData);
271         return BAD_VALUE;
272     }
273 
274     return *(int *)pReplyData = statusTFromBinderStatus(mEffect->command(CommandId::START));
275 }
276 
handleDisable(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)277 status_t EffectConversionHelperAidl::handleDisable(uint32_t cmdSize __unused,
278                                                    const void* pCmdData __unused,
279                                                    uint32_t* replySize, void* pReplyData) {
280     if (!replySize || *replySize != sizeof(int) || !pReplyData) {
281         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
282               numericPointerToString(replySize).c_str(), pReplyData);
283         return BAD_VALUE;
284     }
285 
286     return *(int *)pReplyData = statusTFromBinderStatus(mEffect->command(CommandId::STOP));
287 }
288 
handleSetAudioSource(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)289 status_t EffectConversionHelperAidl::handleSetAudioSource(uint32_t cmdSize, const void* pCmdData,
290                                                           uint32_t* replySize, void* pReplyData) {
291     if (cmdSize != sizeof(uint32_t) || !pCmdData || !replySize || !pReplyData) {
292         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
293               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
294         return BAD_VALUE;
295     }
296     if (!getDescriptor().common.flags.audioSourceIndication) {
297         ALOGW("%s parameter no audioSourceIndication, skipping", __func__);
298         return OK;
299     }
300 
301     audio_source_t source = *(audio_source_t*)pCmdData;
302     AudioSource aidlSource =
303             VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_source_t_AudioSource(source));
304     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
305             mEffect->setParameter(Parameter::make<Parameter::source>(aidlSource))));
306     return *static_cast<int32_t*>(pReplyData) = OK;
307 }
308 
handleSetAudioMode(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)309 status_t EffectConversionHelperAidl::handleSetAudioMode(uint32_t cmdSize, const void* pCmdData,
310                                                         uint32_t* replySize, void* pReplyData) {
311     if (cmdSize != sizeof(uint32_t) || !pCmdData || !replySize || !pReplyData) {
312         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
313               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
314         return BAD_VALUE;
315     }
316     if (!getDescriptor().common.flags.audioModeIndication) {
317         ALOGW("%s parameter no audioModeIndication, skipping", __func__);
318         return OK;
319     }
320     audio_mode_t mode = *(audio_mode_t *)pCmdData;
321     AudioMode aidlMode =
322             VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_audio_mode_t_AudioMode(mode));
323     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
324             mEffect->setParameter(Parameter::make<Parameter::mode>(aidlMode))));
325     return *static_cast<int32_t*>(pReplyData) = OK;
326 }
327 
handleSetDevice(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)328 status_t EffectConversionHelperAidl::handleSetDevice(uint32_t cmdSize, const void* pCmdData,
329                                                      uint32_t* replySize, void* pReplyData) {
330     if (cmdSize != sizeof(uint32_t) || !pCmdData || !replySize || !pReplyData) {
331         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
332               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
333         return BAD_VALUE;
334     }
335     if (!getDescriptor().common.flags.deviceIndication) {
336         ALOGW("%s parameter no deviceIndication, skipping", __func__);
337         return OK;
338     }
339     // convert from bitmask of audio_devices_t to std::vector<AudioDeviceDescription>
340     auto legacyDevices = *(uint32_t*)(pCmdData);
341     // extract the input bit and remove it from bitmasks
342     const auto inputBit = legacyDevices & AUDIO_DEVICE_BIT_IN;
343     legacyDevices &= ~AUDIO_DEVICE_BIT_IN;
344     std::vector<AudioDeviceDescription> aidlDevices;
345     while (legacyDevices) {
346         // get audio_devices_t represented by the last true bit and convert to AIDL
347         const auto lowestBitDevice = legacyDevices & -legacyDevices;
348         AudioDeviceDescription deviceDesc = VALUE_OR_RETURN_STATUS(
349                 ::aidl::android::legacy2aidl_audio_devices_t_AudioDeviceDescription(
350                         static_cast<audio_devices_t>(lowestBitDevice | inputBit)));
351         aidlDevices.emplace_back(deviceDesc);
352         legacyDevices -= lowestBitDevice;
353     }
354 
355     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
356             mEffect->setParameter(Parameter::make<Parameter::deviceDescription>(aidlDevices))));
357     return *static_cast<int32_t*>(pReplyData) = OK;
358 }
359 
handleSetVolume(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)360 status_t EffectConversionHelperAidl::handleSetVolume(uint32_t cmdSize, const void* pCmdData,
361                                                      uint32_t* replySize, void* pReplyData) {
362     if (cmdSize != 2 * sizeof(uint32_t) || !pCmdData) {
363         ALOGE("%s parameter invalid %u %p", __func__, cmdSize, pCmdData);
364         return BAD_VALUE;
365     }
366 
367     constexpr uint32_t unityGain = 1 << 24;
368     uint32_t vl = *(uint32_t*)pCmdData;
369     uint32_t vr = *((uint32_t*)pCmdData + 1);
370     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(
371             mEffect->setParameter(Parameter::make<Parameter::volumeStereo>(Parameter::VolumeStereo(
372                     {.left = (float)vl / unityGain, .right = (float)vr / unityGain})))));
373 
374     // get volume from effect and set if changed, return the volume in command if HAL not return
375     // correct parameter.
376     Parameter::Id id = Parameter::Id::make<Parameter::Id::commonTag>(Parameter::volumeStereo);
377     Parameter volParam;
378     const status_t getParamStatus = statusTFromBinderStatus(mEffect->getParameter(id, &volParam));
379     if (getParamStatus != OK || volParam.getTag() != Parameter::volumeStereo) {
380         ALOGW("%s no valid volume return from HAL, status %d: %s, return volume in command",
381               __func__, getParamStatus, volParam.toString().c_str());
382     } else {
383         Parameter::VolumeStereo appliedVolume = volParam.get<Parameter::volumeStereo>();
384         vl = (uint32_t)(appliedVolume.left * unityGain);
385         vr = (uint32_t)(appliedVolume.right * unityGain);
386     }
387 
388     if (replySize && *replySize == 2 * sizeof(uint32_t) && pReplyData) {
389         uint32_t vol_ret[2] = {vl, vr};
390         memcpy(pReplyData, vol_ret, sizeof(vol_ret));
391     }
392     return OK;
393 }
394 
handleSetOffload(uint32_t cmdSize,const void * pCmdData,uint32_t * replySize,void * pReplyData)395 status_t EffectConversionHelperAidl::handleSetOffload(uint32_t cmdSize, const void* pCmdData,
396                                                       uint32_t* replySize, void* pReplyData) {
397     if (cmdSize < sizeof(effect_offload_param_t) || !pCmdData || !replySize || !pReplyData) {
398         ALOGE("%s parameter invalid, cmdSize %u pCmdData %p replySize %s pReplyData %p", __func__,
399               cmdSize, pCmdData, numericPointerToString(replySize).c_str(), pReplyData);
400         return BAD_VALUE;
401     }
402     effect_offload_param_t* offload = (effect_offload_param_t*)pCmdData;
403     // send to proxy to update active sub-effect
404     if (mIsProxyEffect) {
405         ALOGV("%s offload param offload %s ioHandle %d", __func__,
406               offload->isOffload ? "true" : "false", offload->ioHandle);
407         const auto& effectProxy = std::static_pointer_cast<EffectProxy>(mEffect);
408         RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(effectProxy->setOffloadParam(offload)));
409         if (mCommon.ioHandle != offload->ioHandle) {
410             ALOGV("%s ioHandle update [%d to %d]", __func__, mCommon.ioHandle, offload->ioHandle);
411             mCommon.ioHandle = offload->ioHandle;
412             Parameter aidlParam = UNION_MAKE(Parameter, common, mCommon);
413             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
414         }
415         // update FMQs if the effect instance already open
416         if (State state; effectProxy->getState(&state).isOk() && state != State::INIT) {
417             IEffect::OpenEffectReturn openReturn;
418             updateMqsAndEventFlags(openReturn);
419         }
420     }
421     return *static_cast<int32_t*>(pReplyData) = OK;
422 }
423 
handleVisualizerCapture(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)424 status_t EffectConversionHelperAidl::handleVisualizerCapture(uint32_t cmdSize __unused,
425                                                              const void* pCmdData __unused,
426                                                              uint32_t* replySize,
427                                                              void* pReplyData) {
428     if (!replySize || !pReplyData) {
429         ALOGE("%s parameter invalid replySize %s pReplyData %p", __func__,
430               numericPointerToString(replySize).c_str(), pReplyData);
431         return BAD_VALUE;
432     }
433 
434     const auto& uuid = VALUE_OR_RETURN_STATUS(
435             ::aidl::android::aidl2legacy_AudioUuid_audio_uuid_t(mDesc.common.id.type));
436     if (0 != memcmp(&uuid, SL_IID_VISUALIZATION, sizeof(effect_uuid_t))) {
437         ALOGE("%s visualizer command not supported by %s", __func__,
438               mDesc.common.id.toString().c_str());
439         return BAD_VALUE;
440     }
441 
442     return visualizerCapture(replySize, pReplyData);
443 }
444 
handleVisualizerMeasure(uint32_t cmdSize __unused,const void * pCmdData __unused,uint32_t * replySize,void * pReplyData)445 status_t EffectConversionHelperAidl::handleVisualizerMeasure(uint32_t cmdSize __unused,
446                                                              const void* pCmdData __unused,
447                                                              uint32_t* replySize,
448                                                              void* pReplyData) {
449     if (!replySize || !pReplyData) {
450         ALOGE("%s parameter invalid, replySize %s pReplyData %p", __func__,
451               numericPointerToString(replySize).c_str(), pReplyData);
452         return BAD_VALUE;
453     }
454 
455     const auto& uuid = VALUE_OR_RETURN_STATUS(
456             ::aidl::android::aidl2legacy_AudioUuid_audio_uuid_t(mDesc.common.id.type));
457     if (0 != memcmp(&uuid, SL_IID_VISUALIZATION, sizeof(effect_uuid_t))) {
458         ALOGE("%s visualizer command not supported by %s", __func__,
459               mDesc.common.id.toString().c_str());
460         return BAD_VALUE;
461     }
462 
463     return visualizerMeasure(replySize, pReplyData);
464 }
465 
updateEventFlags()466 status_t EffectConversionHelperAidl::updateEventFlags() {
467     status_t status = BAD_VALUE;
468     EventFlag* efGroup = nullptr;
469     if (mStatusQ && mStatusQ->isValid()) {
470         status = EventFlag::createEventFlag(mStatusQ->getEventFlagWord(), &efGroup);
471         if (status != OK || !efGroup) {
472             ALOGE("%s: create EventFlagGroup failed, ret %d, egGroup %p", __func__, status,
473                   efGroup);
474             status = (status == OK) ? BAD_VALUE : status;
475         }
476     } else if (isBypassingOrTunnel()) {
477         // for effect with bypass (no processing) or offloadIndication flag, it's okay to not have
478         // statusQ
479         return OK;
480     }
481 
482     mEfGroup.reset(efGroup, EventFlagDeleter());
483     return status;
484 }
485 
isBypassingOrTunnel() const486 bool EffectConversionHelperAidl::isBypassingOrTunnel() const {
487     return isBypassing() || isTunnel();
488 }
489 
isBypassing() const490 bool EffectConversionHelperAidl::isBypassing() const {
491     return mEffect &&
492            (mDesc.common.flags.bypass ||
493             (mIsProxyEffect && std::static_pointer_cast<EffectProxy>(mEffect)->isBypassing()));
494 }
495 
isTunnel() const496 bool EffectConversionHelperAidl::isTunnel() const {
497     return mEffect &&
498            (mDesc.common.flags.hwAcceleratorMode == Flags::HardwareAccelerator::TUNNEL ||
499             (mIsProxyEffect && std::static_pointer_cast<EffectProxy>(mEffect)->isTunnel()));
500 }
501 
getDescriptor() const502 Descriptor EffectConversionHelperAidl::getDescriptor() const {
503     if (!mIsProxyEffect) {
504         return mDesc;
505     }
506 
507     Descriptor desc;
508     if (const auto status = mEffect->getDescriptor(&desc); !status.isOk()) {
509         ALOGE("%s failed to get proxy descriptor (%d:%s), using default", __func__,
510               status.getStatus(), status.getMessage());
511         return mDesc;
512     }
513     return desc;
514 }
515 
reopen()516 status_t EffectConversionHelperAidl::reopen() {
517     IEffect::OpenEffectReturn openReturn;
518     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->reopen(&openReturn)));
519 
520     // status MQ won't be changed after open
521     updateDataMqs(openReturn);
522     return OK;
523 }
524 
getAudioChannelCount() const525 size_t EffectConversionHelperAidl::getAudioChannelCount() const {
526     return getChannelCount(mCommon.input.base.channelMask,
527                            ~AudioChannelLayout::LAYOUT_HAPTIC_AB /* mask */);
528 }
529 
getHapticChannelCount() const530 size_t EffectConversionHelperAidl::getHapticChannelCount() const {
531     return getChannelCount(mCommon.input.base.channelMask,
532                            AudioChannelLayout::LAYOUT_HAPTIC_AB /* mask */);
533 }
534 
535 }  // namespace effect
536 }  // namespace android
537