blob: 3baafc93434e377e46d230d1c17e970e29e674e7 [file] [log] [blame]
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07001/*
Kevin Rocard96d2cd92018-11-14 16:22:07 -08002 * Copyright (C) 2018 The Android Open Source Project
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -07003 *
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
Kevin Rocard96d2cd92018-11-14 16:22:07 -080017#include <memory.h>
18
19#define LOG_TAG "EffectHAL"
20#define ATRACE_TAG ATRACE_TAG_AUDIO
21
Kevin Rocard96d2cd92018-11-14 16:22:07 -080022#include "Effect.h"
23#include "common/all-versions/default/EffectMap.h"
Kevin Rocard62588b62017-12-20 11:07:12 -080024
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080025#define ATRACE_TAG ATRACE_TAG_AUDIO
Mikhail Naganovf363ed42020-12-10 18:47:51 -080026#include <HidlUtils.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080027#include <android/log.h>
Mikhail Naganova331de12017-01-04 16:33:55 -080028#include <media/EffectsFactoryApi.h>
Andy Hung502f9d02022-04-26 18:37:36 -070029#include <mediautils/ScopedStatistics.h>
Mikhail Naganov5ec48c22021-01-15 19:05:04 +000030#include <util/EffectUtils.h>
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080031#include <utils/Trace.h>
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070032
Kevin Rocard30a7fcc2018-03-01 15:08:07 -080033#include "VersionUtils.h"
34
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070035namespace android {
36namespace hardware {
37namespace audio {
38namespace effect {
Kevin Rocard96d2cd92018-11-14 16:22:07 -080039namespace CPP_VERSION {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -070040namespace implementation {
41
Mikhail Naganovf363ed42020-12-10 18:47:51 -080042#if MAJOR_VERSION <= 6
Mikhail Naganov7d015382022-01-15 01:15:12 +000043using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::
44 AudioChannelBitfield;
Mikhail Naganovf363ed42020-12-10 18:47:51 -080045#endif
Mikhail Naganov7d015382022-01-15 01:15:12 +000046using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
Mikhail Naganova331de12017-01-04 16:33:55 -080047
48namespace {
49
Andy Hung502f9d02022-04-26 18:37:36 -070050#define SCOPED_STATS() \
51 ::android::mediautils::ScopedStatistics scopedStatistics { \
52 std::string("EffectHal::").append(__func__), mEffectHal->mStatistics \
53 }
54
Mikhail Naganova331de12017-01-04 16:33:55 -080055class ProcessThread : public Thread {
Kevin Rocard22505e62017-12-14 18:50:12 -080056 public:
Mikhail Naganova331de12017-01-04 16:33:55 -080057 // ProcessThread's lifespan never exceeds Effect's lifespan.
Andy Hung502f9d02022-04-26 18:37:36 -070058 ProcessThread(std::atomic<bool>* stop, effect_handle_t effect,
59 std::atomic<audio_buffer_t*>* inBuffer, std::atomic<audio_buffer_t*>* outBuffer,
60 Effect::StatusMQ* statusMQ, EventFlag* efGroup, Effect* effectHal)
61 : Thread(false /*canCallJava*/),
62 mStop(stop),
63 mEffect(effect),
64 mHasProcessReverse((*mEffect)->process_reverse != NULL),
65 mInBuffer(inBuffer),
66 mOutBuffer(outBuffer),
67 mStatusMQ(statusMQ),
68 mEfGroup(efGroup),
69 mEffectHal(effectHal) {}
70 virtual ~ProcessThread() {}
Mikhail Naganova331de12017-01-04 16:33:55 -080071
Kevin Rocard22505e62017-12-14 18:50:12 -080072 private:
Mikhail Naganova331de12017-01-04 16:33:55 -080073 std::atomic<bool>* mStop;
74 effect_handle_t mEffect;
75 bool mHasProcessReverse;
76 std::atomic<audio_buffer_t*>* mInBuffer;
77 std::atomic<audio_buffer_t*>* mOutBuffer;
78 Effect::StatusMQ* mStatusMQ;
79 EventFlag* mEfGroup;
Andy Hung502f9d02022-04-26 18:37:36 -070080 Effect* const mEffectHal;
Mikhail Naganova331de12017-01-04 16:33:55 -080081
82 bool threadLoop() override;
83};
84
85bool ProcessThread::threadLoop() {
86 // This implementation doesn't return control back to the Thread until it decides to stop,
87 // as the Thread uses mutexes, and this can lead to priority inversion.
Kevin Rocard22505e62017-12-14 18:50:12 -080088 while (!std::atomic_load_explicit(mStop, std::memory_order_acquire)) {
Mikhail Naganova331de12017-01-04 16:33:55 -080089 uint32_t efState = 0;
Mikhail Naganove8674562017-02-10 08:37:19 -080090 mEfGroup->wait(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), &efState);
Kevin Rocard22505e62017-12-14 18:50:12 -080091 if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) ||
92 (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -080093 continue; // Nothing to do or time to quit.
Mikhail Naganova331de12017-01-04 16:33:55 -080094 }
95 Result retval = Result::OK;
Kevin Rocard22505e62017-12-14 18:50:12 -080096 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) &&
97 !mHasProcessReverse) {
Mikhail Naganova331de12017-01-04 16:33:55 -080098 retval = Result::NOT_SUPPORTED;
99 }
100
101 if (retval == Result::OK) {
102 // affects both buffer pointers and their contents.
103 std::atomic_thread_fence(std::memory_order_acquire);
104 int32_t processResult;
105 audio_buffer_t* inBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800106 std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800107 audio_buffer_t* outBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800108 std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed);
Mikhail Naganova331de12017-01-04 16:33:55 -0800109 if (inBuffer != nullptr && outBuffer != nullptr) {
Andy Hung502f9d02022-04-26 18:37:36 -0700110 // Time this effect process
111 SCOPED_STATS();
112
Mikhail Naganova331de12017-01-04 16:33:55 -0800113 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) {
114 processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer);
115 } else {
116 processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer);
117 }
118 std::atomic_thread_fence(std::memory_order_release);
119 } else {
120 ALOGE("processing buffers were not set before calling 'process'");
121 processResult = -ENODEV;
122 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800123 switch (processResult) {
124 case 0:
125 retval = Result::OK;
126 break;
127 case -ENODATA:
128 retval = Result::INVALID_STATE;
129 break;
130 case -EINVAL:
131 retval = Result::INVALID_ARGUMENTS;
132 break;
133 default:
134 retval = Result::NOT_INITIALIZED;
Mikhail Naganova331de12017-01-04 16:33:55 -0800135 }
136 }
137 if (!mStatusMQ->write(&retval)) {
138 ALOGW("status message queue write failed");
139 }
140 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING));
141 }
142
143 return false;
144}
145
146} // namespace
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700147
148// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800149const char* Effect::sContextResultOfCommand = "returned status";
150const char* Effect::sContextCallToCommand = "error";
151const char* Effect::sContextCallFunction = sContextCallToCommand;
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800152const char* Effect::sContextConversion = "conversion";
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700153
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800154Effect::Effect(bool isInput, effect_handle_t handle)
155 : mIsInput(isInput), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) {
156 (void)mIsInput; // prevent 'unused field' warnings in pre-V7 versions.
157}
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700158
159Effect::~Effect() {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800160 ATRACE_CALL();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800161 (void)close();
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800162 if (mProcessThread.get()) {
163 ATRACE_NAME("mProcessThread->join");
164 status_t status = mProcessThread->join();
165 ALOGE_IF(status, "processing thread exit error: %s", strerror(-status));
166 }
167 if (mEfGroup) {
168 status_t status = EventFlag::deleteEventFlag(&mEfGroup);
169 ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status));
170 }
171 mInBuffer.clear();
172 mOutBuffer.clear();
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800173#if MAJOR_VERSION <= 5
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800174 int status = EffectRelease(mHandle);
175 ALOGW_IF(status, "Error releasing effect %p: %s", mHandle, strerror(-status));
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800176#endif
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800177 EffectMap::getInstance().remove(mHandle);
178 mHandle = 0;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700179}
180
181// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800182template <typename T>
183size_t Effect::alignedSizeIn(size_t s) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700184 return (s + sizeof(T) - 1) / sizeof(T);
185}
186
187// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800188template <typename T>
189std::unique_ptr<uint8_t[]> Effect::hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize) {
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800190 // Due to bugs in HAL, they may attempt to write into the provided
191 // input buffer. The original binder buffer is r/o, thus it is needed
192 // to create a r/w version.
193 *halDataSize = vec.size() * sizeof(T);
194 std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]);
195 memcpy(&halData[0], &vec[0], *halDataSize);
196 return halData;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700197}
198
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800199#if MAJOR_VERSION <= 6
200
Kevin Rocard22505e62017-12-14 18:50:12 -0800201void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
202 EffectAuxChannelsConfig* config) {
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800203 config->mainChannels = AudioChannelBitfield(halConfig.main_channels);
204 config->auxChannels = AudioChannelBitfield(halConfig.aux_channels);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700205}
206
207// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800208void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
209 channel_config_t* halConfig) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700210 halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels);
211 halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels);
212}
213
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800214#else // MAJOR_VERSION <= 6
215
216void Effect::effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
217 EffectAuxChannelsConfig* config) {
218 (void)HidlUtils::audioChannelMaskFromHal(halConfig.main_channels, mIsInput,
219 &config->mainChannels);
220 (void)HidlUtils::audioChannelMaskFromHal(halConfig.aux_channels, mIsInput,
221 &config->auxChannels);
222}
223
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700224// static
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800225void Effect::effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
226 channel_config_t* halConfig) {
227 (void)HidlUtils::audioChannelMaskToHal(config.mainChannels, &halConfig->main_channels);
228 (void)HidlUtils::audioChannelMaskToHal(config.auxChannels, &halConfig->aux_channels);
229}
230
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800231#endif // MAJOR_VERSION <= 6
232
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700233// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800234void Effect::effectOffloadParamToHal(const EffectOffloadParameter& offload,
235 effect_offload_param_t* halOffload) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700236 halOffload->isOffload = offload.isOffload;
237 halOffload->ioHandle = offload.ioHandle;
238}
239
240// static
Kevin Rocard22505e62017-12-14 18:50:12 -0800241std::vector<uint8_t> Effect::parameterToHal(uint32_t paramSize, const void* paramData,
242 uint32_t valueSize, const void** valueData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700243 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
244 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
245 std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800246 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700247 halParam->psize = paramSize;
248 halParam->vsize = valueSize;
249 memcpy(halParam->data, paramData, paramSize);
250 if (valueData) {
251 if (*valueData) {
252 // Value data is provided.
253 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
254 } else {
255 // The caller needs the pointer to the value data location.
256 *valueData = halParam->data + valueOffsetFromData;
257 }
258 }
259 return halParamBuffer;
260}
261
262Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
263 return analyzeStatus("command", commandName, context, status);
264}
265
Kevin Rocard22505e62017-12-14 18:50:12 -0800266Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
267 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700268 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800269 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
270 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700271 }
272 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800273 case OK:
274 return Result::OK;
275 case -EINVAL:
276 return Result::INVALID_ARGUMENTS;
277 case -ENODATA:
278 return Result::INVALID_STATE;
279 case -ENODEV:
280 return Result::NOT_INITIALIZED;
281 case -ENOMEM:
282 return Result::RESULT_TOO_BIG;
283 case -ENOSYS:
284 return Result::NOT_SUPPORTED;
285 default:
286 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700287 }
288}
289
290void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
291 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800292 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800293 status_t status =
294 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700295 EffectConfig config;
296 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000297 status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700298 }
299 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
300}
301
Kevin Rocard22505e62017-12-14 18:50:12 -0800302Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
303 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700304 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800305 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700306 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800307 return sendCommandReturningStatusAndData(
308 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
309 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700310}
311
Kevin Rocard22505e62017-12-14 18:50:12 -0800312Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
313 uint32_t requestValueSize, uint32_t replyValueSize,
314 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700315 // As it is unknown what method HAL uses for copying the provided parameter data,
316 // it is safer to make sure that input and output buffers do not overlap.
317 std::vector<uint8_t> halCmdBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800318 parameterToHal(paramSize, paramData, requestValueSize, nullptr);
319 const void* valueData = nullptr;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700320 std::vector<uint8_t> halParamBuffer =
Kevin Rocard22505e62017-12-14 18:50:12 -0800321 parameterToHal(paramSize, paramData, replyValueSize, &valueData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700322 uint32_t halParamBufferSize = halParamBuffer.size();
323
324 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800325 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
326 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
327 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
328 onSuccess(halParam->vsize, valueData);
329 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700330}
331
Kevin Rocard22505e62017-12-14 18:50:12 -0800332Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
333 GetSupportedConfigsSuccessCallback onSuccess) {
334 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700335 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800336 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700337 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800338 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
339 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
340 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
341 uint32_t supportedConfigs = *(++halResult32); // skip status field
342 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
343 onSuccess(supportedConfigs, ++halResult32);
344 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700345}
346
Mikhail Naganova331de12017-01-04 16:33:55 -0800347Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
348 status_t status;
349 // Create message queue.
350 if (mStatusMQ) {
351 ALOGE("the client attempts to call prepareForProcessing_cb twice");
352 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
353 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700354 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800355 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
356 if (!tempStatusMQ->isValid()) {
357 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
358 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
359 return Void();
360 }
361 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
362 if (status != OK || !mEfGroup) {
363 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
364 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
365 return Void();
366 }
367
368 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800369 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
Andy Hung502f9d02022-04-26 18:37:36 -0700370 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup, this);
Mikhail Naganova331de12017-01-04 16:33:55 -0800371 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
372 if (status != OK) {
373 ALOGW("failed to start effect processing thread: %s", strerror(-status));
374 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
375 return Void();
376 }
377
378 mStatusMQ = std::move(tempStatusMQ);
379 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
380 return Void();
381}
382
Kevin Rocard22505e62017-12-14 18:50:12 -0800383Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
384 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800385 AudioBufferManager& manager = AudioBufferManager::getInstance();
386 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
387 if (!manager.wrap(inBuffer, &tempInBuffer)) {
388 ALOGE("Could not map memory of the input buffer");
389 return Result::INVALID_ARGUMENTS;
390 }
391 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
392 ALOGE("Could not map memory of the output buffer");
393 return Result::INVALID_ARGUMENTS;
394 }
395 mInBuffer = tempInBuffer;
396 mOutBuffer = tempOutBuffer;
397 // The processing thread only reads these pointers after waking up by an event flag,
398 // so it's OK to update the pair non-atomically.
399 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
400 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
401 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700402}
403
404Result Effect::sendCommand(int commandCode, const char* commandName) {
405 return sendCommand(commandCode, commandName, 0, NULL);
406}
407
Kevin Rocard22505e62017-12-14 18:50:12 -0800408Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700409 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
410 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
411}
412
Kevin Rocard22505e62017-12-14 18:50:12 -0800413Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
414 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700415 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
416}
417
Kevin Rocard22505e62017-12-14 18:50:12 -0800418Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
419 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700420 uint32_t expectedReplySize = *replySize;
421 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
422 if (status == OK && *replySize != expectedReplySize) {
423 status = -ENODATA;
424 }
425 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
426}
427
428Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
429 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
430}
431
Kevin Rocard22505e62017-12-14 18:50:12 -0800432Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
433 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700434 uint32_t replyCmdStatus;
435 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800436 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
437 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700438}
439
Kevin Rocard22505e62017-12-14 18:50:12 -0800440Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
441 uint32_t size, void* data, uint32_t* replySize,
442 void* replyData, uint32_t minReplySize,
443 CommandSuccessCallback onSuccess) {
444 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700445 Result retval;
446 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
447 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
448 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
449 if (commandStatus == OK) {
450 onSuccess();
451 }
452 } else {
453 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
454 }
455 return retval;
456}
457
Kevin Rocard22505e62017-12-14 18:50:12 -0800458Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
459 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
460 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700461 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000462 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700463 if (inputBufferProvider != 0) {
464 LOG_FATAL("Using input buffer provider is not supported");
465 }
466 if (outputBufferProvider != 0) {
467 LOG_FATAL("Using output buffer provider is not supported");
468 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800469 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
470 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700471}
472
Kevin Rocard22505e62017-12-14 18:50:12 -0800473Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
474 const void* valueData) {
475 std::vector<uint8_t> halParamBuffer =
476 parameterToHal(paramSize, paramData, valueSize, &valueData);
477 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
478 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700479}
480
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800481// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800482Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700483 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
484}
485
Kevin Rocard22505e62017-12-14 18:50:12 -0800486Return<Result> Effect::setConfig(const EffectConfig& config,
487 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
488 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
489 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
490 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700491}
492
Kevin Rocard22505e62017-12-14 18:50:12 -0800493Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700494 return sendCommand(EFFECT_CMD_RESET, "RESET");
495}
496
Kevin Rocard22505e62017-12-14 18:50:12 -0800497Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700498 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
499}
500
Kevin Rocard22505e62017-12-14 18:50:12 -0800501Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700502 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
503}
504
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800505Return<Result> Effect::setAudioSource(
506#if MAJOR_VERSION <= 6
507 AudioSource source
508#else
509 const AudioSource& source
510#endif
511) {
512 audio_source_t halSource;
513 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
514 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
515 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
516 &halSourceParam);
517 } else {
518 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
519 }
520}
521
522#if MAJOR_VERSION <= 6
523
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800524Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700525 uint32_t halDevice = static_cast<uint32_t>(device);
526 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
527}
528
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800529Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
530 uint32_t halDevice = static_cast<uint32_t>(device);
531 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
532 &halDevice);
533}
534
535#else // MAJOR_VERSION <= 6
536
537Return<Result> Effect::setDevice(const DeviceAddress& device) {
538 audio_devices_t halDevice;
539 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
540 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
541 status == NO_ERROR) {
542 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
543 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
544 } else {
545 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
546 }
547}
548
549Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
550 audio_devices_t halDevice;
551 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
552 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
553 status == NO_ERROR) {
554 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
555 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
556 &halDeviceParam);
557 } else {
558 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
559 }
560}
561
562#endif // MAJOR_VERSION <= 6
563
Kevin Rocard22505e62017-12-14 18:50:12 -0800564Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
565 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700566 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800567 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700568 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800569 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800570 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800571 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700572 hidl_vec<uint32_t> result;
573 if (retval == Result::OK) {
574 result.setToExternal(&halResult[0], halResultSize);
575 }
576 _hidl_cb(retval, result);
577 return Void();
578}
579
Kevin Rocard22505e62017-12-14 18:50:12 -0800580Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800581 uint32_t halDataSize;
582 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800583 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800584}
585
Kevin Rocard22505e62017-12-14 18:50:12 -0800586Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700587 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800588 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700589}
590
591Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800592 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
593 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
594 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
595 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700596}
597
Kevin Rocard22505e62017-12-14 18:50:12 -0800598Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700599 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
600 return Void();
601}
602
Kevin Rocard22505e62017-12-14 18:50:12 -0800603Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700604 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
605 return Void();
606}
607
Kevin Rocard22505e62017-12-14 18:50:12 -0800608Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
609 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700610 hidl_vec<EffectAuxChannelsConfig> result;
611 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800612 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
613 [&](uint32_t supportedConfigs, void* configsData) {
614 result.resize(supportedConfigs);
615 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
616 for (size_t i = 0; i < result.size(); ++i) {
617 effectAuxChannelsConfigFromHal(*config++, &result[i]);
618 }
619 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700620 _hidl_cb(retval, result);
621 return Void();
622}
623
Kevin Rocard22505e62017-12-14 18:50:12 -0800624Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700625 EffectAuxChannelsConfig result;
626 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800627 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
628 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
629 &result);
630 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700631 _hidl_cb(retval, result);
632 return Void();
633}
634
Kevin Rocard22505e62017-12-14 18:50:12 -0800635Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800636 std::vector<uint32_t> halCmd(
637 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700638 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
639 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
640 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800641 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700642}
643
Kevin Rocard22505e62017-12-14 18:50:12 -0800644Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700645 effect_offload_param_t halParam;
646 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800647 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
648 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700649}
650
Kevin Rocard22505e62017-12-14 18:50:12 -0800651Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700652 effect_descriptor_t halDescriptor;
653 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
654 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
655 EffectDescriptor descriptor;
656 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000657 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700658 }
659 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
660 return Void();
661}
662
Kevin Rocard22505e62017-12-14 18:50:12 -0800663Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
664 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700665 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800666 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700667 uint32_t halResultSize = resultMaxSize;
668 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
669 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800670
671 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
672 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800673 status_t status =
674 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700675 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800676 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700677 result.setToExternal(&halResult[0], halResultSize);
678 }
679 _hidl_cb(status, result);
680 return Void();
681}
682
Kevin Rocard22505e62017-12-14 18:50:12 -0800683Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
684 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700685 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
686}
687
Kevin Rocard22505e62017-12-14 18:50:12 -0800688Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
689 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700690 hidl_vec<uint8_t> value;
691 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800692 parameter.size(), &parameter[0], valueMaxSize,
693 [&](uint32_t valueSize, const void* valueData) {
694 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
695 valueSize);
696 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700697 _hidl_cb(retval, value);
698 return Void();
699}
700
Kevin Rocard22505e62017-12-14 18:50:12 -0800701Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
702 uint32_t configSize,
703 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700704 uint32_t configCount = 0;
705 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800706 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
707 [&](uint32_t supportedConfigs, void* configsData) {
708 configCount = supportedConfigs;
709 result.resize(configCount * configSize);
710 memcpy(&result[0], configsData, result.size());
711 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700712 _hidl_cb(retval, configCount, result);
713 return Void();
714}
715
Kevin Rocard22505e62017-12-14 18:50:12 -0800716Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
717 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700718 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800719 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
720 result.resize(configSize);
721 memcpy(&result[0], configData, result.size());
722 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700723 _hidl_cb(retval, result);
724 return Void();
725}
726
Kevin Rocard22505e62017-12-14 18:50:12 -0800727Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
728 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800729 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700730 halCmd[0] = featureId;
731 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800732 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800733 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700734}
735
Mikhail Naganova331de12017-01-04 16:33:55 -0800736Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800737 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
738 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800739 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800740 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800741 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800742 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800743 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800744#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800745 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800746#elif MAJOR_VERSION >= 6
747 // No need to join the processing thread, it is part of the API contract that the client
748 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800749 Result retval =
750 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
751 EffectMap::getInstance().remove(mHandle);
752 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800753#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800754}
755
Mikhail Naganovfa021442019-02-22 14:28:26 -0800756Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
757 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
758 uint32_t cmdData = fd->data[0];
759 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
Andy Hung502f9d02022-04-26 18:37:36 -0700760 const std::string s = mStatistics->dump();
761 if (s.size() != 0) write(cmdData, s.c_str(), s.size());
Mikhail Naganovfa021442019-02-22 14:28:26 -0800762 }
763 return Void();
764}
765
Kevin Rocard22505e62017-12-14 18:50:12 -0800766} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800767} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700768} // namespace effect
769} // namespace audio
770} // namespace hardware
771} // namespace android