blob: def3a3f3fb7d7f5175fa828e6ae4f26e2df1cfb5 [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 Naganov7cbf2f12016-10-27 20:05:35 -0700319 uint32_t halCmd = featureId;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800320 std::vector<uint32_t> halResult(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700321 uint32_t halResultSize = 0;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800322 return sendCommandReturningStatusAndData(
323 EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", sizeof(uint32_t), &halCmd,
324 &halResultSize, &halResult[0], sizeof(uint32_t), [&] { onSuccess(&halResult[1]); });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700325}
326
Kevin Rocard22505e62017-12-14 18:50:12 -0800327Result Effect::getParameterImpl(uint32_t paramSize, const void* paramData,
328 uint32_t requestValueSize, uint32_t replyValueSize,
329 GetParameterSuccessCallback onSuccess) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700330 // As it is unknown what method HAL uses for copying the provided parameter data,
331 // it is safer to make sure that input and output buffers do not overlap.
Mikhail Naganov4f110342022-07-07 20:37:42 +0000332 std::vector<uint8_t> halCmdBuffer;
333 if (!parameterToHal(paramSize, paramData, requestValueSize, nullptr, &halCmdBuffer)) {
334 return Result::INVALID_ARGUMENTS;
335 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800336 const void* valueData = nullptr;
Mikhail Naganov4f110342022-07-07 20:37:42 +0000337 std::vector<uint8_t> halParamBuffer;
338 if (!parameterToHal(paramSize, paramData, replyValueSize, &valueData, &halParamBuffer)) {
339 return Result::INVALID_ARGUMENTS;
340 }
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700341 uint32_t halParamBufferSize = halParamBuffer.size();
342
343 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800344 EFFECT_CMD_GET_PARAM, "GET_PARAM", halCmdBuffer.size(), &halCmdBuffer[0],
345 &halParamBufferSize, &halParamBuffer[0], sizeof(effect_param_t), [&] {
346 effect_param_t* halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]);
347 onSuccess(halParam->vsize, valueData);
348 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700349}
350
Kevin Rocard22505e62017-12-14 18:50:12 -0800351Result Effect::getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
352 GetSupportedConfigsSuccessCallback onSuccess) {
353 uint32_t halCmd[2] = {featureId, maxConfigs};
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700354 uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize);
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800355 std::vector<uint8_t> halResult(static_cast<size_t>(halResultSize), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700356 return sendCommandReturningStatusAndData(
Kevin Rocard22505e62017-12-14 18:50:12 -0800357 EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", sizeof(halCmd),
358 halCmd, &halResultSize, &halResult[0], 2 * sizeof(uint32_t), [&] {
359 uint32_t* halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]);
360 uint32_t supportedConfigs = *(++halResult32); // skip status field
361 if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs;
362 onSuccess(supportedConfigs, ++halResult32);
363 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700364}
365
Mikhail Naganova331de12017-01-04 16:33:55 -0800366Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
367 status_t status;
368 // Create message queue.
369 if (mStatusMQ) {
370 ALOGE("the client attempts to call prepareForProcessing_cb twice");
371 _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor());
372 return Void();
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700373 }
Mikhail Naganova331de12017-01-04 16:33:55 -0800374 std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/));
375 if (!tempStatusMQ->isValid()) {
376 ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid");
377 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
378 return Void();
379 }
380 status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
381 if (status != OK || !mEfGroup) {
382 ALOGE("failed creating event flag for status MQ: %s", strerror(-status));
383 _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor());
384 return Void();
385 }
386
387 // Create and launch the thread.
Kevin Rocard22505e62017-12-14 18:50:12 -0800388 mProcessThread = new ProcessThread(&mStopProcessThread, mHandle, &mHalInBufferPtr,
Andy Hung502f9d02022-04-26 18:37:36 -0700389 &mHalOutBufferPtr, tempStatusMQ.get(), mEfGroup, this);
Mikhail Naganova331de12017-01-04 16:33:55 -0800390 status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO);
391 if (status != OK) {
392 ALOGW("failed to start effect processing thread: %s", strerror(-status));
393 _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>());
394 return Void();
395 }
396
397 mStatusMQ = std::move(tempStatusMQ);
398 _hidl_cb(Result::OK, *mStatusMQ->getDesc());
399 return Void();
400}
401
Kevin Rocard22505e62017-12-14 18:50:12 -0800402Return<Result> Effect::setProcessBuffers(const AudioBuffer& inBuffer,
403 const AudioBuffer& outBuffer) {
Mikhail Naganova331de12017-01-04 16:33:55 -0800404 AudioBufferManager& manager = AudioBufferManager::getInstance();
405 sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer;
406 if (!manager.wrap(inBuffer, &tempInBuffer)) {
407 ALOGE("Could not map memory of the input buffer");
408 return Result::INVALID_ARGUMENTS;
409 }
410 if (!manager.wrap(outBuffer, &tempOutBuffer)) {
411 ALOGE("Could not map memory of the output buffer");
412 return Result::INVALID_ARGUMENTS;
413 }
414 mInBuffer = tempInBuffer;
415 mOutBuffer = tempOutBuffer;
416 // The processing thread only reads these pointers after waking up by an event flag,
417 // so it's OK to update the pair non-atomically.
418 mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release);
419 mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release);
420 return Result::OK;
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700421}
422
423Result Effect::sendCommand(int commandCode, const char* commandName) {
424 return sendCommand(commandCode, commandName, 0, NULL);
425}
426
Kevin Rocard22505e62017-12-14 18:50:12 -0800427Result Effect::sendCommand(int commandCode, const char* commandName, uint32_t size, void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700428 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL);
429 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
430}
431
Kevin Rocard22505e62017-12-14 18:50:12 -0800432Result Effect::sendCommandReturningData(int commandCode, const char* commandName,
433 uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700434 return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData);
435}
436
Kevin Rocard22505e62017-12-14 18:50:12 -0800437Result Effect::sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
438 void* data, uint32_t* replySize, void* replyData) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700439 uint32_t expectedReplySize = *replySize;
440 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
441 if (status == OK && *replySize != expectedReplySize) {
442 status = -ENODATA;
443 }
444 return analyzeCommandStatus(commandName, sContextCallToCommand, status);
445}
446
447Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) {
448 return sendCommandReturningStatus(commandCode, commandName, 0, NULL);
449}
450
Kevin Rocard22505e62017-12-14 18:50:12 -0800451Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
452 void* data) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700453 uint32_t replyCmdStatus;
454 uint32_t replySize = sizeof(uint32_t);
Kevin Rocard22505e62017-12-14 18:50:12 -0800455 return sendCommandReturningStatusAndData(commandCode, commandName, size, data, &replySize,
456 &replyCmdStatus, replySize, [] {});
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700457}
458
Kevin Rocard22505e62017-12-14 18:50:12 -0800459Result Effect::sendCommandReturningStatusAndData(int commandCode, const char* commandName,
460 uint32_t size, void* data, uint32_t* replySize,
461 void* replyData, uint32_t minReplySize,
462 CommandSuccessCallback onSuccess) {
463 status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700464 Result retval;
465 if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) {
466 uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData);
467 retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus);
468 if (commandStatus == OK) {
469 onSuccess();
470 }
471 } else {
472 retval = analyzeCommandStatus(commandName, sContextCallToCommand, status);
473 }
474 return retval;
475}
476
Kevin Rocard22505e62017-12-14 18:50:12 -0800477Result Effect::setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
478 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
479 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700480 effect_config_t halConfig;
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000481 EffectUtils::effectConfigToHal(config, &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700482 if (inputBufferProvider != 0) {
483 LOG_FATAL("Using input buffer provider is not supported");
484 }
485 if (outputBufferProvider != 0) {
486 LOG_FATAL("Using output buffer provider is not supported");
487 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800488 return sendCommandReturningStatus(commandCode, commandName, sizeof(effect_config_t),
489 &halConfig);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700490}
491
Kevin Rocard22505e62017-12-14 18:50:12 -0800492Result Effect::setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
493 const void* valueData) {
Mikhail Naganov4f110342022-07-07 20:37:42 +0000494 std::vector<uint8_t> halParamBuffer;
495 if (!parameterToHal(paramSize, paramData, valueSize, &valueData, &halParamBuffer)) {
496 return Result::INVALID_ARGUMENTS;
497 }
Kevin Rocard22505e62017-12-14 18:50:12 -0800498 return sendCommandReturningStatus(EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(),
499 &halParamBuffer[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700500}
501
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800502// Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
Kevin Rocard22505e62017-12-14 18:50:12 -0800503Return<Result> Effect::init() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700504 return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT");
505}
506
Kevin Rocard22505e62017-12-14 18:50:12 -0800507Return<Result> Effect::setConfig(const EffectConfig& config,
508 const sp<IEffectBufferProviderCallback>& inputBufferProvider,
509 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
510 return setConfigImpl(EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider,
511 outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700512}
513
Kevin Rocard22505e62017-12-14 18:50:12 -0800514Return<Result> Effect::reset() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700515 return sendCommand(EFFECT_CMD_RESET, "RESET");
516}
517
Kevin Rocard22505e62017-12-14 18:50:12 -0800518Return<Result> Effect::enable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700519 return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE");
520}
521
Kevin Rocard22505e62017-12-14 18:50:12 -0800522Return<Result> Effect::disable() {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700523 return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE");
524}
525
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800526Return<Result> Effect::setAudioSource(
527#if MAJOR_VERSION <= 6
528 AudioSource source
529#else
530 const AudioSource& source
531#endif
532) {
533 audio_source_t halSource;
534 if (status_t status = HidlUtils::audioSourceToHal(source, &halSource); status == NO_ERROR) {
535 uint32_t halSourceParam = static_cast<uint32_t>(halSource);
536 return sendCommand(EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t),
537 &halSourceParam);
538 } else {
539 return analyzeStatus(__func__, "audioSourceToHal", sContextConversion, status);
540 }
541}
542
543#if MAJOR_VERSION <= 6
544
Kevin Rocard30a7fcc2018-03-01 15:08:07 -0800545Return<Result> Effect::setDevice(AudioDeviceBitfield device) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700546 uint32_t halDevice = static_cast<uint32_t>(device);
547 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice);
548}
549
Mikhail Naganovf363ed42020-12-10 18:47:51 -0800550Return<Result> Effect::setInputDevice(AudioDeviceBitfield device) {
551 uint32_t halDevice = static_cast<uint32_t>(device);
552 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
553 &halDevice);
554}
555
556#else // MAJOR_VERSION <= 6
557
558Return<Result> Effect::setDevice(const DeviceAddress& device) {
559 audio_devices_t halDevice;
560 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
561 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
562 status == NO_ERROR) {
563 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
564 return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDeviceParam);
565 } else {
566 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
567 }
568}
569
570Return<Result> Effect::setInputDevice(const DeviceAddress& device) {
571 audio_devices_t halDevice;
572 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
573 if (status_t status = HidlUtils::deviceAddressToHal(device, &halDevice, halDeviceAddress);
574 status == NO_ERROR) {
575 uint32_t halDeviceParam = static_cast<uint32_t>(halDevice);
576 return sendCommand(EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t),
577 &halDeviceParam);
578 } else {
579 return analyzeStatus(__func__, "deviceAddressToHal", sContextConversion, status);
580 }
581}
582
583#endif // MAJOR_VERSION <= 6
584
Kevin Rocard22505e62017-12-14 18:50:12 -0800585Return<void> Effect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
586 setAndGetVolume_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700587 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800588 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700589 uint32_t halResultSize = halDataSize;
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800590 std::vector<uint32_t> halResult(volumes.size(), 0);
Kevin Rocard22505e62017-12-14 18:50:12 -0800591 Result retval = sendCommandReturningData(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800592 &halData[0], &halResultSize, &halResult[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700593 hidl_vec<uint32_t> result;
594 if (retval == Result::OK) {
595 result.setToExternal(&halResult[0], halResultSize);
596 }
597 _hidl_cb(retval, result);
598 return Void();
599}
600
Kevin Rocard22505e62017-12-14 18:50:12 -0800601Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800602 uint32_t halDataSize;
603 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize);
Kevin Rocard22505e62017-12-14 18:50:12 -0800604 return sendCommand(EFFECT_CMD_SET_VOLUME, "SET_VOLUME", halDataSize, &halData[0]);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800605}
606
Kevin Rocard22505e62017-12-14 18:50:12 -0800607Return<Result> Effect::setAudioMode(AudioMode mode) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700608 uint32_t halMode = static_cast<uint32_t>(mode);
Kevin Rocard22505e62017-12-14 18:50:12 -0800609 return sendCommand(EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700610}
611
612Return<Result> Effect::setConfigReverse(
Kevin Rocard22505e62017-12-14 18:50:12 -0800613 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
614 const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
615 return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", config,
616 inputBufferProvider, outputBufferProvider);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700617}
618
Kevin Rocard22505e62017-12-14 18:50:12 -0800619Return<void> Effect::getConfig(getConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700620 getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb);
621 return Void();
622}
623
Kevin Rocard22505e62017-12-14 18:50:12 -0800624Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700625 getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb);
626 return Void();
627}
628
Kevin Rocard22505e62017-12-14 18:50:12 -0800629Return<void> Effect::getSupportedAuxChannelsConfigs(uint32_t maxConfigs,
630 getSupportedAuxChannelsConfigs_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700631 hidl_vec<EffectAuxChannelsConfig> result;
632 Result retval = getSupportedConfigsImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800633 EFFECT_FEATURE_AUX_CHANNELS, maxConfigs, sizeof(channel_config_t),
634 [&](uint32_t supportedConfigs, void* configsData) {
635 result.resize(supportedConfigs);
636 channel_config_t* config = reinterpret_cast<channel_config_t*>(configsData);
637 for (size_t i = 0; i < result.size(); ++i) {
638 effectAuxChannelsConfigFromHal(*config++, &result[i]);
639 }
640 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700641 _hidl_cb(retval, result);
642 return Void();
643}
644
Kevin Rocard22505e62017-12-14 18:50:12 -0800645Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700646 EffectAuxChannelsConfig result;
647 Result retval = getCurrentConfigImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800648 EFFECT_FEATURE_AUX_CHANNELS, sizeof(channel_config_t), [&](void* configData) {
649 effectAuxChannelsConfigFromHal(*reinterpret_cast<channel_config_t*>(configData),
650 &result);
651 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700652 _hidl_cb(retval, result);
653 return Void();
654}
655
Kevin Rocard22505e62017-12-14 18:50:12 -0800656Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800657 std::vector<uint32_t> halCmd(
658 alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t)), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700659 halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS;
660 effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1]));
661 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG,
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800662 "SET_FEATURE_CONFIG AUX_CHANNELS", halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700663}
664
Kevin Rocard22505e62017-12-14 18:50:12 -0800665Return<Result> Effect::offload(const EffectOffloadParameter& param) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700666 effect_offload_param_t halParam;
667 effectOffloadParamToHal(param, &halParam);
Kevin Rocard22505e62017-12-14 18:50:12 -0800668 return sendCommandReturningStatus(EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t),
669 &halParam);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700670}
671
Kevin Rocard22505e62017-12-14 18:50:12 -0800672Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700673 effect_descriptor_t halDescriptor;
674 memset(&halDescriptor, 0, sizeof(effect_descriptor_t));
675 status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor);
676 EffectDescriptor descriptor;
677 if (status == OK) {
Mikhail Naganov5ec48c22021-01-15 19:05:04 +0000678 status = EffectUtils::effectDescriptorFromHal(halDescriptor, &descriptor);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700679 }
680 _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor);
681 return Void();
682}
683
Kevin Rocard22505e62017-12-14 18:50:12 -0800684Return<void> Effect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
685 uint32_t resultMaxSize, command_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700686 uint32_t halDataSize;
Mikhail Naganov6e81e9b2016-11-16 16:30:17 -0800687 std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700688 uint32_t halResultSize = resultMaxSize;
689 std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]);
690 memset(&halResult[0], 0, halResultSize);
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800691
692 void* dataPtr = halDataSize > 0 ? &halData[0] : NULL;
693 void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL;
Kevin Rocard22505e62017-12-14 18:50:12 -0800694 status_t status =
695 (*mHandle)->command(mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700696 hidl_vec<uint8_t> result;
Mikhail Naganovf4f2ff32017-01-19 12:38:39 -0800697 if (status == OK && resultPtr != NULL) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700698 result.setToExternal(&halResult[0], halResultSize);
699 }
700 _hidl_cb(status, result);
701 return Void();
702}
703
Kevin Rocard22505e62017-12-14 18:50:12 -0800704Return<Result> Effect::setParameter(const hidl_vec<uint8_t>& parameter,
705 const hidl_vec<uint8_t>& value) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700706 return setParameterImpl(parameter.size(), &parameter[0], value.size(), &value[0]);
707}
708
Kevin Rocard22505e62017-12-14 18:50:12 -0800709Return<void> Effect::getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
710 getParameter_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700711 hidl_vec<uint8_t> value;
712 Result retval = getParameterImpl(
Kevin Rocard22505e62017-12-14 18:50:12 -0800713 parameter.size(), &parameter[0], valueMaxSize,
714 [&](uint32_t valueSize, const void* valueData) {
715 value.setToExternal(reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)),
716 valueSize);
717 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700718 _hidl_cb(retval, value);
719 return Void();
720}
721
Kevin Rocard22505e62017-12-14 18:50:12 -0800722Return<void> Effect::getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
723 uint32_t configSize,
724 getSupportedConfigsForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700725 uint32_t configCount = 0;
726 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800727 Result retval = getSupportedConfigsImpl(featureId, maxConfigs, configSize,
728 [&](uint32_t supportedConfigs, void* configsData) {
729 configCount = supportedConfigs;
730 result.resize(configCount * configSize);
731 memcpy(&result[0], configsData, result.size());
732 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700733 _hidl_cb(retval, configCount, result);
734 return Void();
735}
736
Kevin Rocard22505e62017-12-14 18:50:12 -0800737Return<void> Effect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
738 getCurrentConfigForFeature_cb _hidl_cb) {
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700739 hidl_vec<uint8_t> result;
Kevin Rocard22505e62017-12-14 18:50:12 -0800740 Result retval = getCurrentConfigImpl(featureId, configSize, [&](void* configData) {
741 result.resize(configSize);
742 memcpy(&result[0], configData, result.size());
743 });
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700744 _hidl_cb(retval, result);
745 return Void();
746}
747
Kevin Rocard22505e62017-12-14 18:50:12 -0800748Return<Result> Effect::setCurrentConfigForFeature(uint32_t featureId,
749 const hidl_vec<uint8_t>& configData) {
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800750 std::vector<uint32_t> halCmd(alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size()), 0);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700751 halCmd[0] = featureId;
752 memcpy(&halCmd[1], &configData[0], configData.size());
Kevin Rocard22505e62017-12-14 18:50:12 -0800753 return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG",
Mikhail Naganov2a652ce2019-11-21 14:03:19 -0800754 halCmd.size(), &halCmd[0]);
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700755}
756
Mikhail Naganova331de12017-01-04 16:33:55 -0800757Return<Result> Effect::close() {
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800758 if (mStopProcessThread.load(std::memory_order_relaxed)) { // only this thread modifies
759 return Result::INVALID_STATE;
Mikhail Naganova331de12017-01-04 16:33:55 -0800760 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800761 mStopProcessThread.store(true, std::memory_order_release);
Mikhail Naganova331de12017-01-04 16:33:55 -0800762 if (mEfGroup) {
Mikhail Naganovb0abafb2017-01-31 17:24:48 -0800763 mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT));
Mikhail Naganova331de12017-01-04 16:33:55 -0800764 }
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800765#if MAJOR_VERSION <= 5
Mikhail Naganova331de12017-01-04 16:33:55 -0800766 return Result::OK;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800767#elif MAJOR_VERSION >= 6
768 // No need to join the processing thread, it is part of the API contract that the client
769 // must finish processing before closing the effect.
Mikhail Naganov532240f2019-12-04 16:18:50 -0800770 Result retval =
771 analyzeStatus("EffectRelease", "", sContextCallFunction, EffectRelease(mHandle));
772 EffectMap::getInstance().remove(mHandle);
773 return retval;
Mikhail Naganov7623ed92019-11-14 13:57:15 -0800774#endif
Mikhail Naganova331de12017-01-04 16:33:55 -0800775}
776
Mikhail Naganovfa021442019-02-22 14:28:26 -0800777Return<void> Effect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& /* options */) {
778 if (fd.getNativeHandle() != nullptr && fd->numFds == 1) {
779 uint32_t cmdData = fd->data[0];
780 (void)sendCommand(EFFECT_CMD_DUMP, "DUMP", sizeof(cmdData), &cmdData);
Andy Hung502f9d02022-04-26 18:37:36 -0700781 const std::string s = mStatistics->dump();
782 if (s.size() != 0) write(cmdData, s.c_str(), s.size());
Mikhail Naganovfa021442019-02-22 14:28:26 -0800783 }
784 return Void();
785}
786
Kevin Rocard22505e62017-12-14 18:50:12 -0800787} // namespace implementation
Kevin Rocard96d2cd92018-11-14 16:22:07 -0800788} // namespace CPP_VERSION
Mikhail Naganov7cbf2f12016-10-27 20:05:35 -0700789} // namespace effect
790} // namespace audio
791} // namespace hardware
792} // namespace android