blob: 5dc42dc70ddbf693a04e9e29b80dfd4f64716434 [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
Mikhail Naganov4f110342022-07-07 20:37:42 +0000241bool Effect::parameterToHal(uint32_t paramSize, const void* paramData, uint32_t valueSize,
242 const void** valueData, std::vector<uint8_t>* halParamBuffer) {
243 constexpr size_t kMaxSize = EFFECT_PARAM_SIZE_MAX - sizeof(effect_param_t);
244 if (paramSize > kMaxSize) {
245 ALOGE("%s: Parameter size is too big: %" PRIu32, __func__, paramSize);
246 return false;
247 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700248 size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t);
Mikhail Naganov4f110342022-07-07 20:37:42 +0000249 if (valueOffsetFromData > kMaxSize) {
250 ALOGE("%s: Aligned parameter size is too big: %zu", __func__, valueOffsetFromData);
251 return false;
252 }
253 if (valueSize > kMaxSize - valueOffsetFromData) {
254 ALOGE("%s: Value size is too big: %" PRIu32 ", max size is %zu", __func__, valueSize,
255 kMaxSize - valueOffsetFromData);
256 android_errorWriteLog(0x534e4554, "237291425");
257 return false;
258 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700259 size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize;
Mikhail Naganov4f110342022-07-07 20:37:42 +0000260 halParamBuffer->resize(halParamBufferSize, 0);
261 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(halParamBuffer->data());
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700262 halParam->psize = paramSize;
263 halParam->vsize = valueSize;
264 memcpy(halParam->data, paramData, paramSize);
265 if (valueData) {
266 if (*valueData) {
267 // Value data is provided.
268 memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize);
269 } else {
270 // The caller needs the pointer to the value data location.
271 *valueData = halParam->data + valueOffsetFromData;
272 }
273 }
Mikhail Naganov4f110342022-07-07 20:37:42 +0000274 return true;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700275}
276
277Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) {
278 return analyzeStatus("command", commandName, context, status);
279}
280
Kevin Rocard22505e62017-12-14 18:50:12 -0800281Result Effect::analyzeStatus(const char* funcName, const char* subFuncName,
282 const char* contextDescription, status_t status) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700283 if (status != OK) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800284 ALOGW("Effect %p %s %s %s: %s", mHandle, funcName, subFuncName, contextDescription,
285 strerror(-status));
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700286 }
287 switch (status) {
Kevin Rocard22505e62017-12-14 18:50:12 -0800288 case OK:
289 return Result::OK;
290 case -EINVAL:
291 return Result::INVALID_ARGUMENTS;
292 case -ENODATA:
293 return Result::INVALID_STATE;
294 case -ENODEV:
295 return Result::NOT_INITIALIZED;
296 case -ENOMEM:
297 return Result::RESULT_TOO_BIG;
298 case -ENOSYS:
299 return Result::NOT_SUPPORTED;
300 default:
301 return Result::INVALID_STATE;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700302 }
303}
304
305void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) {
306 uint32_t halResultSize = sizeof(effect_config_t);
Mikhail Naganov9f289042017-02-23 08:39:36 -0800307 effect_config_t halConfig{};
Kevin Rocard22505e62017-12-14 18:50:12 -0800308 status_t status =
309 (*mHandle)->command(mHandle, commandCode, 0, NULL, &halResultSize, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700310 EffectConfig config;
311 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000312 status = EffectUtils::effectConfigFromHal(halConfig, mIsInput, &config);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700313 }
314 cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config);
315}
316
Kevin Rocard22505e62017-12-14 18:50:12 -0800317Result Effect::getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
318 GetCurrentConfigSuccessCallback onSuccess) {
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000319 if (configSize > kMaxDataSize - sizeof(uint32_t)) {
320 ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
321 android_errorWriteLog(0x534e4554, "240266798");
322 return Result::INVALID_ARGUMENTS;
323 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700324 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800325 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700326 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800327 return sendCommandReturningStatusAndData(
328 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
329 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700330}
331
Kevin Rocard22505e62017-12-14 18:50:12 -0800332Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
333 uint32_t requestValueSize, uint32_t replyValueSize,
334 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700335 // As it is unknown what method HAL uses for copying the provided parameter data,
336 // it is safer to make sure that input and output buffers do not overlap.
Mikhail Naganov4f110342022-07-07 20:37:42 +0000337 std::vector<uint8_t> halCmdBuffer;
338 if (!parameterToHal(paramSize, paramData, requestValueSize, nullptr, &halCmdBuffer)) {
339 return Result::INVALID_ARGUMENTS;
340 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800341 const void* valueData = nullptr;
Mikhail Naganov4f110342022-07-07 20:37:42 +0000342 std::vector<uint8_t> halParamBuffer;
343 if (!parameterToHal(paramSize, paramData, replyValueSize, &valueData, &halParamBuffer)) {
344 return Result::INVALID_ARGUMENTS;
345 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700346 uint32_t halParamBufferSize = halParamBuffer.size();
347
348 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800349 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
350 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
351 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
352 onSuccess(halParam->vsize, valueData);
353 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700354}
355
Kevin Rocard22505e62017-12-14 18:50:12 -0800356Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
357 GetSupportedConfigsSuccessCallback onSuccess) {
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000358 if (maxConfigs != 0 && configSize > (kMaxDataSize - 2 * sizeof(uint32_t)) / maxConfigs) {
359 ALOGE("%s: Config size is too big: %" PRIu32, __func__, configSize);
360 return Result::INVALID_ARGUMENTS;
361 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800362 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov8e3480e2022-09-01 00:31:43 +0000363 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * configSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800364 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700365 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800366 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
367 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
368 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
369 uint32_t supportedConfigs = *(++halResult32); // skip status field
370 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
371 onSuccess(supportedConfigs, ++halResult32);
372 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700373}
374
Mikhail Naganova331de12017-01-04 16:33:55 -0800375Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
376 status_t status;
377 // Create message queue.
378 if (mStatusMQ) {
379 ALOGE("the client attempts to call prepareForProcessing_cb twice");
380 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
381 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700382 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800383 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
384 if (!tempStatusMQ->isValid()) {
385 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
386 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
387 return Void();
388 }
389 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
390 if (status != OK || !mEfGroup) {
391 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
392 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
393 return Void();
394 }
395
396 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800397 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
Andy Hung502f9d02022-04-26 18:37:36 -0700398 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup, this);
Mikhail Naganova331de12017-01-04 16:33:55 -0800399 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
400 if (status != OK) {
401 ALOGW("failed to start effect processing thread: %s", strerror(-status));
402 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
403 return Void();
404 }
405
406 mStatusMQ = std::move(tempStatusMQ);
407 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
408 return Void();
409}
410
Kevin Rocard22505e62017-12-14 18:50:12 -0800411Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
412 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800413 AudioBufferManager& manager = AudioBufferManager::getInstance();
414 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
415 if (!manager.wrap(inBuffer, &tempInBuffer)) {
416 ALOGE("Could not map memory of the input buffer");
417 return Result::INVALID_ARGUMENTS;
418 }
419 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
420 ALOGE("Could not map memory of the output buffer");
421 return Result::INVALID_ARGUMENTS;
422 }
423 mInBuffer = tempInBuffer;
424 mOutBuffer = tempOutBuffer;
425 // The processing thread only reads these pointers after waking up by an event flag,
426 // so it's OK to update the pair non-atomically.
427 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
428 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
429 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700430}
431
432Result Effect::sendCommand(int commandCode, const char* commandName) {
433 return sendCommand(commandCode, commandName, 0, NULL);
434}
435
Kevin Rocard22505e62017-12-14 18:50:12 -0800436Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700437 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
438 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
439}
440
Kevin Rocard22505e62017-12-14 18:50:12 -0800441Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
442 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700443 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
444}
445
Kevin Rocard22505e62017-12-14 18:50:12 -0800446Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
447 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700448 uint32_t expectedReplySize = *replySize;
449 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
450 if (status == OK && *replySize != expectedReplySize) {
451 status = -ENODATA;
452 }
453 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
454}
455
456Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
457 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
458}
459
Kevin Rocard22505e62017-12-14 18:50:12 -0800460Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
461 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700462 uint32_t replyCmdStatus;
463 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800464 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
465 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700466}
467
Kevin Rocard22505e62017-12-14 18:50:12 -0800468Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
469 uint32_t size, void* data, uint32_t* replySize,
470 void* replyData, uint32_t minReplySize,
471 CommandSuccessCallback onSuccess) {
472 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700473 Result retval;
474 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
475 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
476 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
477 if (commandStatus == OK) {
478 onSuccess();
479 }
480 } else {
481 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
482 }
483 return retval;
484}
485
Kevin Rocard22505e62017-12-14 18:50:12 -0800486Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
487 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
488 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700489 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000490 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700491 if (inputBufferProvider != 0) {
492 LOG_FATAL("Using input buffer provider is not supported");
493 }
494 if (outputBufferProvider != 0) {
495 LOG_FATAL("Using output buffer provider is not supported");
496 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800497 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
498 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700499}
500
Kevin Rocard22505e62017-12-14 18:50:12 -0800501Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
502 const void* valueData) {
Mikhail Naganov4f110342022-07-07 20:37:42 +0000503 std::vector<uint8_t> halParamBuffer;
504 if (!parameterToHal(paramSize, paramData, valueSize, &valueData, &halParamBuffer)) {
505 return Result::INVALID_ARGUMENTS;
506 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800507 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
508 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700509}
510
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800511// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800512Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700513 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
514}
515
Kevin Rocard22505e62017-12-14 18:50:12 -0800516Return<Result> Effect::setConfig(const EffectConfig& config,
517 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
518 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
519 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
520 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700521}
522
Kevin Rocard22505e62017-12-14 18:50:12 -0800523Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700524 return sendCommand(EFFECT_CMD_RESET, "RESET");
525}
526
Kevin Rocard22505e62017-12-14 18:50:12 -0800527Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700528 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
529}
530
Kevin Rocard22505e62017-12-14 18:50:12 -0800531Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700532 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
533}
534
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800535Return<Result> Effect::setAudioSource(
536#if MAJOR_VERSION <= 6
537 AudioSource source
538#else
539 const AudioSource& source
540#endif
541) {
542 audio_source_t halSource;
543 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
544 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
545 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
546 &halSourceParam);
547 } else {
548 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
549 }
550}
551
552#if MAJOR_VERSION <= 6
553
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800554Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700555 uint32_t halDevice = static_cast<uint32_t>(device);
556 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
557}
558
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800559Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
560 uint32_t halDevice = static_cast<uint32_t>(device);
561 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
562 &halDevice);
563}
564
565#else // MAJOR_VERSION <= 6
566
567Return<Result> Effect::setDevice(const DeviceAddress& device) {
568 audio_devices_t halDevice;
569 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
570 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
571 status == NO_ERROR) {
572 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
573 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
574 } else {
575 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
576 }
577}
578
579Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
580 audio_devices_t halDevice;
581 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
582 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
583 status == NO_ERROR) {
584 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
585 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
586 &halDeviceParam);
587 } else {
588 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
589 }
590}
591
592#endif // MAJOR_VERSION <= 6
593
Kevin Rocard22505e62017-12-14 18:50:12 -0800594Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
595 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700596 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800597 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700598 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800599 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800600 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800601 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700602 hidl_vec<uint32_t> result;
603 if (retval == Result::OK) {
604 result.setToExternal(&halResult[0], halResultSize);
605 }
606 _hidl_cb(retval, result);
607 return Void();
608}
609
Kevin Rocard22505e62017-12-14 18:50:12 -0800610Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800611 uint32_t halDataSize;
612 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800613 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800614}
615
Kevin Rocard22505e62017-12-14 18:50:12 -0800616Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700617 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800618 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700619}
620
621Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800622 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
623 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
624 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
625 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700626}
627
Kevin Rocard22505e62017-12-14 18:50:12 -0800628Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700629 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
630 return Void();
631}
632
Kevin Rocard22505e62017-12-14 18:50:12 -0800633Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700634 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
635 return Void();
636}
637
Kevin Rocard22505e62017-12-14 18:50:12 -0800638Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
639 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700640 hidl_vec<EffectAuxChannelsConfig> result;
641 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800642 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
643 [&](uint32_t supportedConfigs, void* configsData) {
644 result.resize(supportedConfigs);
645 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
646 for (size_t i = 0; i < result.size(); ++i) {
647 effectAuxChannelsConfigFromHal(*config++, &result[i]);
648 }
649 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700650 _hidl_cb(retval, result);
651 return Void();
652}
653
Kevin Rocard22505e62017-12-14 18:50:12 -0800654Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700655 EffectAuxChannelsConfig result;
656 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800657 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
658 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
659 &result);
660 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700661 _hidl_cb(retval, result);
662 return Void();
663}
664
Kevin Rocard22505e62017-12-14 18:50:12 -0800665Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800666 std::vector<uint32_t> halCmd(
667 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700668 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
669 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
670 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800671 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700672}
673
Kevin Rocard22505e62017-12-14 18:50:12 -0800674Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700675 effect_offload_param_t halParam;
676 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800677 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
678 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700679}
680
Kevin Rocard22505e62017-12-14 18:50:12 -0800681Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700682 effect_descriptor_t halDescriptor;
683 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
684 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
685 EffectDescriptor descriptor;
686 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000687 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700688 }
689 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
690 return Void();
691}
692
Kevin Rocard22505e62017-12-14 18:50:12 -0800693Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
694 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700695 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800696 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700697 uint32_t halResultSize = resultMaxSize;
698 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
699 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800700
701 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
702 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Andy Hung7d5eb5c2022-10-18 18:03:54 -0700703 status_t status = BAD_VALUE;
704 switch (commandId) {
705 case 'gtid': // retrieve the tid, used for spatializer priority boost
706 if (halDataSize == 0 && resultMaxSize == sizeof(int32_t)) {
707 auto ptid = (int32_t*)resultPtr;
708 ptid[0] = mProcessThread ? mProcessThread->getTid() : -1;
709 status = OK;
710 break; // we have handled 'gtid' here.
711 }
712 [[fallthrough]]; // allow 'gtid' overload (checked halDataSize and resultMaxSize).
713 default:
714 status = (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize,
715 resultPtr);
716 break;
717 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700718 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800719 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700720 result.setToExternal(&halResult[0], halResultSize);
721 }
722 _hidl_cb(status, result);
723 return Void();
724}
725
Kevin Rocard22505e62017-12-14 18:50:12 -0800726Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
727 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700728 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
729}
730
Kevin Rocard22505e62017-12-14 18:50:12 -0800731Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
732 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700733 hidl_vec<uint8_t> value;
734 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800735 parameter.size(), &parameter[0], valueMaxSize,
736 [&](uint32_t valueSize, const void* valueData) {
737 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
738 valueSize);
739 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700740 _hidl_cb(retval, value);
741 return Void();
742}
743
Kevin Rocard22505e62017-12-14 18:50:12 -0800744Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
745 uint32_t configSize,
746 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700747 uint32_t configCount = 0;
748 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800749 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
750 [&](uint32_t supportedConfigs, void* configsData) {
751 configCount = supportedConfigs;
752 result.resize(configCount * configSize);
753 memcpy(&result[0], configsData, result.size());
754 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700755 _hidl_cb(retval, configCount, result);
756 return Void();
757}
758
Kevin Rocard22505e62017-12-14 18:50:12 -0800759Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
760 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700761 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800762 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
763 result.resize(configSize);
764 memcpy(&result[0], configData, result.size());
765 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700766 _hidl_cb(retval, result);
767 return Void();
768}
769
Kevin Rocard22505e62017-12-14 18:50:12 -0800770Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
771 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800772 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700773 halCmd[0] = featureId;
774 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800775 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800776 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700777}
778
Mikhail Naganova331de12017-01-04 16:33:55 -0800779Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800780 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
781 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800782 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800783 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800784 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800785 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800786 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800787#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800788 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800789#elif MAJOR_VERSION >= 6
790 // No need to join the processing thread, it is part of the API contract that the client
791 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800792 Result retval =
793 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
794 EffectMap::getInstance().remove(mHandle);
795 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800796#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800797}
798
Mikhail Naganovfa021442019-02-22 14:28:26 -0800799Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
800 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
801 uint32_t cmdData = fd->data[0];
802 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
Andy Hung502f9d02022-04-26 18:37:36 -0700803 const std::string s = mStatistics->dump();
804 if (s.size() != 0) write(cmdData, s.c_str(), s.size());
Mikhail Naganovfa021442019-02-22 14:28:26 -0800805 }
806 return Void();
807}
808
Kevin Rocard22505e62017-12-14 18:50:12 -0800809} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800810} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700811} // namespace effect
812} // namespace audio
813} // namespace hardware
814} // namespace android